manifold/web/components/analytics/charts.tsx

136 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-05-19 20:14:13 +00:00
import { Point, ResponsiveLine } from '@nivo/line'
import dayjs from 'dayjs'
import { zip } from 'lodash'
import { useWindowSize } from 'web/hooks/use-window-size'
2022-05-19 20:14:13 +00:00
import { Col } from '../layout/col'
export function DailyCountChart(props: {
startDate: number
dailyCounts: number[]
2022-03-03 20:59:12 +00:00
small?: boolean
}) {
2022-03-03 20:59:12 +00:00
const { dailyCounts, startDate, small } = props
const { width } = useWindowSize()
const dates = dailyCounts.map((_, i) =>
dayjs(startDate).add(i, 'day').toDate()
)
const points = zip(dates, dailyCounts).map(([date, betCount]) => ({
x: date,
y: betCount,
}))
2022-03-03 20:59:12 +00:00
const data = [{ id: 'Count', data: points, color: '#11b981' }]
const bottomAxisTicks = width && width < 600 ? 6 : undefined
return (
<div
className="w-full overflow-hidden"
2022-03-03 20:59:12 +00:00
style={{ height: !small && (!width || width >= 800) ? 400 : 250 }}
>
<ResponsiveLine
data={data}
2022-03-03 20:59:12 +00:00
yScale={{ type: 'linear', stacked: false }}
xScale={{
type: 'time',
}}
axisBottom={{
tickValues: bottomAxisTicks,
format: (date) => dayjs(date).format('MMM DD'),
}}
colors={{ datum: 'color' }}
2022-03-20 21:23:25 +00:00
pointSize={0}
pointBorderWidth={1}
pointBorderColor="#fff"
enableSlices="x"
enableGridX={!!width && width >= 800}
enableArea
margin={{ top: 20, right: 28, bottom: 22, left: 40 }}
2022-05-19 20:14:13 +00:00
sliceTooltip={({ slice }) => {
const point = slice.points[0]
return <Tooltip point={point} />
}}
/>
</div>
)
}
2022-03-22 21:24:26 +00:00
export function DailyPercentChart(props: {
startDate: number
dailyPercent: number[]
small?: boolean
}) {
const { dailyPercent, startDate, small } = props
const { width } = useWindowSize()
const dates = dailyPercent.map((_, i) =>
dayjs(startDate).add(i, 'day').toDate()
)
const points = zip(dates, dailyPercent).map(([date, betCount]) => ({
2022-03-22 21:24:26 +00:00
x: date,
y: betCount,
}))
const data = [{ id: 'Percent', data: points, color: '#11b981' }]
const bottomAxisTicks = width && width < 600 ? 6 : undefined
2022-03-22 21:24:26 +00:00
return (
<div
className="w-full overflow-hidden"
2022-03-22 21:24:26 +00:00
style={{ height: !small && (!width || width >= 800) ? 400 : 250 }}
>
<ResponsiveLine
data={data}
yScale={{ type: 'linear', stacked: false }}
xScale={{
type: 'time',
}}
axisLeft={{
format: (value) => `${value}%`,
}}
axisBottom={{
tickValues: bottomAxisTicks,
2022-03-22 21:24:26 +00:00
format: (date) => dayjs(date).format('MMM DD'),
}}
colors={{ datum: 'color' }}
pointSize={0}
pointBorderWidth={1}
pointBorderColor="#fff"
enableSlices="x"
enableGridX={!!width && width >= 800}
enableArea
margin={{ top: 20, right: 28, bottom: 22, left: 40 }}
2022-05-19 20:14:13 +00:00
sliceTooltip={({ slice }) => {
const point = slice.points[0]
return <Tooltip point={point} />
}}
2022-03-22 21:24:26 +00:00
/>
</div>
)
}
2022-05-19 20:14:13 +00:00
function Tooltip(props: { point: Point }) {
const { point } = props
return (
<Col
className="border bg-white py-1 px-3 "
style={{
border: '1px solid #ccc',
}}
>
<div
key={point.id}
style={{
color: point.serieColor,
padding: '3px 0',
}}
>
<strong>{point.serieId}</strong> {point.data.yFormatted}
</div>
<div>{dayjs(point.data.x).format('MMM DD')}</div>
</Col>
)
}