How Can I Create A Time Series Line Graph In Chart.js?
I created a python script that fetches data from an API to get weather temperature at a certain time The result is a CSV file that looks like this: Time,Temperature 2020-02-15 18:3
Solution 1:
Basically, convert every single file-line string:
2020-02-15 18:37:39,-8.25
into an Object:
{x: "2020-02-15 18:37:39", y: -8.25}
to be stored inside the Chart.js data : []
Array.
Here's an example on how to create a function csvToChartData()
that returns such an Array (to be used like: ... data: csvToChartData(csv)
)
- Trim and split a file string by newline
\n
into alines
array . - Remove titles (the first array key) by using
lines.shift();
- Convert every line to an object
{x: date, y: temperature}
by splitting each line by comma.split(',')
- Pass that newly mapped Array (by using
.map()
) as your chartdata:
const csv = `Time,Temperature
2020-02-15 18:37:39,-8.25
2020-02-15 19:07:39,-8.08
2020-02-15 19:37:39,-8.41
2020-02-15 20:07:39,-8.2`;
constcsvToChartData = csv => {
const lines = csv.trim().split('\n');
lines.shift(); // remove titles (first line)return lines.map(line => {
const [date, temperature] = line.split(',');
return {
x: date,
y: temperature
}
});
};
const ctx = document.querySelector("#line-chart").getContext('2d');
const config = {
type: 'line',
data: {
labels: [],
datasets: [{
data: csvToChartData(csv),
label: "Temperature",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
distribution: 'linear',
}],
title: {
display: false,
}
}
}
};
newChart(ctx, config);
#line-chart {
display: block;
width: 100%;
}
<canvasid="line-chart"></canvas><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
Fetch data dynamically:
To fetch the data dynamically, say every 5 seconds, you could use AJAX and the Fetch API.
Here's the modified JS example given you have a CSV file called temperature.csv
const config = {
type: "line",
data: {
labels: [],
datasets: [{
data: [], // Set initially to empty datalabel: "Temperature",
borderColor: "#3e95cd",
fill: false
}]
},
options: {
scales: {
xAxes: [{
type: "time",
distribution: "linear"
}],
title: {
display: false
}
}
}
};
const ctx = document.querySelector("#line-chart").getContext("2d");
const temperatureChart = newChart(ctx, config);
constcsvToChartData = csv => {
const lines = csv.trim().split("\n");
lines.shift(); // remove titles (first line)return lines.map(line => {
const [date, temperature] = line.split(",");
return {
x: date,
y: temperature
};
});
};
constfetchCSV = () => fetch("temperature.csv")
.then(data => data.text())
.then(csv => {
temperatureChart.data.datasets[0].data = csvToChartData(csv);
temperatureChart.update();
setTimeout(fetchCSV, 5000); // Repeat every 5 sec
});
fetchCSV(); // First fetch!
Post a Comment for "How Can I Create A Time Series Line Graph In Chart.js?"