module.ts 21 KB

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