module.ts 20 KB

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