Skip to content

Commit

Permalink
fix: cumsum setting
Browse files Browse the repository at this point in the history
  • Loading branch information
majkshkurti committed Oct 16, 2024
1 parent f288225 commit 5145b29
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
37 changes: 31 additions & 6 deletions apps/web/components/modals/MapLayerChart.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
Box,
Button,
Checkbox,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Divider,
FormControlLabel,
Stack,
Typography,
useTheme,
Expand All @@ -26,6 +28,7 @@ import type { SelectorItem } from "@/types/map/common";
import useLayerFields from "@/hooks/map/CommonHooks";

import EmptySection from "@/components/common/EmptySection";
import FormLabelHelper from "@/components/common/FormLabelHelper";
import { Plot } from "@/components/common/PlotlyPlot";
import Selector from "@/components/map/panels/common/Selector";

Expand All @@ -40,7 +43,8 @@ const MapLayerChartModal: React.FC<MapLayerChartDialogProps> = ({ open, onClose,
const { t } = useTranslation("common");
const theme = useTheme();
const params = useParams();
const { chartData, isLoading, isError } = useProjectLayerChartData(projectId, layer.id);
const [cumSum, setCumSum] = useState(false);
const { chartData, isLoading, isError } = useProjectLayerChartData(projectId, layer.id, cumSum);
const { layerFields: fields, isLoading: areFieldsLoading } = useLayerFields(layer.layer_id, undefined);

const chartTypes: SelectorItem[] = useMemo(() => {
Expand Down Expand Up @@ -73,28 +77,31 @@ const MapLayerChartModal: React.FC<MapLayerChartDialogProps> = ({ open, onClose,
if (!layer.charts || !chartData) return {};
let x = layer.charts["x_label"];
let y = layer.charts["y_label"];
let xTitle = x;
let yTitle = cumSum ? `${y} (cumsum)` : y;
const grouped = chartData["group"];
if (chartType === "bar" && orientation === "h") {
[x, y] = [y, x];
[xTitle, yTitle] = [yTitle, xTitle];
}
const xFieldType = fields.find((field) => field.name === x)?.type;
const yFieldType = fields.find((field) => field.name === y)?.type;
const config = {
xaxis: {
type: xFieldType === "string" ? "category" : undefined,
title: x,
title: xTitle,
},
yaxis: {
type: yFieldType === "string" ? "category" : undefined,
title: y,
title: yTitle,
},
title: `${x} vs ${y}`,
title: `${xTitle} vs ${yTitle}`,
};
if (grouped) {
config["barmode"] = "stack";
}
return config;
}, [chartData, chartType, fields, layer.charts, orientation]);
}, [chartData, chartType, cumSum, fields, layer.charts, orientation]);

const chartDataConfig = useMemo(() => {
if (!chartData) return [];
Expand Down Expand Up @@ -133,7 +140,7 @@ const MapLayerChartModal: React.FC<MapLayerChartDialogProps> = ({ open, onClose,
<DialogContent sx={{ px: 2, pt: 0, mt: 0, pb: 0 }}>
<Box>
{chartData && fields && layer.charts && (
<Stack direction="row" sx={{ p: 2 }}>
<Stack sx={{ p: 2 }}>
<Selector
selectedItems={selectedChartType}
setSelectedItems={(item: SelectorItem) => {
Expand All @@ -154,6 +161,24 @@ const MapLayerChartModal: React.FC<MapLayerChartDialogProps> = ({ open, onClose,
label={t("select_chart_type")}
placeholder="Test"
/>
<Divider />
<FormLabelHelper label={t("settings")} color="inherit" />
<FormControlLabel
control={
<Checkbox
size="small"
checked={cumSum}
onChange={(e) => setCumSum(e.target.checked)}
color="primary"
/>
}
label={
<Typography variant="body2" fontWeight="bold">
{t("cumsum")}
</Typography>
}
/>
<Divider />
</Stack>
)}
{isError && <EmptySection icon={ICON_NAME.CIRCLEINFO} label={t("error_loading_chart_data")} />}{" "}
Expand Down
1 change: 1 addition & 0 deletions apps/web/i18n/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"credits": "Credits",
"credits_quota_information": "Credits werden jedes Mal verbraucht, wenn ein Benutzer Werkzeuge aus dem Werkzeugkasten nutzt oder Karten lädt. \nDie genutzten Credits werden zu Beginn jedes Monats zurückgesetzt.",
"csv": "CSV",
"cumsum": "Kumulative Summe",
"current_credits_out_of_total_quota": "{{current}} von {{total}} Credits / pro Monat",
"current_plan": "Aktuelles Lizenzmodell",
"current_projects_out_of_total_quota": "{{current}} von {{total}} Projekten",
Expand Down
1 change: 1 addition & 0 deletions apps/web/i18n/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"credits": "Credits",
"credits_quota_information": "Credit quota units are consumed each time a user is using tools or performs map load. The usage quota resets at the beginning of each month.",
"csv": "CSV",
"cumsum": "Cumulative sum",
"current_credits_out_of_total_quota": "<highlight>{{current}}</highlight> out of {{total}} credits / month",
"current_plan": "Current plan",
"current_projects_out_of_total_quota": "<highlight>{{current}}</highlight> out of {{total}} projects",
Expand Down
4 changes: 2 additions & 2 deletions apps/web/lib/api/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ export const useProjectInitialViewState = (projectId: string) => {
};
};

export const useProjectLayerChartData = (projectId: string, layerId: number) => {
export const useProjectLayerChartData = (projectId: string, layerId: number, cumSum: boolean = false) => {
const { data, isLoading, error, mutate, isValidating } = useSWR(
[`${PROJECTS_API_BASE_URL}/${projectId}/layer/${layerId}/chart-data`],
[`${PROJECTS_API_BASE_URL}/${projectId}/layer/${layerId}/chart-data?cumsum=${cumSum}`],
fetcher
);
return {
Expand Down

0 comments on commit 5145b29

Please sign in to comment.