Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format number in compact forms #2634

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/explore/ClientVolumeOverTimeGraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ReleaseVersionMarkers from '../ReleaseVersionMarkers.svelte';

import { totalClientsGraph, tween } from '../../utils/constants';
import { formatMillion } from '../../utils/formatters';
import { formatCompact } from '../../utils/formatters';

import ReferenceSymbol from '../ReferenceSymbol.svelte';
import TrackingLine from './TrackingLine.svelte';
Expand Down Expand Up @@ -64,7 +64,7 @@
side="left"
lineStyle="short"
ticks={yScale.ticks(4)}
tickFormatter={formatMillion}
tickFormatter={formatCompact}
/>
{#if aggregationLevel === 'build_id'}
<Axis side="bottom" />
Expand Down
4 changes: 2 additions & 2 deletions src/components/explore/CompareClientVolumeGraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Tweenable from '../Tweenable.svelte';
import ChartTitle from './ChartTitle.svelte';
import { compareClientCountsGraph, tween } from '../../utils/constants';
import { formatMillion } from '../../utils/formatters';
import { formatCompact } from '../../utils/formatters';

export let description;
export let leftAudienceValue;
Expand Down Expand Up @@ -75,7 +75,7 @@
/>
<Axis
side="right"
tickFormatter={formatMillion}
tickFormatter={formatCompact}
ticks={yScale.ticks(4)}
/>
</g>
Expand Down
4 changes: 2 additions & 2 deletions src/components/explore/CompareSampleCountGraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Tweenable from '../Tweenable.svelte';
import ChartTitle from './ChartTitle.svelte';
import { compareClientCountsGraph, tween } from '../../utils/constants';
import { formatMillion } from '../../utils/formatters';
import { formatCompact } from '../../utils/formatters';

export let description;
export let leftSampleValue;
Expand Down Expand Up @@ -75,7 +75,7 @@
/>
<Axis
side="right"
tickFormatter={formatMillion}
tickFormatter={formatCompact}
ticks={yScale.ticks(4)}
/>
</g>
Expand Down
3 changes: 1 addition & 2 deletions src/components/explore/DistributionChart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { scaleLinear } from 'd3-scale';
import { tooltip as tooltipAction } from '@graph-paper/core/actions';
import { distributionComparisonGraph } from '../../utils/constants';
import { formatCompact } from '../../utils/formatters';

export let innerHeight;
export let innerWidth;
Expand All @@ -25,8 +26,6 @@
style: 'percent',
maximumFractionDigits: 2,
}).format(t);
let formatCompact = (t) =>
Intl.NumberFormat('en', { notation: 'compact' }).format(t);

$: y = scaleLinear().domain([0, topTick]).range([minHeight, maxHeight]);

Expand Down
4 changes: 2 additions & 2 deletions src/components/explore/ProbeExplorer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import {
formatBuildIDToDateString,
formatMillion,
formatCompact,
formatFromNanoseconds,
} from '../../utils/formatters';

Expand Down Expand Up @@ -70,7 +70,7 @@
export let yScaleType;
export let yDomain;
export let densityMetricType;
export let yTickFormatter = formatMillion;
export let yTickFormatter = formatCompact;
if (data[0].metric_type === 'timing_distribution') {
yTickFormatter = formatFromNanoseconds;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/explore/SampleCountOverTimeGraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ReleaseVersionMarkers from '../ReleaseVersionMarkers.svelte';

import { totalClientsGraph, tween } from '../../utils/constants';
import { formatMillion } from '../../utils/formatters';
import { formatCompact } from '../../utils/formatters';

import ReferenceSymbol from '../ReferenceSymbol.svelte';
import TrackingLine from './TrackingLine.svelte';
Expand Down Expand Up @@ -64,7 +64,7 @@
side="left"
lineStyle="short"
ticks={yScale.ticks(4)}
tickFormatter={formatMillion}
tickFormatter={formatCompact}
/>
{#if aggregationLevel === 'build_id'}
<Axis side="bottom" />
Expand Down
9 changes: 2 additions & 7 deletions src/utils/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ export const formatParenPercent = (fmt, v, pad = 0) => {
return `${p}${f}`;
};

export const formatMillion = (num) =>
// format a number as '1mil' if a million or more
Math.abs(num) > 999999
? `${format(',d')(
Math.sign(num) * (Math.abs(num) / 1000000).toFixed(1)
)}mil`
: format(',d')(Math.sign(num) * Math.abs(num));
export const formatCompact = (t) =>
Intl.NumberFormat('en', { notation: 'compact' }).format(t);

export const formatBuildIDToDateString = (b) => timeFormat('%Y-%m-%d %H')(b);
export const ymd = timeFormat('%Y-%m-%d');
Expand Down