mirror of
https://github.com/grafana/grafana.git
synced 2025-01-08 23:23:45 -06:00
Geomap: set value default on init (#36960)
This commit is contained in:
parent
3ab9249f16
commit
46eac518d3
@ -34,7 +34,7 @@ export interface FrameGeometrySource {
|
||||
lookup?: string;
|
||||
|
||||
// Path to a mappings file
|
||||
lookupSrc: string;
|
||||
lookupSrc?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,9 +8,9 @@ describe('scale dimensions', () => {
|
||||
});
|
||||
expect(out).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"fixed": 2.5,
|
||||
"fixed": 7.5,
|
||||
"max": 10,
|
||||
"min": 2.5,
|
||||
"min": 5,
|
||||
}
|
||||
`);
|
||||
});
|
||||
@ -29,7 +29,7 @@ describe('scale dimensions', () => {
|
||||
);
|
||||
expect(out).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"fixed": 7,
|
||||
"fixed": 10,
|
||||
"max": 7,
|
||||
"min": 5,
|
||||
}
|
||||
|
@ -81,13 +81,18 @@ export function validateScaleConfig(copy: ScaleDimensionConfig, options: ScaleDi
|
||||
if (copy.max > max) {
|
||||
copy.max = max;
|
||||
}
|
||||
|
||||
if (copy.fixed == null) {
|
||||
copy.fixed = copy.min = (copy.max - copy.min) / 2.0;
|
||||
copy.fixed = copy.min + (copy.max - copy.min) / 2.0;
|
||||
}
|
||||
if (copy.fixed > copy.max) {
|
||||
copy.fixed = copy.max;
|
||||
} else if (copy.fixed < copy.min) {
|
||||
copy.fixed = copy.min;
|
||||
|
||||
// Make sure the field value is within the absolute range
|
||||
if (!copy.field) {
|
||||
if (copy.fixed > max) {
|
||||
copy.fixed = max;
|
||||
} else if (copy.fixed < min) {
|
||||
copy.fixed = min;
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, fil
|
||||
if (!layer || !(layer.registerOptionsUI || layer.showLocation || layer.showOpacity)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const builder = new PanelOptionsEditorBuilder();
|
||||
if (layer.showLocation) {
|
||||
builder
|
||||
@ -112,7 +113,13 @@ export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, fil
|
||||
options: options,
|
||||
};
|
||||
|
||||
const currentOptions = { ...options, config: { ...layer.defaultOptions, ...options?.config } };
|
||||
const currentOptions = { ...options, type: layer.id, config: { ...layer.defaultOptions, ...options?.config } };
|
||||
|
||||
// Update the panel options if not set
|
||||
if (!options || (layer.defaultOptions && !options.config)) {
|
||||
onChange(currentOptions as any);
|
||||
}
|
||||
|
||||
const reg = optionsEditorBuilder.getRegistry();
|
||||
|
||||
// Load the options into categories
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { MapLayerRegistryItem, MapLayerOptions, MapLayerHandler, PanelData, GrafanaTheme2 } from '@grafana/data';
|
||||
import { MapLayerRegistryItem, MapLayerOptions, MapLayerHandler, PanelData, GrafanaTheme2, FrameGeometrySourceMode } from '@grafana/data';
|
||||
import Map from 'ol/Map';
|
||||
import Feature from 'ol/Feature';
|
||||
import * as layer from 'ol/layer';
|
||||
@ -23,8 +23,8 @@ export interface MarkersConfig {
|
||||
const defaultOptions: MarkersConfig = {
|
||||
size: {
|
||||
fixed: 5,
|
||||
min: 5,
|
||||
max: 10,
|
||||
min: 2,
|
||||
max: 15,
|
||||
},
|
||||
color: {
|
||||
fixed: '#f00',
|
||||
@ -34,6 +34,15 @@ const defaultOptions: MarkersConfig = {
|
||||
|
||||
export const MARKERS_LAYER_ID = "markers";
|
||||
|
||||
// Used by default when nothing is configured
|
||||
export const defaultMarkersConfig:MapLayerOptions<MarkersConfig> = {
|
||||
type: MARKERS_LAYER_ID,
|
||||
config: defaultOptions,
|
||||
location: {
|
||||
mode: FrameGeometrySourceMode.Auto,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map layer configuration for circle overlay
|
||||
*/
|
||||
@ -51,13 +60,18 @@ export const markersLayer: MapLayerRegistryItem<MarkersConfig> = {
|
||||
create: (map: Map, options: MapLayerOptions<MarkersConfig>, theme: GrafanaTheme2): MapLayerHandler => {
|
||||
const matchers = getLocationMatchers(options.location);
|
||||
const vectorLayer = new layer.Vector({});
|
||||
// Assert default values
|
||||
const config = {
|
||||
...defaultOptions,
|
||||
...options?.config,
|
||||
};
|
||||
|
||||
return {
|
||||
init: () => vectorLayer,
|
||||
update: (data: PanelData) => {
|
||||
if(!data.series?.length) {
|
||||
return; // ignore empty
|
||||
}
|
||||
|
||||
const frame = data.series[0];
|
||||
const info = dataFrameToPoints(frame, matchers);
|
||||
if(info.warning) {
|
||||
@ -65,11 +79,6 @@ export const markersLayer: MapLayerRegistryItem<MarkersConfig> = {
|
||||
return; // ???
|
||||
}
|
||||
|
||||
// Assert default values
|
||||
const config = {
|
||||
...defaultOptions,
|
||||
...options?.config,
|
||||
};
|
||||
const colorDim = getColorDimension(frame, config.color, theme);
|
||||
const sizeDim = getScaledDimension(frame, config.size);
|
||||
const opacity = options.config?.fillOpacity ?? defaultOptions.fillOpacity;
|
||||
@ -152,6 +161,7 @@ export const markersLayer: MapLayerRegistryItem<MarkersConfig> = {
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
// fill in the default values
|
||||
defaultOptions,
|
||||
};
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { FrameGeometrySourceMode, PanelPlugin } from '@grafana/data';
|
||||
import { PanelPlugin } from '@grafana/data';
|
||||
import { BaseLayerEditor } from './editor/BaseLayerEditor';
|
||||
import { DataLayersEditor } from './editor/DataLayersEditor';
|
||||
import { GeomapPanel } from './GeomapPanel';
|
||||
@ -6,7 +6,7 @@ import { MapViewEditor } from './editor/MapViewEditor';
|
||||
import { defaultView, GeomapPanelOptions } from './types';
|
||||
import { mapPanelChangedHandler } from './migrations';
|
||||
import { defaultGrafanaThemedMap } from './layers/basemaps';
|
||||
import { MARKERS_LAYER_ID } from './layers/data/markersLayer';
|
||||
import { defaultMarkersConfig } from './layers/data/markersLayer';
|
||||
|
||||
export const plugin = new PanelPlugin<GeomapPanelOptions>(GeomapPanel)
|
||||
.setNoPadding()
|
||||
@ -51,15 +51,7 @@ export const plugin = new PanelPlugin<GeomapPanelOptions>(GeomapPanel)
|
||||
path: 'layers',
|
||||
name: 'Data Layer',
|
||||
editor: DataLayersEditor,
|
||||
defaultValue: [
|
||||
{
|
||||
type: MARKERS_LAYER_ID,
|
||||
config: {},
|
||||
location: {
|
||||
mode: FrameGeometrySourceMode.Auto,
|
||||
},
|
||||
},
|
||||
],
|
||||
defaultValue: [defaultMarkersConfig],
|
||||
});
|
||||
|
||||
// The controls section
|
||||
|
Loading…
Reference in New Issue
Block a user