Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mortage example edits #785

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 89 additions & 68 deletions examples/mortgage-rates/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,94 +6,115 @@ const pmms = FileAttachment("data/pmms.csv").csv({typed: true});

```js
const color = Plot.scale({color: {domain: ["30Y FRM", "15Y FRM"]}});
function colorLegend(y) {
return html`<span style="border-bottom: solid 2px ${color.apply(`${y}Y FRM`)};">${y}-year fixed-rate</span>`;
}
const colorLegend = (y) => html`<span style="border-bottom: solid 2px ${color.apply(`${y}Y FRM`)};">${y}-year fixed-rate</span>`;
```

```js
const tidy = pmms.flatMap(({date, pmms30, pmms15}) => [{date, rate: pmms30, type: "30Y FRM"}, {date, rate: pmms15, type: "15Y FRM"}]);
const recent = tidy.slice(-53 * 2);
```

```js
function frmCard(y, pmms) {
const key = `pmms${y}`;
const fmt = d3.format("+.2");
const fmt2 = d3.format(".2f");
const diff1 = pmms.at(-1)[key] - pmms.at(-2)[key];
const diffY = pmms.at(-1)[key] - pmms.at(-53)[key];
const range = d3.extent(pmms.slice(-52), d => d[key]);
const range = d3.extent(pmms.slice(-52), (d) => d[key]);
const stroke = color.apply(`${y}Y FRM`);
const rangechart = Plot.plot({
style: "overflow: visible;",
width: 300,
height: 40,
axis: null,
x: {inset: 40},
marks: [
Plot.tickX(pmms.slice(-52), {
x: key,
stroke,
insetTop: 10,
insetBottom: 10,
title: (d) => `${d.date?.toLocaleDateString("en-us")}: ${d[key]}%`,
tip: {anchor: "bottom"}
}),
Plot.tickX(pmms.slice(-1), {x: key, strokeWidth: 2}),
Plot.text([`${range[0]}%`], {frameAnchor: "left"}),
Plot.text([`${range[1]}%`], {frameAnchor: "right"})
],
caption: html`<span style="font-size: 11px">52 week range`
});
return html`<div class="card">
Copy link
Member Author

@mbostock mbostock Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn’t work correctly because of the wrapper span. #11 In general we want to server-side render as much as we can, so I changed the card to be declared statically, and then render the contents as a fragment.

<h2 style="color: ${stroke}">${y}Y FRM</b></h2>
<h1 style="opacity: 0.8;">${pmms.at(-1)[key] ?? "N/A"}%</h1>
return html.fragment`
<h2 style="color: ${stroke}">${y}-year fixed-rate</b></h2>
<h1>${formatPercent(pmms.at(-1)[key])}</h1>
<table class="table">
<tr><td>1-week change <td>${fmt(diff1)}%<td>${trend(diff1)}
<tr><td>1-year change <td>${fmt(diffY)}%<td>${trend(diffY)}
<tr><td>4-week average <td>${fmt2(d3.mean(pmms.slice(-4), d => d[key]))}%<td>
<tr><td>52-week average <td>${fmt2(d3.mean(pmms.slice(-52), d => d[key]))}%<td>
<tr>
<td>1-week change</td>
<td align="right">${formatPercent(diff1, {signDisplay: "always"})}</td>
<td>${trend(diff1)}</td>
<tr>
<td>1-year change</td>
<td align="right">${formatPercent(diffY, {signDisplay: "always"})}</td>
<td>${trend(diffY)}</td>
<tr>
<td>4-week average</td>
<td align="right">${formatPercent(d3.mean(pmms.slice(-4), (d) => d[key]))}</td>
<td></td>
<tr>
<td>52-week average</td>
<td align="right">${formatPercent(d3.mean(pmms.slice(-52), (d) => d[key]))}</td>
<td></td>
</table>
${rangechart}
</div>`;
${Plot.plot({
width: 278,
height: 40,
axis: null,
x: {inset: 40},
marks: [
Plot.tickX(pmms.slice(-52), {
x: key,
stroke,
insetTop: 10,
insetBottom: 10,
title: (d) => `${d.date?.toLocaleDateString("en-us")}: ${d[key]}%`,
tip: {anchor: "bottom"}
}),
Plot.tickX(pmms.slice(-1), {x: key, strokeWidth: 2}),
Plot.text([`${range[0]}%`], {frameAnchor: "left"}),
Plot.text([`${range[1]}%`], {frameAnchor: "right"})
]
})}
<span class="small muted">52-week range</span>
`;
}

function formatPercent(value, format) {
return value == null
? "N/A"
: (value / 100).toLocaleString("en-US", {minimumFractionDigits: 2, style: "percent", ...format});
}

function trend(v) {
if (Math.abs(v) < 0.01) return "";
return v > 0 ? html`<span style="color:steelblue">↗︎` : html`<span style="color:orange">↘︎`;
return v >= 0.005 ? html`<span class="green">↗︎</span>`
: v <= -0.005 ? html`<span class="red">↘︎</span>`
: "→";
}
```

<style>
table.table td:not(:first-child) {text-align:right;}
</style>

> _Each week since April 1971 [Freddie Mac](https://www.freddiemac.com/pmms/about-pmms.html) surveys lenders on the rates and points for their most popular ${colorLegend(30)}, ${colorLegend(15)} and other mortgage products._

<div class="grid grid-cols-2" style="max-width: 672px">${frmCard(30, pmms)} ${frmCard(15, pmms)}</div>
Each week, [Freddie Mac](https://www.freddiemac.com/pmms/about-pmms.html) surveys lenders on rates and points for their ${colorLegend(30)}, ${colorLegend(15)}, and other mortgage products.
Data as of ${pmms.at(-1).date?.toLocaleDateString("en-US", {year: "numeric", month: "long", day: "numeric"})}.

<p style="text-align: right; font-style: italic; font-size: smaller;">Data as of ${pmms.at(-1).date?.toLocaleDateString("en-us", {weekday: "long", year: "numeric", month: "short", day: "numeric", timeZone: "UTC"})
}. Source: <a href="https://www.freddiemac.com/pmms">Freddie Mac</a></p>
<div class="grid grid-cols-2" style="margin-top: 2rem; max-width: 640px;">
<div class="card">${frmCard(30, pmms)}</div>
<div class="card">${frmCard(15, pmms)}</div>
</div>

<p class="card">${Plot.plot({
title: "Past year",
height: 250,
y: {nice: 5, grid: true, label: "rate (%)"},
color,
marks: [
Plot.lineY(recent, {x: "date", y: "rate", stroke: "type", curve: "step", tip: true, markerEnd: true})
]
})}</p>
<div class="grid" style="max-width: 640px;">
<div class="card">
<h2>Rates over the past year</h2>
${resize((width) =>
Plot.plot({
width,
height: 250,
y: {grid: true, label: "rate (%)"},
color,
marks: [
Plot.lineY(tidy.slice(-53 * 2), {x: "date", y: "rate", stroke: "type", curve: "step", tip: true, markerEnd: true})
]
})
)}
</div>
</div>

<p class="card">${
Plot.plot({
title: "Historical data",
x: {nice: 20},
y: {grid: true, label: "rate (%)"},
color,
marks: [
Plot.ruleY([0]),
Plot.lineY(tidy, {x: "date", y: "rate", stroke: "type", tip: true})
]
})}</p>
<div class="grid">
<div class="card">
<h2>Rates over all time (${d3.extent(pmms, (d) => d.date.getUTCFullYear()).join("–")})</h2>
${resize((width) =>
Plot.plot({
width,
y: {grid: true, label: "rate (%)"},
color,
marks: [
Plot.ruleY([0]),
Plot.lineY(tidy, {x: "date", y: "rate", stroke: "type", tip: true})
]
})
)}
</div>
</div>