dt4320279 2013-06-19 11:36
浏览 70
已采纳

将JSON数据导出到Highchart(饼图)

I'm trying to render a highchart from an MYSQL Query. The JSON looks correct, the page loads without any error, but it won't render the highchart.

My Code:

<!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>

I didn't include the SQL Query, since I feel like the JSON ist well formatted (i used JSON_Encode).

But i have no idea why it won't render.

  • 写回答

1条回答 默认 最新

  • dongpu1331 2013-06-19 13:02
    关注

    You already have a JSON, you don't need to parse it using jQuery, so remove that line: myJson = $.parseJSON(myJson); and should work. See: http://jsfiddle.net/H4LS9/

    Edit:

    To get first value as slice name use point as an array: http://jsfiddle.net/H4LS9/1/

            seriesNew.data.push([
               item.Buch,
               item.Anzahl
            ])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记