从PHP生成StockChart的json数据

时间:2014-10-20 14:00:49

标签: php highcharts

我无法让我的生成器为StockChart工作,是不是因为生成器语法输出?

Javascript

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


            rangeSelector : {
                selected : 1
            },

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

            series : [{
                name : 'AAPL Stock Price',
                data : 'http://127.0.0.1:1234/zwrotkav2/api/stockchart.php',
                marker : {
                    enabled : true,
                    radius : 3
                },
                shadow : true,
                tooltip : {
                    valueDecimals : 2
                }
            }]
        });

});

SQL结果。

Date | totalCOunt
0000-00-00 | 14
2013-10-13 |  3
2013-10-16 |  1
2013-10-17 |  1
2013-10-18 |  2
2013-10-25 |  4

我的Php生成器

require_once("../db_config.php");

  try { 
    $sth = $db->prepare("SELECT  DATE(date) Date, COUNT(DISTINCT number) totalCOunt FROM numbers GROUP   BY  DATE(date)");
    $sth->execute();

    /* Fetch all of the remaining rows in the result set */
    // print("Fetch all of the remaining rows in the result set:\n");
    $result = $sth->fetchAll();

     } 
    catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());} 

try{
        foreach ($result as $row) {
        $date =strtotime($row['Date'])*1000;
        echo "[".$date.",". $row['totalCOunt']."],<br>"; 
        }
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());} 

发电机输出 (例如可接受的json http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?)

[1384038000000,2],
[1384124400000,1],
[1384210800000,1],
[1384556400000,1],
[1384729200000,4],
[1385247600000,2],
[1385334000000,2],
[1385420400000,2],
[1385506800000,3],
[1385593200000,1],

编辑2。

使用Json_Encode - 生成器输出

[[0,"14"],[1381788000000,"3"],[1381874400000,"1"],[1381960800000,"1"],[1382047200000,"2"],[1382652000000,"4"],[1383001200000,"1"],[1383174000000,"1"],[1383433200000,"1"],[1383606000000,"1"],[1384038000000,"2"],[1384124400000,"1"],[1384210800000,"1"],

php loop

foreach ($result as $row) {
            $date =strtotime($row['Date'])*1000;
            $return[] = array($date,$row['totalCOunt']); 
}

1 个答案:

答案 0 :(得分:1)

您的编辑看起来更好。现在问题就在于那条线:

data : 'http://127.0.0.1:1234/zwrotkav2/api/stockchart.php',

那什么都不做。要加载数据,您应该使用AJAX。 Here您可以找到相关的教程。