Skip to content

Commit 7d2e200

Browse files
committed
wip: debug histogram data colors not showing up (tooltips show up);
1 parent bb756fa commit 7d2e200

File tree

4 files changed

+99
-19
lines changed

4 files changed

+99
-19
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ A typical Framework project looks like this:
5353
| `yarn deploy` | Deploy your project to Observable |
5454
| `yarn clean` | Clear the local data loader cache |
5555
| `yarn observable` | Run commands like `observable help` |
56+
57+
## GPT-4 reference
58+
59+
https://chat.openai.com/share/0284945f-996d-4e62-85c5-bdc0f4e68e18

docs/components/surveyHistogram.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
// Assuming dy is a predefined value or imported from another module
5+
// For demonstration, let's define dy here. Adjust as necessary based on your actual data and requirements.
6+
const dy = 300; // This should match the actual height or be dynamically calculated based on the data
7+
8+
const marginTop = 0;
9+
const marginRight = 20;
10+
const marginBottom = 30;
11+
const marginLeft = 20;
12+
13+
const canvasCache = new WeakSet();
14+
15+
export function SurveyHistogram(
16+
value,
17+
count,
18+
category,
19+
{canvas = document.createElement("canvas"), color, width, height = 360, label, y1, y2}
20+
) {
21+
const ky = 165; // Adjust this value based on your data's density
22+
23+
const plot = Plot.plot({
24+
figure: true,
25+
width,
26+
height,
27+
marginTop,
28+
marginRight,
29+
marginBottom,
30+
marginLeft,
31+
style: "overflow: visible;",
32+
x: {type: "log", domain: [y1, y2 - 1], label},
33+
y: {axis: null, domain: [0, (height - marginTop - marginBottom) * ky], label: "Count"},
34+
color: {label: color.label},
35+
marks: [
36+
Plot.ruleY([0]),
37+
// Including tip functionality requires further adaptation and is left as an exercise
38+
]
39+
});
40+
41+
const svg = plot.querySelector("svg");
42+
const div = document.createElement("div");
43+
div.style = "position: relative;";
44+
45+
if (!canvasCache.has(canvas)) {
46+
canvasCache.add(canvas);
47+
canvas.width = width - marginLeft - marginRight;
48+
canvas.height = height - marginTop - marginBottom;
49+
canvas.style = `
50+
image-rendering: pixelated;
51+
position: absolute;
52+
left: ${marginLeft}px;
53+
top: ${marginTop}px;
54+
width: ${width - marginLeft - marginRight}px;
55+
height: ${height - marginTop - marginBottom}px;
56+
`;
57+
58+
const context = canvas.getContext("2d");
59+
// Custom rendering logic based on `value`, `count`, and `category` goes here
60+
61+
// This placeholder for the tick function demonstrates the approach; adapt based on your data processing
62+
// The actual drawing logic will vary based on how your data is structured and should be implemented accordingly
63+
}
64+
65+
svg.style.position = "relative";
66+
svg.replaceWith(div);
67+
div.appendChild(canvas);
68+
div.appendChild(svg);
69+
70+
// Implement the `renderTip` and `find` functions if tooltip functionality is needed
71+
72+
return plot;
73+
}

docs/data/income-histogram.parquet

297 KB
Binary file not shown.

docs/index.md

+22-19
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const latencyByRouteCanvas = document.createElement("canvas");
6565

6666
```js
6767
const latencyHistogram = FileAttachment("data/latency-histogram.parquet").parquet();
68-
const histogramCanvas = document.createElement("canvas");
68+
const histogramCanvas_new = document.createElement("canvas");
6969
```
7070

7171
```js
@@ -74,30 +74,33 @@ const routeColor = Object.assign(Plot.scale({color: {domain: topRoutesPixel.map(
7474
const routeSwatch = (route) => html`<span style="white-space: nowrap;"><svg width=10 height=10 fill=${routeColor.apply(route)}><rect width=10 height=10></rect></svg> <span class="small">${route}</span></span>`;
7575
```
7676

77-
<div class="grid grid-cols-2" style="grid-auto-rows: 504px;">
77+
```js
78+
const incomeData = FileAttachment("data/income-histogram.parquet").parquet();
79+
const histogramCanvas = document.createElement("canvas");
80+
```
81+
7882

83+
```js
84+
// Assuming modification for categorization based on age
85+
const ageColorMapping = d3.sort(d3.rollups(incomeData.getChild("age"), (D) => D.length, (d) => d).filter(([d]) => d), ([, d]) => -d).map(([age, count]) => ({age, count}));
86+
const ageColor = Object.assign(Plot.scale({color: {domain: ageColorMapping.map((d) => d.age.toString())}}), {label: "age"});
87+
const ageSwatch = (age) => html`<span style="white-space: nowrap;"><svg width=10 height=10 fill=${ageColor.apply(age.toString())}><rect width=10 height=10></rect></svg> <span class="small">${age}</span></span>`;
88+
```
89+
90+
91+
<div class="grid grid-cols-2" style="grid-auto-rows: 504px;">
92+
<div class="card">
93+
<h2>Income Distribution by Age</h2>
94+
${resize((width) => ApiHistogram(incomeData.getChild("income"), incomeData.getChild("count"), incomeData.getChild("age"), {canvas: histogramCanvas, color: ageColor, width, label: "Income ($)", y1: 0.5, y2: 100_000}))}
95+
</div>
7996
<div class="card">
8097
<h2>Response latency histogram</h2>
81-
${resize((width) => ApiHistogram(latencyHistogram.getChild("duration"), latencyHistogram.getChild("count"), latencyHistogram.getChild("route"), {canvas: histogramCanvas, color: routeColor, width, label: "Duration (ms)", y1: 0.5, y2: 10_000}))}
98+
${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}))}
8299
</div>
83-
<div class="card">${
84-
resize((width) => Plot.plot({
85-
title: "How big are penguins, anyway? 🐧",
86-
width,
87-
grid: true,
88-
x: {label: "Body mass (g)"},
89-
y: {label: "Flipper length (mm)"},
90-
color: {legend: true},
91-
marks: [
92-
Plot.linearRegressionY(penguins, {x: "body_mass_g", y: "flipper_length_mm", stroke: "species"}),
93-
Plot.dot(penguins, {x: "body_mass_g", y: "flipper_length_mm", stroke: "species", tip: true})
94-
]
95-
}))
96-
}</div>
97100
</div>
98101

99102
---
100-
103+
<!--
101104
## Next steps
102105
103106
Here are some ideas of things you could try…
@@ -124,4 +127,4 @@ Here are some ideas of things you could try…
124127
<div class="card">
125128
Visit <a href="https://github.com/observablehq/framework">Framework on GitHub</a> and give us a star. Or file an issue if you’ve found a bug!
126129
</div>
127-
</div>
130+
</div> -->

0 commit comments

Comments
 (0)