-
Notifications
You must be signed in to change notification settings - Fork 28
Scott/chart styles #400
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
base: main
Are you sure you want to change the base?
Scott/chart styles #400
Changes from all commits
8eaf7c8
430cc80
14ede7a
2b9786e
2f2af7d
ea9fc07
3ecce29
f7150cd
b713378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| .tooltip { | ||
| @include type-label-large; | ||
|
|
||
| padding: 10px 12px; | ||
| color: rgb(var(--color-card-content)); | ||
| background-color: rgb(var(--color-card-surface)); | ||
| border-radius: 8px; | ||
| box-shadow: var(--shadow-elevated); | ||
|
|
||
| p { | ||
| margin: 0; | ||
| } | ||
|
|
||
| .label { | ||
| @include type-label-small; | ||
|
|
||
| margin-bottom: 2px; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import styles from './custom_tooltip.module.scss'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've been going with the principle of category-first naming: |
||
|
|
||
| const tooltipFormatter = new Intl.NumberFormat(undefined, { | ||
| maximumFractionDigits: 2, | ||
| notation: 'standard', | ||
| }); | ||
|
|
||
| export const CustomTooltip = ({ | ||
| active, | ||
| payload, | ||
| label, | ||
| }: { | ||
| active?: boolean; | ||
| payload?: { value: number }[]; | ||
| label?: string; | ||
| }) => { | ||
| if (active && payload && payload.length) { | ||
| const value = tooltipFormatter.format(Number(payload?.[0]?.value)); | ||
| return ( | ||
| <div className={styles.tooltip}> | ||
| <p className={styles.label}>{`${label}`}</p> | ||
| <p className={styles.value}>{`${value}`}</p> | ||
| </div> | ||
| ); | ||
| } | ||
| return null; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||||||
| 'use client'; | ||||||||||||||
|
|
||||||||||||||
| import { COLORS } from '@package/tokens/ts'; | ||||||||||||||
| import { | ||||||||||||||
| Bar, | ||||||||||||||
| BarChart, | ||||||||||||||
| CartesianGrid, | ||||||||||||||
| ResponsiveContainer, | ||||||||||||||
| Tooltip, | ||||||||||||||
| XAxis, | ||||||||||||||
| YAxis, | ||||||||||||||
| } from 'recharts'; | ||||||||||||||
|
|
||||||||||||||
| import type { ChartDatum } from './chart'; | ||||||||||||||
| import { CustomTooltip } from './custom_tooltip'; | ||||||||||||||
|
|
||||||||||||||
| const BAR_COLOR = `rgb(${COLORS['card-surface-selected']})`; | ||||||||||||||
| const GRID_COLOR = `rgb(${COLORS['card-chart-grid']})`; | ||||||||||||||
| const AXIS_COLOR = `rgb(${COLORS['card-content-muted']})`; | ||||||||||||||
|
Comment on lines
+17
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reckon all chart colours should probably just be directly tokenized for ease. I.e:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| interface ChartProps { | ||||||||||||||
| data: ChartDatum[]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const compactFormatter = new Intl.NumberFormat(undefined, { | ||||||||||||||
| notation: 'compact', | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| export const DataChartBarHorizontal = ({ data }: ChartProps) => { | ||||||||||||||
| return ( | ||||||||||||||
| <ResponsiveContainer | ||||||||||||||
| width="100%" | ||||||||||||||
| aspect={0.75} | ||||||||||||||
| style={{ paddingBottom: '28px' }} | ||||||||||||||
| > | ||||||||||||||
| <BarChart | ||||||||||||||
| data={data} | ||||||||||||||
| layout="vertical" | ||||||||||||||
| margin={{ top: 32, right: 12, bottom: 0, left: 0 }} | ||||||||||||||
| > | ||||||||||||||
| <CartesianGrid stroke={GRID_COLOR} horizontal={false} /> | ||||||||||||||
| <XAxis | ||||||||||||||
| type="number" | ||||||||||||||
| tickLine={{ stroke: AXIS_COLOR }} | ||||||||||||||
| axisLine={{ stroke: AXIS_COLOR }} | ||||||||||||||
| tick={{ fontSize: 10, fill: AXIS_COLOR }} | ||||||||||||||
| tickFormatter={(value) => compactFormatter.format(value)} | ||||||||||||||
| tickMargin={6} | ||||||||||||||
| /> | ||||||||||||||
| <YAxis | ||||||||||||||
| type="category" | ||||||||||||||
| dataKey="date" | ||||||||||||||
| width={'auto'} | ||||||||||||||
| tickLine={false} | ||||||||||||||
| axisLine={{ stroke: AXIS_COLOR }} | ||||||||||||||
| tick={{ fontSize: 10, fill: AXIS_COLOR }} | ||||||||||||||
| /> | ||||||||||||||
| <Tooltip | ||||||||||||||
| cursor={{ fill: GRID_COLOR, opacity: 0.4 }} | ||||||||||||||
| content={<CustomTooltip />} | ||||||||||||||
| /> | ||||||||||||||
| <Bar dataKey="value" fill={BAR_COLOR} radius={[0, 2, 2, 0]} /> | ||||||||||||||
| </BarChart> | ||||||||||||||
| </ResponsiveContainer> | ||||||||||||||
| ); | ||||||||||||||
| }; | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| 'use client'; | ||
|
|
||
| import { COLORS } from '@package/tokens/ts'; | ||
| import { | ||
| Bar, | ||
| BarChart, | ||
| CartesianGrid, | ||
| ResponsiveContainer, | ||
| Tooltip, | ||
| usePlotArea, | ||
| XAxis, | ||
| YAxis, | ||
| } from 'recharts'; | ||
|
|
||
| import type { ChartDatum } from './chart'; | ||
| import { CustomTooltip } from './custom_tooltip'; | ||
|
|
||
| const BAR_COLOR = `rgb(${COLORS['card-surface-selected']})`; | ||
| const GRID_COLOR = `rgb(${COLORS['card-chart-grid']})`; | ||
| const AXIS_COLOR = `rgb(${COLORS['card-content-muted']})`; | ||
|
|
||
| interface ChartProps { | ||
| data: ChartDatum[]; | ||
| } | ||
|
|
||
| const compactFormatter = new Intl.NumberFormat(undefined, { | ||
| notation: 'compact', | ||
| }); | ||
|
|
||
| const CustomCursor = (props: { | ||
| x?: number; | ||
| y?: number; | ||
| width?: number; | ||
| height?: number; | ||
| top?: number; | ||
| }) => { | ||
| const { x = 0, width = 0, height = 0, top = 0 } = props; | ||
| return ( | ||
| <rect | ||
| x={x} | ||
| y={0} | ||
| width={width} | ||
| height={height + top} | ||
| fill={GRID_COLOR} | ||
| opacity={0.4} | ||
| /> | ||
| ); | ||
| }; | ||
|
sagrimson marked this conversation as resolved.
|
||
|
|
||
| const FullWidthAxisLine = () => { | ||
| const plotArea = usePlotArea(); | ||
| if (!plotArea) return null; | ||
| const y = plotArea.y + plotArea.height; | ||
| return ( | ||
| <line | ||
| x1={0} | ||
| y1={y} | ||
| x2={plotArea.x + plotArea.width} | ||
| y2={y} | ||
| stroke={AXIS_COLOR} | ||
| strokeWidth={1} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export const DataChartBarVertical = ({ data }: ChartProps) => { | ||
| return ( | ||
| <ResponsiveContainer | ||
| width="100%" | ||
| aspect={1.78} | ||
| style={{ paddingBottom: '28px' }} | ||
| > | ||
| <BarChart | ||
| data={data} | ||
| margin={{ top: 32, right: 0, bottom: 0, left: 0 }} | ||
| style={{ overflow: 'hidden' }} | ||
| > | ||
| <CartesianGrid | ||
| stroke={GRID_COLOR} | ||
| vertical={false} | ||
| x={0} | ||
| width={9999} | ||
| /> | ||
| <XAxis | ||
| dataKey="date" | ||
| tickLine={{ stroke: AXIS_COLOR }} | ||
| axisLine={false} | ||
| tick={{ fontSize: 10, fill: AXIS_COLOR }} | ||
| tickMargin={6} | ||
| padding={{ left: 0, right: 4 }} | ||
| /> | ||
| <YAxis | ||
| width="auto" | ||
| tickLine={false} | ||
| axisLine={false} | ||
| tick={{ fontSize: 10, dy: -7, textAnchor: 'end' }} | ||
| tickFormatter={(value) => compactFormatter.format(Number(value))} | ||
| /> | ||
| <FullWidthAxisLine /> | ||
| <Tooltip cursor={<CustomCursor />} content={<CustomTooltip />} /> | ||
| <Bar dataKey="value" fill={BAR_COLOR} radius={[2, 2, 0, 0]} /> | ||
| </BarChart> | ||
| </ResponsiveContainer> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.