score:10
Accepted answer
You can stack bar charts by setting the stacked
option to true on the x- and y-axis and define stack
group property of your datasets.
Below is your snippet with the proposed changes:
new Chart(document.getElementById("MyChart"), {
type: 'bar',
data: {
labels: [2017, 2018, 2019, 2020, 2021, 2022, 2023],
datasets: [{
label: "Income - Base",
type: "bar",
stack: "Base",
backgroundColor: "#eece01",
data: [30, 31, 32, 33, 34, 35, 36],
}, {
label: "Tax - Base",
type: "bar",
stack: "Base",
backgroundColor: "#87d84d",
data: [-15, -16, -17, -18, -19, -20, -21],
}, {
label: "Income - Sensitivity",
type: "bar",
stack: "Sensitivity",
backgroundColor: "#f8981f",
data: [20, 21, 22, 23, 24, 25, 26],
}, {
label: "Tax - Sensitivity",
type: "bar",
stack: "Sensitivity",
backgroundColor: "#00b300",
backgroundColorHover: "#3e95cd",
data: [-10, -11, -12, -13, -14, -15, -16]
}]
},
options: {
scales: {
xAxes: [{
//stacked: true,
stacked: true,
ticks: {
beginAtZero: true,
maxRotation: 0,
minRotation: 0
}
}],
yAxes: [{
stacked: true,
}]
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="MyChart" width="400" height="200"></canvas>
Source: stackoverflow.com
Related Query
- Creating a grouped and stacked chart in Chart.js
- Chart.js stacked and grouped horizontalBar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Hide empty bars in Grouped Stacked Bar Chart - chart.js
- Showing Percentage and Total In stacked Bar Chart of chart.js
- ERROR TypeError: "this.canvas is undefined" | Problem with creating a chart with angular and chart.js
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- Unable to create Stacked Grouped Bar Chart with chart.js / react-chartjs-2 in React
- ChartJS: Grouped Stacked Bar Chart not Grouping
- Creating a stacked budget Vs Actual chart
- 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
- ChartJS: Is this horizontal stacked bar and line graph multiaxis chart even possible?
- chart.js horzontalBar Stacked and Grouped - data not showing - a bug?
- Creating a Doughnut chart using JSON and chart.js
- Creating a real time line chart using chartjs and firebase realtime db
- how to not repeat code while creating multiple charts in chart js
- In Chart.js set chart title, name of x axis and y axis?
- Chart.js - Increase spacing between legend and chart
- Chartjs v2.0: stacked bar chart
- Horizontal stacked bar chart with chart.js
- Vertical stacked bar chart with chart.js
- Chart.js: bar chart first bar and last bar not displaying in full
- chartjs datalabels change font and color of text displaying inside pie chart
- How to save Chart JS charts as image without black background using blobs and filesaver?
- Click event on stacked bar chart - ChartJs
- Creating chart.js chart directly to PNG in Node js?
- ChartJS add tooltip to a grouped bar chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to create stacked bar chart using react-chartjs-2?
More Query from same tag
- Chart.js multi line chart that has different labels
- How to include Chart.js in my AngularJS project?
- Chart.js: Reverse bar chart with regular bars (Bottom to top instead of top to bottom)
- Chart.js V2.7.2 How to make Horizontal scroll bar without loosing Y-Axis
- How to integrate basic ChartJs customizations when using react-chartjs-2?
- Thicker X-Axis with Charts.js
- Chart.js remove gridline
- chart.js LIne Graphs: Fill area above line as opposed to below and to the right
- Chart.js labels of x-axes makes useless space in chartarea
- How do I align chart.js pie charts?
- NPM module doesn't work with Webpack 3 but works with vue-cli3 which based on Webpack 4
- Json data visualization in Javascript with chartjs
- Uncaught TypeError: fn is not a function in chart.js
- React-chartjs not working
- Customize Pie Chart Legend in chart.js
- Add padding between two y-axis scales using chart.js
- How to change legend icon on hover Chartjs
- AngularJS hide and show a char
- Chart.js: refresh second Y axis step on legend click
- Remove gridlines in AngularCharts
- Convert two lists into a dictionary of X and Y Format
- Chart.js pie chart to display "No Data" if the datasets are empty
- Add custom data scale to Chart.js
- Chart js scatter - display label on hover
- Chart.js - Styling Legend
- chartjs Adding percentages to Pie Chart Legend
- Chart.js - Bar values showing on the middle of bar
- How to Display Chart.js line-chart from a MVC Controller
- chart.js pie chart background colors: is there any way to generate them randomly?
- Vue 2: How to unit test component that uses Chart.js (vue-chart-3)