【问题标题】:Export JSON Data to Highchart (Pie)将 JSON 数据导出到 Highchart(饼图)
【发布时间】:2013-06-15 22:11:56
【问题描述】:

我正在尝试从 MYSQL 查询中呈现高图。 JSON 看起来正确,页面加载没有任何错误,但它不会呈现高图。

我的代码:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
  <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
  <link rel="stylesheet" type="text/css" href="http://fiddle.jshell.net/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="http://fiddle.jshell.net/css/result-light.css">
  <style type='text/css'>        
  </style>
<script type='text/javascript'>

$(function () {
    var chart;
    var myJson =  
[{"Buch":"0528713FHVR ","Anzahl":3},{"Buch":"0657222FHVR","Anzahl":2},{"Buch":"A10055035","Anzahl":2},{"Buch":"0657223FHVR","Anzahl":1},{"Buch":"062729XFHVR","Anzahl":1}] 
    $(document).ready(function() {
        var options = {
        chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'My PIE chart'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                        }
                    }
                }
            },
            series: []
    };
        var seriesNew = {
                type: 'pie',
                name: 'Some series name',
                data: []
            };
        myJson = $.parseJSON(myJson);
        jQuery.each(myJson, function (itemNo, item) {
            seriesNew.data.push({
                x: item.Buch,
                y: item.Anzahl
            })
        });
        options.series.push(seriesNew);       
        chart = new Highcharts.Chart(options);
    });
});
</script>
</head>
<body>
  <script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>

我没有包含 SQL 查询,因为我觉得 JSON 格式很好(我使用了 JSON_Encode)。

但我不知道为什么它不会渲染。

【问题讨论】:

    标签: php javascript mysql json highcharts


    【解决方案1】:

    您已经有一个 JSON,您不需要使用 jQuery 解析它,因此删除该行:myJson = $.parseJSON(myJson); 并且应该可以工作。见:http://jsfiddle.net/H4LS9/

    编辑:

    要获取第一个值作为切片名称,请使用点作为数组:http://jsfiddle.net/H4LS9/1/

            seriesNew.data.push([
               item.Buch,
               item.Anzahl
            ])
    

    【讨论】:

    • 是的,非常感谢。不知道为什么我没有看到。现在我必须尝试获取像 0657222FHVR 这样的标题而不是 slice。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多