将json链接到javascript以形成图形
我想显示一个基于数据库数据的图形。我使用PHP和Javascript检索所有数据,然后形成一个图形。这是我的代码,用于将数据转换为jason格式将json链接到javascript以形成图形,javascript,php,mysqli,Javascript,Php,Mysqli,我想显示一个基于数据库数据的图形。我使用PHP和Javascript检索所有数据,然后形成一个图形。这是我的代码,用于将数据转换为jason格式 $totalrow = mysqli_num_rows($result_set); $json='[{'; $i = 0; while($row = mysqli_fetch_assoc($result_set)) { $i++; if($i==1) { $json.= '"name": "datetime", "data": ["'.$row
$totalrow = mysqli_num_rows($result_set);
$json='[{';
$i = 0;
while($row = mysqli_fetch_assoc($result_set)) {
$i++;
if($i==1) {
$json.= '"name": "datetime", "data": ["'.$row['datetime'].'"';
}
else if($i==$totalrow) {
$json.= ']}';
}
else {
$json.= ',"'.$row['datetime'].'"';
}
}
$json.=', {';
$i = 0;
$result_set2 = mysqli_query($conn, $SQLSELECT);
while($row = mysqli_fetch_assoc($result_set2)) {
$i++;
if($i==1) {
$json.= '"name": "PV_Value", "data": ['.$row['PV_Value'];
}
else if($i==$totalrow) {
$json.= ']}';
}
else {
$json.= ','.$row['PV_Value'];
}
}
$json.= ']';
?>
这是我的javascript,用于根据数据库中的数据显示折线图:
$(function () {
$.getJSON('$json', function (data) {
$('#container').highcharts({
chart: {
zoomType: 'x'
},
title: {
text: 'Temperature of PV Value for Process Control'
},
subtitle: {
text: document.ontouchstart === undefined ?
'Click and drag in the plot area to zoom in' : 'Pinch
the chart to zoom in'
},
xAxis: {
type: 'datetime',
events: {
afterSetExtremes: function(e) {
alert("Min: "+e.min+"\n"
+"Max: "+e.max);
}
}
},
yAxis: {
title: {
text: 'PV Value'
}
},
legend: {
enabled: false
},
plotOptions: {
area: {
fillColor: {
linearGradient: {
x1: 0,
y1: 0,
x2: 0,
y2: 1
},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1,
Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},
series: [{
type: 'area',
name: 'USD to EUR',
data: data
}]
});
});
});
问题是我在javascript上的第一行符号($)不是define
$(function () {
请帮助我缩进错误一看,我发现您的代码有以下缺点:
$
错误可能是因为您没有包含jquery库$.getJSON('$json'
应该是$.getJSON('
document.ontouchstart===未定义
应为此document.ontouchstart==='未定义'
您包括jquery了吗?另外,我猜这个
$.getJSON('$json'
应该是$.getJSON('