duanpai6581
2019-03-06 04:49
浏览 99
已采纳

使用highcharts api和mysql显示饼图

I have a query. I want to display a pie chart using the highcharts api. The data is coming from a MySQL database. My table is like (THIS IS MY TABLE FORMAT):

city|area|blank
A   |100 |50
B   |50  |20

My PHP code is

<?php

include "con.php";

$id = $_GET['city'];


$result = mysqli_query($con,"SELECT   area AS A , blank  AS B from `table`  WHERE city = '".$id."' ");

$rows['type'] = 'pie';
$rows['name'] = 'area';
//$rows['innerSize'] = '50%';
while ($r = mysqli_fetch_array($result)) {
    $rows['data'][] = array($r['A'], $r['B']);    
}
$rslt = array();
array_push($rslt,$rows);

print json_encode($rslt, JSON_NUMERIC_CHECK);
mysqli_close($con);

I have been displaying a pie chart but my data was like this (THIS IS EXAMPLE):

id|category|value
1 |area    |100
1 |blank   |20
2 |area    |50
2 |blank   |20

but as I mentioned regarding my table structure earlier, the pie chart is not displaying with it.

My js code:

var c = $('#City :selected').text(); 

                 getAjaxData(c);
                 
    var opt = {
                    chart: {
                        renderTo: 'container1',
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                    },
                    title: {
                        text: 'final chart'
                    },
                    
                    plotOptions: {
                        pie: {
                            allowPointSelect: true,
                            cursor: 'pointer',
                            dataLabels: {
                                enabled: true,
                                color: '#000000',
                                connectorColor: '#000000',
                                formatter: function() {
                                    return '<b>' + this.point.name + '</b>: ' + this.y;
                                }
                            },
                            showInLegend: true
                        }
                    },
                    series: []
                };
                function getAjaxData(c) {
                $.getJSON("file.php", {city:c},function(json) {
                    opt.series = json;
                    chart = new Highcharts.Chart(opt);

                }); 
                
                
                
                }

</div>

2条回答 默认 最新

相关推荐 更多相似问题