简体   繁体   English

svg / d3中轴转换的语法

[英]Syntax for axis transition in svg/d3

I've an incredibly basic syntax question. 我有一个非常基本的语法问题。 I've been learning d3, SVG, and Javascript mostly by editing someone else's code, which is challenging. 我一直在学习d3,SVG和Javascript,主要是通过编辑别人的代码,这很有挑战性。

The goal is to update ay axis after updating the data and the scale, which is based on the data. 目标是在更新数据和基于数据的比例后更新y轴。 I want the axis--ticks and labels and all--to transition with the domain of the data. 我希望轴 - 刻度线和标签以及所有 - 转换为数据域。 The axis isn't getting updated. 轴未更新。 The problem might be related to scope, or I'm referencing the wrong SVG element. 问题可能与范围有关,或者我引用了错误的SVG元素。 (There are actually several plots getting updated simultaneously, but I'm just focusing on the axis of one of them here.) (实际上有几个地块同时更新,但我只关注其中一个的轴。)


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) }

Sorry I can't get this self-contained. 对不起,我不能得到这个自给自足的。

Is transition.select.("y axis").call(yAxis) correct? transition.select.("y axis").call(yAxis)是否正确? Is there anything glaringly off here? 这里有什么明显的东西吗?


Edit: To keep things simple, I now have 编辑:为了简单起见,我现在有了

 // 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. 我想知道我在第一个位置(在makeYaxis() )创建轴的方式是否阻止我正确选择它。 The yy function is returning the right values under the hood, and the data points are getting plotted and rescaled properly. yy函数返回引擎盖下的正确值,数据点将被正确绘制和重新缩放。 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. 根据meetamit的建议和这里的例子,我偶然发现了似乎是一个解决方案。

First, in function makeYaxis() , I removed the line .append("g") . 首先,在函数makeYaxis() ,我删除了行.append("g") I also updated the class name to yaxis . 我还将类名更新为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") : 然后我在我的select(".yaxis")调用中添加了一个额外的句点:

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

I would be very grateful if anyone could explain to me exactly why this solution appears to work. 如果有人能够向我解释为什么这个解决方案似乎有效,我将非常感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM