UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

235 lines (229 loc) • 7.71 kB
/** * DevExtreme (esm/__internal/viz/gauges/base_range_container.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 { each } from "../../core/utils/m_iterator"; import { isString } from "../../core/utils/m_type"; import { extractColor } from "../../viz/core/utils"; import { BaseElement } from "../../viz/gauges/base_indicators"; export function getMaxRangeWidth(width) { return Number(width) || Math.max(Number(width.start), Number(width.end)) } function subtractSegmentAsc(segmentStart, segmentEnd, otherStart, otherEnd) { if (otherStart > segmentStart && otherEnd < segmentEnd) { return [{ start: segmentStart, end: otherStart }, { start: otherEnd, end: segmentEnd }] } if (otherStart >= segmentEnd || otherEnd <= segmentStart) { return [{ start: segmentStart, end: segmentEnd }] } if (otherStart <= segmentStart && otherEnd >= segmentEnd) { return [] } if (otherStart > segmentStart) { return [{ start: segmentStart, end: otherStart }] } return [{ start: otherEnd, end: segmentEnd }] } function subtractSegmentDesc(segmentStart, segmentEnd, otherStart, otherEnd) { if (otherStart < segmentStart && otherEnd > segmentEnd) { return [{ start: segmentStart, end: otherStart }, { start: otherEnd, end: segmentEnd }] } if (otherStart <= segmentEnd || otherEnd >= segmentStart) { return [{ start: segmentStart, end: segmentEnd }] } if (otherStart >= segmentStart && otherEnd <= segmentEnd) { return [] } if (otherStart < segmentStart) { return [{ start: segmentStart, end: otherStart }] } return [{ start: otherEnd, end: segmentEnd }] } function areEqualValues(start, end, _ref) { let { startValue: startValue, endValue: endValue } = _ref; return endValue === startValue && startValue === start && end === start } function isValidSegmentAsc(start, end, options) { return end - start > 0 || areEqualValues(start, end, options) } function isValidSegmentDesc(start, end, options) { return start - end > 0 || areEqualValues(start, end, options) } class BaseRangeContainer extends BaseElement { _init() { this._root = this._renderer.g().attr({ class: "dxg-range-container" }).linkOn(this._container, "range-container") } _dispose() { this._root.linkOff() } clean() { this._root.linkRemove().clear(); this.enabled = null; this._options = null; return this } _getRanges() { const options = this._options; const translator = this._translator; const totalStart = translator.getDomain()[0]; const totalEnd = translator.getDomain()[1]; const totalDelta = totalEnd - totalStart; const isValidSegment = totalDelta >= 0 ? isValidSegmentAsc : isValidSegmentDesc; const subtractSegment = totalDelta >= 0 ? subtractSegmentAsc : subtractSegmentDesc; let ranges = []; let backgroundRanges = [{ start: totalStart, end: totalEnd }]; const backgroundColor = extractColor(options.backgroundColor) || "none"; const width = options.width || {}; const startWidth = Number(width > 0 ? width : width.start); const endWidth = Number(width > 0 ? width : width.end); const deltaWidth = endWidth - startWidth; if (void 0 !== options.ranges && !Array.isArray(options.ranges)) { return null } if (!(startWidth >= 0 && endWidth >= 0 && startWidth + endWidth > 0)) { return null } const rangesOptions = Array.isArray(options.ranges) ? options.ranges : []; const list = rangesOptions.reduce((result, rangeOptions, i) => { const currentOptions = rangeOptions || {}; const start = translator.adjust(currentOptions.startValue); const end = translator.adjust(currentOptions.endValue); if (Number.isFinite(start) && Number.isFinite(end) && isValidSegment(start, end, currentOptions)) { result.push({ start: start, end: end, color: extractColor(currentOptions.color), classIndex: i }) } return result }, []); const palette = this._themeManager.createPalette(options.palette, { type: "indicatingSet", extensionMode: options.paletteExtensionMode, keepLastColorInEnd: true, count: list.length }); each(list, (_, item) => { const paletteColor = palette.getNextColor(); item.color = isString(item.color) && item.color || paletteColor || "none"; item.className = `dxg-range dxg-range-${item.classIndex}`; delete item.classIndex }); each(list, (_, item) => { const newRanges = []; const newBackgroundRanges = []; ranges.forEach(range => { const subs = subtractSegment(range.start, range.end, item.start, item.end); subs.forEach(sub => { sub.color = range.color; sub.className = range.className; newRanges.push(sub) }) }); newRanges.push(item); ranges = newRanges; backgroundRanges.forEach(range => { const subs = subtractSegment(range.start, range.end, item.start, item.end); subs.forEach(sub => { newBackgroundRanges.push(sub) }) }); backgroundRanges = newBackgroundRanges }); each(backgroundRanges, (_, range) => { range.color = backgroundColor; range.className = "dxg-range dxg-background-range"; ranges.push(range) }); each(ranges, (_, range) => { range.startWidth = (range.start - totalStart) / totalDelta * deltaWidth + startWidth; range.endWidth = (range.end - totalStart) / totalDelta * deltaWidth + startWidth }); return ranges } render(options) { this._options = options; this._processOptions(); this._ranges = this._getRanges(); if (this._ranges) { this.enabled = true; this._root.linkAppend() } return this } resize(layout) { this._root.clear(); if (this._isVisible(layout)) { each(this._ranges, (_, range) => { this._createRange(range, layout).attr({ fill: range.color, class: range.className }).append(this._root) }) } return this } getColorForValue(value) { let color = null; each(this._ranges, (_, range) => { const inRange = range.start <= value && value <= range.end || range.start >= value && value >= range.end; if (inRange) { color = range.color } return !inRange }); return color } } export default BaseRangeContainer;