Skip to content

Commit 8adc018

Browse files
committed
add not defined category to legends
1 parent 1616b36 commit 8adc018

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

components/MyLeafletMap.vue

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility
1212
const props = defineProps<{
1313
fetchedData?: GeoJSON.FeatureCollection | null
1414
}>();
15+
1516
const emit = defineEmits<{
1617
(e: 'marker-click', feature: GeoJSON.Feature): void
1718
}>();
19+
20+
const NO_VALUE_COLOR = '#999999';
21+
22+
const { t } = useI18n();
23+
1824
let selectedMarker: L.Layer | null = null;
1925
const originalMarkerStyleMap = new Map<L.Layer, L.PathOptions>();
2026
let legendControl: L.Control | null = null;
@@ -59,14 +65,17 @@ function generateLabels(data: GeoJSON.FeatureCollection): Map<string, string> {
5965
const key = labelKey ?? 'default';
6066
const legendDetail = (data.features[0].properties?.options?.legend_details || []) as LegendDetails[];
6167
62-
const rawValues: (string | number)[] = data.features.map(f => findValueByKey(f, key) ?? 'default');
68+
const rawValues = data.features.map(f => findValueByKey(f, key));
6369
64-
const uniqueValues: string[] = Array.from(
65-
new Set(rawValues.map(v => String(v))),
70+
const uniqueValues = Array.from(
71+
new Set(rawValues.map(v => v === undefined ? undefined : String(v))),
6672
);
6773
6874
if (legendDisplayOption[0] === 'default') {
6975
uniqueValues.forEach((value) => {
76+
if (value === undefined) {
77+
return;
78+
}
7079
const match = legendDetail.find((item: LegendDetails) => item.label === value);
7180
if (match?.color) {
7281
colorMap.set(value, match.color);
@@ -88,29 +97,35 @@ function generateLabels(data: GeoJSON.FeatureCollection): Map<string, string> {
8897
</div>`;
8998
}
9099
});
100+
if (uniqueValues.includes(undefined)) {
101+
div.innerHTML += `
102+
<div style="color:black; margin-bottom:4px;">
103+
<i style="background:${NO_VALUE_COLOR}; width:12px; height:12px; display:inline-block; margin-right:4px;"></i> ${t('notDefined')}
104+
</div>`;
105+
}
91106
92107
return div;
93108
};
94109
}
95110
else if (legendDisplayOption[0] === 'colorVarient') {
96111
const numericValues: number[] = uniqueValues
112+
.filter(v => v !== undefined)
97113
.map(v => +v)
98114
.filter(v => !Number.isNaN(v));
99115
100-
if (numericValues.length === 0)
101-
return colorMap;
102-
103-
const min = Math.min(...numericValues);
104-
const max = Math.max(...numericValues);
116+
const bins: [number, number][] = [];
117+
if (numericValues.length > 0) {
118+
const min = Math.min(...numericValues);
119+
const max = Math.max(...numericValues);
105120
106-
const numBins = 5;
107-
const step = (max - min) / numBins;
121+
const numBins = 5;
122+
const step = (max - min) / numBins;
108123
109-
const bins: [number, number][] = [];
110-
for (let i = 0; i < numBins; i++) {
111-
const start = min + i * step;
112-
const end = i === numBins - 1 ? max : start + step;
113-
bins.push([start, end]);
124+
for (let i = 0; i < numBins; i++) {
125+
const start = min + i * step;
126+
const end = i === numBins - 1 ? max : start + step;
127+
bins.push([start, end]);
128+
}
114129
}
115130
116131
function getBinLabel(value: number): string {
@@ -142,6 +157,13 @@ function generateLabels(data: GeoJSON.FeatureCollection): Map<string, string> {
142157
</div>`;
143158
});
144159
160+
if (uniqueValues.includes(undefined)) {
161+
div.innerHTML += `
162+
<div style="color:black; margin-bottom:4px;">
163+
<i style="background:${NO_VALUE_COLOR}; width:12px; height:12px; display:inline-block; margin-right:4px;"></i> ${t('notDefined')}
164+
</div>`;
165+
}
166+
145167
return div;
146168
};
147169
@@ -193,7 +215,7 @@ function renderMarkers(data: GeoJSON.FeatureCollection | undefined) {
193215
key = feature.properties?.__binLabel;
194216
}
195217
196-
const color = colorMap.get(key) ?? '#999';
218+
const color = colorMap.get(key) ?? NO_VALUE_COLOR;
197219
198220
const geoJsonLayer = L.geoJSON(feature, {
199221
style: () => ({

composables/dataTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface LegendDetails {
2525

2626
export interface Options {
2727
label_option: string
28-
legend_option: string
28+
legend_option: 'default' | 'colorVariant'
2929
type: string
3030
value_group: string
3131
coordinate_field_x?: string

i18n/locales/de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"organization": "Organisation",
1313
"datasetUrl": "Datensatz-URL",
1414
"name": "Name",
15-
"seriesDatasets": "Serien-Datensätze"
15+
"seriesDatasets": "Serien-Datensätze",
16+
"notDefined": "Nicht definiert"
1617
}

i18n/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"organization": "Organization",
1313
"datasetUrl": "Dataset url",
1414
"name": "Name",
15-
"seriesDatasets": "Series Datasets"
15+
"seriesDatasets": "Series Datasets",
16+
"notDefined": "Not defined"
1617
}

0 commit comments

Comments
 (0)