UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

277 lines (275 loc) • 9.65 kB
/** * DevExtreme (esm/__internal/viz/gauges/base_gauge.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 formatHelper from "../../core/format_helper"; import { noop } from "../../core/utils/m_common"; import { extend } from "../../core/utils/m_extend"; import BaseWidget from "../../viz/core/base_widget"; import { plugin as exportPlugin } from "../../viz/core/export"; import { setupWidgetPrototype } from "../../viz/core/helpers"; import { plugin as loadingIndicatorPlugin } from "../../viz/core/loading_indicator"; import { plugin as titlePlugin } from "../../viz/core/title"; import { plugin as tooltipPlugin } from "../../viz/core/tooltip"; import { getAppropriateFormat } from "../../viz/core/utils"; import themeManagerModule from "../../viz/gauges/theme_manager"; import Tracker from "../../viz/gauges/tracker"; import { Translator1D } from "../../viz/translators/translator1d"; const { format: format } = formatHelper; export class BaseGauge extends BaseWidget { _createThemeManager() { return new themeManagerModule.ThemeManager(this._getThemeManagerOptions()) } _initCore() { const { root: root } = this._renderer; this._valueChangingLocker = 0; this._translator = this._factory.createTranslator(); this._tracker = this._factory.createTracker({ renderer: this._renderer, container: root }); this._setTrackerCallbacks() } _beginValueChanging() { this._resetIsReady(); this._onBeginUpdate(); this._valueChangingLocker += 1 } _endValueChanging() { this._valueChangingLocker -= 1; if (0 === this._valueChangingLocker) { this._drawn() } } _setTrackerCallbacks() { const renderer = this._renderer; const tooltip = this._tooltip; this._tracker.setCallbacks({ "tooltip-show": (target, info, callback) => { const tooltipParameters = target.getTooltipParameters(); const offset = renderer.getRootOffset(); const formatObject = extend({ value: tooltipParameters.value, valueText: tooltip.formatValue(tooltipParameters.value), color: tooltipParameters.color }, info); return tooltip.show(formatObject, { x: tooltipParameters.x + offset.left, y: tooltipParameters.y + offset.top, offset: tooltipParameters.offset }, { target: info }, void 0, callback) }, "tooltip-hide": () => tooltip.hide() }) } _dispose() { this._cleanCore(); super._dispose() } _disposeCore() { this._themeManager.dispose(); this._tracker.dispose(); this._tracker = null; this._translator = null } _cleanCore() { this._tracker.deactivate(); this._noAnimation = false; this._cleanContent() } _renderCore() { if (!this._isValidDomain) { return } this._renderContent(); this._renderGraphicObjects(); this._tracker.setTooltipState(this._tooltip.isEnabled()); this._tracker.activate(); this._noAnimation = false } _applyChanges() { super._applyChanges(); this._noAnimation = false; this._resizing = false } _setContentSize() { this._noAnimation = 2 === this._changes.count(); this._resizing = this._noAnimation; super._setContentSize() } _getChangesRequireCoreUpdate() { return ["DOMAIN", "MOSTLY_TOTAL", "EXPORT"] } _isTitleBBoxChanged() { var _this$_titleBBoxCache, _this$_titleBBoxCache2, _this$_titleBBoxCache3; const titleBBox = this._title.getLayoutOptions(); const hasTitleHeightChanged = titleBBox.height !== (null === (_this$_titleBBoxCache = this._titleBBoxCache) || void 0 === _this$_titleBBoxCache ? void 0 : _this$_titleBBoxCache.height); const hasTitleYChanged = titleBBox.y !== (null === (_this$_titleBBoxCache2 = this._titleBBoxCache) || void 0 === _this$_titleBBoxCache2 ? void 0 : _this$_titleBBoxCache2.y); const hasVerticalAlignmentChanged = titleBBox.verticalAlignment !== (null === (_this$_titleBBoxCache3 = this._titleBBoxCache) || void 0 === _this$_titleBBoxCache3 ? void 0 : _this$_titleBBoxCache3.verticalAlignment); this._titleBBoxCache = null; return hasTitleHeightChanged || hasTitleYChanged || hasVerticalAlignmentChanged } _forceCoreUpdate() { const isTriggeredByTitleOnly = this._changes.has("TITLE") && !this._getChangesRequireCoreUpdate().some(change => this._changes.has(change)); if (isTriggeredByTitleOnly) { return this._isTitleBBoxChanged() } return true } _applySize(rect) { this._innerRect = { left: rect[0], top: rect[1], right: rect[2], bottom: rect[3] }; const layoutCache = this._layout._cache; if (this._forceCoreUpdate()) { this._cleanCore(); this._renderCore() } this._layout._cache = this._layout._cache || layoutCache; return [rect[0], this._innerRect.top, rect[2], this._innerRect.bottom] } _change_DOMAIN() { this._setupDomain() } _change_MOSTLY_TOTAL() { this._applyMostlyTotalChange() } _setupDomain() { this._setupDomainCore(); this._isValidDomain = isFinite(1 / (this._translator.getDomain()[1] - this._translator.getDomain()[0])); if (!this._isValidDomain) { this._incidentOccurred("W2301") } this._change(["MOSTLY_TOTAL"]) } _applyMostlyTotalChange() { this._setupCodomain(); this._setupAnimationSettings(); this._setupDefaultFormat(); this._change(["LAYOUT"]) } _setupAnimationSettings() { let option = this.option("animation"); this._animationSettings = null; if (void 0 === option || option) { option = extend({ enabled: true, duration: 1e3, easing: "easeOutCubic" }, option); if (option.enabled && option.duration > 0) { this._animationSettings = { duration: Number(option.duration), easing: option.easing } } } this._containerBackgroundColor = this.option("containerBackgroundColor") || this._themeManager.theme().containerBackgroundColor } _setupDefaultFormat() { const domain = this._translator.getDomain(); this._defaultFormatOptions = getAppropriateFormat(domain[0], domain[1], this._getApproximateScreenRange()) } } setupWidgetPrototype(BaseGauge, { _rootClassPrefix: "dxg", _themeSection: "gauge", _titleBBoxCache: null, _initialChanges: ["DOMAIN"], _themeDependentChanges: ["DOMAIN"], _optionChangesMap: { subtitle: "MOSTLY_TOTAL", indicator: "MOSTLY_TOTAL", geometry: "MOSTLY_TOTAL", animation: "MOSTLY_TOTAL", startValue: "DOMAIN", endValue: "DOMAIN" }, _optionChangesOrder: ["DOMAIN", "MOSTLY_TOTAL"], _updateExtraElements: noop, _factory: { createTranslator: () => new Translator1D, createTracker: parameters => new Tracker(parameters) } }); export function formatValue(value, options, extra) { const normalizedValue = Object.is(value, -0) ? 0 : value; const formatOptions = options ?? {}; const text = format(normalizedValue, formatOptions.format); if ("function" === typeof formatOptions.customizeText) { const formatObject = extend({ value: normalizedValue, valueText: text }, extra); return String(formatOptions.customizeText.call(formatObject, formatObject)) } return text } export function getSampleText(translator, options) { const text1 = formatValue(translator.getDomainStart(), options); const text2 = formatValue(translator.getDomainEnd(), options); return text1.length >= text2.length ? text1 : text2 } function compareArraysElements(array1, array2) { for (let i = 0; i < array1.length; i += 1) { const bothValuesAreNaN = Number.isNaN(array1[i]) && Number.isNaN(array2[i]); if (!bothValuesAreNaN && array1[i] !== array2[i]) { return false } } return true } export function compareArrays(array1, array2) { return Boolean(array1 && array2 && array1.length === array2.length) && compareArraysElements(array1, array2) } BaseGauge.addPlugin(exportPlugin); BaseGauge.addPlugin(titlePlugin); BaseGauge.addPlugin(tooltipPlugin); BaseGauge.addPlugin(loadingIndicatorPlugin); const { _setTooltipOptions: setTooltipOptions } = BaseGauge.prototype; BaseGauge.prototype._setTooltipOptions = function() { setTooltipOptions.call(this); if (this._tracker) { this._tracker.setTooltipState(this._tooltip.isEnabled()) } }; const { _change_TITLE: changeTitle } = BaseGauge.prototype; BaseGauge.prototype._change_TITLE = function() { this._titleBBoxCache = Object.assign({}, this._title.getLayoutOptions()); changeTitle.call(this) };