linear_gauge.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * DevExtreme (viz/gauges/linear_gauge.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 _each = require("../../core/utils/iterator").each;
  11. var _max = Math.max;
  12. var _min = Math.min;
  13. var _round = Math.round;
  14. var registerComponent = require("../../core/component_registrator");
  15. var extend = require("../../core/utils/extend").extend;
  16. var objectUtils = require("../../core/utils/object");
  17. var dxBaseGauge = require("./base_gauge").dxBaseGauge;
  18. var dxGauge = require("./common").dxGauge;
  19. var _normalizeEnum = require("../core/utils").normalizeEnum;
  20. var linearIndicatorsModule = require("./linear_indicators");
  21. var createIndicatorCreator = require("./common").createIndicatorCreator;
  22. var LinearRangeContainer = require("./linear_range_container");
  23. var dxLinearGauge = dxGauge.inherit({
  24. _rootClass: "dxg-linear-gauge",
  25. _factoryMethods: {
  26. rangeContainer: "createLinearRangeContainer",
  27. indicator: "createLinearIndicator"
  28. },
  29. _gridSpacingFactor: 25,
  30. _scaleTypes: {
  31. type: "xyAxes",
  32. drawingType: "linear"
  33. },
  34. _getTicksOrientation: function(scaleOptions) {
  35. return scaleOptions.isHorizontal ? scaleOptions.verticalOrientation : scaleOptions.horizontalOrientation
  36. },
  37. _getThemeManagerOptions: function() {
  38. var options = this.callBase.apply(this, arguments);
  39. options.subTheme = "_linear";
  40. return options
  41. },
  42. _updateScaleTickIndent: function(scaleOptions) {
  43. var indentFromTick = scaleOptions.label.indentFromTick;
  44. var length = scaleOptions.tick.length;
  45. var textParams = this._scale.measureLabels(extend({}, this._canvas));
  46. var verticalTextCorrection = scaleOptions.isHorizontal ? textParams.height + textParams.y : 0;
  47. var isIndentPositive = indentFromTick > 0;
  48. var orientation;
  49. var textCorrection;
  50. var tickCorrection;
  51. if (scaleOptions.isHorizontal) {
  52. orientation = isIndentPositive ? {
  53. center: .5,
  54. top: 0,
  55. bottom: 1
  56. } : {
  57. center: .5,
  58. top: 1,
  59. bottom: 0
  60. };
  61. tickCorrection = length * orientation[scaleOptions.verticalOrientation];
  62. textCorrection = textParams.y
  63. } else {
  64. orientation = isIndentPositive ? {
  65. center: .5,
  66. left: 0,
  67. right: 1
  68. } : {
  69. center: .5,
  70. left: 1,
  71. right: 0
  72. };
  73. tickCorrection = length * orientation[scaleOptions.horizontalOrientation];
  74. textCorrection = -textParams.width
  75. }
  76. scaleOptions.label.indentFromAxis = -indentFromTick + (isIndentPositive ? -tickCorrection + textCorrection : tickCorrection - verticalTextCorrection);
  77. this._scale.updateOptions(scaleOptions)
  78. },
  79. _shiftScale: function(layout, scaleOptions) {
  80. var that = this;
  81. var canvas = extend({}, that._canvas);
  82. var isHorizontal = scaleOptions.isHorizontal;
  83. var scale = that._scale;
  84. canvas[isHorizontal ? "left" : "top"] = that._area[isHorizontal ? "startCoord" : "endCoord"];
  85. canvas[isHorizontal ? "right" : "bottom"] = canvas[isHorizontal ? "width" : "height"] - that._area[isHorizontal ? "endCoord" : "startCoord"];
  86. scale.draw(canvas);
  87. scale.shift({
  88. left: -layout.x,
  89. top: -layout.y
  90. })
  91. },
  92. _setupCodomain: function() {
  93. var that = this;
  94. var geometry = that.option("geometry") || {};
  95. var vertical = "vertical" === _normalizeEnum(geometry.orientation);
  96. var initialStartCoord = -100;
  97. var initialEndCoord = 100;
  98. that._area = {
  99. vertical: vertical,
  100. x: 0,
  101. y: 0,
  102. startCoord: initialStartCoord,
  103. endCoord: initialEndCoord
  104. };
  105. that._rangeContainer.vertical = vertical;
  106. that._translator.setCodomain(initialStartCoord, initialEndCoord)
  107. },
  108. _getScaleLayoutValue: function() {
  109. return this._area[this._area.vertical ? "x" : "y"]
  110. },
  111. _getTicksCoefficients: function(options) {
  112. var coefs = {
  113. inner: 0,
  114. outer: 1
  115. };
  116. if (this._area.vertical) {
  117. if ("left" === options.horizontalOrientation) {
  118. coefs.inner = 1;
  119. coefs.outer = 0
  120. } else {
  121. if ("center" === options.horizontalOrientation) {
  122. coefs.inner = coefs.outer = .5
  123. }
  124. }
  125. } else {
  126. if ("top" === options.verticalOrientation) {
  127. coefs.inner = 1;
  128. coefs.outer = 0
  129. } else {
  130. if ("center" === options.verticalOrientation) {
  131. coefs.inner = coefs.outer = .5
  132. }
  133. }
  134. }
  135. return coefs
  136. },
  137. _correctScaleIndents: function(result, indentFromTick, textParams) {
  138. var vertical = this._area.vertical;
  139. if (indentFromTick >= 0) {
  140. result.max += indentFromTick + textParams[vertical ? "width" : "height"]
  141. } else {
  142. result.min -= -indentFromTick + textParams[vertical ? "width" : "height"]
  143. }
  144. result.indent = textParams[vertical ? "height" : "width"] / 2
  145. },
  146. _measureMainElements: function(elements, scaleMeasurement) {
  147. var that = this;
  148. var x = that._area.x;
  149. var y = that._area.y;
  150. var minBound = 1e3;
  151. var maxBound = 0;
  152. var indent = 0;
  153. var scale = that._scale;
  154. _each(elements.concat(scale), function(_, element) {
  155. var bounds = element.measure ? element.measure({
  156. x: x + element.getOffset(),
  157. y: y + element.getOffset()
  158. }) : scaleMeasurement;
  159. void 0 !== bounds.max && (maxBound = _max(maxBound, bounds.max));
  160. void 0 !== bounds.min && (minBound = _min(minBound, bounds.min));
  161. bounds.indent > 0 && (indent = _max(indent, bounds.indent))
  162. });
  163. return {
  164. minBound: minBound,
  165. maxBound: maxBound,
  166. indent: indent
  167. }
  168. },
  169. _applyMainLayout: function(elements, scaleMeasurement) {
  170. var that = this;
  171. var measurements = that._measureMainElements(elements, scaleMeasurement);
  172. var area = that._area;
  173. var rect;
  174. var offset;
  175. if (area.vertical) {
  176. rect = selectRectBySizes(that._innerRect, {
  177. width: measurements.maxBound - measurements.minBound
  178. });
  179. offset = (rect.left + rect.right) / 2 - (measurements.minBound + measurements.maxBound) / 2;
  180. area.startCoord = rect.bottom - measurements.indent;
  181. area.endCoord = rect.top + measurements.indent;
  182. area.x = _round(area.x + offset)
  183. } else {
  184. rect = selectRectBySizes(that._innerRect, {
  185. height: measurements.maxBound - measurements.minBound
  186. });
  187. offset = (rect.top + rect.bottom) / 2 - (measurements.minBound + measurements.maxBound) / 2;
  188. area.startCoord = rect.left + measurements.indent;
  189. area.endCoord = rect.right - measurements.indent;
  190. area.y = _round(area.y + offset)
  191. }
  192. that._translator.setCodomain(area.startCoord, area.endCoord);
  193. that._innerRect = rect
  194. },
  195. _getElementLayout: function(offset) {
  196. return {
  197. x: _round(this._area.x + offset),
  198. y: _round(this._area.y + offset)
  199. }
  200. },
  201. _getApproximateScreenRange: function() {
  202. var that = this;
  203. var area = that._area;
  204. var s = area.vertical ? that._canvas.height : that._canvas.width;
  205. s > area.totalSize && (s = area.totalSize);
  206. s = .8 * s;
  207. return s
  208. },
  209. _getDefaultSize: function() {
  210. var geometry = this.option("geometry") || {};
  211. if ("vertical" === geometry.orientation) {
  212. return {
  213. width: 100,
  214. height: 300
  215. }
  216. } else {
  217. return {
  218. width: 300,
  219. height: 100
  220. }
  221. }
  222. },
  223. _factory: objectUtils.clone(dxBaseGauge.prototype._factory)
  224. });
  225. function selectRectBySizes(srcRect, sizes, margins) {
  226. var rect = extend({}, srcRect);
  227. var step;
  228. margins = margins || {};
  229. if (sizes) {
  230. rect.left += margins.left || 0;
  231. rect.right -= margins.right || 0;
  232. rect.top += margins.top || 0;
  233. rect.bottom -= margins.bottom || 0;
  234. if (sizes.width > 0) {
  235. step = (rect.right - rect.left - sizes.width) / 2;
  236. if (step > 0) {
  237. rect.left += step;
  238. rect.right -= step
  239. }
  240. }
  241. if (sizes.height > 0) {
  242. step = (rect.bottom - rect.top - sizes.height) / 2;
  243. if (step > 0) {
  244. rect.top += step;
  245. rect.bottom -= step
  246. }
  247. }
  248. }
  249. return rect
  250. }
  251. var indicators = dxLinearGauge.prototype._factory.indicators = {};
  252. dxLinearGauge.prototype._factory.createIndicator = createIndicatorCreator(indicators);
  253. indicators._default = linearIndicatorsModule._default;
  254. indicators.rectangle = linearIndicatorsModule.rectangle;
  255. indicators.rhombus = linearIndicatorsModule.rhombus;
  256. indicators.circle = linearIndicatorsModule.circle;
  257. indicators.trianglemarker = linearIndicatorsModule.trianglemarker;
  258. indicators.textcloud = linearIndicatorsModule.textcloud;
  259. indicators.rangebar = linearIndicatorsModule.rangebar;
  260. dxLinearGauge.prototype._factory.RangeContainer = LinearRangeContainer;
  261. registerComponent("dxLinearGauge", dxLinearGauge);
  262. module.exports = dxLinearGauge;