Highcharts 树状图(Treemap)
本章节我们将为大家介绍 Highcharts 的热点图。
我们在前面的章节已经了解了 Highcharts 配置语法。接下来让我们来看下 Highcharts 的其他配置。
树状图
series 配置
设置 series 的 type 属性为 treemap ,series.type 描述了数据列类型。默认值为 "line"。
var chart = {
type: &qpos;treemap&qpos;
};
实例
文件名:highcharts_tree_map.htm
<html>
<head>
<meta charset="UTF-8" />
<title>Highcharts 教程 | 菜鸟教程(runoob.com)</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>
<script src="http://code.highcharts.com/modules/treemap.js"></script>
<script src="http://code.highcharts.com/modules/heatmap.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
$(document).ready(function() {
var title = {
text: &qpos;Highcharts Treemap&qpos;
};
var colorAxis = {
minColor: &qpos;#FFFFFF&qpos;,
maxColor: Highcharts.getOptions().colors[0]
};
var series= [{
type: "treemap",
layoutAlgorithm: &qpos;squarified&qpos;,
data: [{
name: &qpos;A&qpos;,
value: 6,
colorValue: 1
}, {
name: &qpos;B&qpos;,
value: 6,
colorValue: 2
}, {
name: &qpos;C&qpos;,
value: 4,
colorValue: 3
}, {
name: &qpos;D&qpos;,
value: 3,
colorValue: 4
}, {
name: &qpos;E&qpos;,
value: 2,
colorValue: 5
}, {
name: &qpos;F&qpos;,
value: 2,
colorValue: 6
}, {
name: &qpos;G&qpos;,
value: 1,
colorValue: 7
}]
}];
var json = {};
json.title = title;
json.colorAxis = colorAxis;
json.series = series;
$(&qpos;#container&qpos;).highcharts(json);
});
</script>
</body>
</html>
以上实例输出结果为:
不同等级树状图
以下实例使用不同颜色来标识不同等级的树状图。
实例
文件名:highcharts_tree_levels.htm(完整源码请点击实例查看)
<html>
<head>
<meta charset="UTF-8" />
<title>Highcharts 教程 | 菜鸟教程(runoob.com)</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>
<script src="http://code.highcharts.com/modules/treemap.js"></script>
<script src="http://code.highcharts.com/modules/heatmap.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
$(document).ready(function() {
var title = {
text: &qpos;Fruit consumption&qpos;
};
var series = [{
type: "treemap",
layoutAlgorithm: &qpos;stripes&qpos;,
alternateStartingDirection: true,
levels: [{
level: 1,
layoutAlgorithm: &qpos;sliceAndDice&qpos;,
dataLabels: {
enabled: true,
align: &qpos;left&qpos;,
verticalAlign: &qpos;top&qpos;,
style: {
fontSize: &qpos;15px&qpos;,
fontWeight: &qpos;bold&qpos;
}
}
}],
data: [{
id: &qpos;A&qpos;,
name: &qpos;Apples&qpos;,
color: "#EC2500"
}, {
id:&qpos;B&qpos;,
name: &qpos;Bananas&qpos;,
color: "#ECE100"
}, {
id: &qpos;O&qpos;,
name: &qpos;Oranges&qpos;,
color: &qpos;#EC9800&qpos;
}, {
name: &qpos;Anne&qpos;,
parent: &qpos;A&qpos;,
value: 5
}, {
name: &qpos;Rick&qpos;,
parent: &qpos;A&qpos;,
value: 3
}, {
name: &qpos;Peter&qpos;,
parent: &qpos;A&qpos;,
value: 4
}, {
name: &qpos;Anne&qpos;,
parent: &qpos;B&qpos;,
value: 4
}, {
name: &qpos;Rick&qpos;,
parent: &qpos;B&qpos;,
value: 10
}, {
name: &qpos;Peter&qpos;,
parent: &qpos;B&qpos;,
value: 1
}, {
name: &qpos;Anne&qpos;,
parent: &qpos;O&qpos;,
value: 1
}, {
name: &qpos;Rick&qpos;,
parent: &qpos;O&qpos;,
value: 3
}, {
name: &qpos;Peter&qpos;,
parent: &qpos;O&qpos;,
value: 3
}, {
name: &qpos;Susanne&qpos;,
parent: &qpos;Kiwi&qpos;,
value: 2,
color: &qpos;#9EDE00&qpos;
}]
}];
var json = {};
json.title = title;
json.series = series;
$(&qpos;#container&qpos;).highcharts(json);
});
</script>
</body>
</html>
以上实例输出结果为:
大数据量树状图
以下实例颜色了大数据量的树状图,具体实例数据可通过点击"尝试一下"查看。
文件名:highcharts_tree_largemap.htm
<html>
<head>
<meta charset="UTF-8" />
<title>Highcharts 教程 | 菜鸟教程(runoob.com)</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>
<script src="http://code.highcharts.com/modules/treemap.js"></script>
<script src="http://code.highcharts.com/modules/heatmap.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
$(document).ready(function() {
//省略部分 js 代码
var data = {……};
var points = [],
region_p,
region_val,
region_i,
country_p,
country_i,
cause_p,
cause_i,
cause_name = [];
cause_name[&qpos;Communicable & other Group I&qpos;] = &qpos;Communicable diseases&qpos;;
cause_name[&qpos;Noncommunicable diseases&qpos;] = &qpos;Non-communicable diseases&qpos;;
cause_name[&qpos;Injuries&qpos;] = &qpos;Injuries&qpos;;
region_i = 0;
for (var region in data) {
region_val = 0;
region_p = {
id: "id_" + region_i,
name: region,
color: Highcharts.getOptions().colors[region_i]
};
country_i = 0;
for (var country in data[region]) {
country_p = {
id: region_p.id + "_" + country_i,
name: country,
parent: region_p.id
};
points.push(country_p);
cause_i = 0;
for (var cause in data[region][country]) {
cause_p = {
id: country_p.id + "_" + cause_i,
name: cause_name[cause],
parent: country_p.id,
value: Math.round(+data[region][country][cause])
};
region_val += cause_p.value;
points.push(cause_p);
cause_i++;
}
country_i++;
}
region_p.value = Math.round(region_val / country_i);
points.push(region_p);
region_i++;
}
var chart = {
renderTo: &qpos;container&qpos;
};
var title = {
text: &qpos;Global Mortality Rate 2012, per 100 000 population&qpos;
};
var subtitle: {
text: &qpos;Click points to drill down. Source: <a href="http://apps.who.int/gho/data/node.main.12?lang=en">WHO</a>.&qpos;
};
var series = [{
type: "treemap",
layoutAlgorithm: &qpos;squarified&qpos;,
allowDrillToNode: true,
dataLabels: {
enabled: false
},
levelIsConstant: false,
levels: [{
level: 1,
dataLabels: {
enabled: true
},
borderWidth: 3
}],
data: points
}];
var json = {};
json.title = title;
json.series = series;
$(&qpos;#container&qpos;).highcharts(json);
});
</script>
</body>
</html>
以上实例输出结果为: