circular_indicators.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /**
  2. * DevExtreme (viz/gauges/circular_indicators.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 baseIndicatorsModule = require("./base_indicators");
  11. var BaseIndicator = baseIndicatorsModule.BaseIndicator;
  12. var BaseTextCloudMarker = baseIndicatorsModule.BaseTextCloudMarker;
  13. var BaseRangeBar = baseIndicatorsModule.BaseRangeBar;
  14. var vizUtils = require("../core/utils");
  15. var _Number = Number;
  16. var _getCosAndSin = vizUtils.getCosAndSin;
  17. var _convertAngleToRendererSpace = vizUtils.convertAngleToRendererSpace;
  18. var SimpleIndicator = BaseIndicator.inherit({
  19. _move: function() {
  20. var that = this;
  21. var options = that._options;
  22. var angle = _convertAngleToRendererSpace(that._actualPosition);
  23. that._rootElement.rotate(angle, options.x, options.y);
  24. that._trackerElement && that._trackerElement.rotate(angle, options.x, options.y)
  25. },
  26. _isEnabled: function() {
  27. return this._options.width > 0
  28. },
  29. _isVisible: function(layout) {
  30. return layout.radius - _Number(this._options.indentFromCenter) > 0
  31. },
  32. _getTrackerSettings: function() {
  33. var options = this._options;
  34. var radius = this._getRadius();
  35. var indentFromCenter = this._getIndentFromCenter();
  36. var x = options.x;
  37. var y = options.y - (radius + indentFromCenter) / 2;
  38. var width = options.width / 2;
  39. var length = (radius - indentFromCenter) / 2;
  40. width > 10 || (width = 10);
  41. length > 10 || (length = 10);
  42. return {
  43. points: [x - width, y - length, x - width, y + length, x + width, y + length, x + width, y - length]
  44. }
  45. },
  46. _render: function() {
  47. var that = this;
  48. that._renderPointer()
  49. },
  50. _clearPointer: function() {
  51. delete this._element
  52. },
  53. _clear: function() {
  54. this._clearPointer()
  55. },
  56. _getIndentFromCenter: function(radius) {
  57. return Number(this._options.indentFromCenter) || 0
  58. },
  59. _getRadius: function() {
  60. return 0
  61. },
  62. measure: function(layout) {
  63. var result = {
  64. max: layout.radius
  65. };
  66. if (this._options.indentFromCenter < 0) {
  67. result.inverseHorizontalOffset = result.inverseVerticalOffset = -_Number(this._options.indentFromCenter)
  68. }
  69. return result
  70. },
  71. getTooltipParameters: function() {
  72. var options = this._options;
  73. var cosSin = _getCosAndSin(this._actualPosition);
  74. var r = (this._getRadius() + this._getIndentFromCenter()) / 2;
  75. return {
  76. x: options.x + cosSin.cos * r,
  77. y: options.y - cosSin.sin * r,
  78. value: this._currentValue,
  79. color: options.color,
  80. offset: options.width / 2
  81. }
  82. }
  83. });
  84. var NeedleIndicator = SimpleIndicator.inherit({
  85. _isVisible: function(layout) {
  86. var indentFromCenter = this._adjustOffset(Number(this._options.indentFromCenter), layout.radius);
  87. var offset = this._adjustOffset(Number(this._options.offset), layout.radius);
  88. return layout.radius - indentFromCenter - offset > 0
  89. },
  90. getOffset: function() {
  91. return 0
  92. },
  93. _adjustOffset: function(value, radius) {
  94. var minRadius = Number(this._options.beginAdaptingAtRadius);
  95. var diff = radius / minRadius;
  96. if (diff < 1) {
  97. value = Math.floor(value * diff)
  98. }
  99. return value || 0
  100. },
  101. _getIndentFromCenter: function(radius) {
  102. return this._adjustOffset(Number(this._options.indentFromCenter), this._options.radius)
  103. },
  104. _getRadius: function() {
  105. var options = this._options;
  106. return options.radius - this._adjustOffset(Number(options.offset), options.radius)
  107. },
  108. _renderSpindle: function() {
  109. var that = this;
  110. var options = that._options;
  111. var radius = options.radius;
  112. var spindleSize = 2 * this._adjustOffset(_Number(options.spindleSize) / 2, radius);
  113. var gapSize;
  114. gapSize = 2 * this._adjustOffset(_Number(options.spindleGapSize) / 2, radius) || 0;
  115. if (gapSize > 0) {
  116. gapSize = gapSize <= spindleSize ? gapSize : spindleSize
  117. }
  118. if (spindleSize > 0) {
  119. that._spindleOuter = that._spindleOuter || that._renderer.circle().append(that._rootElement);
  120. that._spindleInner = that._spindleInner || that._renderer.circle().append(that._rootElement);
  121. that._spindleOuter.attr({
  122. "class": "dxg-spindle-border",
  123. cx: options.x,
  124. cy: options.y,
  125. r: spindleSize / 2
  126. });
  127. that._spindleInner.attr({
  128. "class": "dxg-spindle-hole",
  129. cx: options.x,
  130. cy: options.y,
  131. r: gapSize / 2,
  132. fill: options.containerBackgroundColor
  133. })
  134. }
  135. },
  136. _render: function() {
  137. var that = this;
  138. that.callBase();
  139. that._renderSpindle()
  140. },
  141. _clear: function() {
  142. this.callBase();
  143. delete this._spindleOuter;
  144. delete this._spindleInner
  145. }
  146. });
  147. var rectangleNeedle = NeedleIndicator.inherit({
  148. _renderPointer: function() {
  149. var that = this;
  150. var options = that._options;
  151. var y2 = options.y - this._getRadius();
  152. var y1 = options.y - this._getIndentFromCenter();
  153. var x1 = options.x - options.width / 2;
  154. var x2 = x1 + _Number(options.width);
  155. that._element = that._element || that._renderer.path([], "area").append(that._rootElement);
  156. that._element.attr({
  157. points: [x1, y1, x1, y2, x2, y2, x2, y1]
  158. })
  159. }
  160. });
  161. var triangleNeedle = NeedleIndicator.inherit({
  162. _renderPointer: function() {
  163. var that = this;
  164. var options = that._options;
  165. var y2 = options.y - this._getRadius();
  166. var y1 = options.y - this._getIndentFromCenter();
  167. var x1 = options.x - options.width / 2;
  168. var x2 = options.x + options.width / 2;
  169. that._element = that._element || that._renderer.path([], "area").append(that._rootElement);
  170. that._element.attr({
  171. points: [x1, y1, options.x, y2, x2, y1]
  172. })
  173. }
  174. });
  175. var twoColorNeedle = NeedleIndicator.inherit({
  176. _renderPointer: function() {
  177. var that = this;
  178. var options = that._options;
  179. var x1 = options.x - options.width / 2;
  180. var x2 = options.x + options.width / 2;
  181. var y4 = options.y - this._getRadius();
  182. var y1 = options.y - this._getIndentFromCenter();
  183. var fraction = _Number(options.secondFraction) || 0;
  184. var y2;
  185. var y3;
  186. if (fraction >= 1) {
  187. y2 = y3 = y1
  188. } else {
  189. if (fraction <= 0) {
  190. y2 = y3 = y4
  191. } else {
  192. y3 = y4 + (y1 - y4) * fraction;
  193. y2 = y3 + _Number(options.space)
  194. }
  195. }
  196. that._firstElement = that._firstElement || that._renderer.path([], "area").append(that._rootElement);
  197. that._spaceElement = that._spaceElement || that._renderer.path([], "area").append(that._rootElement);
  198. that._secondElement = that._secondElement || that._renderer.path([], "area").append(that._rootElement);
  199. that._firstElement.attr({
  200. points: [x1, y1, x1, y2, x2, y2, x2, y1]
  201. });
  202. that._spaceElement.attr({
  203. points: [x1, y2, x1, y3, x2, y3, x2, y2],
  204. "class": "dxg-hole",
  205. fill: options.containerBackgroundColor
  206. });
  207. that._secondElement.attr({
  208. points: [x1, y3, x1, y4, x2, y4, x2, y3],
  209. "class": "dxg-part",
  210. fill: options.secondColor
  211. })
  212. },
  213. _clearPointer: function() {
  214. delete this._firstElement;
  215. delete this._secondElement;
  216. delete this._spaceElement
  217. }
  218. });
  219. var triangleMarker = SimpleIndicator.inherit({
  220. _isEnabled: function() {
  221. return this._options.length > 0 && this._options.width > 0
  222. },
  223. _isVisible: function(layout) {
  224. return layout.radius > 0
  225. },
  226. _render: function() {
  227. var that = this;
  228. var options = that._options;
  229. var x = options.x;
  230. var y1 = options.y - options.radius;
  231. var dx = options.width / 2 || 0;
  232. var y2 = y1 - _Number(options.length);
  233. that._element = that._element || that._renderer.path([], "area").append(that._rootElement);
  234. var settings = {
  235. points: [x, y1, x - dx, y2, x + dx, y2],
  236. stroke: "none",
  237. "stroke-width": 0,
  238. "stroke-linecap": "square"
  239. };
  240. if (options.space > 0) {
  241. settings["stroke-width"] = Math.min(options.space, options.width / 4) || 0;
  242. settings.stroke = settings["stroke-width"] > 0 ? options.containerBackgroundColor || "none" : "none"
  243. }
  244. that._element.attr(settings).sharp()
  245. },
  246. _clear: function() {
  247. delete this._element
  248. },
  249. _getTrackerSettings: function() {
  250. var options = this._options;
  251. var x = options.x;
  252. var y = options.y - options.radius - options.length / 2;
  253. var width = options.width / 2;
  254. var length = options.length / 2;
  255. width > 10 || (width = 10);
  256. length > 10 || (length = 10);
  257. return {
  258. points: [x - width, y - length, x - width, y + length, x + width, y + length, x + width, y - length]
  259. }
  260. },
  261. measure: function(layout) {
  262. return {
  263. min: layout.radius,
  264. max: layout.radius + _Number(this._options.length)
  265. }
  266. },
  267. getTooltipParameters: function() {
  268. var options = this._options;
  269. var cosSin = _getCosAndSin(this._actualPosition);
  270. var r = options.radius + options.length / 2;
  271. var parameters = this.callBase();
  272. parameters.x = options.x + cosSin.cos * r;
  273. parameters.y = options.y - cosSin.sin * r;
  274. parameters.offset = options.length / 2;
  275. return parameters
  276. }
  277. });
  278. var textCloud = BaseTextCloudMarker.inherit({
  279. _isEnabled: function() {
  280. return true
  281. },
  282. _isVisible: function(layout) {
  283. return layout.radius > 0
  284. },
  285. _getTextCloudOptions: function() {
  286. var that = this;
  287. var cosSin = _getCosAndSin(that._actualPosition);
  288. var nAngle = vizUtils.normalizeAngle(that._actualPosition);
  289. return {
  290. x: that._options.x + cosSin.cos * that._options.radius,
  291. y: that._options.y - cosSin.sin * that._options.radius,
  292. type: nAngle > 270 ? "left-top" : nAngle > 180 ? "top-right" : nAngle > 90 ? "right-bottom" : "bottom-left"
  293. }
  294. },
  295. measure: function(layout) {
  296. var that = this;
  297. var arrowLength = _Number(that._options.arrowLength) || 0;
  298. that._measureText();
  299. var verticalOffset = that._textFullHeight + arrowLength;
  300. var horizontalOffset = that._textFullWidth + arrowLength;
  301. return {
  302. min: layout.radius,
  303. max: layout.radius,
  304. horizontalOffset: horizontalOffset,
  305. verticalOffset: verticalOffset,
  306. inverseHorizontalOffset: horizontalOffset,
  307. inverseVerticalOffset: verticalOffset
  308. }
  309. }
  310. });
  311. var rangeBar = BaseRangeBar.inherit({
  312. _isEnabled: function() {
  313. return this._options.size > 0
  314. },
  315. _isVisible: function(layout) {
  316. return layout.radius - _Number(this._options.size) > 0
  317. },
  318. _createBarItem: function() {
  319. return this._renderer.arc().attr({
  320. "stroke-linejoin": "round"
  321. }).append(this._rootElement)
  322. },
  323. _createTracker: function() {
  324. return this._renderer.arc().attr({
  325. "stroke-linejoin": "round"
  326. })
  327. },
  328. _setBarSides: function() {
  329. var that = this;
  330. that._maxSide = that._options.radius;
  331. that._minSide = that._maxSide - _Number(that._options.size)
  332. },
  333. _getSpace: function() {
  334. var options = this._options;
  335. return options.space > 0 ? 180 * options.space / options.radius / Math.PI : 0
  336. },
  337. _isTextVisible: function() {
  338. var options = this._options.text || {};
  339. return options.indent > 0
  340. },
  341. _setTextItemsSides: function() {
  342. var that = this;
  343. var options = that._options;
  344. var indent = _Number(options.text.indent);
  345. that._lineFrom = options.y - options.radius;
  346. that._lineTo = that._lineFrom - indent;
  347. that._textRadius = options.radius + indent
  348. },
  349. _getPositions: function() {
  350. var that = this;
  351. var basePosition = that._basePosition;
  352. var actualPosition = that._actualPosition;
  353. var mainPosition1;
  354. var mainPosition2;
  355. if (basePosition >= actualPosition) {
  356. mainPosition1 = basePosition;
  357. mainPosition2 = actualPosition
  358. } else {
  359. mainPosition1 = actualPosition;
  360. mainPosition2 = basePosition
  361. }
  362. return {
  363. start: that._startPosition,
  364. end: that._endPosition,
  365. main1: mainPosition1,
  366. main2: mainPosition2,
  367. back1: Math.min(mainPosition1 + that._space, that._startPosition),
  368. back2: Math.max(mainPosition2 - that._space, that._endPosition)
  369. }
  370. },
  371. _buildItemSettings: function(from, to) {
  372. var that = this;
  373. return {
  374. x: that._options.x,
  375. y: that._options.y,
  376. innerRadius: that._minSide,
  377. outerRadius: that._maxSide,
  378. startAngle: to,
  379. endAngle: from
  380. }
  381. },
  382. _updateTextPosition: function() {
  383. var that = this;
  384. var cosSin = _getCosAndSin(that._actualPosition);
  385. var x = that._options.x + that._textRadius * cosSin.cos;
  386. var y = that._options.y - that._textRadius * cosSin.sin;
  387. x += cosSin.cos * that._textWidth * .6;
  388. y -= cosSin.sin * that._textHeight * .6;
  389. that._text.attr({
  390. x: x,
  391. y: y + that._textVerticalOffset
  392. })
  393. },
  394. _updateLinePosition: function() {
  395. var that = this;
  396. var x = that._options.x;
  397. var x1;
  398. var x2;
  399. if (that._basePosition > that._actualPosition) {
  400. x1 = x - 2;
  401. x2 = x
  402. } else {
  403. if (that._basePosition < that._actualPosition) {
  404. x1 = x;
  405. x2 = x + 2
  406. } else {
  407. x1 = x - 1;
  408. x2 = x + 1
  409. }
  410. }
  411. that._line.attr({
  412. points: [x1, that._lineFrom, x1, that._lineTo, x2, that._lineTo, x2, that._lineFrom]
  413. }).rotate(_convertAngleToRendererSpace(that._actualPosition), x, that._options.y).sharp()
  414. },
  415. _getTooltipPosition: function() {
  416. var that = this;
  417. var cosSin = _getCosAndSin((that._basePosition + that._actualPosition) / 2);
  418. var r = (that._minSide + that._maxSide) / 2;
  419. return {
  420. x: that._options.x + cosSin.cos * r,
  421. y: that._options.y - cosSin.sin * r
  422. }
  423. },
  424. measure: function(layout) {
  425. var that = this;
  426. var result = {
  427. min: layout.radius - _Number(that._options.size),
  428. max: layout.radius
  429. };
  430. that._measureText();
  431. if (that._hasText) {
  432. result.max += _Number(that._options.text.indent);
  433. result.horizontalOffset = that._textWidth;
  434. result.verticalOffset = that._textHeight
  435. }
  436. return result
  437. }
  438. });
  439. exports._default = rectangleNeedle;
  440. exports.rectangleneedle = rectangleNeedle;
  441. exports.triangleneedle = triangleNeedle;
  442. exports.twocolorneedle = twoColorNeedle;
  443. exports.trianglemarker = triangleMarker;
  444. exports.textcloud = textCloud;
  445. exports.rangebar = rangeBar;