UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

359 lines (350 loc) • 14.2 kB
/** * DevExtreme (cjs/__internal/viz/gauges/circular_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/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyCircularMainLayout = applyCircularMainLayout; exports.default = void 0; exports.setupCircularCodomain = setupCircularCodomain; var _component_registrator = require("../../core/component_registrator"); var _m_extend = require("../../core/utils/m_extend"); var _m_iterator = require("../../core/utils/m_iterator"); var _object = require("../../core/utils/object"); var _center_template = require("../../viz/core/center_template"); var _helpers = require("../../viz/core/helpers"); var _utils = require("../../viz/core/utils"); var _base_gauge = require("../../viz/gauges/base_gauge"); var circularIndicators = _interopRequireWildcard(require("../../viz/gauges/circular_indicators")); var _circular_range_container = _interopRequireDefault(require("../../viz/gauges/circular_range_container")); var _common = require("../../viz/gauges/common"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) { var r = new WeakMap, n = new WeakMap } return (_interopRequireWildcard = function(e, t) { if (!t && e && e.__esModule) { return e } var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) { return f } if (o = t ? n : r) { if (o.has(e)) { return o.get(e) } o.set(e, f) } for (const t in e) { "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]) } return f })(e, t) } const { PI: PI } = Math; function getSides(startAngle, endAngle) { const startCosSin = (0, _utils.getCosAndSin)(startAngle); const endCosSin = (0, _utils.getCosAndSin)(endAngle); const startCos = startCosSin.cos; const startSin = startCosSin.sin; const endCos = endCosSin.cos; const endSin = endCosSin.sin; return { left: startSin <= 0 && endSin >= 0 || startSin <= 0 && endSin <= 0 && startCos <= endCos || startSin >= 0 && endSin >= 0 && startCos >= endCos ? -1 : Math.min(startCos, endCos, 0), right: startSin >= 0 && endSin <= 0 || startSin >= 0 && endSin >= 0 && startCos >= endCos || startSin <= 0 && endSin <= 0 && startCos <= endCos ? 1 : Math.max(startCos, endCos, 0), up: startCos <= 0 && endCos >= 0 || startCos <= 0 && endCos <= 0 && startSin >= endSin || startCos >= 0 && endCos >= 0 && startSin <= endSin ? -1 : -Math.max(startSin, endSin, 0), down: startCos >= 0 && endCos <= 0 || startCos >= 0 && endCos >= 0 && startSin <= endSin || startCos <= 0 && endCos <= 0 && startSin >= endSin ? 1 : -Math.min(startSin, endSin, 0) } } function getWidth(rect) { return rect.right - rect.left } function getHeight(rect) { return rect.bottom - rect.top } function selectRectByAspectRatio(srcRect, aspectRatio, margins) { const rect = (0, _m_extend.extend)({}, srcRect); const currentMargins = margins || {}; let width = 0; let height = 0; if (aspectRatio > 0) { rect.left += currentMargins.left || 0; rect.right -= currentMargins.right || 0; rect.top += currentMargins.top || 0; rect.bottom -= currentMargins.bottom || 0; if (getWidth(rect) > 0 && getHeight(rect) > 0) { const selfAspectRatio = getHeight(rect) / getWidth(rect); const fitByWidth = selfAspectRatio > 1 ? aspectRatio < selfAspectRatio : aspectRatio <= selfAspectRatio; if (fitByWidth) { width = getWidth(rect) } else { height = getHeight(rect) } if (!(width > 0)) { width = height / aspectRatio } if (!(height > 0)) { height = width * aspectRatio } width = (getWidth(rect) - width) / 2; height = (getHeight(rect) - height) / 2; rect.left += width; rect.right -= width; rect.top += height; rect.bottom -= height } else { rect.right = (rect.left + rect.right) / 2; rect.left = rect.right; rect.bottom = (rect.top + rect.bottom) / 2; rect.top = rect.bottom } } return rect } function setupCircularCodomain(gauge) { const geometry = gauge.option("geometry") || {}; const startAngle = isFinite(geometry.startAngle) ? (0, _utils.normalizeAngle)(geometry.startAngle) : 225; let endAngle = isFinite(geometry.endAngle) ? (0, _utils.normalizeAngle)(geometry.endAngle) : -45; const isFullCircle = Math.abs(startAngle - endAngle) < 1; if (isFullCircle || startAngle < endAngle) { endAngle -= 360 } const area = { x: 0, y: 0, radius: 100, startCoord: startAngle, endCoord: endAngle, sides: isFullCircle ? { left: -1, up: -1, right: 1, down: 1 } : getSides(startAngle, endAngle) }; gauge._area = area; gauge._translator.setCodomain(startAngle, endAngle) } function applyCircularMainLayout(gauge, measurements) { const area = gauge._area; const { sides: sides } = area; const margins = { left: (sides.left < -.1 ? measurements.horizontalMargin : measurements.inverseHorizontalMargin) || 0, right: (sides.right > .1 ? measurements.horizontalMargin : measurements.inverseHorizontalMargin) || 0, top: (sides.up < -.1 ? measurements.verticalMargin : measurements.inverseVerticalMargin) || 0, bottom: (sides.down > .1 ? measurements.verticalMargin : measurements.inverseVerticalMargin) || 0 }; const rect = selectRectByAspectRatio(gauge._innerRect, (sides.down - sides.up) / (sides.right - sides.left), margins); const fittedRadius = Math.min(getWidth(rect) / (sides.right - sides.left), getHeight(rect) / (sides.down - sides.up)); const x = rect.left - getWidth(rect) * sides.left / (sides.right - sides.left); const y = rect.top - getHeight(rect) * sides.up / (sides.down - sides.up); area.x = Math.round(x); area.y = Math.round(y); area.radius = fittedRadius - measurements.maxRadius + area.radius; rect.left -= margins.left; rect.right += margins.right; rect.top -= margins.top; rect.bottom += margins.bottom; gauge._innerRect = rect } class CircularGauge extends _common.dxGauge { _getThemeManagerOptions() { const options = super._getThemeManagerOptions(); options.subTheme = "_circular"; return options } _updateScaleTickIndent(scaleOptions) { const { indentFromTick: indentFromTick } = scaleOptions.label; const length = scaleOptions.tick.visible ? scaleOptions.tick.length : 0; const textParams = this._scale.measureLabels((0, _m_extend.extend)({}, this._canvas)); const scaleOrientation = scaleOptions.orientation; const tickCorrection = length; let indentFromAxis = indentFromTick; if (indentFromTick >= 0) { if ("outside" === scaleOrientation) { indentFromAxis += tickCorrection } else if ("center" === scaleOrientation) { indentFromAxis += tickCorrection / 2 } } else { const labelCorrection = Math.max(textParams.width, textParams.height); indentFromAxis -= labelCorrection; if ("inside" === scaleOrientation) { indentFromAxis -= tickCorrection } else if ("center" === scaleOrientation) { indentFromAxis -= tickCorrection / 2 } } scaleOptions.label.indentFromAxis = indentFromAxis; this._scale.updateOptions(scaleOptions) } _setupCodomain() { setupCircularCodomain(this) } _getCenter() { return this._getElementLayout() } _shiftScale(layout) { const scale = this._scale; const canvas = scale.getCanvas(); canvas.height = 2 * layout.radius; canvas.width = canvas.height; scale.draw(canvas); const centerCoords = scale.getCenter(); scale.shift({ right: layout.x - centerCoords.x, bottom: layout.y - centerCoords.y }) } _getScaleLayoutValue() { return this._area.radius } _getTicksOrientation(scaleOptions) { return scaleOptions.orientation } _getTicksCoefficients(options) { const coefs = { inner: 0, outer: 1 }; if ("inside" === options.orientation) { coefs.inner = 1; coefs.outer = 0 } else if ("center" === options.orientation) { coefs.inner = .5; coefs.outer = .5 } return coefs } _correctScaleIndents(result, indentFromTick, textParams) { if (indentFromTick >= 0) { result.horizontalOffset = indentFromTick + textParams.width; result.verticalOffset = indentFromTick + textParams.height } else { result.verticalOffset = 0; result.horizontalOffset = 0; result.min = result.min - (-indentFromTick + Math.max(textParams.width, textParams.height)) } result.inverseHorizontalOffset = textParams.width / 2; result.inverseVerticalOffset = textParams.height / 2 } _measureMainElements(elements, scaleMeasurement) { const { radius: radius } = this._area; let maxRadius = 0; let minRadius = 1 / 0; let maxHorizontalOffset = 0; let maxVerticalOffset = 0; let maxInverseHorizontalOffset = 0; let maxInverseVerticalOffset = 0; const scale = this._scale; (0, _m_iterator.each)(elements.concat(scale), (_, element) => { const bounds = element.measure ? element.measure({ radius: radius - element.getOffset() }) : scaleMeasurement; if (bounds.min > 0) { minRadius = Math.min(minRadius, bounds.min) } if (bounds.max > 0) { maxRadius = Math.max(maxRadius, bounds.max) } if (bounds.horizontalOffset > 0) { maxHorizontalOffset = Math.max(maxHorizontalOffset, bounds.max + bounds.horizontalOffset) } if (bounds.verticalOffset > 0) { maxVerticalOffset = Math.max(maxVerticalOffset, bounds.max + bounds.verticalOffset) } if (bounds.inverseHorizontalOffset > 0) { maxInverseHorizontalOffset = Math.max(maxInverseHorizontalOffset, bounds.inverseHorizontalOffset) } if (bounds.inverseVerticalOffset > 0) { maxInverseVerticalOffset = Math.max(maxInverseVerticalOffset, bounds.inverseVerticalOffset) } }); maxHorizontalOffset = Math.max(maxHorizontalOffset - maxRadius, 0); maxVerticalOffset = Math.max(maxVerticalOffset - maxRadius, 0); return { minRadius: minRadius, maxRadius: maxRadius, horizontalMargin: maxHorizontalOffset, verticalMargin: maxVerticalOffset, inverseHorizontalMargin: maxInverseHorizontalOffset, inverseVerticalMargin: maxInverseVerticalOffset } } _applyMainLayout(elements, scaleMeasurement) { applyCircularMainLayout(this, this._measureMainElements(elements, scaleMeasurement)) } _getElementLayout() { let offset = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0; return { x: this._area.x, y: this._area.y, radius: Math.round(this._area.radius - offset) } } _getApproximateScreenRange() { const area = this._area; let r = Math.min(this._canvas.width / (area.sides.right - area.sides.left), this._canvas.height / (area.sides.down - area.sides.up)); if (void 0 !== area.totalRadius && r > area.totalRadius) { r = area.totalRadius } r *= .8; return -this._translator.getCodomainRange() * r * PI / 180 } _getDefaultSize() { return { width: 300, height: 300 } } }(0, _helpers.setupWidgetPrototype)(CircularGauge, { _rootClass: "dxg-circular-gauge", _factoryMethods: { rangeContainer: "createCircularRangeContainer", indicator: "createCircularIndicator" }, _gridSpacingFactor: 17, _scaleTypes: { type: "polarAxes", drawingType: "circular" }, _factory: (0, _object.clone)(_base_gauge.BaseGauge.prototype._factory) }); const indicators = {}; CircularGauge.prototype._factory.indicators = indicators; CircularGauge.prototype._factory.createIndicator = (0, _common.createIndicatorCreator)(indicators); indicators._default = circularIndicators._default; indicators.rectangleneedle = circularIndicators.rectangleneedle; indicators.triangleneedle = circularIndicators.triangleneedle; indicators.twocolorneedle = circularIndicators.twocolorneedle; indicators.trianglemarker = circularIndicators.trianglemarker; indicators.textcloud = circularIndicators.textcloud; indicators.rangebar = circularIndicators.rangebar; CircularGauge.prototype._factory.RangeContainer = _circular_range_container.default; (0, _component_registrator.registerComponent)("dxCircularGauge", CircularGauge); CircularGauge.addPlugin(_center_template.plugins.gauge); var _default = exports.default = CircularGauge;