From 9638278cbbde6f087aea91e1220a9c85c3c55989 Mon Sep 17 00:00:00 2001 From: MohamedHamou Date: Mon, 1 Jun 2020 13:34:08 +0200 Subject: [PATCH] dd several colors from field --- src/contributors/MapContributor.ts | 106 ++++++++++++------ .../mapContributorConf.schema.json | 10 +- src/models/models.ts | 5 +- 3 files changed, 84 insertions(+), 37 deletions(-) diff --git a/src/contributors/MapContributor.ts b/src/contributors/MapContributor.ts index a74082eb..ba097a7a 100644 --- a/src/contributors/MapContributor.ts +++ b/src/contributors/MapContributor.ts @@ -864,22 +864,24 @@ export class MapContributor extends Contributor { if (normalizations) { normalizations.forEach(n => this.normalize(feature, n)); } - const colorField = this.featureLayersIndex.get(s).colorField; - if (colorField) { - const flattenColorField = colorField.replace(/\./g, this.FLAT_CHAR); - feature.properties[flattenColorField + '_color'] = getHexColor(feature.properties[flattenColorField], 0.5); - - /** set the key-to-color map to be displayed on the legend. */ - let colorLegend: LegendData = this.legendData.get(flattenColorField + '_color'); - if (!colorLegend) { - colorLegend = {}; - colorLegend.keysColorsMap = new Map(); - } else if (!colorLegend.keysColorsMap) { - colorLegend.keysColorsMap = new Map(); - } - colorLegend.keysColorsMap.set(feature.properties[flattenColorField], - feature.properties[flattenColorField + '_color']); - this.legendData.set(flattenColorField + '_color', colorLegend); + const colorFields = this.featureLayersIndex.get(s).colorFields; + if (colorFields) { + colorFields.forEach(colorField => { + const flattenColorField = colorField.replace(/\./g, this.FLAT_CHAR); + feature.properties[flattenColorField + '_color'] = getHexColor(feature.properties[flattenColorField], 0.5); + + /** set the key-to-color map to be displayed on the legend. */ + let colorLegend: LegendData = this.legendData.get(flattenColorField + '_color'); + if (!colorLegend) { + colorLegend = {}; + colorLegend.keysColorsMap = new Map(); + } else if (!colorLegend.keysColorsMap) { + colorLegend.keysColorsMap = new Map(); + } + colorLegend.keysColorsMap.set(feature.properties[flattenColorField], + feature.properties[flattenColorField + '_color']); + this.legendData.set(flattenColorField + '_color', colorLegend); + }); } const providedFields = this.featureLayersIndex.get(s).providedFields; const fieldsToKeep = new Set(); @@ -937,9 +939,30 @@ export class MapContributor extends Contributor { const properties = Object.assign({}, f.properties); const feature = Object.assign({}, f); feature.properties = properties; + const fieldsToKeep = new Set(); /** set the key-to-color map to be displayed on the legend. */ + const colorFields = this.topologyLayersIndex.get(s).colorFields; + if (colorFields) { + colorFields.forEach(colorField => { + const flattenColorField = colorField.replace(/\./g, this.FLAT_CHAR); + feature.properties[flattenColorField] = feature.properties['hits'][0][flattenColorField]; + feature.properties[flattenColorField + '_color'] = getHexColor(feature.properties[flattenColorField], 0.5); + + /** set the key-to-color map to be displayed on the legend. */ + let colorLegend: LegendData = this.legendData.get(flattenColorField + '_color'); + if (!colorLegend) { + colorLegend = {}; + colorLegend.keysColorsMap = new Map(); + } else if (!colorLegend.keysColorsMap) { + colorLegend.keysColorsMap = new Map(); + } + colorLegend.keysColorsMap.set(feature.properties[flattenColorField], + feature.properties[flattenColorField + '_color']); + fieldsToKeep.add(flattenColorField + '_color'); + this.legendData.set(flattenColorField + '_color', colorLegend); + }); + } const providedFields = this.topologyLayersIndex.get(s).providedFields; - const fieldsToKeep = new Set(); if (providedFields) { providedFields.forEach(pf => { const flattenColorField = pf.color.replace(/\./g, this.FLAT_CHAR); @@ -1603,6 +1626,7 @@ export class MapContributor extends Contributor { topologyLayer.metrics = ls.metrics; topologyLayer.granularity = ls.granularity; topologyLayer.providedFields = ls.provided_fields; + topologyLayer.colorFields = new Set(ls.colors_from_fields ? ls.colors_from_fields : []); return topologyLayer; } @@ -1615,9 +1639,9 @@ export class MapContributor extends Contributor { featureLayer.maxfeatures = ls.maxfeatures; featureLayer.normalizationFields = ls.normalization_fields; featureLayer.includeFields = new Set(ls.include_fields); - featureLayer.colorField = ls.color_from_field; featureLayer.returnedGeometry = ls.returned_geometry; featureLayer.providedFields = ls.provided_fields; + featureLayer.colorFields = new Set(ls.colors_from_fields ? ls.colors_from_fields : []); return featureLayer; } @@ -1692,8 +1716,8 @@ export class MapContributor extends Contributor { includes.add(f); }); } - if (ls.colorField) { - includes.add(ls.colorField); + if (ls.colorFields) { + ls.colorFields.forEach(cf => includes.add(cf)); } if (ls.normalizationFields) { ls.normalizationFields.forEach(nf => { @@ -1950,9 +1974,11 @@ export class MapContributor extends Contributor { this.indexSearchSourcesMetrics(cs, f, ReturnedField.flat); }); } - if (ls.colorField) { - includes.add(ls.colorField); - this.indexSearchSourcesMetrics(cs, ls.colorField, ReturnedField.generatedcolor); + if (ls.colorFields) { + ls.colorFields.forEach(cf => { + includes.add(cf); + this.indexSearchSourcesMetrics(cs, cf, ReturnedField.generatedcolor); + }); } if (ls.providedFields) { ls.providedFields.forEach(pf => { @@ -2023,18 +2049,30 @@ export class MapContributor extends Contributor { } aggregation.raw_geometries.push({geometry: ls.geometrySupport}); } - if (ls.providedFields) { + if (ls.providedFields && ls.providedFields.length > 0) { if (!aggregation.fetch_hits) { aggregation.fetch_hits = {size: 1}; - const fetchSet = new Set(); - ls.providedFields.forEach(pf => { - fetchSet.add(pf.color); - if (pf.label) { - fetchSet.add(pf.label); - } - }); - aggregation.fetch_hits.include = Array.from(fetchSet); + aggregation.fetch_hits.include = []; + } + const fetchSet = new Set(aggregation.fetch_hits.include); + ls.providedFields.forEach(pf => { + fetchSet.add(pf.color); + if (pf.label) { + fetchSet.add(pf.label); + } + }); + aggregation.fetch_hits.include = Array.from(fetchSet); + } + if (ls.colorFields && ls.colorFields.size > 0) { + if (!aggregation.fetch_hits) { + aggregation.fetch_hits = {size: 1}; + aggregation.fetch_hits.include = []; } + const fetchSet = new Set(aggregation.fetch_hits.include); + ls.colorFields.forEach(cf => { + fetchSet.add(cf); + }); + aggregation.fetch_hits.include = Array.from(fetchSet); } sources.push(cs); aggregationsMap.set(aggId, {agg: aggregation, sources}); @@ -2301,6 +2339,10 @@ export class MapContributor extends Contributor { featureLayer.providedFields = featureLayer.providedFields ? existingFeatureLayer.providedFields.concat(featureLayer.providedFields) : existingFeatureLayer.providedFields; } + if (existingFeatureLayer.colorFields) { + featureLayer.colorFields = featureLayer.colorFields ? + new Set([...existingFeatureLayer.colorFields].concat([...featureLayer.colorFields])) : existingFeatureLayer.colorFields; + } } this.layersSourcesIndex.set(ls.id, ls.source); let layers = this.sourcesLayersIndex.get(ls.source); diff --git a/src/jsonSchemas/mapContributorConf.schema.json b/src/jsonSchemas/mapContributorConf.schema.json index 49f69703..c3f89959 100644 --- a/src/jsonSchemas/mapContributorConf.schema.json +++ b/src/jsonSchemas/mapContributorConf.schema.json @@ -68,9 +68,13 @@ } } }, - "color_from_field": { - "description": "a field which values will be transformed to colors, thanks to ARLAS-color-service, and used for a data-driven colouring of the layer features.", - "type": "string" + "colors_from_fields": { + "description": "a list of fields which values will be transformed to colors, thanks to ARLAS-color-service, and used for a data-driven colouring of the layer features.", + "type": "array", + "items": { + "description": "field which values will be transformed to colors, thanks to ARLAS-color-service, and used for a data-driven colouring of the layer features.", + "type": "string" + } }, "normalization_fields": { "description": "List of numeric or date fields patterns or names which values are normalized. The fields values can be normalized locally on the current map extent OR globally; considering the current filters. Also you can normalize fields values (locally or globally) per a given key : For instance I want to normalize the speed of boats for each boat id. **Note** : Global normalization is only possible per a given key.", diff --git a/src/models/models.ts b/src/models/models.ts index f7d0e557..7bdab2ae 100644 --- a/src/models/models.ts +++ b/src/models/models.ts @@ -170,7 +170,7 @@ export interface LayerSourceConfig { geometry_id?: string; geometry_support?: string; agg_geo_field?: string; - color_from_field?: string; + colors_from_fields?: Array; include_fields?: Array; provided_fields?: Array; normalization_fields?: Array; @@ -497,7 +497,7 @@ export class LayerFeatureSource extends LayerSource { public normalizationFields: Array; public includeFields: Set; public providedFields: Array; - public colorField: string; + public colorFields: Set; public returnedGeometry: string; } @@ -518,6 +518,7 @@ export class LayerTopologySource extends LayerSource { public geometrySupport: string; public granularity: Granularity; public providedFields: Array; + public colorFields: Set; } export enum Granularity {