2010-11-20 20 views
6

我想要能夠使用flot圖形的圖例來打開/關閉我的圖形系列。我在flot網站上找到了這些示例,並使用API​​中的Turning series on/off和Labelformatter來構建我現在擁有的內容。我可以將複選框放在圖例元素的旁邊,併爲它們添加一個點擊事件及其火情。但是,再次調用繪圖函數並重置我的複選框值。我已經包含了完整的jquery函數,對不起它有點長。flot圖形,使用圖例打開/關閉系列

<script id="source"> 
var jsonPath = "JsonPriceHistory/" + getParameterByName("CardId") 



$(function() { 
    $.getJSON(jsonPath, function (results) { 

     results = [{ "label": "A", "data": [[1290115114240, 0.7000], [1289396258877, 0.7000], [1289394738247, 0.7000], [1288482602563, 0.7000], [1288479321830, 0.7000], [1288464257267, 0.7000], [1288463414413, 0.7000], [1268440264933, 1.0000], [1268434766653, 1.0000], [1268059707567, 1.0000], [1265934534340, 1.0000]] }, { "label": "B", "data": [[1290115102033, 6.0000], [1289395956947, 6.0000], [1289394743117, 6.0000], [1288482613967, 6.0000], [1288479332767, 6.0000], [1288464270420, 6.0000], [1288463427313, 6.0000], [1268440276413, 6.0000], [1268434778203, 6.0000], [1268059732683, 6.0000], [1265934545390, 6.0000]] }, { "label": "C", "data": [[1290115034640, 0.3000], [1289397347113, 0.3000], [1289396593083, 0.3000], [1289395047560, 0.3000], [1288484556080, 0.3000], [1288482794357, 0.3000], [1288465863503, 0.3000], [1288465248087, 0.3000], [1288464674300, 0.3000], [1268470601960, 0.6000], [1268469438957, 0.6000], [1268468281610, 0.6000], [1268440646800, 0.6000], [1265984810360, 0.8000], [1265955747730, 0.8000]] }, { "label": "C", "data": [[1290115031727, 0.1200], [1289397678960, 0.1200], [1289397337040, 0.1200], [1289396577510, 0.1200], [1289395024607, 0.1200], [1288484550417, 0.1200], [1288482780457, 0.1200], [1288465846327, 0.1200], [1288465231287, 0.1200], [1288464658213, 0.1200], [1268470586860, 0.2000], [1268469423697, 0.2000], [1268468266277, 0.2000], [1268440631390, 0.2000], [1265984774793, 0.2000], [1265955732580, 0.2000]] }, { "label": "D", "data": [[1290114958773, 0.0500], [1289397467207, 0.0500], [1289396747243, 0.0500], [1289395166640, 0.0500]] }, { "label": "E", "data": [[1290114933540, 0.6500], [1289397579447, 0.6500], [1289397242333, 0.6500], [1289396486657, 0.6500], [1289395003947, 0.6500], [1288484568590, 0.6500], [1288482784747, 0.6500], [1288465893750, 0.6500], [1288465278320, 0.6500], [1288464705170, 0.6500], [1268470629373, 0.6500], [1268469467810, 0.6500], [1268468309513, 0.6500], [1268440674610, 0.6500], [1265984889857, 0.6500], [1265955775453, 0.6500]] }, { "label": "F", "data": [[1290114885570, 0.1100], [1289396731507, 0.1100], [1289395170397, 0.1100]]}]; 

     var options = { 
      legend: { 
       show: true, 
       container: $("#overviewLegend"), 
       labelFormatter: function (label, series) { 
        var cb = '<input type="checkbox" name="' + label + '" checked="checked" id="id' + label + '"> ' + label; 
        return cb; 
       } 
      }, 
      series: { 
       points: { show: true }, 
       lines: { show: true } 
      }, 
      grid: { hoverable: true }, 
      xaxis: { 
       mode: "time", 
       minTickSize: [1, "day"], 
       max: new Date().getTime() 
      }, 
      yaxis: { 
       mode: "money", 
       min: 0, 
       tickDecimals: 2, 
       tickFormatter: function (v, axis) { return "$" + v.toFixed(axis.tickDecimals) } 

      } 
     }; 

     var i = 0; 
     $.each(results, function (key, val) { 
      val.color = i; 
      ++i; 
     }); 

     var choiceContainer = $("#overviewLegend"); 

     function plotAccordingToChoices() { 
      var data = []; 
      alert('hi'); 

      choiceContainer.find("input:checked").each(function() { 
       var key = $(this).attr("name"); 
       if (key && results[key]) 
        data.push(results[key]); 
      }); 

      $.plot($("#placeholder"), results, options); 
      choiceContainer.find("input").click(plotAccordingToChoices); 
     } 



     var previousPoint = null; 
     $("#placeholder").bind("plothover", function (event, pos, item) { 
      $("#x").text(pos.x.toFixed(2)); 
      $("#y").text(pos.y.toFixed(2)); 

      if (item) { 
       if (previousPoint != item.datapoint) { 
        previousPoint = item.datapoint; 

        $("#tooltip").remove(); 
        var x = item.datapoint[0].toFixed(2), 
       y = item.datapoint[1].toFixed(2); 

        showTooltip(item.pageX, item.pageY, item.series.label + " $" + y); 
       } 
      } 
      else { 
       $("#tooltip").remove(); 
       previousPoint = null; 
      } 
     }); 

     function showTooltip(x, y, contents) { 
      $('<div id="tooltip">' + contents + '</div>').css({ 
       position: 'absolute', 
       display: 'none', 
       top: y + 5, 
       left: x + 15, 
       border: '1px solid #fdd', 
       padding: '2px', 
       'background-color': '#fee', 
       opacity: 0.80 
      }).appendTo("body").fadeIn(200); 
     } 

     plotAccordingToChoices(); 
    }) 



}); 

+0

你認爲你可以鏈接用的jsfiddle演示,所以我可以工作d直接在你的代碼? – Kieran 2010-11-20 03:24:48

+0

試試這個http://jsfiddle.net/6FLsM/ – 2010-11-20 03:40:12

+0

另請參閱http://stackoverflow.com/questions/4230945/flot-graph-use-legend-to-turn-on-off-series – ericslaw 2011-09-27 19:45:24

回答

15

有一對夫婦的問題,你的代碼:

您正在使用的數據是一個數組的形式,而在演示中呈現的是一個對象。這裏的區別很重要,因爲您已經複製了他們的代碼,但沒有更改代碼以適應此。有問題的線路有:

if (key && results[key]) 
    data.push(results[key]); 

裏面的plotAccordingToChoices()功能。 results[key]在你的情況下將不會工作,因爲key將需要一個數值,但這裏是key一個字符串。解決的辦法是用替換for循環穿過陣列正確的標籤搜索:

for (var i = 0; i < results.length; i++) { 
    if (results[i].label === key) { 
     data.push(results[i]); 
     return true; 
    } 
} 

接下來,問題是,你一遍又一遍地型重構相同的數據再次,這條線:

$.plot($("#placeholder"), results, options); 

results[]從未改變 - 你應該在這裏使用data[]代替:

$.plot($("#placeholder"), data, options); 

然後,不像在演示中,你已經決定SETU繪製圖形時,使用legend選項中的formatlabel函數複選框。這樣做的問題是,如果使用不包含所有結果的新數據重新繪製圖形,則未顯示的線條的複選框將不會顯示,因爲flot不會繪製不存在的線條的標籤。

這意味着您必須按照演示的步驟進行操作 - 單獨創建複選框。我們可以通過將以下行$.each循環,用於固定每行使用的顏色做到這一點:

l = val.label; 
var li = $('<li />').appendTo(choiceContainer); 

$('<input name="' + l + '" id="' + l + '" type="checkbox" checked="checked" />').appendTo(li); 
$('<label>', { 
    text: l, 
    'for': l 
}).appendTo(li); 

這將創建一個複選框 - 爲results陣列中的每個數據集的標籤集。最後,我們把功能結合plotAccordingToChoices在函數外的每個複選框,這樣的結合只發生一次,以防止多個綁定,將所得的混亂:

choiceContainer.find("input").change(plotAccordingToChoices); 

我們也將其更改爲使用change事件,而不是click因爲change這裏比較合適。

最後,作爲獎勵,我們循環圖例表,並從那裏拉的顏色添加到複選框的列表:

$('.legendColorBox > div').each(function(i){ 
    $(this).clone().prependTo(choiceContainer.find("li").eq(i)); 
}); 

我們還需要一點CSS這個工作:

#overviewLegend li > div { 
    display: inline-block; 
    margin-right: 4px; 
} 

看到最後的工作代碼住在這裏:http://jsfiddle.net/yijiang/6FLsM/2/

+0

非常感謝你你的幫助,這是我第一個使用jquery或flot的真正項目。我主要是asp.net/c#工作。 – 2010-11-20 13:57:42

+0

如何獲取畫布內的複選框(與圖例相同的列) – 2014-07-02 07:43:19

+0

是否可以使用彩色框而不是添加複選框 – shorif2000 2015-12-20 20:21:29