From 66305bb129df5bbf7b1ecf5e80ba1d3c7425df4a Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Mon, 26 Sep 2022 16:45:09 -0700 Subject: [PATCH] Tidying, don't gratuitously use d3.format --- web/components/charts/generic-charts.tsx | 43 ++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/web/components/charts/generic-charts.tsx b/web/components/charts/generic-charts.tsx index d16de1cb..573f1b0b 100644 --- a/web/components/charts/generic-charts.tsx +++ b/web/components/charts/generic-charts.tsx @@ -5,7 +5,6 @@ import { bisectCenter, curveLinear, curveStepAfter, - format, pointer, stack, ScaleTime, @@ -23,6 +22,10 @@ export type MultiPoint = readonly [Date, number[]] // [time, [ordered outcome pr export type HistoryPoint = readonly [Date, number] // [time, number or percentage] export type NumericPoint = readonly [number, number] // [number, prob] +const formatPct = (n: number, digits?: number) => { + return `${(n * 100).toFixed(digits ?? 0)}%` +} + const formatDate = ( date: Date, opts: { includeYear: boolean; includeHour: boolean; includeMinute: boolean } @@ -73,12 +76,14 @@ export const SingleValueDistributionChart = (props: { }) => { const { color, data, xScale, yScale, w, h } = props const tooltipRef = useRef(null) + const px = useCallback((p: NumericPoint) => xScale(p[0]), [xScale]) const py0 = yScale(0) const py1 = useCallback((p: NumericPoint) => yScale(p[1]), [yScale]) const formatX = (n: number) => formatLargeNumber(n) - const formatY = (n: number) => format(',.2%')(n) + const formatY = (n: number) => formatPct(n, 2) + const xAxis = axisBottom(xScale).tickFormat(formatX) const yAxis = axisLeft(yScale).tickFormat(formatY) @@ -115,6 +120,7 @@ export const MultiValueHistoryChart = (props: { }) => { const { colors, data, xScale, yScale, labels, w, h, pct } = props const tooltipRef = useRef(null) + const px = useCallback( (p: SeriesPoint) => xScale(p.data[0]), [xScale] @@ -127,19 +133,21 @@ export const MultiValueHistoryChart = (props: { (p: SeriesPoint) => yScale(p[1]), [yScale] ) - const d3Stack = stack() - .keys(range(0, labels.length)) - .value(([_date, probs], o) => probs[o]) const [xStart, xEnd] = xScale.domain() const fmtX = getFormatterForDateRange(xStart, xEnd) - const fmtY = (n: number) => (pct ? format('.0%')(n) : formatLargeNumber(n)) + const fmtY = (n: number) => (pct ? formatPct(n, 0) : formatLargeNumber(n)) const [min, max] = yScale.domain() const tickValues = getTickValues(min, max, h < 200 ? 3 : 5) + const xAxis = axisBottom(xScale).tickFormat(fmtX) const yAxis = axisLeft(yScale).tickValues(tickValues).tickFormat(fmtY) + const d3Stack = stack() + .keys(range(0, labels.length)) + .value(([_date, probs], o) => probs[o]) + return (
{d3Stack(data).map((s, i) => ( - - - + ))}
@@ -176,6 +183,7 @@ export const SingleValueHistoryChart = (props: { }) => { const { color, data, xScale, yScale, pct, w, h } = props const tooltipRef = useRef(null) + const px = useCallback((p: HistoryPoint) => xScale(p[0]), [xScale]) const py0 = yScale(0) const py1 = useCallback((p: HistoryPoint) => yScale(p[1]), [yScale]) @@ -187,10 +195,11 @@ export const SingleValueHistoryChart = (props: { const includeMinute = endDate.diff(startDate, 'hours') < 2 const formatX = (d: Date) => formatDate(d, { includeYear, includeHour, includeMinute }) - const formatY = (n: number) => (pct ? format('.0%')(n) : formatLargeNumber(n)) + const formatY = (n: number) => (pct ? formatPct(n, 0) : formatLargeNumber(n)) const [min, max] = yScale.domain() const tickValues = getTickValues(min, max, h < 200 ? 3 : 5) + const xAxis = axisBottom(xScale).tickFormat(formatX) const yAxis = axisLeft(yScale) .tickValues(tickValues)