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
3
// create stage to put sort buttons along the chart
4
stage = anychart.graphics.create("container");
5
6
// create treemap
7
var chart = anychart.treeMap();
8
9
// setting the maximum levels depth
10
chart.maxDepth(4);
11
12
// set title
13
chart.title("Traversing sample: click button to get next item and drill to it");
14
15
// create ascending sort label
16
nextOne = createLabel("Get next node and drill to it", 10, function() {
17
// try to go to the next node in a tree
18
if (traverser.advance()) {
19
// if successuful - get current node as a data item instance
20
var dataItem = traverser.current();
21
// drill down to this item
22
chart.drillTo(dataItem);
23
}
24
else
25
{
26
// if we can't advance any more - reset the traverser
27
traverser.reset();
28
}
29
});
30
31
// data
32
var rawData = [
33
{name: 'Eurasia',
34
children:[
35
{name: "Asia", children:[
36
{name: "Eastern Asia", value: "7"},
37
{name: "Western Asia", value: "19"},
38
{name: "Southern Asia", value: "8"},
39
{name: "South-Eastern Asia", value: "11"},
40
{name: "Central Asia", value: "5"}
41
]},
42
{name: "Europe", children: [
43
{name: "Eastern Europe", value: "10"},
44
{name: "Western Europe", value: "18"},
45
{name: "Southern Europe", value: "5"},
46
{name: "Northern Europe", value: "10"},
47
]}
48
]}];
49
50
data = anychart.data.tree(rawData, anychart.enums.TreeFillingMethod.TREE);
51
52
// get traverser of a tree
53
traverser = data.getTraverser();
54
// skip root node first time so the first click didn't seem useless
55
traverser.advance()
56
57
// set up label
58
chart.labels().format("{%name}\n({%value} countries)");
59
60
// set data to a chart
61
chart.data(data);
62
63
// set chart bounds and display it
64
chart.bounds(0, "30", "100%", "90%");
65
chart.container(stage);
66
chart.draw();
67
});
68
69
// helper function to create buttons
70
function createLabel(text, offset, action){
71
var label = anychart.standalones.label();
72
label.background({fill: "#9E9E9E"});
73
label.text(text);
74
label.fontColor("#fff");
75
label.padding(5);
76
label.parentBounds(offset, 5, 130, 100);
77
label.listen("click", action);
78
label.container(stage);
79
label.draw();
80
label.listen("mouseOver", function(){
81
label.background().fill("#9E9E9E 0.5")
82
document.body.style.cursor = "pointer";
83
label.draw();
84
});
85
label.listen("mouseOut", function(){
86
label.background().fill("#9E9E9E")
87
document.body.style.cursor = "auto";
88
label.draw();
89
});
90
return label;
91
}