【问题标题】:Highcharts .getJSON - chart not plottingHighcharts .getJSON - 图表未绘制
【发布时间】:2014-08-24 06:34:51
【问题描述】:

之前的帮助使我的 Highcharts 项目进入了当前阶段,但是我仍然遇到了一个主要的绊脚石。 Highcharts 图表未加载使用 .getJSON 检索的数据。我不知道为什么 html 不呈现图表并从 data.php 加载数据。下面的所有代码,有人有什么想法吗? @PawełFus

php

<?php

 $con = mysql_connect("ip_address","root","");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

$result = array();

$sql = mysql_query("SELECT unix_timestamp(DATETIMES), TEST FROM PR");

$result['name'] = 'TEST';
while($r = mysql_fetch_array($sql)) {
     $datetime = $r[0]*1000;
     $result['category'][] = $datetime;
     $result['data'][] = round($r[1],2) ;
}

$json = array();
array_push($json,$result);
print json_encode($json);


?>

php返回

[{"name":"CCGT","category":[1387791900000,1387792200000,1387792500000,1387792800000],"data":[8389,8478,8761,8980,9050]}]

html

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            var options = {
                chart: {
                    renderTo: 'container',
                    type: 'line',
                    marginRight: 130,
                    marginBottom: 25
                },
                title: {
                    text: 'test',
                    x: -20 //center
                },
                subtitle: {
                    text: '',
                    x: -20
                },
                xAxis: {
                    categories: []
                },
                yAxis: {
                    title: {
                        text: 'test'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    formatter: function() {
                            return '<b>'+ this.series.name +'</b><br/>'+
                            this.x +': '+ this.y;
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'top',
                    x: -10,
                    y: 100,
                    borderWidth: 0
                },
                series: []
            }

            $.getJSON("data.php", function(json) {
                options.xAxis.categories = json['category'];
                options.series[0].name = json['name'];
                options.series[0].data = json['data'];
                chart = new Highcharts.Chart(options);
            });
        });
        </script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
    </head>
    <body>
        <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
    </body>
</html>

【问题讨论】:

    标签: highcharts getjson


    【解决方案1】:

    您的系列数组(在选项中)没有对象,因此您不能使用系列[0].name。其次,您的 json 使用不正确,但尝试替换以下行:

    options.xAxis.categories = json['category'];
    options.series[0].name = json['name'];
    options.series[0].data = json['data'];
    

    options.xAxis.categories = json[0]['category'];
    options.series[0] = {};
    options.series[0].name = json[0]['name'];
    options.series[0].data = json[0]['data'];
    

    【讨论】:

    • 感谢您的回复。替换选项行会导致部分成功。图表现在正在呈现(具有正确的系列名称),但数据仍然不显示。现场示例link,data.php 输出存储在这里link。 Chrome 控制台显示 highcharts.js 错误 19. 不熟悉 Highcharts 调试,有什么见解吗?
    • 你用的是这个json还是其他的?
    • 原帖中的 json 用于测试目的,是链接 json 的子集。
    • Ahhh...highcharts.com/errors/19 可能会解释它,将尝试减少查询中的数据点数
    • 但是有没有办法绕过日期时间/类别限制或加载大量 json 日期时间数据的替代方法?
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多