From c2a5ce6ba670df2b43c5dad4436282cbc761c369 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Wed, 28 Sep 2022 20:55:23 -0700 Subject: [PATCH] Tidying --- web/components/charts/contract/choice.tsx | 3 +-- web/components/charts/generic-charts.tsx | 31 +++-------------------- web/components/charts/helpers.tsx | 24 ++++++++++++++++-- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/web/components/charts/contract/choice.tsx b/web/components/charts/contract/choice.tsx index 584d3d7c..08d20442 100644 --- a/web/components/charts/contract/choice.tsx +++ b/web/components/charts/contract/choice.tsx @@ -8,6 +8,7 @@ import { FreeResponseContract, MultipleChoiceContract } from 'common/contract' import { getOutcomeProbability } from 'common/calculate' import { useIsMobile } from 'web/hooks/use-is-mobile' import { + Legend, MARGIN_X, MARGIN_Y, MAX_DATE, @@ -17,7 +18,6 @@ import { formatDateInRange, } from '../helpers' import { - Legend, MultiPoint, MultiValueHistoryChart, MultiValueHistoryTooltipProps, @@ -196,7 +196,6 @@ export const ChoiceContractChart = (props: { yScale={yScale} data={data} colors={CATEGORY_COLORS} - labels={answers.map((answer) => answer.text)} Tooltip={ChoiceTooltip} pct /> diff --git a/web/components/charts/generic-charts.tsx b/web/components/charts/generic-charts.tsx index a4834f4e..d9872b0e 100644 --- a/web/components/charts/generic-charts.tsx +++ b/web/components/charts/generic-charts.tsx @@ -23,7 +23,6 @@ import { formatPct, } from './helpers' import { useEvent } from 'web/hooks/use-event' -import { Row } from 'web/components/layout/row' export type MultiPoint = { x: Date; y: number[]; datum?: T } export type HistoryPoint = { x: Date; y: number; datum?: T } @@ -36,28 +35,6 @@ const getTickValues = (min: number, max: number, n: number) => { return [min, ...range(1, n - 1).map((i) => min + step * i), max] } -type LegendItem = { color: string; label: string; value?: string } - -export const Legend = (props: { className?: string; items: LegendItem[] }) => { - const { items, className } = props - return ( -
    - {items.map((item) => ( -
  1. - - - {item.label} - - {item.value} -
  2. - ))} -
- ) -} - export const SingleValueDistributionChart = (props: { data: DistributionPoint[] w: number @@ -158,14 +135,13 @@ export const MultiValueHistoryChart = (props: { data: MultiPoint[] w: number h: number - labels: readonly string[] colors: readonly string[] xScale: ScaleTime yScale: ScaleContinuousNumeric Tooltip?: TooltipContent> pct?: boolean }) => { - const { colors, data, yScale, labels, w, h, Tooltip, pct } = props + const { colors, data, yScale, w, h, Tooltip, pct } = props const [viewXScale, setViewXScale] = useState>() const [mouseState, setMouseState] = useState>>() @@ -184,17 +160,16 @@ export const MultiValueHistoryChart = (props: { const yAxis = pct ? axisLeft(yScale).tickValues(pctTickValues).tickFormat(formatPct) : axisLeft(yScale) - return { xAxis, yAxis } }, [w, h, pct, xScale, yScale]) const series = useMemo(() => { const d3Stack = stack, number>() - .keys(range(0, labels.length)) + .keys(range(0, Math.max(...data.map(({ y }) => y.length)))) .value(({ y }, o) => y[o]) .order(stackOrderReverse) return d3Stack(data) - }, [data, labels.length]) + }, [data]) const onSelect = useEvent((ev: D3BrushEvent>) => { if (ev.selection) { diff --git a/web/components/charts/helpers.tsx b/web/components/charts/helpers.tsx index 017529af..2ed59ce2 100644 --- a/web/components/charts/helpers.tsx +++ b/web/components/charts/helpers.tsx @@ -8,6 +8,7 @@ import dayjs from 'dayjs' import clsx from 'clsx' import { Contract } from 'common/contract' +import { Row } from 'web/components/layout/row' export const MARGIN = { top: 20, right: 10, bottom: 20, left: 40 } export const MARGIN_X = MARGIN.right + MARGIN.left @@ -181,8 +182,8 @@ export const SVGChart = (props: { ) } +export type TooltipContent

= React.ComponentType

export type TooltipPosition = { top: number; left: number } - export const TooltipContainer = ( props: TooltipPosition & { className?: string; children: React.ReactNode } ) => { @@ -200,7 +201,26 @@ export const TooltipContainer = ( ) } -export type TooltipContent

= React.ComponentType

+export type LegendItem = { color: string; label: string; value?: string } +export const Legend = (props: { className?: string; items: LegendItem[] }) => { + const { items, className } = props + return ( +

    + {items.map((item) => ( +
  1. + + + {item.label} + + {item.value} +
  2. + ))} +
+ ) +} export const getDateRange = (contract: Contract) => { const { createdTime, closeTime, resolutionTime } = contract