2022-05-19 17:42:03 +00:00
|
|
|
import { DatumValue } from '@nivo/core'
|
2022-05-22 22:48:02 +00:00
|
|
|
import { Point, ResponsiveLine } from '@nivo/line'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { NUMERIC_GRAPH_COLOR } from 'common/numeric-constants'
|
|
|
|
import { memo } from 'react'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { range } from 'lodash'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { getDpmOutcomeProbabilities } from '../../../common/calculate-dpm'
|
|
|
|
import { NumericContract } from '../../../common/contract'
|
|
|
|
import { useWindowSize } from '../../hooks/use-window-size'
|
2022-05-22 22:48:02 +00:00
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { formatLargeNumber } from 'common/util/format'
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
export const NumericGraph = memo(function NumericGraph(props: {
|
|
|
|
contract: NumericContract
|
|
|
|
height?: number
|
|
|
|
}) {
|
|
|
|
const { contract, height } = props
|
|
|
|
const { totalShares, bucketCount, min, max } = contract
|
|
|
|
|
|
|
|
const bucketProbs = getDpmOutcomeProbabilities(totalShares)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const xs = range(bucketCount).map(
|
2022-05-19 17:42:03 +00:00
|
|
|
(i) => min + ((max - min) * i) / bucketCount
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const probs = range(bucketCount).map((i) => bucketProbs[`${i}`] * 100)
|
2022-05-19 17:42:03 +00:00
|
|
|
const points = probs.map((prob, i) => ({ x: xs[i], y: prob }))
|
|
|
|
const maxProb = Math.max(...probs)
|
|
|
|
const data = [{ id: 'Probability', data: points, color: NUMERIC_GRAPH_COLOR }]
|
|
|
|
|
|
|
|
const yTickValues = [
|
|
|
|
0,
|
|
|
|
0.25 * maxProb,
|
|
|
|
0.5 & maxProb,
|
|
|
|
0.75 * maxProb,
|
|
|
|
maxProb,
|
|
|
|
]
|
|
|
|
|
|
|
|
const { width } = useWindowSize()
|
|
|
|
|
|
|
|
const numXTickValues = !width || width < 800 ? 2 : 5
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="w-full overflow-hidden"
|
|
|
|
style={{ height: height ?? (!width || width >= 800 ? 350 : 250) }}
|
|
|
|
>
|
|
|
|
<ResponsiveLine
|
|
|
|
data={data}
|
|
|
|
yScale={{ min: 0, max: maxProb, type: 'linear' }}
|
|
|
|
yFormat={formatPercent}
|
|
|
|
axisLeft={{
|
|
|
|
tickValues: yTickValues,
|
|
|
|
format: formatPercent,
|
|
|
|
}}
|
|
|
|
xScale={{
|
|
|
|
type: 'linear',
|
|
|
|
min: min,
|
|
|
|
max: max,
|
|
|
|
}}
|
2022-05-23 03:05:52 +00:00
|
|
|
xFormat={(d) => `${formatLargeNumber(+d, 3)}`}
|
2022-05-19 17:42:03 +00:00
|
|
|
axisBottom={{
|
|
|
|
tickValues: numXTickValues,
|
2022-05-23 03:05:52 +00:00
|
|
|
format: (d) => `${formatLargeNumber(+d, 3)}`,
|
2022-05-19 17:42:03 +00:00
|
|
|
}}
|
|
|
|
colors={{ datum: 'color' }}
|
|
|
|
pointSize={0}
|
|
|
|
enableSlices="x"
|
2022-05-22 22:48:02 +00:00
|
|
|
sliceTooltip={({ slice }) => {
|
|
|
|
const point = slice.points[0]
|
|
|
|
return <Tooltip point={point} />
|
|
|
|
}}
|
2022-05-19 17:42:03 +00:00
|
|
|
enableGridX={!!width && width >= 800}
|
|
|
|
enableArea
|
|
|
|
margin={{ top: 20, right: 28, bottom: 22, left: 50 }}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
function formatPercent(y: DatumValue) {
|
|
|
|
const p = Math.round(+y * 100) / 100
|
|
|
|
return `${p}%`
|
|
|
|
}
|
2022-05-22 22:48:02 +00:00
|
|
|
|
|
|
|
function Tooltip(props: { point: Point }) {
|
|
|
|
const { point } = props
|
|
|
|
return (
|
|
|
|
<Col className="border border-gray-300 bg-white py-2 px-3">
|
|
|
|
<div
|
|
|
|
className="pb-1"
|
|
|
|
style={{
|
|
|
|
color: point.serieColor,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<strong>{point.serieId}</strong> {point.data.yFormatted}
|
|
|
|
</div>
|
|
|
|
<div>{formatLargeNumber(+point.data.x)}</div>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|