Continue tweaking time selection handling to be perfect

This commit is contained in:
Marshall Polaris 2022-09-27 18:34:21 -07:00
parent 399a3121ca
commit ac887a729b
4 changed files with 29 additions and 22 deletions

View File

@ -6,7 +6,7 @@ import { Bet } from 'common/bet'
import { getInitialProbability, getProbability } from 'common/calculate' import { getInitialProbability, getProbability } from 'common/calculate'
import { BinaryContract } from 'common/contract' import { BinaryContract } from 'common/contract'
import { useIsMobile } from 'web/hooks/use-is-mobile' import { useIsMobile } from 'web/hooks/use-is-mobile'
import { MARGIN_X, MARGIN_Y, getDateRange } from '../helpers' import { MARGIN_X, MARGIN_Y, MAX_DATE, getDateRange } from '../helpers'
import { SingleValueHistoryChart } from '../generic-charts' import { SingleValueHistoryChart } from '../generic-charts'
import { useElementWidth } from 'web/hooks/use-element-width' import { useElementWidth } from 'web/hooks/use-element-width'
@ -30,21 +30,22 @@ export const BinaryContractChart = (props: {
height?: number height?: number
}) => { }) => {
const { contract, bets } = props const { contract, bets } = props
const [start, end] = getDateRange(contract) const [contractStart, contractEnd] = getDateRange(contract)
const betPoints = useMemo(() => getBetPoints(bets), [bets]) const betPoints = useMemo(() => getBetPoints(bets), [bets])
const data = useMemo( const data = useMemo(
() => [ () => [
getStartPoint(contract, start), getStartPoint(contract, contractStart),
...betPoints, ...betPoints,
getEndPoint(contract, end), getEndPoint(contract, contractEnd ?? MAX_DATE),
], ],
[contract, betPoints, start, end] [contract, betPoints, contractStart, contractEnd]
) )
const visibleRange = [contractStart, contractEnd ?? Date.now()]
const isMobile = useIsMobile(800) const isMobile = useIsMobile(800)
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const width = useElementWidth(containerRef) ?? 0 const width = useElementWidth(containerRef) ?? 0
const height = props.height ?? (isMobile ? 150 : 250) const height = props.height ?? (isMobile ? 150 : 250)
const xScale = scaleTime([start, end], [0, width - MARGIN_X]) const xScale = scaleTime(visibleRange, [0, width - MARGIN_X])
const yScale = scaleLinear([0, 1], [height - MARGIN_Y, 0]) const yScale = scaleLinear([0, 1], [height - MARGIN_Y, 0])
return ( return (
<div ref={containerRef}> <div ref={containerRef}>

View File

@ -7,7 +7,7 @@ import { Answer } from 'common/answer'
import { FreeResponseContract, MultipleChoiceContract } from 'common/contract' import { FreeResponseContract, MultipleChoiceContract } from 'common/contract'
import { getOutcomeProbability } from 'common/calculate' import { getOutcomeProbability } from 'common/calculate'
import { useIsMobile } from 'web/hooks/use-is-mobile' import { useIsMobile } from 'web/hooks/use-is-mobile'
import { MARGIN_X, MARGIN_Y, getDateRange } from '../helpers' import { MARGIN_X, MARGIN_Y, MAX_DATE, getDateRange } from '../helpers'
import { MultiPoint, MultiValueHistoryChart } from '../generic-charts' import { MultiPoint, MultiValueHistoryChart } from '../generic-charts'
import { useElementWidth } from 'web/hooks/use-element-width' import { useElementWidth } from 'web/hooks/use-element-width'
@ -129,7 +129,7 @@ export const ChoiceContractChart = (props: {
height?: number height?: number
}) => { }) => {
const { contract, bets } = props const { contract, bets } = props
const [start, end] = getDateRange(contract) const [contractStart, contractEnd] = getDateRange(contract)
const answers = useMemo( const answers = useMemo(
() => getTrackedAnswers(contract, CATEGORY_COLORS.length), () => getTrackedAnswers(contract, CATEGORY_COLORS.length),
[contract] [contract]
@ -137,17 +137,19 @@ export const ChoiceContractChart = (props: {
const betPoints = useMemo(() => getBetPoints(answers, bets), [answers, bets]) const betPoints = useMemo(() => getBetPoints(answers, bets), [answers, bets])
const data = useMemo( const data = useMemo(
() => [ () => [
getStartPoint(answers, start), getStartPoint(answers, contractStart),
...betPoints, ...betPoints,
getEndPoint(answers, contract, end), getEndPoint(answers, contract, contractEnd ?? MAX_DATE),
], ],
[answers, contract, betPoints, start, end] [answers, contract, betPoints, contractStart, contractEnd]
) )
const visibleRange = [contractStart, contractEnd ?? Date.now()]
const isMobile = useIsMobile(800) const isMobile = useIsMobile(800)
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const width = useElementWidth(containerRef) ?? 0 const width = useElementWidth(containerRef) ?? 0
const height = props.height ?? (isMobile ? 150 : 250) const height = props.height ?? (isMobile ? 150 : 250)
const xScale = scaleTime([start, end], [0, width - MARGIN_X]) const xScale = scaleTime(visibleRange, [0, width - MARGIN_X])
const yScale = scaleLinear([0, 1], [height - MARGIN_Y, 0]) const yScale = scaleLinear([0, 1], [height - MARGIN_Y, 0])
return ( return (
<div ref={containerRef}> <div ref={containerRef}>

View File

@ -7,7 +7,7 @@ import { getInitialProbability, getProbability } from 'common/calculate'
import { PseudoNumericContract } from 'common/contract' import { PseudoNumericContract } from 'common/contract'
import { NUMERIC_GRAPH_COLOR } from 'common/numeric-constants' import { NUMERIC_GRAPH_COLOR } from 'common/numeric-constants'
import { useIsMobile } from 'web/hooks/use-is-mobile' import { useIsMobile } from 'web/hooks/use-is-mobile'
import { MARGIN_X, MARGIN_Y, getDateRange } from '../helpers' import { MARGIN_X, MARGIN_Y, MAX_DATE, getDateRange } from '../helpers'
import { SingleValueHistoryChart } from '../generic-charts' import { SingleValueHistoryChart } from '../generic-charts'
import { useElementWidth } from 'web/hooks/use-element-width' import { useElementWidth } from 'web/hooks/use-element-width'
@ -42,24 +42,26 @@ export const PseudoNumericContractChart = (props: {
height?: number height?: number
}) => { }) => {
const { contract, bets } = props const { contract, bets } = props
const [start, end] = getDateRange(contract) const [contractStart, contractEnd] = getDateRange(contract)
const betPoints = useMemo( const betPoints = useMemo(
() => getBetPoints(contract, bets), () => getBetPoints(contract, bets),
[contract, bets] [contract, bets]
) )
const data = useMemo( const data = useMemo(
() => [ () => [
getStartPoint(contract, start), getStartPoint(contract, contractStart),
...betPoints, ...betPoints,
getEndPoint(contract, end), getEndPoint(contract, contractEnd ?? MAX_DATE),
], ],
[contract, betPoints, start, end] [contract, betPoints, contractStart, contractEnd]
) )
const visibleRange = [contractStart, contractEnd ?? Date.now()]
const isMobile = useIsMobile(800) const isMobile = useIsMobile(800)
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const width = useElementWidth(containerRef) ?? 0 const width = useElementWidth(containerRef) ?? 0
const height = props.height ?? (isMobile ? 150 : 250) const height = props.height ?? (isMobile ? 150 : 250)
const xScale = scaleTime([start, end], [0, width - MARGIN_X]) const xScale = scaleTime(visibleRange, [0, width - MARGIN_X])
const yScale = contract.isLogScale const yScale = contract.isLogScale
? scaleLog( ? scaleLog(
[Math.max(contract.min, 1), contract.max], [Math.max(contract.min, 1), contract.max],

View File

@ -18,6 +18,9 @@ export const MARGIN = { top: 20, right: 10, bottom: 20, left: 40 }
export const MARGIN_X = MARGIN.right + MARGIN.left export const MARGIN_X = MARGIN.right + MARGIN.left
export const MARGIN_Y = MARGIN.top + MARGIN.bottom export const MARGIN_Y = MARGIN.top + MARGIN.bottom
export const MAX_TIMESTAMP = 8640000000000000
export const MAX_DATE = new Date(MAX_TIMESTAMP)
export const XAxis = <X,>(props: { w: number; h: number; axis: Axis<X> }) => { export const XAxis = <X,>(props: { w: number; h: number; axis: Axis<X> }) => {
const { h, axis } = props const { h, axis } = props
const axisRef = useRef<SVGGElement>(null) const axisRef = useRef<SVGGElement>(null)
@ -198,8 +201,7 @@ export const ChartTooltip = (
export const getDateRange = (contract: Contract) => { export const getDateRange = (contract: Contract) => {
const { createdTime, closeTime, resolutionTime } = contract const { createdTime, closeTime, resolutionTime } = contract
const now = Date.now() const isClosed = !!closeTime && Date.now() > closeTime
const isClosed = !!closeTime && now > closeTime const endDate = resolutionTime ?? (isClosed ? closeTime : null)
const endDate = resolutionTime ?? (isClosed ? closeTime : now) return [new Date(createdTime), endDate ? new Date(endDate) : null] as const
return [new Date(createdTime), new Date(endDate)] as const
} }