HTMLcopy
1
<div id="container"></div>
CSScopy
x
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
9
10
.anychart-tooltip {
11
padding: 15px;
12
background: white;
13
color: #333;
14
box-shadow: 2px 2px 5px #333;
15
border-radius: 0;
16
}
17
18
.anychart-tooltip-title .userpic {
19
float: left;
20
width: 40px;
21
margin-right: 15px;
22
}
23
24
.anychart-tooltip-title .userpic img {
25
max-width: 100%;
26
}
27
28
.anychart-tooltip-title .userinfo {
29
min-width: 210px;
30
text-align: right;
31
vertical-align: middle;
32
}
33
34
.anychart-tooltip-title h4,
35
.anychart-tooltip-title h5 {
36
margin: 0 0 5px;
37
}
38
39
.anychart-tooltip table {
40
width: 100%;
41
min-width: 150px;
42
}
43
44
.anychart-tooltip table tr td:last-child {
45
text-align: right;
46
}
JavaScriptcopy
116
1
anychart.onDocumentReady(function () {
2
// The data used in this sample can be obtained from the CDN
3
// https://cdn.anychart.com/samples-data/gantt-general-features/html-tooltip/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples-data/gantt-general-features/html-tooltip/data.json',
6
function (data) {
7
// create ganttResource chart
8
var chart = anychart.ganttResource();
9
10
// create data tree on raw data
11
var treeData = anychart.data.tree(data, 'as-table');
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
// setup formatters for the title and content of the tooltip using HTML
26
dgTooltip.useHtml(true);
27
dgTooltip.titleFormat(function () {
28
return (
29
'<div class="userpic"><img src="' +
30
this.item.get('userpic') +
31
'" alt=""/></div>' +
32
'<div class="userinfo"><h4>' +
33
this.name +
34
'</h4>Age: ' +
35
this.item.get('age') +
36
'</div>'
37
);
38
});
39
dgTooltip.format(function () {
40
// context object contains total min date (auto calculated for all periods of data item)
41
var startDate = this.minPeriodDate;
42
43
// context object contains total max date (auto calculated for all periods of data item)
44
var endDate = this.maxPeriodDate;
45
46
// Duration in days
47
var duration = Math.round(
48
(endDate - startDate) / (1000 * 60 * 60 * 24)
49
);
50
51
return (
52
'<table><tr><td>Total duration: </td><td>~' +
53
duration +
54
' days</td></tr></table>'
55
);
56
});
57
58
// getting chart's timeline
59
var timeline = chart.getTimeline();
60
61
// getting timeline's tooltip
62
var tlTooltip = timeline.tooltip();
63
64
// setup formatters for the title and content of the tooltip using HTML
65
tlTooltip.useHtml(true);
66
tlTooltip.titleFormat(function () {
67
var hoveredPeriodIndex = this.periodIndex + 1;
68
return '<h5>Period #' + hoveredPeriodIndex + '</h5>';
69
});
70
tlTooltip.format(function () {
71
var hoveredPeriod = this.period;
72
73
if (hoveredPeriod) {
74
tlTooltip.onContentChanged(function () {
75
var tooltip = document.querySelector('.anychart-tooltip');
76
if (tooltip) tooltip.style.visibility = 'visible';
77
});
78
79
// Getting period object's fields.
80
var startDate = hoveredPeriod.start;
81
82
// Getting period object's field.
83
var endDate = hoveredPeriod.end;
84
85
// Duration in hours
86
var duration = Math.round(
87
(endDate - startDate) / (1000 * 60 * 60)
88
);
89
90
return (
91
'<table><tr><td>id:</td><td>' +
92
hoveredPeriod.id +
93
'</td></tr>' +
94
'<tr><td>duration, hours:</td><td>' +
95
duration +
96
'</td></tr></table>'
97
);
98
}
99
tlTooltip.onContentChanged(function () {
100
var tooltip = document.querySelector('.anychart-tooltip');
101
if (tooltip) tooltip.style.visibility = 'hidden';
102
});
103
return '';
104
});
105
106
// set container id for the chart
107
chart.container('container');
108
109
// initiate chart drawing
110
chart.draw();
111
112
// zoom chart all dates range
113
chart.fitAll();
114
}
115
);
116
});