score:12
Accepted answer
This can be easily achieved using a Chart.js plugin called : chartjs-plugin-datalabels.
Here is the minimum options that need to be set for this plugin to display values inside (middle) of the stacked bars :
options: { //your chart options
plugins: {
datalabels: {
display: true,
align: 'center',
anchor: 'center'
}
}
}
although, there are tons of other options that you can use to further customize these values/labels (inside bars), which can be found here.
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var numberWithCommas = function(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
var dataPack1 = [50000, 22000, 26000, 35000, 55000, 55000, 56000, 59000, 60000, 61000, 60100, 62000];
var dataPack2 = [0, 6000, 13000, 14000, 50060, 20030, 20070, 35000, 41000, 4020, 40030, 70050];
var dates = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
// Chart.defaults.global.elements.rectangle.backgroundColor = '#FF0000';
var bar_ctx = document.getElementById('bar-chart');
var bar_chart = new Chart(bar_ctx, {
type: 'bar',
data: {
labels: dates,
datasets: [{
label: 'SoftEnterprises, Sales',
data: dataPack1,
backgroundColor: "rgba(55, 160, 225, 0.7)",
hoverBackgroundColor: "rgba(55, 160, 225, 0.7)",
hoverBorderWidth: 2,
hoverBorderColor: 'lightgrey'
}, {
label: 'SmartSystems, Sales',
data: dataPack2,
backgroundColor: "rgba(225, 58, 55, 0.7)",
hoverBackgroundColor: "rgba(225, 58, 55, 0.7)",
hoverBorderWidth: 2,
hoverBorderColor: 'lightgrey'
}, ]
},
options: {
tooltips: {
mode: 'label',
callbacks: {
label: function(tooltipItem, data) {
return data.datasets[tooltipItem.datasetIndex].label + ": " + numberWithCommas(tooltipItem.yLabel);
}
}
},
scales: {
xAxes: [{
stacked: true,
gridLines: {
display: false
},
}],
yAxes: [{
stacked: true,
ticks: {
callback: function(value) {
return numberWithCommas(value);
},
},
}],
}, // scales
legend: {
display: true
},
plugins: {
datalabels: {
display: true,
align: 'center',
anchor: 'center'
}
}
} // options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<canvas id="bar-chart" width="600" height="350"></canvas>
Source: stackoverflow.com
Related Query
- How to display inline values in a stacked bar chart with Chart.js?
- Chart.Js how to hide datapoints with "0" value in Stacked Bar Chart
- How do I create a stacked horizontal bar chart with Chart.js in React?
- Stacked bar chart with chartjs with included values
- How can I create a stacked bar chart with Charts.JS that displays relative rather than absolute values?
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- Chart.js how to display multiple labels on multi bar stacked chart
- How to fix a stacked logarithmic bar chart values to fit the grid
- How to display a progress over time in one horizontally stacked bar with chartjs
- How to assign values to different lables(legends) of my dataset of stacked bar chart
- CharJS : Is it possible to display stacked bar chart with positive scale on both sides of 0 index
- Horizontal stacked bar chart with chart.js
- Vertical stacked bar chart with chart.js
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to display Line Chart dataset point labels with Chart.js?
- How to create stacked bar chart using react-chartjs-2?
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to display data labels outside in pie chart with lines in ionic
- Stacked horizontal bar chart along total count with chart.js
- How to show data values in top of bar chart and line chart in chart.js 3
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- How can I datalabels and Sum display in the same time on a stacked bar
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- how to display chart.js legend with different bar color
- Stacked bar chart with rounded corner of bar using Chart.js
- Chart.js stacked bar chart - sorting values in bar by value
- ChartJS: how to make a bar chart with a horizontal guideline:
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- How to create Bar chart that gets updated from new ajax requests with ChartJS?
- angular-chart.js : how to show numbers in each bar of stacked bar chart
More Query from same tag
- How to execute a JavaScript function with ChartJS and FreeMarker?
- ChartJS chart is bugged after adding new data
- creating a dynamic number of charts using chart.js using angular
- How to wait for all items to load within ng-repeat before then rendering a chart for each item
- Chart.js custom plugin before destroy
- Chartjs background color multiple Dataset
- Chart.js Change color of the values *inside* the bars of a Bar chart
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Chart.js line doesn't fit in
- Is there any way to get y of specific x which not belongs to the dataset in Chart.js graph?
- ChartJS - Returning labels and data from server on different calls
- How to display pie slice data and tooltip together using chart.js
- Create PDF with mpdf and chart.js
- Chart.js line chart with correctly spaced x labels
- Change Angular js charts.js chart type dynamically
- Chart JS not reloading in Partial View
- ChartJS and Radar Chart animation
- Change the Y-axis values from real numbers to integers in Chart.js
- Why is my Line Chart.JS starting in the middle?
- drawing bar chart with chart.js jQuery
- Passing Array to Chart.js not working, chart not rendering
- SCRIPT438: Object doesn't support property or method 'values'
- How to take a set of Date objects and display the frequency based on time of day using ChartJS
- Setting axis scales in ChartsJS
- Growing chart value - chart.js
- Uncaught TypeError: lineChart.Line is not a function in chart.js
- Use Chartjs with reactjs
- Setting ChartJS plugin on vue-chartjs not working
- Calculating Bar Chart Percentage