bullet.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * DevExtreme (viz/sparklines/bullet.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 BaseSparkline = require("./base_sparkline");
  12. var TARGET_MIN_Y = .02;
  13. var TARGET_MAX_Y = .98;
  14. var BAR_VALUE_MIN_Y = .1;
  15. var BAR_VALUE_MAX_Y = .9;
  16. var DEFAULT_CANVAS_WIDTH = 300;
  17. var DEFAULT_CANVAS_HEIGHT = 30;
  18. var DEFAULT_HORIZONTAL_MARGIN = 1;
  19. var DEFAULT_VERTICAL_MARGIN = 2;
  20. var _Number = Number;
  21. var _isFinite = isFinite;
  22. var dxBullet = BaseSparkline.inherit({
  23. _rootClassPrefix: "dxb",
  24. _rootClass: "dxb-bullet",
  25. _themeSection: "bullet",
  26. _defaultSize: {
  27. width: DEFAULT_CANVAS_WIDTH,
  28. height: DEFAULT_CANVAS_HEIGHT,
  29. left: DEFAULT_HORIZONTAL_MARGIN,
  30. right: DEFAULT_HORIZONTAL_MARGIN,
  31. top: DEFAULT_VERTICAL_MARGIN,
  32. bottom: DEFAULT_VERTICAL_MARGIN
  33. },
  34. _disposeWidgetElements: function() {
  35. delete this._zeroLevelPath;
  36. delete this._targetPath;
  37. delete this._barValuePath
  38. },
  39. _cleanWidgetElements: function() {
  40. this._zeroLevelPath.remove();
  41. this._targetPath.remove();
  42. this._barValuePath.remove()
  43. },
  44. _drawWidgetElements: function() {
  45. this._drawBullet();
  46. this._drawn()
  47. },
  48. _createHtmlElements: function() {
  49. var renderer = this._renderer;
  50. this._zeroLevelPath = renderer.path(void 0, "line").attr({
  51. "class": "dxb-zero-level",
  52. "stroke-linecap": "square"
  53. });
  54. this._targetPath = renderer.path(void 0, "line").attr({
  55. "class": "dxb-target",
  56. "stroke-linecap": "square"
  57. });
  58. this._barValuePath = renderer.path(void 0, "line").attr({
  59. "class": "dxb-bar-value",
  60. "stroke-linecap": "square"
  61. })
  62. },
  63. _prepareOptions: function() {
  64. var that = this;
  65. var options;
  66. var startScaleValue;
  67. var endScaleValue;
  68. var level;
  69. var value;
  70. var target;
  71. that._allOptions = options = that.callBase();
  72. var isValueUndefined = void 0 === that._allOptions.value;
  73. var isTargetUndefined = void 0 === that._allOptions.target;
  74. that._tooltipEnabled = !(isValueUndefined && isTargetUndefined);
  75. if (isValueUndefined) {
  76. that._allOptions.value = 0
  77. }
  78. if (isTargetUndefined) {
  79. that._allOptions.target = 0
  80. }
  81. options.value = value = _Number(options.value);
  82. options.target = target = _Number(options.target);
  83. if (void 0 === that._allOptions.startScaleValue) {
  84. that._allOptions.startScaleValue = target < value ? target : value;
  85. that._allOptions.startScaleValue = that._allOptions.startScaleValue < 0 ? that._allOptions.startScaleValue : 0
  86. }
  87. if (void 0 === that._allOptions.endScaleValue) {
  88. that._allOptions.endScaleValue = target > value ? target : value
  89. }
  90. options.startScaleValue = startScaleValue = _Number(options.startScaleValue);
  91. options.endScaleValue = endScaleValue = _Number(options.endScaleValue);
  92. if (endScaleValue < startScaleValue) {
  93. level = endScaleValue;
  94. that._allOptions.endScaleValue = startScaleValue;
  95. that._allOptions.startScaleValue = level;
  96. that._allOptions.inverted = true
  97. }
  98. },
  99. _updateRange: function() {
  100. var that = this;
  101. var options = that._allOptions;
  102. that._ranges = {
  103. arg: {
  104. invert: options.inverted,
  105. min: options.startScaleValue,
  106. max: options.endScaleValue,
  107. axisType: "continuous",
  108. dataType: "numeric"
  109. },
  110. val: {
  111. min: 0,
  112. max: 1,
  113. axisType: "continuous",
  114. dataType: "numeric"
  115. }
  116. }
  117. },
  118. _drawBullet: function() {
  119. var that = this;
  120. var options = that._allOptions;
  121. var isValidBounds = options.startScaleValue !== options.endScaleValue;
  122. var isValidMin = _isFinite(options.startScaleValue);
  123. var isValidMax = _isFinite(options.endScaleValue);
  124. var isValidValue = _isFinite(options.value);
  125. var isValidTarget = _isFinite(options.target);
  126. if (isValidBounds && isValidMax && isValidMin && isValidTarget && isValidValue) {
  127. this._drawBarValue();
  128. this._drawTarget();
  129. this._drawZeroLevel()
  130. }
  131. },
  132. _getTargetParams: function() {
  133. var that = this;
  134. var options = that._allOptions;
  135. var translatorY = that._valueAxis.getTranslator();
  136. var x = that._argumentAxis.getTranslator().translate(options.target);
  137. return {
  138. points: [x, translatorY.translate(TARGET_MIN_Y), x, translatorY.translate(TARGET_MAX_Y)],
  139. stroke: options.targetColor,
  140. "stroke-width": options.targetWidth
  141. }
  142. },
  143. _getBarValueParams: function() {
  144. var that = this;
  145. var options = that._allOptions;
  146. var translatorX = that._argumentAxis.getTranslator();
  147. var translatorY = that._valueAxis.getTranslator();
  148. var startLevel = options.startScaleValue;
  149. var endLevel = options.endScaleValue;
  150. var value = options.value;
  151. var y2 = translatorY.translate(BAR_VALUE_MIN_Y);
  152. var y1 = translatorY.translate(BAR_VALUE_MAX_Y);
  153. var x1;
  154. var x2;
  155. if (value > 0) {
  156. x1 = startLevel <= 0 ? 0 : startLevel;
  157. x2 = value >= endLevel ? endLevel : value < x1 ? x1 : value
  158. } else {
  159. x1 = endLevel >= 0 ? 0 : endLevel;
  160. x2 = value < startLevel ? startLevel : value > x1 ? x1 : value
  161. }
  162. x1 = translatorX.translate(x1);
  163. x2 = translatorX.translate(x2);
  164. return {
  165. points: [x1, y1, x2, y1, x2, y2, x1, y2],
  166. fill: options.color
  167. }
  168. },
  169. _getCorrectCanvas: function() {
  170. return this._canvas
  171. },
  172. _getZeroLevelParams: function() {
  173. var that = this;
  174. var translatorY = that._valueAxis.getTranslator();
  175. var x = that._argumentAxis.getTranslator().translate(0);
  176. return {
  177. points: [x, translatorY.translate(TARGET_MIN_Y), x, translatorY.translate(TARGET_MAX_Y)],
  178. stroke: that._allOptions.targetColor,
  179. "stroke-width": 1
  180. }
  181. },
  182. _drawZeroLevel: function() {
  183. var that = this;
  184. var options = that._allOptions;
  185. if (0 > options.endScaleValue || 0 < options.startScaleValue || !options.showZeroLevel) {
  186. return
  187. }
  188. that._zeroLevelPath.attr(that._getZeroLevelParams()).sharp().append(that._renderer.root)
  189. },
  190. _drawTarget: function() {
  191. var that = this;
  192. var options = that._allOptions;
  193. var target = options.target;
  194. if (target > options.endScaleValue || target < options.startScaleValue || !options.showTarget) {
  195. return
  196. }
  197. that._targetPath.attr(that._getTargetParams()).sharp().append(that._renderer.root)
  198. },
  199. _drawBarValue: function() {
  200. this._barValuePath.attr(this._getBarValueParams()).append(this._renderer.root)
  201. },
  202. _getTooltipCoords: function() {
  203. var canvas = this._canvas;
  204. var rootOffset = this._renderer.getRootOffset();
  205. var bBox = this._barValuePath.getBBox();
  206. return {
  207. x: bBox.x + bBox.width / 2 + rootOffset.left,
  208. y: canvas.height / 2 + rootOffset.top
  209. }
  210. },
  211. _getTooltipData: function() {
  212. var that = this;
  213. var tooltip = that._tooltip;
  214. var options = that._allOptions;
  215. var value = options.value;
  216. var target = options.target;
  217. var valueText = tooltip.formatValue(value);
  218. var targetText = tooltip.formatValue(target);
  219. return {
  220. originalValue: value,
  221. originalTarget: target,
  222. value: valueText,
  223. target: targetText,
  224. valueText: ["Actual Value:", valueText, "Target Value:", targetText]
  225. }
  226. },
  227. _isTooltipEnabled: function() {
  228. return this._tooltipEnabled
  229. }
  230. });
  231. each(["color", "targetColor", "targetWidth", "showTarget", "showZeroLevel", "value", "target", "startScaleValue", "endScaleValue"], function(_, name) {
  232. dxBullet.prototype._optionChangesMap[name] = "OPTIONS"
  233. });
  234. require("../../core/component_registrator")("dxBullet", dxBullet);
  235. module.exports = dxBullet;