javascript - How to use custom legends in NVD3 charts - Stack Overflow

I am using an NVD3 line chart.The chart by default renders legends which are clickable.I want to build

I am using an NVD3 line chart.

The chart by default renders legends which are clickable.

I want to build a custom legend using <ul> and <li> elements; clicking these <li>s should toggle their respective line series on the chart.

$(function(){
    nv.addGraph(function() {
        var dayChart = nv.models.lineChart()
            .options({
                transitionDuration: 300,
                useInteractiveGuideline: true,
                interpolate: 'monotone'
            });

        dayChart.xAxis
            .axisLabel('Time')
            .tickValues([0, 1, 2, 3, 4])
            .tickFormat(function(d){
                return ["", "0-6", "6-12", "12-18", "18-24"][d]
            });

        dayChart.yAxis
            .axisLabel('Engagement')
            .tickFormat(function(d) {
                if (d == null) {
                    return 'N/A';
                }
                return d3.format(',d')(d);
            })
        ;

        var data = [
                {"values": [
                    { "x": 0 ,"y": 3 }, { "x": 1 ,"y": 5 }, { "x": 2 ,"y": 2 }, { "x": 3 ,"y": 4 }, { "x": 4 ,"y": 2 }],    "key": "Desktop","color":"#4b758d"
                }
                ,
                {"values": [
                    { "x":  0 ,"y": 1 },    { "x": 1 ,"y": 3 }, { "x": 2 ,"y": 4 }, { "x": 3 ,"y": 3 }],    "key": "Mobile","color":"#99c925"                },
                {"values": [
                    { "x": 0  ,"y": 2 },{"x": 1  ,"y": 4 }, { "x": 2 ,"y": 3 }, { "x": 3 ,"y": 5 }],"key": "Tablet","color":"#f23b71"                }
            ];

        d3.select('#line-chart').append('svg')
            .datum(data)
            .call(dayChart);

        nv.utils.windowResize(dayChart.update);

        return dayChart;
    });
});

Plunker for full demo.

I am using an NVD3 line chart.

The chart by default renders legends which are clickable.

I want to build a custom legend using <ul> and <li> elements; clicking these <li>s should toggle their respective line series on the chart.

$(function(){
    nv.addGraph(function() {
        var dayChart = nv.models.lineChart()
            .options({
                transitionDuration: 300,
                useInteractiveGuideline: true,
                interpolate: 'monotone'
            });

        dayChart.xAxis
            .axisLabel('Time')
            .tickValues([0, 1, 2, 3, 4])
            .tickFormat(function(d){
                return ["", "0-6", "6-12", "12-18", "18-24"][d]
            });

        dayChart.yAxis
            .axisLabel('Engagement')
            .tickFormat(function(d) {
                if (d == null) {
                    return 'N/A';
                }
                return d3.format(',d')(d);
            })
        ;

        var data = [
                {"values": [
                    { "x": 0 ,"y": 3 }, { "x": 1 ,"y": 5 }, { "x": 2 ,"y": 2 }, { "x": 3 ,"y": 4 }, { "x": 4 ,"y": 2 }],    "key": "Desktop","color":"#4b758d"
                }
                ,
                {"values": [
                    { "x":  0 ,"y": 1 },    { "x": 1 ,"y": 3 }, { "x": 2 ,"y": 4 }, { "x": 3 ,"y": 3 }],    "key": "Mobile","color":"#99c925"                },
                {"values": [
                    { "x": 0  ,"y": 2 },{"x": 1  ,"y": 4 }, { "x": 2 ,"y": 3 }, { "x": 3 ,"y": 5 }],"key": "Tablet","color":"#f23b71"                }
            ];

        d3.select('#line-chart').append('svg')
            .datum(data)
            .call(dayChart);

        nv.utils.windowResize(dayChart.update);

        return dayChart;
    });
});

Plunker for full demo.

Share Improve this question edited Nov 12, 2015 at 3:20 approxiblue 7,12216 gold badges52 silver badges59 bronze badges asked Nov 1, 2015 at 12:09 PradeepPradeep 1,2721 gold badge19 silver badges38 bronze badges 1
  • Can you please add code over the fiddle so will help you – Mitul Commented Nov 4, 2015 at 6:48
Add a ment  | 

2 Answers 2

Reset to default 4

To disable the legend chart.showLedgend(false). More details on how the entire code looks here.

Assuming you have a global instance of your chart, you need to change the state of the chart.

$(document).on('click', '#myButton', function(){
    chart.dispatch.changeState('key');
    chart.update();
});

This works for me:

$(document).on('click', '#button', 
    function(){
        var state = chart.state;
        state.disabled[idOfItemInLegend] = !state.disabled[idOfItemInLegend];
        chart.dispatch.changeState(state);
        chart.update();
    }
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293919a4621025.html

相关推荐

  • javascript - How to use custom legends in NVD3 charts - Stack Overflow

    I am using an NVD3 line chart.The chart by default renders legends which are clickable.I want to build

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信