|
1 | 1 | <script lang="ts"> |
2 | | - import { scaleOrdinal, scaleTime } from 'd3-scale'; |
3 | | - import { range } from 'd3-array'; |
4 | | - import { PeriodType, State, cls, format } from 'svelte-ux'; |
| 2 | + import { scaleBand, scaleOrdinal, scaleTime } from 'd3-scale'; |
| 3 | + import { bin, range, sum } from 'd3-array'; |
| 4 | + import { timeMonths } from 'd3-time'; |
| 5 | + import { PeriodType, State, cls, format, formatDate } from 'svelte-ux'; |
5 | 6 | import { subDays } from 'date-fns'; |
6 | 7 | import { mdiChevronRight } from '@mdi/js'; |
7 | 8 |
|
|
19 | 20 | Rule, |
20 | 21 | Tooltip, |
21 | 22 | Svg, |
| 23 | + Bars, |
22 | 24 | } from 'layerchart'; |
23 | 25 |
|
24 | 26 | import Preview from '$lib/docs/Preview.svelte'; |
|
39 | 41 | const randomData = range(200).map((d) => { |
40 | 42 | return { x: d, y: Math.random() }; |
41 | 43 | }); |
| 44 | +
|
| 45 | + $: binByTime = bin() |
| 46 | + .thresholds((_data, min, max) => timeMonths(min, max)) |
| 47 | + .value((d) => d.date); |
| 48 | + $: appleStockBinByMonth = binByTime(data.appleStock); |
| 49 | + // Simplify data for simplier brush example |
| 50 | + $: appleStockByMonth = appleStockBinByMonth.map((monthBin) => { |
| 51 | + return { |
| 52 | + date: monthBin.x0, |
| 53 | + value: sum(monthBin, (d) => d.value), |
| 54 | + }; |
| 55 | + }); |
42 | 56 | </script> |
43 | 57 |
|
44 | 58 | <h1>Examples</h1> |
45 | 59 |
|
| 60 | +<h2>Bar (scaleBand) support</h2> |
| 61 | + |
| 62 | +<Preview data={appleStockByMonth}> |
| 63 | + <div class="border rounded p-4 grid gap-1"> |
| 64 | + <State initial={[null, null]} let:value={xDomain} let:set> |
| 65 | + <div class="h-[300px]"> |
| 66 | + <Chart |
| 67 | + _data={appleStockByMonth} |
| 68 | + data={appleStockByMonth.filter( |
| 69 | + (d) => |
| 70 | + (xDomain[0] == null || d.date >= xDomain[0]) && |
| 71 | + (xDomain[1] == null || d.date <= xDomain[1]) |
| 72 | + )} |
| 73 | + x="date" |
| 74 | + xScale={scaleBand().padding(0.2)} |
| 75 | + y="value" |
| 76 | + yDomain={[0, null]} |
| 77 | + yNice |
| 78 | + padding={{ left: 24, bottom: 24 }} |
| 79 | + > |
| 80 | + <Svg> |
| 81 | + <Axis placement="left" grid rule /> |
| 82 | + <Axis |
| 83 | + placement="bottom" |
| 84 | + rule |
| 85 | + ticks={(scale) => scaleTime(scale.domain(), scale.range()).ticks(4)} |
| 86 | + format={(value) => formatDate(value, PeriodType.Custom, { custom: 'MMM yyyy' })} |
| 87 | + /> |
| 88 | + <ChartClipPath> |
| 89 | + <Bars radius={4} strokeWidth={1} class="fill-primary" /> |
| 90 | + </ChartClipPath> |
| 91 | + |
| 92 | + <Brush |
| 93 | + axis="x" |
| 94 | + resetOnEnd |
| 95 | + on:brushEnd={(e) => { |
| 96 | + console.log(e.detail); |
| 97 | + set(e.detail.xDomain); |
| 98 | + }} |
| 99 | + /> |
| 100 | + </Svg> |
| 101 | + </Chart> |
| 102 | + </div> |
| 103 | + </State> |
| 104 | + </div> |
| 105 | +</Preview> |
| 106 | + |
46 | 107 | <h2>Styling via classes</h2> |
47 | 108 |
|
48 | 109 | <Preview data={data.appleStock}> |
|
0 commit comments