UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

465 lines (460 loc) • 15.5 kB
/** * DevExtreme (esm/__internal/viz/gauges/circular_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 { convertAngleToRendererSpace, getCosAndSin, normalizeAngle } from "../../viz/core/utils"; import { BaseIndicator, BaseRangeBar, BaseTextCloudMarker } from "../../viz/gauges/base_indicators"; function correctRadius(layout, size) { if (layout && layout.radius - size <= 0) { layout.radius = size + 1 } return layout } function getTextCloudType(angle) { if (angle > 270) { return "left-top" } if (angle > 180) { return "top-right" } if (angle > 90) { return "right-bottom" } return "bottom-left" } function getTwoColorBounds(fraction, y1, y4, space) { if (fraction >= 1) { return [y1, y1] } if (fraction <= 0) { return [y4, y4] } const y3 = y4 + (y1 - y4) * fraction; return [y3 + space, y3] } function getLineSides(x, basePosition, actualPosition) { if (basePosition > actualPosition) { return [x - 2, x] } if (basePosition < actualPosition) { return [x, x + 2] } return [x - 1, x + 1] } class SimpleIndicator extends BaseIndicator { _move() { const options = this._options; const angle = convertAngleToRendererSpace(this._actualPosition); this._rootElement.rotate(angle, options.x, options.y); if (this._trackerElement) { this._trackerElement.rotate(angle, options.x, options.y) } } _isEnabled() { return this._options.width > 0 } _isVisible(layout) { return layout.radius - Number(this._options.indentFromCenter) > 0 } _getTrackerSettings() { const options = this._options; const radius = this._getRadius(); const indentFromCenter = this._getIndentFromCenter(); const { x: x } = options; const y = options.y - (radius + indentFromCenter) / 2; let width = options.width / 2; let length = (radius - indentFromCenter) / 2; if (!(width > 10)) { width = 10 } if (!(length > 10)) { length = 10 } return { points: [x - width, y - length, x - width, y + length, x + width, y + length, x + width, y - length] } } _clearPointer() { delete this._element } _clear() { this._clearPointer() } _getIndentFromCenter() { return Number(this._options.indentFromCenter) || 0 } _getRadius() { return 0 } measure(layout) { const result = { max: layout.radius }; if (this._options.indentFromCenter < 0) { result.inverseVerticalOffset = -Number(this._options.indentFromCenter); result.inverseHorizontalOffset = result.inverseVerticalOffset } return result } getTooltipParameters() { const options = this._options; const cosSin = getCosAndSin(this._actualPosition); const r = (this._getRadius() + this._getIndentFromCenter()) / 2; return { x: options.x + cosSin.cos * r, y: options.y - cosSin.sin * r, value: this._currentValue, color: options.color, offset: options.width / 2 } } } class NeedleIndicator extends SimpleIndicator { _isVisible(layout) { const indentFromCenter = this._adjustOffset(Number(this._options.indentFromCenter), layout.radius); const offset = this._adjustOffset(Number(this._options.offset), layout.radius); return layout.radius - indentFromCenter - offset > 0 } getOffset() { return 0 } _adjustOffset(value, radius) { const minRadius = Number(this._options.beginAdaptingAtRadius); const diff = radius / minRadius; let result = value; if (diff < 1) { result = Math.floor(value * diff) } return result || 0 } _getIndentFromCenter() { return this._adjustOffset(Number(this._options.indentFromCenter), this._options.radius) } _getRadius() { const options = this._options; return options.radius - this._adjustOffset(Number(options.offset), options.radius) } _renderSpindle() { const options = this._options; const { radius: radius } = options; const spindleSize = 2 * this._adjustOffset(Number(options.spindleSize) / 2, radius); let gapSize = 2 * this._adjustOffset(Number(options.spindleGapSize) / 2, radius) || 0; if (gapSize > 0) { gapSize = gapSize <= spindleSize ? gapSize : spindleSize } if (spindleSize > 0) { this._spindleOuter = this._spindleOuter || this._renderer.circle().append(this._rootElement); this._spindleInner = this._spindleInner || this._renderer.circle().append(this._rootElement); this._spindleOuter.attr({ class: "dxg-spindle-border", cx: options.x, cy: options.y, r: spindleSize / 2 }); this._spindleInner.attr({ class: "dxg-spindle-hole", cx: options.x, cy: options.y, r: gapSize / 2, fill: options.containerBackgroundColor }) } } _render() { this._renderPointer(); this._renderSpindle() } _clear() { super._clear(); delete this._spindleOuter; delete this._spindleInner } } class RectangleNeedle extends NeedleIndicator { _renderPointer() { const options = this._options; const y2 = options.y - this._getRadius(); const y1 = options.y - this._getIndentFromCenter(); const x1 = options.x - options.width / 2; const x2 = x1 + Number(options.width); this._element = this._element || this._renderer.path([], "area").append(this._rootElement); this._element.attr({ points: [x1, y1, x1, y2, x2, y2, x2, y1] }) } } class TriangleNeedle extends NeedleIndicator { _renderPointer() { const options = this._options; const y2 = options.y - this._getRadius(); const y1 = options.y - this._getIndentFromCenter(); const x1 = options.x - options.width / 2; const x2 = options.x + options.width / 2; this._element = this._element || this._renderer.path([], "area").append(this._rootElement); this._element.attr({ points: [x1, y1, options.x, y2, x2, y1] }) } } class TwoColorNeedle extends NeedleIndicator { _renderPointer() { const options = this._options; const x1 = options.x - options.width / 2; const x2 = options.x + options.width / 2; const y4 = options.y - this._getRadius(); const y1 = options.y - this._getIndentFromCenter(); const fraction = Number(options.secondFraction) || 0; const [y2, y3] = getTwoColorBounds(fraction, y1, y4, Number(options.space)); this._firstElement = this._firstElement || this._renderer.path([], "area").append(this._rootElement); this._spaceElement = this._spaceElement || this._renderer.path([], "area").append(this._rootElement); this._secondElement = this._secondElement || this._renderer.path([], "area").append(this._rootElement); this._firstElement.attr({ points: [x1, y1, x1, y2, x2, y2, x2, y1] }); this._spaceElement.attr({ points: [x1, y2, x1, y3, x2, y3, x2, y2], class: "dxg-hole", fill: options.containerBackgroundColor }); this._secondElement.attr({ points: [x1, y3, x1, y4, x2, y4, x2, y3], class: "dxg-part", fill: options.secondColor }) } _clearPointer() { delete this._firstElement; delete this._secondElement; delete this._spaceElement } } class TriangleMarker extends SimpleIndicator { _isEnabled() { return this._options.length > 0 && this._options.width > 0 } _isVisible() { return true } resize(layout) { return super.resize(correctRadius(layout, 0)) } _render() { const options = this._options; const { x: x } = options; const y1 = options.y - options.radius; const dx = options.width / 2 || 0; const y2 = y1 - Number(options.length); this._element = this._element || this._renderer.path([], "area").append(this._rootElement); const settings = { points: [x, y1, x - dx, y2, x + dx, y2], stroke: "none", "stroke-width": 0, "stroke-linecap": "square" }; if (options.space > 0) { settings["stroke-width"] = Math.min(options.space, options.width / 4) || 0; settings.stroke = settings["stroke-width"] > 0 ? options.containerBackgroundColor || "none" : "none" } this._element.attr(settings).sharp() } _clear() { delete this._element } _getTrackerSettings() { const options = this._options; const { x: x } = options; const y = options.y - options.radius - options.length / 2; let width = options.width / 2; let length = options.length / 2; if (!(width > 10)) { width = 10 } if (!(length > 10)) { length = 10 } return { points: [x - width, y - length, x - width, y + length, x + width, y + length, x + width, y - length] } } measure(layout) { return { min: layout.radius, max: layout.radius + Number(this._options.length) } } getTooltipParameters() { const options = this._options; const cosSin = getCosAndSin(this._actualPosition); const r = options.radius + options.length / 2; const parameters = super.getTooltipParameters(); parameters.x = options.x + cosSin.cos * r; parameters.y = options.y - cosSin.sin * r; parameters.offset = options.length / 2; return parameters } } class TextCloud extends BaseTextCloudMarker { _isEnabled() { return true } _isVisible() { return true } resize(layout) { return super.resize(correctRadius(layout, 0)) } _getTextCloudOptions() { const cosSin = getCosAndSin(this._actualPosition); const nAngle = normalizeAngle(this._actualPosition); return { x: this._options.x + cosSin.cos * this._options.radius, y: this._options.y - cosSin.sin * this._options.radius, type: getTextCloudType(nAngle) } } _correctCloudType(type) { return type } measure(layout) { const arrowLength = Number(this._options.arrowLength) || 0; this._measureText(); const verticalOffset = this._textFullHeight + arrowLength; const horizontalOffset = this._textFullWidth + arrowLength; return { min: layout.radius, max: layout.radius, horizontalOffset: horizontalOffset, verticalOffset: verticalOffset, inverseHorizontalOffset: horizontalOffset, inverseVerticalOffset: verticalOffset } } } class RangeBar extends BaseRangeBar { _isEnabled() { return this._options.size > 0 } _isVisible() { return true } resize(layout) { return super.resize(correctRadius(layout, Number(this._options.size))) } _createBarItem() { return this._renderer.arc().attr({ "stroke-linejoin": "round" }).append(this._rootElement) } _createTracker() { return this._renderer.arc().attr({ "stroke-linejoin": "round" }) } _setBarSides() { this._maxSide = this._options.radius; this._minSide = this._maxSide - Number(this._options.size) } _getSpace() { const options = this._options; return options.space > 0 ? 180 * options.space / options.radius / Math.PI : 0 } _isTextVisible() { const options = this._options.text || {}; return options.indent > 0 } _setTextItemsSides() { const options = this._options; const indent = Number(options.text.indent); this._lineFrom = options.y - options.radius; this._lineTo = this._lineFrom - indent; this._textRadius = options.radius + indent } _getPositions() { const basePosition = this._basePosition; const actualPosition = this._actualPosition; const [mainPosition1, mainPosition2] = basePosition >= actualPosition ? [basePosition, actualPosition] : [actualPosition, basePosition]; return { start: this._startPosition, end: this._endPosition, main1: mainPosition1, main2: mainPosition2, back1: Math.min(mainPosition1 + this._space, this._startPosition), back2: Math.max(mainPosition2 - this._space, this._endPosition) } } _buildItemSettings(from, to) { return { x: this._options.x, y: this._options.y, innerRadius: this._minSide, outerRadius: this._maxSide, startAngle: to, endAngle: from } } _updateTextPosition() { const cosSin = getCosAndSin(this._actualPosition); let x = this._options.x + this._textRadius * cosSin.cos; let y = this._options.y - this._textRadius * cosSin.sin; x += cosSin.cos * this._textWidth * .6; y -= cosSin.sin * this._textHeight * .6; this._text.attr({ x: x, y: y + this._textVerticalOffset }) } _updateLinePosition() { const { x: x } = this._options; const [x1, x2] = getLineSides(x, this._basePosition, this._actualPosition); const points = [x1, this._lineFrom, x1, this._lineTo, x2, this._lineTo, x2, this._lineFrom]; this._line.attr({ points: points }).rotate(convertAngleToRendererSpace(this._actualPosition), x, this._options.y).sharp() } _getTooltipPosition() { const cosSin = getCosAndSin((this._basePosition + this._actualPosition) / 2); const r = (this._minSide + this._maxSide) / 2; return { x: this._options.x + cosSin.cos * r, y: this._options.y - cosSin.sin * r } } measure(layout) { const result = { min: layout.radius - Number(this._options.size), max: layout.radius }; this._measureText(); if (this._hasText) { result.max = result.max + Number(this._options.text.indent); result.horizontalOffset = this._textWidth; result.verticalOffset = this._textHeight } return result } } export { RectangleNeedle as _default, RangeBar as rangebar, RectangleNeedle as rectangleneedle, TextCloud as textcloud, TriangleMarker as trianglemarker, TriangleNeedle as triangleneedle, TwoColorNeedle as twocolorneedle };