亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP+mysql+Highcharts生成餅狀圖

 更新時(shí)間:2015年05月04日 15:36:45   投稿:hebedich  
本文將結(jié)合實(shí)際,使用PHP讀取Mysql數(shù)據(jù)表中的數(shù)據(jù),并將獲取的數(shù)據(jù)按照要求輸出給前端JS,再通過配置調(diào)用Highcharts圖表庫(kù)生成餅狀圖。

Mysql

首先我們建一張·chart_pie·表作為統(tǒng)計(jì)數(shù)據(jù)。

-- 
-- 表的結(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);

PHP

在pie.php我們要生成數(shù)據(jù)給前端調(diào)用:

$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)簽可見,即顯示每個(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 
  }] 
 }); 
});

此外,格式化數(shù)據(jù)市,如果要顯示百分比,可使用this.percentage,Highcharts會(huì)自動(dòng)將整數(shù)轉(zhuǎn)換為百分?jǐn)?shù),如果要顯示數(shù)據(jù)量,直接使用this.y。
百分比代碼如下:

formatter: function() { //格式化數(shù)據(jù) 
 return '<b>' + this.point.name + '</b>: ' + twoDecimal(this.percentage) + ' %'; 
}

實(shí)際數(shù)據(jù)是這樣的:

formatter: function() { //格式化數(shù)據(jù) 
 return '<b>' + this.point.name + '</b>: ' + this.y ; 
}

最后我們要保留兩位小數(shù),代碼貼下:

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; 
}

柱狀圖、餅狀圖、曲線圖等都是一樣的。

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論