|
| 1 | +<script lang="ts"> |
| 2 | + import { scaleBand, scaleOrdinal } from 'd3-scale'; |
| 3 | + import { rollup } from 'd3-array'; |
| 4 | + import { quantize } from 'd3-interpolate'; |
| 5 | + import { |
| 6 | + interpolateInferno, |
| 7 | + interpolateRainbow, |
| 8 | + interpolateRdBu, |
| 9 | + interpolateSpectral, |
| 10 | + interpolateViridis, |
| 11 | + schemeSpectral, |
| 12 | + schemeTableau10, |
| 13 | + } from 'd3-scale-chromatic'; |
| 14 | +
|
| 15 | + import { |
| 16 | + PeriodType, |
| 17 | + NumberStepper, |
| 18 | + sort, |
| 19 | + format, |
| 20 | + timerStore, |
| 21 | + ButtonGroup, |
| 22 | + Button, |
| 23 | + Field, |
| 24 | + Switch, |
| 25 | + } from 'svelte-ux'; |
| 26 | +
|
| 27 | + import Chart, { Svg } from '$lib/components/Chart.svelte'; |
| 28 | + import Axis from '$lib/components/Axis.svelte'; |
| 29 | + import Bar from '$lib/components/Bar.svelte'; |
| 30 | + import Group from '$lib/components/Group.svelte'; |
| 31 | + import Highlight from '$lib/components/Highlight.svelte'; |
| 32 | + import Preview from '$lib/docs/Preview.svelte'; |
| 33 | + import Text from '$lib/components/Text.svelte'; |
| 34 | + import Tooltip from '$lib/components/Tooltip.svelte'; |
| 35 | + import TooltipItem from '$lib/components/TooltipItem.svelte'; |
| 36 | +
|
| 37 | + import Labels from '$lib/components/Labels.svelte'; |
| 38 | + import Rule from '$lib/components/Rule.svelte'; |
| 39 | + import ChartClipPath from '$lib/components/ChartClipPath.svelte'; |
| 40 | +
|
| 41 | + export let data; |
| 42 | +
|
| 43 | + const duration = 250; |
| 44 | + let xNice = false; |
| 45 | +
|
| 46 | + const frameTimer = timerStore({ |
| 47 | + initial: 0, |
| 48 | + onTick: (value) => { |
| 49 | + if (value == null || value >= data.keyframes.length - 1) { |
| 50 | + frameTimer.stop(); |
| 51 | + return value; |
| 52 | + } else { |
| 53 | + return value + 1; |
| 54 | + } |
| 55 | + }, |
| 56 | + delay: duration, |
| 57 | + disabled: true, |
| 58 | + }); |
| 59 | + $: ({ isRunning } = frameTimer); |
| 60 | +
|
| 61 | + $: keyframe = data.keyframes[$frameTimer]; |
| 62 | + $: chartData = sort(keyframe?.data ?? [], (d) => d.value, 'desc'); |
| 63 | +
|
| 64 | + const categoryByName = rollup( |
| 65 | + data.data, |
| 66 | + (values) => values[0].category, |
| 67 | + (d) => d.name |
| 68 | + ); |
| 69 | + // const colors = schemeTableau10; |
| 70 | + const colors = schemeSpectral[10]; |
| 71 | + // const colors = quantize(interpolateSpectral, 10); |
| 72 | + const colorScale = scaleOrdinal() |
| 73 | + .domain(Array.from(categoryByName.values()).sort()) |
| 74 | + .range(colors); |
| 75 | +
|
| 76 | + $: console.log({ data, keyframe, chartData }); |
| 77 | +</script> |
| 78 | + |
| 79 | +<h1>Examples</h1> |
| 80 | + |
| 81 | +<div class="grid grid-cols-[1fr,auto,auto] items-center mb-2"> |
| 82 | + <div class="flex items-center gap-3"> |
| 83 | + <ButtonGroup variant="fill-light" class="ml-3"> |
| 84 | + <Button on:click={frameTimer.start} disabled={$isRunning}>Start</Button> |
| 85 | + <Button on:click={frameTimer.stop} disabled={!$isRunning}>Stop</Button> |
| 86 | + </ButtonGroup> |
| 87 | + <Button on:click={frameTimer.reset}>Reset</Button> |
| 88 | + |
| 89 | + <NumberStepper |
| 90 | + value={$frameTimer} |
| 91 | + on:change={(e) => { |
| 92 | + $frameTimer = e.detail.value; |
| 93 | + }} |
| 94 | + /> |
| 95 | + |
| 96 | + <Field let:id> |
| 97 | + <label class="flex gap-2 items-center text-sm"> |
| 98 | + Nice |
| 99 | + <Switch bind:checked={xNice} {id} /> |
| 100 | + </label> |
| 101 | + </Field> |
| 102 | + </div> |
| 103 | + |
| 104 | + <div class="text-xl">{format(keyframe?.date, PeriodType.MonthYear)}</div> |
| 105 | +</div> |
| 106 | + |
| 107 | +<Preview data={chartData}> |
| 108 | + <div class="h-[500px] p-4 border rounded"> |
| 109 | + <Chart |
| 110 | + data={chartData} |
| 111 | + x="value" |
| 112 | + xDomain={[0, null]} |
| 113 | + {xNice} |
| 114 | + y="name" |
| 115 | + yScale={scaleBand().padding(0.1)} |
| 116 | + yDomain={chartData.map((d) => d.name)} |
| 117 | + padding={{ top: 14, left: 4, right: 24 }} |
| 118 | + tooltip={{ mode: 'band' }} |
| 119 | + > |
| 120 | + <Svg> |
| 121 | + <ChartClipPath _height={272}> |
| 122 | + <g> |
| 123 | + {#each chartData as d (d.name)} |
| 124 | + <Bar |
| 125 | + bar={d} |
| 126 | + radius={2} |
| 127 | + fill={colorScale(categoryByName.get(d.name))} |
| 128 | + fill-opacity={0.9} |
| 129 | + class="stroke-1 stroke-surface-content/50" |
| 130 | + tweened={{ duration }} |
| 131 | + /> |
| 132 | + {/each} |
| 133 | + </g> |
| 134 | + <!-- <Axis placement="left" rule tweened={{ duration }} /> --> |
| 135 | + <Rule x /> |
| 136 | + <Axis placement="top" grid rule tweened={{ duration }} /> |
| 137 | + <Highlight area /> |
| 138 | + <!-- <Labels tweened format="integer" placement="inside" /> --> |
| 139 | + <Labels tweened format="integer" placement="inside" let:data let:position let:textProps> |
| 140 | + <Group {...position} tweened={{ duration }}> |
| 141 | + <Text value={data.name} class="fill-black/50 mix-blend-multiply" {...textProps} /> |
| 142 | + </Group> |
| 143 | + </Labels> |
| 144 | + </ChartClipPath> |
| 145 | + </Svg> |
| 146 | + |
| 147 | + <Tooltip header={(d) => d.name} let:data> |
| 148 | + <TooltipItem label="value" value={data.value} format="integer" /> |
| 149 | + <TooltipItem label="category" value={categoryByName.get(data.name)} /> |
| 150 | + </Tooltip> |
| 151 | + </Chart> |
| 152 | + </div> |
| 153 | +</Preview> |
0 commit comments