devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
336 lines (331 loc) • 12.9 kB
JavaScript
/**
* DevExtreme (esm/__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/
*/
import {
registerComponent
} from "../../core/component_registrator";
import {
extend
} from "../../core/utils/m_extend";
import {
each
} from "../../core/utils/m_iterator";
import {
clone
} from "../../core/utils/object";
import {
plugins as centerTemplatePlugins
} from "../../viz/core/center_template";
import {
setupWidgetPrototype
} from "../../viz/core/helpers";
import {
getCosAndSin,
normalizeAngle
} from "../../viz/core/utils";
import {
BaseGauge
} from "../../viz/gauges/base_gauge";
import * as circularIndicators from "../../viz/gauges/circular_indicators";
import CircularRangeContainer from "../../viz/gauges/circular_range_container";
import {
createIndicatorCreator,
dxGauge
} from "../../viz/gauges/common";
const {
PI: PI
} = Math;
function getSides(startAngle, endAngle) {
const startCosSin = getCosAndSin(startAngle);
const endCosSin = 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 = 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
}
export function setupCircularCodomain(gauge) {
const geometry = gauge.option("geometry") || {};
const startAngle = isFinite(geometry.startAngle) ? normalizeAngle(geometry.startAngle) : 225;
let endAngle = isFinite(geometry.endAngle) ? 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)
}
export 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 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(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;
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
}
}
}
setupWidgetPrototype(CircularGauge, {
_rootClass: "dxg-circular-gauge",
_factoryMethods: {
rangeContainer: "createCircularRangeContainer",
indicator: "createCircularIndicator"
},
_gridSpacingFactor: 17,
_scaleTypes: {
type: "polarAxes",
drawingType: "circular"
},
_factory: clone(BaseGauge.prototype._factory)
});
const indicators = {};
CircularGauge.prototype._factory.indicators = indicators;
CircularGauge.prototype._factory.createIndicator = 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 = CircularRangeContainer;
registerComponent("dxCircularGauge", CircularGauge);
CircularGauge.addPlugin(centerTemplatePlugins.gauge);
export default CircularGauge;