migrated graph_tooltip to ts

This commit is contained in:
Patrick O'Carroll 2018-03-29 11:51:34 +02:00
parent 65f7c5f08f
commit 7b9b34c6e1
2 changed files with 27 additions and 27 deletions

View File

@ -1,8 +1,6 @@
import $ from 'jquery'; import $ from 'jquery';
import { appEvents } from 'app/core/core'; import { appEvents } from 'app/core/core';
//var appEvents = appEvents;
export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) { export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
let self = this; let self = this;
let ctrl = scope.ctrl; let ctrl = scope.ctrl;
@ -15,10 +13,11 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
}; };
this.findHoverIndexFromDataPoints = function(posX, series, last) { this.findHoverIndexFromDataPoints = function(posX, series, last) {
var ps = series.datapoints.pointsize; let ps = series.datapoints.pointsize;
var initial = last * ps; let initial = last * ps;
var len = series.datapoints.points.length; let len = series.datapoints.points.length;
for (var j = initial; j < len; j += ps) { let j;
for (j = initial; j < len; j += ps) {
// Special case of a non stepped line, highlight the very last point just before a null point // Special case of a non stepped line, highlight the very last point just before a null point
if ( if (
(!series.lines.steps && series.datapoints.points[initial] != null && series.datapoints.points[j] == null) || (!series.lines.steps && series.datapoints.points[initial] != null && series.datapoints.points[j] == null) ||
@ -32,9 +31,9 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
}; };
this.findHoverIndexFromData = function(posX, series) { this.findHoverIndexFromData = function(posX, series) {
var lower = 0; let lower = 0;
var upper = series.data.length - 1; let upper = series.data.length - 1;
var middle; let middle;
while (true) { while (true) {
if (lower > upper) { if (lower > upper) {
return Math.max(upper, 0); return Math.max(upper, 0);
@ -58,14 +57,14 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
}; };
this.getMultiSeriesPlotHoverInfo = function(seriesList, pos) { this.getMultiSeriesPlotHoverInfo = function(seriesList, pos) {
var value, i, series, hoverIndex, hoverDistance, pointTime, yaxis; let value, i, series, hoverIndex, hoverDistance, pointTime, yaxis;
// 3 sub-arrays, 1st for hidden series, 2nd for left yaxis, 3rd for right yaxis. // 3 sub-arrays, 1st for hidden series, 2nd for left yaxis, 3rd for right yaxis.
var results: any = [[], [], []]; let results: any = [[], [], []];
//now we know the current X (j) position for X and Y values //now we know the current X (j) position for X and Y values
var last_value = 0; //needed for stacked values let last_value = 0; //needed for stacked values
var minDistance, minTime; let minDistance, minTime;
for (i = 0; i < seriesList.length; i++) { for (i = 0; i < seriesList.length; i++) {
series = seriesList[i]; series = seriesList[i];
@ -145,7 +144,7 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
elem.mouseleave(function() { elem.mouseleave(function() {
if (panel.tooltip.shared) { if (panel.tooltip.shared) {
var plot = elem.data().plot; let plot = elem.data().plot;
if (plot) { if (plot) {
$tooltip.detach(); $tooltip.detach();
plot.unhighlight(); plot.unhighlight();
@ -173,25 +172,25 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
}; };
this.show = function(pos, item) { this.show = function(pos, item) {
var plot = elem.data().plot; let plot = elem.data().plot;
var plotData = plot.getData(); let plotData = plot.getData();
var xAxes = plot.getXAxes(); let xAxes = plot.getXAxes();
var xMode = xAxes[0].options.mode; let xMode = xAxes[0].options.mode;
var seriesList = getSeriesFn(); let seriesList = getSeriesFn();
var allSeriesMode = panel.tooltip.shared; let allSeriesMode = panel.tooltip.shared;
var group, value, absoluteTime, hoverInfo, i, series, seriesHtml, tooltipFormat; let group, value, absoluteTime, hoverInfo, i, series, seriesHtml, tooltipFormat;
// if panelRelY is defined another panel wants us to show a tooltip // if panelRelY is defined another panel wants us to show a tooltip
// get pageX from position on x axis and pageY from relative position in original panel // get pageX from position on x axis and pageY from relative position in original panel
if (pos.panelRelY) { if (pos.panelRelY) {
var pointOffset = plot.pointOffset({ x: pos.x }); let pointOffset = plot.pointOffset({ x: pos.x });
if (Number.isNaN(pointOffset.left) || pointOffset.left < 0 || pointOffset.left > elem.width()) { if (Number.isNaN(pointOffset.left) || pointOffset.left < 0 || pointOffset.left > elem.width()) {
self.clear(plot); self.clear(plot);
return; return;
} }
pos.pageX = elem.offset().left + pointOffset.left; pos.pageX = elem.offset().left + pointOffset.left;
pos.pageY = elem.offset().top + elem.height() * pos.panelRelY; pos.pageY = elem.offset().top + elem.height() * pos.panelRelY;
var isVisible = let isVisible =
pos.pageY >= $(window).scrollTop() && pos.pageY <= $(window).innerHeight() + $(window).scrollTop(); pos.pageY >= $(window).scrollTop() && pos.pageY <= $(window).innerHeight() + $(window).scrollTop();
if (!isVisible) { if (!isVisible) {
self.clear(plot); self.clear(plot);
@ -219,7 +218,7 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
if (allSeriesMode) { if (allSeriesMode) {
plot.unhighlight(); plot.unhighlight();
var seriesHoverInfo = self.getMultiSeriesPlotHoverInfo(plotData, pos); let seriesHoverInfo = self.getMultiSeriesPlotHoverInfo(plotData, pos);
seriesHtml = ''; seriesHtml = '';
@ -244,7 +243,7 @@ export default function GraphTooltip(elem, dashboard, scope, getSeriesFn) {
continue; continue;
} }
var highlightClass = ''; let highlightClass = '';
if (item && hoverInfo.index === item.seriesIndex) { if (item && hoverInfo.index === item.seriesIndex) {
highlightClass = 'graph-tooltip-list-item--highlight'; highlightClass = 'graph-tooltip-list-item--highlight';
} }

View File

@ -11,6 +11,7 @@ var scope = {
var elem = $('<div></div>'); var elem = $('<div></div>');
var dashboard = {}; var dashboard = {};
var getSeriesFn;
function describeSharedTooltip(desc, fn) { function describeSharedTooltip(desc, fn) {
var ctx: any = {}; var ctx: any = {};
@ -30,7 +31,7 @@ function describeSharedTooltip(desc, fn) {
describe(desc, function() { describe(desc, function() {
beforeEach(function() { beforeEach(function() {
ctx.setupFn(); ctx.setupFn();
var tooltip = new GraphTooltip(elem, dashboard, scope); var tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
ctx.results = tooltip.getMultiSeriesPlotHoverInfo(ctx.data, ctx.pos); ctx.results = tooltip.getMultiSeriesPlotHoverInfo(ctx.data, ctx.pos);
}); });
@ -39,7 +40,7 @@ function describeSharedTooltip(desc, fn) {
} }
describe('findHoverIndexFromData', function() { describe('findHoverIndexFromData', function() {
var tooltip = new GraphTooltip(elem, dashboard, scope); var tooltip = new GraphTooltip(elem, dashboard, scope, getSeriesFn);
var series = { var series = {
data: [[100, 0], [101, 0], [102, 0], [103, 0], [104, 0], [105, 0], [106, 0], [107, 0]], data: [[100, 0], [101, 0], [102, 0], [103, 0], [104, 0], [105, 0], [106, 0], [107, 0]],
}; };