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条回答 默认 最新

  • douye2488 2019-03-06 09:50
    关注

    here is the answer to plot pie chart using highcharts api. when data is in horizontal format.

    $result = mysqli_query($con,"SELECT   area AS A , blank  AS B from `table`  WHERE city = '".$id."' ");
    
    $rows['type'] = 'pie';
    $rows['name'] = 'values';
    $p = 'area';//add this variable for the datalables
    $a = 'blank';//add this variable for the datalables
    //$rows['innerSize'] = '50%';
    while ($r = mysqli_fetch_array($result)) {
        $rows['data'][] = array($p, $r['A']); 
        $rows['data'][] = array($a, $r['B']);    
    }
    $rslt = array();
    array_push($rslt,$rows);
    
    
    print json_encode($rslt, JSON_NUMERIC_CHECK);

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)