【问题标题】:Dynamic Flot graph - show hide series by clicking on legend text or box on graph动态 Flot 图表 - 通过单击图表上的图例文本或框显示隐藏系列
【发布时间】:2012-12-21 12:49:14
【问题描述】:

我正在研究 3 系列的动态浮动图。我需要在单击图例时隐藏/显示系列。我已经看到了不同的示例,它们适用于静态图,但适用于动态图,即使它是第一次工作,但是当使用新数据值更新图时,所有内容都使用默认选项显示。一旦我隐藏了系列,我希望它被隐藏,直到我再次单击以显示它。

这是我的代码。基本上,我从 JSON 中获取数据并以 10 秒的间隔动态更新浮点图。所以新数据将每 10 秒显示一次,这就是该系列再次显示的地方。

 <div id="placeholder4" style="width:1000px;height:300px;background:#C89175"></div>

 <script type="text/javascript">
     $(function() {
         somePlot = null;

         togglePlot = function(seriesIdx)
         {
              var someData = somePlot.getData();
              someData[seriesIdx].lines.show = !someData[seriesIdx].lines.show;
              somePlot.setData(someData);
              somePlot.draw();

         }


// Initilizaiton of Series and Counter
         var i = 0;
         var data_Total = [[], [], []];
         // data_Total[0] : Stip Data
         // data_Total[1] : Decline Data
         // data_Total[2] : Volume Data
//Setting Options for Graph Display
         var options = {

             points: { show: true },
             //legend: {toggle: true },
             series: {
        lines: { show: true }
                 },
              legend: {
        labelFormatter: function(label, series){
          return '<a href="#" onClick="togglePlot('+series.idx+'); return false;">'+label+'</a>';
        }
    },

            grid: {backgroundColor: "#FCFCFC", labelMargin:12,hoverable: true,tickColor:"#AD5C5C" },
              xaxis: { mode: "categories", show:true,color:"white",axisLabel:'Time Series' },
                         yaxis:{show:true,color:"white",min:0,max:10000,axisLabel:'Total/ Stip/ Decline'}


         }

//Function that will be called recursively with specified Time Interval
         function fetchData() {
//Function that will push data in to Series1 thru an ajax call
             function getDPSStipData(series) {
                 var stipItem = [series.data[i][0], series.data[i][1]];
                 data_Total[0].push(stipItem);
             }
             $.ajax({
                 url: "./JSon/stipdpssec.json",
                 method: 'GET',
                 dataType: 'json',
                 success: getDPSStipData
             });
//Function that will push data in to Series2 thru an ajax call
             function getDPSDeclineData(series) {
                 var declineItem = [series.data[i][0], series.data[i][1]];
                 data_Total[1].push(declineItem);
             }
             $.ajax({
                 url: "./JSon/declinedpssec.json",
                 method: 'GET',
                 dataType: 'json',
                 success: getDPSDeclineData
             });
//Function that will push data in to Series3 thru an ajax call
             function getDPSTotalVolumeData(series) {
                 var totalVolItem = [series.data[i][0], series.data[i][1]];
                 data_Total[2].push(totalVolItem);
             }
             $.ajax({
                 url: "./JSon/totaldpssec.json",
                 method: 'GET',
                 dataType: 'json',
                 success: getDPSTotalVolumeData
             });
//Moving forward the ticks if size > 10
             if (data_Total[0].length > 10)
             {
                 data_Total[0] = data_Total[0].splice(1,10);
                 data_Total[1] = data_Total[1].splice(1,10);
                 data_Total[2] = data_Total[2].splice(1,10);
             }

// Plotting of Graph
             //$.plot($("#placeholder4"), [{ data: data_Total[2], label: "TotalVolume"},{ data: data_Total[0], label: "Stip",yaxis:2 }, { data: data_Total[1], label: "Decline",yaxis:2 }], options);
 somePlot=$.plot($("#placeholder4"), [{ data: data_Total[2], label: "TotalVolume",idx:0},{ data: data_Total[0], label: "Stip",color: "green",idx:1 }, { data: data_Total[1], label: "Decline",color:"red",idx:2 }], options);

             i++;
 } 

//fetchData

setInterval(fetchData, 10000);



     });
</script>

【问题讨论】:

    标签: dynamic graph show-hide flot series


    【解决方案1】:

    这是我为你整理的quick example

    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 '<a href="#" onClick="togglePlot('+series.idx+'); return false;">'+label+'</a>';
            }
        }
    });
    

    【讨论】:

    • 此代码正在运行,但当此实时图表在 10 秒后使用新数据点刷新时再次显示系列。我需要隐藏图表上的系列,直到我再次单击图例。请告诉我
    • 如何刷新图表?如果你想让人们帮助你,你必须提供足够的信息......
    • 嗨,马克,我已经发布了详细信息和代码。你有机会看看吗
    • @user1908522,刚刚看了。问题是您正在重新创建每个 ajax 成功的情节。这会覆盖该行的“可见性”。您应该绘制一次绘图,然后使用getDatasetData 方法更新数据。如果您每次都重新创建它,则必须在 $.plot 调用中将线设置为可见或不可见。
    • 嗨,马克,你是对的,我每次都调用 plot 有一些间隔时间。您提到“在 $.plot 上将线设置为可见或不可见”,所以我选择了一个系列,然后该线变得不可见,我必须使其不可见,直到我再次单击它。你能告诉我下次调用时如何将系列线的状态传递给情节吗
    【解决方案2】:

    您可以像这样在重新渲染图表后继续提供图例点击:

    HTML:

    <div id=graph></div>
    

    JS:

    $('#graph').on('click', 'div.legend tr', function() {
        var tr = $(this);
        var index = tr.parent().find('tr').index(tr);
        // Do something with the fact they clicked item (index)
    });
    

    图例中没有存储任何内容来标记每行所代表的内容,因此您需要从其他地方获取该信息 - 上述代码所做的只是让您获得点击的图例项的索引。

    为了可用性,您应该告诉用户这是可点击的:

    CSS:

    #graph div.legend tr {
        cursor: pointer;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      相关资源
      最近更新 更多