diff --git a/CHANGELOG.md b/CHANGELOG.md index 434a54326..a63b5c35c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/react-ui/src/widgets/PieWidgetUI.js b/packages/react-ui/src/widgets/PieWidgetUI.js index ae38a27de..4191c27d9 100644 --- a/packages/react-ui/src/widgets/PieWidgetUI.js +++ b/packages/react-ui/src/widgets/PieWidgetUI.js @@ -4,7 +4,6 @@ import ReactEcharts from 'echarts-for-react'; import { useTheme } from '@material-ui/core'; import { getChartSerie, - applyChartFilter, areChartPropsEqual, disableSerie, setColor @@ -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); @@ -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) => {