Rendering Chart from CSV - CanvasJS JavaScript Charts
Rendering Chart from CSV - CanvasJS JavaScript Charts
by canvasjs
HTML
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
JavaScript
var dataSeries = [
{
type: "line",
legendText: "S11",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "S12",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "S21",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "D2",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "A1",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "A2",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "CCond",
showInLegend: true,
dataPoints: []
},
{
type: "line",
legendText: "Temp",
showInLegend: true,
dataPoints: []
}
];
function getDataSeriesFromCSV(csv) {
var csvLines = points = [];
csvLines = csv.split(/[\r?\n|\r|\n]+/);
//for accessing the last row of CSV you can use csvLines[csvLines.length - 1];
for (var i = 0; i < csvLines.length; i++) {
if (csvLines[i].length > 0) {
points = csvLines[i].split(",");
var year = parseInt(points[0].split(" ")[0].split("/")[2]);
var month = parseInt(points[0].split(" ")[0].split("/")[1]);
var day = parseInt(points[0].split(" ")[0].split("/")[0]);
var hour = parseInt(points[0].split(" ")[1].split(":")[0]);
var minute = parseInt(points[0].split(" ")[1].split(":")[1]);
var second = parseInt(points[0].split(" ")[1].split(":")[2]);
var xVal = new Date(year, month, day, hour, minute, second);
dataSeries[0].dataPoints.push({ x: xVal, y: parseFloat(points[1]) }); // S11
dataSeries[1].dataPoints.push({ x: xVal, y: parseFloat(points[2]) }); // S12
dataSeries[2].dataPoints.push({ x: xVal, y: parseFloat(points[3]) }); // S21
dataSeries[3].dataPoints.push({ x: xVal, y: parseFloat(points[4]) }); // D2
dataSeries[4].dataPoints.push({ x: xVal, y: parseFloat(points[5]) }); // A1
dataSeries[5].dataPoints.push({ x: xVal, y: parseFloat(points[6]) }); // A2
dataSeries[6].dataPoints.push({ x: xVal, y: parseFloat(points[7]) }); //...