Highcharts 树状图(Treemap)


本章节我们将为大家介绍 Highcharts 的热点图。

我们在前面的章节已经了解了 Highcharts 配置语法。接下来让我们来看下 Highcharts 的其他配置。

树状图

series 配置

设置 series 的 type 属性为 treemap ,series.type 描述了数据列类型。默认值为 "line"。

  1. var chart = {
  2.    type: 'treemap'
  3. };

实例

文件名:highcharts_tree_map.htm

  1. <html>
  2. <head>
  3. <title>Highcharts 教程 | 手册网(shouce.ren)</title>
  4.    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  5.    <script src="/try/demo_source/highcharts.js"></script>
  6.    <script src="http://code.highcharts.com/modules/treemap.js"></script>    
  7.    <script src="http://code.highcharts.com/modules/heatmap.js"></script>  
  8. </head>
  9. <body>
  10. <div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
  11. <script language="JavaScript">
  12. $(document).ready(function() {    
  13.    var title = {
  14.       text: 'Highcharts Treemap'   
  15.    };       
  16.    
  17.    var colorAxis = {
  18.       minColor: '#FFFFFF',
  19.       maxColor: Highcharts.getOptions().colors[0]
  20.    };
  21.    
  22.    var series= [{
  23.      type: "treemap",
  24.      layoutAlgorithm: 'squarified',
  25.      data: [{
  26.         name: 'A',
  27.         value: 6,
  28.         colorValue: 1
  29.      }, {
  30.         name: 'B',
  31.         value: 6,
  32.         colorValue: 2
  33.      }, {
  34.         name: 'C',
  35.         value: 4,
  36.         colorValue: 3
  37.      }, {
  38.         name: 'D',
  39.         value: 3,
  40.         colorValue: 4
  41.      }, {
  42.         name: 'E',
  43.         value: 2,
  44.         colorValue: 5
  45.      }, {
  46.         name: 'F',
  47.         value: 2,
  48.         colorValue: 6
  49.      }, {
  50.         name: 'G',
  51.         value: 1,
  52.         colorValue: 7
  53.      }]
  54.    }];     
  55.       
  56.    var json = {};     
  57.    json.title = title;          
  58.    json.colorAxis = colorAxis;   
  59.    json.series = series;       
  60.    
  61.    $('#container').highcharts(json);
  62. });
  63. </script>
  64. </body>
  65. </html>

不同等级树状图

以下实例使用不同颜色来标识不同等级的树状图。

实例

文件名:highcharts_tree_levels.htm(完整源码请点击实例查看)

  1. <html>
  2. <head>
  3. <title>Highcharts 教程 | 手册网(shouce.ren)</title>
  4.    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  5.    <script src="/try/demo_source/highcharts.js"></script>
  6.    <script src="http://code.highcharts.com/modules/treemap.js"></script>    
  7.    <script src="http://code.highcharts.com/modules/heatmap.js"></script>  
  8. </head>
  9. <body>
  10. <div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
  11. <script language="JavaScript">
  12. $(document).ready(function() {    
  13.    var title = {
  14.       text: 'Fruit consumption'   
  15.    };        
  16.    
  17.    var series = [{
  18.       type: "treemap",
  19.       layoutAlgorithm: 'stripes',
  20.       alternateStartingDirection: true,
  21.       levels: [{
  22.          level: 1,
  23.          layoutAlgorithm: 'sliceAndDice',
  24.          dataLabels: {
  25.             enabled: true,
  26.             align: 'left',
  27.             verticalAlign: 'top',
  28.             style: {
  29.                fontSize: '15px',
  30.                fontWeight: 'bold'
  31.             }
  32.          }
  33.       }],
  34.       data: [{
  35.          id: 'A',
  36.          name: 'Apples',
  37.          color: "#EC2500"
  38.       }, {
  39.          id:'B',
  40.          name: 'Bananas',
  41.          color: "#ECE100"
  42.       }, {
  43.          id: 'O',
  44.          name: 'Oranges',
  45.          color: '#EC9800'
  46.       }, {
  47.          name: 'Anne',
  48.          parent: 'A',
  49.          value: 5
  50.       }, {
  51.          name: 'Rick',
  52.          parent: 'A',
  53.          value: 3
  54.       }, {
  55.          name: 'Peter',
  56.          parent: 'A',
  57.          value: 4
  58.       }, {
  59.          name: 'Anne',
  60.          parent: 'B',
  61.          value: 4
  62.       }, {
  63.          name: 'Rick',
  64.          parent: 'B',
  65.          value: 10
  66.       }, {
  67.          name: 'Peter',
  68.          parent: 'B',
  69.          value: 1
  70.       }, {
  71.          name: 'Anne',
  72.          parent: 'O',
  73.          value: 1
  74.       }, {
  75.          name: 'Rick',
  76.          parent: 'O',
  77.          value: 3
  78.       }, {
  79.          name: 'Peter',
  80.          parent: 'O',
  81.          value: 3
  82.       }, {
  83.          name: 'Susanne',
  84.          parent: 'Kiwi',
  85.          value: 2,
  86.          color: '#9EDE00'
  87.       }]
  88.    }]; 
  89.       
  90.    var json = {};     
  91.    json.title = title;            
  92.    json.series = series;       
  93.    
  94.    $('#container').highcharts(json);
  95. });
  96. </script>
  97. </body>
  98. </html>

大数据量树状图

以下实例颜色了大数据量的树状图,具体实例数据可通过点击"尝试一下"查看。

文件名:highcharts_tree_largemap.htm

  1. <html>
  2. <head>
  3. <title>Highcharts Tutorial</title>
  4.    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  5.    <script src="/try/demo_source/highcharts.js"></script>    
  6.    <script src="http://code.highcharts.com/modules/treemap.js"></script>    
  7.    <script src="http://code.highcharts.com/modules/heatmap.js"></script>  
  8. </head>
  9. <body>
  10. <div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
  11. <script language="JavaScript">
  12. $(document).ready(function() { 
  13. //省略部分 js 代码
  14.  var data = {……};
  15.  var points = [],
  16. region_p,
  17. region_val,
  18. region_i,
  19. country_p,
  20. country_i,
  21. cause_p,
  22. cause_i,
  23. cause_name = [];
  24. cause_name['Communicable & other Group I'] = 'Communicable diseases';
  25. cause_name['Noncommunicable diseases'] = 'Non-communicable diseases';
  26. cause_name['Injuries'] = 'Injuries';
  27. region_i = 0;
  28. for (var region in data) {
  29. region_val = 0;
  30. region_p = {
  31. id: "id_" + region_i,
  32. name: region,
  33. color: Highcharts.getOptions().colors[region_i]
  34. };
  35. country_i = 0;
  36. for (var country in data[region]) {
  37. country_p = {
  38. id: region_p.id + "_" + country_i,
  39. name: country,
  40. parent: region_p.id
  41. };
  42. points.push(country_p);
  43. cause_i = 0;
  44. for (var cause in data[region][country]) {
  45. cause_p = {
  46. id: country_p.id + "_" + cause_i,
  47. name: cause_name[cause],
  48. parent: country_p.id,
  49. value: Math.round(+data[region][country][cause])
  50. };
  51. region_val += cause_p.value;
  52. points.push(cause_p);
  53. cause_i++;
  54. }
  55. country_i++;
  56. }
  57. region_p.value = Math.round(region_val / country_i);
  58. points.push(region_p);
  59. region_i++;
  60. }
  61.    var chart = {
  62.       renderTo: 'container'
  63.    };
  64.  
  65.    var title = {
  66.       text: 'Global Mortality Rate 2012, per 100 000 population'   
  67.    };        
  68.    
  69.    var subtitle: {
  70.       text: 'Click points to drill down. Source: <a href="http://apps.who.int/gho/data/node.main.12?lang=en">WHO</a>.'
  71.    };
  72.    
  73.    var series = [{
  74.       type: "treemap",
  75.       layoutAlgorithm: 'squarified',
  76.       allowDrillToNode: true,
  77.       dataLabels: {
  78.          enabled: false
  79.       },
  80.       levelIsConstant: false,
  81.       levels: [{
  82.          level: 1,
  83.          dataLabels: {
  84.             enabled: true
  85.          },
  86.       borderWidth: 3
  87.       }],
  88.       data: points
  89.    }]; 
  90.       
  91.    var json = {};     
  92.    json.title = title;            
  93.    json.series = series;       
  94.    
  95.    $('#container').highcharts(json);
  96. });
  97. </script>
  98. </body>
  99. </html>