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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器