Using ChartJS 2.9.4, why are these 2 charts different?
<div >
<canvas id="topcanvas"></canvas>
<canvas id="bottomcanvas"></canvas>
</div>
var leftdata = {
labels: [0, 1, 2, 3, 4, 5],
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var rightdata = {
labels: [0, 1, 2, 3, "4", 5], // the #4 being a string is the only difference
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var options = {
scales: {
xAxes: [{
id: "A",
display: false,
barPercentage: 1.25,
ticks: {
max: 4
}
},
{
id: "B",
display: false,
stacked: true,
offset: true,
barPercentage: 1.25,
ticks: {
max: 4
}
},
{
display: true,
ticks: {
autoSkip: false,
max: 5
}
}
],
yAxes: [{
id: "bar-y-axis1",
ticks: {
beginAtZero: true
}
}]
}
};
var leftctx = document.getElementById("topcanvas").getContext("2d");
new Chart(leftctx, {
type: 'bar',
data: leftdata,
options: options
});
var rightctx = document.getElementById("bottomcanvas").getContext("2d");
new Chart(rightctx, {
type: 'bar',
data: rightdata,
options: options
});
Link to JSFiddle
click(function(){ barChart. data. datasets[0]. backgroundColor = "rgba(192,192,192,1)"; createNumbers(); console.
Bar chart Bar charts are created by setting type to bar (to flip the direction of the bars, set type to horizontalBar ). The colors of the bars are set by passing one color to backgroundColor (all bars will have the same color), or an array of colors.
What is a stacked bar chart? A stacked bar chart is a type of bar graph that represents the proportional contribution of individual data points in comparison to a total. The height or length of each bar represents how much each group contributes to the total.
This behaviour is fixed in v3 of the lib
var leftdata = {
labels: [0, 1, 2, 3, 4, 5],
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var rightdata = {
labels: [0, 1, 2, 3, "4", 5], // the #4 being a string is the only difference
datasets: [{
label: "A",
backgroundColor: 'rgba(0, 0, 255, 0.5)',
data: [12, 19, 3, 5, 1],
xAxisID: "A"
},
{
label: "B",
backgroundColor: 'rgba(0, 255, 0, 0.5)',
data: [14, 19, 6, 2, 4],
xAxisID: "B"
}
]
};
var options = {
scales: {
A: {
display: false,
barPercentage: 1.25,
ticks: {
max: 4
},
position: 'bottom'
},
B: {
display: false,
stacked: true,
offset: true,
barPercentage: 1.25,
position: 'bottom',
ticks: {
max: 4
}
},
x: {
display: true,
ticks: {
autoSkip: false,
max: 5
}
},
y: {
id: "bar-y-axis1",
ticks: {
beginAtZero: true
}
}
}
};
var leftctx = document.getElementById("topcanvas").getContext("2d");
new Chart(leftctx, {
type: 'bar',
data: leftdata,
options: options
});
var rightctx = document.getElementById("bottomcanvas").getContext("2d");
new Chart(rightctx, {
type: 'bar',
data: rightdata,
options: options
});
<div class="grid">
<canvas id="topcanvas"></canvas>
<canvas id="bottomcanvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With