javascript - 从 MySQL 加载数据以在 D3 中用于创建折线图

标签 javascript python mysql django d3.js

实际上我想使用D3来创建图表以进行数据可视化。我的数据驻留在 MySQL 数据库中,我通过 python/django 进行交互。我需要使用这些数据创建 D3 图表。我发现有两种方法可以做同样的事情。

  1. 创建一个具有 x 和 y 坐标的字典数组,并使用它来创建图表
  2. 在运行时创建包含数据的 csv 或 tsv 文件并使用相同的文件

我的问题是有没有更好的方法来实现同样的目标?上述两种方法中,哪一种在运行时间方面更好,为什么?

如果您能给我指出一些已完成的示例或教程,那就太好了

最佳答案

好吧,如果您能够从 mysql 数据库加载数据,那么无论如何您都会在客户端的对象(数组、映射等)中获取它。

因此,没有必要从中写入 csv 文件,然后再次提取相同的数据。

你可以这样做:

mysql.load("whatever",function(err, yourData){
  if(err) return err;
  
  d3.select(".somechart").selectAll(".someclass#someid")
    .data(yourData) // Feed data to your selection
    .enter() // enter the selection to modify it
    .append(".someclass#someid") // append some more elements to your selection
    // change attribute of each new element of the selection using yourData
    .attr("someattribute",function(d,i){ 
      // i is the index of the element in the selection
      // d is equivalent to yourData[i]
      // if yourData is an array of {x,y} then
      return d.x;
    }
    .attr("someOtherAttribute",function(d,i){
      return d.y;
    }
  
  });
}

我建议你(至少)看看this d3js tutorial ,它将帮助您更好地掌握如何使用d3。

关于javascript - 从 MySQL 加载数据以在 D3 中用于创建折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368973/

相关文章:

python - opencv python 文档

mysql - 如何找到数据库中相关表之间的孤立条目?

php - SQL查询如何从多行返回数据

javascript - 在 Meteor.js 中填写向导表单后创建用户

javascript - 滚动时在视口(viewport)上正确对齐 div

javascript - QML:如何等到组件因属性更改而重新绘制?

python - 在 Python 中通过 SOCKS5 代理代理 UDP

javascript - 如何改进 JavaScript 中的 if else 结构?

python - 并行运行多个子进程,在屏幕上显示所有输出,直到完成

c# - 如何从 sql 数据库中检索 asp.net mvc 中的行数?