-
-
Notifications
You must be signed in to change notification settings - Fork 254
feat(dashboard): add indicator widget type #1392
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
2891ec0
52932a0
461233c
36f3d0a
b793e45
5fcb973
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import type { DashboardWidget } from "@geolibre/core"; | ||
| import type { DashboardWidget, DashboardWidgetType, IndicatorAggregation } from "@geolibre/core"; | ||
| import { | ||
| Button, | ||
| ColorField, | ||
|
|
@@ -20,7 +20,6 @@ import { | |
| MIN_HISTOGRAM_BINS, | ||
| numericColumns, | ||
| type BarAggregation, | ||
| type ChartType, | ||
| } from "../../lib/attribute-charts"; | ||
| import { useLayerChartData } from "../../hooks/useLayerChartData"; | ||
|
|
||
|
|
@@ -56,7 +55,7 @@ export function WidgetEditorDialog({ | |
| }: WidgetEditorDialogProps) { | ||
| const { t } = useTranslation(); | ||
| const [layerId, setLayerId] = useState(""); | ||
| const [type, setType] = useState<ChartType>("histogram"); | ||
| const [type, setType] = useState<DashboardWidgetType>("histogram"); | ||
| const [field, setField] = useState(""); | ||
| const [xField, setXField] = useState(""); | ||
| const [yField, setYField] = useState(""); | ||
|
|
@@ -65,6 +64,10 @@ export function WidgetEditorDialog({ | |
| const [aggregation, setAggregation] = useState<BarAggregation>("count"); | ||
| const [valueField, setValueField] = useState(""); | ||
| const [title, setTitle] = useState(""); | ||
| // "indicator" widget fields (issue #1381). | ||
| const [indicatorAggregation, setIndicatorAggregation] = useState<IndicatorAggregation>("count"); | ||
| const [prefix, setPrefix] = useState(""); | ||
| const [suffix, setSuffix] = useState(""); | ||
| // "" means no custom color: fall back to the theme primary / palette. | ||
| const [color, setColor] = useState(""); | ||
|
|
||
|
|
@@ -82,6 +85,9 @@ export function WidgetEditorDialog({ | |
| setValueField(widget?.valueField ?? ""); | ||
| setTitle(widget?.title ?? ""); | ||
| setColor(widget?.color ?? ""); | ||
| setIndicatorAggregation(widget?.indicatorAggregation ?? "count"); | ||
| setPrefix(widget?.prefix ?? ""); | ||
| setSuffix(widget?.suffix ?? ""); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [open, widget]); | ||
|
|
||
|
|
@@ -97,14 +103,16 @@ export function WidgetEditorDialog({ | |
| // sum/average rather than count); scatter needs two numeric fields so x and y | ||
| // aren't forced to the same column; the rest need one numeric field. | ||
| const isCategorical = type === "bar" || type === "pie"; | ||
| const isIndicator = type === "indicator"; | ||
| const canSave = | ||
| layerId !== "" && | ||
| (isCategorical | ||
| ? hasCategory && (aggregation === "count" || hasNumeric) | ||
| : type === "scatter" | ||
| ? numericCols.length >= 2 | ||
| : hasNumeric); | ||
|
|
||
| (isIndicator | ||
| ? indicatorAggregation === "count" || hasNumeric | ||
| : isCategorical | ||
| ? hasCategory && (aggregation === "count" || hasNumeric) | ||
| : type === "scatter" | ||
| ? numericCols.length >= 2 | ||
| : hasNumeric); | ||
| const save = () => { | ||
| if (!canSave) return; | ||
| const next: DashboardWidget = { | ||
|
|
@@ -134,6 +142,15 @@ export function WidgetEditorDialog({ | |
| next.aggregation = agg; | ||
| if (agg !== "count") next.valueField = pick(valueField, numericCols); | ||
| } | ||
| if (type === "indicator") { | ||
| next.indicatorAggregation = indicatorAggregation; | ||
| if (indicatorAggregation !== "count") { | ||
| next.field = pick(field, numericCols); | ||
| } | ||
| // Preserve whitespace: prefix/suffix may have intentional spaces (" ha"). | ||
| if (prefix) next.prefix = prefix; | ||
| if (suffix) next.suffix = suffix; | ||
| } | ||
| onSave(next); | ||
| onOpenChange(false); | ||
| }; | ||
|
|
@@ -169,7 +186,7 @@ export function WidgetEditorDialog({ | |
| </Select> | ||
| </div> | ||
|
|
||
| {!hasChartable ? ( | ||
| {!hasChartable && type !== "indicator" ? ( | ||
| <p className="text-sm text-muted-foreground">{t("dashboard.editor.noFields")}</p> | ||
| ) : ( | ||
|
Comment on lines
+189
to
191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Keep the widget type selector available without chartable fields. For a new widget, 🤖 Prompt for AI Agents |
||
| <div className="flex flex-wrap items-end gap-3"> | ||
|
|
@@ -180,7 +197,7 @@ export function WidgetEditorDialog({ | |
| className="w-36" | ||
| value={type} | ||
| onChange={(event) => { | ||
| const nextType = event.target.value as ChartType; | ||
| const nextType = event.target.value as DashboardWidgetType; | ||
| setType(nextType); | ||
| // Pie has no "average"; drop a carried-over mean so the | ||
| // select doesn't show a stale value with no matching option. | ||
|
|
@@ -207,6 +224,7 @@ export function WidgetEditorDialog({ | |
| <option value="pie" disabled={!hasCategory}> | ||
| {t("dashboard.chartType.pie")} | ||
| </option> | ||
| <option value="indicator">{t("dashboard.chartType.indicator")}</option> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| </Select> | ||
| </div> | ||
|
|
||
|
|
@@ -312,6 +330,72 @@ export function WidgetEditorDialog({ | |
| )} | ||
| </> | ||
| )} | ||
|
|
||
| {type === "indicator" && ( | ||
| <> | ||
| <div className="grid gap-1.5"> | ||
| <Label htmlFor="widget-indicator-agg"> | ||
| {t("dashboard.editor.aggregate")} | ||
| </Label> | ||
| <Select | ||
| id="widget-indicator-agg" | ||
| className="w-32" | ||
| value={indicatorAggregation} | ||
| onChange={(event) => | ||
| setIndicatorAggregation(event.target.value as IndicatorAggregation) | ||
| } | ||
| > | ||
| <option value="count">{t("dashboard.indicatorAggregation.count")}</option> | ||
| <option value="sum" disabled={!hasNumeric}> | ||
| {t("dashboard.indicatorAggregation.sum")} | ||
| </option> | ||
| <option value="mean" disabled={!hasNumeric}> | ||
| {t("dashboard.indicatorAggregation.mean")} | ||
| </option> | ||
| <option value="min" disabled={!hasNumeric}> | ||
| {t("dashboard.indicatorAggregation.min")} | ||
| </option> | ||
| <option value="max" disabled={!hasNumeric}> | ||
| {t("dashboard.indicatorAggregation.max")} | ||
| </option> | ||
| <option value="median" disabled={!hasNumeric}> | ||
| {t("dashboard.indicatorAggregation.median")} | ||
| </option> | ||
| </Select> | ||
| </div> | ||
| {indicatorAggregation !== "count" && ( | ||
| <FieldSelect | ||
| id="widget-indicator-field" | ||
| label={t("dashboard.editor.field")} | ||
| value={pick(field, numericCols)} | ||
| options={numericCols} | ||
| onChange={setField} | ||
| /> | ||
| )} | ||
| <div className="flex gap-2"> | ||
| <div className="grid gap-1.5"> | ||
| <Label htmlFor="widget-prefix">{t("dashboard.editor.prefix")}</Label> | ||
| <Input | ||
| id="widget-prefix" | ||
| className="w-20" | ||
| value={prefix} | ||
| placeholder="€" | ||
| onChange={(event) => setPrefix(event.target.value)} | ||
| /> | ||
| </div> | ||
| <div className="grid gap-1.5"> | ||
| <Label htmlFor="widget-suffix">{t("dashboard.editor.suffix")}</Label> | ||
| <Input | ||
| id="widget-suffix" | ||
| className="w-20" | ||
| value={suffix} | ||
| placeholder=" ha" | ||
| onChange={(event) => setSuffix(event.target.value)} | ||
| /> | ||
| </div> | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
| )} | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.