2022-09-30 23:57:48 +00:00
|
|
|
import { useMemo } from 'react'
|
2022-09-28 04:18:22 +00:00
|
|
|
import { last, sortBy } from 'lodash'
|
2022-09-28 08:00:39 +00:00
|
|
|
import { scaleTime, scaleLog, scaleLinear } from 'd3-scale'
|
2022-09-28 03:24:42 +00:00
|
|
|
|
|
|
|
import { Bet } from 'common/bet'
|
2022-09-29 06:27:42 +00:00
|
|
|
import { DAY_MS } from 'common/util/time'
|
2022-09-28 03:24:42 +00:00
|
|
|
import { getInitialProbability, getProbability } from 'common/calculate'
|
2022-09-29 04:14:34 +00:00
|
|
|
import { formatLargeNumber } from 'common/util/format'
|
2022-09-28 03:24:42 +00:00
|
|
|
import { PseudoNumericContract } from 'common/contract'
|
|
|
|
import { NUMERIC_GRAPH_COLOR } from 'common/numeric-constants'
|
2022-09-28 04:18:22 +00:00
|
|
|
import {
|
2022-09-29 19:51:38 +00:00
|
|
|
TooltipProps,
|
2022-09-28 04:18:22 +00:00
|
|
|
MARGIN_X,
|
|
|
|
MARGIN_Y,
|
|
|
|
getDateRange,
|
|
|
|
getRightmostVisibleDate,
|
2022-09-29 04:14:34 +00:00
|
|
|
formatDateInRange,
|
2022-09-28 04:18:22 +00:00
|
|
|
} from '../helpers'
|
2022-09-29 19:51:38 +00:00
|
|
|
import { HistoryPoint, SingleValueHistoryChart } from '../generic-charts'
|
2022-09-29 04:14:34 +00:00
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Avatar } from 'web/components/avatar'
|
2022-09-28 03:24:42 +00:00
|
|
|
|
|
|
|
// mqp: note that we have an idiosyncratic version of 'log scale'
|
|
|
|
// contracts. the values are stored "linearly" and can include zero.
|
|
|
|
// as a result, we have to do some weird-looking stuff in this code
|
|
|
|
|
2022-09-29 01:03:30 +00:00
|
|
|
const getScaleP = (min: number, max: number, isLogScale: boolean) => {
|
|
|
|
return (p: number) =>
|
|
|
|
isLogScale
|
|
|
|
? 10 ** (p * Math.log10(max - min + 1)) + min - 1
|
|
|
|
: p * (max - min) + min
|
2022-09-28 03:24:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 01:03:30 +00:00
|
|
|
const getBetPoints = (bets: Bet[], scaleP: (p: number) => number) => {
|
2022-09-29 04:14:34 +00:00
|
|
|
return sortBy(bets, (b) => b.createdTime).map((b) => ({
|
|
|
|
x: new Date(b.createdTime),
|
|
|
|
y: scaleP(b.probAfter),
|
2022-09-30 23:16:04 +00:00
|
|
|
obj: b,
|
2022-09-29 04:14:34 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2022-09-30 23:16:04 +00:00
|
|
|
const PseudoNumericChartTooltip = (
|
|
|
|
props: TooltipProps<Date, HistoryPoint<Bet>>
|
|
|
|
) => {
|
|
|
|
const { data, mouseX, xScale } = props
|
2022-09-29 04:14:34 +00:00
|
|
|
const [start, end] = xScale.domain()
|
2022-09-30 23:16:04 +00:00
|
|
|
const d = xScale.invert(mouseX)
|
2022-09-29 04:14:34 +00:00
|
|
|
return (
|
2022-09-30 05:45:51 +00:00
|
|
|
<Row className="items-center gap-2">
|
2022-09-30 23:16:04 +00:00
|
|
|
{data.obj && <Avatar size="xs" avatarUrl={data.obj.userAvatarUrl} />}
|
|
|
|
<span className="font-semibold">{formatDateInRange(d, start, end)}</span>
|
|
|
|
<span className="text-greyscale-6">{formatLargeNumber(data.y)}</span>
|
2022-09-29 04:14:34 +00:00
|
|
|
</Row>
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PseudoNumericContractChart = (props: {
|
|
|
|
contract: PseudoNumericContract
|
|
|
|
bets: Bet[]
|
2022-09-30 23:57:48 +00:00
|
|
|
width: number
|
|
|
|
height: number
|
2022-09-30 03:18:33 +00:00
|
|
|
onMouseOver?: (p: HistoryPoint<Bet> | undefined) => void
|
2022-09-28 03:24:42 +00:00
|
|
|
}) => {
|
2022-09-30 23:57:48 +00:00
|
|
|
const { contract, bets, width, height, onMouseOver } = props
|
2022-09-29 01:03:30 +00:00
|
|
|
const { min, max, isLogScale } = contract
|
2022-09-30 04:35:20 +00:00
|
|
|
const [start, end] = getDateRange(contract)
|
2022-09-29 01:03:30 +00:00
|
|
|
const scaleP = useMemo(
|
|
|
|
() => getScaleP(min, max, isLogScale),
|
|
|
|
[min, max, isLogScale]
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
2022-09-29 01:03:30 +00:00
|
|
|
const startP = scaleP(getInitialProbability(contract))
|
|
|
|
const endP = scaleP(getProbability(contract))
|
|
|
|
const betPoints = useMemo(() => getBetPoints(bets, scaleP), [bets, scaleP])
|
2022-09-28 03:24:42 +00:00
|
|
|
const data = useMemo(
|
|
|
|
() => [
|
2022-09-30 04:35:20 +00:00
|
|
|
{ x: new Date(start), y: startP },
|
2022-09-28 03:24:42 +00:00
|
|
|
...betPoints,
|
2022-09-30 04:35:20 +00:00
|
|
|
{ x: new Date(end ?? Date.now() + DAY_MS), y: endP },
|
2022-09-28 03:24:42 +00:00
|
|
|
],
|
2022-09-30 04:35:20 +00:00
|
|
|
[betPoints, start, startP, end, endP]
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
2022-09-28 04:18:22 +00:00
|
|
|
const rightmostDate = getRightmostVisibleDate(
|
2022-09-30 04:35:20 +00:00
|
|
|
end,
|
|
|
|
last(betPoints)?.x?.getTime(),
|
|
|
|
Date.now()
|
2022-09-28 04:18:22 +00:00
|
|
|
)
|
2022-09-30 04:35:20 +00:00
|
|
|
const visibleRange = [start, rightmostDate]
|
2022-10-03 09:02:38 +00:00
|
|
|
const xScale = scaleTime(visibleRange, [0, width ?? 0 - MARGIN_X])
|
2022-09-29 01:03:30 +00:00
|
|
|
// clamp log scale to make sure zeroes go to the bottom
|
|
|
|
const yScale = isLogScale
|
2022-10-03 09:02:38 +00:00
|
|
|
? scaleLog([Math.max(min, 1), max], [height ?? 0 - MARGIN_Y, 0]).clamp(true)
|
|
|
|
: scaleLinear([min, max], [height ?? 0 - MARGIN_Y, 0])
|
2022-09-28 03:24:42 +00:00
|
|
|
return (
|
2022-09-30 23:57:48 +00:00
|
|
|
<SingleValueHistoryChart
|
|
|
|
w={width}
|
|
|
|
h={height}
|
|
|
|
xScale={xScale}
|
|
|
|
yScale={yScale}
|
|
|
|
data={data}
|
|
|
|
onMouseOver={onMouseOver}
|
|
|
|
Tooltip={PseudoNumericChartTooltip}
|
|
|
|
color={NUMERIC_GRAPH_COLOR}
|
|
|
|
/>
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
|
|
|
}
|