[英]Convert cumulative chart to non-cumulative using highcharts
我想將累積圖表轉換為非累積圖表。 基本上,我有演示給您看:
這是累積圖: http : //jsfiddle.net/h1qq1rj8/
這是非累積圖: http : //jsfiddle.net/h1qq1rj8/2/
$(function () {
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Total fruit consumtion, grouped by gender'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
allowDecimals: false,
min: 0,
title: {
text: 'Number of fruits'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.x + '</b><br/>' +
this.series.name + ': ' + this.y + '<br/>' +
'Total: ' + this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [1, 8, 12, 13, 23], // I need to convert this data to non-cumulative
stack: 'male'
}]
});
});
我已經累積了數據,將其轉換為非累積數據,所以我在后端減去了數據,但是由於性能下降,難道沒有辦法在highcharts API本身中使圖表非累積數據嗎?
編輯:
我還有一個圖形,其中數據以不同的方式格式化: http : //jsfiddle.net/h1qq1rj8/3/
Highcharts不會在繪制圖表之前對數據進行計算,但是您可以將該特定部分的計算從服務器端移到客戶端。
您可以在數組上使用reduce
函數以獲得所需的結果:
old_ar = [1, 8, 12, 13, 23] new_ar = old_ar.reduce(function(a, b, c, d) { if (a.length) {a.push(d[c]-d[c-1])} else {a = [b]} return a;}, []) console.log(new_ar)
對於第二種數據樣式,您可以使用以下方法:
old_ar = [[0,1],[1,8],[2,12],[3,13],[4,23]] new_ar = old_ar.reduce(function(a, b, c, d) { if (a.length) {a.push([d[c][0], d[c][1]-d[c-1][1]])} else {a = [b]} return a;}, []) console.log(new_ar)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.