Highstock json数据未显示

时间:2013-06-11 18:03:31

标签: php json highcharts highstock

嘿伙计们,我无法将数据加载到highstock图表中。

我的json.php调用了一个示例MySQL数据库,看起来像这样:

$result = mysql_query("SELECT UNIX_TIMESTAMP(date)*1000 AS timestamp,value from sample") or die('Could not query');

if(mysql_num_rows($result)){
    echo 'Test';

    $first = true;
    $row=mysql_fetch_assoc($result);
    while($row=mysql_fetch_row($result)){

        if($first) {
            $first = false;
        } else {
            echo ',';
        }
        $json_str = json_encode($row, JSON_NUMERIC_CHECK);
        echo $json_str;
    }
    if(array_key_exists('callback', $_GET)){
    $callback = $_GET['callback'];
    echo $callback. '('.$json_str.');';
    }
} else {
    echo '[]';
}

mysql_close($db);

我调用Json.php的index.htm来自示例highstock模板我只是改变了getJson以匹配我的引用。这是代码。非常感谢任何帮助,谢谢。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highstock Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
$(function() {
    $.getJSON('json.php', function(data) {

        // Create the chart
        $('#container').highcharts('StockChart', {


            rangeSelector : {
                selected : 1
            },

            title : {
                text : 'Test'
            },

            series : [{
                name : 'Test',
                data : data,
                marker : {
                    enabled : true,
                    radius : 3
                },
                shadow : true,
                tooltip : {
                    valueDecimals : 2
                }
            }]
        });
    });
});
        </script>
    </head>
    <body>
<script src="js/highstock.js"></script>
<script src="js/modules/exporting.js"></script>

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

此外,我的json以这种方式解析:

  

测试[1370899026000,10],[1370899026000,4],[1368177426000,11],[1370899026000,12],[1370899026000,13],[1370899026000,11],[1370899026000,13],[1370899026000,11 ],[1370899026000,13],[1370899026000,9],[1370899026000,14],[1370899026000,12],[1370899026000,10],[1370899026000,15],[1370899026000,12],[1378826226000,7] [1370899026000,9],[1370899026000,11],[1370899026000,7],[1370899026000,3],[1370899026000,6],[1370899026000,0],[1370899026000,11],[1370899026000,5],[1370899026000 ,9],[1370899026000,7],[1370899026000,8],[1370899026000,8],[1370899026000,9],[1370899026000,13],[1370899026000,11],[1370899026000,10],[1370899026000,13 ],[1370899026000,12],[1370899026000,12],[1370899026000,11],[1370899026000,13],[1370899026000,10],[1370899026000,8],[1370899026000,15],[1370899026000,13] [1370899026000,12],[1370899026000,14],[1370899026000,9],[1370899026000,9],[1370899026000,12],[1370899026000,13],[1370899026000,4],[1370899026000,4],[1370899026000 ,4],[1370899026000,13],[1370899026000,5],[1370899026000,10],[137089 9026000,4],[1370899026000,10],[1370899026000,22],[1370899026000,9],[1370899026000,5],[1370899026000,9],[1370899026000,10],[1370899026000,5],[1370899026000, 7],[1370899026000,10],[1370899026000,5],[1370899026000,7],[1370899026000,9],[1370899026000,9],[1370899026000,10],[1370899026000,6],[1370899026000,6] [1370899026000,6],[1370899026000,12],[1370899026000,7],[1370899026000,12],[1370899026000,8],[1370899026000,13],[1370899026000,12],[1370899026000,9],[ 1370899026000,7],[1370899026000,7],[1370899026000,9],[1370899026000,12],[1370899026000,13],[1370899026000,9],[1370899026000,10],[1370899026000,4],[1370899026000, 11],[1370899026000,12],[1370899026000,13],[1370899026000,11],[1370899026000,13],[1370899026000,11],[1370899026000,13],[1370899026000,9],[1370899026000,14] [1370899026000,12],[1370899026000,10],[1370899026000,15],[1370899026000,12],[1370899026000,7],[1370899026000,9],[1370899026000,11],[1370899026000,7],[ 1370899026000,3],[1370899026000,6],[1370899026000,0],[1370899026000,11],[13708990 26000,5],[1370899026000,9],[1370899026000,7],[1370899026000,8],[1370899026000,8],[1370899026000,9],[1370899026000,13],[1370899026000,11],[1370899026000, 10],[1370899026000,13],[1370899026000,12],[1370899026000,12],[1370899026000,11],[1370899026000,13],[1370899026000,10],[1370899026000,8],[1370899026000,15] [1370899026000,13],[1370899026000,12],[1370899026000,14],[1370899026000,9],[1370899026000,9],[1370899026000,12],[1370899026000,13],[1370899026000,4],[ 1370899026000,4],[1370899026000,4],[1370899026000,13],[1370899026000,5],[1370899026000,10],[1370899026000,4],[1370899026000,10],[1370899026000,22],[1370899026000, 9],[1370899026000,5],[1370899026000,9],[1370899026000,10],[1370899026000,5],[1370899026000,7]

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

请查看http://docs.highcharts.com/#preprocessing-data-from-a-database并查看json_encode的简单示例:

    tmp = array();

$tmp[] = array('A',5);
$tmp[] = array('B',6);
$tmp[] = array('C',1);
$tmp[] = array('D',2);
$rows = array();

for($i=0; $i<count($tmp); $i++)
{
    $row['name'] = $tmp[$i][0];
    $row['data'] = array($tmp[$i][1]);
    array_push($rows,$row);
}

print json_encode($rows);

Highcharts:

$(document).ready(function() {
            var options = {
                chart: {
                    renderTo: 'container',
                    type: 'column'
                        },
                series: [{
                    name: 'Browser share',
                    data: []
                }]
            }

            $.getJSON("datavotecolum.php", function(json) {
          console.log(json);
                options.series = json;
                chart = new Highcharts.Chart(options,function(chart){

               console.log(chart.series);
               });
            });



        });