score:58
Accepted answer
With v2.1.x, you can achieve this using the stacked
option
...
options: {
scales:{
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
...
Stack Snippet
var config = {
type: 'bar',
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
type: 'line',
label: 'Dataset 2',
data: [-65, -10, -80, -81, -56, -85, 40],
borderColor: 'black',
fill: false
}, {
type: 'bar',
label: 'Dataset 1',
backgroundColor: "red",
data: [65, 0, 80, 81, 56, 85, 40],
}, {
type: 'bar',
label: 'Dataset 3',
backgroundColor: "blue",
data: [-65, 0, -80, -81, -56, -85, -40]
}]
},
options: {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>
score:2
Just make sure that you provide an array of object(s), not just one object for xAxis
and yAxis
.
"options": {
"scales": {
"xAxes": [
{
"stacked": true
}
],
"yAxes": [
{
"stacked": true
}
]
}
}
score:43
After fiddling forever, I finally got it to work, here's a sample. Somebody please update the docs!
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes 1',
data: [10, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(255, 99, 132, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(255,99,132,1)',
'rgba(255,99,132,1)',
'rgba(255,99,132,1)',
'rgba(255,99,132,1)',
'rgba(255,99,132,1)'
],
borderWidth: 2
},
{
label: '# of Votes 2',
data: [15, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 159, 64, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 159, 64, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255, 159, 64, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 2
}
]
},
options: {
scales: {
yAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}],
xAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
Source: stackoverflow.com
Related Query
- Chartjs v2.0: stacked bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Click event on stacked bar chart - ChartJs
- Multiple stacked bar chart using ChartJs
- Stacked bar chart starting from 0 - ChartJS React
- ChartJs Stacked Bar Chart
- Stacked bar chart with chartjs with included values
- Chartjs Stacked bar chart not displaying correctly
- Stacked Bar chart fixed width bar chart issue - Chartjs
- Multi-colored legend stacked bar chart Chartjs
- ChartJS add custom tooltip to a stacked bar chart
- Chartjs - Stacked bar chart data order causes invisible other values
- ChartJS - Horizontal Stacked Bar Chart Tooltip being cut off by Bottom of Canvas
- Chartjs - Stacked bar chart blocking other values
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- ChartJS stacked bar chart not displaying as stacked
- how to insert dynamic data from sql into chartjs stacked bar chart javascript
- ChartJS 2.7.3 stacked bar chart with overlap
- Chartjs v3 overlap stacked bar chart with groups
- Draw stacked horizontal bar chart with Average line using ChartJS
- How to configure Chartjs Bar Stacked Chart in Laravel 8
- PrimeFaces: ChartJs stacked bar chart label removal
- Chartjs Bar Chart showing old data when hovering
- Horizontal stacked bar chart with chart.js
- Vertical stacked bar chart with chart.js
- Changing fontFamily on ChartJS bar chart
- chartjs : how to set custom scale in bar chart
- Chartjs Bar Chart Legend
- ChartJS add tooltip to a grouped bar chart
- How to create stacked bar chart using react-chartjs-2?
More Query from same tag
- How to do automatic pan with Chart.js
- Can the colors of bars in a bar chart be varied based on their value?
- Create a line chart using an user input table from a data table in chart.js
- LinearGradient doesnt't work on chartJs canvas for Angular
- Chart.js hidden property
- chartjs-plugin-zoom not working on offline environment javascript .js
- Can I set different fill color based on a given threshold in Chart.js?
- React-chartjs-2 - Each dataset needs a unique key
- Chartjs 2.5.0 -> Labels below the chart
- Is there a way to let Chart.js to round up decimals?
- How to fix ERROR in node_modules/chart.js/types/animation.d.ts(20,37): error TS1005: ',' expected. in chart.js
- Add a click event to the bar chart of chartjs
- How to remove trial version watermark in canvasjs using Jquery (without license)
- Chart.JS - multiple - box - annotations - displays only the last box
- How to change fonts and axis labels for Chart.js image rendering with QuickChart?
- How to get Chart js labels in scientific notation
- How can I update my ChartJS in real time (It's works only when I zoom-in and zoom-out)
- Unwanted line chart ''shadow" using chart.js
- angular-chartjs line chart TypeError: t.merge is not a function
- dyamically constructing javascript object
- Testing Chart.js with Jest/Enzyme - Failed to create chart: can't acquire context from the given item
- Chart.js customization, two Y-Axis overlay, chart area padding, odd tick padding
- Chart.js sciptable options: accessing the values of the data object?
- How to put images/ charts into table?
- Inserting data from arrays into a pie chart (chart.js)
- Creating a chart
- Merge two objects with different keys in chartjs bar
- How to increase the size of the donut or pie chart and keep the legend next to it in chart JS?
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- Styling the middle text of Chart.js's doughnut chart