0

嘿伙计们,我无法将我的数据加载到 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,13] 1370899026000,13],[1370899026000,9],[1370899026000,14],[1370899026000,12],[1370899026000,10],[1370899026000,10],[1370899026000,15] 9],[1370899026000,11],[1370899026000,7],[1370899026000,3],[1370899026000,6],[1370899026000,00 ,[1370899026000,7],[1370899026000,8],[1370899026000,8],[1370899026000,9],[1370899026000,13] 1370899026000,12],[1370899026000,12],[1370899026000,11],[1370899026000,13],[1370899026000,10] 12],[1370899026000,14],[1370899026000,9],[1370899026000,9],[1370899026000,12],[1370899026000,13],[1370899026000,4],[1370899026000,4],[1370899026000,4],[13708990260002600026000,00026000,13] ,[1370899026000,10],[1370899026000,22],[1370899026000,9],[1370899026000,5],[1370899026000,00026000,9] 1370899026000,10],[1370899026000,5],[1370899026000,7],[1370899026000,9],[1370899026000,9],[1370899026000,9] 6],[1370899026000,12],[1370899026000,7],[1370899026000,12],[1370899026000,8],[137089902600026000,00026000,13],[1370899026000,13],[1370899026000,12] ,[1370899026000,7],[1370899026000,9],[1370899026000,12],[1370899026000,13] 1370899026000,12],[1370899026000,13],[1370899026000,11],[1370899026000,13],[1370899026000,11],[1370899026000,13],[1370899026000,9],[1370899026000,14],[1370899026000,12],[1370899026000,10] ,9],[1370899026000,11],[1370899026000,7],[1370899026000,3],[1370899026000,6],[1370899026000],[1370899026000] ],[1370899026000,7],[1370899026000,8],[1370899026000,8],[1370899026000,9],[1370899026000,13],[1370899026000,13],[1370899026000,11] [1370899026000,12],[1370899026000,12],[1370899026000,11],[1370899026000,13],[1370899026000,10] ,12],[1370899026000,14],[1370899026000,9],[1370899026000,9],[1370899026000,12],[1370899026000,13] ],[1370899026000,13],[1370899026000,5],[1370899026000,10],[1370899026000,4],[1370899026000,10],[1370899026000,22],[1370899026000,9],[1370899026000,5],[1370899026000,9],[1370899026000,10],[137026000,10],[137026000,10],[1370],089028900266,

4

2 回答 2

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);

高图:

$(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);
               });
            });



        });   
于 2013-06-12T12:50:26.317 回答
0

尝试关闭

[]

Highstock 小提琴:http: //jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/areaspline/

该小提琴的数据: http ://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback= ?

于 2013-06-11T21:09:56.817 回答