I want to load a csv file to my html file to plot some data. I have figured out how to plot in html but the next challenge is to import data from a csv file. I am new to html and javascript and would appreciate inputs or even other modules aside from CanvasJS.
<!DOCTYPE HTML>
<html>
<head>
<meta charset ="UTF-8">
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
theme: "light2",
title:{
text: "DOE TEST Chart"
},
axisY:{
includeZero: false
},
data: [{
type: "line",
dataPoints: dataPoints
}]
});
$.get("file:////C:/Users/TestDev/Documents/html_files/js_plots/Sample/zpw.csv", getDataPointsFromCSV);
function getDataPointsFromCSV(csv) {
var csvLines = points = [];
csvLines = csv.split(/[\r?\n]\r|\n]+/);
for (var i = 0; i < csvLines.length; i++) {
if (csvLines[i].length > 0){
points = csvLines[i].split(",");
dataPoints.push({
label: points[0],
y: parseFloat(points[1])
});
}
}
chart.render();
}
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; max-width: 920px; margin: 0px auto;"></div>
<script src="../../canvasjs.min.js"></script>
</body>
</html>
尝试将包含csv文件的Sample文件夹移动到项目中,并使用类似“Sample / zpw.csv”的相对URL
Due to security issues browsers doesn't allow you to read files from local filesystem . However, you can save your CSV in Google-Sheets and export it as CSV and use the link to the CSV in $.get("Google-Sheet-CSV-URL")
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.