【问题标题】:how to add brush in nvd3 like d3如何像d3一样在nvd3中添加画笔
【发布时间】:2015-07-29 11:07:43
【问题描述】:
我正在 nvd3 FIDDLE 中创建图表
我已经完成了图表,它工作得很好,但现在我想像 d3 一样在其中添加 brush,请参阅此示例:http://bl.ocks.org/mbostock/1667367。但我到处搜索,我找到了一个解决方案,即crossfilter,但是是否可以使用像 d3 和 nvd3 这样的刷子有任何刷子功能?请帮我。
nv.addGraph(function() {
var chart = nv.models.multiBarChart();
chart.xAxis
.tickFormat(d3.format(',f'));
chart.yAxis
.tickFormat(d3.format(',.1f'));
chart.multibar.stacked(true); // default to stacked
chart.showControls(true); // don't show controls
d3.select('#chart svg')
.datum(test_data)
.transition().duration(500).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
【问题讨论】:
标签:
javascript
d3.js
nvd3.js
【解决方案1】:
我想知道如何使用交叉过滤器添加画笔?
这里的任何方式都是解决方案
nv.addGraph(function() {
var chart = nv.models.multiBarChart();
chart.xAxis
.tickFormat(d3.format(',f'));
chart.yAxis
.tickFormat(d3.format(',.1f'));
chart.multibar.stacked(true); // default to stacked
chart.showControls(true); // don't show controls
d3.select('#chart svg')
.datum(test_data)
.transition().duration(500).call(chart);
var brushC = d3.select('#chart svg g');
brushC.empty() ? brushC.append('g'): brushC.select('g')
.attr("class", "brush")
.call(d3.svg.brush()
.x(chart.xAxis.scale())
.on('brushend', onBrush)
.extent([0,0])
)
.selectAll('rect')
.attr('height',320 )//change according to you chart height
//i have hard coded it since it was a 'quick and dirty' fix
//you may try to get it from chart, if you can please update me.
;
}
nv.utils.windowResize(chart.update);
return chart;
});
onBrush 功能
function onBrush() {
brushExtent = d3.event.target.extent();
console.log(brushExtent);
}
添加 CSS
rect.extent{
color:grey;
opacity:0.4;
}