admin管理员组文章数量:1297087
I have a stacked column chart that shows 3 keys / values for each month stacked one on top of the other. Some months may have negatives. The current functionality is for highcharts to put two stacked labels for each month. One for the positives (on top) and one for the negatives (on bottom).
Please see the code below and the js fiddle as an example:
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
stackLabels: {
enabled: true,
align: 'center'
}
},
plotOptions: {
column: {
stacking: 'normal',
pointPadding: 0,
groupPadding: 0,
dataLabels: {
enabled: false,
color: '#FFFFFF'
}
}
},
series: [{
data: [29.9, -71.5, 106.4, -129.2, 144.0, -176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
data: [144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2]
},
{
data: [55.9, 90.5, 106.4, 350.2, 144.0, 52.0, 130.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
/
My desired functionality is to actually have one stacked label that includes the sum of all three values instead of two separate labels. So for February (in the fiddle) it would show 195 above the column because 266.50 - 71.50 = 195.
I have been trying to figure a way to do this but have been unsuccessful because highcharts is treating the positives and negatives as separate charts. Any help would be appreciated! Thank you.
I have a stacked column chart that shows 3 keys / values for each month stacked one on top of the other. Some months may have negatives. The current functionality is for highcharts to put two stacked labels for each month. One for the positives (on top) and one for the negatives (on bottom).
Please see the code below and the js fiddle as an example:
$(function () {
$('#container').highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
stackLabels: {
enabled: true,
align: 'center'
}
},
plotOptions: {
column: {
stacking: 'normal',
pointPadding: 0,
groupPadding: 0,
dataLabels: {
enabled: false,
color: '#FFFFFF'
}
}
},
series: [{
data: [29.9, -71.5, 106.4, -129.2, 144.0, -176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
data: [144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2]
},
{
data: [55.9, 90.5, 106.4, 350.2, 144.0, 52.0, 130.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
http://jsfiddle/sph1LjtL/
My desired functionality is to actually have one stacked label that includes the sum of all three values instead of two separate labels. So for February (in the fiddle) it would show 195 above the column because 266.50 - 71.50 = 195.
I have been trying to figure a way to do this but have been unsuccessful because highcharts is treating the positives and negatives as separate charts. Any help would be appreciated! Thank you.
Share Improve this question asked Jan 6, 2016 at 17:52 John BJohn B 1694 silver badges14 bronze badges4 Answers
Reset to default 3See Working fiddle here
Used the solution provided by Pawel Fus (Highcharts) at this Link
yAxis: {
stackLabels: {
enabled: true,
align: 'center',
formatter: function() {
var sum = 0;
var series = this.axis.series;
for (var i in series) {
if (series[i].visible && series[i].options.stacking == 'normal')
sum += series[i].yData[this.x];
}
if(this.total > 0 ) {
return Highcharts.numberFormat(sum,1);
} else {
return '';
}
}
}
}
This might be one solution, using the yAxis.stackLabels.formatter
function, and doing a lookup for a negative stack when processing a positive stack. In code:
yAxis: {
stackLabels: {
formatter: function() {
if(this.total >= 0) {
if(this.axis.stacks["-column"][this.x] != null)
return this.total + this.axis.stacks["-column"][this.x].total;
else
return this.total;
}
}
// ...
}
}
See this updated JSFiddle demonstration of how it works.
Improving on the answer by Pawel Fus and linked above by Nishith to acmodate missing data points as well as negative sums (in which case the sum is shown below the bars for the given x-axis index):
yAxis: {
stackLabels: {
enabled: true,
formatter: function() {
var sum = 0;
var series = this.axis.series;
for (var i in series) {
if (series[i].visible
&& series[i].options.stacking == 'normal'
&& this.x in series[i].yData)
sum += series[i].yData[this.x];
}
if (sum >= 0 && this.isNegative == false
|| sum < 0 && this.isNegative == true) {
return Highcharts.numberFormat(sum,1);
} else {
return '';
}
}
}
}
See JSFiddle here
This can be done using the yAxis.stackLabels.formatter
and looping through the series data items (as well as applying a filter onto which axis to apply the label). Here is a pleted (very verbose) example:
yAxis: {
stackLabels: {
enabled: true,
align: 'center',
formatter: function() {
console.log(this);
var theIndex = this.x;
var theSeries = this.axis.series;
var theSum = 0;
for (var i = 0; i < theSeries.length; ++i) {
theSum = theSum + theSeries[i].yData[theIndex]; //console.log(theSeries[i].yData[theIndex]);
}
if (this.isNegative == false) {
return theSum;
}
}
}
},
Inside the formatter
I am getting the index (x
) of the points for later use in picking out the correct data points to sum. Then setting looping through all the series items. Finally I am getting the yData
points out of the series object and summing them up for the appropriate xAxis index. The next step is to only apply the filter to the positive axis location. Fully functional jsFiddle. Note that you are going to have to do some work with the decimal precision and I leave that up to you.
本文标签: javascriptHighcharts Stack Column Sum Positive and Negative TogetherStack Overflow
版权声明:本文标题:javascript - Highcharts Stack Column Sum Positive and Negative Together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641937a2389968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论