2022-09-30 23:57:48 +00:00
|
|
|
import { useMemo } from 'react'
|
2022-09-28 04:18:22 +00:00
|
|
|
import { last, sum, sortBy, groupBy } from 'lodash'
|
2022-09-28 08:00:39 +00:00
|
|
|
import { scaleTime, scaleLinear } from 'd3-scale'
|
2022-10-03 16:31:07 +00:00
|
|
|
import { curveStepAfter } from 'd3-shape'
|
2022-09-28 03:24:42 +00:00
|
|
|
|
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { Answer } from 'common/answer'
|
|
|
|
import { FreeResponseContract, MultipleChoiceContract } from 'common/contract'
|
|
|
|
import { getOutcomeProbability } from 'common/calculate'
|
2022-09-29 06:27:42 +00:00
|
|
|
import { DAY_MS } from 'common/util/time'
|
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
|
|
|
getDateRange,
|
|
|
|
getRightmostVisibleDate,
|
2022-09-29 04:14:34 +00:00
|
|
|
formatPct,
|
|
|
|
formatDateInRange,
|
2022-09-28 04:18:22 +00:00
|
|
|
} from '../helpers'
|
2022-09-29 19:51:38 +00:00
|
|
|
import { MultiPoint, MultiValueHistoryChart } 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
|
|
|
|
|
|
|
// thanks to https://observablehq.com/@jonhelfman/optimal-orders-for-choosing-categorical-colors
|
2022-10-04 22:36:03 +00:00
|
|
|
export const CATEGORY_COLORS = [
|
2022-09-28 03:24:42 +00:00
|
|
|
'#00b8dd',
|
|
|
|
'#eecafe',
|
|
|
|
'#874c62',
|
|
|
|
'#6457ca',
|
|
|
|
'#f773ba',
|
|
|
|
'#9c6bbc',
|
|
|
|
'#a87744',
|
|
|
|
'#af8a04',
|
|
|
|
'#bff9aa',
|
|
|
|
'#f3d89d',
|
|
|
|
'#c9a0f5',
|
|
|
|
'#ff00e5',
|
|
|
|
'#9dc6f7',
|
|
|
|
'#824475',
|
|
|
|
'#d973cc',
|
|
|
|
'#bc6808',
|
|
|
|
'#056e70',
|
|
|
|
'#677932',
|
|
|
|
'#00b287',
|
|
|
|
'#c8ab6c',
|
|
|
|
'#a2fb7a',
|
|
|
|
'#f8db68',
|
|
|
|
'#14675a',
|
|
|
|
'#8288f4',
|
|
|
|
'#fe1ca0',
|
|
|
|
'#ad6aff',
|
|
|
|
'#786306',
|
|
|
|
'#9bfbaf',
|
|
|
|
'#b00cf7',
|
|
|
|
'#2f7ec5',
|
|
|
|
'#4b998b',
|
|
|
|
'#42fa0e',
|
|
|
|
'#5b80a1',
|
|
|
|
'#962d9d',
|
|
|
|
'#3385ff',
|
|
|
|
'#48c5ab',
|
|
|
|
'#b2c873',
|
|
|
|
'#4cf9a4',
|
|
|
|
'#00ffff',
|
|
|
|
'#3cca73',
|
|
|
|
'#99ae17',
|
|
|
|
'#7af5cf',
|
|
|
|
'#52af45',
|
|
|
|
'#fbb80f',
|
|
|
|
'#29971b',
|
|
|
|
'#187c9a',
|
|
|
|
'#00d539',
|
|
|
|
'#bbfa1a',
|
|
|
|
'#61f55c',
|
|
|
|
'#cabc03',
|
|
|
|
'#ff9000',
|
|
|
|
'#779100',
|
|
|
|
'#bcfd6f',
|
|
|
|
'#70a560',
|
|
|
|
]
|
|
|
|
|
2022-10-04 08:18:22 +00:00
|
|
|
const MARGIN = { top: 20, right: 10, bottom: 20, left: 40 }
|
|
|
|
const MARGIN_X = MARGIN.left + MARGIN.right
|
|
|
|
const MARGIN_Y = MARGIN.top + MARGIN.bottom
|
|
|
|
|
2022-09-28 03:24:42 +00:00
|
|
|
const getTrackedAnswers = (
|
|
|
|
contract: FreeResponseContract | MultipleChoiceContract,
|
|
|
|
topN: number
|
|
|
|
) => {
|
|
|
|
const { answers, outcomeType, totalBets } = contract
|
|
|
|
const validAnswers = answers.filter((answer) => {
|
|
|
|
return (
|
|
|
|
(answer.id !== '0' || outcomeType === 'MULTIPLE_CHOICE') &&
|
|
|
|
totalBets[answer.id] > 0.000000001
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return sortBy(
|
|
|
|
validAnswers,
|
|
|
|
(answer) => -1 * getOutcomeProbability(contract, answer.id)
|
|
|
|
).slice(0, topN)
|
|
|
|
}
|
|
|
|
|
|
|
|
const getBetPoints = (answers: Answer[], bets: Bet[]) => {
|
|
|
|
const sortedBets = sortBy(bets, (b) => b.createdTime)
|
|
|
|
const betsByOutcome = groupBy(sortedBets, (bet) => bet.outcome)
|
|
|
|
const sharesByOutcome = Object.fromEntries(
|
|
|
|
Object.keys(betsByOutcome).map((outcome) => [outcome, 0])
|
|
|
|
)
|
2022-09-29 04:14:34 +00:00
|
|
|
const points: MultiPoint<Bet>[] = []
|
2022-09-28 03:24:42 +00:00
|
|
|
for (const bet of sortedBets) {
|
|
|
|
const { outcome, shares } = bet
|
|
|
|
sharesByOutcome[outcome] += shares
|
|
|
|
|
|
|
|
const sharesSquared = sum(
|
|
|
|
Object.values(sharesByOutcome).map((shares) => shares ** 2)
|
|
|
|
)
|
2022-09-29 04:14:34 +00:00
|
|
|
points.push({
|
|
|
|
x: new Date(bet.createdTime),
|
|
|
|
y: answers.map((a) => sharesByOutcome[a.id] ** 2 / sharesSquared),
|
2022-09-30 23:16:04 +00:00
|
|
|
obj: bet,
|
2022-09-29 04:14:34 +00:00
|
|
|
})
|
2022-09-28 03:24:42 +00:00
|
|
|
}
|
|
|
|
return points
|
|
|
|
}
|
|
|
|
|
2022-09-30 05:45:51 +00:00
|
|
|
type LegendItem = { color: string; label: string; value?: string }
|
|
|
|
const Legend = (props: { className?: string; items: LegendItem[] }) => {
|
|
|
|
const { items, className } = props
|
|
|
|
return (
|
|
|
|
<ol className={className}>
|
|
|
|
{items.map((item) => (
|
|
|
|
<li key={item.label} className="flex flex-row justify-between gap-4">
|
|
|
|
<Row className="items-center gap-2 overflow-hidden">
|
|
|
|
<span
|
|
|
|
className="h-4 w-4 shrink-0"
|
|
|
|
style={{ backgroundColor: item.color }}
|
|
|
|
></span>
|
|
|
|
<span className="text-semibold overflow-hidden text-ellipsis">
|
|
|
|
{item.label}
|
|
|
|
</span>
|
|
|
|
</Row>
|
|
|
|
<span className="text-greyscale-6">{item.value}</span>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ol>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-04 22:36:03 +00:00
|
|
|
export function useChartAnswers(
|
|
|
|
contract: FreeResponseContract | MultipleChoiceContract
|
|
|
|
) {
|
|
|
|
return useMemo(
|
|
|
|
() => getTrackedAnswers(contract, CATEGORY_COLORS.length),
|
|
|
|
[contract]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-28 03:24:42 +00:00
|
|
|
export const ChoiceContractChart = (props: {
|
|
|
|
contract: FreeResponseContract | MultipleChoiceContract
|
|
|
|
bets: Bet[]
|
2022-09-30 23:57:48 +00:00
|
|
|
width: number
|
|
|
|
height: number
|
2022-09-30 03:18:33 +00:00
|
|
|
onMouseOver?: (p: MultiPoint<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 04:14:34 +00:00
|
|
|
const [start, end] = getDateRange(contract)
|
2022-10-04 22:36:03 +00:00
|
|
|
const answers = useChartAnswers(contract)
|
2022-09-28 03:24:42 +00:00
|
|
|
const betPoints = useMemo(() => getBetPoints(answers, bets), [answers, bets])
|
|
|
|
const data = useMemo(
|
|
|
|
() => [
|
2022-09-30 04:35:20 +00:00
|
|
|
{ x: new Date(start), y: answers.map((_) => 0) },
|
2022-09-28 03:24:42 +00:00
|
|
|
...betPoints,
|
2022-09-29 04:14:34 +00:00
|
|
|
{
|
2022-09-30 04:35:20 +00:00
|
|
|
x: new Date(end ?? Date.now() + DAY_MS),
|
2022-09-29 04:14:34 +00:00
|
|
|
y: answers.map((a) => getOutcomeProbability(contract, a.id)),
|
|
|
|
},
|
2022-09-28 03:24:42 +00:00
|
|
|
],
|
2022-09-29 04:14:34 +00:00
|
|
|
[answers, contract, betPoints, start, end]
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
2022-09-28 04:18:22 +00:00
|
|
|
const rightmostDate = getRightmostVisibleDate(
|
2022-09-29 04:14:34 +00:00
|
|
|
end,
|
2022-09-30 04:35:20 +00:00
|
|
|
last(betPoints)?.x?.getTime(),
|
|
|
|
Date.now()
|
2022-09-28 04:18:22 +00:00
|
|
|
)
|
2022-09-29 04:14:34 +00:00
|
|
|
const visibleRange = [start, rightmostDate]
|
2022-09-29 06:27:42 +00:00
|
|
|
const xScale = scaleTime(visibleRange, [0, width - MARGIN_X])
|
2022-09-28 03:24:42 +00:00
|
|
|
const yScale = scaleLinear([0, 1], [height - MARGIN_Y, 0])
|
2022-09-29 04:14:34 +00:00
|
|
|
|
|
|
|
const ChoiceTooltip = useMemo(
|
2022-09-30 23:16:04 +00:00
|
|
|
() => (props: TooltipProps<Date, MultiPoint<Bet>>) => {
|
2022-10-04 21:02:44 +00:00
|
|
|
const { data, x, xScale } = props
|
2022-09-29 04:14:34 +00:00
|
|
|
const [start, end] = xScale.domain()
|
2022-10-04 21:02:44 +00:00
|
|
|
const d = xScale.invert(x)
|
2022-09-29 04:14:34 +00:00
|
|
|
const legendItems = sortBy(
|
2022-09-30 23:16:04 +00:00
|
|
|
data.y.map((p, i) => ({
|
2022-09-29 04:14:34 +00:00
|
|
|
color: CATEGORY_COLORS[i],
|
|
|
|
label: answers[i].text,
|
|
|
|
value: formatPct(p),
|
|
|
|
p,
|
|
|
|
})),
|
|
|
|
(item) => -item.p
|
|
|
|
).slice(0, 10)
|
|
|
|
return (
|
2022-09-30 05:45:51 +00:00
|
|
|
<>
|
2022-09-29 04:14:34 +00:00
|
|
|
<Row className="items-center gap-2">
|
2022-09-30 23:16:04 +00:00
|
|
|
{data.obj && (
|
|
|
|
<Avatar size="xxs" avatarUrl={data.obj.userAvatarUrl} />
|
|
|
|
)}
|
2022-09-30 05:45:51 +00:00
|
|
|
<span className="text-semibold text-base">
|
2022-09-30 23:16:04 +00:00
|
|
|
{formatDateInRange(d, start, end)}
|
2022-09-30 05:45:51 +00:00
|
|
|
</span>
|
2022-09-29 04:14:34 +00:00
|
|
|
</Row>
|
2022-09-30 05:45:51 +00:00
|
|
|
<Legend className="max-w-xs" items={legendItems} />
|
|
|
|
</>
|
2022-09-29 04:14:34 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
[answers]
|
|
|
|
)
|
|
|
|
|
2022-09-28 03:24:42 +00:00
|
|
|
return (
|
2022-09-30 23:57:48 +00:00
|
|
|
<MultiValueHistoryChart
|
|
|
|
w={width}
|
|
|
|
h={height}
|
2022-10-04 08:18:22 +00:00
|
|
|
margin={MARGIN}
|
2022-09-30 23:57:48 +00:00
|
|
|
xScale={xScale}
|
|
|
|
yScale={yScale}
|
2022-10-04 08:18:22 +00:00
|
|
|
yKind="percent"
|
2022-09-30 23:57:48 +00:00
|
|
|
data={data}
|
|
|
|
colors={CATEGORY_COLORS}
|
2022-10-03 16:31:07 +00:00
|
|
|
curve={curveStepAfter}
|
2022-09-30 23:57:48 +00:00
|
|
|
onMouseOver={onMouseOver}
|
|
|
|
Tooltip={ChoiceTooltip}
|
|
|
|
/>
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
|
|
|
}
|