实际上我想使用D3来创建图表以进行数据可视化。我的数据驻留在 MySQL 数据库中,我通过 python/django 进行交互。我需要使用这些数据创建 D3 图表。我发现有两种方法可以做同样的事情。
- 创建一个具有 x 和 y 坐标的字典数组,并使用它来创建图表
- 在运行时创建包含数据的 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/