var chartColors = {
red: 'rgb(255, 99, 132)',
blue: 'rgb(54, 162, 235)'
};
var color = Chart.helpers.color;
var config = {
type: 'bar',
data: {
datasets: [{
type: 'line',
yAxisID: 'temperature',
backgroundColor: 'transparent',
borderColor: chartColors.red,
pointBackgroundColor: chartColors.red,
tension: 0,
fill: false
}, {
yAxisID: 'precipitation',
backgroundColor: color(chartColors.blue).alpha(0.5).rgbString(),
borderColor: 'transparent'
}]
},
plugins: [ChartDataSource],
options: {
title: {
display: true,
text: 'Sheet data source (datapoint) sample'
},
scales: {
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
id: 'temperature',
gridLines: {
drawOnChartArea: false
},
scaleLabel: {
display: true,
labelString: 'Temperature (°C)'
}
}, {
id: 'precipitation',
position: 'right',
gridLines: {
drawOnChartArea: false
},
scaleLabel: {
display: true,
labelString: 'Precipitation (mm)'
}
}]
},
plugins: {
datasource: {
type: 'sheet',
url: 'sample-datapoint.xlsx',
rowMapping: 'datapoint',
datapointLabels: 'Sheet1!A1:C1',
datapointLabelMapping: {
_dataset: 'dataset',
_index: 'month',
x: 'month',
y: 'value'
},
data: 'Sheet1!A2:C15'
}
}
}
};
window.onload = function() {
var ctx = document.getElementById('myChart').getContext('2d');
window.myChart = new Chart(ctx, config);
};
|
A |
B |
C |
1 |
dataset |
month |
value |
2 |
Temperature |
January |
7 |
3 |
Temperature |
February |
7 |
4 |
Temperature |
March |
10 |
5 |
Temperature |
April |
15 |
6 |
Temperature |
May |
20 |
7 |
Temperature |
June |
23 |
8 |
Temperature |
July |
26 |
9 |
Precipitation |
January |
8.1 |
10 |
Precipitation |
February |
14.9 |
11 |
Precipitation |
March |
41.0 |
12 |
Precipitation |
April |
31.4 |
13 |
Precipitation |
May |
42.6 |
14 |
Precipitation |
June |
57.5 |
15 |
Precipitation |
July |
36.0 |
<head>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script src="https://cdn.jsdelivr.net/npm/xlsx@0.14.3/dist/xlsx.full.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datasource@0.1.0"></script>
</head>
<body>
<div>
<canvas id="myChart"></canvas>
</div>
</body>