简体   繁体   English

D3 条形图按日期和上下文刷亮

[英]D3 bar chart by date with context brushing

I have a D3 bar chart with date/time on the X axis.我有一个 D3 条形图,X 轴上有日期/时间。 This is working well now, but I'd like to add a smaller brushable chart below it and am having trouble due to some of the date manipulations I had to do to make the bars center over the X axis tick lines - see this post for details on that .这现在运行良好,但我想在它下面添加一个较小的可刷图,但由于我必须进行一些日期操作以使条形图居中在 X 轴刻度线上,我遇到了麻烦 - 请参阅这篇文章细节

The brush does not seem to be calculating the X axis date range correctly.画笔似乎没有正确计算 X 轴日期范围。

Here is the x domain definition and brush function.这里是x域定义和刷机功能。 JSFiddle for the full code.完整代码的 JSFiddle。

main_x.domain([
    d3.min(data.result, function(d) { return d.date.setDate(d.date.getDate() - 1); }),                  
    d3.max(data.result, function(d) { return d.date.setDate(d.date.getDate() + 2); })
]);

function brushed() {

    // I thought that re-defining the x domain without the date manipulations might work, but I 
    // was getting some odd results
    //main_x.domain(d3.extent(data.result, function(d) { return d.date; }));

    main_x.domain(brush.empty() ? main_x.domain() : brush.extent());
    console.log(brush.extent());

    bar.selectAll("rect")
        .attr("width", function(d) { return main_width/len; })
        .attr("x", function(d) { return main_x(d.date) - (main_width/len)/2; });

    main.select(".x.axis").call(main_xAxis);
}

The problem is that you're using the same scale for the focus and the context chart.问题是您对焦点和上下文图表使用相同的比例。 As soon as you change that, the range of the selection changes (same size, but different underlying scale).一旦你改变它,选择的范围就会改变(相同的大小,但不同的基础比例)。 To fix, use two different scales.要修复,请使用两种不同的比例。

I've done this here , introducing mini_x as the x scale for the context chart.我已经在这里完成,引入mini_x作为上下文图表的 x 比例。

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

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