简体   繁体   English

D3更新条形图

[英]D3 Update bar chart

So I'm relatively new to D3, but I have been trouble shooting this issue for a few days, with no luck. 因此,我对D3还是比较陌生,但是几天来我一直很难解决这个问题,但是没有运气。 Wondering if I'm making a newbie mistake. 想知道我是否犯了新手错误。

I'm attempting to create a graph which represents volume vs. time, and allow the user to update the graph with a different date range. 我正在尝试创建一个表示体积与时间的关系图,并允许用户使用其他日期范围更新该图。

function updateVolumeGraph(data, div) {

//Get the data again
data.forEach(function(d) {
    d.startTime = new Date(d.startTime);
    d.totalVolume = d.totalVolume;
});

//Scale the range of the data again 
x.domain(d3.extent(data, function(d) { return d.startTime;}));
y.domain([0, d3.max(data, function(d) { return d.totalVolume; })]);

//Select the section we want to apply our changes to
var graphElement = d3.select(div).transition();

var bars = graphElement.selectAll(".bar").data(data); <<< Undefined

//Add some bars
bars.enter().append("rect")
             .attr("class", "bar")
              .attr("x", function(d) { return x(d.startTime); })
              .attr("y", function(d) { return y(d.totalVolume); })
              .attr("height", function(d) { return height - y(d.totalVolume); })
              .attr("width", barWidth);

    svg.select(".x.axis") // change the x axis
        .duration(750)
        .call(xAxisVolume);
    svg.select(".y.axis") // change the y axis
        .duration(750)
        .call(yAxisVolume);
};

My issue comes at the place where I have tagged it as "Undefined". 我的问题出在我将其标记为“未定义”的地方。 graphElement.selectAll(".bars") returns an array of "rects" but the .data(data) call is undefined. graphElement.selectAll(“。bars”)返回一个数组“ rects”,但是.data(data)调用未定义。

Any advice you could offer would be most appreciated! 您能提供的任何建议将不胜感激!

The issue you are having is that you are setting graphElement to the return value of you call to the transition method. 您遇到的问题是,您正在将graphElement设置为您对transition方法的调用的返回值。

var graphElement = d3.select(div).transition();

var bars = graphElement.selectAll(".bar").data(data);

Instead of chaining these methods, try calling it as a separate call after setting the graphElement variable. 在设置graphElement变量之后,尝试将其作为单独的调用而不是链接这些方法。

var graphElement = d3.select(div);
graphElement.transition();
var bars = graphElement.selectAll(".bar").data(data);

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

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