Highcharts和JSON问题

时间:2013-06-30 01:04:12

标签: php javascript mysql json highcharts

我正在制作一个时间线图,我对数据有问题我对数据库的查询结果没有产生双引号(“)。他们留下了我如何得到结果的样本

  

[{“name”:“Tv Cable”,“data”:[“[1370925000000,100]”]},{“name”:“Tv Satelital”,“data”:[“[1365654600000,100] ”, “[1368505800000,200]”, “[1370320200000,1500]”, “[1370925000000,500]”, “[1370925000000,560]”, “[1370925000000,50]”, “[1370925000000,500]”, “[1370925000000,800]”,“[1370925000000,500]”,“[1373776200000,1000]”]},{“名称”:“电视互联网”,“数据”:[“[1371097800000,500]”]} ,{“name”:“Tv Telefonia”}]

我需要遵循

  

[{“name”:“Tv Cable”,“data”:[[1370925000000,100]]},{“name”:“Tv Satelital”,“data”:[[1365654600000,100],[1368505800000 ,200],[1370320200000,1500],[1370925000000,500],[1370925000000,560],[1370925000000,50],[1370925000000,500],[1370925000000,800],[1370925000000,500],[1373776200000,1000 ]],{“name”:“Tv Internet”,“data”:[[1371097800000,500]]},{“name”:“Tv Telefonia”}]

我在尝试修复它的时候,我没有设法做到这一点?

我让我的图形代码看看我做了什么

sql.php

<?php
$fecha = date("d-m-Y"); // fecha actual 
$ano = date("Y"); // A単o actual 
$mes = date("m"); // Mes actual 
$dia = date("d"); // Dia actual  
$mes_inicio= $mes-2;

$con = mysql_connect("localhost","xyolcarx_inter","xYolcar19572059");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("xyolcarx_inter", $con);
$rows = array();
for ($i=1;$i<=4;$i++){
$sth = mysql_query("SELECT monto,(unix_timestamp(fecha)*1000) FROM ventas WHERE codigo_ser = '".$i."' ORDER BY fecha ASC ");
$sth2 = mysql_query("SELECT * FROM servicio WHERE codigo_ser= '".$i."'");
while($r2 = mysql_fetch_array($sth2)) {
$rows[$i]['name'] = $r2['servicio'];
}
while($r = mysql_fetch_array($sth)) {
$rows[$i]['data'][] = '['.$r['(unix_timestamp(fecha)*1000)'].','.$r['monto'].']';
}
}
$result = array();
for ($i=1;$i<=4;$i++){  
array_push($result,$rows[$i]);
}
print json_encode($result, JSON_NUMERIC_CHECK);

mysql_close($con);
?>

的index.php

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>

  </style>



<script type='text/javascript'>//<![CDATA[ 

$(function() {

    $.getJSON('sql.php', function(data) {
        // Create the chart
        window.chart = new Highcharts.StockChart({
            chart : {
                renderTo : 'container'
            },

            rangeSelector : {
                selected : 1
            },

            title : {
                text : 'AAPL Stock Price'
            },

            series : data
        });
    });

});

//]]>  

</script>


</head>
<body>
  <script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>

<div id="container" style="height: 500px; min-width: 500px"></div>

</body>


</html>

1 个答案:

答案 0 :(得分:1)

这一行

$rows[$i]['data'][] = '['.$r['(unix_timestamp(fecha)*1000)'].','.$r['monto'].']';

应该是这样的

$rows[$i]['data'][] = array($r['(unix_timestamp(fecha)*1000)'],$r['monto']);

不要自己构建json。只需构建数组结构,让json_encode为您完成剩下的工作。


修改更多建议:

改变这个:

SELECT monto,(unix_timestamp(fecha)*1000) FROM ventas...

到此:

SELECT monto, (unix_timestamp(fecha)*1000) AS fecha_timestamp FROM ventas...

所以你可以在php中轻松访问它:

$rows[$i]['data'][] = array($r['fecha_timestamp'],$r['monto']);

修改更多建议:

Mysql有时会遇到unix_timestamp(fecha)*1000的性能问题,因为它必须将它认为的32位int转换为64位int,这很糟糕。在php中这样做。

改变这个:

SELECT monto, (unix_timestamp(fecha)*1000) AS fecha_timestamp FROM ventas...

到此:

SELECT monto, unix_timestamp(fecha) AS fecha_timestamp FROM ventas...

所以你可以在php中完成这项工作:

$rows[$i]['data'][] = array($r['fecha_timestamp']*1000,$r['monto']);