module.ts 21 KB

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