Skip to content

Commit 3fe972a

Browse files
authored
default template component (#574)
* * add a Plot component * remove the bignumber component closes #534 related #248 * fix test
1 parent febf851 commit 3fe972a

File tree

5 files changed

+57
-80
lines changed

5 files changed

+57
-80
lines changed

templates/default/docs/components/bigNumber.js.tmpl

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as Plot from "npm:@observablehq/plot";
2+
import * as d3 from "npm:d3";
3+
4+
export function timeline(events) {
5+
return Plot.plot({
6+
insetTop: 30,
7+
insetBottom: 10,
8+
insetRight: 10,
9+
height: 250,
10+
x: {
11+
domain: d3.extent(events, (d) => d["year"]),
12+
label: "Year",
13+
nice: true,
14+
},
15+
y: { axis: null },
16+
marks: [
17+
Plot.axisX({tickFormat: d3.format(".0f")}),
18+
Plot.ruleX(events, {x: "year", y: "y", stroke: "#eee", strokeWidth: 2}),
19+
Plot.ruleY([0], {stroke: "#eee"}),
20+
Plot.dot(events, {x: "year", y: "y", fill: "currentColor", r: 5}),
21+
Plot.text(events, {
22+
x: "year",
23+
y: "y",
24+
text: "name",
25+
dy: -20,
26+
lineWidth: 10,
27+
fontSize: 12,
28+
}),
29+
]
30+
});
31+
}

templates/default/docs/example-dashboard.md.tmpl

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,36 @@ theme: dashboard
55

66
# Rocket launches 🚀
77

8-
<!-- import components and tools -->
9-
10-
```js
11-
import {BigNumber} from "./components/bigNumber.js";
12-
```
13-
148
<!-- load and transform the data -->
159

1610
```js
17-
const launchHistory = await FileAttachment("data/launchHistory.csv") // data loader
18-
.csv({typed: true})
19-
const countLaunchesByStateId = id => launchHistory.filter(d => d.stateId === id).length;
11+
const launchHistory = await FileAttachment("data/launchHistory.csv")
12+
.csv({typed: true});
13+
14+
const format = d3.format(",");
15+
function launches(id) {
16+
return format(launchHistory.filter((d) => d.stateId === id).length);
17+
}
2018
```
2119

22-
<!-- embed BigNumber into cards -->
20+
<!-- cards with big numbers -->
2321

2422
<div class="grid grid-cols-4">
2523
<div class="card">
2624
<h2>United States</h2>
27-
${BigNumber(countLaunchesByStateId("US"), {title: "Launches"})}
25+
<span class="big">${launches("US")}</span>
2826
</div>
2927
<div class="card">
3028
<h2>Soviet Union</h2>
31-
${BigNumber(countLaunchesByStateId("SU"), {title: "Launches"})}
29+
<span class="big">${launches("SU")}</span>
3230
</div>
3331
<div class="card">
3432
<h2>Russia</h2>
35-
${BigNumber(countLaunchesByStateId("RU"), {title: "Launches"})}
33+
<span class="big">${launches("RU")}</span>
3634
</div>
3735
<div class="card">
3836
<h2>China</h2>
39-
${BigNumber(countLaunchesByStateId("CN"), {title: "Launches"})}
37+
<span class="big">${launches("CN")}</span>
4038
</div>
4139
</div>
4240

@@ -45,17 +43,17 @@ const countLaunchesByStateId = id => launchHistory.filter(d => d.stateId === id)
4543
<div class="card grid grid-cols-8">
4644
${resize((width) => Plot.plot({
4745
width,
48-
title: "Launches Over the Years",
46+
title: "Launches over the years",
4947
height: 300,
50-
x: { label: null, interval: "year" },
51-
y: { grid: true, label: "Launches" },
52-
color: { legend: true, label: "State" },
48+
x: {label: null, interval: "year"},
49+
y: {grid: true, label: "Launches"},
50+
color: {legend: true, label: "State"},
5351
marks: [
5452
Plot.barY(
5553
launchHistory,
5654
Plot.groupX(
57-
{ y: "count" },
58-
{ x: d => new Date(d.date), fill: "state", tip: {format: {x: false}} }
55+
{y: "count"},
56+
{x: d => new Date(d.date), fill: "state", tip: {format: {x: false}}}
5957
)
6058
),
6159
Plot.ruleY([0])

templates/default/docs/example-report.md.tmpl

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
toc: true
33
---
44

5-
# A brief history of rocket launches 🚀
5+
# A brief history of space exploration
66

7-
This report is a brief overview of the history and current state of rocket launches and general space exploration.
7+
This report is a brief overview of the history and current state of rocket launches and space exploration.
88

99
## Background
1010

@@ -16,37 +16,16 @@ This led to the launch of the first artificial satellite, Sputnik 1, and the man
1616

1717
## The Space Shuttle era
1818

19-
The late 20th century witnessed a significant milestone in rocket launches.
19+
```js
20+
const events = FileAttachment("./data/spaceHistory.json").json();
21+
```
2022

2123
```js
22-
const events = await FileAttachment("./data/spaceHistory.json").json(); // static file
24+
import {timeline} from "./components/timeline.js";
2325
```
2426

2527
```js
26-
const timeline = Plot.plot({
27-
insetTop: 30,
28-
insetBottom: 10,
29-
insetRight: 10,
30-
height: 250,
31-
x: {
32-
domain: d3.extent(events, d => d["year"]),
33-
label: "Year",
34-
nice: true
35-
},
36-
y: { axis: null },
37-
marks: [
38-
Plot.axisX({
39-
tickFormat: d3.format(".0f"),
40-
}),
41-
Plot.ruleX(events, { x: "year", y: "y", stroke: "#eee", strokeWidth: 2 }),
42-
Plot.ruleY([0], {stroke: "#eee"}),
43-
Plot.dot(events, {x: "year" , y: "y", fill: "currentColor", r: 5}),
44-
Plot.text(
45-
events,
46-
{x: "year", y: "y", text: "name", dy: -20, lineWidth: 10, fontSize: 12}),
47-
]
48-
});
49-
display(timeline);
28+
display(timeline(events));
5029
```
5130

5231
### Sputnik 1 (1957)

test/create-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("create", async () => {
1212
new Set(effects.outputs.keys()),
1313
new Set([
1414
"template-test/.gitignore",
15-
"template-test/docs/components/bigNumber.js",
15+
"template-test/docs/components/timeline.js",
1616
"template-test/docs/data/launchHistory.csv.js",
1717
"template-test/docs/data/spaceHistory.json",
1818
"template-test/docs/example-dashboard.md",

0 commit comments

Comments
 (0)