timeSeries.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. define([
  2. 'lodash',
  3. 'kbn'
  4. ],
  5. function (_, kbn) {
  6. 'use strict';
  7. function TimeSeries(opts) {
  8. this.datapoints = opts.datapoints;
  9. this.label = opts.alias;
  10. this.id = opts.alias;
  11. this.alias = opts.alias;
  12. this.color = opts.color;
  13. this.valueFormater = kbn.valueFormats.none;
  14. this.stats = {};
  15. this.legend = true;
  16. }
  17. function matchSeriesOverride(aliasOrRegex, seriesAlias) {
  18. if (!aliasOrRegex) { return false; }
  19. if (aliasOrRegex[0] === '/') {
  20. var regex = kbn.stringToJsRegex(aliasOrRegex);
  21. return seriesAlias.match(regex) != null;
  22. }
  23. return aliasOrRegex === seriesAlias;
  24. }
  25. function translateFillOption(fill) {
  26. return fill === 0 ? 0.001 : fill/10;
  27. }
  28. TimeSeries.prototype.applySeriesOverrides = function(overrides) {
  29. this.lines = {};
  30. this.points = {};
  31. this.bars = {};
  32. this.yaxis = 1;
  33. this.zindex = 0;
  34. delete this.stack;
  35. for (var i = 0; i < overrides.length; i++) {
  36. var override = overrides[i];
  37. if (!matchSeriesOverride(override.alias, this.alias)) {
  38. continue;
  39. }
  40. if (override.lines !== void 0) { this.lines.show = override.lines; }
  41. if (override.points !== void 0) { this.points.show = override.points; }
  42. if (override.bars !== void 0) { this.bars.show = override.bars; }
  43. if (override.fill !== void 0) { this.lines.fill = translateFillOption(override.fill); }
  44. if (override.stack !== void 0) { this.stack = override.stack; }
  45. if (override.linewidth !== void 0) { this.lines.lineWidth = override.linewidth; }
  46. if (override.pointradius !== void 0) { this.points.radius = override.pointradius; }
  47. if (override.steppedLine !== void 0) { this.lines.steps = override.steppedLine; }
  48. if (override.zindex !== void 0) { this.zindex = override.zindex; }
  49. if (override.fillBelowTo !== void 0) { this.fillBelowTo = override.fillBelowTo; }
  50. if (override.color !== void 0) { this.color = override.color; }
  51. if (override.transform !== void 0) { this.transform = override.transform; }
  52. if (override.legend !== void 0) { this.legend = override.legend; }
  53. if (override.yaxis !== void 0) {
  54. this.yaxis = override.yaxis;
  55. }
  56. }
  57. };
  58. TimeSeries.prototype.getFlotPairs = function (fillStyle) {
  59. var result = [];
  60. this.stats.total = 0;
  61. this.stats.max = -Number.MAX_VALUE;
  62. this.stats.min = Number.MAX_VALUE;
  63. this.stats.avg = null;
  64. this.stats.current = null;
  65. this.allIsNull = true;
  66. var ignoreNulls = fillStyle === 'connected';
  67. var nullAsZero = fillStyle === 'null as zero';
  68. var currentTime;
  69. var currentValue;
  70. for (var i = 0; i < this.datapoints.length; i++) {
  71. currentValue = this.datapoints[i][0];
  72. currentTime = this.datapoints[i][1];
  73. if (currentValue === null) {
  74. if (ignoreNulls) { continue; }
  75. if (nullAsZero) {
  76. currentValue = 0;
  77. }
  78. }
  79. if (currentValue !== null) {
  80. if (_.isNumber(currentValue)) {
  81. this.stats.total += currentValue;
  82. this.allIsNull = false;
  83. }
  84. if (currentValue > this.stats.max) {
  85. this.stats.max = currentValue;
  86. }
  87. if (currentValue < this.stats.min) {
  88. this.stats.min = currentValue;
  89. }
  90. }
  91. result.push([currentTime, currentValue]);
  92. }
  93. if (this.datapoints.length >= 2) {
  94. this.stats.timeStep = this.datapoints[1][1] - this.datapoints[0][1];
  95. }
  96. if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; }
  97. if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; }
  98. if (result.length) {
  99. this.stats.avg = (this.stats.total / result.length);
  100. this.stats.current = result[result.length-1][1];
  101. if (this.stats.current === null && result.length > 1) {
  102. this.stats.current = result[result.length-2][1];
  103. }
  104. }
  105. return result;
  106. };
  107. TimeSeries.prototype.updateLegendValues = function(formater, decimals, scaledDecimals) {
  108. this.valueFormater = formater;
  109. this.decimals = decimals;
  110. this.scaledDecimals = scaledDecimals;
  111. };
  112. TimeSeries.prototype.formatValue = function(value) {
  113. return this.valueFormater(value, this.decimals, this.scaledDecimals);
  114. };
  115. return TimeSeries;
  116. });