UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

289 lines (287 loc) • 10.1 kB
/** * DevExtreme (esm/__internal/viz/gauges/linear_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 { setupWidgetPrototype } from "../../viz/core/helpers"; import { normalizeEnum } from "../../viz/core/utils"; import { BaseGauge } from "../../viz/gauges/base_gauge"; import { createIndicatorCreator, dxGauge } from "../../viz/gauges/common"; import * as linearIndicators from "../../viz/gauges/linear_indicators"; import LinearRangeContainer from "../../viz/gauges/linear_range_container"; function selectRectBySizes(srcRect, sizes, margins) { const rect = extend({}, srcRect); const currentMargins = margins || {}; if (sizes) { rect.left += currentMargins.left || 0; rect.right -= currentMargins.right || 0; rect.top += currentMargins.top || 0; rect.bottom -= currentMargins.bottom || 0; if (sizes.width > 0) { const step = (rect.right - rect.left - sizes.width) / 2; if (step > 0) { rect.left += step; rect.right -= step } } if (sizes.height > 0) { const step = (rect.bottom - rect.top - sizes.height) / 2; if (step > 0) { rect.top += step; rect.bottom -= step } } } return rect } class LinearGauge extends dxGauge { _getTicksOrientation(scaleOptions) { return scaleOptions.isHorizontal ? scaleOptions.verticalOrientation : scaleOptions.horizontalOrientation } _getThemeManagerOptions() { const options = super._getThemeManagerOptions(); options.subTheme = "_linear"; return options } _getInvertedState() { return !this._area.vertical && this.option("rtlEnabled") } _prepareScaleSettings() { const scaleOptions = super._prepareScaleSettings(); scaleOptions.inverted = this._getInvertedState(); return scaleOptions } _updateScaleTickIndent(scaleOptions) { const { indentFromTick: indentFromTick } = scaleOptions.label; const { length: length } = scaleOptions.tick; const textParams = this._scale.measureLabels(extend({}, this._canvas)); const verticalTextCorrection = scaleOptions.isHorizontal ? textParams.height + textParams.y : 0; const isIndentPositive = indentFromTick > 0; const { tickCorrection: tickCorrection, textCorrection: textCorrection } = scaleOptions.isHorizontal ? { tickCorrection: length * (isIndentPositive ? { center: .5, top: 0, bottom: 1 } : { center: .5, top: 1, bottom: 0 })[scaleOptions.verticalOrientation], textCorrection: textParams.y } : { tickCorrection: length * (isIndentPositive ? { center: .5, left: 0, right: 1 } : { center: .5, left: 1, right: 0 })[scaleOptions.horizontalOrientation], textCorrection: -textParams.width }; scaleOptions.label.indentFromAxis = -indentFromTick + (isIndentPositive ? -tickCorrection + textCorrection : tickCorrection - verticalTextCorrection); this._scale.updateOptions(scaleOptions) } _shiftScale(layout, scaleOptions) { const canvas = extend({}, this._canvas); const { isHorizontal: isHorizontal } = scaleOptions; const scale = this._scale; canvas[isHorizontal ? "left" : "top"] = this._area[isHorizontal ? "startCoord" : "endCoord"]; canvas[isHorizontal ? "right" : "bottom"] = canvas[isHorizontal ? "width" : "height"] - this._area[isHorizontal ? "endCoord" : "startCoord"]; scale.draw(canvas); scale.shift({ left: -layout.x, top: -layout.y }) } _setupCodomain() { const geometry = this.option("geometry") || {}; const vertical = "vertical" === normalizeEnum(geometry.orientation); this._area = { vertical: vertical, x: 0, y: 0, startCoord: -100, endCoord: 100 }; this._rangeContainer.vertical = vertical; this._translator.setInverted(this._getInvertedState()); this._translator.setCodomain(-100, 100) } _getScaleLayoutValue() { return this._area[this._area.vertical ? "x" : "y"] } _getTicksCoefficients(options) { const coefs = { inner: 0, outer: 1 }; if (this._area.vertical) { if ("left" === options.horizontalOrientation) { coefs.inner = 1; coefs.outer = 0 } else if ("center" === options.horizontalOrientation) { coefs.inner = .5; coefs.outer = .5 } } else if ("top" === options.verticalOrientation) { coefs.inner = 1; coefs.outer = 0 } else if ("center" === options.verticalOrientation) { coefs.inner = .5; coefs.outer = .5 } return coefs } _correctScaleIndents(result, indentFromTick, textParams) { const { vertical: vertical } = this._area; if (indentFromTick >= 0) { result.max = result.max + indentFromTick + textParams[vertical ? "width" : "height"] } else { result.min = result.min - (-indentFromTick + textParams[vertical ? "width" : "height"]) } result.indent = textParams[vertical ? "height" : "width"] / 2 } _measureMainElements(elements, scaleMeasurement) { const { x: x, y: y } = this._area; let minBound = 1e3; let maxBound = 0; let indent = 0; const scale = this._scale; each(elements.concat(scale), (_, element) => { const bounds = element.measure ? element.measure({ x: x + element.getOffset(), y: y + element.getOffset() }) : scaleMeasurement; if (void 0 !== bounds.max) { maxBound = Math.max(maxBound, bounds.max) } if (void 0 !== bounds.min) { minBound = Math.min(minBound, bounds.min) } if (bounds.indent > 0) { indent = Math.max(indent, bounds.indent) } }); return { minBound: minBound, maxBound: maxBound, indent: indent } } _applyMainLayout(elements, scaleMeasurement) { const measurements = this._measureMainElements(elements, scaleMeasurement); const area = this._area; const rect = area.vertical ? selectRectBySizes(this._innerRect, { width: measurements.maxBound - measurements.minBound }) : selectRectBySizes(this._innerRect, { height: measurements.maxBound - measurements.minBound }); if (area.vertical) { const offset = (rect.left + rect.right) / 2 - (measurements.minBound + measurements.maxBound) / 2; area.startCoord = rect.bottom - measurements.indent; area.endCoord = rect.top + measurements.indent; area.x = Math.round(area.x + offset) } else { const offset = (rect.top + rect.bottom) / 2 - (measurements.minBound + measurements.maxBound) / 2; area.startCoord = rect.left + measurements.indent; area.endCoord = rect.right - measurements.indent; area.y = Math.round(area.y + offset) } this._translator.setCodomain(area.startCoord, area.endCoord); this._innerRect = rect } _getElementLayout(offset) { return { x: Math.round(this._area.x + offset), y: Math.round(this._area.y + offset) } } _getApproximateScreenRange() { const area = this._area; let s = area.vertical ? this._canvas.height : this._canvas.width; if (void 0 !== area.totalSize && s > area.totalSize) { s = area.totalSize } s *= .8; return s } _getDefaultSize() { const geometry = this.option("geometry") || {}; if ("vertical" === geometry.orientation) { return { width: 100, height: 300 } } return { width: 300, height: 100 } } } setupWidgetPrototype(LinearGauge, { _rootClass: "dxg-linear-gauge", _factoryMethods: { rangeContainer: "createLinearRangeContainer", indicator: "createLinearIndicator" }, _gridSpacingFactor: 25, _scaleTypes: { type: "xyAxes", drawingType: "linear" }, _factory: clone(BaseGauge.prototype._factory) }); const indicators = {}; LinearGauge.prototype._factory.indicators = indicators; LinearGauge.prototype._factory.createIndicator = createIndicatorCreator(indicators); indicators._default = linearIndicators._default; indicators.rectangle = linearIndicators.rectangle; indicators.rhombus = linearIndicators.rhombus; indicators.circle = linearIndicators.circle; indicators.trianglemarker = linearIndicators.trianglemarker; indicators.textcloud = linearIndicators.textcloud; indicators.rangebar = linearIndicators.rangebar; LinearGauge.prototype._factory.RangeContainer = LinearRangeContainer; registerComponent("dxLinearGauge", LinearGauge); export default LinearGauge;