Reputation: 1626
I have a demo example which looks like below
I have two datasets the one is bar and the other is line,
datasets: [
{
label: 'Line chart',
type:'line',
borderColor:'red',
data: [
1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4
],
fill:false,
borderWidth: 1
},
{
label: '# of Votes',
type:'Bar chart',
data: [
12,null,null,null,13,null,null,null,17,null,null,null,23,null,null,null
],
borderWidth: 1
},
]
Line chart represents each day and bars represent each month.I want a bar to cover 4 labels(to point that its monthly).So my desired behavior would be like below
How can I achieve this?My JsFiddle example : https://jsfiddle.net/jn52swdo/
Upvotes: 1
Views: 408
Reputation: 31439
You can achieve this by adding a second x
axis where you link to.
Example:
var options = {
type: 'bar',
data: {
labels: ["Month-1", "", "", "", "", "", "Month-2", "", "", "", "", "", "Month-3", "", "", "", "", "", "Month-4", "", "", "", "", ""],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5],
borderWidth: 1,
xAxisID: 'x2',
categoryPercentage: 1,
}]
},
options: {
scales: {
x: {},
x2: {
display: false,
labels: ["", "", "", ""]
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.0/chart.js"></script>
</body>
Upvotes: 1