简体   繁体   English

D3.js 条形图“输入”似乎在错误的位置绘制新条形

[英]D3.js bar chart 'enter' appears to draw the new bars in wrong position

I have been following several tutorials / articles on D3.js to create a bar chart with drill-through type functionality.我一直在关注 D3.js 上的几篇教程/文章,以创建具有钻取类型功能的条形图。 I have it working pretty much how I want it but have a problem that I just can't seem to figure out.我让它几乎按照我想要的方式工作,但有一个我似乎无法弄清楚的问题。

I have an example that I am working on here (this is cut down version from the main one I'm working on, just to show the issue): http://jsfiddle.net/MattFrewin/bobec00k/1/我有一个我在这里工作的例子(这是我正在处理的主要版本的缩减版本,只是为了显示问题): http : //jsfiddle.net/MattFrewin/bobec00k/1/

You can see that the initial bar chart shows 1 bar for 'Team A', you can click on this to drill through to the next set of data - which shows 3 bars of data.您可以看到初始条形图为“团队 A”显示 1 个条形图,您可以单击此条形图深入查看下一组数据 - 显示 3 个数据条形。 The problem is that the new bars are all slightly out of place.问题是新的酒吧都有点不合适。 You will notice that I am using a margin for the chart axis which offsets the bars, and from what I can tell it is this that is not being taken into account on the new bars.您会注意到,我正在使用偏移条形图的图表轴的边距,据我所知,新条形图中没有考虑到这一点。 Here is the bit of code which I think is where the issues is:这是我认为问题所在的一些代码:

//Create bars
chartBarData.enter()
  .append("rect")
    .attr({
        "x": function (d) { return xScale(d.key); },
        "y": height,
        "width": xScale.rangeBand(),
        "height": 0,
        "fill": function (d) { return "rgb(0, 130, 0)"; }
    })
    .transition().delay(animDur).duration(animDur)
    .attr({
        "x": function (d) { return xScale(d.key); },
        "y": function (d) { return yScale(d.values.count); },
        "width": xScale.rangeBand(),
        "height": function (d) { return height - yScale(d.values.count); },
        "fill": function (d) { return "rgb(0, 130, 0)"; }
    });

I have found a way around it by putting the margins into the 'x' and 'y' parts of the 'rect' attributes but this causes further similar issues when drilling further down into the data.我找到了一种解决方法,将边距放入 'rect' 属性的 'x' 和 'y' 部分,但这会在进一步深入数据时导致进一步的类似问题。 I'm sure there is something obvious I am missing or probably just do not quite understand D3 fully yet!我敢肯定我遗漏了一些明显的东西,或者可能只是还没有完全理解 D3! Any help is appreciated.任何帮助表示赞赏。

EDIT: I should also add that the D3 'update' section appears to work fine, using the same scales as the 'enter' section - which is confusing!编辑:我还应该补充一点,D3“更新”部分似乎工作正常,使用与“输入”部分相同的比例 - 这令人困惑! As you will see on the jsfiddle link, the first bar is lined up correctly when you drill through but the other two are not.正如您将在 jsfiddle 链接上看到的那样,当您钻取时,第一个条形排列正确,但其他两个没有。

It seems bars are not inserted into the proper location, in chart_drilldown function try to replace:似乎 bar 没有插入正确的位置,在chart_drilldown函数中尝试替换:

var chartBarData = theChart.selectAll("rect")
        .data(groupedData);

with

var chartBarData = theChart.select("g").selectAll("rect")
        .data(groupedData);

Modified JSFiddle修改后的JSFiddle

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

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