簡體   English   中英

高庫存圖表-JSON輸入不起作用

[英]Highstock chart - JSON input not working

我正在嘗試使用Highcharts的Highstock創建圖表,但無法弄清楚如何從PHP文件中提供正確的JSON數據。

這是我的HTML文件。 用於獲取getJSON的數據的原始URL是http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?

正在撥打三個不同的電話。 例如,一個是: http : //www.highcharts.com/samples/data/jsonp.php?filename=goog-c.json&callback=?

<html><head>   

    <script src="https://code.highcharts.com/stock/highstock.js"></script>
    <script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

</head><body>

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

    <script>
        $(function () {
            var seriesOptions = [],
                seriesCounter = 0,
                names = ['MSFT', 'AAPL', 'GOOG'];

            /**
             * Create the chart when all data is loaded
             * @returns {undefined}
             */
            function createChart() {

                Highcharts.stockChart('container', {

                    rangeSelector: {
                        selected: 4
                    },

                    yAxis: {
                        labels: {
                            formatter: function () {
                                return (this.value > 0 ? ' + ' : '') + this.value + '%';
                            }   
                        },
                        plotLines: [{
                            value: 0,
                            width: 2,
                            color: 'silver'
                        }]
                    },

                    plotOptions: {
                        series: {
                            compare: 'percent',
                            showInNavigator: true
                        }
                    },

                    tooltip: {
                        pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                        valueDecimals: 2,
                        split: true
                    },

                    series: seriesOptions
                });
            }

            $.each(names, function (i, name) {

            console.log('name: '+name);

                $.getJSON('http://localhost/projects/AGF/testobject.php',    function (data) {

                console.log(data);

                    seriesOptions[i] = {
                        name: name,
                        data: data
                    };

                    // As we're loading the data asynchronously, we don't know what order it will arrive. So
                    // we keep a counter and create the chart when all the data is loaded.
                    seriesCounter += 1;



                    if (seriesCounter === names.length) {
                        createChart();
                    }
                });
            });
        });
    </script>

</body></html>

我只是復制了PHP文件返回的內容,並從我自己的PHP文件testobject.php回顯了該內容。

testobject.php:

 <?php
    echo "?([
    [1258934400000,290.88],
    [1259020800000,291.25])";
 ?>

我將JSON縮短為2個對象,並刪除了注釋。 第一個? Highstock是必需的,它將作為回調參數添加到原始URL中。 每個對象中的第一個數字是日期的整數值。

最終,我將從數據庫中查詢數據並以這種格式輸出。

我的問題是,如果回答基本相同,為什么這不起作用?

謝謝。

highstock不需要問號, ()也不需要,因此您的JSON無效。 該標記適用於JSONP,因為highchart正在從其他域請求數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM