使用Chart.js库。程序仅在Microsoft Edge浏览器中有效。其他浏览器仅给出黑色画布Chart.js版本2.9.3“ https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js”
错误:data1.js:30 Fetch API无法加载file:/// C:/Users/avtor/Documents/Job/stock_bot/myChart/myChart1/intraday_5min_MSFT.csv。对于CORS请求,URL方案必须为“ http”或“ https”。
getData @ data1.js:30data1.js:30未捕获(承诺中)TypeError:无法获取在getData(data1.js:30)在getChart1(data1.js:5)在data1.js:3
<!DOCTYPE HTML>
<html>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.js" ></script>
<head>
<title>Money Thingy</title>
</head>
<body>
<canvas id="myChart" style='height: 100;width: 800;background:#282c34;z-index: -50;'></canvas>
<script src="data1.js"> </script>
</body>
</html>
js部分(从CSV文件读取数据)
const xLabels=[];
const yTemps =[];
getChart1();
async function getChart1(){
await getData();
var ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: xLabels,
datasets: [{
label: 'MSFT',
data: yTemps,
backgroundColor: [
'rgba(255, 99, 132, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)'
],
borderWidth: 1
}]
}
});
}
async function getData() {
const response = await fetch ('intraday_5min_MSFT.csv');
const data = await response.text();
const table = data.split('\n').slice (1);
table.forEach(row => {
const columns= row.split(',');
const time =columns[0];
xLabels.push(time);
const volume = columns[4];
yTemps.push (volume);
console.log(time,volume);
});
因此您可以通过两种方法解决此问题。安装apache server
并从localhost.
安装apache中提供文件,并将文件移至Linux中的\var\www\html
和Windows中的/xampp/htdocs/
。
或者您可以在IDE中安装live server
扩展名,以便从服务器加载文件。