2015-06-15 92 views
0

這是我的情況http://jsfiddle.net/co3L0bdb/4/如何使用Chart.js更新折線圖時設置全局/特定屬性?

感謝@potatopeelings幫助我現在能夠物業highlightFill設置爲我所有的datasets當我顯示/隱藏在圖中的一系列這部作品甚至。 我說:

currentChart.datasets[0].points[8].highlightStroke = "rgba(220,0,0,1)"; 
currentChart.datasets[1].points[8].highlightStroke = "rgba(0,255,0,1)"; 
currentChart.datasets[2].points[8].highlightStroke = "rgba(0,0,255,1)"; 

但我得到什麼,但我不想,就是當我躲在一個系列,經過2次迭代,它開始繪製隱藏數據,並停止刪除的創舉數據該系列。 我該如何設置即使是系列屬性,如highlightStroke,沒有描述的副作用?

回答

0

而不是將數據集屬性設置爲空對象,根本不包括它在數據集中。所以

data = { 
    labels: chartlabel, 
    datasets: [SP, NC, SPA] 
}; 

變得

data = { 
    labels: chartlabel, 
    datasets: [] 
}; 

if (SP.label !== undefined) data.datasets.push(SP) 
if (NC.label !== undefined) data.datasets.push(NC) 
if (SPA.label !== undefined) data.datasets.push(SPA) 
在2個地方


更換數據插入,點屬性由環(3個數據集,而不是硬編碼)設置。所以

setInterval(function() { 
    currentChart.addData([randomScalingFactor(), randomScalingFactor(), randomScalingFactor()], ++latestLabel); 
currentChart.datasets[0].points[8].highlightStroke = "rgba(220,0,0,1)"; 
currentChart.datasets[1].points[8].highlightStroke = "rgba(0,255,0,1)"; 
currentChart.datasets[2].points[8].highlightStroke = "rgba(0,0,255,1)"; 

成爲

var d = [] 
currentChart.datasets.forEach(function(e) { 
    d.push(randomScalingFactor()) 
}) 
currentChart.addData(d, ++latestLabel); 
currentChart.datasets.forEach(function(dataset) { 
    dataset.points[8].highlightStroke = dataset.points[0].highlightStroke; 
}) 

的highlightStroke新點從highlightStroke複製爲同一系列的第一點。


小提琴 - http://jsfiddle.net/zs99pw4L/

+0

再次感謝您, 您的解決方案的工作原理,但我實際上解決了問題的方式我張貼爲答覆 – Sabba

0
currentChart2.datasets.forEach(function(e) { 
    e.points[e.points.length - 1].highlightFill=e.points[0].highlightFill; 
    e.points[e.points.length - 1].highlightStroke=e.points[0].highlightStroke; 
}); 

解決問題了。 http://jsfiddle.net/co3L0bdb/5/

+0

乾杯!順便說一句,一旦你刪除一個系列 - 你想要什麼?在刪除它之後添加一個系列後,它會進行水平壓縮。 – potatopeelings

+0

你是對的,我不想要這個,這個代碼只是一個在我的真實應用程序中的實例,添加該行可以正常工作。 – Sabba

+0

好吧,解釋它。 – potatopeelings

相關問題