Skip to content Skip to sidebar Skip to footer

Chart Js Show Value Inside Stacked Bar Chart

Stacked Bar Chart

Chart Js is a popular open-source JavaScript library used for data visualization. It is easy to use and provides a wide range of chart types. One of the most commonly used chart types is the stacked bar chart. A stacked bar chart is a chart that uses bars to represent data and displays the total value of each bar by stacking the values of each segment. In this article, we will discuss how to show values inside a stacked bar chart using Chart Js.

Creating a Stacked Bar Chart

Stacked Bar Chart Creation

Before we dive into showing values inside a stacked bar chart, let's first create a stacked bar chart using Chart Js. To create a stacked bar chart, we need to define the data and the options. The data is an array of objects, where each object represents a bar. Each object has a label and an array of values, where each value represents a segment of the bar. The options define the chart type, title, legend, and other chart-specific configurations.

Showing Values Inside a Stacked Bar Chart

Show Value Inside Stacked Bar Chart

To show values inside a stacked bar chart, we need to use the plugins provided by Chart Js. The plugins are additional scripts that extend the functionality of Chart Js. The plugin we need to use is called "chartjs-plugin-datalabels". This plugin allows us to show labels inside the chart bars.

To use the "chartjs-plugin-datalabels" plugin, we need to include it in our code and define the options. The options define the position, font size, color, and other label-specific configurations. We can also format the label text using the "formatter" function.

Example Code

Example Code For Stacked Bar Chart

Here is an example code that shows how to create a stacked bar chart with values inside the bars:

<canvas id="myChart"></canvas><script src="https://cdn.jsdelivr.net/npm/chart.js@3.5.1/dist/chart.min.js"></script><script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2.0.0/dist/chartjs-plugin-datalabels.min.js"></script><script>var ctx = document.getElementById('myChart').getContext('2d');var myChart = new Chart(ctx, {type: 'bar',data: {labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],datasets: [{label: 'Dataset 1',data: [12, 19, 3, 5, 2, 3, 7],backgroundColor: 'rgba(255, 99, 132, 0.2)',borderColor: 'rgba(255, 99, 132, 1)',borderWidth: 1,}, {label: 'Dataset 2',data: [2, 3, 20, 5, 1, 4, 2],backgroundColor: 'rgba(54, 162, 235, 0.2)',borderColor: 'rgba(54, 162, 235, 1)',borderWidth: 1,}]},options: {plugins: {datalabels: {anchor: 'end',align: 'top',font: {size: 16,},formatter: function(value, context) {return value;}}},scales: {y: {stacked: true,ticks: {beginAtZero: true}},x: {stacked: true}}}});</script>

In the above code, we first create a canvas element with an ID of "myChart". We then include the Chart Js and "chartjs-plugin-datalabels" scripts. We define the data and options for the chart, including the plugins option with the datalabels configuration. Finally, we create a new Chart object with the canvas element and the data and options.

Conclusion

Conclusion

In conclusion, showing values inside a stacked bar chart using Chart Js is easy with the "chartjs-plugin-datalabels" plugin. By following the steps outlined in this article, you can create a stacked bar chart with values inside the bars to better visualize your data. With Chart Js, you can create a wide range of charts and customize them to fit your needs.

Related video of Chart Js Show Value Inside Stacked Bar Chart