HTMLcopy
1
<div id="container"></div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
// The data used in this sample can be obtained from the CDN
3
// https://cdn.anychart.com/samples/pert-charts/pc-card-with-pert-chart/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples/pert-charts/pc-card-with-pert-chart/data.json',
6
function (data) {
7
// set filling method for tree
8
var treeData = anychart.data.tree(data, 'as-table');
9
// create PERT chart
10
var chart = anychart.pert();
11
// set data
12
chart
13
.data(treeData)
14
// set spacing between milestones
15
.horizontalSpacing('15.75%')
16
// set padding for chart
17
.padding([25, 50, 0, 50]);
18
19
// get duration project
20
var duration = Math.ceil(chart.getStat('pertChartProjectDuration'));
21
// get deviation project
22
var deviation = Math.ceil(
23
chart.getStat('pertChartCriticalPathStandardDeviation')
24
);
25
26
// set title text
27
chart
28
.title()
29
.enabled(true)
30
.useHtml(true)
31
.padding([0, 0, 35, 0])
32
.text(
33
'PC Card with PERT Chart' +
34
'<br>Project duration: ' +
35
duration +
36
'±' +
37
deviation +
38
' days'
39
);
40
41
// set settings for tasks
42
var tasks = chart.tasks();
43
// format upper label tasks
44
tasks.upperLabels().format(function () {
45
return this.item.get('fullName');
46
});
47
// format lower label tasks
48
tasks.lowerLabels().format(function () {
49
// format time for tasks
50
return timeTask(this.duration);
51
});
52
53
// create tasks tooltip
54
var tasksTooltip = tasks.tooltip();
55
// tooltip settings
56
tasksTooltip.separator(true).titleFormat(function () {
57
// return fullName from data
58
return this.item.get('fullName');
59
});
60
61
// set settings for critical tasks
62
chart.criticalPath().tasks().stroke('1 #D5A1DD').color('#9E44B6');
63
64
// set settings for milestones
65
var milestones = chart.milestones();
66
milestones.fill(function () {
67
if (this.creator) {
68
return '#9DACFF';
69
}
70
return this.isStart ? '#a5b3b3' : '#60727b';
71
});
72
// hover fill/stroke color for milestones item
73
milestones
74
.hovered()
75
.fill(function () {
76
if (this.creator) {
77
return anychart.color.lighten('#9DACFF', 0.25);
78
}
79
return this.isStart ? '#60727b' : '#60727b';
80
})
81
.stroke(function () {
82
return this.creator ? '1.5 #9DACFF' : null;
83
});
84
milestones.labels().format(function () {
85
if (this.creator) {
86
var name = this.creator.get('name');
87
return this.isStart ? 'S ' + name : 'F ' + name;
88
}
89
return this.isStart ? 'Start' : 'Finish';
90
});
91
milestones.tooltip().format(defuaultMilesoneTooltipTextFormatter);
92
93
// set settings for critical milestones
94
var critMilestones = chart.criticalPath().milestones();
95
// fill color for critMilestones item
96
critMilestones.fill(function () {
97
if (this.creator) {
98
return '#A489D4';
99
}
100
return this.isStart ? '#60727b' : '#60727b';
101
});
102
103
// hover fill/stroke color for critMilestones item
104
critMilestones
105
.hovered()
106
.fill(function () {
107
if (this.creator) {
108
return anychart.color.lighten('#A489D4', 0.25);
109
}
110
return this.isStart ? '#60727b' : '#60727b';
111
})
112
.stroke(function () {
113
return this.creator ? '1.5 #A489D4' : null;
114
});
115
// format labels
116
critMilestones.labels().format(function () {
117
if (this.creator) {
118
var name = this.creator.get('name');
119
return this.isStart ? 'S ' + name : 'F ' + name;
120
}
121
return '>>';
122
});
123
124
// set container id for the chart
125
chart.container('container');
126
// initiate chart drawing
127
chart.draw();
128
}
129
);
130
});
131
132
function timeTask(duration) {
133
var days = Math.floor(duration);
134
var hours = Math.ceil(24 * (duration - days));
135
var daysPart = days !== 0 ? 'd:' + days + ' ' : '';
136
var hoursPart = hours !== 0 ? 'h:' + hours + ' ' : '';
137
138
return daysPart + hoursPart;
139
}
140
141
function defuaultMilesoneTooltipTextFormatter() {
142
var result = '';
143
var i = 0;
144
if (this.successors && this.successors.length) {
145
result += 'Successors:';
146
for (i = 0; i < this.successors.length; i++) {
147
result += '\n - ' + this.successors[i].get('fullName');
148
}
149
if (this.predecessors && this.predecessors.length) result += '\n\n';
150
}
151
if (this.predecessors && this.predecessors.length) {
152
result += 'Predecessors:';
153
for (i = 0; i < this.predecessors.length; i++) {
154
result += '\n - ' + this.predecessors[i].get('fullName');
155
}
156
}
157
return result;
158
}