graph.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import 'vendor/flot/jquery.flot';
  3. import 'vendor/flot/jquery.flot.selection';
  4. import 'vendor/flot/jquery.flot.time';
  5. import 'vendor/flot/jquery.flot.stack';
  6. import 'vendor/flot/jquery.flot.stackpercent';
  7. import 'vendor/flot/jquery.flot.fillbelow';
  8. import 'vendor/flot/jquery.flot.crosshair';
  9. import 'vendor/flot/jquery.flot.dashes';
  10. import './jquery.flot.events';
  11. import $ from 'jquery';
  12. import _ from 'lodash';
  13. import moment from 'moment';
  14. import kbn from 'app/core/utils/kbn';
  15. import {tickStep} from 'app/core/utils/ticks';
  16. import {appEvents, coreModule} from 'app/core/core';
  17. import GraphTooltip from './graph_tooltip';
  18. import {ThresholdManager} from './threshold_manager';
  19. import {EventManager} from 'app/features/annotations/all';
  20. import {convertValuesToHistogram, getSeriesValues} from './histogram';
  21. /** @ngInject **/
  22. function graphDirective($rootScope, timeSrv, popoverSrv, contextSrv) {
  23. return {
  24. restrict: 'A',
  25. template: '',
  26. link: function(scope, elem) {
  27. var ctrl = scope.ctrl;
  28. var dashboard = ctrl.dashboard;
  29. var panel = ctrl.panel;
  30. var annotations = [];
  31. var data;
  32. var plot;
  33. var sortedSeries;
  34. var rootScope = scope.$root;
  35. var panelWidth = 0;
  36. var eventManager = new EventManager(ctrl);
  37. var thresholdManager = new ThresholdManager(ctrl);
  38. var tooltip = new GraphTooltip(elem, dashboard, scope, function() {
  39. return sortedSeries;
  40. });
  41. // panel events
  42. ctrl.events.on('panel-teardown', () => {
  43. thresholdManager = null;
  44. if (plot) {
  45. plot.destroy();
  46. plot = null;
  47. }
  48. });
  49. /**
  50. * Split graph rendering into two parts.
  51. * First, calculate series stats in buildFlotPairs() function. Then legend rendering started
  52. * (see ctrl.events.on('render') in legend.ts).
  53. * When legend is rendered it emits 'legend-rendering-complete' and graph rendered.
  54. */
  55. ctrl.events.on('render', (renderData) => {
  56. data = renderData || data;
  57. if (!data) {
  58. return;
  59. }
  60. annotations = ctrl.annotations || [];
  61. buildFlotPairs(data);
  62. ctrl.events.emit('render-legend');
  63. });
  64. ctrl.events.on('legend-rendering-complete', () => {
  65. render_panel();
  66. });
  67. // global events
  68. appEvents.on('graph-hover', (evt) => {
  69. // ignore other graph hover events if shared tooltip is disabled
  70. if (!dashboard.sharedTooltipModeEnabled()) {
  71. return;
  72. }
  73. // ignore if we are the emitter
  74. if (!plot || evt.panel.id === panel.id || ctrl.otherPanelInFullscreenMode()) {
  75. return;
  76. }
  77. tooltip.show(evt.pos);
  78. }, scope);
  79. appEvents.on('graph-hover-clear', (event, info) => {
  80. if (plot) {
  81. tooltip.clear(plot);
  82. }
  83. }, scope);
  84. function getLegendHeight(panelHeight) {
  85. const LEGEND_PADDING = 23;
  86. if (!panel.legend.show || panel.legend.rightSide) {
  87. return 0;
  88. }
  89. let legendHeight = getLegendContainerHeight() + LEGEND_PADDING;
  90. return Math.min(legendHeight, Math.floor(panelHeight/2));
  91. }
  92. function getLegendContainerHeight() {
  93. try {
  94. let graphWrapperElem = elem.parent().parent();
  95. let legendElem = graphWrapperElem.find('.graph-legend-wrapper');
  96. let legendHeight = legendElem.height();
  97. return legendHeight;
  98. } catch (e) {
  99. console.log(e);
  100. return 0;
  101. }
  102. }
  103. function setElementHeight() {
  104. try {
  105. var height = ctrl.height - getLegendHeight(ctrl.height);
  106. elem.css('height', height + 'px');
  107. return true;
  108. } catch (e) { // IE throws errors sometimes
  109. console.log(e);
  110. return false;
  111. }
  112. }
  113. function shouldAbortRender() {
  114. if (!data) {
  115. return true;
  116. }
  117. if (!setElementHeight()) { return true; }
  118. if (panelWidth === 0) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. function drawHook(plot) {
  124. // Update legend values
  125. var yaxis = plot.getYAxes();
  126. for (var i = 0; i < data.length; i++) {
  127. var series = data[i];
  128. var axis = yaxis[series.yaxis - 1];
  129. var formater = kbn.valueFormats[panel.yaxes[series.yaxis - 1].format];
  130. // decimal override
  131. if (_.isNumber(panel.decimals)) {
  132. series.updateLegendValues(formater, panel.decimals, null);
  133. } else {
  134. // auto decimals
  135. // legend and tooltip gets one more decimal precision
  136. // than graph legend ticks
  137. var tickDecimals = (axis.tickDecimals || -1) + 1;
  138. series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2);
  139. }
  140. if (!rootScope.$$phase) { scope.$digest(); }
  141. }
  142. // add left axis labels
  143. if (panel.yaxes[0].label && panel.yaxes[0].show) {
  144. $("<div class='axisLabel left-yaxis-label flot-temp-elem'></div>").text(panel.yaxes[0].label).appendTo(elem);
  145. }
  146. // add right axis labels
  147. if (panel.yaxes[1].label && panel.yaxes[1].show) {
  148. $("<div class='axisLabel right-yaxis-label flot-temp-elem'></div>").text(panel.yaxes[1].label).appendTo(elem);
  149. }
  150. thresholdManager.draw(plot);
  151. }
  152. function processOffsetHook(plot, gridMargin) {
  153. var left = panel.yaxes[0];
  154. var right = panel.yaxes[1];
  155. if (left.show && left.label) { gridMargin.left = 20; }
  156. if (right.show && right.label) { gridMargin.right = 20; }
  157. // apply y-axis min/max options
  158. var yaxis = plot.getYAxes();
  159. for (var i = 0; i < yaxis.length; i++) {
  160. var axis = yaxis[i];
  161. var panelOptions = panel.yaxes[i];
  162. axis.options.max = axis.options.max !== null ? axis.options.max : panelOptions.max;
  163. axis.options.min = axis.options.min !== null ? axis.options.min : panelOptions.min;
  164. }
  165. }
  166. // Series could have different timeSteps,
  167. // let's find the smallest one so that bars are correctly rendered.
  168. // In addition, only take series which are rendered as bars for this.
  169. function getMinTimeStepOfSeries(data) {
  170. var min = Number.MAX_VALUE;
  171. for (let i = 0; i < data.length; i++) {
  172. if (!data[i].stats.timeStep) {
  173. continue;
  174. }
  175. if (panel.bars) {
  176. if (data[i].bars && data[i].bars.show === false) {
  177. continue;
  178. }
  179. } else {
  180. if (typeof data[i].bars === 'undefined' || typeof data[i].bars.show === 'undefined' || !data[i].bars.show) {
  181. continue;
  182. }
  183. }
  184. if (data[i].stats.timeStep < min) {
  185. min = data[i].stats.timeStep;
  186. }
  187. }
  188. return min;
  189. }
  190. // Function for rendering panel
  191. function render_panel() {
  192. panelWidth = elem.width();
  193. if (shouldAbortRender()) {
  194. return;
  195. }
  196. // give space to alert editing
  197. thresholdManager.prepare(elem, data);
  198. // un-check dashes if lines are unchecked
  199. panel.dashes = panel.lines ? panel.dashes : false;
  200. // Populate element
  201. let options: any = buildFlotOptions(panel);
  202. prepareXAxis(options, panel);
  203. configureYAxisOptions(data, options);
  204. thresholdManager.addFlotOptions(options, panel);
  205. eventManager.addFlotEvents(annotations, options);
  206. sortedSeries = sortSeries(data, panel);
  207. callPlot(options, true);
  208. }
  209. function buildFlotPairs(data) {
  210. for (let i = 0; i < data.length; i++) {
  211. let series = data[i];
  212. series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode);
  213. // if hidden remove points and disable stack
  214. if (ctrl.hiddenSeries[series.alias]) {
  215. series.data = [];
  216. series.stack = false;
  217. }
  218. }
  219. }
  220. function prepareXAxis(options, panel) {
  221. switch (panel.xaxis.mode) {
  222. case 'series': {
  223. options.series.bars.barWidth = 0.7;
  224. options.series.bars.align = 'center';
  225. for (let i = 0; i < data.length; i++) {
  226. let series = data[i];
  227. series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]];
  228. }
  229. addXSeriesAxis(options);
  230. break;
  231. }
  232. case 'histogram': {
  233. let bucketSize: number;
  234. let values = getSeriesValues(data);
  235. if (data.length && values.length) {
  236. let histMin = _.min(_.map(data, s => s.stats.min));
  237. let histMax = _.max(_.map(data, s => s.stats.max));
  238. let ticks = panel.xaxis.buckets || panelWidth / 50;
  239. bucketSize = tickStep(histMin, histMax, ticks);
  240. let histogram = convertValuesToHistogram(values, bucketSize);
  241. data[0].data = histogram;
  242. options.series.bars.barWidth = bucketSize * 0.8;
  243. } else {
  244. bucketSize = 0;
  245. }
  246. addXHistogramAxis(options, bucketSize);
  247. break;
  248. }
  249. case 'table': {
  250. options.series.bars.barWidth = 0.7;
  251. options.series.bars.align = 'center';
  252. addXTableAxis(options);
  253. break;
  254. }
  255. default: {
  256. options.series.bars.barWidth = getMinTimeStepOfSeries(data) / 1.5;
  257. addTimeAxis(options);
  258. break;
  259. }
  260. }
  261. }
  262. function callPlot(options, incrementRenderCounter) {
  263. try {
  264. plot = $.plot(elem, sortedSeries, options);
  265. if (ctrl.renderError) {
  266. delete ctrl.error;
  267. delete ctrl.inspector;
  268. }
  269. } catch (e) {
  270. console.log('flotcharts error', e);
  271. ctrl.error = e.message || "Render Error";
  272. ctrl.renderError = true;
  273. ctrl.inspector = {error: e};
  274. }
  275. if (incrementRenderCounter) {
  276. ctrl.renderingCompleted();
  277. }
  278. }
  279. function buildFlotOptions(panel) {
  280. const stack = panel.stack ? true : null;
  281. let options = {
  282. hooks: {
  283. draw: [drawHook],
  284. processOffset: [processOffsetHook],
  285. },
  286. legend: { show: false },
  287. series: {
  288. stackpercent: panel.stack ? panel.percentage : false,
  289. stack: panel.percentage ? null : stack,
  290. lines: {
  291. show: panel.lines,
  292. zero: false,
  293. fill: translateFillOption(panel.fill),
  294. lineWidth: panel.dashes ? 0 : panel.linewidth,
  295. steps: panel.steppedLine
  296. },
  297. dashes: {
  298. show: panel.dashes,
  299. lineWidth: panel.linewidth,
  300. dashLength: [panel.dashLength, panel.spaceLength]
  301. },
  302. bars: {
  303. show: panel.bars,
  304. fill: 1,
  305. barWidth: 1,
  306. zero: false,
  307. lineWidth: 0
  308. },
  309. points: {
  310. show: panel.points,
  311. fill: 1,
  312. fillColor: false,
  313. radius: panel.points ? panel.pointradius : 2
  314. },
  315. shadowSize: 0
  316. },
  317. yaxes: [],
  318. xaxis: {},
  319. grid: {
  320. minBorderMargin: 0,
  321. markings: [],
  322. backgroundColor: null,
  323. borderWidth: 0,
  324. hoverable: true,
  325. clickable: true,
  326. color: '#c8c8c8',
  327. margin: { left: 0, right: 0 },
  328. labelMarginX: 0,
  329. },
  330. selection: {
  331. mode: "x",
  332. color: '#666'
  333. },
  334. crosshair: {
  335. mode: 'x'
  336. }
  337. };
  338. return options;
  339. }
  340. function sortSeries(series, panel) {
  341. var sortBy = panel.legend.sort;
  342. var sortOrder = panel.legend.sortDesc;
  343. var haveSortBy = sortBy !== null || sortBy !== undefined;
  344. var haveSortOrder = sortOrder !== null || sortOrder !== undefined;
  345. var shouldSortBy = panel.stack && haveSortBy && haveSortOrder;
  346. var sortDesc = panel.legend.sortDesc === true ? -1 : 1;
  347. series.sort((x, y) => {
  348. if (x.zindex > y.zindex) {
  349. return 1;
  350. }
  351. if (x.zindex < y.zindex) {
  352. return -1;
  353. }
  354. if (shouldSortBy) {
  355. if (x.stats[sortBy] > y.stats[sortBy]) {
  356. return 1 * sortDesc;
  357. }
  358. if (x.stats[sortBy] < y.stats[sortBy]) {
  359. return -1 * sortDesc;
  360. }
  361. }
  362. return 0;
  363. });
  364. return series;
  365. }
  366. function translateFillOption(fill) {
  367. if (panel.percentage && panel.stack) {
  368. return fill === 0 ? 0.001 : fill/10;
  369. } else {
  370. return fill/10;
  371. }
  372. }
  373. function addTimeAxis(options) {
  374. var ticks = panelWidth / 100;
  375. var min = _.isUndefined(ctrl.range.from) ? null : ctrl.range.from.valueOf();
  376. var max = _.isUndefined(ctrl.range.to) ? null : ctrl.range.to.valueOf();
  377. options.xaxis = {
  378. timezone: dashboard.getTimezone(),
  379. show: panel.xaxis.show,
  380. mode: "time",
  381. min: min,
  382. max: max,
  383. label: "Datetime",
  384. ticks: ticks,
  385. timeformat: time_format(ticks, min, max),
  386. };
  387. }
  388. function addXSeriesAxis(options) {
  389. var ticks = _.map(data, function(series, index) {
  390. return [index + 1, series.alias];
  391. });
  392. options.xaxis = {
  393. timezone: dashboard.getTimezone(),
  394. show: panel.xaxis.show,
  395. mode: null,
  396. min: 0,
  397. max: ticks.length + 1,
  398. label: "Datetime",
  399. ticks: ticks
  400. };
  401. }
  402. function addXHistogramAxis(options, bucketSize) {
  403. let ticks, min, max;
  404. let defaultTicks = panelWidth / 50;
  405. if (data.length && bucketSize) {
  406. ticks = _.map(data[0].data, point => point[0]);
  407. min = _.min(ticks);
  408. max = _.max(ticks);
  409. // Adjust tick step
  410. let tickStep = bucketSize;
  411. let ticks_num = Math.floor((max - min) / tickStep);
  412. while (ticks_num > defaultTicks) {
  413. tickStep = tickStep * 2;
  414. ticks_num = Math.ceil((max - min) / tickStep);
  415. }
  416. // Expand ticks for pretty view
  417. min = Math.floor(min / tickStep) * tickStep;
  418. max = Math.ceil(max / tickStep) * tickStep;
  419. ticks = [];
  420. for (let i = min; i <= max; i += tickStep) {
  421. ticks.push(i);
  422. }
  423. } else {
  424. // Set defaults if no data
  425. ticks = defaultTicks / 2;
  426. min = 0;
  427. max = 1;
  428. }
  429. options.xaxis = {
  430. timezone: dashboard.getTimezone(),
  431. show: panel.xaxis.show,
  432. mode: null,
  433. min: min,
  434. max: max,
  435. label: "Histogram",
  436. ticks: ticks
  437. };
  438. // Use 'short' format for histogram values
  439. configureAxisMode(options.xaxis, 'short');
  440. }
  441. function addXTableAxis(options) {
  442. var ticks = _.map(data, function(series, seriesIndex) {
  443. return _.map(series.datapoints, function(point, pointIndex) {
  444. var tickIndex = seriesIndex * series.datapoints.length + pointIndex;
  445. return [tickIndex + 1, point[1]];
  446. });
  447. });
  448. ticks = _.flatten(ticks, true);
  449. options.xaxis = {
  450. timezone: dashboard.getTimezone(),
  451. show: panel.xaxis.show,
  452. mode: null,
  453. min: 0,
  454. max: ticks.length + 1,
  455. label: "Datetime",
  456. ticks: ticks
  457. };
  458. }
  459. function configureYAxisOptions(data, options) {
  460. var defaults = {
  461. position: 'left',
  462. show: panel.yaxes[0].show,
  463. index: 1,
  464. logBase: panel.yaxes[0].logBase || 1,
  465. min: parseNumber(panel.yaxes[0].min),
  466. max: parseNumber(panel.yaxes[0].max),
  467. tickDecimals: panel.yaxes[0].decimals
  468. };
  469. options.yaxes.push(defaults);
  470. if (_.find(data, {yaxis: 2})) {
  471. var secondY = _.clone(defaults);
  472. secondY.index = 2;
  473. secondY.show = panel.yaxes[1].show;
  474. secondY.logBase = panel.yaxes[1].logBase || 1;
  475. secondY.position = 'right';
  476. secondY.min = parseNumber(panel.yaxes[1].min);
  477. secondY.max = parseNumber(panel.yaxes[1].max);
  478. secondY.tickDecimals = panel.yaxes[1].decimals;
  479. options.yaxes.push(secondY);
  480. applyLogScale(options.yaxes[1], data);
  481. configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format);
  482. }
  483. applyLogScale(options.yaxes[0], data);
  484. configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format);
  485. }
  486. function parseNumber(value: any) {
  487. if (value === null || typeof value === 'undefined') {
  488. return null;
  489. }
  490. return _.toNumber(value);
  491. }
  492. function applyLogScale(axis, data) {
  493. if (axis.logBase === 1) {
  494. return;
  495. }
  496. const minSetToZero = axis.min === 0;
  497. if (axis.min < Number.MIN_VALUE) {
  498. axis.min = null;
  499. }
  500. if (axis.max < Number.MIN_VALUE) {
  501. axis.max = null;
  502. }
  503. var series, i;
  504. var max = axis.max, min = axis.min;
  505. for (i = 0; i < data.length; i++) {
  506. series = data[i];
  507. if (series.yaxis === axis.index) {
  508. if (!max || max < series.stats.max) {
  509. max = series.stats.max;
  510. }
  511. if (!min || min > series.stats.logmin) {
  512. min = series.stats.logmin;
  513. }
  514. }
  515. }
  516. axis.transform = function(v) { return (v < Number.MIN_VALUE) ? null : Math.log(v) / Math.log(axis.logBase); };
  517. axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); };
  518. if (!max && !min) {
  519. max = axis.inverseTransform(+2);
  520. min = axis.inverseTransform(-2);
  521. } else if (!max) {
  522. max = min*axis.inverseTransform(+4);
  523. } else if (!min) {
  524. min = max*axis.inverseTransform(-4);
  525. }
  526. if (axis.min) {
  527. min = axis.inverseTransform(Math.ceil(axis.transform(axis.min)));
  528. } else {
  529. min = axis.min = axis.inverseTransform(Math.floor(axis.transform(min)));
  530. }
  531. if (axis.max) {
  532. max = axis.inverseTransform(Math.floor(axis.transform(axis.max)));
  533. } else {
  534. max = axis.max = axis.inverseTransform(Math.ceil(axis.transform(max)));
  535. }
  536. if (!min || min < Number.MIN_VALUE || !max || max < Number.MIN_VALUE) {
  537. return;
  538. }
  539. if (Number.isFinite(min) && Number.isFinite(max)) {
  540. if (minSetToZero) {
  541. axis.min = 0.1;
  542. min = 1;
  543. }
  544. axis.ticks = generateTicksForLogScaleYAxis(min, max, axis.logBase);
  545. if (minSetToZero) {
  546. axis.ticks.unshift(0.1);
  547. }
  548. if (axis.ticks[axis.ticks.length - 1] > axis.max) {
  549. axis.max = axis.ticks[axis.ticks.length - 1];
  550. }
  551. } else {
  552. axis.ticks = [1, 2];
  553. delete axis.min;
  554. delete axis.max;
  555. }
  556. }
  557. function generateTicksForLogScaleYAxis(min, max, logBase) {
  558. let ticks = [];
  559. var nextTick;
  560. for (nextTick = min; nextTick <= max; nextTick *= logBase) {
  561. ticks.push(nextTick);
  562. }
  563. const maxNumTicks = Math.ceil(ctrl.height/25);
  564. const numTicks = ticks.length;
  565. if (numTicks > maxNumTicks) {
  566. const factor = Math.ceil(numTicks/maxNumTicks) * logBase;
  567. ticks = [];
  568. for (nextTick = min; nextTick <= (max * factor); nextTick *= factor) {
  569. ticks.push(nextTick);
  570. }
  571. }
  572. return ticks;
  573. }
  574. function configureAxisMode(axis, format) {
  575. axis.tickFormatter = function(val, axis) {
  576. return kbn.valueFormats[format](val, axis.tickDecimals, axis.scaledDecimals);
  577. };
  578. }
  579. function time_format(ticks, min, max) {
  580. if (min && max && ticks) {
  581. var range = max - min;
  582. var secPerTick = (range/ticks) / 1000;
  583. var oneDay = 86400000;
  584. var oneYear = 31536000000;
  585. if (secPerTick <= 45) {
  586. return "%H:%M:%S";
  587. }
  588. if (secPerTick <= 7200 || range <= oneDay) {
  589. return "%H:%M";
  590. }
  591. if (secPerTick <= 80000) {
  592. return "%m/%d %H:%M";
  593. }
  594. if (secPerTick <= 2419200 || range <= oneYear) {
  595. return "%m/%d";
  596. }
  597. return "%Y-%m";
  598. }
  599. return "%H:%M";
  600. }
  601. elem.bind("plotselected", function (event, ranges) {
  602. if (panel.xaxis.mode !== 'time') {
  603. // Skip if panel in histogram or series mode
  604. plot.clearSelection();
  605. return;
  606. }
  607. if ((ranges.ctrlKey || ranges.metaKey) && contextSrv.isEditor) {
  608. // Add annotation
  609. setTimeout(() => {
  610. eventManager.updateTime(ranges.xaxis);
  611. }, 100);
  612. } else {
  613. scope.$apply(function() {
  614. timeSrv.setTime({
  615. from : moment.utc(ranges.xaxis.from),
  616. to : moment.utc(ranges.xaxis.to),
  617. });
  618. });
  619. }
  620. });
  621. elem.bind("plotclick", function (event, pos, item) {
  622. if (panel.xaxis.mode !== 'time') {
  623. // Skip if panel in histogram or series mode
  624. return;
  625. }
  626. if ((pos.ctrlKey || pos.metaKey) && contextSrv.isEditor) {
  627. // Skip if range selected (added in "plotselected" event handler)
  628. let isRangeSelection = pos.x !== pos.x1;
  629. if (!isRangeSelection) {
  630. setTimeout(() => {
  631. eventManager.updateTime({from: pos.x, to: null});
  632. }, 100);
  633. }
  634. }
  635. });
  636. scope.$on('$destroy', function() {
  637. tooltip.destroy();
  638. elem.off();
  639. elem.remove();
  640. });
  641. }
  642. };
  643. }
  644. coreModule.directive('grafanaGraph', graphDirective);