bar_point.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * DevExtreme (viz/series/points/bar_point.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 extend = require("../../../core/utils/extend").extend;
  11. var _extend = extend;
  12. var _math = Math;
  13. var _floor = _math.floor;
  14. var _abs = _math.abs;
  15. var symbolPoint = require("./symbol_point");
  16. var CANVAS_POSITION_DEFAULT = "canvas_position_default";
  17. var DEFAULT_BAR_TRACKER_SIZE = 9;
  18. var CORRECTING_BAR_TRACKER_VALUE = 4;
  19. var RIGHT = "right";
  20. var LEFT = "left";
  21. var TOP = "top";
  22. var BOTTOM = "bottom";
  23. module.exports = _extend({}, symbolPoint, {
  24. correctCoordinates: function(correctOptions) {
  25. var that = this;
  26. var correction = _floor(correctOptions.offset - correctOptions.width / 2);
  27. if (that._options.rotated) {
  28. that.height = correctOptions.width;
  29. that.yCorrection = correction;
  30. that.xCorrection = null
  31. } else {
  32. that.width = correctOptions.width;
  33. that.xCorrection = correction;
  34. that.yCorrection = null
  35. }
  36. },
  37. _getGraphicBBox: function() {
  38. return {
  39. x: this.x,
  40. y: this.y,
  41. width: this.width,
  42. height: this.height
  43. }
  44. },
  45. _getLabelConnector: function(location) {
  46. return this._getGraphicBBox(location)
  47. },
  48. _getLabelPosition: function() {
  49. var that = this;
  50. var position;
  51. var initialValue = that.initialValue;
  52. var invert = that._getValTranslator().getBusinessRange().invert;
  53. var isDiscreteValue = "discrete" === that.series.valueAxisType;
  54. var isFullStacked = that.series.isFullStackedSeries();
  55. var notAxisInverted = !isDiscreteValue && (initialValue >= 0 && !invert || initialValue < 0 && invert) || isDiscreteValue && !invert || isFullStacked;
  56. if (!that._options.rotated) {
  57. position = notAxisInverted ? TOP : BOTTOM
  58. } else {
  59. position = notAxisInverted ? RIGHT : LEFT
  60. }
  61. return position
  62. },
  63. _getLabelCoords: function(label) {
  64. var that = this;
  65. var coords;
  66. if (0 === that.initialValue && that.series.isFullStackedSeries()) {
  67. if (!this._options.rotated) {
  68. coords = that._getLabelCoordOfPosition(label, TOP)
  69. } else {
  70. coords = that._getLabelCoordOfPosition(label, RIGHT)
  71. }
  72. } else {
  73. if ("inside" === label.getLayoutOptions().position) {
  74. coords = that._getLabelCoordOfPosition(label, "inside")
  75. } else {
  76. coords = symbolPoint._getLabelCoords.call(this, label)
  77. }
  78. }
  79. return coords
  80. },
  81. _checkLabelPosition: function(label, coord) {
  82. var that = this;
  83. var visibleArea = that._getVisibleArea();
  84. if (that._isPointInVisibleArea(visibleArea, that._getGraphicBBox())) {
  85. return that._moveLabelOnCanvas(coord, visibleArea, label.getBoundingRect())
  86. }
  87. return coord
  88. },
  89. hideInsideLabel: function(label, coord) {
  90. var graphicBBox = this._getGraphicBBox();
  91. var labelBBox = label.getBoundingRect();
  92. if (this._options.resolveLabelsOverlapping) {
  93. if ((coord.y <= graphicBBox.y && coord.y + labelBBox.height >= graphicBBox.y + graphicBBox.height || coord.x <= graphicBBox.x && coord.x + labelBBox.width >= graphicBBox.x + graphicBBox.width) && !(coord.y > graphicBBox.y + graphicBBox.height || coord.y + labelBBox.height < graphicBBox.y || coord.x > graphicBBox.x + graphicBBox.width || coord.x + labelBBox.width < graphicBBox.x)) {
  94. label.draw(false);
  95. return true
  96. }
  97. }
  98. return false
  99. },
  100. _moveLabelOnCanvas: function(coord, visibleArea, labelBBox) {
  101. var x = coord.x;
  102. var y = coord.y;
  103. if (visibleArea.minX > x) {
  104. x = visibleArea.minX
  105. }
  106. if (visibleArea.maxX < x + labelBBox.width) {
  107. x = visibleArea.maxX - labelBBox.width
  108. }
  109. if (visibleArea.minY > y) {
  110. y = visibleArea.minY
  111. }
  112. if (visibleArea.maxY < y + labelBBox.height) {
  113. y = visibleArea.maxY - labelBBox.height
  114. }
  115. return {
  116. x: x,
  117. y: y
  118. }
  119. },
  120. _showForZeroValues: function() {
  121. return this._options.label.showForZeroValues || this.initialValue
  122. },
  123. _drawMarker: function(renderer, group, animationEnabled) {
  124. var that = this;
  125. var style = that._getStyle();
  126. var r = that._options.cornerRadius;
  127. var rotated = that._options.rotated;
  128. var _that$getMarkerCoords = that.getMarkerCoords(),
  129. x = _that$getMarkerCoords.x,
  130. y = _that$getMarkerCoords.y,
  131. width = _that$getMarkerCoords.width,
  132. height = _that$getMarkerCoords.height;
  133. if (animationEnabled) {
  134. if (rotated) {
  135. width = 0;
  136. x = that.defaultX
  137. } else {
  138. height = 0;
  139. y = that.defaultY
  140. }
  141. }
  142. that.graphic = renderer.rect(x, y, width, height).attr({
  143. rx: r,
  144. ry: r
  145. }).smartAttr(style).data({
  146. "chart-data-point": that
  147. }).append(group)
  148. },
  149. _getSettingsForTracker: function() {
  150. var that = this;
  151. var y = that.y;
  152. var height = that.height;
  153. var x = that.x;
  154. var width = that.width;
  155. if (that._options.rotated) {
  156. if (1 === width) {
  157. width = DEFAULT_BAR_TRACKER_SIZE;
  158. x -= CORRECTING_BAR_TRACKER_VALUE
  159. }
  160. } else {
  161. if (1 === height) {
  162. height = DEFAULT_BAR_TRACKER_SIZE;
  163. y -= CORRECTING_BAR_TRACKER_VALUE
  164. }
  165. }
  166. return {
  167. x: x,
  168. y: y,
  169. width: width,
  170. height: height
  171. }
  172. },
  173. getGraphicSettings: function() {
  174. var graphic = this.graphic;
  175. return {
  176. x: graphic.attr("x"),
  177. y: graphic.attr("y"),
  178. height: graphic.attr("height"),
  179. width: graphic.attr("width")
  180. }
  181. },
  182. _getEdgeTooltipParams: function() {
  183. var isPositive = this.value >= 0;
  184. var xCoord;
  185. var yCoord;
  186. var invertedBusinessRange = this._getValTranslator().getBusinessRange().invert;
  187. var x = this.x,
  188. y = this.y,
  189. width = this.width,
  190. height = this.height;
  191. if (this._options.rotated) {
  192. yCoord = y + height / 2;
  193. if (invertedBusinessRange) {
  194. xCoord = isPositive ? x : x + width
  195. } else {
  196. xCoord = isPositive ? x + width : x
  197. }
  198. } else {
  199. xCoord = x + width / 2;
  200. if (invertedBusinessRange) {
  201. yCoord = isPositive ? y + height : y
  202. } else {
  203. yCoord = isPositive ? y : y + height
  204. }
  205. }
  206. return {
  207. x: xCoord,
  208. y: yCoord,
  209. offset: 0
  210. }
  211. },
  212. getTooltipParams: function(location) {
  213. if ("edge" === location) {
  214. return this._getEdgeTooltipParams()
  215. }
  216. var center = this.getCenterCoord();
  217. center.offset = 0;
  218. return center
  219. },
  220. getCenterCoord: function() {
  221. var width = this.width,
  222. height = this.height,
  223. x = this.x,
  224. y = this.y;
  225. return {
  226. x: x + width / 2,
  227. y: y + height / 2
  228. }
  229. },
  230. _truncateCoord: function(coord, minBounce, maxBounce) {
  231. if (null === coord) {
  232. return coord
  233. }
  234. if (coord < minBounce) {
  235. return minBounce
  236. }
  237. if (coord > maxBounce) {
  238. return maxBounce
  239. }
  240. return coord
  241. },
  242. _getErrorBarBaseEdgeLength: function() {
  243. return this._options.rotated ? this.height : this.width
  244. },
  245. _translateErrorBars: function(argVisibleArea) {
  246. symbolPoint._translateErrorBars.call(this);
  247. if (this._errorBarPos < argVisibleArea[0] || this._errorBarPos > argVisibleArea[1]) {
  248. this._errorBarPos = void 0
  249. }
  250. },
  251. _translate: function() {
  252. var that = this;
  253. var rotated = that._options.rotated;
  254. var valAxis = rotated ? "x" : "y";
  255. var argAxis = rotated ? "y" : "x";
  256. var valIntervalName = rotated ? "width" : "height";
  257. var argIntervalName = rotated ? "height" : "width";
  258. var argTranslator = that._getArgTranslator();
  259. var valTranslator = that._getValTranslator();
  260. var argVisibleArea = that.series.getArgumentAxis().getVisibleArea();
  261. var valVisibleArea = that.series.getValueAxis().getVisibleArea();
  262. var arg;
  263. var val;
  264. var minVal;
  265. arg = argTranslator.translate(that.argument);
  266. that[argAxis] = arg = null === arg ? arg : arg + (that[argAxis + "Correction"] || 0);
  267. val = valTranslator.translate(that.value, 1);
  268. minVal = valTranslator.translate(that.minValue);
  269. that["v" + valAxis] = val;
  270. that["v" + argAxis] = arg + that[argIntervalName] / 2;
  271. val = that._truncateCoord(val, valVisibleArea[0], valVisibleArea[1]);
  272. minVal = that._truncateCoord(minVal, valVisibleArea[0], valVisibleArea[1]);
  273. that[valIntervalName] = _abs(val - minVal);
  274. val = val < minVal ? val : minVal;
  275. that._calculateVisibility(rotated ? val : arg, rotated ? arg : val, that.width, that.height);
  276. that[valAxis] = null === val ? val : val + (that[valAxis + "Correction"] || 0);
  277. that["min" + valAxis.toUpperCase()] = null === minVal ? minVal : minVal + (that[valAxis + "Correction"] || 0);
  278. that["default" + valAxis.toUpperCase()] = valTranslator.translate(CANVAS_POSITION_DEFAULT);
  279. that._translateErrorBars(argVisibleArea);
  280. if (that.inVisibleArea && null !== that[argAxis]) {
  281. if (that[argAxis] < argVisibleArea[0]) {
  282. that[argIntervalName] = that[argIntervalName] - (argVisibleArea[0] - that[argAxis]);
  283. that[argAxis] = argVisibleArea[0]
  284. }
  285. if (that[argAxis] + that[argIntervalName] > argVisibleArea[1]) {
  286. that[argIntervalName] = argVisibleArea[1] - that[argAxis]
  287. }
  288. }
  289. },
  290. _updateMarker: function(animationEnabled, style) {
  291. this.graphic.smartAttr(_extend({}, style, !animationEnabled ? this.getMarkerCoords() : {}))
  292. },
  293. getMarkerCoords: function() {
  294. var that = this;
  295. var x = that.x;
  296. var y = that.y;
  297. var width = that.width;
  298. var height = that.height;
  299. var argAxis = that.series.getArgumentAxis();
  300. var rotated = that._options.rotated;
  301. if (argAxis.getAxisPosition) {
  302. var axisOptions = argAxis.getOptions();
  303. var edgeOffset = Math.round(axisOptions.width / 2);
  304. var argAxisPosition = argAxis.getAxisPosition();
  305. if (axisOptions.visible) {
  306. if (!rotated) {
  307. height -= that.minY === that.defaultY && that.minY === argAxisPosition - argAxis.getAxisShift() ? edgeOffset : 0;
  308. height < 0 && (height = 0)
  309. } else {
  310. var isStartFromAxis = that.minX === that.defaultX && that.minX === argAxisPosition - argAxis.getAxisShift();
  311. x += isStartFromAxis ? edgeOffset : 0;
  312. width -= isStartFromAxis ? edgeOffset : 0;
  313. width < 0 && (width = 0)
  314. }
  315. }
  316. }
  317. return {
  318. x: x,
  319. y: y,
  320. width: width,
  321. height: height
  322. }
  323. },
  324. coordsIn: function(x, y) {
  325. var that = this;
  326. return x >= that.x && x <= that.x + that.width && y >= that.y && y <= that.y + that.height
  327. }
  328. });