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

Calculate "Others", based on the aggregationType #922

Merged
merged 1 commit into from
Nov 29, 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
48 changes: 40 additions & 8 deletions packages/react-ui/src/widgets/CategoryWidgetUI/CategoryWidgetUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Tooltip,
Box
} from '@mui/material';
import { AggregationTypes } from '@carto/react-core';

import { useIntl } from 'react-intl';

Expand Down Expand Up @@ -40,10 +41,44 @@ function usePrevious(value) {
return ref.current;
}

const aggregateRest = ({ items, aggregationType }) => {
let aggregatedValue = undefined;

if (aggregationType === AggregationTypes.AVG) {
// We should calculate the average based on each element weight, which is equivalent to ∑(value×count) / ∑count
aggregatedValue =
items.reduce((accum, elem) => accum + elem.value * elem.count, 0) /
items.reduce((accum, elem) => accum + elem.count, 0);
} else {
const defaultValue =
{
[AggregationTypes.MIN]: Infinity,
[AggregationTypes.MAX]: -Infinity
}[aggregationType] || 0;

aggregatedValue = items.reduce((accum, elem, index, arr) => {
switch (aggregationType) {
case AggregationTypes.SUM:
case AggregationTypes.COUNT:
return accum + elem.value;
case AggregationTypes.MIN:
return Math.min(accum, elem.value);
case AggregationTypes.MAX:
return Math.max(accum, elem.value);
default:
return accum;
}
}, defaultValue);
}

return { name: REST_CATEGORY, value: aggregatedValue };
};

const REST_CATEGORY = '__rest__';
function CategoryWidgetUI(props) {
const {
data,
aggregationType,
formatter,
labels,
maxItems,
Expand Down Expand Up @@ -175,13 +210,10 @@ function CategoryWidgetUI(props) {
if (!blockedCategories.length) {
const main = list.slice(0, maxItems);
if (main.length < list.length) {
const rest = list.slice(maxItems).reduce(
(acum, elem) => {
acum.value += elem.value;
return acum;
},
{ name: REST_CATEGORY, value: 0 }
);
const rest = aggregateRest({
items: list.slice(maxItems),
aggregationType
});
return [...main, rest];
} else {
return main;
Expand Down Expand Up @@ -218,7 +250,7 @@ function CategoryWidgetUI(props) {
: list;
}
},
[blockedCategories, labels, maxItems, searchValue, showAll]
[blockedCategories, labels, maxItems, searchValue, showAll, aggregationType]
);

const getCategoriesCount = useCallback(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-widgets/src/widgets/CategoryWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function CategoryWidget(props) {
{(!!data.length || isLoading) && (
<CategoryWidgetUI
data={data}
aggregationType={operation}
formatter={formatter}
labels={labels}
selectedCategories={selectedCategories}
Expand Down