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

二當家的發表於2019-02-16
演示下載地址:http://www.erdangjiade.com/js/50.html
效果圖:
  1. Mysql
  2. 首先我們建一張·chart_pie·表作為統計資料。
  3.  
  4.  表的結構 `chart_pie` 
  5.  
  6.  
  7. CREATE TABLE IF NOT EXISTS `chart_pie` ( 
  8.   `id` int(11) NOT NULL AUTO_INCREMENT, 
  9.   `title` varchar(30) NOT NULL, 
  10.   `pv` int(10) NOT NULL, 
  11.   PRIMARY KEY (`id`) 
  12. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ; 
  13.  
  14.  
  15.  轉存表中的資料 `chart_pie` 
  16.  
  17.  
  18. INSERT INTO `chart_pie` (`id`, `title`, `pv`) VALUES 
  19. (1, `百度`, 1239), 
  20. (2, `google`, 998), 
  21. (3, `搜搜`, 342), 
  22. (4, `必應`, 421), 
  23. (5, `搜狗`, 259), 
  24. (6, `其他`, 83);
  25. PHP
  26. pie.php我們要生成資料給前端呼叫:
  27. $query = mysql_query(“select * from chart_pie”);  
  28. while($row = mysql_fetch_array($query)){  
  29.     $arr[] = array(  
  30.         $row[`title`],intval($row[`pv`])  
  31.     );  
  32. }  
  33. $data = json_encode($arr);
  34. jQuery
  35. $(function() { 
  36.     $(`#highcharts`).highcharts({ 
  37.         chart: { 
  38.             renderTo: `chart_pie`, 
  39.             //餅狀圖關聯html元素id值 
  40.             defaultSeriesType: `pie`, 
  41.             //預設圖表型別為餅狀圖 
  42.             plotBackgroundColor: `#ffc`, 
  43.             //設定圖表區背景色 
  44.             plotShadow: true //設定陰影 
  45.         }, 
  46.         title: { 
  47.             text: `搜尋引擎統計分析` //圖表標題 
  48.         }, 
  49.         credits: { 
  50.             text: `erdangjiade.com` 
  51.         }, 
  52.         tooltip: { 
  53.             formatter: function() { //滑鼠滑向影像提示框的格式化提示資訊 
  54.                 return `<b>` + this.point.name + `</b>: ` + twoDecimal(this.percentage) + ` %`; 
  55.             } 
  56.         }, 
  57.         plotOptions: { 
  58.             pie: { 
  59.                 allowPointSelect: true, 
  60.                 //允許選中,點選選中的扇形區可以分離出來顯示 
  61.                 cursor: `pointer`, 
  62.                 //當滑鼠指向扇形區時變為手型(可點選) 
  63.                 //showInLegend: true,  //如果要顯示圖例,可將該項設定為true 
  64.                 dataLabels: { 
  65.                     enabled: true, 
  66.                     //設定資料標籤可見,即顯示每個扇形區對應的資料 
  67.                     color: `#000000`, 
  68.                     //資料顯示顏色 
  69.                     connectorColor: `#999`, 
  70.                     //設定資料域扇形區的連線線的顏色 
  71.                     style: { 
  72.                         fontSize: `12px` //資料顯示的大小 
  73.                     }, 
  74.                     formatter: function() { //格式化資料 
  75.                         return `<b>` + this.point.name + `</b>: ` + twoDecimal(this.percentage) + ` %`; 
  76.                         //return `<b>` + this.point.name + `</b>: ` + this.y ; 
  77.                     } 
  78.                 } 
  79.             } 
  80.         }, 
  81.         series: [{ //資料列 
  82.             name: `search engine`, 
  83.             data: data //核心資料列來源於php讀取的資料並解析成JSON 
  84.         }] 
  85.     }); 
  86. });
  87. 此外,格式化資料市,如果要顯示百分比,可使用this.percentageHighcharts會自動將整數轉換為百分數,如果要顯示資料量,直接使用this.y
  88. 百分比程式碼如下:
  89. formatter: function() { //格式化資料  
  90.     return `<b>` + this.point.name + `</b>: ` + twoDecimal(this.percentage) + ` %`;  
  91. }
  92. 實際資料是這樣的:
  93. formatter: function() { //格式化資料  
  94.     return `<b>` + this.point.name + `</b>: ` + this.;  
  95. }
  96. 最後我們要保留兩位小數,程式碼貼下:
  97. function twoDecimal(x) { //保留2位小數 
  98.     var f_x = parseFloat(x); 
  99.     if (isNaN(f_x)) { 
  100.         alert(`錯誤的引數`); 
  101.         return false; 
  102.     } 
  103.     var f_x = Math.round(* 100) / 100; 
  104.     var s_x = f_x.toString(); 
  105.     var pos_decimal = s_x.indexOf(`.`); 
  106.     if (pos_decimal < 0) { 
  107.         pos_decimal = s_x.length; 
  108.         s_x += `.`; 
  109.     } 
  110.     while (s_x.length <= pos_decimal + 2) { 
  111.         s_x += `0`; 
  112.     } 
  113.     return s_x; 
  114. }


相關文章