crosshair.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * DevExtreme (viz/chart_components/crosshair.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 _utils = require("../core/utils");
  11. var _extend = require("../../core/utils/extend");
  12. var math = Math;
  13. var mathAbs = math.abs;
  14. var mathMin = math.min;
  15. var mathMax = math.max;
  16. var mathFloor = math.floor;
  17. var HORIZONTAL = "horizontal";
  18. var VERTICAL = "vertical";
  19. var LABEL_BACKGROUND_PADDING_X = 8;
  20. var LABEL_BACKGROUND_PADDING_Y = 4;
  21. var CENTER = "center";
  22. var RIGHT = "right";
  23. var LEFT = "left";
  24. var TOP = "top";
  25. var BOTTOM = "bottom";
  26. exports.getMargins = function() {
  27. return {
  28. x: LABEL_BACKGROUND_PADDING_X,
  29. y: LABEL_BACKGROUND_PADDING_Y
  30. }
  31. };
  32. function getRectangleBBox(bBox) {
  33. return {
  34. x: bBox.x - LABEL_BACKGROUND_PADDING_X,
  35. y: bBox.y - LABEL_BACKGROUND_PADDING_Y,
  36. width: bBox.width + 2 * LABEL_BACKGROUND_PADDING_X,
  37. height: bBox.height + 2 * LABEL_BACKGROUND_PADDING_Y
  38. }
  39. }
  40. function getLabelCheckerPosition(x, y, isHorizontal, canvas) {
  41. var params = isHorizontal ? ["x", "width", "y", "height", y, 0] : ["y", "height", "x", "width", x, 1];
  42. return function(bBox, position, coord) {
  43. var labelCoord = {
  44. x: coord.x,
  45. y: coord.y
  46. };
  47. var rectangleBBox = getRectangleBBox(bBox);
  48. var delta = isHorizontal ? coord.y - bBox.y - bBox.height / 2 : coord.y - bBox.y;
  49. labelCoord.y = isHorizontal || !isHorizontal && position === BOTTOM ? coord.y + delta : coord.y;
  50. if (rectangleBBox[params[0]] < 0) {
  51. labelCoord[params[0]] -= rectangleBBox[params[0]]
  52. } else {
  53. if (rectangleBBox[params[0]] + rectangleBBox[params[1]] + delta * params[5] > canvas[params[1]]) {
  54. labelCoord[params[0]] -= rectangleBBox[params[0]] + rectangleBBox[params[1]] + delta * params[5] - canvas[params[1]]
  55. }
  56. }
  57. if (params[4] - rectangleBBox[params[3]] / 2 < 0) {
  58. labelCoord[params[2]] -= params[4] - rectangleBBox[params[3]] / 2
  59. } else {
  60. if (params[4] + rectangleBBox[params[3]] / 2 > canvas[params[3]]) {
  61. labelCoord[params[2]] -= params[4] + rectangleBBox[params[3]] / 2 - canvas[params[3]]
  62. }
  63. }
  64. return labelCoord
  65. }
  66. }
  67. function Crosshair(renderer, options, params, group) {
  68. var that = this;
  69. that._renderer = renderer;
  70. that._crosshairGroup = group;
  71. that._options = {};
  72. that.update(options, params)
  73. }
  74. Crosshair.prototype = {
  75. constructor: Crosshair,
  76. update: function(options, params) {
  77. var that = this;
  78. var canvas = params.canvas;
  79. that._canvas = {
  80. top: canvas.top,
  81. bottom: canvas.height - canvas.bottom,
  82. left: canvas.left,
  83. right: canvas.width - canvas.right,
  84. width: canvas.width,
  85. height: canvas.height
  86. };
  87. that._axes = params.axes;
  88. that._panes = params.panes;
  89. that._prepareOptions(options, HORIZONTAL);
  90. that._prepareOptions(options, VERTICAL)
  91. },
  92. dispose: function() {
  93. var that = this;
  94. that._renderer = that._crosshairGroup = that._options = that._axes = that._canvas = that._horizontalGroup = that._verticalGroup = that._horizontal = that._vertical = that._circle = that._panes = null
  95. },
  96. _prepareOptions: function(options, direction) {
  97. var lineOptions = options[direction + "Line"];
  98. this._options[direction] = {
  99. visible: lineOptions.visible,
  100. line: {
  101. stroke: lineOptions.color || options.color,
  102. "stroke-width": lineOptions.width || options.width,
  103. dashStyle: lineOptions.dashStyle || options.dashStyle,
  104. opacity: lineOptions.opacity || options.opacity,
  105. "stroke-linecap": "butt"
  106. },
  107. label: (0, _extend.extend)(true, {}, options.label, lineOptions.label)
  108. }
  109. },
  110. _createLines: function(options, sharpParam, group) {
  111. var lines = [];
  112. var canvas = this._canvas;
  113. var points = [canvas.left, canvas.top, canvas.left, canvas.top];
  114. for (var i = 0; i < 2; i++) {
  115. lines.push(this._renderer.path(points, "line").attr(options).sharp(sharpParam).append(group))
  116. }
  117. return lines
  118. },
  119. render: function() {
  120. var that = this;
  121. var renderer = that._renderer;
  122. var options = that._options;
  123. var verticalOptions = options.vertical;
  124. var horizontalOptions = options.horizontal;
  125. var extraOptions = horizontalOptions.visible ? horizontalOptions.line : verticalOptions.line;
  126. var circleOptions = {
  127. stroke: extraOptions.stroke,
  128. "stroke-width": extraOptions["stroke-width"],
  129. dashStyle: extraOptions.dashStyle,
  130. opacity: extraOptions.opacity
  131. };
  132. var canvas = that._canvas;
  133. that._horizontal = {};
  134. that._vertical = {};
  135. that._circle = renderer.circle(canvas.left, canvas.top, 0).attr(circleOptions).append(that._crosshairGroup);
  136. that._horizontalGroup = renderer.g().append(that._crosshairGroup);
  137. that._verticalGroup = renderer.g().append(that._crosshairGroup);
  138. if (verticalOptions.visible) {
  139. that._vertical.lines = that._createLines(verticalOptions.line, "h", that._verticalGroup);
  140. that._vertical.labels = that._createLabels(that._axes[0], verticalOptions, false, that._verticalGroup)
  141. }
  142. if (horizontalOptions.visible) {
  143. that._horizontal.lines = that._createLines(horizontalOptions.line, "v", that._horizontalGroup);
  144. that._horizontal.labels = that._createLabels(that._axes[1], horizontalOptions, true, that._horizontalGroup)
  145. }
  146. that.hide()
  147. },
  148. _createLabels: function(axes, options, isHorizontal, group) {
  149. var that = this;
  150. var canvas = that._canvas;
  151. var renderer = that._renderer;
  152. var x;
  153. var y;
  154. var text;
  155. var labels = [];
  156. var background;
  157. var currentLabelPos;
  158. var labelOptions = options.label;
  159. if (labelOptions.visible) {
  160. axes.forEach(function(axis) {
  161. var position = axis.getOptions().position;
  162. if (axis.getTranslator().getBusinessRange().isEmpty()) {
  163. return
  164. }
  165. currentLabelPos = axis.getLabelsPosition();
  166. if (isHorizontal) {
  167. y = canvas.top;
  168. x = currentLabelPos
  169. } else {
  170. x = canvas.left;
  171. y = currentLabelPos
  172. }
  173. var align = position === TOP || position === BOTTOM ? CENTER : position === RIGHT ? LEFT : RIGHT;
  174. background = renderer.rect(0, 0, 0, 0).attr({
  175. fill: labelOptions.backgroundColor || options.line.stroke
  176. }).append(group);
  177. text = renderer.text("0", 0, 0).css((0, _utils.patchFontOptions)(options.label.font)).attr({
  178. align: align,
  179. "class": labelOptions.cssClass
  180. }).append(group);
  181. labels.push({
  182. text: text,
  183. background: background,
  184. axis: axis,
  185. options: labelOptions,
  186. pos: {
  187. coord: currentLabelPos,
  188. side: position
  189. },
  190. startXY: {
  191. x: x,
  192. y: y
  193. }
  194. })
  195. })
  196. }
  197. return labels
  198. },
  199. _updateText: function(value, axisName, labels, point, func) {
  200. var that = this;
  201. labels.forEach(function(label) {
  202. var axis = label.axis;
  203. var coord = label.startXY;
  204. var textElement = label.text;
  205. var backgroundElement = label.background;
  206. var text = "";
  207. if (!axis.name || axis.name === axisName) {
  208. text = axis.getFormattedValue(value, label.options, point)
  209. }
  210. if (text) {
  211. textElement.attr({
  212. text: text,
  213. x: coord.x,
  214. y: coord.y
  215. });
  216. textElement.attr(func(textElement.getBBox(), label.pos.side, coord));
  217. that._updateLinesCanvas(label);
  218. backgroundElement.attr(getRectangleBBox(textElement.getBBox()))
  219. } else {
  220. textElement.attr({
  221. text: ""
  222. });
  223. backgroundElement.attr({
  224. x: 0,
  225. y: 0,
  226. width: 0,
  227. height: 0
  228. })
  229. }
  230. })
  231. },
  232. hide: function() {
  233. this._crosshairGroup.attr({
  234. visibility: "hidden"
  235. })
  236. },
  237. _updateLinesCanvas: function(label) {
  238. var position = label.pos.side;
  239. var labelCoord = label.pos.coord;
  240. var coords = this._linesCanvas;
  241. var canvas = this._canvas;
  242. coords[position] = coords[position] !== canvas[position] && mathAbs(coords[position] - canvas[position]) < mathAbs(labelCoord - canvas[position]) ? coords[position] : labelCoord
  243. },
  244. _updateLines: function(lines, x, y, r, isHorizontal) {
  245. var coords = this._linesCanvas;
  246. var canvas = this._canvas;
  247. var points = isHorizontal ? [
  248. [mathMin(x - r, coords.left), canvas.top, x - r, canvas.top],
  249. [x + r, canvas.top, mathMax(coords.right, x + r), canvas.top]
  250. ] : [
  251. [canvas.left, mathMin(coords.top, y - r), canvas.left, y - r],
  252. [canvas.left, y + r, canvas.left, mathMax(coords.bottom, y + r)]
  253. ];
  254. for (var i = 0; i < 2; i++) {
  255. lines[i].attr({
  256. points: points[i]
  257. }).sharp(isHorizontal ? "v" : "h", isHorizontal ? y === canvas.bottom ? -1 : 1 : x === canvas.right ? -1 : 1)
  258. }
  259. },
  260. _resetLinesCanvas: function() {
  261. var canvas = this._canvas;
  262. this._linesCanvas = {
  263. left: canvas.left,
  264. right: canvas.right,
  265. top: canvas.top,
  266. bottom: canvas.bottom
  267. }
  268. },
  269. _getClipRectForPane: function(x, y) {
  270. var panes = this._panes;
  271. var i;
  272. var coords;
  273. for (i = 0; i < panes.length; i++) {
  274. coords = panes[i].coords;
  275. if (coords.left <= x && coords.right >= x && coords.top <= y && coords.bottom >= y) {
  276. return panes[i].clipRect
  277. }
  278. }
  279. return {
  280. id: null
  281. }
  282. },
  283. show: function(data) {
  284. var that = this;
  285. var point = data.point;
  286. var pointData = point.getCrosshairData(data.x, data.y);
  287. var r = point.getPointRadius();
  288. var horizontal = that._horizontal;
  289. var vertical = that._vertical;
  290. var rad = !r ? 0 : r + 3;
  291. var canvas = that._canvas;
  292. var x = mathFloor(pointData.x);
  293. var y = mathFloor(pointData.y);
  294. if (x >= canvas.left && x <= canvas.right && y >= canvas.top && y <= canvas.bottom) {
  295. that._crosshairGroup.attr({
  296. visibility: "visible"
  297. });
  298. that._resetLinesCanvas();
  299. that._circle.attr({
  300. cx: x,
  301. cy: y,
  302. r: rad,
  303. "clip-path": that._getClipRectForPane(x, y).id
  304. });
  305. if (horizontal.lines) {
  306. that._updateText(pointData.yValue, pointData.axis, horizontal.labels, point, getLabelCheckerPosition(x, y, true, canvas));
  307. that._updateLines(horizontal.lines, x, y, rad, true);
  308. that._horizontalGroup.attr({
  309. translateY: y - canvas.top
  310. })
  311. }
  312. if (vertical.lines) {
  313. that._updateText(pointData.xValue, pointData.axis, vertical.labels, point, getLabelCheckerPosition(x, y, false, canvas));
  314. that._updateLines(vertical.lines, x, y, rad, false);
  315. that._verticalGroup.attr({
  316. translateX: x - canvas.left
  317. })
  318. }
  319. } else {
  320. that.hide()
  321. }
  322. }
  323. };
  324. exports.Crosshair = Crosshair;