Skip to content

Commit

Permalink
align implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Jul 1, 2021
1 parent 33fde42 commit cf77f83
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
PercentilesFieldMeta,
RangeFieldMeta,
StyleMetaData,
TileMetaFeature,
} from '../../../../../common/descriptor_types';
import { IField } from '../../../fields/field';
import { IVectorLayer } from '../../../layers/vector_layer';
Expand All @@ -58,6 +59,10 @@ export interface IDynamicStyleProperty<T> extends IStyleProperty<T> {
getFieldMetaRequest(): Promise<unknown | null>;
pluckOrdinalStyleMetaFromFeatures(features: Feature[]): RangeFieldMeta | null;
pluckCategoricalStyleMetaFromFeatures(features: Feature[]): CategoryFieldMeta | null;
pluckOrdinalStyleMetaFromTileMetaFeatures(features: TileMetaFeature[]): RangeFieldMeta | null;
pluckCategoricalStyleMetaFromTileMetaFeatures(
features: TileMetaFeature[]
): CategoryFieldMeta | null;
getValueSuggestions(query: string): Promise<string[]>;
enrichGeoJsonAndMbFeatureState(
featureCollection: FeatureCollection,
Expand Down Expand Up @@ -297,6 +302,41 @@ export class DynamicStyleProperty<T>
: DATA_MAPPING_FUNCTION.INTERPOLATE;
}

pluckOrdinalStyleMetaFromTileMetaFeatures(
metaFeatures: TileMetaFeature[]
): RangeFieldMeta | null {
if (!this.isOrdinal()) {
return null;
}

const name = this.getFieldName();
let min = Infinity;
let max = -Infinity;
for (let i = 0; i < metaFeatures.length; i++) {
const fieldMeta = metaFeatures[i].properties.fieldMeta;
if (fieldMeta && fieldMeta[name] && fieldMeta[name].range) {
min = Math.min(fieldMeta[name].range?.min as number, min);
max = Math.max(fieldMeta[name].range?.max as number, max);
}
}
return {
min,
max,
delta: max - min,
};
}

pluckCategoricalStyleMetaFromTileMetaFeatures(
features: TileMetaFeature[]
): CategoryFieldMeta | null {
const size = this.getNumberOfCategories();
if (!this.isCategorical() || size <= 0) {
return null;
}

throw new Error('not implemented');
}

pluckOrdinalStyleMetaFromFeatures(features: Feature[]): RangeFieldMeta | null {
if (!this.isOrdinal()) {
return null;
Expand Down
23 changes: 7 additions & 16 deletions x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -507,27 +507,18 @@ export class VectorStyle implements IVectorStyle {

dynamicProperties.forEach((dynamicProperty) => {
const name = dynamicProperty.getFieldName();

const ordinalStyleMeta = dynamicProperty.pluckOrdinalStyleMetaFromTileMetaFeatures(
metaFeatures
);

if (!styleMeta.fieldMeta[name]) {
styleMeta.fieldMeta[name] = {};
}

let min = Infinity;
let max = -Infinity;
for (let i = 0; i < metaFeatures.length; i++) {
const fieldMeta = metaFeatures[i].properties.fieldMeta;
if (fieldMeta && fieldMeta[name] && fieldMeta[name].range) {
min = Math.min(fieldMeta[name].range?.min as number, min);
max = Math.max(fieldMeta[name].range?.max as number, max);
}
if (ordinalStyleMeta) {
styleMeta.fieldMeta[name].range = ordinalStyleMeta;
}

styleMeta.fieldMeta[name] = {
range: {
min,
max,
delta: max - min,
},
};
});

return styleMeta;
Expand Down

0 comments on commit cf77f83

Please sign in to comment.