我正在尝试使用json使用setdata方法更新morris.js图。在通过控制台显示json时,这看起来不错,但当我执行morris setdata时,它只接受未定义的值实际上,图表仍然是空白的
<script>
var MagD = new Morris.Line({
element : 'chart',
data:[<?php echo $chart_data; ?>],
xkey:'hora',
ykeys:['mag'],
labels:['magnitud'],
hideHover:'auto',
stacked:true,
resize: true,
parseTime: false
});
</script>
<script>
$(document).ready(function(){
$('#ActBtn').click(function(){
$("#anoG option:selected").each(function () {
ano = $(this).val();
});
$("#mesG option:selected").each(function () {
mes = $(this).val();
});
$("#diaG option:selected").each(function () {
dia = $(this).val();
$.ajax({
type : 'POST',
dataType:'json',
url : 'getGrafica.php',
data : { mes: mes, ano: ano, dia:dia },
success: function(json){
var nuevaData = JSON.parse(JSON.stringify(json));
console.log(json);
MagD.setData(JSON.parse(JSON.stringify(nuevaData)));
},
error: function(json){
alert("Something went wrong, please refresh");
}
});
});
{
});
});
</script>
我用于post方法的php如下
<?php
$conexion=mysqli_connect('localhost','root','','luz');
$ano = $_POST['ano'];
$mes = $_POST['mes'];
$dia = $_POST['dia'];
$query = "SELECT * FROM sensor WHERE dia = $dia AND mes = $mes AND ano = $ano";
$result = mysqli_query($conexion, $query);
$chart_data = '';
while($row = mysqli_fetch_array($result))
{
$chart_data .= "{ hora:'".$row["hora"]."', mag:".$row["magnitud"]."}, ";
}
$chart_data = substr($chart_data, 0, -2);
$chart_data = json_encode("[".$chart_data."]");
echo($chart_data)
?>
当我在控制台中检查json对象时,它会给我以下数据
[{ hora:'02:15:30', mag:15.05}, { hora:'02:26:31', mag:15.05}, { hora:'02:27:52',
mag:15.11}, { hora:'02:29:01', mag:15.28}]
然后是价值观我的图的属性仍然未定义,但它不会给我带来任何错误
暂无答案!
目前还没有任何答案,快来回答吧!