module.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import 'vendor/flot/jquery.flot';
  4. import 'vendor/flot/jquery.flot.gauge';
  5. import 'app/features/panel/panellinks/link_srv';
  6. import kbn from 'app/core/utils/kbn';
  7. import config from 'app/core/config';
  8. import TimeSeries from 'app/core/time_series2';
  9. import { MetricsPanelCtrl } from 'app/plugins/sdk';
  10. import { GrafanaThemeType, getValueFormat, getColorFromHexRgbOrName } from '@grafana/ui';
  11. class SingleStatCtrl extends MetricsPanelCtrl {
  12. static templateUrl = 'module.html';
  13. dataType = 'timeseries';
  14. series: any[];
  15. data: any;
  16. fontSizes: any[];
  17. unitFormats: any[];
  18. invalidGaugeRange: boolean;
  19. panel: any;
  20. events: any;
  21. valueNameOptions: any[] = [
  22. { value: 'min', text: 'Min' },
  23. { value: 'max', text: 'Max' },
  24. { value: 'avg', text: 'Average' },
  25. { value: 'current', text: 'Current' },
  26. { value: 'total', text: 'Total' },
  27. { value: 'name', text: 'Name' },
  28. { value: 'first', text: 'First' },
  29. { value: 'delta', text: 'Delta' },
  30. { value: 'diff', text: 'Difference' },
  31. { value: 'range', text: 'Range' },
  32. { value: 'last_time', text: 'Time of last point' },
  33. ];
  34. tableColumnOptions: any;
  35. // Set and populate defaults
  36. panelDefaults = {
  37. links: [],
  38. datasource: null,
  39. maxDataPoints: 100,
  40. interval: null,
  41. targets: [{}],
  42. cacheTimeout: null,
  43. format: 'none',
  44. prefix: '',
  45. postfix: '',
  46. nullText: null,
  47. valueMaps: [{ value: 'null', op: '=', text: 'N/A' }],
  48. mappingTypes: [{ name: 'value to text', value: 1 }, { name: 'range to text', value: 2 }],
  49. rangeMaps: [{ from: 'null', to: 'null', text: 'N/A' }],
  50. mappingType: 1,
  51. nullPointMode: 'connected',
  52. valueName: 'avg',
  53. prefixFontSize: '50%',
  54. valueFontSize: '80%',
  55. postfixFontSize: '50%',
  56. thresholds: '',
  57. colorBackground: false,
  58. colorValue: false,
  59. colors: ['#299c46', 'rgba(237, 129, 40, 0.89)', '#d44a3a'],
  60. sparkline: {
  61. show: false,
  62. full: false,
  63. lineColor: 'rgb(31, 120, 193)',
  64. fillColor: 'rgba(31, 118, 189, 0.18)',
  65. },
  66. gauge: {
  67. show: false,
  68. minValue: 0,
  69. maxValue: 100,
  70. thresholdMarkers: true,
  71. thresholdLabels: false,
  72. },
  73. tableColumn: '',
  74. };
  75. /** @ngInject */
  76. constructor($scope, $injector, private linkSrv, private $sanitize) {
  77. super($scope, $injector);
  78. _.defaults(this.panel, this.panelDefaults);
  79. this.events.on('data-received', this.onDataReceived.bind(this));
  80. this.events.on('data-error', this.onDataError.bind(this));
  81. this.events.on('data-snapshot-load', this.onDataReceived.bind(this));
  82. this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
  83. this.onSparklineColorChange = this.onSparklineColorChange.bind(this);
  84. this.onSparklineFillChange = this.onSparklineFillChange.bind(this);
  85. }
  86. onInitEditMode() {
  87. this.fontSizes = ['20%', '30%', '50%', '70%', '80%', '100%', '110%', '120%', '150%', '170%', '200%'];
  88. this.addEditorTab('Options', 'public/app/plugins/panel/singlestat/editor.html', 2);
  89. this.addEditorTab('Value Mappings', 'public/app/plugins/panel/singlestat/mappings.html', 3);
  90. this.unitFormats = kbn.getUnitFormats();
  91. }
  92. setUnitFormat(subItem) {
  93. this.panel.format = subItem.value;
  94. this.refresh();
  95. }
  96. onDataError(err) {
  97. this.onDataReceived([]);
  98. }
  99. onDataReceived(dataList) {
  100. const data: any = {
  101. scopedVars: _.extend({}, this.panel.scopedVars),
  102. };
  103. if (dataList.length > 0 && dataList[0].type === 'table') {
  104. this.dataType = 'table';
  105. const tableData = dataList.map(this.tableHandler.bind(this));
  106. this.setTableValues(tableData, data);
  107. } else {
  108. this.dataType = 'timeseries';
  109. this.series = dataList.map(this.seriesHandler.bind(this));
  110. this.setValues(data);
  111. }
  112. this.data = data;
  113. this.render();
  114. }
  115. seriesHandler(seriesData) {
  116. const series = new TimeSeries({
  117. datapoints: seriesData.datapoints || [],
  118. alias: seriesData.target,
  119. });
  120. series.flotpairs = series.getFlotPairs(this.panel.nullPointMode);
  121. return series;
  122. }
  123. tableHandler(tableData) {
  124. const datapoints = [];
  125. const columnNames = {};
  126. tableData.columns.forEach((column, columnIndex) => {
  127. columnNames[columnIndex] = column.text;
  128. });
  129. this.tableColumnOptions = columnNames;
  130. if (!_.find(tableData.columns, ['text', this.panel.tableColumn])) {
  131. this.setTableColumnToSensibleDefault(tableData);
  132. }
  133. tableData.rows.forEach(row => {
  134. const datapoint = {};
  135. row.forEach((value, columnIndex) => {
  136. const key = columnNames[columnIndex];
  137. datapoint[key] = value;
  138. });
  139. datapoints.push(datapoint);
  140. });
  141. return datapoints;
  142. }
  143. setTableColumnToSensibleDefault(tableData) {
  144. if (tableData.columns.length === 1) {
  145. this.panel.tableColumn = tableData.columns[0].text;
  146. } else {
  147. this.panel.tableColumn = _.find(tableData.columns, col => {
  148. return col.type !== 'time';
  149. }).text;
  150. }
  151. }
  152. setTableValues(tableData, data) {
  153. if (!tableData || tableData.length === 0) {
  154. return;
  155. }
  156. if (tableData[0].length === 0 || tableData[0][0][this.panel.tableColumn] === undefined) {
  157. return;
  158. }
  159. const datapoint = tableData[0][0];
  160. data.value = datapoint[this.panel.tableColumn];
  161. if (_.isString(data.value)) {
  162. data.valueFormatted = _.escape(data.value);
  163. data.value = 0;
  164. data.valueRounded = 0;
  165. } else {
  166. const decimalInfo = this.getDecimalsForValue(data.value);
  167. const formatFunc = getValueFormat(this.panel.format);
  168. data.valueFormatted = formatFunc(
  169. datapoint[this.panel.tableColumn],
  170. decimalInfo.decimals,
  171. decimalInfo.scaledDecimals
  172. );
  173. data.valueRounded = kbn.roundValue(data.value, this.panel.decimals || 0);
  174. }
  175. this.setValueMapping(data);
  176. }
  177. canModifyText() {
  178. return !this.panel.gauge.show;
  179. }
  180. setColoring(options) {
  181. if (options.background) {
  182. this.panel.colorValue = false;
  183. this.panel.colors = ['rgba(71, 212, 59, 0.4)', 'rgba(245, 150, 40, 0.73)', 'rgba(225, 40, 40, 0.59)'];
  184. } else {
  185. this.panel.colorBackground = false;
  186. this.panel.colors = ['rgba(50, 172, 45, 0.97)', 'rgba(237, 129, 40, 0.89)', 'rgba(245, 54, 54, 0.9)'];
  187. }
  188. this.render();
  189. }
  190. invertColorOrder() {
  191. const tmp = this.panel.colors[0];
  192. this.panel.colors[0] = this.panel.colors[2];
  193. this.panel.colors[2] = tmp;
  194. this.render();
  195. }
  196. onColorChange(panelColorIndex) {
  197. return color => {
  198. this.panel.colors[panelColorIndex] = color;
  199. this.render();
  200. };
  201. }
  202. onSparklineColorChange(newColor) {
  203. this.panel.sparkline.lineColor = newColor;
  204. this.render();
  205. }
  206. onSparklineFillChange(newColor) {
  207. this.panel.sparkline.fillColor = newColor;
  208. this.render();
  209. }
  210. getDecimalsForValue(value) {
  211. if (_.isNumber(this.panel.decimals)) {
  212. return { decimals: this.panel.decimals, scaledDecimals: null };
  213. }
  214. const delta = value / 2;
  215. let dec = -Math.floor(Math.log(delta) / Math.LN10);
  216. const magn = Math.pow(10, -dec);
  217. const norm = delta / magn; // norm is between 1.0 and 10.0
  218. let size;
  219. if (norm < 1.5) {
  220. size = 1;
  221. } else if (norm < 3) {
  222. size = 2;
  223. // special case for 2.5, requires an extra decimal
  224. if (norm > 2.25) {
  225. size = 2.5;
  226. ++dec;
  227. }
  228. } else if (norm < 7.5) {
  229. size = 5;
  230. } else {
  231. size = 10;
  232. }
  233. size *= magn;
  234. // reduce starting decimals if not needed
  235. if (Math.floor(value) === value) {
  236. dec = 0;
  237. }
  238. const result: any = {};
  239. result.decimals = Math.max(0, dec);
  240. result.scaledDecimals = result.decimals - Math.floor(Math.log(size) / Math.LN10) + 2;
  241. return result;
  242. }
  243. setValues(data) {
  244. data.flotpairs = [];
  245. if (this.series.length > 1) {
  246. const error: any = new Error();
  247. error.message = 'Multiple Series Error';
  248. error.data =
  249. 'Metric query returns ' +
  250. this.series.length +
  251. ' series. Single Stat Panel expects a single series.\n\nResponse:\n' +
  252. JSON.stringify(this.series);
  253. throw error;
  254. }
  255. if (this.series && this.series.length > 0) {
  256. const lastPoint = _.last(this.series[0].datapoints);
  257. const lastValue = _.isArray(lastPoint) ? lastPoint[0] : null;
  258. const formatFunc = getValueFormat(this.panel.format);
  259. if (this.panel.valueName === 'name') {
  260. data.value = 0;
  261. data.valueRounded = 0;
  262. data.valueFormatted = this.series[0].alias;
  263. } else if (_.isString(lastValue)) {
  264. data.value = 0;
  265. data.valueFormatted = _.escape(lastValue);
  266. data.valueRounded = 0;
  267. } else if (this.panel.valueName === 'last_time') {
  268. data.value = lastPoint[1];
  269. data.valueRounded = data.value;
  270. data.valueFormatted = formatFunc(data.value, 0, 0, this.dashboard.isTimezoneUtc());
  271. } else {
  272. data.value = this.series[0].stats[this.panel.valueName];
  273. data.flotpairs = this.series[0].flotpairs;
  274. const decimalInfo = this.getDecimalsForValue(data.value);
  275. data.valueFormatted = formatFunc(
  276. data.value,
  277. decimalInfo.decimals,
  278. decimalInfo.scaledDecimals,
  279. this.dashboard.isTimezoneUtc()
  280. );
  281. data.valueRounded = kbn.roundValue(data.value, decimalInfo.decimals);
  282. }
  283. // Add $__name variable for using in prefix or postfix
  284. data.scopedVars['__name'] = { value: this.series[0].label };
  285. }
  286. this.setValueMapping(data);
  287. }
  288. setValueMapping(data) {
  289. // check value to text mappings if its enabled
  290. if (this.panel.mappingType === 1) {
  291. for (let i = 0; i < this.panel.valueMaps.length; i++) {
  292. const map = this.panel.valueMaps[i];
  293. // special null case
  294. if (map.value === 'null') {
  295. if (data.value === null || data.value === void 0) {
  296. data.valueFormatted = map.text;
  297. return;
  298. }
  299. continue;
  300. }
  301. // value/number to text mapping
  302. const value = parseFloat(map.value);
  303. if (value === data.valueRounded) {
  304. data.valueFormatted = map.text;
  305. return;
  306. }
  307. }
  308. } else if (this.panel.mappingType === 2) {
  309. for (let i = 0; i < this.panel.rangeMaps.length; i++) {
  310. const map = this.panel.rangeMaps[i];
  311. // special null case
  312. if (map.from === 'null' && map.to === 'null') {
  313. if (data.value === null || data.value === void 0) {
  314. data.valueFormatted = map.text;
  315. return;
  316. }
  317. continue;
  318. }
  319. // value/number to range mapping
  320. const from = parseFloat(map.from);
  321. const to = parseFloat(map.to);
  322. if (to >= data.valueRounded && from <= data.valueRounded) {
  323. data.valueFormatted = map.text;
  324. return;
  325. }
  326. }
  327. }
  328. if (data.value === null || data.value === void 0) {
  329. data.valueFormatted = 'no value';
  330. }
  331. }
  332. removeValueMap(map) {
  333. const index = _.indexOf(this.panel.valueMaps, map);
  334. this.panel.valueMaps.splice(index, 1);
  335. this.render();
  336. }
  337. addValueMap() {
  338. this.panel.valueMaps.push({ value: '', op: '=', text: '' });
  339. }
  340. removeRangeMap(rangeMap) {
  341. const index = _.indexOf(this.panel.rangeMaps, rangeMap);
  342. this.panel.rangeMaps.splice(index, 1);
  343. this.render();
  344. }
  345. addRangeMap() {
  346. this.panel.rangeMaps.push({ from: '', to: '', text: '' });
  347. }
  348. link(scope, elem, attrs, ctrl) {
  349. const $location = this.$location;
  350. const linkSrv = this.linkSrv;
  351. const $timeout = this.$timeout;
  352. const $sanitize = this.$sanitize;
  353. const panel = ctrl.panel;
  354. const templateSrv = this.templateSrv;
  355. let data, linkInfo;
  356. const $panelContainer = elem.find('.panel-container');
  357. elem = elem.find('.singlestat-panel');
  358. function applyColoringThresholds(valueString) {
  359. const color = getColorForValue(data, data.value);
  360. if (color) {
  361. return '<span style="color:' + color + '">' + valueString + '</span>';
  362. }
  363. return valueString;
  364. }
  365. function getSpan(className, fontSize, applyColoring, value) {
  366. value = $sanitize(templateSrv.replace(value, data.scopedVars));
  367. value = applyColoring ? applyColoringThresholds(value) : value;
  368. return '<span class="' + className + '" style="font-size:' + fontSize + '">' + value + '</span>';
  369. }
  370. function getBigValueHtml() {
  371. let body = '<div class="singlestat-panel-value-container">';
  372. if (panel.prefix) {
  373. body += getSpan('singlestat-panel-prefix', panel.prefixFontSize, panel.colorPrefix, panel.prefix);
  374. }
  375. body += getSpan('singlestat-panel-value', panel.valueFontSize, panel.colorValue, data.valueFormatted);
  376. if (panel.postfix) {
  377. body += getSpan('singlestat-panel-postfix', panel.postfixFontSize, panel.colorPostfix, panel.postfix);
  378. }
  379. body += '</div>';
  380. return body;
  381. }
  382. function getValueText() {
  383. let result = panel.prefix ? templateSrv.replace(panel.prefix, data.scopedVars) : '';
  384. result += data.valueFormatted;
  385. result += panel.postfix ? templateSrv.replace(panel.postfix, data.scopedVars) : '';
  386. return result;
  387. }
  388. function addGauge() {
  389. const width = elem.width();
  390. const height = elem.height();
  391. // Allow to use a bit more space for wide gauges
  392. const dimension = Math.min(width, height * 1.3);
  393. ctrl.invalidGaugeRange = false;
  394. if (panel.gauge.minValue > panel.gauge.maxValue) {
  395. ctrl.invalidGaugeRange = true;
  396. return;
  397. }
  398. const plotCanvas = $('<div></div>');
  399. const plotCss = {
  400. top: '10px',
  401. margin: 'auto',
  402. position: 'relative',
  403. height: height * 0.9 + 'px',
  404. width: dimension + 'px',
  405. };
  406. plotCanvas.css(plotCss);
  407. const thresholds = [];
  408. for (let i = 0; i < data.thresholds.length; i++) {
  409. thresholds.push({
  410. value: data.thresholds[i],
  411. color: data.colorMap[i],
  412. });
  413. }
  414. thresholds.push({
  415. value: panel.gauge.maxValue,
  416. color: data.colorMap[data.colorMap.length - 1],
  417. });
  418. const bgColor = config.bootData.user.lightTheme ? 'rgb(230,230,230)' : 'rgb(38,38,38)';
  419. const fontScale = parseInt(panel.valueFontSize, 10) / 100;
  420. const fontSize = Math.min(dimension / 5, 100) * fontScale;
  421. // Reduce gauge width if threshold labels enabled
  422. const gaugeWidthReduceRatio = panel.gauge.thresholdLabels ? 1.5 : 1;
  423. const gaugeWidth = Math.min(dimension / 6, 60) / gaugeWidthReduceRatio;
  424. const thresholdMarkersWidth = gaugeWidth / 5;
  425. const thresholdLabelFontSize = fontSize / 2.5;
  426. const options = {
  427. series: {
  428. gauges: {
  429. gauge: {
  430. min: panel.gauge.minValue,
  431. max: panel.gauge.maxValue,
  432. background: { color: bgColor },
  433. border: { color: null },
  434. shadow: { show: false },
  435. width: gaugeWidth,
  436. },
  437. frame: { show: false },
  438. label: { show: false },
  439. layout: { margin: 0, thresholdWidth: 0 },
  440. cell: { border: { width: 0 } },
  441. threshold: {
  442. values: thresholds,
  443. label: {
  444. show: panel.gauge.thresholdLabels,
  445. margin: thresholdMarkersWidth + 1,
  446. font: { size: thresholdLabelFontSize },
  447. },
  448. show: panel.gauge.thresholdMarkers,
  449. width: thresholdMarkersWidth,
  450. },
  451. value: {
  452. color: panel.colorValue ? getColorForValue(data, data.valueRounded) : null,
  453. formatter: () => {
  454. return getValueText();
  455. },
  456. font: {
  457. size: fontSize,
  458. family: '"Helvetica Neue", Helvetica, Arial, sans-serif',
  459. },
  460. },
  461. show: true,
  462. },
  463. },
  464. };
  465. elem.append(plotCanvas);
  466. const plotSeries = {
  467. data: [[0, data.value]],
  468. };
  469. $.plot(plotCanvas, [plotSeries], options);
  470. }
  471. function addSparkline() {
  472. const width = elem.width() + 20;
  473. if (width < 30) {
  474. // element has not gotten it's width yet
  475. // delay sparkline render
  476. setTimeout(addSparkline, 30);
  477. return;
  478. }
  479. const height = ctrl.height;
  480. const plotCanvas = $('<div></div>');
  481. const plotCss: any = {};
  482. plotCss.position = 'absolute';
  483. if (panel.sparkline.full) {
  484. plotCss.bottom = '5px';
  485. plotCss.left = '-5px';
  486. plotCss.width = width - 10 + 'px';
  487. const dynamicHeightMargin = height <= 100 ? 5 : Math.round(height / 100) * 15 + 5;
  488. plotCss.height = height - dynamicHeightMargin + 'px';
  489. } else {
  490. plotCss.bottom = '0px';
  491. plotCss.left = '-5px';
  492. plotCss.width = width - 10 + 'px';
  493. plotCss.height = Math.floor(height * 0.25) + 'px';
  494. }
  495. plotCanvas.css(plotCss);
  496. const options = {
  497. legend: { show: false },
  498. series: {
  499. lines: {
  500. show: true,
  501. fill: 1,
  502. zero: false,
  503. lineWidth: 1,
  504. fillColor: getColorFromHexRgbOrName(panel.sparkline.fillColor, config.theme.type),
  505. },
  506. },
  507. yaxes: { show: false },
  508. xaxis: {
  509. show: false,
  510. mode: 'time',
  511. min: ctrl.range.from.valueOf(),
  512. max: ctrl.range.to.valueOf(),
  513. },
  514. grid: { hoverable: false, show: false },
  515. };
  516. elem.append(plotCanvas);
  517. const plotSeries = {
  518. data: data.flotpairs,
  519. color: getColorFromHexRgbOrName(panel.sparkline.lineColor, config.theme.type),
  520. };
  521. $.plot(plotCanvas, [plotSeries], options);
  522. }
  523. function render() {
  524. if (!ctrl.data) {
  525. return;
  526. }
  527. data = ctrl.data;
  528. // get thresholds
  529. data.thresholds = panel.thresholds.split(',').map(strVale => {
  530. return Number(strVale.trim());
  531. });
  532. // Map panel colors to hex or rgb/a values
  533. data.colorMap = panel.colors.map(color =>
  534. getColorFromHexRgbOrName(color, config.bootData.user.lightTheme ? GrafanaThemeType.Light : GrafanaThemeType.Dark)
  535. );
  536. const body = panel.gauge.show ? '' : getBigValueHtml();
  537. if (panel.colorBackground) {
  538. const color = getColorForValue(data, data.value);
  539. console.log(color);
  540. if (color) {
  541. $panelContainer.css('background-color', color);
  542. if (scope.fullscreen) {
  543. elem.css('background-color', color);
  544. } else {
  545. elem.css('background-color', '');
  546. }
  547. }
  548. } else {
  549. $panelContainer.css('background-color', '');
  550. elem.css('background-color', '');
  551. }
  552. elem.html(body);
  553. if (panel.sparkline.show) {
  554. addSparkline();
  555. }
  556. if (panel.gauge.show) {
  557. addGauge();
  558. }
  559. elem.toggleClass('pointer', panel.links.length > 0);
  560. if (panel.links.length > 0) {
  561. linkInfo = linkSrv.getPanelLinkAnchorInfo(panel.links[0], data.scopedVars);
  562. } else {
  563. linkInfo = null;
  564. }
  565. }
  566. function hookupDrilldownLinkTooltip() {
  567. // drilldown link tooltip
  568. const drilldownTooltip = $('<div id="tooltip" class="">hello</div>"');
  569. elem.mouseleave(() => {
  570. if (panel.links.length === 0) {
  571. return;
  572. }
  573. $timeout(() => {
  574. drilldownTooltip.detach();
  575. });
  576. });
  577. elem.click(evt => {
  578. if (!linkInfo) {
  579. return;
  580. }
  581. // ignore title clicks in title
  582. if ($(evt).parents('.panel-header').length > 0) {
  583. return;
  584. }
  585. if (linkInfo.target === '_blank') {
  586. window.open(linkInfo.href, '_blank');
  587. return;
  588. }
  589. if (linkInfo.href.indexOf('http') === 0) {
  590. window.location.href = linkInfo.href;
  591. } else {
  592. $timeout(() => {
  593. $location.url(linkInfo.href);
  594. });
  595. }
  596. drilldownTooltip.detach();
  597. });
  598. elem.mousemove(e => {
  599. if (!linkInfo) {
  600. return;
  601. }
  602. drilldownTooltip.text('click to go to: ' + linkInfo.title);
  603. drilldownTooltip.place_tt(e.pageX, e.pageY - 50);
  604. });
  605. }
  606. hookupDrilldownLinkTooltip();
  607. this.events.on('render', () => {
  608. render();
  609. ctrl.renderingCompleted();
  610. });
  611. }
  612. }
  613. function getColorForValue(data, value) {
  614. if (!_.isFinite(value)) {
  615. return null;
  616. }
  617. for (let i = data.thresholds.length; i > 0; i--) {
  618. if (value >= data.thresholds[i - 1]) {
  619. return data.colorMap[i];
  620. }
  621. }
  622. return _.first(data.colorMap);
  623. }
  624. export { SingleStatCtrl, SingleStatCtrl as PanelCtrl, getColorForValue };