天天看點

Highcharts+PHP+Mysql生成餅狀統計圖

效果圖:

Highcharts+PHP+Mysql生成餅狀統計圖

Mysql

首先我們建一張·chart_pie·表作為統計資料。

-- 

-- 表的結構 `chart_pie` 

CREATE TABLE IF NOT EXISTS `chart_pie` ( 

  `id` int(11) NOT NULL AUTO_INCREMENT, 

  `title` varchar(30) NOT NULL, 

  `pv` int(10) NOT NULL, 

  PRIMARY KEY (`id`) 

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ; 

-- 轉存表中的資料 `chart_pie` 

INSERT INTO `chart_pie` (`id`, `title`, `pv`) VALUES 

(1, '百度', 1239), 

(2, 'google', 998), 

(3, '搜搜', 342), 

(4, '必應', 421), 

(5, '搜狗', 259), 

(6, '其他', 83);

PHP

在pie.php我們要生成資料給前端調用:

$query = mysql_query("select * from chart_pie");  

while($row = mysql_fetch_array($query)){  

    $arr[] = array(  

        $row['title'],intval($row['pv'])  

    );  

}  

$data = json_encode($arr);

jQuery

$(function() { 

    $('#highcharts').highcharts({ 

        chart: { 

            renderTo: 'chart_pie', 

            //餅狀圖關聯html元素id值 

            defaultSeriesType: 'pie', 

            //預設圖表類型為餅狀圖 

            plotBackgroundColor: '#ffc', 

            //設定圖表區背景色 

            plotShadow: true //設定陰影 

        }, 

        title: { 

            text: '搜尋引擎統計分析' //圖表标題 

        credits: { 

            text: 'erdangjiade.com' 

        tooltip: { 

            formatter: function() { //滑鼠滑向圖像提示框的格式化提示資訊 

                return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %'; 

            } 

        plotOptions: { 

            pie: { 

                allowPointSelect: true, 

                //允許選中,點選選中的扇形區可以分離出來顯示 

                cursor: 'pointer', 

                //當滑鼠指向扇形區時變為手型(可點選) 

                //showInLegend: true,  //如果要顯示圖例,可将該項設定為true 

                dataLabels: { 

                    enabled: true, 

                    //設定資料标簽可見,即顯示每個扇形區對應的資料 

                    color: '#000000', 

                    //資料顯示顔色 

                    connectorColor: '#999', 

                    //設定資料域扇形區的連接配接線的顔色 

                    style: { 

                        fontSize: '12px' //資料顯示的大小 

                    }, 

                    formatter: function() { //格式化資料 

                        return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %'; 

                        //return '<b>' + this.point.name + '</b>: ' + this.y ; 

                    } 

                } 

        series: [{ //資料列 

            name: 'search engine', 

            data: data //核心資料列來源于php讀取的資料并解析成JSON 

        }] 

    }); 

});

此外,格式化資料市,如果要顯示百分比,可使用this.percentage,Highcharts會自動将整數轉換為百分數,如果要顯示資料量,直接使用this.y。

百分比代碼如下:

formatter: function() { //格式化資料  

    return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %';  

}

實際資料是這樣的:

    return '<b>' + this.point.name + '</b>: ' + this.y ;  

最後我們要保留兩位小數,代碼貼下:

function twoDecimal(x) { //保留2位小數 

    var f_x = parseFloat(x); 

    if (isNaN(f_x)) { 

        alert('錯誤的參數'); 

        return false; 

    } 

    var f_x = Math.round(x * 100) / 100; 

    var s_x = f_x.toString(); 

    var pos_decimal = s_x.indexOf('.'); 

    if (pos_decimal < 0) { 

        pos_decimal = s_x.length; 

        s_x += '.'; 

    while (s_x.length <= pos_decimal + 2) { 

        s_x += '0'; 

    return s_x;