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
// data
3
var data = [
4
{id: '1', duration: 30, name: '1', fullName: 'Aerodynamics'},
5
{id: '2', duration: 50, name: '2', fullName: 'Build & Test Model'},
6
{id: '3', duration: 35, name: '3', fullName: 'Structure'},
7
{id: '4', duration: 50, name: '4', dependsOn: ['1'], fullName: 'Propulsion'},
8
{id: '5', duration: 60, name: '5', dependsOn: ['2'], fullName: 'Build Prototype'},
9
{id: '6', duration: 40, name: '6', dependsOn: ['3'], fullName: 'Control & Stability'},
10
{id: '7', duration: 20, name: '7', dependsOn: ['4'], fullName: 'Wind Tunnel'},
11
{id: '8', duration: 20, name: '8', dependsOn: ['6'], fullName: 'Computation'},
12
{id: '9', duration: 45, name: '9', dependsOn: ['7'], fullName: 'Review'},
13
{id: '10', duration: 30, name: '10', dependsOn: ['8'], fullName: 'Flight Simulation'},
14
{id: '11', duration: 50, name: '11', dependsOn: ['9'], fullName: 'Research flights'},
15
{id: '12', duration: 45, name: '12', dependsOn: ['10'], fullName: 'Revise & Review'},
16
{id: '13', duration: 25, name: '13', dependsOn: ['5'], fullName: 'Finalize'}
17
];
18
19
// set filling method for tree
20
var treeData = anychart.data.tree(data, "as-table");
21
22
// create PERT chart
23
var chart = anychart.pert();
24
// set spacing between milestones
25
chart.horizontalSpacing('18.7%');
26
// set padding for chart
27
chart.padding([25, 50, 0, 50]);
28
// set data
29
chart.data(treeData);
30
31
// get duration project
32
var duration = chart.getStat("pertChartProjectDuration");
33
34
// set settings for tasks
35
var tasks = chart.tasks();
36
// format upper label task
37
tasks.upperLabels().format(function () {
38
return this.item.get('fullName');
39
});
40
41
// format lower label task
42
tasks.lowerLabels().format(function () {
43
return this.duration + ' days';
44
});
45
decorateLabels(tasks);
46
47
// create tasks tooltip
48
var taskTooltip = tasks.tooltip();
49
// tooltip settings
50
taskTooltip.separator().enabled(true);
51
taskTooltip.titleFormat(function () {
52
// return fullName from data
53
return this.item.get('fullName');
54
}).title().useHtml('true');
55
56
// set settings for critical tasks
57
var critTasks = chart.criticalPath().tasks();
58
// format upper label task
59
critTasks.upperLabels().format(function () {
60
return this.item.get('fullName');
61
});
62
63
// format lower label task
64
critTasks.lowerLabels().format(function () {
65
return this.duration + ' days';
66
});
67
decorateLabels(critTasks);
68
69
// create tasks tooltip
70
var critTasksTooltip = critTasks.tooltip();
71
// tooltip settings
72
critTasksTooltip.separator().enabled(true);
73
critTasksTooltip.titleFormat(function () {
74
// return fullName from data
75
return this.item.get('fullName');
76
}).title().useHtml('true');
77
78
// set settings for milestones
79
var milestones = chart.milestones();
80
// set milestones color
81
milestones.color('#2C81D5');
82
83
milestones.tooltip().format(defuaultMilesoneTooltipformat);
84
85
milestones.hovered().fill(function () {
86
return anychart.color.lighten(this.sourceColor, 0.25);
87
});
88
// set milestones item size
89
milestones.size('6.5%');
90
91
// set settings for critical milestones
92
var critMilestones = chart.criticalPath().milestones();
93
// set color
94
critMilestones.color('#E24B26');
95
// set critMilestones item size
96
critMilestones.size('6.5%');
97
// format label
98
critMilestones.labels().format(function () {
99
return this['creator'] ? this['creator'].get('name') : this['isStart'] ? 'Start' : 'Finish';
100
});
101
102
critMilestones.tooltip().format(defuaultMilesoneTooltipformat);
103
104
// fill color for critMilestones item
105
critMilestones.fill(function () {
106
return this['creator'] ? this.sourceColor : this['isStart'] ? '#60727B' : '#60727B';
107
});
108
// hoverFill color for critMilestones item
109
critMilestones.hovered().fill(function () {
110
return this['creator'] ? anychart.color.lighten(this.sourceColor, 0.25) : this['isStart'] ? '#60727b' : '#60727b';
111
});
112
// hoverStroke color for critMilestones item
113
critMilestones.hovered().stroke(function () {
114
return this['creator'] ? '1.5 #a94e3d' : null;
115
});
116
117
// set container id for the chart
118
chart.container('container');
119
// initiate chart drawing
120
chart.draw();
121
});
122
123
function decorateLabels(tasks) {
124
decorateUpperLabels(tasks.upperLabels());
125
decorateUpperLabels(tasks.hovered().upperLabels(), true);
126
decorateUpperLabels(tasks.selected().upperLabels(), true);
127
decorateLowerLabels(tasks.lowerLabels());
128
decorateLowerLabels(tasks.hovered().lowerLabels(), true);
129
decorateLowerLabels(tasks.selected().lowerLabels(), true);
130
}
131
132
function decorateUpperLabels(labels, opt_isBold) {
133
labels.padding({'top': 0, 'right': 15, 'bottom': 0, 'left': 15})
134
.fontSize(10).fontWeight(opt_isBold ? 'bold' : 'normal');
135
}
136
137
function decorateLowerLabels(labels, opt_isBold) {
138
labels.padding({'top': 0, 'right': 5, 'bottom': 0, 'left': 5})
139
.fontSize(9).fontOpacity(0.5).fontWeight(opt_isBold ? 'bold' : 'normal');
140
}
141
142
function defuaultMilesoneTooltipformat() {
143
var result = '';
144
var i = 0;
145
if (this['successors'] && this['successors'].length) {
146
result += 'Successors:';
147
for (i = 0; i < this['successors'].length; i++) {
148
result += '\n - ' + this['successors'][i].get('fullName');
149
}
150
if (this['predecessors'] && this['predecessors'].length)
151
result += '\n\n';
152
}
153
if (this['predecessors'] && this['predecessors'].length) {
154
result += 'Predecessors:';
155
for (i = 0; i < this['predecessors'].length; i++) {
156
result += '\n - ' + this['predecessors'][i].get('fullName');
157
}
158
}
159
return result;
160
}