Dynamic Flot graph - show hide series by clicking on legend text or box on graph

后端 未结 2 1005
挽巷
挽巷 2020-12-10 02:29

I am working on dynamic flot graph with 3 series. My need is to hide/show series when clicked on legend. I have seen different examples that will work fine for static graphs

2条回答
  •  执笔经年
    2020-12-10 03:05

    Here's a quick example I put together for you.

    somePlot = null;
    
    togglePlot = function(seriesIdx)
    {
      var someData = somePlot.getData();
      someData[seriesIdx].lines.show = !someData[seriesIdx].lines.show;
      somePlot.setData(someData);
      somePlot.draw();
    }
    
    var data = [
        {
        label: 'foo',
        color: 'red',
        data: [[1, 300], [2, 300], [3, 300], [4, 300], [5, 300]],
          idx: 0},
    {
        label: 'bar',
        color: 'blue',
        data: [[1, 800], [2, 600], [3, 400], [4, 200], [5, 0]],
          idx: 1},
    {
        label: 'baz',
        color: 'yellow',
        data: [[1, 100], [2, 200], [3, 300], [4, 400], [5, 500]],
          idx: 2},
    {
        label: 'dart',
        color: 'green',
        data: [[1, 500], [2, 350], [3, 400], [4, 700], [5, 50]],
          idx: 3}
        ];
    
    somePlot = $.plot($("#placeholder"), data, {
        series: {
            lines: {
                show: true
            }
        },
        legend: {
            labelFormatter: function(label, series){
              return ''+label+'';
            }
        }
    });
    

提交回复
热议问题