课程表

Highcharts课程

工具箱
速查手册

Highcharts 树状图

当前位置:免费教程 » JS/JS库/框架 » Highcharts

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

我来试一下

以上实例输出结果为:


不同等级树状图

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

实例

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

  1. <html>
  2. <head>
  3. <meta charset="UTF-8" />
  4. <title>Highcharts 教程 | W3xue教程(w3xue.com)</title>
  5. <script src="/js/jquery-2.1.4.min.js"></script>
  6. <script src="http://code.highcharts.com/highcharts.js"></script>
  7. <script src="http://code.highcharts.com/modules/treemap.js"></script>
  8. <script src="http://code.highcharts.com/modules/heatmap.js"></script>
  9. </head>
  10. <body>
  11. <div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
  12. <script language="JavaScript">
  13. $(document).ready(function() {
  14. var title = {
  15. text: 'Fruit consumption'
  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. var json = {};
  90. json.title = title;
  91. json.series = series;
  92. $('#container').highcharts(json);
  93. });
  94. </script>
  95. </body>
  96. </html>

我来试一下

以上实例输出结果为:


大数据量树状图

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

文件名:highcharts_tree_largemap.htm

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

我来试一下

以上实例输出结果为:

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号