我对D3.js有点陌生,我正尝试使用从MySQL数据库获得的json数据制作线形图。但是我得到这个错误:
php json错误
也尝试使用从getdata.php文件中获取的数据制作.json文件,但随后出现此错误:
单独的json文件出错
从数据库获取json数据的php代码:
<!DOCTYPE html>
<html>
<head>
<title>Database data to json format</title>
</head>
<body>
<?php
$connect = mysqli_connect("localhost", "**********", "*********", "ismanieji_vazonai");
$sql = "SELECT Data, Temperatura FROM parametrai";
$result = mysqli_query($connect, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo json_encode($json_array);
/*
echo '<pre>';
print_r($json_array);
echo '</pre>';
*/
?>
</body>
</html>
html代码:
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// Adds the svg canvas
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 + ")");
// Get the data
d3.json("getdata.php", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
</script>
</body>
这是我得到的json格式数据的样子:
[{"Data":"2018-12-19 18:06:54","Temperatura":"24.1"},
{"Data":"2018-12-19 18:07:00","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:05","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:10","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:16","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:21","Temperatura":"24.1"},
{"Data":"2018-12-19 18:07:26","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:32","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:37","Temperatura":"23.8"},
{"Data":"2018-12-19 18:07:42","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:47","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:53","Temperatura":"24.2"},
{"Data":"2018-12-19 18:07:58","Temperatura":"24.2"},
{"Data":"2018-12-19 18:08:03","Temperatura":"24.2"},
{"Data":"2018-12-19 18:08:09","Temperatura":"24.2"},
{"Data":"2018-12-19 18:08:14","Temperatura":"24.2"},
{"Data":"2018-12-19 18:08:34","Temperatura":"24.2"},
{"Data":"2018-12-19 18:08:54","Temperatura":"24.2"},
{"Data":"2018-12-19 18:09:15","Temperatura":"24.2"},
{"Data":"2018-12-19 18:09:35","Temperatura":"24.2"},
{"Data":"2018-12-19 18:09:55","Temperatura":"24.2"},
{"Data":"2018-12-19 18:10:16","Temperatura":"24.2"},
{"Data":"2018-12-19 18:10:36","Temperatura":"24.2"},
{"Data":"2018-12-19 18:10:56","Temperatura":"24.1"},
{"Data":"2018-12-19 18:11:17","Temperatura":"24.1"},
{"Data":"2018-12-19 18:11:37","Temperatura":"24.1"},
{"Data":"2018-12-19 18:11:57","Temperatura":"24.1"},
{"Data":"2018-12-19 18:12:17","Temperatura":"24"},
{"Data":"2018-12-19 18:12:38","Temperatura":"24"},
{"Data":"2018-12-19 18:12:58","Temperatura":"24"},
{"Data":"2018-12-19 18:13:18","Temperatura":"24"},
{"Data":"2018-12-19 18:13:39","Temperatura":"24"},
{"Data":"2018-12-19 18:13:59","Temperatura":"24"}]