我正在try 从我的raspberry pi加载一个由两列组成的csv文件—时间和温度—并用chartjs制作一个图表.在一些教程中,我发现我可以用d3做到这一点,但似乎我对promise 做了一些错误的事情.
var csv = `
21:34:03,17.25
21:35:04,17.18
21:36:03,17.18
21:37:03,17.18
21:38:03,17.18
21:39:03,17.18
21:40:03,17.18
21:41:03,17.12
21:42:03,17.18
21:43:03,17.12
21:44:03,17.12
21:46:03,17.12
21:47:02,17.12
21:48:03,17.12
21:49:02,17.12
21:50:03,17.06
21:51:03,17.06
21:52:03,17.06
21:53:03,17.06
21:54:03,17.06
21:55:04,17.06
21:56:03,17.06
21:57:02,17.06
21:58:03,17.06
22:00:04,17.00
22:01:02,17.00`;
//couldn't find a way to embed the csv into the text function for the snippet
d3.text("temp.csv").then(makeChart);
function makeChart(temp) {
var result = "x, y\n" + temp; //now you have the header
var datos = d3.csvParse(result);
var chart = new Chart('chart', {
type: 'line',
data: {
labels: datos.x,
datasets: [{
data: datos.y
}]
}
});
console.log(this.chart.data);
return this.chart;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<canvas id="chart" width="400" height="400"></canvas>
示例数据(应为临时csv):
21:34:03,17.25
21:35:04,17.18
21:36:03,17.18
21:37:03,17.18
21:38:03,17.18
21:39:03,17.18
21:40:03,17.18
21:41:03,17.12
21:42:03,17.18
21:43:03,17.12
21:44:03,17.12
21:46:03,17.12
21:47:02,17.12
21:48:03,17.12
21:49:02,17.12
21:50:03,17.06
21:51:03,17.06
21:52:03,17.06
21:53:03,17.06
21:54:03,17.06
21:55:04,17.06
21:56:03,17.06
21:57:02,17.06
21:58:03,17.06
22:00:04,17.00
22:01:02,17.00
我不知道还需要添加什么,但我发现的唯一解决方法是完全go 掉d3,尽管我想了解如何在这个示例中使用它的promise .