javascript - 带有 d3.json(bardata.php) 的空白网页,但适用于 d3.json(bardata.json)

标签 javascript php json d3.js

图表在使用 d3.json(bardata.json) 时正常创建,但在使用 d3.json(bardata.php) 时出现空白网页(无图表)-- bardata.json 是 bardata.php 回显的输出文件

这是bardata.php文件

<?php
$username = "root"; 
$password = "nk";   
$host = "localhost";
$database="homedb";

$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);

$myquery = "
SELECT  `date`, `value` FROM  `bar_data`
";
$query = mysql_query($myquery);

if ( ! $query ) {
    echo mysql_error();
    die;
}

$data = array();

for ($x = 0; $x < mysql_num_rows($query); $x++) {
    $data[] = mysql_fetch_assoc($query);
}

echo json_encode($data);     

mysql_close($server);
?>

这是 bardata.json

[{"date":"2013-01","value":"53"},{"date":"2013-02","value":"165"},{"date":"2013-03","value":"269"},{"date":"2013-04","value":"344"},{"date":"2013-05","value":"376"},{"date":"2013-06","value":"410"},{"date":"2013-07","value":"421"},{"date":"2013-08","value":"405"},{"date":"2013-09","value":"376"},{"date":"2013-10","value":"359"},{"date":"2013-11","value":"392"},{"date":"2013-12","value":"433"},{"date":"2014-01","value":"455"},{"date":"2014-02","value":"478"}]

这是我的 index.html

<!DOCTYPE html>
<meta charset="utf-8">

<style> /* set the CSS */

body { font: 12px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}

</style>

<body>

<script src="http://d3js.org/d3.v3.js"></script>

<script>

var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = 600 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%Y-%m").parse;

var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

var y = d3.scale.linear().range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%Y-%m"));

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);

var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", 
      "translate(" + margin.left + "," + margin.top + ")");

//getting blank web browser when passing .php output

d3.json("bardata.php", function(error, data) {

    if (error) return console.warn(error);
    console.log(data);
    data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.value = +d.value;
});

//if I use .json than its working, the same .json which i get by echoing the bardata.php output

/*d3.json("bardata.json", function(error, data) {

    if (error) return console.warn(error);
    console.log(data);
    data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.value = +d.value;
});*/    

x.domain(data.map(function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis)
  .selectAll("text")
  .style("text-anchor", "end")
  .attr("dx", "-.8em")
  .attr("dy", "-.55em")
  .attr("transform", "rotate(-90)" );

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", ".71em")
  .style("text-anchor", "end")
  .text("Value ($)");

svg.selectAll("bar")
  .data(data)
  .enter().append("rect")
  .style("fill", "steelblue")
  .attr("x", function(d) { return x(d.date); })
  .attr("width", x.rangeBand())
  .attr("y", function(d) { return y(d.value); })
  .attr("height", function(d) { return height - y(d.value); });

});

</script>

</body>

我用的这个库对吗?或者我做错了什么? ;所有文件 bardata.json、bardata.php、index.html 都在同一目录中。

当调试 firefox 浏览器时,在这一行出现这个错误, “如果(错误)返回控制台。警告(错误);”

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Stack trace:
d3_json@http://d3js.org/d3.v3.js:9488:12
respond@http://d3js.org/d3.v3.js:1939:20

我是 d3.js 的新手。谢谢。

最佳答案

在重新创建您的设置并对其进行测试后,它确实可以毫无问题地加载和创建条形图。我已经在 Chrome、Firefox、Edge、Safari 上对其进行了测试,它们都可以正常工作。检查编码以确保它是 UTF-8,以便 json_encode 确实选择数组。使用 d3.js 的本地副本进行测试。

关于javascript - 带有 d3.json(bardata.php) 的空白网页,但适用于 d3.json(bardata.json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417807/

相关文章:

php - mysql select匹配所有数组元素

json - Geom 到简单的 json

c# MVC 4 Web API - 从 JSON 字符串返回对象

javascript - 在数组中搜索 javascript

javascript - meteor 计时器中的 Meteor.setTimeout() 问题?

javascript - 从时间服务器获取时间并更新移动设备时间

javascript - 使用 Socket.io 传递 JavaScript 变量

javascript - 防止 Controller 中的文本提交失败

php - 尝试打开 MySQL 连接时出现 MySQL 错误

php - 从字符串中获取特定字符串?