UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

470 lines (468 loc) • 15.8 kB
/** * DevExtreme (esm/__internal/viz/vector_map/vector_map.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 "../../viz/vector_map/projection"; import { registerComponent as componentRegistrator } from "../../core/component_registrator"; import { plugins as annotationsPlugins } from "../../viz/core/annotations"; import BaseWidget from "../../viz/core/base_widget"; import { plugin as ExportPlugin } from "../../viz/core/export"; import { setupWidgetPrototype } from "../../viz/core/helpers"; import { plugin as LoadingIndicatorPlugin } from "../../viz/core/loading_indicator"; import { plugin as TitlePlugin } from "../../viz/core/title"; import { plugin as TooltipPlugin } from "../../viz/core/tooltip"; import { parseScalar } from "../../viz/core/utils"; import { ControlBar } from "../../viz/vector_map/control_bar/control_bar"; import { DataExchanger } from "../../viz/vector_map/data_exchanger"; import { GestureHandler } from "../../viz/vector_map/gesture_handler"; import { LayoutControl } from "../../viz/vector_map/layout"; import { LegendsControl } from "../../viz/vector_map/legend"; import { getMaxBound, MapLayerCollection } from "../../viz/vector_map/map_layer"; import { Projection } from "../../viz/vector_map/projection.main"; import { TooltipViewer } from "../../viz/vector_map/tooltip_viewer"; import { Tracker } from "../../viz/vector_map/tracker"; import { generateDataKey } from "../../viz/vector_map/vector_map.utils"; const DEFAULT_WIDTH = 800; const DEFAULT_HEIGHT = 400; function mergeBounds(sumBounds, dataBounds) { return dataBounds ? [Math.min(dataBounds[0], dataBounds[2], sumBounds[0]), Math.min(dataBounds[1], dataBounds[3], sumBounds[3]), Math.max(dataBounds[0], dataBounds[2], sumBounds[2]), Math.max(dataBounds[1], dataBounds[3], sumBounds[1])] : sumBounds } class VectorMap extends BaseWidget { _getDefaultSize() { return { width: 800, height: 400 } } _initLayerCollection(dataKey) { this._layerCollection = new MapLayerCollection({ renderer: this._renderer, projection: this._projection, themeManager: this._themeManager, tracker: this._tracker, dataKey: dataKey, eventTrigger: this._eventTrigger, dataExchanger: this._dataExchanger, tooltip: this._tooltip, notifyDirty: this._notifyDirty, notifyReady: this._notifyReady, dataReady: () => { const bounds = this.option("getBoundsFromData") && !this.option("bounds") ? this._applyBoundsFromData() : void 0; if (!this.option("projection")) { const dataBounds = bounds || this._getBoundsFromData(); if (Math.ceil(dataBounds[0]) < -180 || Math.ceil(dataBounds[3]) < -90 || Math.floor(dataBounds[2]) > 180 || Math.floor(dataBounds[1]) > 90) { const longitudeLength = dataBounds[2] - dataBounds[0]; const latitudeLength = dataBounds[1] - dataBounds[3]; this._projection.setEngine({ to: coordinates => [2 * (coordinates[0] - dataBounds[0]) / longitudeLength - 1, 2 * (coordinates[1] - dataBounds[3]) / latitudeLength - 1], from: coordinates => [(coordinates[0] + 1) * longitudeLength / 2 + dataBounds[0], (coordinates[1] + 1) * latitudeLength / 2 + dataBounds[3]] }) } } } }) } _applyBoundsFromData() { this._preventProjectionEvents(); const bounds = this._getBoundsFromData(); this._projection.setBounds(bounds); this._allowProjectionEvents(); return bounds } _getBoundsFromData() { const boundsFromDataSource = this._getBoundingBoxFromDataSource(); let bounds = boundsFromDataSource; if (!bounds) { const layersBounds = this.getLayers().map(l => l.getBounds()).filter(x => void 0 !== x); const boundsByData = getMaxBound(layersBounds); if (boundsByData) { bounds = boundsByData } } bounds = bounds || []; return [bounds[0], bounds[3], bounds[2], bounds[1]] } _initLegendsControl() { this._legendsControl = new LegendsControl({ renderer: this._renderer, container: this._root, widget: this, layoutControl: this._layoutControl, themeManager: this._themeManager, dataExchanger: this._dataExchanger, notifyDirty: this._notifyDirty, notifyReady: this._notifyReady }) } _initControlBar(dataKey) { this._controlBar = new ControlBar({ renderer: this._renderer, container: this._root, layoutControl: this._layoutControl, projection: this._projection, tracker: this._tracker, dataKey: dataKey }) } _initElements() { const dataKey = generateDataKey(); let notifyCounter = 0; let preventProjectionEvents = false; this._preventProjectionEvents = () => { preventProjectionEvents = true }; this._allowProjectionEvents = () => { preventProjectionEvents = false }; this._notifyDirty = () => { this._resetIsReady(); notifyCounter += 1 }; this._notifyReady = () => { this._allowProjectionEvents(); notifyCounter -= 1; if (0 === notifyCounter) { this._drawn() } }; this._preventProjectionEvents(); this._dataExchanger = new DataExchanger; this._projection = new Projection({ centerChanged: value => { if (!preventProjectionEvents) { this._eventTrigger("centerChanged", { center: value }) } }, zoomChanged: value => { if (!preventProjectionEvents) { this._eventTrigger("zoomFactorChanged", { zoomFactor: value }) } } }); this._tracker = new Tracker({ root: this._root, projection: this._projection, dataKey: dataKey }); this._gestureHandler = new GestureHandler({ projection: this._projection, renderer: this._renderer, tracker: this._tracker }); this._layoutControl = new LayoutControl(this); this._layoutControl.suspend(); this._initLayerCollection(dataKey); this._createHtmlStructure(); this._initControlBar(dataKey); this._initLegendsControl(); this._prepareExtraElements(); this._tooltipViewer = new TooltipViewer({ tracker: this._tracker, tooltip: this._tooltip, layerCollection: this._layerCollection }) } _change_RESUME_LAYOUT() { this._layoutControl.resume() } _initCore() { this._root = this._renderer.root.attr({ align: "center", cursor: "default" }); this._initElements() } _disposeCore() { this._controlBar.dispose(); this._gestureHandler.dispose(); this._tracker.dispose(); this._legendsControl.dispose(); this._layerCollection.dispose(); this._layoutControl.dispose(); this._tooltipViewer.dispose(); this._dataExchanger.dispose(); this._projection.dispose(); Object.assign(this, { _dataExchanger: null, _gestureHandler: null, _projection: null, _tracker: null, _layoutControl: null, _root: null, _layerCollection: null, _controlBar: null, _legendsControl: null }) } _setupInteraction() { const options = { centeringEnabled: !!parseScalar(this._getOption("panningEnabled", true), true), zoomingEnabled: !!parseScalar(this._getOption("zoomingEnabled", true), true) }; this._gestureHandler.setInteraction(options); this._controlBar.setInteraction(options) } _applySize(rect) { const layout = { left: rect[0], top: rect[1], width: rect[2] - rect[0], height: rect[3] - rect[1], right: 0, bottom: 0 }; this._projection.setSize(layout); this._layoutControl.setSize(layout); this._layerCollection.setRect([layout.left, layout.top, layout.width, layout.height]); this._requestChange(["EXTRA_ELEMENTS"]) } _optionChanging(name, currentValue, nextValue) { if (currentValue && nextValue) { if (name.startsWith("layers")) { if (currentValue.dataSource && nextValue.dataSource && currentValue !== nextValue) { currentValue.dataSource = null } else if (name.endsWith(".dataSource")) { this.option(name, null) } } } } _applyChanges() { this._notifyDirty(); super._applyChanges(); this._notifyReady() } _change_PROJECTION() { this._setProjection() } _change_BOUNDS() { this._setBounds() } _change_MAX_ZOOM_FACTOR() { this._setMaxZoom() } _change_ZOOM_FACTOR() { this._setZoom() } _change_CENTER() { this._setCenter() } _change_BACKGROUND() { this._setBackgroundOptions() } _change_LAYERS() { this._setLayerCollectionOptions() } _change_CONTROL_BAR() { this._setControlBarOptions() } _change_EXTRA_ELEMENTS() { this._renderExtraElements() } _change_LEGENDS() { this._setLegendsOptions() } _change_TRACKER() { this._setTrackerOptions() } _change_INTERACTION() { this._setupInteraction() } _setProjection() { this._projection.setEngine(this.option("projection")) } _setBounds() { this._projection.setBounds(this.option("bounds")) } _setMaxZoom() { this._projection.setMaxZoom(this.option("maxZoomFactor")) } _setZoom() { this._projection.setZoom(this.option("zoomFactor")) } _setCenter() { this._projection.setCenter(this.option("center")) } _setBackgroundOptions() { this._layerCollection.setBackgroundOptions(this._getOption("background")) } _setLayerCollectionOptions() { this._layerCollection.setOptions(this.option("layers")) } _getBoundingBoxFromDataSource() { const layers = this._layerCollection.items(); const infinityBounds = [1 / 0, -1 / 0, -1 / 0, 1 / 0]; const resultBBox = layers && layers.length ? layers.reduce((sumBBox, l) => { let result = sumBBox; const layerData = l.getData(); const itemCount = layerData.count(); if (itemCount > 0) { const rootBBox = layerData.getBBox(); if (rootBBox) { result = mergeBounds(result, rootBBox) } else { for (let i = 0; i < itemCount; i += 1) { result = mergeBounds(result, layerData.getBBox(i)) } } } return result }, infinityBounds) : void 0; return resultBBox === infinityBounds ? void 0 : resultBBox } _setControlBarOptions() { this._controlBar.setOptions(this._getOption("controlBar")) } _setLegendsOptions() { this._legendsControl.setOptions(this.option("legends")) } _setTrackerOptions() { this._tracker.setOptions({ touchEnabled: this._getOption("touchEnabled", true), wheelEnabled: this._getOption("wheelEnabled", true) }) } getLayers() { const layers = this._layerCollection.items(); return layers.map(l => l.proxy) } getLayerByIndex(index) { const layer = this._layerCollection.byIndex(index); return layer ? layer.proxy : null } getLayerByName(name) { const layer = this._layerCollection.byName(name); return layer ? layer.proxy : null } clearSelection() { const layers = this._layerCollection.items(); layers.forEach(layer => { layer.clearSelection() }); return this } center(value) { if (void 0 === value) { return this._projection.getCenter() } this._projection.setCenter(value); return this } zoomFactor(value) { if (void 0 === value) { return this._projection.getZoom() } this._projection.setZoom(value); return this } viewport(value) { if (void 0 === value) { return this._projection.getViewport() } this._projection.setViewport(value); return this } convertToGeo(x, y) { return this._projection.fromScreenPoint([x, y]) } convertToXY(longitude, latitude) { return this._projection.toScreenPoint([longitude, latitude]) } } setupWidgetPrototype(VectorMap, { _eventsMap: { onClick: { name: "click" }, onCenterChanged: { name: "centerChanged" }, onZoomFactorChanged: { name: "zoomFactorChanged" }, onHoverChanged: { name: "hoverChanged" }, onSelectionChanged: { name: "selectionChanged" } }, _rootClassPrefix: "dxm", _rootClass: "dxm-vector-map", _themeSection: "map", _fontFields: ["layer:area.label.font", "layer:marker:dot.label.font", "layer:marker:bubble.label.font", "layer:marker:pie.label.font", "layer:marker:image.label.font", "legend.font", "legend.title.font", "legend.title.subtitle.font"], _initialChanges: ["PROJECTION", "RESUME_LAYOUT", "LAYOUT_INIT", "BOUNDS", "MAX_ZOOM_FACTOR", "ZOOM_FACTOR", "CENTER"], _layoutChangesOrder: ["RESUME_LAYOUT", "LAYERS"], _customChangesOrder: ["EXTRA_ELEMENTS"], _optionChangesMap: { background: "BACKGROUND", layers: "LAYERS", extraElements: "EXTRA_ELEMENTS", controlBar: "CONTROL_BAR", legends: "LEGENDS", touchEnabled: "TRACKER", wheelEnabled: "TRACKER", panningEnabled: "INTERACTION", zoomingEnabled: "INTERACTION", projection: "PROJECTION", bounds: "BOUNDS", maxZoomFactor: "MAX_ZOOM_FACTOR", zoomFactor: "ZOOM_FACTOR", center: "CENTER" }, _optionChangesOrder: ["PROJECTION", "BOUNDS", "MAX_ZOOM_FACTOR", "ZOOM_FACTOR", "CENTER", "BACKGROUND", "CONTROL_BAR", "LEGENDS", "TRACKER", "INTERACTION"], _themeDependentChanges: ["BACKGROUND", "LAYERS", "CONTROL_BAR", "LEGENDS", "TRACKER", "INTERACTION"] }); componentRegistrator("dxVectorMap", VectorMap); VectorMap.addPlugin(ExportPlugin); VectorMap.addPlugin(TitlePlugin); VectorMap.addPlugin(TooltipPlugin); VectorMap.addPlugin(LoadingIndicatorPlugin); VectorMap.addPlugin(annotationsPlugins.core); VectorMap.addPlugin(annotationsPlugins.vectorMap); export default VectorMap;