-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add TimeSlider + filtering of data based on it
- Loading branch information
1 parent
a213267
commit d4bdccd
Showing
5 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { bisect, scaleLinear } from "d3"; | ||
import React, { ChangeEvent } from "react"; | ||
|
||
import { ChartState, useChartState } from "@/charts/shared/use-chart-state"; | ||
import { useInteractiveFilters } from "@/charts/shared/use-interactive-filters"; | ||
import { TableChartState } from "@/charts/table/table-state"; | ||
import { Slider } from "@/components/form"; | ||
import { parseDate } from "@/configurator/components/ui-helpers"; | ||
import useEvent from "@/utils/use-event"; | ||
|
||
export const TimeSlider = ({ componentIri }: { componentIri?: string }) => { | ||
const [IFState, dispatch] = useInteractiveFilters(); | ||
const [t, setT] = React.useState(0); | ||
const chartState = useChartState() as NonNullable< | ||
Exclude<ChartState, TableChartState> | ||
>; | ||
|
||
const sortedMs = React.useMemo(() => { | ||
// FIXME: enable interactive filters for maps! | ||
if (componentIri && chartState.chartType !== "map") { | ||
const uniqueValues = [ | ||
...new Set( | ||
chartState.allData.map((d) => d[componentIri]).filter(Boolean) | ||
), | ||
] as string[]; | ||
const sortedMs = uniqueValues | ||
.sort() | ||
.map(parseDate) | ||
.map((d) => d.getTime()); | ||
|
||
return sortedMs; | ||
} | ||
|
||
return []; | ||
// @ts-ignore - allData is not yet there for the maps | ||
}, [chartState.chartType, chartState.allData, componentIri]); | ||
|
||
const msScale = React.useMemo(() => { | ||
if (sortedMs.length) { | ||
const [min, max] = [sortedMs[0], sortedMs[sortedMs.length - 1]]; | ||
return scaleLinear().range([min, max]); | ||
} | ||
}, [sortedMs]); | ||
|
||
const onChange = useEvent((e: ChangeEvent<HTMLInputElement>) => { | ||
const t = +e.target.value; | ||
setT(t); | ||
|
||
if (msScale) { | ||
const tMs = msScale(t); | ||
const i = bisect(sortedMs, tMs); | ||
const updateMs = sortedMs[i - 1]; | ||
|
||
if (IFState.timeSlider.value?.getTime() !== updateMs) { | ||
dispatch({ | ||
type: "SET_TIME_SLIDER_FILTER", | ||
value: new Date(updateMs), | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
React.useEffect(() => { | ||
onChange({ target: { value: "0" } } as ChangeEvent<HTMLInputElement>); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [sortedMs]); | ||
|
||
return ( | ||
<Slider | ||
name="time-slider" | ||
min={0} | ||
max={1} | ||
step={0.0001} | ||
value={t} | ||
onChange={onChange} | ||
/> | ||
); | ||
}; |