2012-01-06 187 views
1

flot screenshotFlot Stacked酒吧

所以我有上面的flot堆積酒吧使用flot。現在我還有一個懸停效果,當在主欄中的任何欄上方顯示該部分的編號時。我想要顯示的是參考當前欄而不是數字的部分的百分比,但是我沒有在任何地方看到我如何能夠達到欄的總數(即,查找周圍的數字4200基於此吧)。

回答

5

爲了獲得總數,您需要查看數據並查找系列中與所點擊項目對應的元素,然後對其值進行求和。

 <div id="placeholder" style="width:600px;height:300px;"></div> 
    <div id="clickdata" ></div> 


<script id="source"> 
$(function() { 
    var d1 = []; 
    for (var i = 0; i <= 10; i += 1) 
     d1.push([i, parseInt(Math.random() * 30)]); 

    var d2 = []; 
    for (var i = 0; i <= 10; i += 1) 
     d2.push([i, parseInt(Math.random() * 30)]); 

    var d3 = []; 
    for (var i = 0; i <= 10; i += 1) 
     d3.push([i, parseInt(Math.random() * 30)]); 

    var stack = 0, bars = true, lines = false, steps = false; 

    function plotWithOptions() { 
     return $.plot($("#placeholder"), [ d1, d2, d3 ], { 
      grid: { clickable: true }, 
      series: { 
       stack: stack, 
       lines: { show: lines, fill: true, steps: steps }, 
       bars: { show: bars, barWidth: 0.6, hoverable: true, clickable: true } 
      } 
     }); 

    } 

    var plot = plotWithOptions(); 

    function getTotalForIndex(index) { 
     var total = parseFloat(d1[index][1]) + parseFloat(d2[index][1]) + parseFloat(d3[index][1]); 
     return total; 
    } 

    $("#placeholder").bind("plotclick", function (event, pos, item) { 
     if (item) { 
      $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ". which has total value "+ getTotalForIndex(item.dataIndex)); 
      plot.highlight(item.series, item.datapoint); 
     } 
    }); 
}); 
</script> 
+0

我不是在尋找系列中的總數,我在尋找總數在酒吧。如果series1爲[[1,2],[2,6]],並且series2爲[[1,12],[2,52]],我想要得到bar1-14和bar2 = 58的總數不是series1 = 8和series2 = 64.我已經看到了flothover事件的項目,但我只能看到系列數據,而不是條形數據。 – ryanzec 2012-01-06 14:46:51

+0

如果你運行我的例子,它會給出欄中的總數而不是系列。獲取索引的總數,找到每個系列中的條索引值並返回總計 – Richard 2012-01-06 14:51:57

+0

Opps,但沒有意識到代碼示例是可滾動的,看起來應該起作用。希望我不會這樣做,我會認爲吧檯總數將是本身提供的東西。 – ryanzec 2012-01-06 15:01:05