|
| 1 | +<script lang="ts"> |
| 2 | + import { BarChart } from 'layerchart'; |
| 3 | + import '../styles/metrics-stacked-bar-chart.css'; |
| 4 | +
|
| 5 | + type StackedDatum = Record<string, string | number>; |
| 6 | + type StackedSeries = { key: string; label: string; color?: string }; |
| 7 | +
|
| 8 | + let { |
| 9 | + data, |
| 10 | + series, |
| 11 | + x = 'key', |
| 12 | + bandPadding = 0.2, |
| 13 | + labelType = 'string', |
| 14 | + minLabelWidth = 90, |
| 15 | + barWidth = 6, |
| 16 | + showLegend = true |
| 17 | + }: { |
| 18 | + data: StackedDatum[]; |
| 19 | + series: StackedSeries[]; |
| 20 | + x?: string; |
| 21 | + bandPadding?: number; |
| 22 | + labelType?: 'string' | 'emoji'; |
| 23 | + minLabelWidth?: number; |
| 24 | + barWidth?: number; |
| 25 | + showLegend?: boolean; |
| 26 | + } = $props(); |
| 27 | +
|
| 28 | + let containerWidth = $state(0); |
| 29 | +
|
| 30 | + const labelClass = $derived(labelType === 'emoji' ? 'emoji-tick-label' : 'text-tick-label'); |
| 31 | +
|
| 32 | + const tickCount = $derived.by(() => { |
| 33 | + if (containerWidth <= 0) { |
| 34 | + return 4; |
| 35 | + } |
| 36 | + return Math.max(2, Math.floor(containerWidth / minLabelWidth)); |
| 37 | + }); |
| 38 | +
|
| 39 | + const chartWidth = $derived.by(() => { |
| 40 | + if (data.length === 0) { |
| 41 | + return containerWidth; |
| 42 | + } |
| 43 | + return Math.max(containerWidth, data.length * barWidth); |
| 44 | + }); |
| 45 | +
|
| 46 | + const axisProps = $derived.by(() => ({ |
| 47 | + xAxis: { |
| 48 | + ticks: tickCount, |
| 49 | + tickLabelProps: { |
| 50 | + class: labelClass |
| 51 | + } |
| 52 | + }, |
| 53 | + yAxis: { |
| 54 | + ticks: 5 |
| 55 | + } |
| 56 | + })); |
| 57 | +
|
| 58 | + const maxStackValue = $derived.by(() => { |
| 59 | + if (data.length === 0 || series.length === 0) { |
| 60 | + return 0; |
| 61 | + } |
| 62 | + return data.reduce((maxValue, datum) => { |
| 63 | + const total = series.reduce((sum, item) => { |
| 64 | + const value = Number(datum[item.key] ?? 0); |
| 65 | + return sum + (Number.isFinite(value) ? value : 0); |
| 66 | + }, 0); |
| 67 | + return Math.max(maxValue, total); |
| 68 | + }, 0); |
| 69 | + }); |
| 70 | +
|
| 71 | + const chartPadding = $derived.by(() => { |
| 72 | + const label = Math.round(maxStackValue).toLocaleString(); |
| 73 | + const estimatedLabelWidth = Math.max(48, Math.min(140, 12 + label.length * 8)); |
| 74 | + return { |
| 75 | + top: 20, |
| 76 | + right: 12, |
| 77 | + bottom: 24, |
| 78 | + left: estimatedLabelWidth |
| 79 | + }; |
| 80 | + }); |
| 81 | +</script> |
| 82 | + |
| 83 | +{#if data.length > 0 && series.length > 0} |
| 84 | + <div class="metrics-stacked-bar-chart__container"> |
| 85 | + <div class="metrics-stacked-bar-chart__scroll" bind:clientWidth={containerWidth}> |
| 86 | + <article class="metrics-stacked-bar-chart" style={`width: ${chartWidth}px;`}> |
| 87 | + <BarChart |
| 88 | + {data} |
| 89 | + {series} |
| 90 | + {bandPadding} |
| 91 | + {x} |
| 92 | + seriesLayout="stack" |
| 93 | + padding={chartPadding} |
| 94 | + props={{ |
| 95 | + bars: { |
| 96 | + radius: 0 |
| 97 | + }, |
| 98 | + ...axisProps, |
| 99 | + tooltip: { |
| 100 | + root: { |
| 101 | + classes: { root: 'layerchart-tooltip' } |
| 102 | + } |
| 103 | + } |
| 104 | + }} |
| 105 | + /> |
| 106 | + </article> |
| 107 | + </div> |
| 108 | + {#if showLegend} |
| 109 | + <ul class="metrics-stacked-bar-chart__legend" aria-label="Chart legend"> |
| 110 | + {#each series as item} |
| 111 | + <li class="metrics-stacked-bar-chart__legend-item"> |
| 112 | + <span |
| 113 | + class="metrics-stacked-bar-chart__legend-swatch" |
| 114 | + style={`--legend-color: ${item.color ?? '#9aa0a6'}`} |
| 115 | + ></span> |
| 116 | + <span class="metrics-stacked-bar-chart__legend-label">{item.label}</span> |
| 117 | + </li> |
| 118 | + {/each} |
| 119 | + </ul> |
| 120 | + {/if} |
| 121 | + </div> |
| 122 | +{/if} |
0 commit comments