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: '2014 年全球最大人口城市排名'
   };
   // 副標題
   var subtitle = {
      text: '世界名城'
   };
   // X 軸
   var xAxis = {
      type: 'category',
      labels: {
         // 標籤選擇
         rotation: -45,
         style: {
            fontSize: '13px',
            fontFamily: 'Verdana, sans-serif'
         }
      }
   };
   // Y 軸
   var yAxis ={
      min: 0,
      title: {
        text: '人口 (百萬)'
      }
   };
   // 提示
   var tooltip = {
      pointFormat: '2008 年人口: <b>{point.y:.1f} 百萬</b>'
   };
   var credits = {
      enabled: false
   };
   // 資料
   var series= [{
            name: '人口',
            data: [
                ['Shanghai', 23.7],
                ['Lagos', 16.1],
                ['Instanbul', 14.2],
                ['Karachi', 14.0],
                ['Mumbai', 12.5],
                ['Moscow', 12.1],
                ['Sao Paulo', 11.8],
                ['Beijing', 11.7],
                ['Guangzhou', 11.1],
                ['Delhi', 11.1],
                ['Shenzhen', 10.5],
                ['Seoul', 10.4],
                ['Jakarta', 10.0],
                ['Kinshasa', 9.3],
                ['Tianjin', 9.3],
                ['Tokyo', 9.0],
                ['Cairo', 8.9],
                ['Dhaka', 8.9],
                ['Mexico City', 8.9],
                ['Lima', 8.9]
            ],
            dataLabels: {
                enabled: true,
                // 旋轉程度由 rotation 屬性決定。我們還可以通過其他屬性來定義文字標籤的背景,間隔,邊框等。
                rotation: -90,
                color: '#FFFFFF',
                align: 'right',
                format: '{point.y:.1f}', // one decimal
                y: 10, // 10 pixels down from the top
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
   }];

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

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

二 執行結果