1 回答
TA贡献1893条经验 获得超10个赞
我通过使用实时服务器解决了这个问题
<html>
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4/dist/Chart.min.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="200"></canvas>
<script>
const xlabel=[];
const ytemp=[];
chartIt();
async function chartIt(){
await getDatach();
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels:xlabel,
datasets: [{
label: 'some data',
data: ytemp,
fill:false,
backgroundColor: 'rgba(255, 99, 132, 0.2)' ,
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
}
});
}
async function getDatach(){
const response = await fetch('ZonAnn.Ts+dSST.csv');
const data=await response.text();
const table=data.split('\n').slice(1);
table.forEach(row => {
const columns=row.split(',');
const year=columns[0];
xlabel.push(year);
const temp=columns[1];
ytemp.push( parseFloat (temp)+14);
});
}
</script>
</body>
</html>
添加回答
举报