Does anyone know how to get multiple x-Axes labels in a react-chartjs-2 chart? I see several examples where people are using chart.js but not react-chartjs-2. I suspect react-chartjs-2 may behave slightly differently when it comes to options.scales.xAxes
.
I'm using chart.js v3.6.1 & react-chartjs-2 v4.0.0
I found an example using chart.js, but not react-chartjs-2, that represents what I want. The below code I adapted to use react-chartjs-2. The label's don't display properly even though I didn't alter the "options" or "dataset" from the original working example.
import React from 'react'
import { Bar } from 'react-chartjs-2';
import 'chart.js/auto';
import { Chart } from 'react-chartjs-2';
const TaskProgress3 = () => {
const data = {
labels: ["January;2015", "February;2015", "March;2015", "January;2016", "February;2016", "March;2016"],
datasets: [{
id: 1,
label: '# of Votes',
xAxisID:'xAxis1',
data: [12, 19, 3, 5, 2, 3]
}]
};
const options = {
scales:{
xAxes:[
{
id:'xAxis1',
type:"category",
ticks:{
callback:function(label){
var month = label.split(";")[0];
return month;
}
}
},
{
id:'xAxis2',
type:"category",
gridLines: {
drawOnChartArea: false,
},
ticks:{
callback:function(label){
var month = label.split(";")[0];
var year = label.split(";")[1];
if(month === "February"){
return year;
}else{
return "";
}
}
}
}],
yAxes:[{
ticks:{
beginAtZero:true
}
}]
}
};
return (
<>
<div>Bar Chart</div>
<Bar
datasetIdKey='id'
data={data}
options={options}
height="100px"
width="600px"
/>
</>
)
}
export default TaskProgress3;