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

fix duplicated logic for category selection in PieWidgetUI #332

Merged
merged 9 commits into from
Feb 15, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released

- Fix duplicated logic for category selection in PieWidgetUI [#332] (https://github.com/CartoDB/carto-react/pull/332)
- Fix aggr operation considering null as valid for count [#326](https://github.com/CartoDB/carto-react/pull/326)
- Add legends tests/stories/types [#328](https://github.com/CartoDB/carto-react/pull/328)
- Fix error in getPalette, causing wrong colors when using CARTOColors [#328](https://github.com/CartoDB/carto-react/pull/328)
Expand Down
43 changes: 23 additions & 20 deletions packages/react-ui/src/widgets/PieWidgetUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ReactEcharts from 'echarts-for-react';
import { useTheme } from '@material-ui/core';
import {
getChartSerie,
applyChartFilter,
areChartPropsEqual,
disableSerie,
setColor
Expand Down Expand Up @@ -81,15 +80,15 @@ function __generateSerie({ name, data, theme, animation, selectedCategories, lab
name,
animation,
data: data.map((item) => {
// Avoid modify data item
const clonedItem = { ...item };

if (labels?.[clonedItem.name]) {
clonedItem.name = labels[clonedItem.name];
}
// Avoid modifying data item
const clonedItem = {
...item,
name: labels?.[item.name] || item.name,
key: item.name
};

const disabled =
selectedCategories?.length && !selectedCategories.includes(clonedItem.name);
selectedCategories?.length && !selectedCategories.includes(clonedItem.key);

if (disabled) {
disableSerie(clonedItem, theme);
Expand Down Expand Up @@ -247,24 +246,28 @@ function PieWidgetUI({
(params) => {
if (onSelectedCategoriesChange) {
const echart = chartInstance.current.getEchartsInstance();
const { option, serie } = getChartSerie(echart, params.seriesIndex);

applyChartFilter(serie, params.dataIndex, theme);
const { serie } = getChartSerie(echart, params.seriesIndex);
let newSelectedCategories = serie.data
.filter((category) => !category.disabled)
.map((category) => category.key);

echart.setOption(option, true);
if (newSelectedCategories.length === serie.data.length) {
newSelectedCategories = [];
}

const activeCategories = serie.data.filter((category) => !category.disabled);
const { name } = data[params.dataIndex];

defaultLabel.current = __getDefaultLabel(activeCategories);
const selectedCategoryIdx = newSelectedCategories.indexOf(name);
if (selectedCategoryIdx === -1) {
newSelectedCategories.push(name);
} else {
newSelectedCategories.splice(selectedCategoryIdx, 1);
}

onSelectedCategoriesChange(
activeCategories.length === serie.data.length
? []
: activeCategories.map((category) => category.name)
);
onSelectedCategoriesChange(newSelectedCategories);
}
},
[onSelectedCategoriesChange, theme]
[data, onSelectedCategoriesChange]
);

const mouseoverEvent = useCallback((params) => {
Expand Down