-
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.
- Loading branch information
1 parent
0aac329
commit f299ac2
Showing
10 changed files
with
304 additions
and
154 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from "react"; | ||
|
||
import { BaseYGetter, sortData } from "@/charts/combo/combo-state-props"; | ||
import { getLabelWithUnit } from "@/charts/shared/chart-helpers"; | ||
import { | ||
BaseVariables, | ||
ChartStateData, | ||
TemporalXVariables, | ||
useBaseVariables, | ||
useChartData, | ||
useTemporalXVariables, | ||
} from "@/charts/shared/chart-state"; | ||
import { ComboConfig } from "@/configurator"; | ||
|
||
import { ChartProps } from "../shared/ChartProps"; | ||
|
||
type NumericalYComboLineDualVariables = { | ||
y: { | ||
lineLeft: BaseYGetter; | ||
lineRight: BaseYGetter; | ||
}; | ||
}; | ||
|
||
export type ComboLineDualStateVariables = BaseVariables & | ||
TemporalXVariables & | ||
NumericalYComboLineDualVariables; | ||
|
||
export const useComboLineDualStateVariables = ( | ||
props: ChartProps<ComboConfig> & { aspectRatio: number } | ||
): ComboLineDualStateVariables => { | ||
const { chartConfig, dimensionsByIri, measuresByIri } = props; | ||
const { fields } = chartConfig; | ||
const { x } = fields; | ||
|
||
const baseVariables = useBaseVariables(chartConfig); | ||
const temporalXVariables = useTemporalXVariables(x, { | ||
dimensionsByIri, | ||
}); | ||
|
||
if (chartConfig.chartSubtype !== "line") { | ||
throw new Error("This hook is only for line charts!"); | ||
} | ||
|
||
if (chartConfig.fields.y.axisMode !== "dual") { | ||
throw new Error("This hook is only for dual axis line charts!"); | ||
} | ||
|
||
const leftIri = chartConfig.fields.y.leftAxisComponentIri; | ||
const rightIri = chartConfig.fields.y.rightAxisComponentIri; | ||
const numericalYVariables: NumericalYComboLineDualVariables = { | ||
y: { | ||
lineLeft: { | ||
iri: leftIri, | ||
label: getLabelWithUnit(measuresByIri[leftIri]), | ||
getY: (d) => { | ||
return d[leftIri] !== null ? Number(d[leftIri]) : null; | ||
}, | ||
}, | ||
lineRight: { | ||
iri: rightIri, | ||
label: getLabelWithUnit(measuresByIri[rightIri]), | ||
getY: (d) => { | ||
return d[rightIri] !== null ? Number(d[rightIri]) : null; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return { | ||
...baseVariables, | ||
...temporalXVariables, | ||
...numericalYVariables, | ||
}; | ||
}; | ||
|
||
export const useComboLineDualStateData = ( | ||
chartProps: ChartProps<ComboConfig> & { aspectRatio: number }, | ||
variables: ComboLineDualStateVariables | ||
): ChartStateData => { | ||
const { chartConfig, observations } = chartProps; | ||
// FIXME: handle properly. | ||
const plottableData = observations; | ||
const sortedPlottableData = React.useMemo(() => { | ||
return sortData(plottableData, { | ||
getX: variables.getX, | ||
}); | ||
}, [plottableData, variables.getX]); | ||
const data = useChartData(sortedPlottableData, { | ||
chartConfig, | ||
getXAsDate: variables.getX, | ||
}); | ||
|
||
return { | ||
...data, | ||
allData: sortedPlottableData, | ||
}; | ||
}; |
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,9 @@ | ||
import { ComboLineDualState } from "@/charts/combo/combo-line-dual-state"; | ||
import { useChartState } from "@/charts/shared/chart-state"; | ||
|
||
export const ComboLineDual = () => { | ||
const state = useChartState() as ComboLineDualState; | ||
console.log(state); | ||
|
||
return null; | ||
}; |
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,87 @@ | ||
import React from "react"; | ||
|
||
import { BaseYGetter, sortData } from "@/charts/combo/combo-state-props"; | ||
import { getLabelWithUnit } from "@/charts/shared/chart-helpers"; | ||
import { | ||
BaseVariables, | ||
ChartStateData, | ||
TemporalXVariables, | ||
useBaseVariables, | ||
useChartData, | ||
useTemporalXVariables, | ||
} from "@/charts/shared/chart-state"; | ||
import { ComboConfig } from "@/configurator"; | ||
|
||
import { ChartProps } from "../shared/ChartProps"; | ||
|
||
type NumericalYComboLineSingleVariables = { | ||
y: { | ||
lines: BaseYGetter[]; | ||
}; | ||
}; | ||
|
||
export type ComboLineSingleStateVariables = BaseVariables & | ||
TemporalXVariables & | ||
NumericalYComboLineSingleVariables; | ||
|
||
export const useComboLineSingleStateVariables = ( | ||
props: ChartProps<ComboConfig> & { aspectRatio: number } | ||
): ComboLineSingleStateVariables => { | ||
const { chartConfig, dimensionsByIri, measuresByIri } = props; | ||
const { fields } = chartConfig; | ||
const { x } = fields; | ||
|
||
const baseVariables = useBaseVariables(chartConfig); | ||
const temporalXVariables = useTemporalXVariables(x, { | ||
dimensionsByIri, | ||
}); | ||
|
||
if (chartConfig.chartSubtype !== "line") { | ||
throw new Error("This hook is only for line charts!"); | ||
} | ||
|
||
if (chartConfig.fields.y.axisMode !== "single") { | ||
throw new Error("This hook is only for single axis line charts!"); | ||
} | ||
|
||
const numericalYVariables: NumericalYComboLineSingleVariables = { | ||
y: { | ||
lines: chartConfig.fields.y.componentIris.map((iri) => ({ | ||
iri, | ||
label: getLabelWithUnit(measuresByIri[iri]), | ||
getY: (d) => { | ||
return d[iri] !== null ? Number(d[iri]) : null; | ||
}, | ||
})), | ||
}, | ||
}; | ||
|
||
return { | ||
...baseVariables, | ||
...temporalXVariables, | ||
...numericalYVariables, | ||
}; | ||
}; | ||
|
||
export const useComboLineSingleStateData = ( | ||
chartProps: ChartProps<ComboConfig> & { aspectRatio: number }, | ||
variables: ComboLineSingleStateVariables | ||
): ChartStateData => { | ||
const { chartConfig, observations } = chartProps; | ||
// FIXME: handle properly. | ||
const plottableData = observations; | ||
const sortedPlottableData = React.useMemo(() => { | ||
return sortData(plottableData, { | ||
getX: variables.getX, | ||
}); | ||
}, [plottableData, variables.getX]); | ||
const data = useChartData(sortedPlottableData, { | ||
chartConfig, | ||
getXAsDate: variables.getX, | ||
}); | ||
|
||
return { | ||
...data, | ||
allData: sortedPlottableData, | ||
}; | ||
}; |
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,69 @@ | ||
import { ScaleLinear, ScaleOrdinal, ScaleTime } from "d3"; | ||
|
||
import { | ||
ComboLineSingleStateVariables, | ||
useComboLineSingleStateData, | ||
useComboLineSingleStateVariables, | ||
} from "@/charts/combo/combo-line-single-state-props"; | ||
import { | ||
ChartContext, | ||
ChartStateData, | ||
CommonChartState, | ||
InteractiveXTimeRangeState, | ||
} from "@/charts/shared/chart-state"; | ||
import { TooltipInfo } from "@/charts/shared/interaction/tooltip"; | ||
import { InteractionProvider } from "@/charts/shared/use-interaction"; | ||
import { Observer } from "@/charts/shared/use-width"; | ||
import { ComboConfig } from "@/configurator"; | ||
import { Observation } from "@/domain/data"; | ||
|
||
import { ChartProps } from "../shared/ChartProps"; | ||
|
||
export type ComboLineSingleState = CommonChartState & | ||
ComboLineSingleStateVariables & | ||
InteractiveXTimeRangeState & { | ||
chartType: "combo"; | ||
xKey: string; | ||
xScale: ScaleTime<number, number>; | ||
yScale: ScaleLinear<number, number>; | ||
colors: ScaleOrdinal<string, string>; | ||
chartWideData: ArrayLike<Observation>; | ||
getAnnotationInfo: (d: Observation) => TooltipInfo; | ||
}; | ||
|
||
const useComboLineSingleState = ( | ||
chartProps: ChartProps<ComboConfig> & { aspectRatio: number }, | ||
variables: ComboLineSingleStateVariables, | ||
data: ChartStateData | ||
): ComboLineSingleState => { | ||
return {} as unknown as ComboLineSingleState; | ||
}; | ||
|
||
const ComboLineSingleChartProvider = ( | ||
props: React.PropsWithChildren< | ||
ChartProps<ComboConfig> & { aspectRatio: number } | ||
> | ||
) => { | ||
const { children, ...chartProps } = props; | ||
const variables = useComboLineSingleStateVariables(chartProps); | ||
const data = useComboLineSingleStateData(chartProps, variables); | ||
const state = useComboLineSingleState(chartProps, variables, data); | ||
|
||
return ( | ||
<ChartContext.Provider value={state}>{children}</ChartContext.Provider> | ||
); | ||
}; | ||
|
||
export const ComboLineSingleChart = ( | ||
props: React.PropsWithChildren< | ||
ChartProps<ComboConfig> & { aspectRatio: number } | ||
> | ||
) => { | ||
return ( | ||
<Observer> | ||
<InteractionProvider> | ||
<ComboLineSingleChartProvider {...props} /> | ||
</InteractionProvider> | ||
</Observer> | ||
); | ||
}; |
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,9 @@ | ||
import { ComboLineSingleState } from "@/charts/combo/combo-line-single-state"; | ||
import { useChartState } from "@/charts/shared/chart-state"; | ||
|
||
export const ComboLineSingle = () => { | ||
const state = useChartState() as ComboLineSingleState; | ||
console.log(state); | ||
|
||
return null; | ||
}; |
Oops, something went wrong.