Skip to content

Commit

Permalink
[Axis] Fix ticks defined as function. Useful to only show first/las…
Browse files Browse the repository at this point in the history
…t values or filter (ex. integers only). Issue #119
  • Loading branch information
techniq committed Mar 22, 2024
1 parent 524cbc4 commit c78f17a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-apricots-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"layerchart": patch
---

[Axis] Fix `ticks` defined as function. Useful to only show first/last values or filter (ex. integers only)
8 changes: 5 additions & 3 deletions packages/layerchart/src/lib/components/Axis.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
$: tickVals = Array.isArray(ticks)
? ticks
: isScaleBand(scale)
? scale.domain()
: scale.ticks(typeof ticks === 'function' ? ticks(scale) : ticks);
: typeof ticks === 'function'
? ticks(scale)
: isScaleBand(scale)
? scale.domain()
: scale.ticks(ticks);
function getCoords(tick: any) {
switch (placement) {
Expand Down
109 changes: 109 additions & 0 deletions packages/layerchart/src/routes/docs/components/Axis/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,112 @@
</Chart>
</div>
</Preview>

<h2>Only first/last ticks</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<Chart
{data}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, 100]}
yNice
padding={{ top: 20, bottom: 20, left: 20, right: 20 }}
>
<Svg>
<Axis placement="bottom" ticks={(scale) => scale.domain()} />
<Axis placement="left" ticks={(scale) => scale.domain()} />
</Svg>
</Chart>
</div>
</Preview>

<h2>Integer only ticks</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<Chart
{data}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, 2]}
yNice
padding={{ top: 20, bottom: 20, left: 20, right: 20 }}
>
<Svg>
<Axis placement="bottom" />
<Axis
placement="left"
ticks={(scale) => scale.ticks().filter(Number.isInteger)}
format="integer"
/>
</Svg>
</Chart>
</div>
</Preview>

<h2>Explicit ticks</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<Chart
{data}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, 100]}
yNice
padding={{ top: 20, bottom: 20, left: 20, right: 20 }}
>
<Svg>
<Axis placement="bottom" />
<Axis placement="left" ticks={[0, 50, 100]} />
</Svg>
</Chart>
</div>
</Preview>

<h2>Tick count</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<Chart
{data}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, 100]}
yNice
padding={{ top: 20, bottom: 20, left: 20, right: 20 }}
>
<Svg>
<Axis placement="bottom" />
<Axis placement="left" ticks={20} />
</Svg>
</Chart>
</div>
</Preview>

<h2>Remove default tick count</h2>

<Preview {data}>
<div class="h-[300px] p-4 border rounded">
<Chart
{data}
x="date"
xScale={scaleTime()}
y="value"
yDomain={[0, 100]}
yNice
padding={{ top: 20, bottom: 20, left: 20, right: 20 }}
>
<Svg>
<Axis placement="bottom" />
<Axis placement="left" ticks={undefined} />
</Svg>
</Chart>
</div>
</Preview>

0 comments on commit c78f17a

Please sign in to comment.