interval_translator.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * DevExtreme (viz/translators/interval_translator.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var typeUtils = require("../../core/utils/type");
  11. var isNumber = typeUtils.isNumeric;
  12. var isDefined = typeUtils.isDefined;
  13. var dateUtils = require("../../core/utils/date");
  14. var addInterval = dateUtils.addInterval;
  15. var dateToMilliseconds = dateUtils.dateToMilliseconds;
  16. var floor = Math.floor;
  17. var adjust = require("../../core/utils/math").adjust;
  18. module.exports = {
  19. _intervalize: function(value, interval) {
  20. if (!isDefined(value)) {
  21. return
  22. }
  23. if ("datetime" === this._businessRange.dataType) {
  24. if (isNumber(value)) {
  25. value = new Date(value)
  26. } else {
  27. value = new Date(value.getTime())
  28. }
  29. value = dateUtils.correctDateWithUnitBeginning(value, interval)
  30. } else {
  31. value = adjust(floor(adjust(value / interval)) * interval, interval)
  32. }
  33. return value
  34. },
  35. translate: function(bp, direction, interval) {
  36. var that = this;
  37. var specialValue = that.translateSpecialCase(bp);
  38. if (isDefined(specialValue)) {
  39. return Math.round(specialValue)
  40. }
  41. interval = interval || that._options.interval;
  42. if (!that.isValid(bp, interval)) {
  43. return null
  44. }
  45. return that.to(bp, direction, interval)
  46. },
  47. getInterval: function() {
  48. return Math.round(this._canvasOptions.ratioOfCanvasRange * (this._businessRange.interval || Math.abs(this._canvasOptions.rangeMax - this._canvasOptions.rangeMin)))
  49. },
  50. zoom: function() {},
  51. getMinScale: function() {},
  52. getScale: function() {},
  53. _parse: function(value) {
  54. return "datetime" === this._businessRange.dataType ? new Date(value) : Number(value)
  55. },
  56. _fromValue: function(value) {
  57. return this._parse(value)
  58. },
  59. _toValue: function(value) {
  60. return this._parse(value)
  61. },
  62. isValid: function(value, interval) {
  63. var that = this;
  64. var co = that._canvasOptions;
  65. var rangeMin = co.rangeMin;
  66. var rangeMax = co.rangeMax;
  67. interval = interval || that._options.interval;
  68. if (null === value || isNaN(value)) {
  69. return false
  70. }
  71. value = "datetime" === that._businessRange.dataType && isNumber(value) ? new Date(value) : value;
  72. if (interval !== that._options.interval) {
  73. rangeMin = that._intervalize(rangeMin, interval);
  74. rangeMax = that._intervalize(rangeMax, interval)
  75. }
  76. if (value.valueOf() < rangeMin || value.valueOf() >= addInterval(rangeMax, interval)) {
  77. return false
  78. }
  79. return true
  80. },
  81. to: function(bp, direction, interval) {
  82. var that = this;
  83. interval = interval || that._options.interval;
  84. var v1 = that._intervalize(bp, interval);
  85. var v2 = addInterval(v1, interval);
  86. var res = that._to(v1);
  87. var p2 = that._to(v2);
  88. if (!direction) {
  89. res = floor((res + p2) / 2)
  90. } else {
  91. if (direction > 0) {
  92. res = p2
  93. }
  94. }
  95. return res
  96. },
  97. _to: function(value) {
  98. var co = this._canvasOptions;
  99. var rMin = co.rangeMinVisible;
  100. var rMax = co.rangeMaxVisible;
  101. var offset = value - rMin;
  102. if (value < rMin) {
  103. offset = 0
  104. } else {
  105. if (value > rMax) {
  106. offset = addInterval(rMax, this._options.interval) - rMin
  107. }
  108. }
  109. return this._conversionValue(this._calculateProjection(offset * this._canvasOptions.ratioOfCanvasRange))
  110. },
  111. from: function(position, direction) {
  112. var that = this;
  113. var origInterval = that._options.interval;
  114. var interval = origInterval;
  115. var co = that._canvasOptions;
  116. var rMin = co.rangeMinVisible;
  117. var rMax = co.rangeMaxVisible;
  118. var value;
  119. if ("datetime" === that._businessRange.dataType) {
  120. interval = dateToMilliseconds(origInterval)
  121. }
  122. value = that._calculateUnProjection((position - that._canvasOptions.startPoint) / that._canvasOptions.ratioOfCanvasRange);
  123. value = that._intervalize(addInterval(value, interval / 2, direction > 0), origInterval);
  124. if (value < rMin) {
  125. value = rMin
  126. } else {
  127. if (value > rMax) {
  128. value = rMax
  129. }
  130. }
  131. return value
  132. },
  133. _add: function() {
  134. return NaN
  135. },
  136. isValueProlonged: true
  137. };