score:0
you can try something like this fiddle, but i did not format and position the labels as of now, you can do it as you wish.
"onComplete": function() {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
var sums = [0, 0, 0];
this.data.datasets.forEach(function(dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function(bar, index) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y);
sums[index] += data;
});
});
sums.forEach(function(v, i) {
ctx.fillText(v, 150*(i+1)+(i*50), 40);
});
}
score:0
The formatter
is called for each datalabel, meaning for every bar. That's the reason why you get the same value on top of every bar.
Possible solutions would be either to plot an "invisible" bar for the combined value or check inside the formatter
function if it is called for the centered bar. Then you could use mulitline labels to create a label which has the value for the current bar and the combined value as well.
Edit:
After thinking about it the easiest way would be to use mixed charts. You can just calculate your combined values for each group and add a chart for these values. For example you add a line chart with showLines: false
.
new Chart('myMixedCharts', {
type: 'bar',
data: {
labels: ['foo', 'bar'],
datasets: [
{
label: "Value 1",
data: [12, 5]
},
{
label: "Value 2",
data: [7, 8]
},
{
label: "Group value",
data: [19, 13],
type: "line",
showLines: false
}
]
}
});
Or as jsFiddle
Source: stackoverflow.com
Related Query
- Chart js sum of grouped bars
- Hide empty bars in Grouped Stacked Bar Chart - chart.js
- chartjs add dots to bars in grouped bar chart
- How to prevent first/last bars from being cut off in a chart with time scale
- Chart.js Bar Chart - how to chart bars from 0
- ChartJS add tooltip to a grouped bar chart
- Chart.JS Mixed Chart - Bars Not Showing
- ChartJs bar chart - keep bars left instead of equally spread across the width
- Chart JS grouped sub labels
- Creating a grouped and stacked chart in Chart.js
- How to have solid colored bars in angular-chart bar chart
- Stacked bar chart results in misaligned bars
- How to make bar chart animation where all bars grow at the same speed?
- Possible to merge 2 bars in bar chart (chart.js)
- How to show gradient vertically on chart js grouped bar chart?
- how to customize tool tip while mouse go over bars on Chart js bar chart
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- Grouped Bar Chart ChartJS
- Equal distance grouped bar chart irrespective of scale - chart.js
- Chart.js stacked and grouped horizontalBar chart
- how to display multiple sum with chart js and laravel?
- Alternating bars in bar chart in chart.js are not labelled
- Chart.js: Reverse bar chart with regular bars (Bottom to top instead of top to bottom)
- Can the colors of bars in a bar chart be varied based on their value?
- Chart.js - Pie chart calculate sum of visible values after legend click
- How to show a "total" sum label on the top of stacked bars
- Set fixed label size for grouped bar chart in angular Chartjs
- Chart.js 3.5: linear gradient doesn't apply properly when multiple bars in the same chart
- Mixed Chart calculating difference between two bars - ChartJS
More Query from same tag
- Chartkick-vue bar chart - "horizontalBar" is not a registered controller
- how to add multiple data in chart js dynamically from JSON
- ChartJS and jsPDF - why the background is black?
- How can I show JSON data in Chart.js with Javascript if static values are working but dynamic from mySQL are not?
- ChartJS not showing from MySQL
- Wkhtmltopdf does not render Chart.JS 2.5.0 graph
- Run Chartjs Dart example
- Angularjs line chart get data from sql
- Drawing a doughnut chart with columns inside to represent hourly stats in chart.js
- how can conditionally show/hide gridlines on the yAxis using Chart.js?
- Unable to hide x axis for horizontal bar chart vue js
- chart.js scale xaxis with date, strange renderer
- Customize Chart js dataset data
- Chart.js only appears randomly, and disappears on page refresh
- Chart.js multiple include prevention
- how to add data from database to chartjs-plugin-streaming
- Chart.js legend alignment
- charts.js chart size it's diferente than container setted size
- ChartJS - How to add Text between Pie Chart and Legend
- Chart.js shows no graphs
- Chart.js LineChart how to show only part of dataset and support horizontal scrolling
- Destroy ChartJS rendered with Symfony
- How to add label in chart.js for polar chart area
- How to get fixed legend and tooltip for dynamically generated data vue-chartjs
- Chart.js line graph change x axis seperation
- Chart.js - Hide tooltip caret
- In chartjs, add link text on tooltip
- Change ChartJs axis title dynamically
- Changing Legend Label Position in VueChartjs
- Put chart.js chart inside JQuery tooltip?