UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

255 lines (252 loc) • 8.63 kB
/** * DevExtreme (esm/__internal/viz/sparklines/bullet.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { registerComponent as componentRegistrator } from "../../core/component_registrator"; import { setupWidgetPrototype } from "../../viz/core/helpers"; import BaseSparkline from "../../viz/sparklines/base_sparkline"; const TARGET_MIN_Y = .02; const TARGET_MAX_Y = .98; const BAR_VALUE_MIN_Y = .1; const BAR_VALUE_MAX_Y = .9; const DEFAULT_CANVAS_WIDTH = 300; const DEFAULT_CANVAS_HEIGHT = 30; const DEFAULT_HORIZONTAL_MARGIN = 1; const DEFAULT_VERTICAL_MARGIN = 2; function getPositiveBarBounds(value, startLevel, endLevel) { const x1 = startLevel <= 0 ? 0 : startLevel; let x2 = value; if (value >= endLevel) { x2 = endLevel } else if (value < x1) { x2 = x1 } return { x1: x1, x2: x2 } } function getNegativeBarBounds(value, startLevel, endLevel) { const x1 = endLevel >= 0 ? 0 : endLevel; let x2 = value; if (value < startLevel) { x2 = startLevel } else if (value > x1) { x2 = x1 } return { x1: x1, x2: x2 } } class Bullet extends BaseSparkline { _disposeWidgetElements() { delete this._zeroLevelPath; delete this._targetPath; delete this._barValuePath } _cleanWidgetElements() { this._zeroLevelPath.remove(); this._targetPath.remove(); this._barValuePath.remove() } _drawWidgetElements() { this._drawBullet(); this._drawn() } _createHtmlElements() { const renderer = this._renderer; this._zeroLevelPath = renderer.path(void 0, "line").attr({ class: "dxb-zero-level", "stroke-linecap": "square" }); this._targetPath = renderer.path(void 0, "line").attr({ class: "dxb-target", "stroke-linecap": "square" }); this._barValuePath = renderer.path(void 0, "line").attr({ class: "dxb-bar-value", "stroke-linecap": "square" }) } _prepareOptions() { const options = super._prepareOptions(); this._allOptions = options; const isValueUndefined = void 0 === options.value; const isTargetUndefined = void 0 === options.target; this._tooltipEnabled = !(isValueUndefined && isTargetUndefined); if (isValueUndefined) { options.value = 0 } if (isTargetUndefined) { options.target = 0 } const value = Number(options.value); const target = Number(options.target); options.value = value; options.target = target; if (void 0 === options.startScaleValue) { options.startScaleValue = target < value ? target : value; options.startScaleValue = options.startScaleValue < 0 ? options.startScaleValue : 0 } if (void 0 === options.endScaleValue) { options.endScaleValue = target > value ? target : value } const startScaleValue = Number(options.startScaleValue); const endScaleValue = Number(options.endScaleValue); options.startScaleValue = startScaleValue; options.endScaleValue = endScaleValue; if (endScaleValue < startScaleValue) { options.endScaleValue = startScaleValue; options.startScaleValue = endScaleValue; options.inverted = true } } _updateRange() { const options = this._allOptions; this._ranges = { arg: { invert: options.rtlEnabled ? !options.inverted : options.inverted, min: options.startScaleValue, max: options.endScaleValue, axisType: "continuous", dataType: "numeric" }, val: { min: 0, max: 1, axisType: "continuous", dataType: "numeric" } } } _drawBullet() { const options = this._allOptions; const isValidBounds = options.startScaleValue !== options.endScaleValue; const isValidMin = isFinite(options.startScaleValue); const isValidMax = isFinite(options.endScaleValue); const isValidValue = isFinite(options.value); const isValidTarget = isFinite(options.target); if (isValidBounds && isValidMax && isValidMin && isValidTarget && isValidValue) { this._drawBarValue(); this._drawTarget(); this._drawZeroLevel() } } _getTargetParams() { const options = this._allOptions; const translatorY = this._valueAxis.getTranslator(); const x = this._argumentAxis.getTranslator().translate(options.target); return { points: [x, translatorY.translate(.02), x, translatorY.translate(.98)], stroke: options.targetColor, "stroke-width": options.targetWidth } } _getBarValueParams() { const options = this._allOptions; const translatorX = this._argumentAxis.getTranslator(); const translatorY = this._valueAxis.getTranslator(); const startLevel = options.startScaleValue; const endLevel = options.endScaleValue; const { value: value } = options; const y2 = translatorY.translate(.1); const y1 = translatorY.translate(.9); const bounds = value > 0 ? getPositiveBarBounds(value, startLevel, endLevel) : getNegativeBarBounds(value, startLevel, endLevel); const x1 = translatorX.translate(bounds.x1); const x2 = translatorX.translate(bounds.x2); return { points: [x1, y1, x2, y1, x2, y2, x1, y2], fill: options.color } } _getCorrectCanvas() { return this._canvas } _getZeroLevelParams() { const translatorY = this._valueAxis.getTranslator(); const x = this._argumentAxis.getTranslator().translate(0); return { points: [x, translatorY.translate(.02), x, translatorY.translate(.98)], stroke: this._allOptions.targetColor, "stroke-width": 1 } } _drawZeroLevel() { const options = this._allOptions; if (options.endScaleValue < 0 || options.startScaleValue > 0 || !options.showZeroLevel) { return } this._zeroLevelPath.attr(this._getZeroLevelParams()).sharp().append(this._renderer.root) } _drawTarget() { const options = this._allOptions; const { target: target } = options; if (target > options.endScaleValue || target < options.startScaleValue || !options.showTarget) { return } this._targetPath.attr(this._getTargetParams()).sharp().append(this._renderer.root) } _drawBarValue() { this._barValuePath.attr(this._getBarValueParams()).append(this._renderer.root) } _getTooltipCoords() { const canvas = this._canvas; const rootOffset = this._renderer.getRootOffset(); const bBox = this._barValuePath.getBBox(); return { x: bBox.x + bBox.width / 2 + rootOffset.left, y: canvas.height / 2 + rootOffset.top } } _getTooltipData() { const tooltip = this._tooltip; const options = this._allOptions; const { value: value, target: target } = options; const valueText = tooltip.formatValue(value); const targetText = tooltip.formatValue(target); return { originalValue: value, originalTarget: target, value: valueText, target: targetText, valueText: ["Actual Value:", valueText, "Target Value:", targetText] } } _isTooltipEnabled() { return this._tooltipEnabled } } setupWidgetPrototype(Bullet, { _rootClassPrefix: "dxb", _rootClass: "dxb-bullet", _themeSection: "bullet", _defaultSize: { width: 300, height: 30, left: 1, right: 1, top: 2, bottom: 2 } }); ["color", "targetColor", "targetWidth", "showTarget", "showZeroLevel", "value", "target", "startScaleValue", "endScaleValue"].forEach(name => { Bullet.prototype._optionChangesMap[name] = "OPTIONS" }); componentRegistrator("dxBullet", Bullet); export default Bullet;