2014-09-29 09:57:05 -05:00
|
|
|
define([
|
|
|
|
'jquery',
|
|
|
|
'kbn',
|
|
|
|
],
|
|
|
|
function ($, kbn) {
|
|
|
|
'use strict';
|
|
|
|
|
2014-10-01 02:08:11 -05:00
|
|
|
function registerTooltipFeatures(elem, dashboard, scope) {
|
2014-09-29 09:57:05 -05:00
|
|
|
|
|
|
|
var $tooltip = $('<div id="tooltip">');
|
|
|
|
|
|
|
|
elem.mouseleave(function () {
|
2014-10-01 02:08:11 -05:00
|
|
|
if (scope.panel.tooltip.shared || dashboard.sharedCrosshair) {
|
2014-09-29 09:57:05 -05:00
|
|
|
var plot = elem.data().plot;
|
2014-10-01 02:08:11 -05:00
|
|
|
if (plot) {
|
|
|
|
$tooltip.detach();
|
|
|
|
plot.unhighlight();
|
|
|
|
scope.appEvent('clearCrosshair');
|
|
|
|
}
|
2014-09-29 09:57:05 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function findHoverIndex(posX, series) {
|
|
|
|
for (var j = 0; j < series.data.length; j++) {
|
|
|
|
if (series.data[j][0] > posX) {
|
|
|
|
return Math.max(j - 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return j - 1;
|
|
|
|
}
|
|
|
|
|
2014-10-01 03:30:24 -05:00
|
|
|
function showTooltip(title, innerHtml, pos) {
|
|
|
|
var body = '<div class="graph-tooltip small"><div class="graph-tooltip-time">'+ title + '</div> ' ;
|
|
|
|
body += innerHtml + '</div>';
|
|
|
|
$tooltip.html(body).place_tt(pos.pageX + 20, pos.pageY);
|
|
|
|
}
|
|
|
|
|
2014-09-29 09:57:05 -05:00
|
|
|
elem.bind("plothover", function (event, pos, item) {
|
|
|
|
var plot = elem.data().plot;
|
|
|
|
var data = plot.getData();
|
|
|
|
var group, value, timestamp, seriesInfo, format, i, series, hoverIndex, seriesHtml;
|
|
|
|
|
2014-09-29 23:24:56 -05:00
|
|
|
if(dashboard.sharedCrosshair){
|
2014-10-01 02:08:11 -05:00
|
|
|
scope.appEvent('setCrosshair', { pos: pos, scope: scope });
|
2014-09-29 23:24:56 -05:00
|
|
|
}
|
2014-10-01 02:08:11 -05:00
|
|
|
|
2014-09-29 09:57:05 -05:00
|
|
|
if (scope.panel.tooltip.shared) {
|
|
|
|
plot.unhighlight();
|
|
|
|
|
|
|
|
//check if all series has same length if so, only one x index will
|
|
|
|
//be checked and only for exact timestamp values
|
|
|
|
var pointCount = data[0].data.length;
|
|
|
|
for (i = 1; i < data.length; i++) {
|
|
|
|
if (data[i].data.length !== pointCount) {
|
2014-10-01 03:30:24 -05:00
|
|
|
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);
|
2014-09-29 09:57:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seriesHtml = '';
|
|
|
|
series = data[0];
|
|
|
|
hoverIndex = findHoverIndex(pos.x, series);
|
|
|
|
|
|
|
|
//now we know the current X (j) position for X and Y values
|
|
|
|
timestamp = dashboard.formatDate(series.data[hoverIndex][0]);
|
|
|
|
var last_value = 0; //needed for stacked values
|
|
|
|
|
|
|
|
for (i = data.length-1; i >= 0; --i) {
|
|
|
|
//stacked values should be added in reverse order
|
|
|
|
series = data[i];
|
|
|
|
seriesInfo = series.info;
|
|
|
|
format = scope.panel.y_formats[seriesInfo.yaxis - 1];
|
|
|
|
|
2014-09-29 23:24:56 -05:00
|
|
|
if (scope.panel.stack) {
|
|
|
|
if (scope.panel.stack && scope.panel.tooltip.value_type === 'individual') {
|
|
|
|
value = series.data[hoverIndex][1];
|
|
|
|
} else {
|
|
|
|
last_value += series.data[hoverIndex][1];
|
|
|
|
value = last_value;
|
|
|
|
}
|
2014-09-29 09:57:05 -05:00
|
|
|
} else {
|
2014-09-29 23:24:56 -05:00
|
|
|
value = series.data[hoverIndex][1];
|
2014-09-29 09:57:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
value = kbn.valueFormats[format](value, series.yaxis.tickDecimals);
|
|
|
|
|
|
|
|
if (seriesInfo.alias) {
|
|
|
|
group = '<i class="icon-minus" style="color:' + series.color +';"></i> ' + seriesInfo.alias;
|
|
|
|
} else {
|
|
|
|
group = kbn.query_color_dot(series.color, 15) + ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
//pre-pending new values
|
|
|
|
seriesHtml = group + ': <span class="graph-tooltip-value">' + value + '</span><br>' + seriesHtml;
|
|
|
|
|
|
|
|
plot.highlight(i, hoverIndex);
|
|
|
|
}
|
|
|
|
|
2014-10-01 03:30:24 -05:00
|
|
|
showTooltip(timestamp, seriesHtml, pos);
|
2014-09-29 09:57:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (item) {
|
|
|
|
seriesInfo = item.series.info;
|
|
|
|
format = scope.panel.y_formats[seriesInfo.yaxis - 1];
|
2014-10-01 03:30:24 -05:00
|
|
|
group = '<i class="icon-minus" style="color:' + item.series.color +';"></i> ' + seriesInfo.alias;
|
2014-09-29 09:57:05 -05:00
|
|
|
|
|
|
|
if (scope.panel.stack && scope.panel.tooltip.value_type === 'individual') {
|
|
|
|
value = item.datapoint[1] - item.datapoint[2];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = item.datapoint[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
value = kbn.valueFormats[format](value, item.series.yaxis.tickDecimals);
|
|
|
|
timestamp = dashboard.formatDate(item.datapoint[0]);
|
2014-10-01 03:30:24 -05:00
|
|
|
group += ': <span class="graph-tooltip-value">' + value + '</span>';
|
2014-09-29 09:57:05 -05:00
|
|
|
|
2014-10-01 03:30:24 -05:00
|
|
|
showTooltip(timestamp, group, pos);
|
2014-09-29 09:57:05 -05:00
|
|
|
} else {
|
|
|
|
$tooltip.detach();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
register: registerTooltipFeatures
|
|
|
|
};
|
|
|
|
});
|