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/gantt-general-features/chart-tooltips/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples/gantt-general-features/chart-tooltips/data.json',
6
function (data) {
7
// create data tree on raw data
8
var treeData = anychart.data.tree(data, 'as-table');
9
10
// create resource gantt chart
11
var chart = anychart.ganttResource();
12
13
// set data for the chart
14
chart.data(treeData);
15
16
// set the spliter position
17
chart.splitterPosition(200);
18
19
// getting chart's data grid
20
var dataGrid = chart.dataGrid();
21
22
// getting data grid's tooltip
23
var dgTooltip = dataGrid.tooltip();
24
25
dgTooltip.titleFormat(function () {
26
return 'Custom DG tooltip for ' + this.name;
27
});
28
29
dgTooltip.format(function () {
30
// context object contains total min date (auto calculated for all periods of data item)
31
var startDate = this.minPeriodDate;
32
33
// context object contains total max date (auto calculated for all periods of data item)
34
var endDate = this.maxPeriodDate;
35
36
// Duration in days
37
var duration = Math.round(
38
(endDate - startDate) / (1000 * 60 * 60 * 24)
39
);
40
41
return duration
42
? 'Total duration: ~' + duration + ' d.'
43
: '-----';
44
});
45
46
// getting chart's timeline
47
var timeline = chart.getTimeline();
48
49
// getting timeline's tooltip
50
var tlTooltip = timeline.tooltip();
51
52
tlTooltip.titleFormat(function () {
53
return 'Custom TL tooltip for ' + this.name;
54
});
55
56
tlTooltip.format(function () {
57
var hoveredPeriod = this.period;
58
59
if (hoveredPeriod) {
60
var hoveredPeriodIndex = this.periodIndex + 1;
61
62
// Getting period object's fields.
63
var startDate = hoveredPeriod.start;
64
65
// Getting period object's field.
66
var endDate = hoveredPeriod.end;
67
68
// Duration in hours
69
var duration = Math.round(
70
(endDate - startDate) / (1000 * 60 * 60)
71
);
72
73
return (
74
'Period #' +
75
hoveredPeriodIndex +
76
'\nid: ' +
77
hoveredPeriod.id +
78
'\nduration: ' +
79
duration +
80
'h.'
81
);
82
}
83
return 'No periods under cursor';
84
});
85
86
// set container id for the chart
87
chart.container('container');
88
89
// initiate chart drawing
90
chart.draw();
91
92
// zoom chart all dates range
93
chart.fitAll();
94
}
95
);
96
});