UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

475 lines (473 loc) • 15.4 kB
/** * DevExtreme (esm/__internal/viz/gauges/base_indicators.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 { extend } from "../../core/utils/m_extend"; import { each } from "../../core/utils/m_iterator"; import { extractColor, patchFontOptions } from "../../viz/core/utils"; import { formatValue, getSampleText } from "../../viz/gauges/base_gauge"; export class BaseElement { constructor(parameters) { each(parameters, (name, value) => { this[`_${name}`] = value }); this._init() } dispose() { this._dispose(); const fields = this; each(fields, name => { fields[name] = null }); return this } getOffset() { return Number(this._options.offset) || 0 } } export class BaseIndicator extends BaseElement { _init() { this._rootElement = this._createRoot().linkOn(this._owner, { name: "value-indicator", after: "core" }); this._trackerElement = this._createTracker() } _dispose() { this._rootElement.linkOff() } _setupAnimation() { if (this._options.animation) { this._animation = { step: pos => { const animation = this._animation; this._actualValue = animation.start + animation.delta * pos; this._actualPosition = this._translator.translate(this._actualValue); this._move() }, duration: this._options.animation.duration > 0 ? Number(this._options.animation.duration) : 0, easing: this._options.animation.easing } } } _runAnimation(value) { const animation = this._animation; animation.start = this._actualValue; animation.delta = value - this._actualValue; this._rootElement.animate({ _: 0 }, { step: animation.step, duration: animation.duration, easing: animation.easing }) } _createRoot() { return this._renderer.g().attr({ class: this._className }) } _createTracker() { return this._renderer.path([], "area") } _getTrackerSettings() { return } clean() { if (this._animation) { this._rootElement.stopAnimation() } this._rootElement.linkRemove().clear(); this._clear(); this._tracker.detach(this._trackerElement); this._animation = null; this.enabled = null; this._options = null; return this } render(options) { this.type = options.type; this._options = options; const currentValue = this._translator.adjust(this._options.currentValue); this._currentValue = currentValue; this._actualValue = currentValue; this.enabled = this._isEnabled(); if (this.enabled) { this._setupAnimation(); this._rootElement.attr({ fill: extractColor(this._options.color) }).linkAppend(); this._tracker.attach(this._trackerElement, this, this._trackerInfo) } return this } resize(layout) { this._rootElement.clear(); this._clear(); this.visible = this._isVisible(layout); if (this.visible) { extend(this._options, layout); this._actualPosition = this._translator.translate(this._actualValue); this._render(); this._trackerElement.attr(this._getTrackerSettings()); this._move() } return this } value(arg, noAnimation) { const rootElement = this._rootElement; let visibility = null; if (void 0 === arg) { return this._currentValue } if (null === arg) { visibility = "hidden"; this._currentValue = arg } else { const val = this._translator.adjust(arg); if (this._currentValue !== val && Number.isFinite(val)) { this._currentValue = val; if (this.visible) { this._applyValue(val, noAnimation) } } } rootElement.attr({ visibility: visibility }); return this } _applyValue(value, noAnimation) { if (this._animation && !noAnimation) { this._runAnimation(value) } else { this._actualValue = value; this._actualPosition = this._translator.translate(value); this._move() } } } const RIGHT_BOTTOM = [0, -1, -1, 0, 0, 1, 1, 0]; const BOTTOM_RIGHT = [-1, 0, 0, -1, 1, 0, 0, 1]; const LEFT_BOTTOM = [0, -1, 1, 0, 0, 1, -1, 0]; const BOTTOM_LEFT = [1, 0, 0, -1, -1, 0, 0, 1]; const LEFT_TOP = [0, 1, 1, 0, 0, -1, -1, 0]; const TOP_LEFT = [1, 0, 0, 1, -1, 0, 0, -1]; const RIGHT_TOP = [0, 1, -1, 0, 0, -1, 1, 0]; const TOP_RIGHT = [-1, 0, 0, 1, 1, 0, 0, -1]; const COEFFICIENTS_MAP = { "right-bottom": RIGHT_BOTTOM, rb: RIGHT_BOTTOM, "bottom-right": BOTTOM_RIGHT, br: BOTTOM_RIGHT, "left-bottom": LEFT_BOTTOM, lb: LEFT_BOTTOM, "bottom-left": BOTTOM_LEFT, bl: BOTTOM_LEFT, "left-top": LEFT_TOP, lt: LEFT_TOP, "top-left": TOP_LEFT, tl: TOP_LEFT, "right-top": RIGHT_TOP, rt: RIGHT_TOP, "top-right": TOP_RIGHT, tr: TOP_RIGHT }; function getTextCloudInfo(options) { let { x: x, y: y } = options; const type = COEFFICIENTS_MAP[options.type]; const { cloudWidth: cloudWidth, cloudHeight: cloudHeight } = options; let tailWidth = options.tailLength; let tailHeight = options.tailLength; const cx = x; const cy = y; if (0 !== type[0]) { tailHeight = Math.min(tailHeight, cloudHeight / 3) } else { tailWidth = Math.min(tailWidth, cloudWidth / 3) } const points = [Math.round(x), Math.round(y)]; x += type[0] * (cloudWidth + tailWidth); y += type[1] * (cloudHeight + tailHeight); points.push(Math.round(x), Math.round(y)); x += type[2] * cloudWidth; y += type[3] * cloudHeight; points.push(Math.round(x), Math.round(y)); x += type[4] * cloudWidth; y += type[5] * cloudHeight; points.push(Math.round(x), Math.round(y)); x += type[6] * (cloudWidth - tailWidth); y += type[7] * (cloudHeight - tailHeight); points.push(Math.round(x), Math.round(y)); return { cx: Math.round(cx + type[0] * tailWidth + (type[0] + type[2]) * cloudWidth / 2), cy: Math.round(cy + type[1] * tailHeight + (type[1] + type[3]) * cloudHeight / 2), points: points } } export class BaseTextCloudMarker extends BaseIndicator { _move() { const options = this._options; const textCloudOptions = this._getTextCloudOptions(); const text = formatValue(this._actualValue, options.text); this._text.attr({ text: text }); const bBox = this._text.getBBox(); const { x: x, y: y } = textCloudOptions; const cloudWidth = (bBox.width || text.length * this._textUnitWidth) + 2 * options.horizontalOffset; const cloudHeight = (bBox.height || this._textHeight) + 2 * options.verticalOffset; const info = getTextCloudInfo({ x: x, y: y, cloudWidth: cloudWidth, cloudHeight: cloudHeight, tailLength: options.arrowLength, type: this._correctCloudType(textCloudOptions.type, { x: x, y: y }, { width: cloudWidth, height: cloudHeight }) }); this._text.attr({ x: info.cx, y: info.cy + this._textVerticalOffset }); this._cloud.attr({ points: info.points }); if (this._trackerElement) { this._trackerElement.attr({ points: info.points }) } } _measureText() { if (!this._textVerticalOffset) { const root = this._createRoot().append(this._owner); const sampleText = getSampleText(this._translator, this._options.text); const text = this._renderer.text(sampleText, 0, 0).attr({ align: "center" }).css(patchFontOptions(this._options.text.font)).append(root); const bBox = text.getBBox(); root.remove(); this._textVerticalOffset = -bBox.y - bBox.height / 2; this._textWidth = bBox.width; this._textHeight = bBox.height; this._textUnitWidth = this._textWidth / sampleText.length; this._textFullWidth = this._textWidth + 2 * this._options.horizontalOffset; this._textFullHeight = this._textHeight + 2 * this._options.verticalOffset } } _render() { this._measureText(); this._cloud = this._cloud || this._renderer.path([], "area").append(this._rootElement); this._text = this._text || this._renderer.text().append(this._rootElement); this._text.attr({ align: "center" }).css(patchFontOptions(this._options.text.font)) } _clear() { delete this._cloud; delete this._text } getTooltipParameters() { const position = this._getTextCloudOptions(); return { x: position.x, y: position.y, value: this._currentValue, color: this._options.color } } } export class BaseRangeBar extends BaseIndicator { _measureText() { this._hasText = this._isTextVisible(); if (this._hasText && !this._textVerticalOffset) { const root = this._createRoot().append(this._owner); const text = this._renderer.text(getSampleText(this._translator, this._options.text), 0, 0).attr({ class: "dxg-text", align: "center" }).css(patchFontOptions(this._options.text.font)).append(root); const bBox = text.getBBox(); root.remove(); this._textVerticalOffset = -bBox.y - bBox.height / 2; this._textWidth = bBox.width; this._textHeight = bBox.height } } _move() { this._updateBarItemsPositions(); if (this._hasText) { this._text.attr({ text: formatValue(this._actualValue, this._options.text) }); this._updateTextPosition(); this._updateLinePosition() } } _updateBarItems() { const options = this._options; const translator = this._translator; this._setBarSides(); this._startPosition = translator.translate(translator.getDomainStart()); this._endPosition = translator.translate(translator.getDomainEnd()); this._basePosition = translator.translate(options.baseValue); this._space = this._getSpace(); const backgroundColor = options.backgroundColor || "none"; const hasSpace = "none" !== backgroundColor && this._space > 0; if (!hasSpace) { this._space = 0 } const spaceColor = hasSpace ? options.containerBackgroundColor || "none" : "none"; this._backItem1.attr({ fill: backgroundColor }); this._backItem2.attr({ fill: backgroundColor }); this._spaceItem1.attr({ fill: spaceColor }); this._spaceItem2.attr({ fill: spaceColor }) } _getSpace() { return 0 } _updateTextItems() { if (this._hasText) { this._line = this._line || this._renderer.path([], "line").attr({ class: "dxg-main-bar", "stroke-linecap": "square" }).append(this._rootElement); this._text = this._text || this._renderer.text("", 0, 0).attr({ class: "dxg-text" }).append(this._rootElement); this._text.attr({ align: this._getTextAlign() }).css(this._getFontOptions()); this._setTextItemsSides() } else { if (this._line) { this._line.remove(); delete this._line } if (this._text) { this._text.remove(); delete this._text } } } _isTextVisible() { return false } _getTextAlign() { return "center" } _getFontOptions() { const options = this._options; let { font: font } = options.text; if (!font || !font.color) { font = extend({}, font, { color: options.color }) } return patchFontOptions(font) } _updateBarItemsPositions() { const positions = this._getPositions(); this._backItem1.attr(this._buildItemSettings(positions.start, positions.back1)); this._backItem2.attr(this._buildItemSettings(positions.back2, positions.end)); this._spaceItem1.attr(this._buildItemSettings(positions.back1, positions.main1)); this._spaceItem2.attr(this._buildItemSettings(positions.main2, positions.back2)); this._mainItem.attr(this._buildItemSettings(positions.main1, positions.main2)); if (this._trackerElement) { this._trackerElement.attr(this._buildItemSettings(positions.main1, positions.main2)) } } _render() { this._measureText(); if (!this._backItem1) { this._backItem1 = this._createBarItem(); this._backItem1.attr({ class: "dxg-back-bar" }) } if (!this._backItem2) { this._backItem2 = this._createBarItem(); this._backItem2.attr({ class: "dxg-back-bar" }) } if (!this._spaceItem1) { this._spaceItem1 = this._createBarItem(); this._spaceItem1.attr({ class: "dxg-space-bar" }) } if (!this._spaceItem2) { this._spaceItem2 = this._createBarItem(); this._spaceItem2.attr({ class: "dxg-space-bar" }) } if (!this._mainItem) { this._mainItem = this._createBarItem(); this._mainItem.attr({ class: "dxg-main-bar" }) } this._updateBarItems(); this._updateTextItems() } _clear() { delete this._backItem1; delete this._backItem2; delete this._spaceItem1; delete this._spaceItem2; delete this._mainItem; delete this._hasText; delete this._line; delete this._text } getTooltipParameters() { const position = this._getTooltipPosition(); return { x: position.x, y: position.y, value: this._currentValue, color: this._options.color, offset: 0 } } }