Expose onMouseOver chart event to hook into from outside (#967)

This commit is contained in:
Marshall Polaris 2022-09-29 20:18:33 -07:00 committed by GitHub
parent 2625ab1549
commit 5b5a919ed7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 28 deletions

View File

@ -46,20 +46,20 @@ export const BinaryContractChart = (props: {
contract: BinaryContract contract: BinaryContract
bets: Bet[] bets: Bet[]
height?: number height?: number
onMouseOver?: (p: HistoryPoint<Bet> | undefined) => void
}) => { }) => {
const { contract, bets } = props const { contract, bets, onMouseOver } = props
const [startDate, endDate] = getDateRange(contract) const [startDate, endDate] = getDateRange(contract)
const startP = getInitialProbability(contract) const startP = getInitialProbability(contract)
const endP = getProbability(contract) const endP = getProbability(contract)
const betPoints = useMemo(() => getBetPoints(bets), [bets]) const betPoints = useMemo(() => getBetPoints(bets), [bets])
const data = useMemo( const data = useMemo(() => {
() => [ return [
{ x: startDate, y: startP }, { x: startDate, y: startP },
...betPoints, ...betPoints,
{ x: endDate ?? new Date(Date.now() + DAY_MS), y: endP }, { x: endDate ?? new Date(Date.now() + DAY_MS), y: endP },
], ]
[startDate, startP, endDate, endP, betPoints] }, [startDate, startP, endDate, endP, betPoints])
)
const rightmostDate = getRightmostVisibleDate( const rightmostDate = getRightmostVisibleDate(
endDate, endDate,
@ -84,6 +84,7 @@ export const BinaryContractChart = (props: {
yScale={yScale} yScale={yScale}
data={data} data={data}
color="#11b981" color="#11b981"
onMouseOver={onMouseOver}
Tooltip={BinaryChartTooltip} Tooltip={BinaryChartTooltip}
pct pct
/> />

View File

@ -125,8 +125,9 @@ export const ChoiceContractChart = (props: {
contract: FreeResponseContract | MultipleChoiceContract contract: FreeResponseContract | MultipleChoiceContract
bets: Bet[] bets: Bet[]
height?: number height?: number
onMouseOver?: (p: MultiPoint<Bet> | undefined) => void
}) => { }) => {
const { contract, bets } = props const { contract, bets, onMouseOver } = props
const [start, end] = getDateRange(contract) const [start, end] = getDateRange(contract)
const answers = useMemo( const answers = useMemo(
() => getTrackedAnswers(contract, CATEGORY_COLORS.length), () => getTrackedAnswers(contract, CATEGORY_COLORS.length),
@ -194,6 +195,7 @@ export const ChoiceContractChart = (props: {
yScale={yScale} yScale={yScale}
data={data} data={data}
colors={CATEGORY_COLORS} colors={CATEGORY_COLORS}
onMouseOver={onMouseOver}
Tooltip={ChoiceTooltip} Tooltip={ChoiceTooltip}
pct pct
/> />

View File

@ -33,8 +33,9 @@ const NumericChartTooltip = (props: TooltipProps<DistributionPoint>) => {
export const NumericContractChart = (props: { export const NumericContractChart = (props: {
contract: NumericContract contract: NumericContract
height?: number height?: number
onMouseOver?: (p: DistributionPoint | undefined) => void
}) => { }) => {
const { contract } = props const { contract, onMouseOver } = props
const { min, max } = contract const { min, max } = contract
const data = useMemo(() => getNumericChartData(contract), [contract]) const data = useMemo(() => getNumericChartData(contract), [contract])
const isMobile = useIsMobile(800) const isMobile = useIsMobile(800)
@ -54,6 +55,7 @@ export const NumericContractChart = (props: {
yScale={yScale} yScale={yScale}
data={data} data={data}
color={NUMERIC_GRAPH_COLOR} color={NUMERIC_GRAPH_COLOR}
onMouseOver={onMouseOver}
Tooltip={NumericChartTooltip} Tooltip={NumericChartTooltip}
/> />
)} )}

View File

@ -58,8 +58,9 @@ export const PseudoNumericContractChart = (props: {
contract: PseudoNumericContract contract: PseudoNumericContract
bets: Bet[] bets: Bet[]
height?: number height?: number
onMouseOver?: (p: HistoryPoint<Bet> | undefined) => void
}) => { }) => {
const { contract, bets } = props const { contract, bets, onMouseOver } = props
const { min, max, isLogScale } = contract const { min, max, isLogScale } = contract
const [startDate, endDate] = getDateRange(contract) const [startDate, endDate] = getDateRange(contract)
const scaleP = useMemo( const scaleP = useMemo(
@ -102,6 +103,7 @@ export const PseudoNumericContractChart = (props: {
xScale={xScale} xScale={xScale}
yScale={yScale} yScale={yScale}
data={data} data={data}
onMouseOver={onMouseOver}
Tooltip={PseudoNumericChartTooltip} Tooltip={PseudoNumericChartTooltip}
color={NUMERIC_GRAPH_COLOR} color={NUMERIC_GRAPH_COLOR}
/> />

View File

@ -38,6 +38,7 @@ export const DistributionChart = <P extends DistributionPoint>(props: {
color: string color: string
xScale: ScaleContinuousNumeric<number, number> xScale: ScaleContinuousNumeric<number, number>
yScale: ScaleContinuousNumeric<number, number> yScale: ScaleContinuousNumeric<number, number>
onMouseOver?: (p: P | undefined) => void
Tooltip?: TooltipComponent<P> Tooltip?: TooltipComponent<P>
}) => { }) => {
const { color, data, yScale, w, h, Tooltip } = props const { color, data, yScale, w, h, Tooltip } = props
@ -71,12 +72,9 @@ export const DistributionChart = <P extends DistributionPoint>(props: {
const onMouseOver = useEvent((mouseX: number) => { const onMouseOver = useEvent((mouseX: number) => {
const queryX = xScale.invert(mouseX) const queryX = xScale.invert(mouseX)
const item = data[xBisector.left(data, queryX) - 1] const item = data[xBisector.left(data, queryX) - 1]
if (item == null) { const result = item ? { ...item, x: queryX } : undefined
// this can happen if you are on the very left or right edge of the chart, props.onMouseOver?.(result)
// so your queryX is out of bounds return result
return
}
return { ...item, x: queryX }
}) })
return ( return (
@ -108,6 +106,7 @@ export const MultiValueHistoryChart = <P extends MultiPoint>(props: {
colors: readonly string[] colors: readonly string[]
xScale: ScaleTime<number, number> xScale: ScaleTime<number, number>
yScale: ScaleContinuousNumeric<number, number> yScale: ScaleContinuousNumeric<number, number>
onMouseOver?: (p: P | undefined) => void
Tooltip?: TooltipComponent<P> Tooltip?: TooltipComponent<P>
pct?: boolean pct?: boolean
}) => { }) => {
@ -156,12 +155,9 @@ export const MultiValueHistoryChart = <P extends MultiPoint>(props: {
const onMouseOver = useEvent((mouseX: number) => { const onMouseOver = useEvent((mouseX: number) => {
const queryX = xScale.invert(mouseX) const queryX = xScale.invert(mouseX)
const item = data[xBisector.left(data, queryX) - 1] const item = data[xBisector.left(data, queryX) - 1]
if (item == null) { const result = item ? { ...item, x: queryX } : undefined
// this can happen if you are on the very left or right edge of the chart, props.onMouseOver?.(result)
// so your queryX is out of bounds return result
return
}
return { ...item, x: queryX }
}) })
return ( return (
@ -196,10 +192,11 @@ export const SingleValueHistoryChart = <P extends HistoryPoint>(props: {
color: string color: string
xScale: ScaleTime<number, number> xScale: ScaleTime<number, number>
yScale: ScaleContinuousNumeric<number, number> yScale: ScaleContinuousNumeric<number, number>
onMouseOver?: (p: P | undefined) => void
Tooltip?: TooltipComponent<P> Tooltip?: TooltipComponent<P>
pct?: boolean pct?: boolean
}) => { }) => {
const { color, data, pct, yScale, w, h, Tooltip } = props const { color, data, yScale, w, h, Tooltip, pct } = props
const [viewXScale, setViewXScale] = useState<ScaleTime<number, number>>() const [viewXScale, setViewXScale] = useState<ScaleTime<number, number>>()
const xScale = viewXScale ?? props.xScale const xScale = viewXScale ?? props.xScale
@ -235,12 +232,9 @@ export const SingleValueHistoryChart = <P extends HistoryPoint>(props: {
const onMouseOver = useEvent((mouseX: number) => { const onMouseOver = useEvent((mouseX: number) => {
const queryX = xScale.invert(mouseX) const queryX = xScale.invert(mouseX)
const item = data[xBisector.left(data, queryX) - 1] const item = data[xBisector.left(data, queryX) - 1]
if (item == null) { const result = item ? { ...item, x: queryX } : undefined
// this can happen if you are on the very left or right edge of the chart, props.onMouseOver?.(result)
// so your queryX is out of bounds return result
return
}
return { ...item, x: queryX }
}) })
return ( return (