Skip to content

Commit

Permalink
Merge pull request #321 from gisaia/feature/colors_from_field
Browse files Browse the repository at this point in the history
Add several colors from field
  • Loading branch information
MohamedHamouGisaia authored Jun 1, 2020
2 parents b6e22ba + 9638278 commit 6cfd7ad
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 37 deletions.
106 changes: 74 additions & 32 deletions src/contributors/MapContributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
Expand Down Expand Up @@ -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<string>();
/** 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<string>();
if (providedFields) {
providedFields.forEach(pf => {
const flattenColorField = pf.color.replace(/\./g, this.FLAT_CHAR);
Expand Down Expand Up @@ -1603,6 +1626,7 @@ export class MapContributor extends Contributor {
topologyLayer.metrics = ls.metrics;
topologyLayer.granularity = <any>ls.granularity;
topologyLayer.providedFields = ls.provided_fields;
topologyLayer.colorFields = new Set(ls.colors_from_fields ? ls.colors_from_fields : []);
return topologyLayer;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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<string>();
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<string>(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<string>(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});
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/jsonSchemas/mapContributorConf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
5 changes: 3 additions & 2 deletions src/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
include_fields?: Array<string>;
provided_fields?: Array<ColorConfig>;
normalization_fields?: Array<NormalizationFieldConfig>;
Expand Down Expand Up @@ -497,7 +497,7 @@ export class LayerFeatureSource extends LayerSource {
public normalizationFields: Array<NormalizationFieldConfig>;
public includeFields: Set<string>;
public providedFields: Array<ColorConfig>;
public colorField: string;
public colorFields: Set<string>;
public returnedGeometry: string;
}

Expand All @@ -518,6 +518,7 @@ export class LayerTopologySource extends LayerSource {
public geometrySupport: string;
public granularity: Granularity;
public providedFields: Array<ColorConfig>;
public colorFields: Set<string>;
}

export enum Granularity {
Expand Down

0 comments on commit 6cfd7ad

Please sign in to comment.