HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
stage = anychart.graphics.create("container");
3
4
var data = [
5
{id: 'Eurasia', parent: null},
6
{id: 'Asia', parent: 'Eurasia'},
7
{id: 'Eastern Asia', parent: 'Asia'},
8
{id: 'Mongolia', parent: 'Eastern Asia', value: 1564116, capital: 'Ulan-Bator'},
9
{id: 'China', parent: 'Eastern Asia', value: 1564116, capital: 'Beijing'},
10
{id: 'Southern Korea', parent: 'Eastern Asia', value: 1564116, capital: 'Seoul'},
11
{id: 'Northern Korea', parent: 'Eastern Asia', value: 120540, capital: 'Pyongyang'},
12
{id: 'Japan', parent: 'Eastern Asia', value: 1564116, capital: 'Tokio'},
13
{id: 'Europe', parent: 'Eurasia'},
14
{id: 'Northern Europe', parent: 'Europe'},
15
{id: 'Finland', parent: 'Northern Europe', value: 338424, capital: 'Helsinki'},
16
{id: 'Great Britain', parent: 'Northern Europe', value: 209331, capital: 'London'},
17
{id: 'Ireland', parent: 'Northern Europe', value: 84421, capital: 'Dublin'},
18
{id: 'Scandinavia', parent: 'Northern Europe', value: 928057}];
19
20
// create treemap chart
21
var chart = anychart.treeMap();
22
23
// create storage for tree data
24
var treeData = anychart.data.tree(data, "asTable");
25
26
// set data to chart
27
chart.data(treeData);
28
29
// locate an item in a tree data and get it as an object
30
// that can be used in drillTo method
31
var item = treeData.search("id", "Finland");
32
33
// create drill to label
34
drillToLabel = createLabel("Drill To Finland.", 10, function() {
35
// Drill down to the specified item.
36
chart.drillTo(item);
37
});
38
39
// create drill up label
40
drillUpLabel = createLabel("Drill Up a Level", 130, function() {
41
chart.drillUp();
42
});
43
44
// listen for draw event and update path in a title
45
chart.listen("chartDraw",function(){
46
chart.title(printPath(chart.getDrilldownPath()))
47
});
48
49
// put title with path to the bottom of the chart
50
chart.title().orientation("bottom");
51
chart.title().padding(10);
52
// format labels
53
chart.labels().format("{%id}");
54
// set chart bounds and display it
55
chart.bounds(0, "30", "100%", "90%");
56
chart.container(stage);
57
chart.draw();
58
});
59
60
// function to turn current drill down path structure to string
61
function printPath(path){
62
// simply go through the array of Tree Data Items (https://api.anychart.com/7.14.3/anychart.data.Tree.DataItem)
63
// and use get() method to obtain id field which stores region names in this case
64
var text = "";
65
for (i = 0; i < path.length; i++) {
66
text += path[i].get("id") + "\\";
67
}
68
return text;
69
}
70
71
// helper function to create buttons
72
function createLabel(text, offset, action){
73
var label = anychart.standalones.label();
74
label.background({fill: "#9E9E9E"});
75
label.text(text);
76
label.fontColor("#fff");
77
label.padding(5);
78
label.parentBounds(offset, 5, 130, 100);
79
label.listen("click", action);
80
label.container(stage);
81
label.draw();
82
return label;
83
}