Skip to content

Commit 8878081

Browse files
committed
feat: add rent histogram
1 parent 60d7c3e commit 8878081

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

docs/components/apiHistogram.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Plot from "npm:@observablehq/plot";
22
import * as d3 from "npm:d3";
33
// import {dy} from "./apiHeatmap.js";
44

5-
const dy = 300; // number of rows
5+
const dy = 400; // number of rows
66
const marginTop = 0;
77
const marginRight = 20;
88
const marginBottom = 30;
@@ -29,7 +29,7 @@ export function ApiHistogram(
2929
style: "overflow: visible;",
3030
x: {type: "log", domain: [y1, y2 - 1], label},
3131
y: {axis: null, domain: [0, (height - marginTop - marginBottom) * ky], label: "requests"},
32-
color: {label: color.label},
32+
color: {label: color.label, legend: false},
3333
marks: [
3434
Plot.ruleY([0]),
3535
Plot.tip({length: 1}, {fill: [""], x: [y1], y: [0], format: {x: null, y: null}, render: renderTip})

docs/data/rent-histogram.parquet

91.5 KB
Binary file not shown.

docs/index.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {ApiHistogram} from "./components/apiHistogram.js";
6060

6161
```js
6262
const incomeHistogram = FileAttachment("data/income-histogram.parquet").parquet();
63-
const histogramCanvas = document.createElement("canvas");
63+
const incomeCanvas = document.createElement("canvas");
6464
```
6565

6666
```js
@@ -70,17 +70,30 @@ const sectorColor = Object.assign(Plot.scale({color: {domain: sectorColorMapping
7070
const sectorSwatch = (sector) => html`<span style="white-space: nowrap;"><svg width=10 height=10 fill=${sectorColor.apply(sector)}><rect width=10 height=10></rect></svg> <span class="small">${sector}</span></span>`;
7171
```
7272

73+
```js
74+
// Import the rent histogram data from the parquet file
75+
const rentHistogram = FileAttachment("data/rent-histogram.parquet").parquet();
76+
const rentCanvas = document.createElement("canvas");
77+
```
78+
79+
```js
80+
// Create the color mapping for regions
81+
const regionColorMapping = d3.sort(d3.rollups(rentHistogram.getChild("region"), (D) => D.length, (d) => d).filter(([d]) => d), ([, d]) => -d).map(([region, count]) => ({ region, count }));
82+
const regionColor = Object.assign(Plot.scale({ color: { domain: regionColorMapping.map((d) => d.region) } }), {label: "region" });
83+
const regionSwatch = (region) => html`<span style="white-space: nowrap;"><svg width=10 height=10 fill=${regionColor.apply(region)}><rect width=10 height=10></rect></svg><span class="small">${region}</span></span>`;
84+
```
7385

74-
<!-- <div class="grid grid-cols-2" style="grid-auto-rows: 504px;"> -->
86+
87+
<div class="grid grid-cols-2" style="grid-auto-rows: 504px;">
88+
<div class="card">
89+
<h2>Income distribution by sector (<a href="https://github.com/jaanli/exploring_american_community_survey_data/blob/main/american_community_survey/models/public_use_microdata_sample/income-histogram-with-sector.sql">code</a> for data transform)</h2>
90+
${resize((width) => ApiHistogram(incomeHistogram.getChild("income"), incomeHistogram.getChild("count"), incomeHistogram.getChild("sector"), {canvas: incomeCanvas, color: sectorColor, width, label: "Income ($)", y1: 1_000, y2: 200_000}))}
91+
</div>
7592
<div class="card">
76-
<h2>Income distribution by sector</h2>
77-
${resize((width) => ApiHistogram(incomeHistogram.getChild("income"), incomeHistogram.getChild("count"), incomeHistogram.getChild("sector"), {canvas: histogramCanvas, color: sectorColor, width, label: "Income ($)", y1: 1_000, y2: 200_000}))}
93+
<h2>Rent Distribution by Region (<a href="https://github.com/jaanli/exploring_american_community_survey_data/blob/main/american_community_survey/models/public_use_microdata_sample/household-histogram-with-region.sql">code</a> for data transform)</h2>
94+
${resize((width) => ApiHistogram(rentHistogram.getChild("rent"), rentHistogram.getChild("count"), rentHistogram.getChild("region"), {canvas: rentCanvas, color: regionColor, width, label: "Rent ($)", y1: 100, y2: 20_000 }))}
7895
</div>
79-
<!-- <div class="card">
80-
<h2>Response latency histogram</h2>
81-
${resize((width) => ApiHistogram(latencyHistogram.getChild("duration"), latencyHistogram.getChild("count"), latencyHistogram.getChild("route"), {canvas: histogramCanvas_new, color: routeColor, width, label: "Duration (ms)", y1: 0.5, y2: 10_000}))}
82-
</div> -->
83-
<!-- </div> -->
96+
</div>
8497

8598
---
8699
<!--

0 commit comments

Comments
 (0)