I tried to visualize the data, which stored at Google Sheet, with Chart.js in multiple-axis line chart.
My data look like this:
I tried the following code, but it only showed the first two data (0:00 and 1:00) instead of the whole set data. Also, it has single Y-axis only. May I know what's wrong with my code?
function BuildChart(labels, valuesa, valuesb, chartTitle) {
var data = {
labels: labels,
datasets: [{
label: 'Temperature', // Name the series
data: valuesa,
},
{
label: chartTitle, // Name the series
data: valuesb,
}]
};
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Temperature',
fill: false,
data: valuesa,
backgroundColor: ['rgb(255, 99, 132, 0.8)']
},{
label: 'Relative Humidity',
fill: false,
data: valuesb,
backgroundColor: ['rgb(255, 99, 132, 0.8)'],
options: {
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: false, // Add to prevent default behaviour of full-width/height
scales: {
xAxes: [{
ticks: {
beginAtZero: true,
scaleLabel: {
display: false,
labelString: ''
}
}
}],
yAxes: [{
scaleLabel: {
display: false,
labelString: ''
}
}]
},
},
}
]}})
return myChart;
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var json = JSON.parse(this.response);
console.log(json);
// Map json labels back to values array
var labels = json.feed.entry.map(function (e) {
return e.gsx$tag.$t;
});
// Map json values back to values array
var valuesa = json.feed.entry.map(function (e) {
return e.gsx$dailytemperature.$t
});
// Map json values back to values array
var valuesb = json.feed.entry.map(function (e) {
return e.gsx$dailyrh.$t
});
BuildChart(labels, valuesa, valuesb, "Temperature", "Relative Humidity");
}
};
xhttp.open("GET", "https://spreadsheets.google.com/", false);
xhttp.send();
Your help is highly appreciated. Thank you.