Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Show selected fields in the tooltip for aggregation layers #2814

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/components/src/map/layer-hover-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getTooltipDisplayValue
} from '@kepler.gl/reducers';
import {useIntl} from 'react-intl';
import {capitalizeFirstLetter} from '@kepler.gl/utils';

export const StyledLayerName = styled(CenterFlexbox)`
color: ${props => props.theme.textColorHl};
Expand Down Expand Up @@ -169,6 +170,22 @@ const CellInfo = ({
return null;
}, [fieldsToShow, sizeField, layer, data.elevationValue]);

const aggregatedData = useMemo(() => {
if (data.aggregatedData && fieldsToShow) {
return fieldsToShow.reduce((acc, field) => {
const dataForField = data.aggregatedData?.[field.name];
if (dataForField?.measure && field.name !== colorField?.name) {
acc.push({
name: `${capitalizeFirstLetter(dataForField.measure)} of ${field.name}`,
value: dataForField.value
});
}
return acc;
}, [] as {name: string; value?: string}[]);
}
return [];
}, [data.aggregatedData, fieldsToShow, colorField?.name]);

const colorMeasure = layer.getVisualChannelDescription('color').measure;
const sizeMeasure = layer.getVisualChannelDescription('size').measure;
return (
Expand All @@ -180,6 +197,9 @@ const CellInfo = ({
{sizeField && layer.visualChannels.size && sizeMeasure ? (
<Row name={sizeMeasure} key="size" value={elevationValue || 'N/A'} />
) : null}
{aggregatedData.map((dataForField, idx) => (
<Row name={dataForField.name} key={`data_${idx}`} value={dataForField.value || 'N/A'} />
))}
</tbody>
);
};
Expand Down
21 changes: 17 additions & 4 deletions src/layers/src/aggregation-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import Layer, {
VisualChannelDescription,
VisualChannels
} from './base-layer';
import {hexToRgb, aggregate} from '@kepler.gl/utils';
import {hexToRgb, aggregate, DataContainerInterface} from '@kepler.gl/utils';
import {
HIGHLIGH_COLOR_3D,
CHANNEL_SCALES,
FIELD_OPTS,
DEFAULT_AGGREGATION,
ColorRange
} from '@kepler.gl/constants';
import {Merge, LayerColumn} from '@kepler.gl/types';
import {Field, LayerColumn, Merge} from '@kepler.gl/types';
import {KeplerTable, Datasets} from '@kepler.gl/table';

type AggregationLayerColumns = {
Expand Down Expand Up @@ -170,9 +170,22 @@ export default class AggregationLayer extends Layer {
};
}

getHoverData(object) {
getHoverData(object: any, dataContainer: DataContainerInterface, fields: Field[]): any {
if (!object) return object;
const measure = this.config.visConfig.colorAggregation;
// aggregate all fields for the hovered group
const aggregatedData = fields.reduce((accu, field) => {
accu[field.name] = {
measure,
value: aggregate(object.points, measure, (d: {index: number}) => {
return dataContainer.valueAt(d.index, field.fieldIdx);
})
};
return accu;
}, {});

// return aggregated object
return object;
return {aggregatedData, ...object};
}

getFilteredItemCount() {
Expand Down
13 changes: 12 additions & 1 deletion src/reducers/src/layer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ export type LayersToRender = {
[layerId: string]: boolean;
};

export type AggregationLayerHoverData = {points: any[]; colorValue?: any; elevationValue?: any};
export type AggregationLayerHoverData = {
points: any[];
colorValue?: any;
elevationValue?: any;
aggregatedData?: Record<
string,
{
measure: string;
value?: any;
}
>;
};

export type LayerHoverProp = {
data: DataRow | AggregationLayerHoverData | null;
Expand Down
Loading