2022-03-15 19:42:04 +00:00
|
|
|
import { DatumValue } from '@nivo/core'
|
|
|
|
import { ResponsiveLine } from '@nivo/line'
|
|
|
|
import dayjs from 'dayjs'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { groupBy, sortBy, sumBy } from 'lodash'
|
2022-04-06 16:45:34 +00:00
|
|
|
import { memo } from 'react'
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Bet } from 'common/bet'
|
2022-07-28 02:40:33 +00:00
|
|
|
import { FreeResponseContract, MultipleChoiceContract } from 'common/contract'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { getOutcomeProbability } from 'common/calculate'
|
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-03-16 23:47:12 +00:00
|
|
|
const NUM_LINES = 6
|
|
|
|
|
2022-04-06 16:45:34 +00:00
|
|
|
export const AnswersGraph = memo(function AnswersGraph(props: {
|
2022-07-28 02:40:33 +00:00
|
|
|
contract: FreeResponseContract | MultipleChoiceContract
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[]
|
2022-03-24 17:03:08 +00:00
|
|
|
height?: number
|
2022-03-15 22:27:51 +00:00
|
|
|
}) {
|
2022-06-10 17:23:27 +00:00
|
|
|
const { contract, bets, height } = props
|
2022-03-16 23:47:12 +00:00
|
|
|
const { createdTime, resolutionTime, closeTime, answers } = contract
|
2022-03-15 19:42:04 +00:00
|
|
|
|
|
|
|
const { probsByOutcome, sortedOutcomes } = computeProbsByOutcome(
|
|
|
|
bets,
|
2022-03-15 22:27:51 +00:00
|
|
|
contract
|
2022-03-15 19:42:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const isClosed = !!closeTime && Date.now() > closeTime
|
|
|
|
const latestTime = dayjs(
|
|
|
|
resolutionTime && isClosed
|
|
|
|
? Math.min(resolutionTime, closeTime)
|
|
|
|
: isClosed
|
|
|
|
? closeTime
|
|
|
|
: resolutionTime ?? Date.now()
|
|
|
|
)
|
|
|
|
|
|
|
|
const { width } = useWindowSize()
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
const isLargeWidth = !width || width > 800
|
|
|
|
const labelLength = isLargeWidth ? 50 : 20
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-06-17 03:51:48 +00:00
|
|
|
// Add a fake datapoint so the line continues to the right
|
|
|
|
const endTime = latestTime.valueOf()
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const times = sortBy([
|
2022-03-16 23:47:12 +00:00
|
|
|
createdTime,
|
2022-03-15 19:42:04 +00:00
|
|
|
...bets.map((bet) => bet.createdTime),
|
2022-03-16 23:47:12 +00:00
|
|
|
endTime,
|
2022-03-15 19:42:04 +00:00
|
|
|
])
|
|
|
|
const dateTimes = times.map((time) => new Date(time))
|
|
|
|
|
2022-03-16 23:47:12 +00:00
|
|
|
const data = sortedOutcomes.map((outcome) => {
|
|
|
|
const betProbs = probsByOutcome[outcome]
|
|
|
|
// Add extra point for contract start and end.
|
|
|
|
const probs = [0, ...betProbs, betProbs[betProbs.length - 1]]
|
2022-03-15 19:42:04 +00:00
|
|
|
|
|
|
|
const points = probs.map((prob, i) => ({
|
|
|
|
x: dateTimes[i],
|
|
|
|
y: Math.round(prob * 100),
|
|
|
|
}))
|
|
|
|
|
|
|
|
const answer =
|
|
|
|
answers?.find((answer) => answer.id === outcome)?.text ?? 'None'
|
|
|
|
const answerText =
|
|
|
|
answer.slice(0, labelLength) + (answer.length > labelLength ? '...' : '')
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
return { id: answerText, data: points }
|
2022-03-15 19:42:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
data.reverse()
|
|
|
|
|
|
|
|
const yTickValues = [0, 25, 50, 75, 100]
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
const numXTickValues = isLargeWidth ? 5 : 2
|
2022-08-26 05:21:06 +00:00
|
|
|
const hourAgo = latestTime.subtract(1, 'hours')
|
|
|
|
const startDate = dayjs(contract.createdTime).isBefore(hourAgo)
|
2022-03-15 19:42:04 +00:00
|
|
|
? new Date(contract.createdTime)
|
2022-08-26 05:21:06 +00:00
|
|
|
: hourAgo.toDate()
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-06-22 15:45:15 +00:00
|
|
|
const multiYear = !dayjs(startDate).isSame(latestTime, 'year')
|
2022-03-15 19:42:04 +00:00
|
|
|
const lessThanAWeek = dayjs(startDate).add(1, 'week').isAfter(latestTime)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2022-04-18 23:02:40 +00:00
|
|
|
className="w-full"
|
|
|
|
style={{ height: height ?? (isLargeWidth ? 350 : 250) }}
|
2022-03-15 19:42:04 +00:00
|
|
|
>
|
|
|
|
<ResponsiveLine
|
|
|
|
data={data}
|
2022-03-16 23:47:12 +00:00
|
|
|
yScale={{ min: 0, max: 100, type: 'linear', stacked: true }}
|
2022-03-15 19:42:04 +00:00
|
|
|
yFormat={formatPercent}
|
|
|
|
gridYValues={yTickValues}
|
|
|
|
axisLeft={{
|
|
|
|
tickValues: yTickValues,
|
|
|
|
format: formatPercent,
|
|
|
|
}}
|
|
|
|
xScale={{
|
|
|
|
type: 'time',
|
|
|
|
min: startDate,
|
|
|
|
max: latestTime.toDate(),
|
|
|
|
}}
|
2022-06-22 15:45:15 +00:00
|
|
|
xFormat={(d) =>
|
|
|
|
formatTime(+d.valueOf(), multiYear, lessThanAWeek, lessThanAWeek)
|
|
|
|
}
|
2022-03-15 19:42:04 +00:00
|
|
|
axisBottom={{
|
|
|
|
tickValues: numXTickValues,
|
2022-06-22 15:45:15 +00:00
|
|
|
format: (time) => formatTime(+time, multiYear, lessThanAWeek, false),
|
2022-03-15 19:42:04 +00:00
|
|
|
}}
|
2022-03-15 22:20:37 +00:00
|
|
|
colors={{ scheme: 'pastel1' }}
|
2022-03-15 19:42:04 +00:00
|
|
|
pointSize={0}
|
2022-06-17 03:51:48 +00:00
|
|
|
curve="stepAfter"
|
2022-03-15 19:42:04 +00:00
|
|
|
enableSlices="x"
|
|
|
|
enableGridX={!!width && width >= 800}
|
|
|
|
enableArea
|
2022-03-15 22:20:37 +00:00
|
|
|
areaOpacity={1}
|
2022-06-17 03:51:48 +00:00
|
|
|
margin={{ top: 20, right: 20, bottom: 25, left: 40 }}
|
2022-04-18 23:02:40 +00:00
|
|
|
legends={[
|
|
|
|
{
|
|
|
|
anchor: 'top-left',
|
|
|
|
direction: 'column',
|
|
|
|
justify: false,
|
|
|
|
translateX: isLargeWidth ? 5 : 2,
|
|
|
|
translateY: 0,
|
|
|
|
itemsSpacing: 0,
|
|
|
|
itemTextColor: 'black',
|
|
|
|
itemDirection: 'left-to-right',
|
|
|
|
itemWidth: isLargeWidth ? 288 : 138,
|
|
|
|
itemHeight: 20,
|
|
|
|
itemBackground: 'white',
|
|
|
|
itemOpacity: 0.9,
|
|
|
|
symbolSize: 12,
|
|
|
|
effects: [
|
|
|
|
{
|
|
|
|
on: 'hover',
|
|
|
|
style: {
|
|
|
|
itemBackground: 'rgba(255, 255, 255, 1)',
|
|
|
|
itemOpacity: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
2022-03-15 19:42:04 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
2022-04-06 16:45:34 +00:00
|
|
|
})
|
2022-03-15 19:42:04 +00:00
|
|
|
|
|
|
|
function formatPercent(y: DatumValue) {
|
|
|
|
return `${Math.round(+y.toString())}%`
|
|
|
|
}
|
|
|
|
|
2022-06-22 15:45:15 +00:00
|
|
|
function formatTime(
|
|
|
|
time: number,
|
|
|
|
includeYear: boolean,
|
|
|
|
includeHour: boolean,
|
|
|
|
includeMinute: boolean
|
|
|
|
) {
|
2022-03-15 19:42:04 +00:00
|
|
|
const d = dayjs(time)
|
|
|
|
|
2022-06-22 15:45:15 +00:00
|
|
|
if (d.add(1, 'minute').isAfter(Date.now())) return 'Now'
|
|
|
|
|
|
|
|
let format: string
|
|
|
|
if (d.isSame(Date.now(), 'day')) {
|
|
|
|
format = '[Today]'
|
|
|
|
} else if (d.add(1, 'day').isSame(Date.now(), 'day')) {
|
|
|
|
format = '[Yesterday]'
|
|
|
|
} else {
|
|
|
|
format = 'MMM D'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (includeMinute) {
|
|
|
|
format += ', h:mma'
|
|
|
|
} else if (includeHour) {
|
|
|
|
format += ', ha'
|
|
|
|
} else if (includeYear) {
|
|
|
|
format += ', YYYY'
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.format(format)
|
2022-03-15 19:42:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 02:40:33 +00:00
|
|
|
const computeProbsByOutcome = (
|
|
|
|
bets: Bet[],
|
|
|
|
contract: FreeResponseContract | MultipleChoiceContract
|
|
|
|
) => {
|
|
|
|
const { totalBets, outcomeType } = contract
|
2022-03-22 00:48:48 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const betsByOutcome = groupBy(bets, (bet) => bet.outcome)
|
2022-03-15 19:42:04 +00:00
|
|
|
const outcomes = Object.keys(betsByOutcome).filter((outcome) => {
|
|
|
|
const maxProb = Math.max(
|
|
|
|
...betsByOutcome[outcome].map((bet) => bet.probAfter)
|
|
|
|
)
|
2022-07-28 02:40:33 +00:00
|
|
|
return (
|
|
|
|
(outcome !== '0' || outcomeType === 'MULTIPLE_CHOICE') &&
|
|
|
|
maxProb > 0.02 &&
|
|
|
|
totalBets[outcome] > 0.000000001
|
|
|
|
)
|
2022-03-15 19:42:04 +00:00
|
|
|
})
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const trackedOutcomes = sortBy(
|
2022-03-15 19:42:04 +00:00
|
|
|
outcomes,
|
2022-03-15 22:27:51 +00:00
|
|
|
(outcome) => -1 * getOutcomeProbability(contract, outcome)
|
2022-03-16 23:47:12 +00:00
|
|
|
).slice(0, NUM_LINES)
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const probsByOutcome = Object.fromEntries(
|
2022-03-16 23:47:12 +00:00
|
|
|
trackedOutcomes.map((outcome) => [outcome, [] as number[]])
|
2022-03-15 19:42:04 +00:00
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const sharesByOutcome = Object.fromEntries(
|
2022-03-15 19:42:04 +00:00
|
|
|
Object.keys(betsByOutcome).map((outcome) => [outcome, 0])
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const bet of bets) {
|
|
|
|
const { outcome, shares } = bet
|
|
|
|
sharesByOutcome[outcome] += shares
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const sharesSquared = sumBy(
|
2022-03-15 19:42:04 +00:00
|
|
|
Object.values(sharesByOutcome).map((shares) => shares ** 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const outcome of trackedOutcomes) {
|
|
|
|
probsByOutcome[outcome].push(
|
|
|
|
sharesByOutcome[outcome] ** 2 / sharesSquared
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { probsByOutcome, sortedOutcomes: trackedOutcomes }
|
|
|
|
}
|