简体   繁体   English

高库存图表-JSON输入不起作用

[英]Highstock chart - JSON input not working

I am trying to create a chart with Highstock from Highcharts, but can't figure out how to supply the correct JSON data from the PHP file. 我正在尝试使用Highcharts的Highstock创建图表,但无法弄清楚如何从PHP文件中提供正确的JSON数据。

This is my HTML file. 这是我的HTML文件。 The original URL used for fetching the data in getJSON is http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=? 用于获取getJSON的数据的原始URL是http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?

Three different calls are being made. 正在拨打三个不同的电话。 For example, one is: http://www.highcharts.com/samples/data/jsonp.php?filename=goog-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>

I simply copied the content from that the PHP file is returning and echoed it from my own PHP file testobject.php 我只是复制了PHP文件返回的内容,并从我自己的PHP文件testobject.php回显了该内容。

testobject.php: testobject.php:

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

I shortened the JSON to 2 objects and removed the comments. 我将JSON缩短为2个对象,并删除了注释。 The first ? 第一个? is required by the Highstock, which is added as a callback parameter in the original URL. Highstock是必需的,它将作为回调参数添加到原始URL中。 The first number in each object is the integer value of the date. 每个对象中的第一个数字是日期的整数值。

Eventually I will be the querying the data from a database and outputting it in this format. 最终,我将从数据库中查询数据并以这种格式输出。

My question is why isn't this working, if the responses are essentially the same? 我的问题是,如果回答基本相同,为什么这不起作用?

Thanks. 谢谢。

That question mark is not required by highstock neither are the () so your JSON is invalid. highstock不需要问号, ()也不需要,因此您的JSON无效。 That markup is for JSONP because the highchart is requesting data from a different domain. 该标记适用于JSONP,因为highchart正在从其他域请求数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM