range.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * DevExtreme (viz/translators/range.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 extend = require("../../core/utils/extend").extend;
  12. var _isDefined = typeUtils.isDefined;
  13. var _isDate = typeUtils.isDate;
  14. var _isFunction = typeUtils.isFunction;
  15. var unique = require("../core/utils").unique;
  16. var minSelector = "min";
  17. var maxSelector = "max";
  18. var minVisibleSelector = "minVisible";
  19. var maxVisibleSelector = "maxVisible";
  20. var baseSelector = "base";
  21. var axisTypeSelector = "axisType";
  22. function otherLessThan(thisValue, otherValue) {
  23. return otherValue < thisValue
  24. }
  25. function otherGreaterThan(thisValue, otherValue) {
  26. return otherValue > thisValue
  27. }
  28. function compareAndReplace(thisValue, otherValue, setValue, compare) {
  29. var otherValueDefined = _isDefined(otherValue);
  30. if (_isDefined(thisValue)) {
  31. if (otherValueDefined && compare(thisValue, otherValue)) {
  32. setValue(otherValue)
  33. }
  34. } else {
  35. if (otherValueDefined) {
  36. setValue(otherValue)
  37. }
  38. }
  39. }
  40. var _Range = exports.Range = function(range) {
  41. range && extend(this, range)
  42. };
  43. _Range.prototype = {
  44. constructor: _Range,
  45. addRange: function(otherRange) {
  46. var that = this;
  47. var categories = that.categories;
  48. var otherCategories = otherRange.categories;
  49. var compareAndReplaceByField = function(field, compare) {
  50. compareAndReplace(that[field], otherRange[field], function(value) {
  51. that[field] = value
  52. }, compare)
  53. };
  54. var controlValuesByVisibleBounds = function(valueField, visibleValueField, compare) {
  55. compareAndReplace(that[valueField], that[visibleValueField], function(value) {
  56. _isDefined(that[valueField]) && (that[valueField] = value)
  57. }, compare)
  58. };
  59. var checkField = function(field) {
  60. that[field] = that[field] || otherRange[field]
  61. };
  62. checkField("invert");
  63. checkField(axisTypeSelector);
  64. checkField("dataType");
  65. checkField("isSpacedMargin"), checkField("checkMinDataVisibility");
  66. checkField("checkMaxDataVisibility");
  67. if ("logarithmic" === that[axisTypeSelector]) {
  68. checkField(baseSelector)
  69. } else {
  70. that[baseSelector] = void 0
  71. }
  72. compareAndReplaceByField(minSelector, otherLessThan);
  73. compareAndReplaceByField(maxSelector, otherGreaterThan);
  74. if ("discrete" === that[axisTypeSelector]) {
  75. checkField(minVisibleSelector);
  76. checkField(maxVisibleSelector)
  77. } else {
  78. compareAndReplaceByField(minVisibleSelector, otherLessThan);
  79. compareAndReplaceByField(maxVisibleSelector, otherGreaterThan)
  80. }
  81. compareAndReplaceByField("interval", otherLessThan);
  82. controlValuesByVisibleBounds(minSelector, minVisibleSelector, otherLessThan);
  83. controlValuesByVisibleBounds(minSelector, maxVisibleSelector, otherLessThan);
  84. controlValuesByVisibleBounds(maxSelector, maxVisibleSelector, otherGreaterThan);
  85. controlValuesByVisibleBounds(maxSelector, minVisibleSelector, otherGreaterThan);
  86. if (void 0 === categories) {
  87. that.categories = otherCategories
  88. } else {
  89. that.categories = otherCategories ? unique(categories.concat(otherCategories)) : categories
  90. }
  91. return that
  92. },
  93. isEmpty: function() {
  94. return (!_isDefined(this[minSelector]) || !_isDefined(this[maxSelector])) && (!this.categories || 0 === this.categories.length)
  95. },
  96. correctValueZeroLevel: function() {
  97. var that = this;
  98. if ("logarithmic" === that[axisTypeSelector] || _isDate(that[maxSelector]) || _isDate(that[minSelector])) {
  99. return that
  100. }
  101. function setZeroLevel(min, max) {
  102. that[min] < 0 && that[max] < 0 && (that[max] = 0);
  103. that[min] > 0 && that[max] > 0 && (that[min] = 0)
  104. }
  105. setZeroLevel(minSelector, maxSelector);
  106. setZeroLevel(minVisibleSelector, maxVisibleSelector);
  107. return that
  108. },
  109. sortCategories: function(sort) {
  110. if (false === sort || !this.categories) {
  111. return
  112. }
  113. if (Array.isArray(sort)) {
  114. var sortValues = sort.map(function(item) {
  115. return item.valueOf()
  116. });
  117. var filteredSeriesCategories = this.categories.filter(function(item) {
  118. return sortValues.indexOf(item.valueOf()) === -1
  119. });
  120. this.categories = sort.concat(filteredSeriesCategories)
  121. } else {
  122. var notAFunction = !_isFunction(sort);
  123. if (notAFunction && "string" !== this.dataType) {
  124. sort = function(a, b) {
  125. return a.valueOf() - b.valueOf()
  126. }
  127. } else {
  128. if (notAFunction) {
  129. sort = false
  130. }
  131. }
  132. sort && this.categories.sort(sort)
  133. }
  134. }
  135. };