如果我从mysql数据库中读取数据,为什么直方图不显示


Why is the histogram not displaying if I read the data from a mysql database

我想根据这个例子重现一个直方图http://bl.ocks.org/mbostock/3048450但是从sql表中读取输入数据。将数据读取为"值"是可行的,但只有当我从SQL中读取数据后发出警报时,浏览器中才会显示条形图。

我假设svg是在读取所有数据之前呈现的?

var values = [];
var counter = 0;
d3.json("php/data2.php", function(error, arrw) {
    arrw.forEach(function(d) {
        values.push(parseFloat(arrw[counter]["close"]));
        counter++;
    });
}); 
alert(); // without this alert the bars are not displayed in the svg

尝试使用D3库queue()来计时更新。

所以类似于:

queue()
  .defer(d3.json, "php/data2.php")
  .await(ready);
function ready(error, arrw) {
  //now you can use the data set in your general update paradigm
  //and did not have to wait the data to load sequentially
 arrw.forEach(function(d) {
        values.push(parseFloat(arrw[counter]["close"]));
        counter++;
});
}

有关队列的更多信息

有关D3和mySQL的更多信息。

在加载完所有doms($(document).ready())后运行此脚本如何?请尝试并祝您好运。