Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { prepareInteractionBreakdownTable } from '../../lib/prepare-table'
import StatsTable from '../../components/StatsTable.astro'
import ViewSwitcher from '../../components/ViewSwitcher.astro'
import { INTERACTION_PHASES } from '../../lib/interaction-phases'
import { stackLegendSwatch } from '../../lib/chart-styles'
import InteractionChartCaption from './InteractionChartCaption.astro'
import StackedBarChart from '../../components/StackedBarChart.astro'

Expand Down Expand Up @@ -37,7 +38,11 @@ const chart1Table = prepareInteractionBreakdownTable(chart1)
defaultValue="chart"
>
<StackedBarChart slot="chart" {...chart1}>
<InteractionChartCaption slot="caption" />
<InteractionChartCaption
slot="caption"
swatchColor={(_, index, count) => stackLegendSwatch(index, count)}
outlined
/>
</StackedBarChart>
<StatsTable slot="table" {...chart1Table} />
</ViewSwitcher>
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
---
import { INTERACTION_PHASES } from '../../lib/interaction-phases'

type InteractionPhase = (typeof INTERACTION_PHASES)[number]

interface Props {
title?: string
swatchColor?: (
phase: InteractionPhase,
index: number,
count: number,
) => string
outlined?: boolean
}

const { title } = Astro.props
const {
title,
swatchColor = (phase: InteractionPhase) => phase.color,
outlined = false,
} = Astro.props
---

<figcaption>
Expand All @@ -25,10 +37,13 @@ const { title } = Astro.props
</ul>
<ul class="legend" aria-hidden="true">
{
INTERACTION_PHASES.map(({ label, color }) => (
INTERACTION_PHASES.map((phase, index) => (
<li>
<span class="swatch" style={`background: ${color}`} />
{label}
<span
class:list={['swatch', { outlined }]}
style={`background: ${swatchColor(phase, index, INTERACTION_PHASES.length)}`}
/>
{phase.label}
</li>
))
}
Expand Down Expand Up @@ -80,4 +95,8 @@ const { title } = Astro.props
height: 0.75rem;
border-radius: 0.2rem;
}

.swatch.outlined {
border: 1px solid var(--ft-muted);
}
</style>
29 changes: 26 additions & 3 deletions packages/docs/src/components/ComparisonBarChart.astro
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,16 @@ const chartPayload = JSON.stringify({
formatValue,
getFrameworkLogoSource,
} from '../lib/chart'
import { highlightedGray, primaryGray } from '../lib/chart-styles'
import {
BAR_FILL_OPACITY,
BAR_FOCUS_FILL_OPACITY,
BAR_STROKE_WIDTH,
primaryGray,
} from '../lib/chart-styles'
import {
getBrandByDisplayName,
resolveBrandColor,
} from '../lib/framework-logos'
import type { ChartDatum, ComparisonChartPayload } from '../lib/types'

type Payload = ComparisonChartPayload & {
Expand Down Expand Up @@ -254,6 +263,17 @@ const chartPayload = JSON.stringify({

const mutedColor = getCssVar('--ft-muted', '#697586')
const names = rows.map((d) => d.name)
const theme = document.documentElement.dataset.theme
const barColors = new Map(
rows.map((d) => {
const brand = getBrandByDisplayName(d.name)
return [
d.name,
brand ? resolveBrandColor(brand.color, theme) : primaryGray,
] as const
}),
)
const barColor = (d: ChartDatum) => barColors.get(d.name) ?? primaryGray
const logoLayer = renderLogoLayer(wrapper, names)

const definition = defineChart({
Expand Down Expand Up @@ -306,13 +326,16 @@ const chartPayload = JSON.stringify({
id: 'values',
x: 'value',
y: 'name',
fill: primaryGray,
fill: barColor,
fillOpacity: BAR_FILL_OPACITY,
stroke: barColor,
strokeWidth: BAR_STROKE_WIDTH,
radius: 8,
inset: SHARED_BAR_INSET,
states: [
{
when: { focus: 'primary' },
style: { fill: highlightedGray },
style: { fillOpacity: BAR_FOCUS_FILL_OPACITY },
transition: {
type: 'tween',
duration: 140,
Expand Down
54 changes: 36 additions & 18 deletions packages/docs/src/components/StackedBarChart.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { SHARED_CHART_HEIGHT, formatValue } from '../lib/chart'
import { stackLegendSwatch } from '../lib/chart-styles'
import type { ChartValueFormat } from '../lib/types'
interface Series {
key: string
Expand All @@ -24,9 +25,11 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
{
!Astro.slots.has('caption') && (
<ul class="legend" aria-label="Chart series">
{series.map((item) => (
{series.map((item, index) => (
<li>
<span style={`background: ${item.color}`} />
<span
style={`background: ${stackLegendSwatch(index, series.length)}`}
/>
{item.label}
</li>
))}
Expand Down Expand Up @@ -114,7 +117,7 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
width: 0.8rem;
height: 0.8rem;
border-radius: 2px;
border: 1px solid var(--ft-border);
border: 1px solid var(--ft-muted);
}
</style>
<script>
Expand All @@ -135,6 +138,11 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
formatValue,
getFrameworkLogoSource,
} from '../lib/chart'
import { BAR_STROKE_WIDTH, stackFillOpacity } from '../lib/chart-styles'
import {
getBrandByDisplayName,
resolveBrandColor,
} from '../lib/framework-logos'
import type { ChartValueFormat } from '../lib/types'
interface Segment {
name: string
Expand All @@ -143,6 +151,7 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
start: number
end: number
color: string
opacity: number
total: number
}
interface Payload {
Expand All @@ -161,21 +170,36 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
payload.series.reduce((sum, item) => sum + row.values[item.key]!, 0)
const rows = payload.data.sort((a, b) => totalFor(a) - totalFor(b))
if (!rows.length) return
const theme = document.documentElement.dataset.theme
const muted =
getComputedStyle(document.documentElement)
.getPropertyValue('--ft-muted')
.trim() || '#697586'
const textColor =
getComputedStyle(document.documentElement)
.getPropertyValue('--ft-text')
.trim() || '#1f2933'
const brandColor = (name: string) => {
const brand = getBrandByDisplayName(name)
return brand ? resolveBrandColor(brand.color, theme) : muted
}
const segments = rows.flatMap((row) => {
let offset = 0
const total = payload.series.reduce(
(sum, item) => sum + row.values[item.key]!,
0,
)
return payload.series.map((item) => {
const color = brandColor(row.name)
return payload.series.map((item, index) => {
const value = row.values[item.key]!
const segment = {
name: row.name,
label: item.label,
value,
start: offset,
end: offset + value,
color: item.color,
color,
opacity: stackFillOpacity(index, payload.series.length),
total,
}
offset += value
Expand All @@ -202,10 +226,6 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
return logo
}),
)
const muted =
getComputedStyle(document.documentElement)
.getPropertyValue('--ft-muted')
.trim() || '#697586'
const format = (value: number) =>
formatValue(value, payload.valueFormat, {
sigFigs: 2,
Expand All @@ -222,6 +242,9 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
x2: 'end',
y: 'name',
fill: d.color,
fillOpacity: d.opacity,
stroke: d.color,
strokeWidth: BAR_STROKE_WIDTH,
inset: SHARED_BAR_INSET,
radius: [
d.start === 0 ? 8 : 0,
Expand All @@ -237,14 +260,7 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
text: (d) => format(d.total),
anchor: (d) => (d.total / max > 0.85 ? 'end' : 'start'),
dx: (d) => (d.total / max > 0.85 ? -10 : 10),
fill: (d) =>
d.total / max > 0.85
? segments.findLast(
(segment) => segment.name === d.name && segment.value > 0,
)?.color === '#f7df1e'
? '#203329'
: '#fff'
: muted,
fill: (d) => (d.total / max > 0.85 ? textColor : muted),
fontWeight: 600,
}),
],
Expand Down Expand Up @@ -276,7 +292,9 @@ const { title, series, data, valueFormat = 'kb' } = Astro.props
.map((segment) => ({
label: segment.label,
value: format(segment.value),
color: segment.color,
color: `color-mix(in srgb, ${segment.color} ${Math.round(
segment.opacity * 100,
)}%, transparent)`,
})),
],
}),
Expand Down
15 changes: 15 additions & 0 deletions packages/docs/src/lib/chart-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ export function lineChartGrid() {
export const lineChartLineStyle = { strokeWidth: 3 }
export const lineChartPointStyle = { r: 4, strokeWidth: 3 }
export const lineChartLatestPointStyle = { ...lineChartPointStyle, r: 6 }

export const BAR_FILL_OPACITY = 0.3
export const BAR_FOCUS_FILL_OPACITY = 0.55
export const BAR_STROKE_WIDTH = 1

export function stackFillOpacity(index: number, count: number) {
const min = 0.15
const max = 0.75
return count <= 1
? BAR_FILL_OPACITY
: min + ((max - min) * index) / (count - 1)
}

export const stackLegendSwatch = (index: number, count: number) =>
`color-mix(in srgb, var(--ft-muted) ${Math.round(stackFillOpacity(index, count) * 100)}%, transparent)`
Loading