SVG/D3中轴转换的语法



我有一个难以置信的基本语法问题。我一直在学习D3,SVG和JavaScript,主要是通过编辑其他人的代码来挑战。

目标是在更新数据和比例后更新Y轴,该数据基于数据。我想要轴 - 键和标签,以及与数据域的全面过渡。轴未更新。该问题可能与范围有关,或者我正在引用错误的SVG元素。(实际上,有几个地块同时更新,但我只是在这里关注其中一个的轴。)

  
function makeYaxis (chart, scale, nticks, label, width, height, xmf, visName)
{
    var yAxis = d3.svg.axis()
    .scale(scale)
    .orient("left")
    .ticks(nticks);
    chart.append("svg:g")
    .attr("class","y axis")
    .append("g")
    .attr("transform","translate(60,1)") // fix magic #
    .call(yAxis);
    var xMove = xmf.yylMarginFactor * width - 1;
    var yMove = (((1 - xmf.xxbMarginFactor) * height + 
          xmf.xxtMarginFactor * height) / 2);
    chart.append("svg:text")
    .attr("class", visName + "xLabel")
    .attr("x", 0)
    .attr("y", 0)
    .attr("dy", "-2.8em")
    .text(label)
    .attr("transform", "rotate(-90) translate(-" + yMove + "," + xMove + ")");
}
function makeYscale (plotMargin, thedata, xmf)
{
    var dataMin = d3.min(thedata[0]);
    var dataMax = d3.max(thedata[0]);
    var yyMargin = d3.max([plotMargin * (dataMax - dataMin),0.05]);
    var yy = d3.scale.linear()
    .domain([dataMin - yyMargin, yyMargin + dataMax])
    .range([(1 - xmf.xxbMarginFactor) * height, xmf.xxtMarginFactor * height]);
    return yy;
}
// Set up this visualization
var chart = d3.select("#vis1")
  .append("svg:svg")
  .attr("class", "vis1chart")
  .attr("width", width)
  .attr("height", height);
var yy = makeYscale(plotMargin, thetas, xmf);
makeYaxis(chart, yy, nYTicks, "parameter", width, height, xmf, "vis1"); var points = chart.selectAll("circle.vis1points") .data(thetas) .enter().append("svg:circle") .attr("class", "vis1points") .attr("cx", function (d, i) { return xx(i); }) .attr("cy", function (d, i) { return yy(d); }) .attr("r", 3); points.transition() .attr("cx", function (d, i) { return xx(i); }) .attr("cy", yy) .duration(dur); vis1move(); function vis1move () { function movePoints () { var tmp = chart.selectAll(".vis1points").data(thetas[0], function (d, i) { return i; } ); var opa1 = points.style("stroke-opacity"); var opa2 = points.style("fill-opacity"); tmp .enter().insert("svg:circle") .attr("class", "vis1points") .attr("cx", function (d, i) { return xx(i); }) .attr("cy", yy) .attr("r", 3) .style("stroke-opacity", opa1) .style("fill-opacity", opa2); tmp.transition() .duration(10) .attr("cx", function (d, i) { return xx(i); }) tmp.exit() .transition().duration(10).attr("r",0).remove(); }; // (Code for updating theta, the data, goes here) // Update axes for this vis yy = makeYscale(plotMargin, thetas, xmf); var transition = chart.transition().duration(10); var yAxis = d3.svg.axis() .scale(yy) .orient("left") .ticks(4); transition.select("y axis").call(yAxis); movePoints(); // Previously had been before axis update (fixed) }

对不起,我无法获得这个独立的。

transition.select.("y axis").call(yAxis)正确吗?这里有什么明显的吗?


编辑:要保持简单,我现在有

 // Update axes for this vis
    yy = makeYscale(plotMargin, thetas, xmf);
    var yAxis = d3.svg.axis().scale(yy);
    chart.select("y axis").transition().duration(10).call(yAxis);

I'm wondering if something in the way I created the axis in the first place (in makeYaxis()) prevents me from selecting it properly. The yy function is returning the right values under the hood, and the data points are getting plotted and rescaled properly. It's just that the axis is "stuck."

Following meetamit's suggestions and the example here, I have stumbled on what appears to be a solution.

First, in function makeYaxis(), I removed the line .append("g"). I also updated the class name to yaxis. The function now reads

function makeYaxis (chart, scale, nticks, label, width, height, xmf, visName)
{
    var yAxis = d3.svg.axis()
    .scale(scale)
    .orient("left")
    .ticks(nticks);
    chart.append("svg:g")
    .attr("class","yaxis") // note new class name
//  .append("g")
    .attr("transform","translate(60,1)") 
    .call(yAxis);
    // everything else as before
}

I then added an extra period in my call to select(".yaxis"):

chart.select(".yaxis").transition().duration(10).call(yAxis);

如果有人可以向我解释为什么此解决方案似乎有效。

,我将非常感激。

最新更新