Skip to content
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

feat: Add time slider to interactive filters #816

Merged
merged 15 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions app/charts/area/areas-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ import {
stackOffsetDivergingPositiveZeros,
useOptionalNumericVariable,
usePlottableData,
usePreparedData,
useDataAfterInteractiveFilters,
bprusinowski marked this conversation as resolved.
Show resolved Hide resolved
useSegment,
useStringVariable,
useTemporalVariable,
} from "@/charts/shared/chart-helpers";
import { CommonChartState } from "@/charts/shared/chart-state";
import { TooltipInfo } from "@/charts/shared/interaction/tooltip";
import useChartFormatters from "@/charts/shared/use-chart-formatters";
import { ChartContext, ChartProps } from "@/charts/shared/use-chart-state";
import { InteractionProvider } from "@/charts/shared/use-interaction";
import { useInteractiveFilters } from "@/charts/shared/use-interactive-filters";
import { Bounds, Observer, useWidth } from "@/charts/shared/use-width";
import { Observer, useWidth } from "@/charts/shared/use-width";
import { AreaFields } from "@/configurator";
import { isTemporalDimension, Observation } from "@/domain/data";
import {
Expand All @@ -52,10 +52,11 @@ import { sortByIndex } from "@/utils/array";
import { estimateTextWidth } from "@/utils/estimate-text-width";
import { makeDimensionValueSorters } from "@/utils/sorting-values";

export interface AreasState {
export interface AreasState extends CommonChartState {
chartType: "area";
data: Observation[];
bounds: Bounds;
allData: Observation[];
preparedData: Observation[];
getX: (d: Observation) => Date;
xScale: ScaleTime<number, number>;
xEntireScale: ScaleTime<number, number>;
Expand Down Expand Up @@ -92,8 +93,8 @@ const useAreasState = (
} = chartProps;
const width = useWidth();
const formatNumber = useFormatNumber();
const estimateNumberWidth = (d: number) => estimateTextWidth(formatNumber(d));
const timeFormatUnit = useTimeFormatUnit();
const [interactiveFilters] = useInteractiveFilters();

const xDimension = dimensions.find((d) => d.iri === fields.x.componentIri);

Expand Down Expand Up @@ -133,10 +134,6 @@ const useAreasState = (
);

const xKey = fields.x.componentIri;
const hasInteractiveTimeFilter = useMemo(
() => interactiveFiltersConfig?.timeRange.active,
[interactiveFiltersConfig?.timeRange.active]
);

// All Data (used for brushing)
const sortedData = useMemo(
Expand Down Expand Up @@ -168,12 +165,9 @@ const useAreasState = (
plotters: [getX, getY],
});

// Data for chart
const preparedData = usePreparedData({
legendFilterActive: interactiveFiltersConfig?.legend.active,
timeRangeFilterActive: interactiveFiltersConfig?.timeRange.active,
const preparedData = useDataAfterInteractiveFilters({
sortedData: plottableSortedData,
interactiveFilters,
interactiveFiltersConfig,
getX,
getSegment,
});
Expand Down Expand Up @@ -320,18 +314,18 @@ const useAreasState = (
]);

/** Dimensions */
const left = hasInteractiveTimeFilter
? estimateTextWidth(formatNumber(entireMaxTotalValue))
: Math.max(
estimateTextWidth(formatNumber(yScale.domain()[0])),
estimateTextWidth(formatNumber(yScale.domain()[1]))
);
const bottom = hasInteractiveTimeFilter ? BRUSH_BOTTOM_SPACE : 40;
const [yMin, yMax] = yScale.domain();
const left = interactiveFiltersConfig?.timeRange.active
? estimateNumberWidth(entireMaxTotalValue)
: Math.max(estimateNumberWidth(yMin), estimateNumberWidth(yMax));
const bottom = interactiveFiltersConfig?.timeRange.active
? BRUSH_BOTTOM_SPACE
: 40;

const margins = {
top: 50,
right: 40,
bottom: bottom,
bottom,
left: left + LEFT_MARGIN_OFFSET,
};
const chartWidth = width - margins.left - margins.right;
Expand Down Expand Up @@ -366,11 +360,8 @@ const useAreasState = (
});

const yAnchor = 0;

const xPlacement = "center";

const yPlacement = "top";

const yValueFormatter = (value: number | null) =>
formatNumberWithUnit(
value,
Expand Down Expand Up @@ -400,8 +391,10 @@ const useAreasState = (

return {
chartType: "area",
data,
bounds,
data,
allData: plottableSortedData,
preparedData,
getX,
xScale,
xEntireScale,
Expand Down
Loading