/** * @packageDocumentation * @module map */ import assert from 'assert'; import Plotly from './plotly/plotly-scatter'; import { Config, Data, Layout, PlotlyScatterElement } from './plotly/plotly-scatter'; import * as plotlyStyles from './plotly/plotly-styles'; import { Property, Settings } from '../dataset'; import { DisplayTarget, EnvironmentIndexer, Indexes } from '../indexer'; import { cameraToPlotly, plotlyToCamera } from '../utils/camera'; import { Bounds, GUID, PositioningCallback, Warnings, arrayMaxMin, transformedAxisLabel, } from '../utils'; import { enumerate, getElement, getFirstKey } from '../utils'; import { MapData, NumericProperties, NumericProperty } from './data'; import { MarkerData } from './marker'; import { AxisOptions, MapOptions, get3DSymbol } from './options'; import { LODSampler } from './lod'; import * as styles from '../styles'; import { DEFAULT_CONFIG, DEFAULT_LAYOUT, getAxisAutoRange, getAxisRange } from './utils'; type NestedAttributes = Record<`${string}.${string}`, unknown>; const LEGEND_TRUNCATION_MARK = '...'; function truncateLegendName(name: string): string { const MAX_LEGEND_NAME_LENGTH = 25; if (name.length > MAX_LEGEND_NAME_LENGTH) { return name.slice(0, MAX_LEGEND_NAME_LENGTH - 1) + LEGEND_TRUNCATION_MARK; } return name; } /** * The {@link PropertiesMap} class displays a 2D or 3D map (scatter plot) of * properties in the dataset, using [plotly.js](https://plot.ly/javascript/) * for rendering. * * Properties can be used as x, y, or z values, as well as points color and * size. Additionally, string properties can be used as symbols for the scatter * plot markers. */ export class PropertiesMap { /** Callback fired when the plot is clicked and the position of the active marker changes */ public onselect: (indexes: Indexes) => void; /** * Callback fired when the active marker is changed by clicking on the map * * @param guid GUID of the new active marker * @param indexes index of the environment the new active marker is showing */ public activeChanged: (guid: GUID, indexes: Indexes) => void; /** * Callback to get the initial positioning of the settings modal. * * The callback gets the current placement of the settings as a * [DOMRect](https://developer.mozilla.org/en-US/docs/Web/API/DOMRect), and * should return top and left positions in pixels, used with `position: * fixed`. The callback is called once, the first time the settings are * opened. */ public positionSettingsModal: PositioningCallback; public warnings: Warnings; /// Shadow root for isolation private _shadow: ShadowRoot; /// HTML element holding the full plot private _root: HTMLElement; /// Plotly plot private _plot!: PlotlyScatterElement; /// All known properties private _data: MapData; /// GUID of the currently selected point private _active?: GUID; /// Map associating currently selected markers GUID to additional data private _selected: Map; /// environment indexer private _indexer: EnvironmentIndexer; // widget display target private _target: DisplayTarget; /// Settings of the map private _options: MapOptions; /// Button used to reset the range of color axis private _colorReset: HTMLButtonElement; /** * LOD (Level of Detail) Configuration * * Speeds up rendering of large datasets by downsampling points * when zoomed out. */ /// start sampling above this count and keep this many points in view private static readonly LOD_MAX_POINTS = 50000; /// cached ranking for the current axes and selection private _lod: LODSampler | null = null; /// Stores the subset of point indices to display when LOD is active private _lodIndices: number[] | null = null; /// True while writing derived option values programmatically; the reactive /// onchange handlers (updateColors, axis rangeChange) skip while it is set. private _internalUpdate = false; /// Guard to skip concurrent LOD updates. When set, other callers simply // return early instead of waiting private _lodBusy = false; // timer for copying plot ranges and camera back into map settings private _afterplotRequest: number | null = null; // separate timer for choosing points after the view changes private _lodRequest: number | null = null; // flag used to track whether the user is currently dragging the 3D plot private _isDragging3D = false; // handler for the global mouseup event used to fix a plotly bug in 3D private _mouseupHandler?: () => void; // last event data received from plotly_relayouting private _lastRelayoutingEvent: Plotly.PlotRelayoutEvent | null = null; // pending axis-range relayout, coalesced to one relayout per tick private _pendingRangeUpdate: Record = {}; private _rangeFlushScheduled = false; // observes the container so the plot reflows on non-window resizes private _resizeObserver?: ResizeObserver; private _resizePending = false; /** * Create a new {@link PropertiesMap} inside the DOM element with the given HTML * `id` * * @param element HTML element or string 'id' of the element where * the map should live * @param settings settings for all panels * @param indexer {@link EnvironmentIndexer} used to translate indexes from * environments index to structure/atom indexes * @param target widget display target, either stucture or atom * @param properties properties to be displayed */ constructor( element: string | HTMLElement, settings: Settings, indexer: EnvironmentIndexer, target: DisplayTarget, properties: { [name: string]: Property }, warnings: Warnings ) { this._indexer = indexer; this._target = target; this.onselect = () => {}; this.activeChanged = () => {}; this._selected = new Map(); this.warnings = warnings; // DOM structure outside the map: // - containerElement/element (#chemiscope-map) // - hostElement (layer needed for removal) // - this._shadow (shadow root) // - this._root // - plotly root // - viewer button // - marker // Attach a shadow DOM to the host element for isolation const containerElement = getElement(element); const hostElement = document.createElement('div'); hostElement.style.setProperty('height', '100%'); containerElement.appendChild(hostElement); this._shadow = hostElement.attachShadow({ mode: 'open' }); // Create a root element inside the shadow DOM to append plot to it this._root = document.createElement('div'); this._root.style.setProperty('height', '100%'); this._shadow.appendChild(this._root); if (this._root.style.position === '') { this._root.style.position = 'relative'; } // Create a div for the plot this._plot = document.createElement('div') as unknown as PlotlyScatterElement; this._plot.style.width = '100%'; this._plot.style.height = '100%'; this._root.appendChild(this._plot); // plotly treats ctrl+click as a right click, but still starts a 2D pan that // ends with an invalid relayout and a rejected promise. Replace it with a // shift+click, which pans in zoom mode like ctrl+click pans in 3D this._plot.addEventListener( 'mousedown', (event) => { if (!event.ctrlKey || this._is3D()) { return; } event.stopPropagation(); event.preventDefault(); event.target?.dispatchEvent( new MouseEvent('mousedown', { bubbles: true, cancelable: true, clientX: event.clientX, clientY: event.clientY, button: event.button, buttons: event.buttons, shiftKey: this._plot._fullLayout.dragmode === 'zoom', }) ); }, true ); // Initialize data with the given properties this._data = new MapData(properties, this.warnings); // Initialize options used in the modal const currentProperties = this._getCurrentProperties(); this._options = new MapOptions( this._root, currentProperties, (rect) => this.positionSettingsModal(rect), settings, this.warnings ); this._colorReset = this._options.getModalElement('map-color-reset'); // Determine whether to show the LOD option based on dataset size const nPoints = Object.values(currentProperties)[0].values.length; if (nPoints > PropertiesMap.LOD_MAX_POINTS) { this._options.showLODOption(true); } // Initial LOD computation this._computeLOD(); // Connect the settings to event listeners or handlers this._connectSettings(); // Define the default position for the settings modal on top of the plot, // centered horizontally this.positionSettingsModal = (rect: DOMRect) => { const rootRect = this._root.getBoundingClientRect(); return { left: rootRect.left + rootRect.width / 2 - rect.width / 2, top: rootRect.top + 20, }; }; // Create the Plotly plot within the plot element this._createPlot(); this._setupLegendTooltips(); // Adopt styles with the plot stylesheets as last one because the plot // needs to be created to obtain it this._shadow.adoptedStyleSheets = [ styles.bootstrap, styles.chemiscope, plotlyStyles.globalStyleSheet, plotlyStyles.getPlotStyleSheet(this._plot), ]; // Plotly's `responsive` config only reacts to window resizes; observe the // container so the plot also reflows when its size changes for other reasons // (e.g. toggling a Jupyter sidebar). this._resizeObserver = new ResizeObserver(() => { if (this._resizePending) { return; } this._resizePending = true; window.requestAnimationFrame(() => { this._resizePending = false; Plotly.Plots.resize(this._plot); // the room left for the colorbar changes with the plot height const len = this._colorbarLen(); // this runs on every frame of a resize, only redraw if it changed if (len !== this._plot._fullLayout.coloraxis?.colorbar?.len) { this._relayout({ 'coloraxis.colorbar.len': len } as unknown as Layout); } }); }); this._resizeObserver.observe(this._plot); } public remove(): void { this._resizeObserver?.disconnect(); // cancel the pending settings sync before removing its inputs if (this._afterplotRequest !== null) { window.clearTimeout(this._afterplotRequest); this._afterplotRequest = null; } // cancel the pending point redraw before removing the plot if (this._lodRequest !== null) { window.clearTimeout(this._lodRequest); this._lodRequest = null; } // Remove the the shadow root's host. It is not possible to remove the shadow root directly. this._shadow.host.remove(); // Remove options this._options.remove(); // remove SVG element created by Plotly document.getElementById('js-plotly-tester')?.remove(); if (this._mouseupHandler !== undefined) { window.removeEventListener('mouseup', this._mouseupHandler); } } /** * Change display target and adapt the element to the new target * @param target display target */ public switchTarget(target: DisplayTarget): Promise { // Check if the target value actually changed if (target !== this._target) { // Set new widget target this._target = target; // Reset LOD this._lodIndices = null; // Process markers to correctly set then to the updated map this._handleMarkers(); if (this._active !== undefined) { const activeMarker = this._selected.get(this._active); if (activeMarker !== undefined) { this.onselect( this._indexer.fromEnvironment(activeMarker.current, this._target) ); } } // Update the map options based on the chosen target this._setupMapOptions(); // Recompute LOD with new options this._computeLOD(); // Append the callbacks to the new options this._connectSettings(); // Re-render the plot with the new data and layout return this._react(this._getTraces(), this._getLayout(), this._getConfig()); } return Promise.resolve(); } /** * Change the environment indicated by the currently active marker to * the one with the given `indexes` */ public select(indexes: Indexes): void { if (this._active === undefined) { throw Error('tries to update selected environment, but there is no active marker'); } // data.select needs `indexes.environment`, so make sure it is defined if (indexes.environment === undefined) { const fullIndexes = this._indexer.fromStructureAtom( this._target, indexes.structure, indexes.atom ); if (fullIndexes === undefined) { const atomStr = indexes.atom === undefined ? '' : ` / atom ${indexes.atom}`; throw Error( `can not find the environnement for structure ${indexes.structure}` + atomStr ); } indexes = fullIndexes; } const data = this._selected.get(this._active); assert(data !== undefined); // this prevents infinite recursion // will return false if the data already corresponds to this index if (data.select(indexes)) { this._updateMarkers(); } } /** * Set the marker with given GUID as the active marker. * * @param guid the GUID of the new active viewer */ public setActive(guid: GUID): void { if (this._active !== undefined) { const oldData = this._selected.get(this._active); assert(oldData !== undefined); oldData.deactivate(); } this._active = guid; const data = this._selected.get(this._active); assert(data !== undefined); data.activate(); if (this._is3D()) { this._restyle({ 'marker.size': this._sizes(1) } as Data, 1); } } /** * Add a new marker to the map. The new marker is set as the active one * * @param guid GUID of the new marker * @param color GUID of the marker indicating the new viewer * @param indexes indexes of the environment that the new marker should show */ public addMarker(guid: GUID, color: string, indexes: Indexes): void { assert(!this._selected.has(guid)); const data = new MarkerData(guid, color, indexes.environment); this._root.appendChild(data.marker); data.marker.onclick = () => { this.setActive(guid); this.activeChanged(guid, this._indexer.fromEnvironment(data.current, this._target)); }; this._selected.set(guid, data); this._updateMarkers([data]); this.setActive(guid); } /** * Removes a marker from the map. * * @param guid GUID of the marker to remove */ public removeMarker(guid: GUID): void { // If we remove the active marker, let's change which one is active if (this._active === guid) { if (this._selected.size === 1) { // we are removing the last one this._active = undefined; } else { this.setActive(getFirstKey(this._selected, guid)); } } // remove HTML marker const data = this._selected.get(guid); assert(data !== undefined); data.remove(); this._selected.delete(guid); this._updateMarkers(); } /** * Apply saved settings to the map. */ public applySettings(settings: Settings): void { this._options.applySettings(settings); } /** * Save the values of the current settings in a way that an be used with * {@link applySettings} or saved to JSON. */ public saveSettings(): Settings { return this._options.saveSettings(); } /** * Add the given `callback` to be called whenever a setting changes. The * callback will be given the path to the settings as a list of keys; and * the new value of the setting. * * There is currently no way to remove a callback. */ public onSettingChange(callback: (keys: string[], value: unknown) => void): void { this._options.onSettingChange(callback); } /** * Export the plot as a PNG data URI, hiding the "selected" trace (index 1) * during the export. */ public async exportPNG(): Promise { const fullLayout = this._plot._fullLayout; const width = Math.max(fullLayout.width, 600); const ratio = fullLayout.height / fullLayout.width; const height = width * ratio; const opts: Plotly.ToImgopts = { format: 'png', width: width, height: height, scale: 3, }; // Hide the "selected" trace (index 1) for the export. try { await Plotly.restyle(this._plot, { visible: false }, [1]); const dataUrl = await Plotly.toImage(this._plot, opts); await Plotly.restyle(this._plot, { visible: true }, [1]); return dataUrl; } catch (e) { void Plotly.restyle(this._plot, { visible: true }, [1]); throw e; } } // ====================================================================== // Private Initialization // ====================================================================== /** Actually create the Plotly plot */ private _createPlot() { this._plot.innerHTML = ''; // Get plot data const traces = this._getTraces(); // Build layout from the options of the settings const layout = this._getLayout(); // Get config const config = this._getConfig(); Plotly.newPlot(this._plot, traces, layout, config) .then(() => { // In some cases (e.g. in Jupyter notebooks) plotly does not comply // with the dimensions of its container unless it receives a resize // event _after_ it has loaded. This triggers the event. window.requestAnimationFrame(() => { window.dispatchEvent(new Event('resize')); }); }) .catch((e: unknown) => setTimeout(() => { throw e; }) ); this._plot.classList.add('chsp-map'); // Add callbacks this._plot.on('plotly_relayouting', (event) => { if (this._is3D()) { // Keep track of dragging in 3D to ensure that // the plot gets updated at the end of the dragging, // since plotly does not trigger if the mouse is released // outside of the plot area this._isDragging3D = true; this._lastRelayoutingEvent = event; } else { // In 2D, we use this to update the html markers this._updateMarkers(); } // cancel the point redraw while the user is still zooming or dragging // redrawing could make plotly jump back to the previous view if (this._lodRequest !== null) { window.clearTimeout(this._lodRequest); this._lodRequest = null; } }); if (this._mouseupHandler !== undefined) { window.removeEventListener('mouseup', this._mouseupHandler); } this._mouseupHandler = () => { // We need to track mouse up events because plotly does not trigger // relayout when the user stops dragging the 3D plot outside of the // plot area, preventing LOD updates if (this._isDragging3D) { this._isDragging3D = false; if (this._is3D() && this._lastRelayoutingEvent !== null) { // Adds axis ranges to avoid resetting the range to default values const scene = this._plot._fullLayout.scene; if (scene !== undefined) { const event = this._lastRelayoutingEvent as unknown as Record< string, unknown >; event['scene.xaxis.range'] = scene.xaxis.range; event['scene.yaxis.range'] = scene.yaxis.range; event['scene.zaxis.range'] = scene.zaxis.range; } this._relayout(this._lastRelayoutingEvent); } } }; window.addEventListener('mouseup', this._mouseupHandler); this._plot.on('plotly_click', (eventData: Plotly.PlotMouseEvent) => { const event = eventData; // don't update selected env on double click, since it is bound to // 'reset zoom level' in 2D mode. if (event.event && event.event.detail === 2) { return; } // don't intercept clicks while the subsampling is updating if (this._lodBusy) { return; } let environment = event.points[0].pointNumber; // LOD: Map the clicked point back to real environment index if LOD is active on main trace if (this._lodIndices !== null && event.points[0].curveNumber === 0) { if (environment >= this._lodIndices.length) { // ignore stray clicks that happen during redrawing return; } environment = this._lodIndices[environment]; } if (this._is3D() && event.points[0].data.name === 'selected') { // if someone has clicked on a selection marker, set to active // this is only used in 3D mode, since in 2D the HTML marker // directly deal with the click event for (const [i, [guid, data]] of enumerate(this._selected.entries())) { if (event.points[0].pointNumber === i) { environment = data.current; if (this._active !== guid) { this.setActive(guid); this.activeChanged( guid, this._indexer.fromEnvironment(data.current, this._target) ); } break; } } } const indexes = this._indexer.fromEnvironment(environment, this._target); this.select(indexes); this.onselect(indexes); }); this._plot.on('plotly_afterplot', () => { this._isDragging3D = false; if (this._lodBusy) { return; } if (this._afterplotRequest !== null) { window.clearTimeout(this._afterplotRequest); } this._afterplotRequest = window.setTimeout(() => { this._afterplotRequest = null; this._afterplot(); }, 50); }); // 3D LOD: Listen to relayout to catch 3D camera changes (zoom/pan) this._plot.on('plotly_relayout', (event: Plotly.PlotRelayoutEvent) => { this._isDragging3D = false; this._lastRelayoutingEvent = null; if (this._lodBusy) { return; } if (this._afterplotRequest !== null) { window.clearTimeout(this._afterplotRequest); } // copy the plot's axis limits and camera into map settings // this timer must not replace the pending point update below this._afterplotRequest = window.setTimeout(() => { this._afterplotRequest = null; this._afterplot(); }, 50); // colorbar or title changes must not restart the point-update delay const viewChanged = Object.keys(event).some( (key) => /^(scene\.)?[xyz]axis\.(range|autorange|type)/.test(key) || /^scene\.(camera|aspectratio)/.test(key) || key === 'autosize' || key === 'width' || key === 'height' ); if (viewChanged) { // replace the previous request so a burst of events causes one update if (this._lodRequest !== null) { window.clearTimeout(this._lodRequest); } // wait 100 ms for a pause, then choose points for the latest view this._lodRequest = window.setTimeout(() => { this._lodRequest = null; if (this._options.useLOD.value && this._lod !== null) { this._updateLOD(this._getBounds()); } }, 100); } }); // Handle double-click to reset view (global LOD) this._plot.on('plotly_doubleclick', () => { this._resetToGlobalView(); return false; }); this._updateMarkers(); // set step of min/max select arrows based on the plot range const bounds = this._getBounds(); this._setScaleStep(bounds.x, 'x'); this._setScaleStep(bounds.y, 'y'); if (bounds.z !== undefined) { this._setScaleStep(bounds.z, 'z'); } // Hack to fix a Plotly bug preventing zooming on Safari this._plot.addEventListener('wheel', () => {}); // Hack to ensure that the active trace is selected AFTER the plot is drawn // which seems to be necessary to ensure _fullLayout actually contains // the state of the plotly viewer in 3D setTimeout(() => { if (this._active !== undefined) { this.setActive(this._active); const data = this._selected.get(this._active); if (data !== undefined) { this.activeChanged( this._active, this._indexer.fromEnvironment(data.current, this._target) ); } } }, 1000); } /** Add all the required callback to the settings */ private _connectSettings() { // Range reset button const resetRanges = this._options.getModalElement('map-range-reset'); resetRanges.onclick = () => { this._resetToGlobalView(); }; this._connectAxisSettings(); this._connectColorSettings(); this._connectSymbolSettings(); this._connectSizeSettings(); this._options.markerOutline.onchange.push(() => { const width = this._options.markerOutline.value ? 0.5 : 0; this._restyle({ 'marker.line.width': width } as Data, 0); }); this._options.joinPoints.onchange.push(() => { const mode = this._options.joinPoints.value ? 'lines+markers' : 'markers'; this._restyle({ mode: mode } as Data, [0]); }); // ======= Level of Detail settings this._options.useLOD.onchange.push(() => { const bounds = this._getBounds(); this._computeLOD(bounds); // Force a full restyle. Since _lodIndices will be null if disabled, // this will render all points. void this._restyleFull(); }); // ======= camera state update this._options.camera.onchange.push((camera, origin) => { if (origin === 'JS' && camera !== undefined && this._is3D()) { const update = cameraToPlotly(camera); this._relayout({ 'scene.camera': update.camera, 'scene.aspectratio': update.aspectratio, } as unknown as Layout); } }); } /** Connect x/y/z axis settings to plot updates */ private _connectAxisSettings() { // Send a warning if a property contains negative values, that will be // discarded when using a log scale for this axis const negativeLogWarning = (axis: AxisOptions) => { // skip if axis has no property set (e.g. z axis in 2d mode) if (axis.property.value === '') { return; } // sampling on log axes excludes negative values, so it cannot drive this warning // check the original property even when the displayed sample is empty if ( axis.scale.value === 'log' && this._property(axis.property.value).values.some((value) => value < 0) && axis.min.value <= 0 ) { this.warnings.sendMessage( 'This property contains negative values. Note that taking the log will discard them.' ); } }; // ======= x axis settings this._options.x.property.onchange.push(() => { // Reset min/max when changing property to trigger autoscale negativeLogWarning(this._options.x); this._handleAxisPropertyChange('x'); }); this._options.x.scale.onchange.push(() => { negativeLogWarning(this._options.x); this._options.setLogLabel(this._options.x, 'x'); this._rescaleLOD(); if (this._is3D()) { this._relayout({ 'scene.xaxis.type': this._options.x.scale.value, } as unknown as Layout); } else { this._relayout({ 'xaxis.type': this._options.x.scale.value as Plotly.AxisType }); } }); // function creating a function to be used as onchange callback // for .min and .max const rangeChange = (name: string, axis: AxisOptions, minOrMax: 'min' | 'max') => { return () => { // ignore programmatic (derived) min/max writes if (this._internalUpdate) { return; } // the z axis only exists in 3D; relaying it out in 2D makes Plotly throw if (name === 'zaxis' && !this._is3D()) { return; } const min = axis.min.value; const max = axis.max.value; if (min > max) { this.warnings.sendMessage( `The inserted min and max values in ${name} are such that min > max! The last inserted value was reset.` ); // revert the invalid value without re-running this handler this.withInternalUpdate(() => { if (minOrMax === 'min') { axis.min.reset(); } else { axis.max.reset(); } }); return; } negativeLogWarning(axis); const key = this._is3D() ? `scene.${name}` : name; let update: Record; if (isNaN(min) && isNaN(max)) { // both bounds cleared: turn autorange on. A `[NaN, NaN]` range is // read by Plotly as autorange-off with default bounds update = { [`${key}.autorange`]: true }; } else if (this._is3D()) { update = { [`${key}.range`]: [min, max], [`${key}.autorange`]: false }; } else { update = { [`${key}.range`]: [min, max] }; } this._scheduleRangeRelayout(update); }; }; this._options.x.min.onchange.push(rangeChange('xaxis', this._options.x, 'min')); this._options.x.max.onchange.push(rangeChange('xaxis', this._options.x, 'max')); // ======= y axis settings this._options.y.property.onchange.push(() => { negativeLogWarning(this._options.y); this._handleAxisPropertyChange('y'); }); this._options.y.scale.onchange.push(() => { negativeLogWarning(this._options.y); this._options.setLogLabel(this._options.y, 'y'); this._rescaleLOD(); if (this._is3D()) { this._relayout({ 'scene.yaxis.type': this._options.y.scale.value, } as unknown as Layout); } else { this._relayout({ 'yaxis.type': this._options.y.scale.value as Plotly.AxisType }); } }); this._options.y.min.onchange.push(rangeChange('yaxis', this._options.y, 'min')); this._options.y.max.onchange.push(rangeChange('yaxis', this._options.y, 'max')); // ======= z axis settings // setup initial state of the z axis settings if (this._options.z.property.value === '') { this._options.z.disable(); } else { this._options.z.enable(); } this._options.z.property.onchange.push(() => { // Reset min/max when changing property to trigger autoscale this.withInternalUpdate(() => { this._options.z.min.value = NaN; this._options.z.max.value = NaN; }); negativeLogWarning(this._options.z); const was3D = this._plot._fullData[0].type === 'scatter3d'; // If no z property selected -> switch back to 2D if (this._options.z.property.value === '') { if (was3D) { // discard the 3d sample before switching traces this._computeLOD(); // autoscale only once the 2D react has settled, so the gl scene // repaints (a relayout racing the react leaves it stale) void this._switch2D() .then(() => this._autoscaleAxes()) .then(() => this._updateLOD(this._getBounds())) .catch((e: unknown) => setTimeout(() => { throw e; }) ); } return; } // sample the new z coordinates before autoscaling this._computeLOD(); // autoscale must see the new z values, even when already in 3d // wait for the restyle or scene switch to finish first const ready = was3D ? this._restyleFull() : this._switch3D(); void ready .then(() => Plotly.relayout(this._plot, { 'scene.zaxis.title.text': this._title(this._options.z.property.value), } as unknown as Layout) ) .then(() => this._autoscaleAxes()) .then(() => { // The zrange is now known, and we can trigger a proper subsampling const zRange = this._plot._fullLayout.scene.zaxis.range as number[]; this.withInternalUpdate(() => { this._options.z.min.value = zRange[0]; this._options.z.max.value = zRange[1]; }); if (this._is3D()) { this._setScaleStep(this._getBounds().z as number[], 'z'); } // refine the sample once the new axis ranges are known this._updateLOD(this._getBounds()); }) .catch((e: unknown) => { setTimeout(() => { throw e; }); }); }); this._options.z.scale.onchange.push(() => { negativeLogWarning(this._options.z); this._options.setLogLabel(this._options.z, 'z'); if (this._options.z.property.value !== '') { this._rescaleLOD(); this._relayout({ 'scene.zaxis.type': this._options.z.scale.value, } as unknown as Layout); } }); this._options.z.min.onchange.push(rangeChange('zaxis', this._options.z, 'min')); this._options.z.max.onchange.push(rangeChange('zaxis', this._options.z, 'max')); } /** Connect color axis and palette settings to plot updates */ private _connectColorSettings() { // ======= color axis settings if (this._options.hasColors()) { // setup initial color range (must do before setting up events) const determineColorRange = (optionMin: number, optionMax: number) => { const [min, max] = getAxisRange(optionMin, optionMax, 'map.color', this.warnings); const minProvided = min !== undefined; const maxProvided = max !== undefined; const getMinMaxFromValues = () => { return arrayMaxMin(this._getNumericColors()); }; if (minProvided || maxProvided) { // Calculate min/max value from values const { min, max } = getMinMaxFromValues(); return { min: minProvided ? optionMin : min, max: maxProvided ? optionMax : max, }; } // Calculate default range return getMinMaxFromValues(); }; const { min, max } = determineColorRange( this._options.color.min.value, this._options.color.max.value ); this._options.color.min.value = min; this._options.color.max.value = max; this._setScaleStep([min, max], 'color'); // fill the selection range with the data bounds, unless it was // restored from saved settings if ( isNaN(this._options.color.select.min.value) && isNaN(this._options.color.select.max.value) ) { this._resetSelectionRange(); } } else { this._options.color.min.value = 0; this._options.color.max.value = 0; } this._options.color.property.onchange.push(() => { if (this._options.hasColors()) { this._options.color.mode.enable(); this._options.color.min.enable(); this._options.color.max.enable(); this._colorReset.disabled = false; const numericValues = this._getNumericColors(); // Color mode warning needs to be called before setting min and max to avoid isFinite error if (canChangeColors(numericValues, 'property')) { const { min, max } = arrayMaxMin(numericValues); // We have to set max first and min second here to avoid sending // a spurious warning in `colorRangeChange` below in case the // new min is bigger than the old max. this._options.color.min.value = Number.NEGATIVE_INFINITY; this._options.color.max.value = max; this._options.color.min.value = min; this._setScaleStep([min, max], 'color'); const colorbar = this._colorbarSettings(); this._relayout({ 'coloraxis.colorbar.title.text': colorbar.title, 'coloraxis.showscale': this._showColorbar(), 'legend.maxheight': this._legendMaxHeight(), // null clears old category labels when switching to numeric colors 'coloraxis.colorbar.tickmode': colorbar.tickmode, 'coloraxis.colorbar.tickvals': colorbar.tickvals ?? null, 'coloraxis.colorbar.ticktext': colorbar.ticktext ?? null, } as unknown as Layout); // refill the selection range with the new property's bounds this._resetSelectionRange(); } } else { this._options.color.mode.disable(); this._options.color.min.disable(); this._options.color.max.disable(); this._colorReset.disabled = true; this._options.color.min.value = 0; this._options.color.max.value = 0; this._relayout({ 'coloraxis.colorbar.title.text': undefined, 'coloraxis.showscale': false, 'legend.maxheight': this._legendMaxHeight(), } as unknown as Layout); } // the new property's range can reveal previously hidden points if (this._options.color.select.mode.value.startsWith('range')) { this._computeLOD(this._getBounds()); } this._restyleLegendColors(); void this._restyleFull(); }); const colorRangeChange = (minOrMax: 'min' | 'max') => { const min = this._options.color.min.value; const max = this._options.color.max.value; if (min > max) { this.warnings.sendMessage( `The inserted min and max values in color are such that min > max! The last inserted value was reset.` ); if (minOrMax === 'min') { this._options.color.min.reset(); } else { this._options.color.max.reset(); } return; } this._relayout({ 'coloraxis.cmax': max, 'coloraxis.cmin': min, // looks like changing only 'coloraxis.cmax'/'coloraxis.cmin' do // not update the color of the points (although it does change // the colorbar). Asking for an update of 'coloraxis.colorscale' // seems to do the trick. This is possibly a Plotly bug, we // would need to investigate a bit more. 'coloraxis.colorscale': this._options.colorScale(), } as unknown as Layout); // with a subset selection active the points use explicit colors // (valueToColor / grey), which the coloraxis relayout does not touch, // so restyle the marker colors directly if (this._options.color.select.mode.value !== 'all') { this._restyle({ 'marker.color': this._colors() } as Data, [0, 1]); } this._restyleLegendColors(); }; const canChangeColors = (values: number[], changed: string): boolean => { const mode = this._options.color.mode.value; let invalidValues = ''; if (mode === 'log' || mode === 'sqrt') { invalidValues = '<= 0'; } else if (mode === 'inverse') { invalidValues = '== 0'; } const allValuesNaN = values.every((value) => isNaN(value)); const someValuesNaN = values.some((value) => isNaN(value)); if (allValuesNaN) { this.warnings.sendMessage( `The selected property contains only values ${invalidValues}. ` + 'To display this property, select an appropriate color scale. ' + `The ${changed} will be set to its last value.` ); if (changed === 'property') { this._options.color.property.reset(); } else { this._options.color.mode.reset(); } return false; } else if (someValuesNaN) { this.warnings.sendMessage( `The selected property contains some values ${invalidValues}. ` + 'These values will be colored in grey.' ); return true; } else { return true; } }; this._options.color.mode.onchange.push(() => { const numericValues = this._getNumericColors(); // Color mode warning needs to be called before setting min and max to avoid isFinite error if (canChangeColors(numericValues, 'color scale')) { const { min, max } = arrayMaxMin(numericValues); // We have to set min to infinity first, then max, and then min here // to avoid sending a spurious warning in `colorRangeChange` below // in case the new min is bigger than the old max. this._options.color.min.value = Number.NEGATIVE_INFINITY; this._options.color.max.value = max; this._options.color.min.value = min; this._setScaleStep([min, max], 'color'); const colorbar = this._colorbarSettings(); this._relayout({ 'coloraxis.colorbar.title.text': colorbar.title, 'coloraxis.showscale': this._showColorbar(), 'coloraxis.colorbar.tickmode': colorbar.tickmode, 'coloraxis.colorbar.tickvals': colorbar.tickvals ?? null, 'coloraxis.colorbar.ticktext': colorbar.ticktext ?? null, } as unknown as Layout); void this._restyleFull(); } }); this._options.color.min.onchange.push(() => { colorRangeChange('min'); }); this._options.color.max.onchange.push(() => { colorRangeChange('max'); }); this._colorReset.onclick = () => { const numericValues = this._getNumericColors(); const { min, max } = arrayMaxMin(numericValues); this._options.color.min.value = min; this._options.color.max.value = max; this._relayout({ 'coloraxis.cmax': max, 'coloraxis.cmin': min, // same as above regarding update of the points color 'coloraxis.colorscale': this._options.colorScale(), } as unknown as Layout); }; // setup initial state of the color GUI this._options.color.property.enable(); if (this._options.hasColors()) { this._options.color.mode.enable(); this._options.color.min.enable(); this._options.color.max.enable(); this._options.color.opacity.enable(); this._colorReset.disabled = false; } else { this._options.color.min.disable(); this._options.color.max.disable(); this._colorReset.disabled = true; } // ======= color palette this._options.color.palette.onchange.push(() => { this._relayout({ 'coloraxis.colorscale': this._options.colorScale(), } as unknown as Layout); this._restyleLegendColors(); void this._restyleFull(); }); // ======= opacity this._options.color.opacity.onchange.push(() => { this._restyle({ 'marker.opacity': this._options.color.opacity.value / 100, }); }); // ======= selection // Selection changes alter the foreground/background split, which feeds // into LOD prioritization, so recompute LOD before restyling. const updateColors = () => { // ignore programmatic (derived) selection writes if (this._internalUpdate) { return; } // recompute LOD at the current view bounds so the foreground keeps // the same density as the visible region this._computeLOD(this._getBounds()); void this._restyleFull(); }; this._options.color.select.mode.onchange.push(updateColors); this._options.color.select.category.onchange.push(updateColors); this._options.color.select.min.onchange.push(updateColors); this._options.color.select.max.onchange.push(updateColors); } /** Connect symbol settings to plot updates */ private _connectSymbolSettings() { // ======= markers symbols this._options.symbol.onchange.push(() => { this._restyle({ 'marker.symbol': this._symbols() }, [0, 1]); this._restyle({ name: this._legendNames(), showlegend: this._showlegend(), } as unknown as Data); this._restyleLegendColors(); this._relayout({ 'coloraxis.colorbar.len': this._colorbarLen(), 'coloraxis.showscale': this._showColorbar(), 'legend.maxheight': this._legendMaxHeight(), } as unknown as Layout); }); } /** Connect marker size settings to plot updates */ private _connectSizeSettings() { // ======= markers size // setup initial state of the marker size settings if (this._options.size.property.value === '') { this._options.size.mode.disable(); } else { this._options.size.mode.enable(); } this._options.size.property.onchange.push(() => { if (this._options.size.property.value !== '') { this._options.size.mode.enable(); } else { this._options.size.mode.disable(); } this._restyle({ 'marker.size': this._sizes(0) } as Data, 0); }); this._options.size.factor.onchange.push(() => { this._restyle({ 'marker.size': this._sizes(0) } as Data, 0); }); this._options.size.mode.onchange.push(() => { this._restyle({ 'marker.size': this._sizes(0) } as Data, 0); }); } /** * Update options with the structure or atom default or config settings */ private _setupMapOptions() { // Helper function to update `_option` values with related target configuration const refreshMapOptions = (properties: NumericProperties) => { // Check if target's properties actually exist if (properties && Object.keys(properties).length > 1) { // Delete previous target modal from DOM this._options.remove(); // Re-create modal this._options = new MapOptions( this._root, this._getCurrentProperties(), (rect) => this.positionSettingsModal(rect), {}, this.warnings ); // Update reference to the color reset button, as the old one was removed from DOM this._colorReset = this._options.getModalElement('map-color-reset'); // Update LOD toggle visibility based on the new target's dataset size. const nPoints = Object.values(properties)[0].values.length; if (nPoints > PropertiesMap.LOD_MAX_POINTS) { this._options.showLODOption(true); } } }; // Apply structure or atom options based on target if (this._target !== 'atom') { refreshMapOptions(this._data['structure']); } else { refreshMapOptions(this._data['atom']); } } // ====================================================================== // Plotly Wrappers // ====================================================================== /** * Forward to Plotly.react * Updates the Plotly plot with new data and layout with re-rendering * * @param traces array of data traces to update * @param layout layout properties to update * @param config optional plot configuration */ private _react( traces: Plotly.Data[], layout: Partial, config?: Partial ): Promise { return Plotly.react(this._plot, traces, layout, config as Config) .then(() => {}) // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable .catch((error: Error) => { setTimeout(() => { throw error; }); }); } /** * Forward to Plotly.restyle. * Updates specific properties of traces without re-rendering the entire plot * * @param data properties to update * @param traces optional, indices of traces or a single trace index to update */ private _restyle(data: Partial | NestedAttributes, traces?: number | number[]) { Plotly.restyle(this._plot, data, traces).catch((e: unknown) => setTimeout(() => { throw e; }) ); } /** * Helper to trigger a full update of the plot traces (main, selected, dummy) * when data, styling, or selection changes. */ private _restyleFull(layoutUpdate?: Partial): Promise { const fullUpdate: Record = { x: this._coordinates(this._options.x), y: this._coordinates(this._options.y), z: this._coordinates(this._options.z), 'marker.color': this._colors(), 'marker.size': this._sizes(), 'marker.symbol': this._symbols(), 'marker.line.color': this._lineColors(), hovertemplate: this._options.hovertemplate(this._colorType()), customdata: this._colorValues(), 'marker.opacity': this._options.color.opacity.value / 100, visible: this._selectTrace(true, true, this._options.hasColors()), }; const layout = layoutUpdate || {}; if (layoutUpdate || this._is3D()) { if (this._is3D() && !('scene.camera' in layout)) { // explicitely preserve the camera position when updating the plot // to prevent it from snapping back to the default position // see https://github.com/lab-cosmo/chemiscope/issues/310 const currentLayout = this._plot._fullLayout; if (currentLayout.scene !== undefined) { // @ts-expect-error scene is defined in the layout layout['scene.camera'] = currentLayout.scene.camera; } } return Plotly.update( this._plot, fullUpdate as unknown as Data, layout as Layout, [0, 1, 2] ); } else { // Update main (0), selected (1) and dummy (2) traces return Plotly.restyle(this._plot, fullUpdate as unknown as Data, [0, 1, 2]); } } /** * Forward to Plotly.relayout * Updates the layout properties of the plot * * @param layout layout properties to update */ private _relayout(layout: Partial | NestedAttributes) { Plotly.relayout(this._plot, layout).catch((e: unknown) => setTimeout(() => { throw e; }) ); } /** * Coalesce axis-range relayouts issued in the same tick into a single relayout. * Applying settings clears every axis min/max in quick succession, which would * otherwise trigger a separate relayout per bound. */ private _scheduleRangeRelayout(update: Record): void { Object.assign(this._pendingRangeUpdate, update); if (this._rangeFlushScheduled) { return; } this._rangeFlushScheduled = true; queueMicrotask(() => { this._rangeFlushScheduled = false; const pending = this._pendingRangeUpdate; this._pendingRangeUpdate = {}; if (Object.keys(pending).length > 0) { this._relayout(pending as Partial); } }); } /** * Autoscale the axes of the current mode with a standalone autorange relayout, * returning its promise. This is the only update that reliably repaints the gl * scene with the new range, so it must run on its own once the operation that * changed the data or trace type has settled (e.g. from a `.then`), not folded * into a react/update. With `force`, pinned axes are autoscaled too. */ private _autoscaleAxes(force = false): Promise { const update: Record = {}; if (this._is3D()) { update['scene.xaxis.autorange'] = true; update['scene.yaxis.autorange'] = true; update['scene.zaxis.autorange'] = true; } else { // `force` autoscales regardless of pinned min/max (used by the reset), // otherwise only axes the user has not pinned are autoscaled if (force || getAxisAutoRange(this._options.x.min.value, this._options.x.max.value)) { update['xaxis.autorange'] = true; } if (force || getAxisAutoRange(this._options.y.min.value, this._options.y.max.value)) { update['yaxis.autorange'] = true; } } if (Object.keys(update).length === 0) { return Promise.resolve(); } return Plotly.relayout(this._plot, update as unknown as Layout); } // ====================================================================== // Plot Configuration // ====================================================================== /** * Build the traces from the options data */ private _getTraces(): Plotly.Data[] { const type = this._is3D() ? 'scatter3d' : 'scattergl'; // The main trace, containing default data const main = { name: '', type: type, x: this._coordinates(this._options.x, 0)[0], y: this._coordinates(this._options.y, 0)[0], z: this._coordinates(this._options.z, 0)[0], hovertemplate: this._options.hovertemplate(this._colorType()), customdata: this._colorValues(0)[0], marker: { color: this._colors(0)[0], coloraxis: 'coloraxis', showscale: false, line: { color: this._lineColors(0)[0], width: this._options.markerOutline.value ? 0.5 : 0, }, opacity: this._options.color.opacity.value / 100, size: this._sizes(0)[0], sizemode: 'area', symbol: this._symbols(0)[0], }, line: { color: 'black', width: 0.5, dash: 'solid', }, mode: this._options.joinPoints.value ? 'lines+markers' : 'markers', showlegend: false, }; // Create a second trace to store the last clicked point, in order to // display it on top of the main plot with different styling. This is // only used in 3D mode, since it is way slower than moving // this._selectedMarker around. const selected = { name: 'selected', type: type, x: [], y: [], z: [], hoverinfo: 'none', marker: { color: [], line: { color: [], width: 2, }, opacity: 1, size: [], sizemode: 'area', }, mode: 'markers', showlegend: false, }; // Dummy trace to display the colorbar regardless of the styling of // the main trace. Useful when activating selection mode const range = this._getColorRange(); const dummy = { name: 'colorbar-dummy', type: type, x: this._coordinates(this._options.x, 2)[0], y: this._coordinates(this._options.y, 2)[0], z: this._coordinates(this._options.z, 2)[0], marker: { color: [range.min, range.max], coloraxis: 'coloraxis', showscale: true, opacity: 0, size: 0, }, visible: this._options.hasColors(), showlegend: false, hoverinfo: 'none', }; const traces = [main as Data, selected as Data, dummy as Data]; // Calculate legend names and show legend flags based on data properties const legendNames = this._legendNames().slice(3); const showlegend = this._showlegend().slice(3); assert(legendNames.length === showlegend.length); const currentLength = legendNames.length; // Adjust arrays based on maxSymbols configuration if (this._data.maxSymbols > 0) { // resize & fill arrays legendNames.length = this._data.maxSymbols; legendNames.fill('', currentLength); showlegend.length = this._data.maxSymbols; showlegend.fill(false, currentLength); } // add empty traces to be able to display the symbols legend // one trace for each possible symbol const legendColors = this._legendColors(); for (let s = 0; s < this._data.maxSymbols; s++) { const data = { name: legendNames[s], type: type, // We need to add a dummy point to force plotly to display the // associated legend; but we don't want to see the point in the // map. Setting the coordinates to NaN achieve this. x: [NaN], y: [NaN], z: [NaN], marker: { color: legendColors[s], size: 10, symbol: this._is3D() ? get3DSymbol(s) : s, }, mode: 'markers', showlegend: showlegend[s], }; traces.push(data as Data); } return traces; } /** * Builds the layout to be provided to Plotly from the options */ private _getLayout(): Partial { // make a copy of the default layout const layout = JSON.parse(JSON.stringify(DEFAULT_LAYOUT)) as typeof DEFAULT_LAYOUT; // and set values specific to the displayed dataset layout.xaxis.title.text = this._title(this._options.x.property.value); layout.yaxis.title.text = this._title(this._options.y.property.value); layout.xaxis.type = this._options.x.scale.value; layout.yaxis.type = this._options.y.scale.value; layout.scene.xaxis.title.text = this._title(this._options.x.property.value); layout.scene.yaxis.title.text = this._title(this._options.y.property.value); layout.scene.zaxis.title.text = this._title(this._options.z.property.value); layout.scene.xaxis.type = this._options.x.scale.value; layout.scene.yaxis.type = this._options.y.scale.value; layout.scene.zaxis.type = this._options.z.scale.value; layout.coloraxis.colorscale = this._options.colorScale(); layout.coloraxis.cmin = this._options.color.min.value; layout.coloraxis.cmax = this._options.color.max.value; layout.coloraxis.colorbar.len = this._colorbarLen(); const colorbar = this._colorbarSettings(); layout.coloraxis.colorbar.title.text = colorbar.title; layout.coloraxis.colorbar.tickmode = colorbar.tickmode; layout.coloraxis.colorbar.tickvals = colorbar.tickvals; layout.coloraxis.colorbar.ticktext = colorbar.ticktext; layout.coloraxis.showscale = this._showColorbar(); layout.legend.maxheight = this._legendMaxHeight(); // Set ranges for the axes layout.xaxis.range = getAxisRange( this._options.x.min.value, this._options.x.max.value, 'map.x', this.warnings ); layout.yaxis.range = getAxisRange( this._options.y.min.value, this._options.y.max.value, 'map.y', this.warnings ); layout.zaxis.range = getAxisRange( this._options.z.min.value, this._options.z.max.value, 'map.z', this.warnings ); // 3D scenes have a separate axis configuration if (this._is3D()) { layout.scene.xaxis.range = getAxisRange( this._options.x.min.value, this._options.x.max.value, 'map.x', this.warnings ); layout.scene.xaxis.autorange = getAxisAutoRange( this._options.x.min.value, this._options.x.max.value ); layout.scene.yaxis.range = getAxisRange( this._options.y.min.value, this._options.y.max.value, 'map.y', this.warnings ); layout.scene.yaxis.autorange = getAxisAutoRange( this._options.y.min.value, this._options.y.max.value ); layout.scene.zaxis.range = getAxisRange( this._options.z.min.value, this._options.z.max.value, 'map.z', this.warnings ); layout.scene.zaxis.autorange = getAxisAutoRange( this._options.z.min.value, this._options.z.max.value ); layout.dragmode = 'orbit'; if (this._options.camera.value) { const update = cameraToPlotly(this._options.camera.value); const scene = layout.scene; Object.assign(scene.camera, update.camera); Object.assign(scene, { aspectratio: update.aspectratio }); } } return layout as Partial; } /** * Get the Plotly configuration based on current mode (2D/3D) */ private _getConfig(): Config { // Clone default config const config = { ...DEFAULT_CONFIG }; config.modeBarButtonsToRemove = [...DEFAULT_CONFIG.modeBarButtonsToRemove]; config.modeBarButtonsToAdd = [...DEFAULT_CONFIG.modeBarButtonsToAdd]; if (this._is3D()) { config.modeBarButtonsToRemove.push('resetCameraDefault3d'); } // Add custom reset button for both 2D and 3D, so that the reset // always uses the current property ranges instead of the initial ones // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment config.modeBarButtonsToAdd = [ [ { name: 'Reset View', // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access icon: (Plotly as any).Icons.home, click: () => { this._resetToGlobalView(); }, }, // eslint-disable-next-line @typescript-eslint/no-explicit-any ] as any, ...config.modeBarButtonsToAdd, ]; return config as unknown as Config; } // ====================================================================== // Trace Attributes Generators // ====================================================================== /** * Get the values associated with the given `axis`, to use with the given * plotly `trace`, or all of them if `trace === undefined` * * @param axis Options of the axis we need coordinates for * @param trace plotly trace for which we require coordinate * @return data usable with Plotly.restyle */ private _coordinates(axis: AxisOptions, trace?: number): Array { // this happen for the z axis in 2D mode if (axis.property.value === '') { return this._selectTrace(undefined, undefined, undefined, trace); } const values = this._property(axis.property.value).values; // LOD: Apply binning filter for main trace (trace 0) // If trace is undefined (both), _selectTrace distributes the first argument to // main trace let mainValues = values; if (this._options.color.select.mode.value.endsWith('hide')) { const mask = this._getSelectionMask(); mainValues = values.map((v, i) => (mask[i] ? v : NaN)); } if (trace === 0 || trace === undefined) { mainValues = this._applyLOD(mainValues); } // in 2d mode, set all selected markers coordinates to NaN since we are // using HTML markers instead. const selected = []; for (const marker of this._selected.values()) { if (this._is3D()) { selected.push(values[marker.current]); } else { selected.push(NaN); } } const val0 = values[0] || 0; const dummy = [val0, val0]; return this._selectTrace(mainValues, selected, dummy, trace); } /** * Get the color values to use with the given plotly `trace`, or all of * them if `trace === undefined` */ private _colors(trace?: number): Array> { let colors; if (this._options.hasColors()) { colors = this._property(this._options.color.property.value).values; } else { colors = new Array(this._property(this._options.x.property.value).values.length).fill( 0.5 ) as number[]; } const mask = this._getSelectionMask(); const values = this._options.calculateColors(colors); let finalValues = values; if (mask.some((v) => !v)) { // We have some filtered out points. // If values are numbers, we need to convert to colors to mix with RGBA. if (values.length > 0 && typeof values[0] === 'number') { const numValues = values as number[]; const range = this._getColorRange(); const min = range.min; const max = range.max; finalValues = numValues.map((v, i) => { if (!mask[i]) return '#d3d3d3'; return this._options.valueToColor(v, min, max); }); } else { // Already strings (fixed color) finalValues = values.map((v, i) => { if (!mask[i]) return '#d3d3d3'; return v; }); } } // LOD: Apply filter to main trace values const mainValues = trace === 0 || trace === undefined ? this._applyLOD(finalValues) : finalValues; const selected = []; for (const data of this._selected.values()) { selected.push(data.color); } let dummy = [0.5, 0.5]; if (this._options.hasColors()) { const range = this._getColorRange(); dummy = [range.min, range.max]; } return this._selectTrace>(mainValues, selected, dummy, trace); } /** Get the type of the current color property values */ private _colorType(): 'number' | 'integer' | 'string' { if (this._options.hasColors()) { const prop = this._property(this._options.color.property.value); if (prop.string !== undefined) { return 'string'; } if (prop.values.every((v) => Number.isInteger(v))) { return 'integer'; } } return 'number'; } /** * Get the numeric color values to use with the given plotly `trace`, or all of * them if `trace === undefined`. Used for customdata. */ private _colorValues(trace?: number): Array> { if (this._options.hasColors()) { const prop = this._property(this._options.color.property.value); if (prop.string) { const interner = prop.string; const values = prop.values.map((v) => interner.string(v)); const mainValues = trace === 0 || trace === undefined ? this._applyLOD(values) : values; const selected = []; for (const data of this._selected.values()) { const valIndex = prop.values[data.current]; selected.push(interner.string(valIndex)); } const val0 = values[0] || ''; const dummy = [val0, val0]; return this._selectTrace(mainValues, selected, dummy, trace); } else { const rawValues = prop.values; const values = this._options.calculateColors(rawValues); const mainValues = trace === 0 || trace === undefined ? this._applyLOD(values) : values; const selected = []; for (const data of this._selected.values()) { selected.push(values[data.current]); } const range = this._getColorRange(); const dummy = [range.min, range.max]; return this._selectTrace(mainValues, selected, dummy, trace); } } const values = new Array( this._property(this._options.x.property.value).values.length ).fill(NaN); const mainValues = trace === 0 || trace === undefined ? this._applyLOD(values) : values; return this._selectTrace(mainValues, [], [NaN, NaN], trace); } /** * Get the values to use as marker size with the given plotly `trace`, or * all of them if `trace === undefined`. */ private _sizes(trace?: number): Array { let sizes; if (this._options.size.property.value !== '') { sizes = this._property(this._options.size.property.value).values; } else { sizes = new Array(this._property(this._options.x.property.value).values.length).fill( 1.0 ) as number[]; } let values = this._options.calculateSizes(sizes); const mask = this._getSelectionMask(); if (mask.some((v) => !v)) { values = values.map((v, i) => (mask[i] ? v : v * 0.25)); } // LOD: Apply filter to main trace values const mainValues = trace === 0 || trace === undefined ? this._applyLOD(values) : values; const selected = []; if (this._is3D()) { for (const guid of this._selected.keys()) { if (guid === this._active) { selected.push(1000); } else { selected.push(500); } } } const dummy = [0, 0]; return this._selectTrace( mainValues as number | number[], selected, dummy, trace ); } /** * Get the values to use as marker line color with the given plotly `trace`, or * all of them if `trace === undefined`. */ private _lineColors(trace?: number): Array { const n = this._property(this._options.x.property.value).values.length; // dafault to black outline let values = new Array(n).fill('black') as string[]; const mask = this._getSelectionMask(); // unselected points have transparent outline if (mask.some((v) => !v)) { values = values.map((v, i) => (mask[i] ? v : 'rgba(0,0,0,0)')); } // LOD: Apply filter to main trace values const mainValues = trace === 0 || trace === undefined ? this._applyLOD(values) : values; // Assume trace 1 (selection) line color is black. const selected = new Array(this._selected.size).fill('black'); // Dummy trace for colorbar is invisible const dummy = ['rgba(0,0,0,0)', 'rgba(0,0,0,0)']; return this._selectTrace( mainValues as string | string[], selected, dummy, trace ); } /** * Get the values to use as marker symbol with the given plotly `trace`, or * all of them if `trace === undefined`. */ private _symbols(trace?: number): Array { if (this._options.symbol.value === '') { // default to 0 (i.e. circles) return this._selectTrace('circle', 'circle', 'circle', trace); } const property = this._property(this._options.symbol.value); const symbols = this._options.getSymbols(property); // LOD: Apply filter to main trace values const mainValues = trace === 0 || trace === undefined ? this._applyLOD(symbols as (string | number)[]) : symbols; const selected = []; for (const data of this._selected.values()) { selected.push(symbols[data.current]); } const sym0 = symbols[0] || 'circle'; const dummy = [sym0, sym0]; return this._selectTrace( mainValues as typeof symbols, selected as typeof symbols, dummy as typeof symbols, trace ); } /** * Select either main, selected, dummy or all of them depending on `trace`, and return * them in a mode usable with `Plotly.restyle`/{@link PropertiesMap._restyle} */ private _selectTrace(main: T, selected: T, dummy: T, trace?: number): T[] { if (trace === 0) { return [main]; } else if (trace === 1) { return [selected]; } else if (trace === 2) { return [dummy]; } else if (trace === undefined) { return [main, selected, dummy]; } else { throw Error('internal error: invalid trace number'); } } /** * Get the numeric color values after applying all the scaling and transforms * selected by the options. */ private _getNumericColors(): number[] { const propValues = this._property(this._options.color.property.value).values; const values = this._options.calculateColors(propValues); return values.filter((v): v is number => typeof v === 'number'); } /** Range used to assign colors to points and legend markers */ private _getColorRange(): { min: number; max: number } { let min = this._options.color.min.value; let max = this._options.color.max.value; if (isNaN(min) || isNaN(max)) { const numericValues = this._getNumericColors(); const { min: dMin, max: dMax } = arrayMaxMin(numericValues); if (isNaN(min)) min = dMin; if (isNaN(max)) max = dMax; } return { min, max }; } /** * Set option values inside `fn` without running the reactive onchange * handlers that check `_internalUpdate`. Nesting- and exception-safe. */ private withInternalUpdate(fn: () => void): void { const previous = this._internalUpdate; this._internalUpdate = true; try { fn(); } finally { this._internalUpdate = previous; } } /** * Set the selection range (mark: range) bounds to the color property's raw * data min/max — the values the filter compares against (see * `_getSelectionMask`). */ private _resetSelectionRange(): void { if (!this._options.hasColors()) { return; } const values = this._property(this._options.color.property.value).values; const finite = values.filter((v): v is number => typeof v === 'number' && isFinite(v)); if (finite.length === 0) { return; } const { min, max } = arrayMaxMin(finite); // set min to -Infinity first so an intermediate min > max does not trip // the min/max validation while updating both bounds this.withInternalUpdate(() => { this._options.color.select.min.value = Number.NEGATIVE_INFINITY; this._options.color.select.max.value = max; this._options.color.select.min.value = min; }); } /** * Get the mask of selected points based on the current selection mode */ private _getSelectionMask(): boolean[] { const n = this._property(this._options.x.property.value).values.length; if (this._options.color.select.mode.value === 'all') { return new Array(n).fill(true); } let colors; if (this._options.hasColors()) { colors = this._property(this._options.color.property.value).values; } else { // "Fixed" color corresponds to a constant value // (should not be used for filtering but should never happen) colors = new Array(n).fill(0.5); } let categoryValues: string[] | undefined; const catProp = this._options.getCategorySelectionProperty(); if (catProp) { const prop = this._property(catProp); if (prop.string) { const interner = prop.string; categoryValues = prop.values.map((v) => interner.string(v)); } } return this._options.getFilterMask(colors, categoryValues); } /** Get the length of the colorbar to accommodate for the legend */ private _colorbarLen(): number { /// Heigh of a legend item in plot unit const count = this._symbolsCount(); const margins = DEFAULT_LAYOUT.margin.t + DEFAULT_LAYOUT.margin.b; const plotHeight = this._plot.clientHeight - margins; // no height yet, the resize observer will recompute this if (count === 0 || plotHeight <= 0) { return 1; } const LEGEND_ITEM_HEIGHT_PX = 19; const legendHeight = (count * LEGEND_ITEM_HEIGHT_PX + 10) / plotHeight; const PADDING = 0.025; // a longer legend gets a scrollbar instead of taking more room return 1 - Math.min(legendHeight, DEFAULT_LAYOUT.legend.maxheight) - PADDING; } /** Colorbar title and ticks for the current property and color transform */ private _colorbarSettings(): { title: string; tickmode: 'auto' | 'array'; tickvals: number[] | undefined; ticktext: string[] | undefined; } { if (this._options.hasColors()) { const property = this._property(this._options.color.property.value); if (property.string !== undefined) { const names = property.string.strings(); // Category indices transform them like point colors const values = this._options.calculateColors(names.map((_, i) => i)); const tickvals: number[] = []; const ticktext: string[] = []; values.forEach((value, i) => { // invalid transformed values have no position on the colorbar if (typeof value === 'number' && isFinite(value)) { tickvals.push(value); ticktext.push(truncateLegendName(names[i])); } }); // Omit the title to leave room for the category names return { title: '', tickmode: 'array', tickvals, ticktext }; } } const title = transformedAxisLabel( this._title(this._options.color.property.value), this._options.color.mode.value, true ); return { title, tickmode: 'auto', tickvals: undefined, ticktext: undefined }; } /** Max height of the legend which can use the whole plot without a colorbar */ private _legendMaxHeight(): number { return this._showColorbar() ? DEFAULT_LAYOUT.legend.maxheight : 1; } /** Should we show the legend for the various symbols used? */ private _showlegend(): boolean[] { const result = [false, false, false]; const activeCount = this._symbolsCount(); for (let i = 0; i < this._data.maxSymbols; i++) { result.push(i < activeCount); } return result; } /** Get the list of symbols names to use for the legend */ private _legendNames(): string[] { const result = ['', '', '']; const symbolValue = this._options.symbol.value; const names = symbolValue !== '' ? (this._property(symbolValue).string?.strings() ?? []) : []; for (let i = 0; i < this._data.maxSymbols; i++) { result.push(truncateLegendName(names[i] ?? '')); } return result; } /** Check whether the same property controls symbol and color */ private _legendCarriesColors(): boolean { const symbol = this._options.symbol.value; return symbol !== '' && this._options.color.property.value === symbol; } /** Show a colorbar */ private _showColorbar(): boolean { return this._options.hasColors() && !this._legendCarriesColors(); } /** Use category colors in the legend */ private _legendColors(): string[] { const colors = new Array(Math.max(this._data.maxSymbols, 0)).fill('black'); if (this._legendCarriesColors()) { // Use the same transform and color range as the plotted points const { min, max } = this._getColorRange(); const values = this._options.calculateColors( Array.from({ length: this._symbolsCount() }, (_, i) => i) ); values.forEach((value, i) => { colors[i] = typeof value === 'number' ? this._options.valueToColor(value, min, max) : value; }); } return colors; } /** Refresh legend colors */ private _restyleLegendColors(): void { const colors = this._legendColors(); if (colors.length === 0) { return; } // The first three traces hold the points, selection and invisible colorbar points const traces = colors.map((_, i) => 3 + i); this._restyle({ 'marker.color': colors } as unknown as Data, traces); } /** * Show a tooltip with the full name when hovering truncated legend * entries */ private _setupLegendTooltips(): void { const tooltip = document.createElement('div'); tooltip.className = 'chsp-legend-tooltip'; this._root.appendChild(tooltip); this._plot.addEventListener('mouseover', (event) => { const item = (event.target as Element).closest('g.traces'); const label = item?.querySelector('.legendtext')?.getAttribute('data-unformatted'); const noSymbol = this._options.symbol.value === ''; if (!item || !label || !label.endsWith(LEGEND_TRUNCATION_MARK) || noSymbol) { return; } const names = this._property(this._options.symbol.value).string?.strings() ?? []; const fullName = names.find((name) => truncateLegendName(name) === label); if (fullName === undefined) { return; } tooltip.textContent = fullName; tooltip.style.display = 'block'; const rootRect = this._root.getBoundingClientRect(); const itemRect = item.getBoundingClientRect(); tooltip.style.top = `${itemRect.bottom - rootRect.top + 4}px`; const left = Math.min( itemRect.left - rootRect.left, rootRect.width - tooltip.offsetWidth - 8 ); tooltip.style.left = `${Math.max(0, left)}px`; }); this._plot.addEventListener('mouseout', (event) => { const item = (event.target as Element).closest('g.traces'); if (item && !item.contains(event.relatedTarget as Node)) { tooltip.style.display = 'none'; } }); } /** How many symbols are on this plot?*/ private _symbolsCount(): number { if (this._options.symbol.value !== '') { const property = this._property(this._options.symbol.value); assert(property.string !== undefined); return property.string.strings().length; } return 0; } private _title(name: string): string { let propertyTitle = name; if (name !== '') { const units = this._property(name).units; if (units !== undefined) { propertyTitle = name + ` / ${units}`; } } return propertyTitle; } // ====================================================================== // Axis & View Helpers // ====================================================================== private _handleAxisPropertyChange(axis: 'x' | 'y'): void { const axisOptions = this._options[axis]; // Reset min/max when changing property to trigger autoscale this.withInternalUpdate(() => { axisOptions.min.value = NaN; axisOptions.max.value = NaN; }); // Recompute LOD since axis property changed this._computeLOD(); // update the data and the axis title, then autoscale once that has settled // (see _autoscaleAxes) void this._restyleFull({ [`scene.${axis}axis.title.text`]: this._title(axisOptions.property.value), [`${axis}axis.title.text`]: this._title(axisOptions.property.value), } as unknown as Partial) .then(() => this._autoscaleAxes()) .catch((e: unknown) => setTimeout(() => { throw e; }) ); const bounds = this._getBounds(); this._setScaleStep(bounds[axis], axis); } /** Sync axis min/max so the UI reflects the current visible ranges in the plot */ private _updateAxisSettings(bounds: Bounds): void { this.withInternalUpdate(() => { this._options.x.min.value = bounds.x[0]; this._options.x.max.value = bounds.x[1]; this._options.y.min.value = bounds.y[0]; this._options.y.max.value = bounds.y[1]; if (bounds.z !== undefined) { this._options.z.min.value = bounds.z[0]; this._options.z.max.value = bounds.z[1]; } }); } /** Changes the step of the arrow buttons in min/max input based on dataset range*/ private _setScaleStep(axisBounds: number[], name: 'x' | 'y' | 'z' | 'color'): void { if (axisBounds !== undefined) { // round to 10 decimal places so it does not break in Firefox const step = Math.round(((axisBounds[1] - axisBounds[0]) / 20) * 10 ** 10) / 10 ** 10; const minElement = this._options.getModalElement(`map-${name}-min`); const maxElement = this._options.getModalElement(`map-${name}-max`); minElement.step = `${step}`; maxElement.step = `${step}`; } } // Get the current boundaries on x/y/z axis private _getBounds(): Bounds { if (this._is3D()) { // HACK: `_fullLayout` is not public, so it might break const layout = this._plot._fullLayout.scene; // camera-only updates can leave layout ranges stale const scene = layout._scene; if (scene !== undefined && scene.glplot !== undefined) { // undo plotly's internal coordinate scaling const bounds = scene.glplot.bounds; const range = (i: number): [number, number] => [ bounds[0][i] / scene.dataScale[i], bounds[1][i] / scene.dataScale[i], ]; return { x: range(0), y: range(1), z: range(2) }; } return { x: layout.xaxis.range as [number, number], y: layout.yaxis.range as [number, number], z: layout.zaxis.range as [number, number], }; } else { const layout = this._plot._fullLayout; return { x: layout.xaxis.range as [number, number], y: layout.yaxis.range as [number, number], }; } } // Computes the pixel coordinate inside the plot div of a given value along // the given axis private _pixelCoordinate(value: number, axisName: 'x' | 'y'): number { assert(!this._is3D()); let axis; switch (axisName) { case 'x': axis = this._plot._fullLayout.xaxis; break; case 'y': axis = this._plot._fullLayout.yaxis; break; } return axis.l2p(value) + axis._offset; } // ====================================================================== // Interaction & Event Handlers // ====================================================================== /** * Function used as callback to update the axis ranges in settings after * the user changes zoom or range on the plot */ private _afterplot(): void { if (this._lodBusy) { return; } // Set camera if (this._is3D()) { const { camera, aspectratio } = this._plot._fullLayout.scene; this._options.camera.setValue(plotlyToCamera({ camera, aspectratio }), 'DOM'); } const bounds = this._getBounds(); this._updateAxisSettings(bounds); // Update markers for 2D mode if (!this._is3D()) { this._updateMarkers(); } } /** * Resets the view to global bounds and recomputes LOD on the full dataset. * Used for double-click and autoscale events. */ private _resetToGlobalView() { // Skip if another LOD update is in progress if (this._lodBusy) { return; } this._lodBusy = true; // Reset settings to Auto (NaN) this.withInternalUpdate(() => { this._options.x.min.value = NaN; this._options.x.max.value = NaN; this._options.y.min.value = NaN; this._options.y.max.value = NaN; this._options.z.min.value = NaN; this._options.z.max.value = NaN; }); // recompute the global LOD so autorange uses the full dataset this._computeLOD(); // restyle the data first (and, in 3D, reset the camera/aspect ratio), then // autoscale once that has settled (see _autoscaleAxes) const layoutUpdate: Record = {}; if (this._is3D()) { layoutUpdate['scene.aspectratio'] = { x: 1, y: 1, z: 1 }; layoutUpdate['scene.camera'] = { center: { x: 0, y: 0, z: 0 }, eye: { x: 1.25, y: 1.25, z: 1.25 }, projection: { type: 'orthographic' }, up: { x: 0, y: 0, z: 1 }, }; } // Release the lock before the async chain so `_afterplot` (which bails while // `_lodBusy`) can run once the autoscale has settled. this._lodBusy = false; void this._restyleFull( this._is3D() ? (layoutUpdate as unknown as Partial) : undefined ) // force: the reset always autoscales, even if the axes were pinned .then(() => this._autoscaleAxes(true)) .then(() => { if (!this._is3D()) { this._updateMarkers(); } // sync the settings UI to the freshly autoscaled ranges (must run // after the autoscale, otherwise it would write the old range back) this._afterplot(); }) .catch((e: unknown) => setTimeout(() => { throw e; }) ); } /** Switch current plot from 2D to 3D */ private _switch3D(): Promise { assert(this._is3D()); this._options.z.enable(); // Ensure 3D traces are prepared // Show selected environments markers is handled by _getTraces logic for 3D // _switch3D logic for markers: for (const data of this._selected.values()) { data.toggleVisible(false); } this._updateMarkers(); // We do a full react to update config (modebar) // Settings (like markers) are applied in _getTraces/_getLayout // which rely on _options and internal state. _options.z is enabled above. return this._react(this._getTraces(), this._getLayout(), this._getConfig()); } /** Switch current plot from 3D back to 2D */ private _switch2D(): Promise { assert(!this._is3D()); this._options.z.disable(); // Show selected environments markers for (const data of this._selected.values()) { data.toggleVisible(true); } // We do a full react to update config (modebar) // _getTraces uses _sizes() and options to set marker line width and size // consistently in 2D and 3D return this._react(this._getTraces(), this._getLayout(), this._getConfig()); } // ====================================================================== // Markers Management // ====================================================================== /** * Processes the markers by updating their current value to structure or environment index on the current target * In 'structure' target, it set structure index, and in 'atom' target its environment index */ private _handleMarkers() { for (const [key, marker] of this._selected.entries()) { if (this._target === 'structure') { marker.current = this._indexer.fromEnvironment(marker.current, 'atom').structure; } else { const environment = this._indexer.fromStructure( marker.current, 'atom' )?.environment; assert(environment !== undefined); marker.current = environment; } this._selected.set(key, marker); } } /** * Update the position, color & size of markers. If `markers` is present, only * markers inside the array are updated, otherwise everything is updated. */ private _updateMarkers(markers?: MarkerData[]): void { if (markers === undefined) { markers = Array.from(this._selected.values()); } if (this._is3D()) { markers.forEach((marker) => marker.toggleVisible(false)); this._restyle( { 'marker.color': this._colors(1), 'marker.opacity': this._options.color.opacity.value / 100, 'marker.size': this._sizes(1), 'marker.symbol': this._symbols(1), 'marker.line.color': this._lineColors(1), x: this._coordinates(this._options.x, 1), y: this._coordinates(this._options.y, 1), z: this._coordinates(this._options.z, 1), } as Data, 1 ); } else { // LOD: Retrieve raw values for marker positioning, skipping _coordinates // because _coordinates applies LOD filtering which might hide the marker's index const allX = this._property(this._options.x.property.value).values; const allY = this._property(this._options.y.property.value).values; const plotWidth = this._plot.getBoundingClientRect().width; for (const marker of markers) { let x = allX[marker.current]; let y = allY[marker.current]; // make sure we deal with log scale if (this._options.x.scale.value === 'log') { x = Math.log10(x); } if (this._options.y.scale.value === 'log') { y = Math.log10(y); } // convert to pixel coordinates x = plotWidth - this._pixelCoordinate(x, 'x'); y = this._pixelCoordinate(y, 'y'); const bounds = this._getBounds(); const xMin = plotWidth - this._pixelCoordinate(bounds.x[1], 'x'); const xMax = plotWidth - this._pixelCoordinate(bounds.x[0], 'x'); // pixel coordinates are inverted for y (y is measured from the // top of the page, not the bottom) const yMin = this._pixelCoordinate(bounds.y[1], 'y'); const yMax = this._pixelCoordinate(bounds.y[0], 'y'); const isInsideRange = (value: number, min: number, max: number) => { // allow points a bit outside of the range, according to // this the tolerance value in pixels const tolerance = 10; return value + tolerance > min && value - tolerance < max; }; if (isInsideRange(x, xMin, xMax) && isInsideRange(y, yMin, yMax)) { marker.toggleVisible(true); marker.update({ x, y }); } else { marker.toggleVisible(false); } } } } // ====================================================================== // Level of Detail (LOD) // ====================================================================== // axes or filters change => _computeLOD builds a new ranking and chooses points // zoom, pan or rotation => _updateLOD calls _selectLOD to reuse that ranking // linear/log switch => _rescaleLOD rebuilds the ranking and redraws /** Build a new sampler for the current axes and filters, then choose points to show */ private _computeLOD(bounds?: Bounds): void { // clear the old sample even if this update no longer needs LOD this._lod = null; this._lodIndices = null; if (!this._options.useLOD.value) { return; } const xValues = this._property(this._options.x.property.value).values; // small datasets can display every point if (xValues.length <= PropertiesMap.LOD_MAX_POINTS) { return; } // hide mode excludes background points, other filters give foreground priority const selectMode = this._options.color.select.mode.value; let priority: boolean[] | undefined; let visible: boolean[] | undefined; if (selectMode !== 'all') { const mask = this._getSelectionMask(); if (selectMode.endsWith('hide')) { visible = mask; const foreground: number[] = []; for (let i = 0; i < mask.length; i++) { if (mask[i]) { foreground.push(i); } } if (foreground.length <= PropertiesMap.LOD_MAX_POINTS) { // show every remaining point when filtering makes sampling unnecessary this._lodIndices = foreground; return; } } else { // prefer matching points within each cell, but still allow the others priority = mask; } } // rank in plot coordinates, so that log axes match the view bounds const axes = [this._options.x, this._options.y]; if (this._is3D() && this._options.z.property.value !== '') { axes.push(this._options.z); } const coordinates = axes.map((axis) => { const values = this._property(axis.property.value).values; // zero and negative inputs have no finite log coordinate return axis.scale.value === 'log' ? values.map((v) => Math.log10(v)) : values; }); // the sampler owns the ranking, the map uses its selected point ids this._lod = new LODSampler(coordinates, PropertiesMap.LOD_MAX_POINTS, priority, visible); this._selectLOD(bounds); } /** Rebuild and redraw because switching linear/log changes the sampling coordinates */ private _rescaleLOD(): void { if (this._lod === null) { return; } // changing axis scale makes the cached coordinates obsolete this._computeLOD(); void this._restyleFull(); } /** * Choose point ids from the existing ranking without redrawing the plot * Return whether the ids changed, so the caller knows if a redraw is needed */ private _selectLOD(bounds?: Bounds): boolean { if (this._lod === null) { return false; } let changed; // without view bounds, start with a sample of the full dataset if (bounds === undefined) { changed = this._lod.select(); } else if (this._is3D()) { // rotations change screen visibility even when axis bounds stay fixed const size = this._plot._fullLayout._size; // plotly saves the camera and zoom before emitting relayout const { camera, aspectratio } = this._plot._fullLayout.scene; changed = this._lod.selectCamera(bounds, camera, aspectratio, size.w / size.h); } else { changed = this._lod.select([bounds.x, bounds.y]); } // all trace arrays use these same ids to stay aligned this._lodIndices = this._lod.indices; return changed; } /** After zoom or pan, redraw only if the selected points changed */ private _updateLOD(bounds: Bounds): void { // Early exit if another LOD update is already in progress if (this._lodBusy) { return; } this._lodBusy = true; try { if (this._selectLOD(bounds)) { // new ids also need matching colors, sizes and hover data void this._restyleFull(); } } finally { this._lodBusy = false; } } /** * Helper to filter data arrays using the computed LOD indices. */ private _applyLOD(values: T[]): T[] { if (this._lodIndices === null) { return values; } const result = new Array(this._lodIndices.length); for (let i = 0; i < this._lodIndices.length; i++) { result[i] = values[this._lodIndices[i]]; } return result; } // ====================================================================== // General Helpers // ====================================================================== /** Get the property with the given name */ private _property(name: string): NumericProperty { const result = this._data[this._target][name]; if (result === undefined) { throw Error(`unknown property '${name}' requested in map`); } return result; } /** Returns the properties related to the target (structure or environment) */ private _getCurrentProperties() { const properties = this._data[this._target]; const propertiesNames = Object.keys(properties); if (propertiesNames.length < 2) { // better error message in case the user forgot to give the // environments in the data if (this._target === 'structure') { if (Object.keys(this._data['atom']).length >= 2) { throw Error( 'could not find enough structure properties to display, \n but there are atom properties. You could choose "atom" as \n target to display them.' ); } } let message = 'we need at least two properties to plot in the map'; if (propertiesNames.length === 0) { message += ', we have none'; } else { message += `, we have only one: '${propertiesNames[0]}'`; } throw Error(message); } return properties; } /** Is the the current plot a 3D plot? */ private _is3D(): boolean { return this._options.is3D(); } }