HTMLcopy
1
<button onclick="drillToItem()">Drill Down to Item 3-4</button>
2
<button onclick="drillUpALevel()">Drill Up 1 Level</button>
3
<div id="container"></div>
CSScopy
15
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
button {
8
margin: 10px 0 0 10px;
9
}
10
#container {
11
position: absolute;
12
width: 100%;
13
top: 35px;
14
bottom: 0;
15
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// get data
4
var data = getData();
5
6
// create a data tree
7
treeData = anychart.data.tree(data, "as-tree");
8
9
// create a chart and set the data
10
chart = anychart.treeMap(treeData);
11
12
// set the depth of hints
13
chart.hintDepth(2);
14
15
// configure labels
16
chart.labels().format("{%name}");
17
18
/* listen to the chartDraw event
19
and add the drilldown path to the chart title */
20
chart.listen("chartDraw", function() {
21
// get the drilldown path and convert it to a string
22
var text = printPath(chart.getDrilldownPath());
23
// set the chart title
24
chart.title().useHtml(true);
25
chart.title("Tree Data Model: search()<br><br>Path: " +
26
"<span style = 'color:#990000; font-style:italic'>" +
27
text + "</span>");
28
});
29
30
// set the container id
31
chart.container("container");
32
33
// initiate drawing the chart
34
chart.draw();
35
});
36
37
// get data
38
function getData() {
39
// create data
40
var data = [
41
{name: "Root", children: [
42
{name: "Item 1", children: [
43
{name: "Item 1-1", children: [
44
{name: "Item 1-1-1", value: 1000},
45
{name: "Item 1-1-2", value: 600},
46
{name: "Item 1-1-3", value: 550},
47
{name: "Item 1-1-4", value: 300},
48
{name: "Item 1-1-5", value: 150}
49
]},
50
{name: "Item 1-2", value: 2300},
51
{name: "Item 1-3", value: 1500}
52
]},
53
{name: "Item 2", children: [
54
{name: "Item 2-1", children: [
55
{name: "Item 2-1-1", value: 2100},
56
{name: "Item 2-1-2", value: 1000},
57
{name: "Item 2-1-3", value: 800},
58
{name: "Item 2-1-4", value: 750}
59
]},
60
{name: "Item 2-2", children: [
61
{name: "Item 2-2-1", value: 560},
62
{name: "Item 2-2-2", value: 300},
63
{name: "Item 2-2-3", value: 150},
64
{name: "Item 2-2-4", value: 90}
65
]},
66
{name: "Item 2-3", value: 400}
67
]},
68
{name: "Item 3", children: [
69
{name: "Item 3-1", children: [
70
{name: "Item 3-1-1", value: 850},
71
{name: "Item 3-1-2", value: 400},
72
{name: "Item 3-1-3", value: 150}
73
]},
74
{name: "Item 3-2", value: 1350},
75
{name: "Item 3-3", value: 1300},
76
{name: "Item 3-4", children: [
77
{name: "Item 3-4-1", value: 400},
78
{name: "Item 3-4-2", value: 300},
79
{name: "Item 3-4-3", value: 250},
80
{name: "Item 3-4-4", value: 150}
81
]}
82
]}
83
]}
84
];
85
return data;
86
}
87
88
// convert the current drilldown path to a string
89
function printPath(path) {
90
/* go through the array of data items
91
and use the get() method to obtain the "name" field */
92
var text = "";
93
for (i = 0; i < path.length; i++){
94
text += path[i].get("name") + "\\";
95
}
96
return text;
97
}
98
99
// drill down to a data item
100
function drillToItem() {
101
/* locate an item in the data tree
102
and get it as an object */
103
var item = treeData.search("name", "Item 3-4");
104
// drill down to the item
105
chart.drillTo(item);
106
}
107
108
// drill up a level
109
function drillUpALevel() {
110
chart.drillUp();
111
}