14👍
✅
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`;
const csvToChartData = 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,
}
}
}
};
new Chart(ctx, config);
#line-chart {
display: block;
width: 100%;
}
<canvas id="line-chart"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="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 data
label: "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 = new Chart(ctx, config);
const csvToChartData = 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 fetchCSV = () => 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!
Source:stackexchange.com