Chart.js Mixed Chart
Chart.js can create mixed charts composed of two or more different chart types, such as a combination of bar and line charts.
When creating a mixed chart, we specify the chart type on each dataset.
Mixed Chart type The attribute is scatter.
Bar Chart type The attribute is bar, and the line chart type The attribute is line, and type describes the chart type.
const mixedChart = new Chart(ctx, {
data: {
datasets: [{
type: 'bar',
label: '柱形图数据集',
data: [45, 49,52, 48]
}, {
type: 'line',
label: '折线图数据集',
data: [50, 40, 45, 49],
}],
labels: ['一月份', '二月份', '三月份', '四月份']
},
options: options
});
Next, let's create a simple mixed chart:
Example
const
ctx
=
document.
getElementById
(
'myChart'
)
;
const
data
=
{
labels
:
[
'January'
,
'February'
,
'March'
,
'April'
]
,
datasets
:
[
{
type
:
'bar'
,
label
:
'Bar Chart Data Set'
,
data
:
[
45
,
49
,
52
,
48
]
,
borderColor
:
'rgb(255, 99, 132)'
,
backgroundColor
:
'rgba(255, 99, 132, 0.2)'
}
,
{
type
:
'line'
,
label
:
'Line Chart Data Set'
,
data
:
[
50
,
40
,
45
,
49
]
,
fill
:
false
,
borderColor
:
'rgb(54, 162, 235)'
}
]
}
;
const
config
=
{
type
:
'scatter'
,
data
:
data
,
options
:
{
responsive
:
true
,
// Make the chart responsive, adapting to window size changes
maintainAspectRatio
:
false
,
// Maintain the chart's original aspect ratio
scales
:
{
y
:
{
beginAtZero
:
true
}
}
}
}
;
const
myChart
=
new
Chart
(
ctx
,
config
)
;
Try it »
The output of the above example is: