highcharts - How to display total of extra data in stacked column high charts -
i have stacked column showing number of fruits eaten john , joe. aside number of fruits, show how cost of fruit consumption is. in case, can show amount of each fruit eaten john or joe (ex. john ate 5 apples $100 while joe ate 3 apples $60). however, show total cost per fruit (for apples, total consumption 8 apples worth $160). there way can this?
you can see preliminary work here: http://jsfiddle.net/spzba/
$(function () { $('#container').highcharts({ chart: { type: 'column' }, title: { text: 'stacked column chart' }, xaxis: { categories: ['apples', 'oranges', 'pears'] }, yaxis: { min: 0, title: { text: 'total fruit consumption' }, stacklabels: { enabled: true, style: { fontweight: 'bold', color: (highcharts.theme && highcharts.theme.textcolor) || 'gray' } } }, legend: { align: 'right', x: -70, verticalalign: 'top', y: 20, floating: true, backgroundcolor: (highcharts.theme && highcharts.theme.background2) || 'white', bordercolor: '#ccc', borderwidth: 1, shadow: false }, tooltip: { formatter: function() { return '<b>'+ this.x +'</b><br/>'+ this.series.name +': '+ this.y + ' (<b>$ ' + this.point.amount +') <br/>'+ 'total: '+ this.point.stacktotal; } }, plotoptions: { column: { stacking: 'normal', datalabels: { enabled: true, color: (highcharts.theme && highcharts.theme.datalabelscolor) || 'white', style: { textshadow: '0 0 3px black, 0 0 3px black' } } } }, series: [{ name: 'john', data: [{y: 5, amount: 100}, {y: 3, amount: 60}, {y: 4, amount: 80}] }, { name: 'joe', data: [{y: 3, amount: 60}, {y: 4, amount: 80}, {y: 4, amount: 80}] }] }); });
is possible show total amount per fruit consumed? put total amount beside "total" label when hovering column. ex. in figure, "total: 8 ($160)".
thank much!
it's bit round houses can array of series this.series.chart.series
, , each series has points array can use current this.point.x
index to, iterating series can calculate total amount
property.
tooltip: { formatter: function() { var allseries = this.series.chart.series; var totalamount = 0; for(var s in allseries) { totalamount += allseries[s].points[this.point.x].amount; } return '<b>'+ this.x +'</b><br/>'+ this.series.name +': '+ this.y + ' (<b>$ ' + this.point.amount +') <br/>'+ 'total: '+ this.point.stacktotal + ' (<b>$ ' + totalamount +')'; } }
Comments
Post a Comment