how to add brush in nvd3 like d3

夙愿已清 提交于 2019-12-24 16:15:40

问题


I am creating graph in nvd3 FIDDLE

I am done with graph and its working nicely but now I want to add brush in it like d3 see this example: http://bl.ocks.org/mbostock/1667367. but i searched every where, i found one solution i.e crossfilter but is it possible to use brush like d3 and nvd3 has any brush function ? please help me.

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


回答1:


I wonder how can you add brush using crossfilter?

Any way here is the solution

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

function onBrush() {
    brushExtent = d3.event.target.extent();
    console.log(brushExtent);
}

add CSS

rect.extent{
    color:grey;
    opacity:0.4;
}


来源:https://stackoverflow.com/questions/31698729/how-to-add-brush-in-nvd3-like-d3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!