timeSeries.js 3.9 KB

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