【问题标题】:Highcarts doesn't load my JSON dataHighcharts 不加载我的 JSON 数据
【发布时间】:2017-01-17 09:04:01
【问题描述】:

我正在尝试使用 Highcharts 创建图表。我的 JSON 输出是常规的 JSON 格式,由 PHP 的 json_encode() 生成。 这是我的 JSON 输出:

[["2015-07-13 16:41:05","3"],["2015-08-15 16:41:05","1"],["2015-09-18 16:41:05","4"],["2015-10-28 16:41:05","3"],["2015-11-20 16:41:05","1"],["2015-11-28 16:41:05","5"],["2015-12-13 16:41:05","1"],["2015-12-21 16:41:05","2"],["2015-12-24 16:41:05","1"],["2015-12-25 16:41:05","2"],["2015-12-28 16:41:05","1"],["2016-01-03 16:41:05","2"],["2016-01-09 16:41:05","4"],["2016-01-14 16:41:05","6"],["2016-01-17 16:41:05","3"],["2016-01-18 16:41:05","3"],["2016-01-20 16:41:05","2"],["2016-01-21 16:41:05","2"],["2016-01-22 16:41:05","2"],["2016-01-23 16:41:05","1"],["2016-01-24 16:41:05","5"],["2016-01-25 13:41:05","1"],["2016-01-25 16:41:00","1"],["2016-01-25 16:41:05","1"],["2016-01-27 16:41:05","1"],["2016-01-28 16:41:05","1"],["2016-01-29 16:41:05","1"],["2016-02-09 16:41:05","1"],["2016-02-10 16:41:05","2"],["2016-02-11 16:41:05","3"],["2016-02-15 16:41:05","2"],["2016-02-18 16:41:05","1"],["2016-02-21 16:41:05","1"],["2016-02-23 16:41:05","1"],["2016-02-24 16:41:05","1"],["2016-02-25 16:41:05","1"],["2016-02-26 16:41:05","1"],["2016-02-28 16:41:05","1"],["2016-03-01 16:41:05","1"],["2016-03-02 16:41:05","1"],["2016-03-05 16:41:05","1"],["2016-03-12 16:41:05","1"],["2016-03-21 16:41:05","1"],["2016-04-25 16:41:05","1"],["2016-05-05 16:41:05","2"],["2016-05-11 16:41:05","1"],["2016-05-12 16:41:05","1"],["2016-05-24 16:41:05","1"],["2016-05-25 16:41:05","1"],["2016-06-01 16:41:05","1"],["2016-06-12 16:41:05","1"],["2016-06-25 10:41:05","1"],["2016-06-25 16:41:05","1"],["2016-07-01 10:41:05","1"],["2016-07-01 16:41:05","1"],["2016-07-02 16:41:05","1"],["2016-07-25 16:21:05","1"],["2016-07-25 16:41:05","1"],["2016-08-13 16:41:05","1"],["2016-08-28 16:41:05","1"],["2016-09-20 16:41:05","3"],["2016-09-29 16:41:05","2"],["2016-10-01 14:20:18","1"],["2016-10-09 14:20:18","1"],["2016-10-10 14:20:18","1"],["2016-10-28 14:20:18","2"],["2016-10-30 14:20:18","2"],["2016-11-01 13:44:29","1"],["2016-11-06 08:26:18","2"],["2016-11-10 13:44:29","1"],["2016-11-13 13:44:29","2"],["2016-11-14 19:13:42","4"],["2016-11-15 23:27:39","3"],["2016-12-06 19:04:06","3"],["2016-12-08 10:49:28","3"],["2016-12-10 23:01:44","1"],["2016-12-14 21:27:44","1"],["2016-12-15 23:27:44","2"],["2016-12-15 23:27:53","3"],["2016-12-18 18:06:28","1"],["2016-12-21 20:06:28","3"],["2016-12-23 20:06:20","5"],["2016-12-29 20:18:18","3"],["2017-01-03 20:06:32","3"]]

但 Highcarts 不会加载图表中的数据。 URL、输出和数据都设置好了。请求返回 200 并且可以定期加载数据,因为我已经通过 AJAX 对其进行了检查。 这是我的 Highchart 函数。请注意,我的申请中有几份报告。这就是我指定 cartFilling() 函数的原因:

function cartFilling() {
    var url = 'components/com_product/views/reports/callback.php?callback=cart-filling&date='+date_scope;
    jQuery.getJSON(url, function (data) {
        Highcharts.chart('cartFilling', {
            chart: {
                zoomType: 'x'
            },
            title: {
                text: 'Shopping Cart Filling Actions'
            },
            subtitle: {
                text: document.ontouchstart === undefined ?
                        'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
            },
            xAxis: {
                type: 'datetime'
            },
            yAxis: {
                title: {
                    text: 'Carts Filled'
                }
            },
            legend: {
                enabled: false
            },
            plotOptions: {
                area: {
                    fillColor: {
                        linearGradient: {
                            x1: 0,
                            y1: 0,
                            x2: 0,
                            y2: 1
                        },
                        stops: [
                            [0, Highcharts.getOptions().colors[0]],
                            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                        ]
                    },
                    marker: {
                        radius: 2
                    },
                    lineWidth: 1,
                    states: {
                        hover: {
                            lineWidth: 1
                        }
                    },
                    threshold: null
                }
            },

            series: [{
                type: 'area',
                name: 'Cart Filling',
                data: data
            }]
        });
    });
}

非常感谢任何帮助。

【问题讨论】:

  • 您必须将日期字符串解析为 javascript 时间戳。而不是这个“2015-07-13 16:41:05”,你需要这样的东西 1436798465000。使用 new Date("2015-07-13 16:41:05").getTime() 来解析你的 json。

标签: javascript jquery json ajax highcharts


【解决方案1】:

您的解决方案完美运行,您只需要重写数据数组。

而不是这个:

["2015-07-13 16:41:05", "3"]

使用这个:

[Date.UTC(2015,6,13,16,41,5), 3]

请注意在 Date.UTC 函数中 0=January,1=February 等

http://jsfiddle.net/dpegbt7t/

【讨论】:

  • 丹尼尔还是不行。有效的 JSON 格式为 ["Date.UTC(2015,6,13,16,41,5)", "3"]。我试过但没用。我还删除了引用,所以它说:[Date.UTC(2015,6,13,16,41,5), 3]。这个也不行。你怎么看?
  • 我在 jQuery 1.8.2 中使用过 HighCharts 5.0.7,它对我有用。 highcharts.com/demo/spline-irregular-timeHighChart 需要数组中第二个元素的数字类型。
  • 向 yAxis 添加了 type: 'number' 但没用。我在检索不是 JSON 的数据类型的数据时遇到问题。另外,日期有问题。所以,我根本看不到加载的图表。但现在我做了几个更正。虽然它不起作用,但至少,我现在可以看到一个空图表。
  • 请看我在答案中添加的小提琴。正确检索数据格式后,使用 praseInt() 或 parseFloat() 解析数字以及日期。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
相关资源
最近更新 更多