Skip to content

Commit b5f2f57

Browse files
Additional axis examples (#764)
* fix for empty layers toggle showing small circle * New barchart-xinterval-xinset and linechart-tickspacing - updates to Axis.md and BarChart.md text - updated Scatterplot domain-nice to use Switch instead of autotoggling * added needed padding * Add `tickSpacing` @default to jsdoc * refine docs --------- Co-authored-by: Sean Lynch <techniq35@gmail.com>
1 parent 2fe4011 commit b5f2f57

7 files changed

Lines changed: 94 additions & 6 deletions

File tree

docs/src/content/components/Axis.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ related: [Grid, Rule]
99

1010
:example{ name="placement-bottom-left" showCode }
1111

12+
## tickSpacing
13+
14+
If using a continuous scales (ex. linear, time) and ticks become too crowded, you can use `tickSpacing` to control the number of pixels alloted for each tick (higher => fewer ticks).
15+
16+
::note
17+
Default: `80` for horizontal axes (top/bottom/angle) and `50` for vertical axes (left/right/radius).
18+
::
19+
20+
:example{ name="linechart-tickspacing" showCode }
21+
22+
::tip
23+
See also: time scale [auto](/docs/components/Axis/time-scale-auto), [multiline](/docs/components/Axis/time-scale-auto-multiline), and [brush](/docs/components/Axis/time-scale-brush-multiline) examples
24+
::
25+
26+
## band scales
27+
28+
When creating time-series bar charts, it can be useful to use a time scale axis instead of a bar scale axis. This helps show gaps in data (such as on [weekends](/docs/components/BarChart/time-scale-interval)) and provides improved axis ticks.
29+
30+
To enable this, you must define the interval (daily, hourly, etc) of your data using [d3-time interval](https://d3js.org/d3-time#timeMillisecond), such as `xInterval={timeDay}`.
31+
32+
Since band padding is not available when not using a band scale, you can leverage `xInset={...}` to add padding between bars.
33+
34+
:example{ name="barchart-xinterval-xinset" showCode }
35+
1236
<!-- ## Examples
1337
1438
### Placement (bottom / left)

docs/src/content/components/BarChart.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ related: [Chart, Bars]
99

1010
:example{ name="vertical-default" showCode }
1111

12+
:::tip
13+
See also: [Axis](/docs/components/Axis) for examples of using time scale axes with bar charts
14+
:::
15+
1216
<!-- ## Examples
1317
1418
### Vertical (default)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<script lang="ts">
2+
import { BarChart } from 'layerchart';
3+
import { timeDay } from 'd3-time';
4+
import { createDateSeries } from '$lib/utils/data.js';
5+
import { Switch } from 'svelte-ux';
6+
7+
const data = $derived(createDateSeries({ count: 30, min: 50, max: 100, value: 'integer' }));
8+
let xInterval = $state(true);
9+
let xInset = $state(true);
10+
11+
export { data };
12+
</script>
13+
14+
<div class="flex justify-between pb-4 screenshot-hidden">
15+
<label class="flex gap-2">
16+
<Switch bind:checked={xInterval} />
17+
{xInterval ? 'Applying xInterval={timeDay}' : 'Not applying xInterval={timeDay}'}
18+
</label>
19+
<label class="flex gap-2">
20+
<Switch bind:checked={xInset} />
21+
{xInset ? 'Applying xInset' : 'Not applying xInset'}
22+
</label>
23+
</div>
24+
25+
<BarChart
26+
{data}
27+
x="date"
28+
y="value"
29+
props={{ xAxis: { tickSpacing: 200 }, bars: { insets: { x: xInset ? 4 : undefined } } }}
30+
xInterval={xInterval ? timeDay : undefined}
31+
height={300}
32+
/>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script lang="ts">
2+
import { LineChart } from 'layerchart';
3+
import { createDateSeries } from '$lib/utils/data.js';
4+
import { Switch } from 'svelte-ux';
5+
6+
const data = createDateSeries({ count: 30, min: 50, max: 100, value: 'integer' });
7+
8+
let tickSpacing = $state(true);
9+
10+
export { data };
11+
</script>
12+
13+
<label class="flex gap-2 pb-4 screenshot-hidden">
14+
<Switch bind:checked={tickSpacing} />
15+
{tickSpacing ? 'Applying tickSpacing' : 'Not applying tickSpacing'}
16+
</label>
17+
18+
<LineChart
19+
{data}
20+
x="date"
21+
y="value"
22+
props={{ xAxis: { tickSpacing: tickSpacing ? 200 : undefined } }}
23+
height={300}
24+
/>

docs/src/examples/components/Axis/time-scale-auto-multiline.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@
111111
{#each examples as example}
112112
<div>
113113
<div class="text-sm mb-1">{example.label}</div>
114-
<div class="h-[100px] p-4 border rounded-sm">
114+
<div class="border rounded-sm">
115115
<Chart
116116
xDomain={example.domain}
117-
padding={defaultChartPadding({ top: 30, bottom: 30, left: 25, right: 25 })}
117+
padding={defaultChartPadding({ top: 40, bottom: 40, left: 25, right: 25 })}
118+
height={100}
118119
>
119120
<Layer>
120121
<Axis placement="top" rule grid tickMultiline {tickSpacing} />
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<script lang="ts">
22
import { ScatterChart } from 'layerchart';
33
import { getSpiral } from '$lib/utils/data.js';
4+
import { Switch } from 'svelte-ux';
45
56
const data = getSpiral({ angle: 137.5, radius: 10, count: 100, width: 500, height: 500 });
67
78
let applyNice = $state(true);
8-
setInterval(() => {
9-
applyNice = !applyNice;
10-
}, 5000);
119
1210
export { data };
1311
</script>
1412

15-
{applyNice ? 'Applying Nice' : 'Not applying Nice'}
13+
<label class="flex gap-2 pb-4 screenshot-hidden">
14+
<Switch bind:checked={applyNice} />
15+
{applyNice ? 'Applying Nice' : 'Not applying Nice'}
16+
</label>
17+
1618
<ScatterChart {data} x="x" y="y" xNice={applyNice} yNice={applyNice} padding={24} height={400} />

packages/layerchart/src/lib/components/Axis.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
4646
/**
4747
* Width or height of each tick in pixels (enabling responsive count)
48+
* @default 80 (top|bottom|angle) or 50 (left|right|radius)
4849
*/
4950
tickSpacing?: number;
5051

0 commit comments

Comments
 (0)