-
Notifications
You must be signed in to change notification settings - Fork 1
/
HistogramInteractions.tsx
231 lines (206 loc) · 6.26 KB
/
HistogramInteractions.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import * as React from "react";
import * as d3 from "d3";
import Chart from "../components/Chart";
import Axis from "../components/Axis";
import type { WeatherData } from "../hooks/useWeatherData";
import type { BoundedDimensions } from "../utils/types";
import styles from "./styles/HistogramInteractions.module.css";
//* Step 2. Create chart dimensions
const width = 600;
const dimensions: BoundedDimensions = {
width,
//? Histograms are easiest to read when they are wider than they are tall!
height: width * 0.6,
margin: {
top: 30,
right: 10,
bottom: 50,
left: 50,
},
boundedWidth: 0,
boundedHeight: 0,
};
dimensions.boundedWidth =
dimensions.width - dimensions.margin.left - dimensions.margin.right;
dimensions.boundedHeight =
dimensions.height - dimensions.margin.top - dimensions.margin.bottom;
type TooltipState = {
show: boolean;
range?: {
min: number;
max: number;
};
value?: number;
coords: {
x: number;
y: number;
};
};
type TooltipAction =
| {
type: "SHOW";
payload: Omit<TooltipState, "show">;
}
| { type: "HIDE" };
function tooltipReducer(state: TooltipState, action: TooltipAction) {
switch (action.type) {
case "HIDE":
return { ...state, show: false };
case "SHOW":
return { ...state, show: true, ...action.payload };
}
}
function HistogramInteractions({ dataset }: { dataset: WeatherData[] }) {
//* Step 1b. Access Data
const xAccessor = (d: WeatherData) => d.humidity;
//* Step 4. Create scales
const xScale = d3
.scaleLinear()
.domain(d3.extent(dataset, xAccessor) as [number, number])
.range([0, dimensions.boundedWidth])
.nice();
const binGenerator = d3
.bin<WeatherData, number>()
.domain(xScale.domain() as [number, number])
.value(xAccessor)
.thresholds(12);
const bins = binGenerator(dataset);
type WeatherDataBin = typeof bins[number];
const yAccessor = (bin: WeatherDataBin) => bin.length;
const yScale = d3
.scaleLinear()
//? For histograms, we want the y axis to always start at 0
.domain([0, d3.max(bins, yAccessor)] as [0, number])
.range([dimensions.boundedHeight, 0])
.nice();
const barPadding = 1;
const xAccessorScaled = (d: WeatherDataBin) =>
xScale(d.x0 as number) + barPadding;
const yAccessorScaled = (bin: WeatherDataBin) => yScale(yAccessor(bin));
const widthAccessorScaled = (d: WeatherDataBin) =>
xScale(d.x1 as number) - xScale(d.x0 as number) - barPadding;
const heightAccessorScaled = (d: WeatherDataBin) =>
dimensions.boundedHeight - yScale(yAccessor(d));
const midpointAccessorScaled = (d: WeatherDataBin) =>
xScale(d.x0 as number) +
(xScale(d.x1 as number) - xScale(d.x0 as number)) / 2;
const mean = d3.mean(dataset, xAccessor) as number;
//* Step 7a. Handle interactions
const [tooltip, dispatch] = React.useReducer(tooltipReducer, {
show: false,
coords: {
x: 0,
y: 0,
},
});
function showTooltip(bin: WeatherDataBin) {
const x =
xScale(bin.x0 as number) +
barPadding / 2 +
(xScale(bin.x1 as number) - xScale(bin.x0 as number)) / 2 +
dimensions.margin.left;
const y = yScale(yAccessor(bin)) + dimensions.margin.top;
dispatch({
type: "SHOW",
payload: {
range: {
min: bin.x0 as number,
max: bin.x1 as number,
},
value: bin.length,
coords: {
x,
y,
},
},
});
}
function hideTooltip() {
dispatch({ type: "HIDE" });
}
return (
<div className={styles.container}>
<div className={styles.wrapper}>
{/* Step 3. Draw canvas */}
<Chart dimensions={dimensions}>
{/* Step 5. Draw data */}
<g role="list" tabIndex={0} aria-label="histogram bars">
{bins.map((bin, i) => (
<g
key={`histogram-bin-${i}`}
className={styles.bin}
role="listitem"
tabIndex={0}
aria-label={`There were ${yAccessor(
bin
)} days with humidity between ${bin.x0} and ${bin.x1}`}
onMouseEnter={() => showTooltip(bin)}
onMouseLeave={() => hideTooltip()}
//? We can even add interactions on accessible focus/blur events!
onFocus={() => showTooltip(bin)}
onBlur={() => hideTooltip()}
>
<rect
x={xAccessorScaled(bin)}
y={yAccessorScaled(bin)}
width={widthAccessorScaled(bin)}
height={heightAccessorScaled(bin)}
fill="hsl(221deg 98% 67%)"
/>
<text
x={midpointAccessorScaled(bin)}
y={yAccessorScaled(bin) - 5}
textAnchor="middle"
fill="hsl(0deg 0% 40%)"
fontSize="12px"
>
{yAccessor(bin)}
</text>
</g>
))}
</g>
{/* Step 6. Draw peripherals */}
<line
x1={xScale(mean)}
x2={xScale(mean)}
y1={-15}
y2={dimensions.boundedHeight}
stroke="maroon"
strokeDasharray="2px 4px"
/>
<text
role="presentation"
aria-hidden={true}
x={xScale(mean)}
y={-20}
textAnchor="middle"
fill="maroon"
fontSize="12px"
>
mean
</text>
<Axis dimension="x" scale={xScale} label="Humidity" />
</Chart>
{/* Step 7b. Create interactions */}
<div
className={styles.tooltip}
style={{
opacity: tooltip.show ? 1 : 0,
transform: `translate(calc(${tooltip.coords.x}px - 50%), calc(${tooltip.coords.y}px - 100%))`,
}}
>
<div className={styles.tooltipRange}>
Humidity:{" "}
<span id="range">
{tooltip.range?.min} - {tooltip.range?.max}
</span>
</div>
<div className="tooltipValue">
<span id="count">{tooltip.value}</span> days
</div>
</div>
</div>
</div>
);
}
export default HistogramInteractions;