- 論壇徽章:
- 0
|
PHP+mysql+Highcharts實(shí)現(xiàn)餅狀統(tǒng)計(jì)圖。
此處貼代碼,詳細(xì)參考:http://www.lai18.com/content/409203.html
首先我們建一張・chart_pie・表作為統(tǒng)計(jì)數(shù)據(jù)。- -- Mysql
- -- 首先我們建一張・chart_pie・表作為統(tǒng)計(jì)數(shù)據(jù)。
- -- edit http://www.lai18.com
- -- 表的結(jié)構(gòu) `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 ;
-
- --
- -- 轉(zhuǎn)存表中的數(shù)據(jù) `chart_pie`
- --
-
- INSERT INTO `chart_pie` (`id`, `title`, `pv`) VALUES
- (1, '百度', 1239),
- (2, 'google', 998),
- (3, '搜搜', 342),
- (4, '必應(yīng)', 421),
- (5, '搜狗', 259),
- (6, '其他', 83);
復(fù)制代碼 在pie.php我們要生成數(shù)據(jù)給前端調(diào)用- /*
- * 在pie.php我們要生成數(shù)據(jù)給前端調(diào)用
- * edit http://www.lai18.com
- * 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',
- //餅狀圖關(guān)聯(lián)html元素id值
- defaultSeriesType: 'pie',
- //默認(rèn)圖表類型為餅狀圖
- plotBackgroundColor: '#ffc',
- //設(shè)置圖表區(qū)背景色
- plotShadow: true //設(shè)置陰影
- },
- title: {
- text: '搜索引擎統(tǒng)計(jì)分析' //圖表標(biāo)題
- },
- credits: {
- text: 'jb51.net'
- },
- tooltip: {
- formatter: function() { //鼠標(biāo)滑向圖像提示框的格式化提示信息
- return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %';
- }
- },
- plotOptions: {
- pie: {
- allowPointSelect: true,
- //允許選中,點(diǎn)擊選中的扇形區(qū)可以分離出來(lái)顯示
- cursor: 'pointer',
- //當(dāng)鼠標(biāo)指向扇形區(qū)時(shí)變?yōu)槭中停ǹ牲c(diǎn)擊)
- //showInLegend: true, //如果要顯示圖例,可將該項(xiàng)設(shè)置為true
- dataLabels: {
- enabled: true,
- //設(shè)置數(shù)據(jù)標(biāo)簽可見(jiàn),即顯示每個(gè)扇形區(qū)對(duì)應(yīng)的數(shù)據(jù)
- color: '#000000',
- //數(shù)據(jù)顯示顏色
- connectorColor: '#999',
- //設(shè)置數(shù)據(jù)域扇形區(qū)的連接線的顏色
- style: {
- fontSize: '12px' //數(shù)據(jù)顯示的大小
- },
- formatter: function() { //格式化數(shù)據(jù)
- return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %';
- //return '<b>' + this.point.name + '</b>: ' + this.y ;
- }
- }
- }
- },
- series: [{ //數(shù)據(jù)列
- name: 'search engine',
- data: data //核心數(shù)據(jù)列來(lái)源于php讀取的數(shù)據(jù)并解析成JSON
- }]
- });
- });
復(fù)制代碼 此外,格式化數(shù)據(jù)市,如果要顯示百分比,可使用this.percentage- //此外,格式化數(shù)據(jù)市,如果要顯示百分比,可使用this.percentage
- formatter: function() { //格式化數(shù)據(jù)
- return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %';
- }
復(fù)制代碼 最后我們要保留兩位小數(shù),代碼貼下- //最后我們要保留兩位小數(shù),代碼貼下
- // edit http://www.lai18.com
- function twoDecimal(x) { //保留2位小數(shù)
- var f_x = parseFloat(x);
- if (isNaN(f_x)) {
- alert('錯(cuò)誤的參數(shù)');
- 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;
- }
復(fù)制代碼 |
|