1. 程式人生 > >Highcharts 使用百分比的堆疊柱形圖

Highcharts 使用百分比的堆疊柱形圖

一 程式碼

<html>
<head>
<meta charset="UTF-8" />
<title>Highcharts 使用百分比的堆疊柱形圖</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
$(document).ready(function() {
   // 圖是柱形圖
   var chart = {
      type: 'column'
   };
   // 主標題
   var title = {
      text: '堆疊柱形圖'
   };
   // X 軸
   var xAxis = {
      categories: ['蘋果', '橘子', '梨子', '葡萄', '香蕉']
   };
   // Y軸
   var yAxis ={
      min: 0,
      title: {
        text: '水果總消費量'
      }
   };
   // 提示語
   var tooltip = {
      pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.percentage:.0f}%)<br/>',
      shared: true
   };
   // 按百分比顯示
   var plotOptions = {
      column: {
         stacking: 'percent'
      }
   };
   var credits = {
      enabled: false
   };
   // 資料
   var series= [{
        name: '小馬',
            data: [5, 3, 4, 7, 2]
        }, {
            name: '小林',
            data: [2, 2, 3, 2, 1]
        }, {
            name: '小紅',
            data: [3, 4, 4, 2, 5]
   }];

   var json = {};
   json.chart = chart;
   json.title = title;
   json.xAxis = xAxis;
   json.yAxis = yAxis;
   json.tooltip = tooltip;
   json.plotOptions = plotOptions;
   json.credits = credits;
   json.series = series;
   $('#container').highcharts(json);

});
</script>
</body>
</html>

二 執行結果