-
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.
(WIP) feat: Add TimeSlider + filtering of data based on it
- Loading branch information
1 parent
6ad5123
commit 2ebea50
Showing
4 changed files
with
119 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { 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"; | ||
|
||
export const TimeSlider = ({ componentIri }: { componentIri?: string }) => { | ||
const [_, dispatch] = useInteractiveFilters(); | ||
const [t, setT] = React.useState(0); | ||
const chartState = useChartState() as NonNullable< | ||
Exclude<ChartState, TableChartState> | ||
>; | ||
const onChange = React.useMemo(() => { | ||
// FIXME: enable interactive filters for maps! | ||
if (componentIri && chartState.chartType !== "map") { | ||
const rawRange = [ | ||
...new Set( | ||
chartState.allData.map((d) => d[componentIri]).filter(Boolean) | ||
), | ||
].sort() as string[]; | ||
const range = rawRange.map(parseDate); | ||
const [min, max] = [range[0], range[range.length - 1]]; | ||
const scale = scaleLinear().range([min.getTime(), max.getTime()]); | ||
const reversedRange = range.reverse(); | ||
|
||
return (e: ChangeEvent<HTMLInputElement>) => { | ||
const t = +e.target.value; | ||
setT(t); | ||
|
||
const tDate = new Date(scale(t)); | ||
const IFDate = reversedRange.find((d) => d <= tDate) as Date; | ||
dispatch({ type: "SET_TIME_SLIDER_FILTER", value: IFDate }); | ||
}; | ||
} else { | ||
return (e: ChangeEvent<HTMLInputElement>) => { | ||
const t = +e.target.value; | ||
setT(t); | ||
}; | ||
} | ||
// @ts-ignore - allData is not there yet for maps | ||
}, [chartState.chartType, chartState.allData, componentIri, dispatch]); | ||
|
||
React.useEffect(() => { | ||
onChange({ target: { value: "0" } } as ChangeEvent<HTMLInputElement>); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return ( | ||
<Slider | ||
name="time-slider" | ||
min={0} | ||
max={1} | ||
step={0.0001} | ||
value={t} | ||
onChange={onChange} | ||
/> | ||
); | ||
}; |