【问题标题】:Highstock chart - JSON input not workingHighstock 图表 - JSON 输入不起作用
【发布时间】:2016-11-19 18:59:34
【问题描述】:

我正在尝试使用 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 个对象并删除了 cmets。第一个? 是Highstock 需要的,它作为回调参数添加到原始URL 中。每个对象中的第一个数字是日期的整数值。

最终我将从数据库中查询数据并以这种格式输出。

我的问题是,如果响应基本相同,为什么这不起作用?

谢谢。

【问题讨论】:

  • highstock 不需要问号,() 也不需要,所以你的 json 无效
  • 我的天啊...修复了它。那么为什么当原始 PHP 与 .和 ()?您能否将其发布为答案,以便我接受并关闭。谢谢。
  • google jsonp 你会发现原因
  • 我明白了,以前从未听说过 JSONP,但这解释了为什么我的跨浏览器 JSON 请求不起作用。谢谢(你的)信息。就像我说的,如果你把它作为答案发布,我会接受它。

标签: javascript json highstock


【解决方案1】:

highstock 不需要那个问号,() 也不需要,因此您的 JSON 无效。该标记用于 JSONP,因为 highchart 正在请求来自不同域的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2018-08-21
    • 2020-07-10
    相关资源
    最近更新 更多