Skip to content

Commit da9afa8

Browse files
authored
Components (#522)
Adds the following Components docs: - Components overview - Inputs overview - Layout section - Grid - Card - Resize - Charts section (Observable Plot snippets, grouped into pages by general type, e.g. "Area charts") - Inputs section (single page per input)
1 parent 2bcb447 commit da9afa8

33 files changed

+13988
-13
lines changed

docs/charts/area.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Area chart
2+
3+
Copy the code snippets below, paste into a JavaScript code block, then substitute your own data to create area charts using the [area mark](https://observablehq.com/plot/marks/area) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
## Area chart
6+
7+
```js echo
8+
Plot.plot({
9+
marks: [
10+
Plot.areaY(aapl, {x: "Date", y: "Close"}),
11+
Plot.ruleY([0])
12+
]
13+
})
14+
```
15+
16+
## Band area chart
17+
18+
```js echo
19+
Plot.plot({
20+
marks: [
21+
Plot.areaY(weather.slice(-365), {x: "date", y1: "temp_min", y2: "temp_max", curve: "step"})
22+
]
23+
})
24+
```
25+
26+
## Stacked area chart
27+
28+
```js echo
29+
Plot.plot({
30+
y: {
31+
tickFormat: "s"
32+
},
33+
marks: [
34+
Plot.areaY(industries, {x: "date", y: "unemployed", fill: "industry"}),
35+
Plot.ruleY([0])
36+
]
37+
})
38+
```
39+
40+

docs/charts/arrow.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Arrow chart
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create arrow charts using the [arrow mark](https://observablehq.com/plot/marks/arrow) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
```js echo
6+
Plot.plot({
7+
x: {
8+
type: "log"
9+
},
10+
marks: [
11+
Plot.arrow(citywages, {
12+
x1: "POP_1980",
13+
y1: "R90_10_1980",
14+
x2: "POP_2015",
15+
y2: "R90_10_2015",
16+
bend: true
17+
})
18+
]
19+
})
20+
```

docs/charts/bar.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Bar chart
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create bar charts using the [bar mark](https://observablehq.com/plot/marks/bar) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
## Sorted bar chart
6+
7+
```js echo
8+
Plot.plot({
9+
marks: [
10+
Plot.barY(alphabet, {x: "letter", y: "frequency", sort: {x: "y", reverse: true}}),
11+
Plot.ruleY([0])
12+
]
13+
})
14+
```
15+
16+
## Horizontal bar chart
17+
18+
```js echo
19+
Plot.plot({
20+
marks: [
21+
Plot.barX(alphabet, {x: "frequency", y: "letter", sort: {y: "x", reverse: true}}),
22+
Plot.ruleX([0])
23+
]
24+
})
25+
```
26+
27+
## Top 10 bar chart
28+
29+
```js echo
30+
Plot.plot({
31+
marks: [
32+
Plot.barX(olympians, Plot.groupY({x: "count"}, {y: "nationality", sort: {y: "x", reverse: true, limit: 10}})),
33+
Plot.ruleX([0])
34+
]
35+
})
36+
```
37+
38+
## Weighted top 10 bar chart
39+
40+
```js echo
41+
Plot.plot({
42+
marks: [
43+
Plot.barX(olympians, Plot.groupY({x: "sum"}, {x: "gold", y: "nationality", sort: {y: "x", reverse: true, limit: 10}})),
44+
Plot.ruleX([0])
45+
]
46+
})
47+
```
48+
49+
## Temporal bar chart
50+
51+
```js echo
52+
Plot.plot({
53+
marks: [
54+
Plot.rectY(weather.slice(-42), {x: "date", y: "wind", interval: d3.utcDay}),
55+
Plot.ruleY([0])
56+
]
57+
})
58+
```

docs/charts/cell.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Cell chart
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a cell chart using the [cell mark](https://observablehq.com/plot/marks/cell) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
```js echo
6+
Plot.plot({
7+
marks: [
8+
Plot.cell(weather.slice(-365), {
9+
x: d => d.date.getUTCDate(),
10+
y: d => d.date.getUTCMonth(),
11+
fill: "temp_max"
12+
})
13+
]
14+
})
15+
```

docs/charts/dot.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Scatterplot
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a scatterplot chart using the [dot mark](https://observablehq.com/plot/marks/dot) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
```js echo
6+
Plot.plot({
7+
marks: [
8+
Plot.dot(cars, {x: "power (hp)", y: "economy (mpg)"})
9+
]
10+
})
11+
```

docs/charts/facets.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Faceted chart
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a faceted chart with small multiples using the [facet channel](https://observablehq.com/plot/features/facets) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
```js echo
6+
Plot.plot({
7+
facet: {
8+
data: penguins,
9+
x: "species"
10+
},
11+
marks: [
12+
Plot.frame(),
13+
Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm"})
14+
]
15+
})
16+
```

docs/charts/grouping-data.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Grouping data
2+
3+
[Observable Plot](https://observablehq.com/plot/) provides a number of [transforms](https://observablehq.com/plot/features/transforms) that help you perform common data transformations. The [group](https://observablehq.com/plot/transforms/group) and [bin](https://observablehq.com/plot/transforms/bin) transforms (for categorical and quantitative dimensions, respectively) group data into discrete bins. A reducer (_e.g._ sum, count, or mean) can then be applied to visualize summary values by bin.
4+
5+
## Hexbin chart
6+
7+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a hexbin chart using the [dot mark](https://observablehq.com/plot/marks/dot) and the [hexbin transform](https://observablehq.com/plot/transforms/hexbin).
8+
9+
```js echo
10+
Plot.plot({
11+
color: {scheme: "ylgnbu"},
12+
marks: [
13+
Plot.dot(olympians, Plot.hexbin({fill: "sum"}, {x: "weight", y: "height"}))
14+
]
15+
})
16+
```
17+
18+
## Histogram
19+
20+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a histogram using the [rect mark](https://observablehq.com/plot/marks/rect) and the [bin transform](https://observablehq.com/plot/transforms/bin).
21+
22+
```js echo
23+
Plot.plot({
24+
marks: [
25+
Plot.rectY(olympians, Plot.binX({y: "count"}, {x: "weight"})),
26+
Plot.ruleY([0])
27+
]
28+
})
29+
```

docs/charts/line.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Line chart
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a line chart using the [line mark](https://observablehq.com/plot/marks/line) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
## Basic line chart
6+
7+
```js echo
8+
Plot.plot({
9+
marks: [
10+
Plot.ruleY([0]),
11+
Plot.lineY(aapl, {x: "Date", y: "Close"})
12+
]
13+
})
14+
```
15+
16+
## Multi-series line chart
17+
18+
```js echo
19+
Plot.plot({
20+
marks: [
21+
Plot.ruleY([0]),
22+
Plot.lineY(industries, {x: "date", y: "unemployed", z: "industry"})
23+
]
24+
})
25+
```
26+
27+
## Moving average line chart
28+
29+
```js echo
30+
Plot.plot({
31+
marks: [
32+
Plot.ruleY([0]),
33+
Plot.lineY(aapl, Plot.windowY({x: "Date", y: "Close", k: 10, reduce: "mean"}))
34+
]
35+
})
36+
```

docs/charts/tick.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Tick chart
2+
3+
Copy the code snippet below, paste into a JavaScript code block, then substitute your own data to create a tick chart using the [tick mark](https://observablehq.com/plot/marks/tick) from [Observable Plot](https://observablehq.com/plot/).
4+
5+
```js echo
6+
Plot.plot({
7+
marks: [
8+
Plot.tickX(cars, {x: "economy (mpg)", y: "year"})
9+
]
10+
})
11+
```

docs/components.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Components
2+
3+
You don’t have to start from scratch: components are reusable pieces of code (functions, themes, snippets, etc.) that make it quicker to update page layout and appearance, and add common page content.
4+
5+
The Observable CLI offers three flavors of components: [layout helpers](#layout-helpers), [Observable Plot snippets](#observable-plot-snippets), and [Observable Inputs](#observable-inputs).
6+
7+
## Layout helpers
8+
9+
A collection of elements useful for formatting page content: themes, `card` and `grid` CSS classes, and the `resize` function.
10+
11+
### Themes
12+
13+
<!-- TODO update link to themes gallery layout/themes.md page once added-->
14+
Observable Markdown offers a number of [built-in themes](./config#theme) that you can compose to create, say, wide pages with an alternative dark color theme:
15+
16+
```js run=false
17+
theme: ["dark", "alt", "wide"]
18+
```
19+
20+
The code above, when included in the [config file](./config), specifies the default theme for the project. In addition, you can specify the theme for a single page in its [front matter](markdown#front-matter):
21+
22+
```yaml
23+
---
24+
theme: [dark, alt, wide]
25+
---
26+
```
27+
28+
You are not limited to the built-in themes. For complete control over the design of your project, see the [style option](./config/#style) instead.
29+
30+
### Grid
31+
32+
The included [`grid`](./layout/grid) CSS classes make it easier to control how page content is arranged.
33+
34+
<div class="grid grid-cols-2">
35+
<div class="card"><h1>A</h1>1 × 1</div>
36+
<div class="card grid-rowspan-2"><h1>B</h1>1 × 2</div>
37+
<div class="card"><h1>C</h1>1 × 1</div>
38+
<div class="card grid-colspan-2"><h1>D</h1>1 × 2</div>
39+
</div>
40+
41+
```html run=false
42+
<div class="grid grid-cols-2">
43+
<div class="card"><h1>A</h1>1 × 1</div>
44+
<div class="card grid-rowspan-2"><h1>B</h1> 1 × 2</div>
45+
<div class="card"><h1>C</h1>1 × 1</div>
46+
<div class="card grid-colspan-2"><h1>D</h1>1 × 2</div>
47+
</div>
48+
```
49+
50+
### Card
51+
52+
The [`card`](./layout/card) CSS class has default styles that help create a card: container borders, background color, padding and optional titles and subtitles.
53+
54+
<div class="grid grid-cols-2">
55+
<div class="card">
56+
<h2>A card title</h2>
57+
<h3>A card subtitle</h3>
58+
${
59+
Plot.plot({
60+
marks: [
61+
Plot.dot(penguins, {x: "body_mass_g", y: "flipper_length_mm"})
62+
]
63+
})
64+
}
65+
</div>
66+
<div class="card">
67+
<p>Tortor condimentum lacinia quis vel eros. Arcu risus quis varius quam quisque id. Magnis dis parturient montes nascetur ridiculus mus mauris. Porttitor leo a diam sollicitudin. Odio facilisis mauris sit amet massa vitae tortor. Nibh venenatis cras sed felis eget velit aliquet sagittis. Ullamcorper sit amet risus nullam eget felis eget nunc. In egestas erat imperdiet sed euismod nisi porta lorem mollis. A erat nam at lectus urna duis convallis. Id eu nisl nunc mi ipsum faucibus vitae. Purus ut faucibus pulvinar elementum integer enim neque volutpat ac.</p>
68+
</div>
69+
</div>
70+
71+
```html run=false
72+
<div class="grid grid-cols-2">
73+
<div class="card">
74+
<h2>A card title</h2>
75+
<h3>A card subtitle</h3>
76+
${
77+
Plot.plot({
78+
marks: [
79+
Plot.dot(penguins, {x: "body_mass_g", y: "flipper_length_mm"})
80+
]
81+
})
82+
}
83+
</div>
84+
<div class="card">
85+
<p>Tortor condimentum lacinia quis vel eros. Arcu risus quis varius quam quisque id. Magnis dis parturient montes nascetur ridiculus mus mauris. Porttitor leo a diam sollicitudin. Odio facilisis mauris sit amet massa vitae tortor. Nibh venenatis cras sed felis eget velit aliquet sagittis. Ullamcorper sit amet risus nullam eget felis eget nunc. In egestas erat imperdiet sed euismod nisi porta lorem mollis. A erat nam at lectus urna duis convallis. Id eu nisl nunc mi ipsum faucibus vitae. Purus ut faucibus pulvinar elementum integer enim neque volutpat ac.</p>
86+
</div>
87+
</div>
88+
```
89+
90+
### Resize
91+
92+
The [`resize`](./layout/resize) function automatically recomputes a DOM element (often, a chart) when the dimensions of its parent container change.
93+
94+
Resize exists in the Observable standard library, or can be imported explicitly:
95+
96+
```js echo
97+
import {resize} from "npm:@observablehq/stdlib";
98+
```
99+
100+
<div>
101+
${resize((width) => Plot.barY([9, 4, 8, 1, 11, 3, 4, 2, 7, 5]).plot({width}))}
102+
</div>
103+
104+
```html run=false
105+
<div>
106+
${resize((width) => Plot.barY([9, 4, 8, 1, 11, 3, 4, 2, 7, 5]).plot({width}))}
107+
</div>
108+
```
109+
110+
## Observable Plot snippets
111+
112+
[Observable Plot](https://observablehq.com/plot/) is a free, open source JavaScript library for concise and expressive data visualization, built by Observable.
113+
114+
Several examples of Observable Plot code are included in this documentation, covering some common chart types including area charts ([stacked](./charts/area#stacked-area-chart) and [band area](./charts/area#band-area-chart)), bar charts ([sorted](./charts/bar#sorted-bar-chart), [temporal](./charts/bar#temporal-bar-chart), and [weighted](./charts/bar#weighted-top-10-bar-chart)), line charts ([single-series](./charts/line#basic-line-chart), [multi-series](./charts/line#multi-series-line-chart) and [moving average](./charts/line#moving-average-line-chart)), [scatterplots](./charts/dot#scatterplot), and more. See [Observable Plot’s gallery](https://observablehq.com/@observablehq/plot-gallery) for even more examples.
115+
116+
All examples use common datasets that are loaded when referenced by name, such as the `weather` dataset in the code snippet below.
117+
118+
```js echo
119+
Plot.plot({
120+
marks: [
121+
Plot.cell(weather, {
122+
x: d => d.date.getUTCDate(),
123+
y: d => d.date.getUTCMonth(),
124+
fill: "temp_max"
125+
})
126+
]
127+
})
128+
```
129+
130+
If the chart type you want to add is not included as a snippet here, don’t sweat - a great number of examples (in both [Observable Plot](https://observablehq.com/@observablehq/plot-gallery) and [D3](https://observablehq.com/@d3/gallery)) are available to explore and reuse.
131+
132+
**Can I use other data visualization libraries?** Absolutely. Use any other visualization library you like by [importing from npm](./javascript/imports).
133+
134+
## Observable Inputs
135+
136+
The [Observable Inputs](./lib/inputs) library provides a suite of lightweight interface components — buttons, sliders, dropdowns, checkboxes, and the like — that viewers can update to explore interactive displays (for example, selecting only a few of many categories to show in a bar chart).
137+
138+
The [radio input](./inputs/radio) prompts a user to select a penguin species:
139+
140+
```js echo
141+
const pickSpecies = view(Inputs.radio(["Adelie", "Chinstrap", "Gentoo"], {value: "Gentoo", label: "Penguin species:"}))
142+
```
143+
144+
The value of `pickSpecies` (<tt>="${pickSpecies}"</tt>) can then be accessed elsewhere in the page, as a parameter in other computations, and to create interactive charts, tables or text with [inline expressions](./javascript#inline-expressions).

0 commit comments

Comments
 (0)