forked from creative-area/grafana-panel-plugin-horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
horizon.tooltip.js
210 lines (186 loc) · 8.75 KB
/
horizon.tooltip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
define([
'jquery',
'lodash',
],
function($, _) {
'use strict';
var formatValue = function (series, value) {
if (!_.isFinite(value)) {
value = null; // Prevent NaN formatting
}
return series.valueFormater(value, series.decimals, series.scaledDecimals);
}
function HorizonTooltip(elem, dashboard, scope, getSeriesFn) {
var self = this;
var ctrl = scope.ctrl;
var panel = ctrl.panel;
var $tooltip = $('<div id="tooltip" class="graph-tooltip">');
this.findHoverIndexFromDataPoints = function(posX, series, last) {
var ps = series.datapoints.pointsize;
var initial = last * ps;
var len = series.datapoints.points.length;
for (var j = initial; j < len; j += ps) {
if (series.datapoints.points[j] > posX) {
return Math.max(j - ps, 0) / ps;
}
}
return j / ps - 1;
};
this.findHoverIndexFromData = function(posX, series) {
var len = series.data.length;
for (var j = 0; j < len; j++) {
if (series.data[j][0] > posX) {
return Math.max(j - 1, 0);
}
}
return j - 1;
};
this.findSerieIndexFromPos = function(pos, nbSeries) {
var offset = elem.offset();
var elemData = elem.data();
return Math.min(nbSeries - 1, Math.floor((pos.pageY - offset.top) / (elemData.horizonHeight + elemData.marginBottom)));
};
this.showTooltip = function(title, innerHtml, pos) {
var body = '<div class="graph-tooltip-time">' + title + '</div> ';
body += innerHtml + '</div>';
$tooltip.html(body).place_tt(pos.pageX + 20, pos.pageY);
};
this.getMultiSeriesPlotHoverInfo = function(seriesList, pos) {
var value, i, series, hoverIndex;
var results = [];
//now we know the current X (j) position for X and Y values
var last_value = 0; //needed for stacked values
for (i = 0; i < seriesList.length; i++) {
series = seriesList[i];
if (!series.data.length || (panel.legend.hideEmpty && series.allIsNull)) {
results.push({
hidden: true
});
continue;
}
hoverIndex = this.findHoverIndexFromData(pos.x, series);
results.time = series.data[hoverIndex][0];
if (series.stack) {
if (panel.tooltip.value_type === 'individual') {
value = series.data[hoverIndex][1];
} else if (!series.stack) {
value = series.data[hoverIndex][1];
} else {
last_value += series.data[hoverIndex][1];
value = last_value;
}
} else {
value = series.data[hoverIndex][1];
}
// Highlighting multiple Points depending on the plot type
if (series.lines.steps || series.stack) {
// stacked and steppedLine plots can have series with different length.
// Stacked series can increase its length on each new stacked serie if null points found,
// to speed the index search we begin always on the last found hoverIndex.
var newhoverIndex = this.findHoverIndexFromDataPoints(pos.x, series, hoverIndex);
results.push({
value: value,
hoverIndex: newhoverIndex
});
} else {
results.push({
value: value,
hoverIndex: hoverIndex
});
}
}
return results;
};
elem.mouseleave(function() {
var $horizonLines = elem.find(".horizon-tooltip");
$horizonLines.each(function(i, horizonLine) {
var dataHorizonLine = $(horizonLine).data();
var plot = dataHorizonLine.plot;
if (plot) {
$tooltip.detach();
plot.unhighlight();
plot.clearCrosshair();
}
});
if (dashboard.sharedCrosshair) {
ctrl.publishAppEvent('clearCrosshair');
}
});
elem.bind("plothover", function(event, pos, item) {
var seriesList = getSeriesFn();
var plot;
var value, timestamp, series;
if (dashboard.sharedCrosshair) {
ctrl.publishAppEvent('setCrosshair', {
pos: pos,
scope: scope
});
}
if (seriesList.length === 0) {
return;
}
var $horizonLineHover = $(event.target);
plot = $horizonLineHover.data('plot');
plot.unhighlight();
var serieIndex = self.findSerieIndexFromPos(pos, seriesList.length);
var $horizonLines = elem.find(".horizon-tooltip");
$horizonLines.each(function(i, horizonLine) {
var dataHorizonLine = $(horizonLine).data();
if (dataHorizonLine.pos !== serieIndex) {
plot = dataHorizonLine.plot;
if (plot) {
plot.setCrosshair({
x: pos.x,
y: pos.y
});
}
}
});
if (panel.tooltip.shared) {
var seriesHtml = '';
var nbSeries = seriesList.length;
timestamp = null;
for (var i = 0; i < nbSeries; i++) {
series = seriesList[i];
var hoverIndex = self.findHoverIndexFromData(pos.x, series);
if (series) {
var highlightClass = '';
if (serieIndex === i) {
highlightClass = 'graph-tooltip-list-item--highlight';
}
value = formatValue(series, series.data[hoverIndex][1]);
timestamp = timestamp || dashboard.formatDate(series.data[hoverIndex][0]);
seriesHtml += '<div class="graph-tooltip-list-item ' + highlightClass + '"><div class="graph-tooltip-series-name">';
seriesHtml += '<i class="fa fa-minus"></i> ' + series.label + ':</div>';
seriesHtml += '<div class="graph-tooltip-value">' + value + '</div></div>';
}
}
self.showTooltip(timestamp, seriesHtml, pos);
}
// single series tooltip
else if (item) {
var plotData = plot.getData();
var seriesHoverInfo = self.getMultiSeriesPlotHoverInfo(plotData, pos);
if (seriesHoverInfo.pointCountMismatch) {
self.showTooltip('Shared tooltip error', '<ul>' +
'<li>Series point counts are not the same</li>' +
'<li>Set null point mode to null or null as zero</li>' +
'<li>For influxdb users set fill(0) in your query</li></ul>', pos);
return;
}
timestamp = dashboard.formatDate(seriesHoverInfo.time);
series = seriesList[serieIndex];
if (series) {
var hoverInfo = seriesHoverInfo[0];
value = formatValue(series, hoverInfo.value);
self.showTooltip(timestamp, series.label + ': <strong>' + value + '</strong>', pos);
}
}
// no hit
else {
$tooltip.detach();
}
});
}
return HorizonTooltip;
});