Skip to content

Commit e1112f8

Browse files
committed
add bar examples
1 parent 9867cf0 commit e1112f8

File tree

2 files changed

+53
-81
lines changed

2 files changed

+53
-81
lines changed

src/lib/helpers/scales.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,13 @@ export function createScale<T extends ScaleOptions>(
320320

321321
if (scaleOptions.interval) {
322322
if (isOrdinal) {
323-
domain = domainFromInterval(domain, scaleOptions.interval);
323+
domain = domainFromInterval(domain, scaleOptions.interval, name);
324324
} else {
325-
throw new Error(
326-
'Setting interval via axis options is only supported for ordinal scales'
327-
);
325+
if (markTypes.size > 0) {
326+
console.warn(
327+
'Setting interval via axis options is only supported for ordinal scales'
328+
);
329+
}
328330
}
329331
}
330332

@@ -361,10 +363,11 @@ export function createScale<T extends ScaleOptions>(
361363
};
362364
}
363365

364-
function domainFromInterval(domain: RawValue[], interval: string | number) {
366+
function domainFromInterval(domain: RawValue[], interval: string | number, name: ScaleName) {
365367
const interval_ = maybeInterval(interval);
366368
const [lo, hi] = extent(domain);
367-
return interval_.range(lo, interval_.offset(hi));
369+
const out = interval_.range(lo, interval_.offset(hi));
370+
return name === 'y' ? out.toReversed() : out;
368371
}
369372

370373
/**

src/routes/marks/bar/+page.md

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,63 @@ title: Bar mark
77
import StackedBarPlot from './StackedBarPlot.svelte';
88
</script>
99

10-
Bars are cool. They come in two flavors: [BarY](#BarY) for vertical bars (columns) and [BarX](#BarX) for horizontal bars.
11-
12-
Here's a very simple bar chart:
10+
Bars are useful to show quantitative data for different categories. They come in two flavors: [BarX](#BarX) for horizontal bars (y axis requires band scale) and [BarY](#BarY) for vertical bars aka. columns (x axis requires band scale).
1311

1412
```svelte live
1513
<script>
1614
import { Plot, BarX, RuleX } from 'svelteplot';
15+
const data = [
16+
{ year: 2019, value: 20 },
17+
{ year: 2020, value: 24 },
18+
{ year: 2021, value: 32 },
19+
{ year: 2022, value: 39 },
20+
{ year: 2024, value: 56 }
21+
];
1722
</script>
1823
19-
<Plot
20-
y={{ type: 'band' }}
21-
x={{ grid: true }}
22-
height={200}
23-
marginTop={0}>
24-
<BarX data={[1, 2, 3, 4, 5]} />
24+
<Plot>
25+
<BarX {data} x="value" y="year" />
2526
<RuleX data={[0]} />
2627
</Plot>
2728
```
2829

2930
```svelte
30-
<Plot y={{ type: 'band' }} x={{ grid: true }} height={120}>
31-
<BarX data={[1, 2, 3, 4, 5]} />
31+
<Plot>
32+
<BarX {data} x="value" y="year" />
33+
<RuleX data={[0]} />
34+
</Plot>
35+
```
36+
37+
[fork](https://svelte.dev/playground/7a0d38cf74be4a9985feb7bef0456008?version=5)
38+
39+
SveltePlot automatically infers a band scale for the y axis in the above example. but since our data is missing a value for 2023, the value `"2023"` is entirely missing from the band scale domain. We could fix this by passing the domain value manually, or by using the `interval` option of the y axis:
40+
41+
```svelte live
42+
<script>
43+
import { Plot, BarX, RuleX } from 'svelteplot';
44+
const data = [
45+
{ year: 2019, value: 20 },
46+
{ year: 2020, value: 24 },
47+
{ year: 2021, value: 32 },
48+
{ year: 2022, value: 39 },
49+
{ year: 2024, value: 56 }
50+
];
51+
</script>
52+
53+
<Plot y={{ interval: 1 }}>
54+
<BarX {data} x="value" y="year" />
55+
<RuleX data={[0]} />
56+
</Plot>
57+
```
58+
59+
```
60+
<Plot y={{ interval: 1 }}>
61+
<BarX {data} x="value" y="year" />
3262
<RuleX data={[0]} />
3363
</Plot>
3464
```
3565

36-
You can create stacked bar charts by defining a fill channel which will be used for grouping the series by the implicit [stack transform](/transforms/stack):
66+
You can create stacked bar charts by defining a fill channel which will be used for grouping the series by the implicit [stack transform](/transforms/stack). In the following example we're first grouping the penguins dataset by island to then stack them by species:
3767

3868
```svelte live
3969
<script lang="ts">
@@ -82,7 +112,7 @@ You can create stacked bar charts by defining a fill channel which will be used
82112

83113
The `BarX` component renders horizontal bars, typically used with a band scale on the y-axis. This is ideal for categorical data where the categories run along the y-axis, and the values extend horizontally.
84114

85-
### Properties
115+
**Properties**
86116

87117
- **data** - The data array to visualize
88118
- **x** - Value accessor for the x channel (length of bar)
@@ -99,35 +129,11 @@ The `BarX` component renders horizontal bars, typically used with a band scale o
99129

100130
Additionally, `BarX` supports all common styling properties like `fill`, `stroke`, `opacity`, etc.
101131

102-
### Example
103-
104-
```svelte
105-
<Plot y={{ type: 'band' }} x={{ grid: true }}>
106-
<BarX
107-
data={myData}
108-
y="category"
109-
x="value"
110-
fill="steelblue" />
111-
</Plot>
112-
```
113-
114-
For stacked bar charts, provide a `fill` channel that will be used for grouping the series:
115-
116-
```svelte
117-
<Plot y={{ type: 'band' }} color={{ legend: true }}>
118-
<BarX
119-
data={myData}
120-
y="category"
121-
x="value"
122-
fill="group" />
123-
</Plot>
124-
```
125-
126132
## BarY
127133

128134
The `BarY` component renders vertical bars (columns), typically used with a band scale on the x-axis. This is ideal for categorical data where the categories run along the x-axis, and the values extend vertically.
129135

130-
### Properties
136+
**Properties**
131137

132138
- **data** - The data array to visualize
133139
- **x** - Value accessor for the x channel (position on the category axis)
@@ -145,45 +151,8 @@ The `BarY` component renders vertical bars (columns), typically used with a band
145151

146152
Additionally, `BarY` supports all common styling properties like `fill`, `stroke`, `opacity`, etc.
147153

148-
### Example
149-
150-
```svelte live
151-
<script>
152-
import { Plot, BarY, RuleY } from 'svelteplot';
153-
import { range } from 'es-toolkit';
154-
</script>
155-
156-
<Plot>
157-
<BarY
158-
data={range(2, 10).map((v) => v ** 2)}
159-
fill="steelblue" />
160-
<RuleY y={0} />
161-
</Plot>
162-
```
163-
164-
```svelte
165-
<Plot>
166-
<BarY
167-
data={range(2, 10).map((v) => v ** 2)}
168-
fill="steelblue" />
169-
<RuleY y={0} />
170-
</Plot>
171-
```
172-
173154
[fork](https://svelte.dev/playground/8b9fb6c1946d4579a3dc9da32f6c983c?version=5)
174155

175-
For stacked bar charts, provide a `fill` channel that will be used for grouping the series:
176-
177-
```svelte
178-
<Plot x={{ type: 'band' }} color={{ legend: true }}>
179-
<BarY
180-
data={myData}
181-
x="category"
182-
y="value"
183-
fill="group" />
184-
</Plot>
185-
```
186-
187156
## Insets
188157

189158
You can create bullet bars using the `inset` option and two `BarX` layers:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy