Skip to content

Commit

Permalink
Use better formatters for chart value labels
Browse files Browse the repository at this point in the history
  • Loading branch information
mondoreale committed May 2, 2024
1 parent 7baad21 commit e7ed55b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/components/Graphs/TimeSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ const formatDate = (milliseconds: number, dateDisplay: DateDisplay = 'day') => {
return `${monthName} ${date.getDate()}`
}

function defaultLabelFormat(x: number): string {
return `${x}`
}

const UnstyledTimeSeriesGraph = ({
graphData,
showCrosshair,
dateDisplay,
height,
ratio,
labelFormat,
labelFormat = defaultLabelFormat,
...props
}: Props) => {
const [tooltipWidth, setTooltipWidth] = useState(0)
Expand Down Expand Up @@ -142,10 +146,8 @@ const UnstyledTimeSeriesGraph = ({
if (data.payload != null && data.payload[0] != null) {
return (
<CrosshairValue ref={tooltipRef}>
{typeof labelFormat === 'function'
? labelFormat(data.payload[0].payload.y)
: data.payload[0].payload.y}{' '}
({formatDate(data.payload[0].payload.x, dateDisplay)})
{labelFormat(data.payload[0].payload.y)} (
{formatDate(data.payload[0].payload.x, dateDisplay)})
</CrosshairValue>
)
}
Expand Down
26 changes: 22 additions & 4 deletions src/components/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ function NetworkStatsGraph({ metricKey }: NetworkStatsGraphProps) {
ratio="1:2"
showCrosshair
dateDisplay={['realtime', '24hours'].includes(interval) ? 'hour' : 'day'}
labelFormat={(value) => value.toPrecision(4)}
labelFormat={(value) => networkMetricValueFormatter(metricKey, value)}
/>
<Intervals
options={['realtime', '24hours', '1month', '3months', 'all']}
Expand All @@ -380,6 +380,18 @@ function NetworkStatsGraph({ metricKey }: NetworkStatsGraphProps) {
)
}

function networkMetricValueFormatter(metricKey: NetworkMetricKey, value: number): string {
if (metricKey === 'apy') {
return `${value.toFixed(2)}%`
}

if (metricKey === 'tvl') {
return value < 10 ** 6 ? value.toPrecision(4) : `${(value / 10 ** 6).toPrecision(4)}M`
}

return value.toPrecision(4)
}

interface NodeStatsProps {
nodeId: string
}
Expand Down Expand Up @@ -445,9 +457,7 @@ export function NodeStats({ nodeId }: NodeStatsProps) {
ratio="1:2"
showCrosshair
dateDisplay={['realtime', '24hours'].includes(interval) ? 'hour' : 'day'}
labelFormat={(value) => {
return (/bytes/i.test(metricKey) ? value / 1024 / 1024 : value).toPrecision(4)
}}
labelFormat={(value) => nodeMetricValueFormatter(metricKey, value)}
/>
<Intervals
options={['realtime', '24hours', '1month', '3months', 'all']}
Expand All @@ -457,3 +467,11 @@ export function NodeStats({ nodeId }: NodeStatsProps) {
</>
)
}

function nodeMetricValueFormatter(metricKey: NodeMetricKey, value: number): string {
if (/bytes/i.test(metricKey)) {
return (value / 1024 / 1024).toPrecision(4)
}

return value.toPrecision(4)
}

0 comments on commit e7ed55b

Please sign in to comment.