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
var tree = anychart.data.tree([
4
{"name": "January", "id": "1", "year 2004": "12000", "year 2005": "18000"},
5
{"name": "Parent Node", "id": "2", "year 2004": "15000", "year 2005": "10000"},
6
{"name": "Child Node 1", "parent": "2", "id": "3", "year 2004": "16000", "year 2005": "18000"},
7
{"name": "Child Node 2", "parent": "2", "id": "4", "year 2004": "15000", "year 2005": "11000"},
8
{"name": "Node 2", "id": "5", "year 2004": "14000", "year 2005": "9000"}
9
], anychart.enums.TreeFillingMethod.AS_TABLE);
10
11
var stage = anychart.graphics.create("container");
12
13
// create Data Grid
14
treeDataGrid = anychart.standalones.dataGrid();
15
16
// set Data Tree
17
treeDataGrid.data(tree).headerHeight(20);
18
19
// create button for Parent node
20
treeDataGrid.listen("signal", treeDataGrid.draw, false, treeDataGrid);
21
22
// set style for second column
23
treeDataGrid.column(1).cellTextSettingsOverrider(labelTextSettingsOverrider);
24
var column2004 = treeDataGrid.column(2);
25
column2004.title().text("Year 2004"); // set title for the second column
26
column2004.width(100); // set width for the second column
27
column2004.format(function(item) { // format data
28
return item.get("year 2004"); // set name of the field with required data
29
});
30
31
// set style for second column
32
treeDataGrid.column(1).cellTextSettingsOverrider(labelTextSettingsOverrider);
33
var column2005 = treeDataGrid.column(3);
34
column2005.title().text("Year 2005"); // set title for the third column
35
column2005.width(100); // set width for the third column
36
column2005.format(function(item) { // format data
37
return item.get("year 2005"); // set name of the field with required data
38
});
39
40
// draw chart
41
treeDataGrid.container(stage).height(127).top(37).draw();
42
43
var title = anychart.standalones.title();
44
title.parentBounds(0, 0, "100%", 30)
45
.text("Data from Custom Columns")
46
.container(stage)
47
.draw();
48
49
// set container height
50
treeDataGrid.container().height(fullChildrenHeight()+treeDataGrid.top()+treeDataGrid.headerHeight()+1);
51
52
// get height of all items in the tree
53
function fullChildrenHeight () {
54
var height = 0;
55
var traverser = tree.getTraverser();
56
traverser.reset();
57
while(traverser.advance()){
58
59
// get an item
60
var currentItem = traverser.current();
61
62
// get height of the item
63
var currentHeight = currentItem.get(anychart.enums.GanttDataFields.ROW_HEIGHT);
64
65
// if item height is undefined
66
if (currentHeight !== void 0){
67
height += (currentHeight + 1);
68
} else {
69
// if height of current item is undefined, default height is 20px + 1px row space
70
height +=21;
71
}
72
}
73
return height;
74
}
75
});
76
77
// style setter
78
function labelTextSettingsOverrider(label, dataItem) {
79
if (!dataItem.getParent()) {
80
label.fontColor("#0055aa").fontWeight("bold").fontSize(12).fontStyle("italic");
81
} else {
82
if (dataItem.numChildren()) {
83
label.fontColor("#559955").fontWeight("bold").fontStyle("italic");
84
}
85
}
86
}