ChartJS无法与CSV源的D3合作

发布于 2025-02-04 08:06:22 字数 1883 浏览 3 评论 0原文

我正在尝试从我的Raspberry Pi加载一个由两个列组成的CSV文件,并使用Chartjs制作图表。在某些教程中,我发现我可以使用D3做到这一点,但是看来我在承诺上做错了什么。

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>

示例数据(应该是TEMP.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

我不知道要添加什么,但是我发现的唯一解决方法是完全摆脱D3,尽管我想了解如何在此示例中使用其承诺。

I'm attempting to load a csv file composed of two columns - time and temperature - from my raspberry pi and make a chart with chartjs. In some tutorial I found that I could do that with d3 but it seems that I'm doing something wrong with the promises.

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>

Sample data (this is supposed to be temp.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

I don't know what else to add but the only workaround I found was to get rid of d3 altogether though I'd like to understand how to use its promises in this example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2025-02-11 08:06:22

您的承诺还可以(即d3.text(“ temp.csv”)。然后(makechart);),但是您在makechart中的逻辑有一些问题:

  1. 删除空间在y您添加的标头之间 - 否则,它创建了像这样的对象键“ y”,而不仅仅是y
  2. y值需要转换为float,因为csvparse默认为文本,而无需转换函数
  3. datos.x.x and <代码> datos.y 不要将任何内容都称为datos没有特定的xy键 - 它是它的数组对象,每个对象具有Xy键。 的这些键的数组

因此,您可以使用MAP来提取以下工作示例

var url = "https://gist.githubusercontent.com/robinmackenzie/ff787ddb871cef050d7e6279991a0f07/raw/4ce35de3a9bef27363d83e7da2f3365ffa8d2345/data.csv";

d3.text(url)
  .then(csv => makeChart(csv))
  
function makeChart(temp) {
  // add the header (no space between , and y
  var result = "x,y\n" + temp; //now you have the header

  // csv parse - need to convert y values to float
  var datos = d3.csvParse(result, d => {
    return {
      x: d.x,
      y: parseFloat(d.y)
    }
  });

  // render chart
  var chart = new Chart('chart', {
    type: 'line',
    data: {
      labels: datos.map(d => d.x), // <-- just get x values
      datasets: [{
        data: datos.map(d => d.y) // <-- just get y values
      }]
    }
  });

}
<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="100"></canvas>

Your promise is OK (i.e. d3.text("temp.csv").then(makeChart);) but your logic in makeChart has a few issues:

  1. Remove the space between the , and y in the header you add - otherwise it's creating a object key like this " y" instead of just y
  2. The y values need to be converted to float because csvParse defaults to values as text without a conversion function
  3. datos.x and datos.y do not refer to anything as datos has no specific x and y keys - it is an array of objects, each of which has x and y keys. So, you can use map to extract the arrays of those keys

Working example below:

var url = "https://gist.githubusercontent.com/robinmackenzie/ff787ddb871cef050d7e6279991a0f07/raw/4ce35de3a9bef27363d83e7da2f3365ffa8d2345/data.csv";

d3.text(url)
  .then(csv => makeChart(csv))
  
function makeChart(temp) {
  // add the header (no space between , and y
  var result = "x,y\n" + temp; //now you have the header

  // csv parse - need to convert y values to float
  var datos = d3.csvParse(result, d => {
    return {
      x: d.x,
      y: parseFloat(d.y)
    }
  });

  // render chart
  var chart = new Chart('chart', {
    type: 'line',
    data: {
      labels: datos.map(d => d.x), // <-- just get x values
      datasets: [{
        data: datos.map(d => d.y) // <-- just get y values
      }]
    }
  });

}
<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="100"></canvas>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文