module.ts 21 KB

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