-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathprogrammatic-lasso.js
More file actions
293 lines (253 loc) · 6.98 KB
/
Copy pathprogrammatic-lasso.js
File metadata and controls
293 lines (253 loc) · 6.98 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { axisBottom, axisRight } from 'd3-axis';
import { scaleLinear } from 'd3-scale';
import { select } from 'd3-selection';
import createScatterplot from '../src';
import createMenu from './menu';
import { checkSupport } from './utils';
const parentWrapper = document.querySelector('#parent-wrapper');
const canvasWrapper = document.querySelector('#canvas-wrapper');
const canvas = document.querySelector('#canvas');
// Create button container with grid layout
const buttonContainer = document.createElement('div');
buttonContainer.style.cssText = `
position: absolute;
top: 10px;
left: 10px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
max-width: 400px;
max-height: calc(100vh - 20px);
overflow-y: auto;
z-index: 1000;
`;
parentWrapper.appendChild(buttonContainer);
const xDomain = [0, 100];
const yDomain = [0, 100];
const xScale = scaleLinear().domain(xDomain);
const yScale = scaleLinear().domain(yDomain);
const xAxis = axisBottom(xScale);
const yAxis = axisRight(yScale);
const axisContainer = select(parentWrapper).append('svg');
const xAxisContainer = axisContainer.append('g');
const yAxisContainer = axisContainer.append('g');
const xAxisPadding = 20;
const yAxisPadding = 40;
axisContainer.node().style.position = 'absolute';
axisContainer.node().style.top = 0;
axisContainer.node().style.left = 0;
axisContainer.node().style.width = '100%';
axisContainer.node().style.height = '100%';
axisContainer.node().style.pointerEvents = 'none';
canvasWrapper.style.right = `${yAxisPadding}px`;
canvasWrapper.style.bottom = `${xAxisPadding}px`;
let { width, height } = canvasWrapper.getBoundingClientRect();
xAxisContainer.attr('transform', `translate(0, ${height})`).call(xAxis);
yAxisContainer.attr('transform', `translate(${width}, 0)`).call(yAxis);
// Render grid
xAxis.tickSizeInner(-height);
yAxis.tickSizeInner(-width);
let points = [];
let numPoints = 5000;
let pointSize = 4;
let opacity = 0.66;
let selection = [];
const selectHandler = ({ points: selectedPoints }) => {
console.log('Selected:', selectedPoints.length, 'points');
selection = selectedPoints;
};
const deselectHandler = () => {
console.log('Deselected');
selection = [];
};
const scatterplot = createScatterplot({
canvas,
pointSize,
opacity,
xScale,
yScale,
showReticle: true,
lassoInitiator: true,
pointColor: [0.33, 0.5, 1, 1],
pointColorActive: [1, 0.5, 0, 1],
});
checkSupport(scatterplot);
console.log(`Scatterplot v${scatterplot.get('version')}`);
scatterplot.subscribe('select', selectHandler);
scatterplot.subscribe('deselect', deselectHandler);
scatterplot.subscribe('view', (event) => {
xAxisContainer.call(xAxis.scale(event.xScale));
yAxisContainer.call(yAxis.scale(event.yScale));
});
scatterplot.subscribe(
'init',
() => {
xAxisContainer.call(xAxis.scale(scatterplot.get('xScale')));
yAxisContainer.call(yAxis.scale(scatterplot.get('yScale')));
},
1
);
const resizeHandler = () => {
({ width, height } = canvasWrapper.getBoundingClientRect());
xAxisContainer.attr('transform', `translate(0, ${height})`).call(xAxis);
yAxisContainer.attr('transform', `translate(${width}, 0)`).call(yAxis);
// Render grid
xAxis.tickSizeInner(-height);
yAxis.tickSizeInner(-width);
};
window.addEventListener('resize', resizeHandler);
window.addEventListener('orientationchange', resizeHandler);
// Generate points in DATA SPACE (not NDC)
const generatePoints = (num) => {
const pts = [];
for (let i = 0; i < num; i++) {
const x = Math.random() * 100; // 0 to 100 (data space)
const y = Math.random() * 100; // 0 to 100 (data space)
// Convert to NDC for scatterplot
const xNdc = (x / 100) * 2 - 1;
const yNdc = (y / 100) * 2 - 1;
pts.push([
xNdc,
yNdc,
Math.round(Math.random() * 4), // category
Math.random(), // value
x, // store original x for reference
y, // store original y for reference
]);
}
return pts;
};
const setNumPoints = (newNumPoints) => {
points = generatePoints(newNumPoints);
scatterplot.draw(points);
};
createMenu({ scatterplot, setNumPoints });
scatterplot.set({
colorBy: 'category',
pointColor: [
'#3a84cc',
'#56bf92',
'#eecb62',
'#c76526',
'#d192b7',
],
});
// Helper function to create a button
const createButton = (label, onClick, wide = false) => {
const btn = document.createElement('button');
btn.textContent = label;
btn.style.cssText = `
padding: 6px 10px;
background: #3a84cc;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 11px;
white-space: nowrap;
text-align: center;
${wide ? 'grid-column: 1 / -1;' : ''}
`;
btn.addEventListener('mouseenter', () => {
btn.style.background = '#2a6cb0';
});
btn.addEventListener('mouseleave', () => {
btn.style.background = '#3a84cc';
});
btn.addEventListener('click', onClick);
return btn;
};
// Button 1: Select bottom-left triangle
buttonContainer.appendChild(
createButton('△ Bottom-Left', () => {
scatterplot.lassoSelect([
[10, 10],
[40, 10],
[10, 40],
]);
})
);
// Button 2: Select top-right circle (approximated by polygon)
buttonContainer.appendChild(
createButton('○ Top-Right', () => {
const cx = 75;
const cy = 75;
const radius = 20;
const sides = 16;
const polygon = [];
for (let i = 0; i < sides; i++) {
const angle = (i / sides) * Math.PI * 2;
polygon.push([
cx + Math.cos(angle) * radius,
cy + Math.sin(angle) * radius,
]);
}
scatterplot.lassoSelect(polygon);
})
);
// Button 3: Select center rectangle
buttonContainer.appendChild(
createButton('▭ Center', () => {
scatterplot.lassoSelect([
[30, 30],
[70, 30],
[70, 70],
[30, 70],
]);
})
);
// Button 4: Add diagonal stripe (merge)
buttonContainer.appendChild(
createButton('+ Diagonal (Merge)', () => {
scatterplot.lassoSelect(
[
[0, 40],
[60, 100],
[70, 100],
[10, 40],
],
{ merge: true }
);
})
);
// Button 5: Remove center square
buttonContainer.appendChild(
createButton('− Center (Remove)', () => {
scatterplot.lassoSelect(
[
[40, 40],
[60, 40],
[60, 60],
[40, 60],
],
{ remove: true }
);
})
);
// Button 6: Star shape
buttonContainer.appendChild(
createButton('★ Star', () => {
const cx = 50;
const cy = 50;
const outerRadius = 30;
const innerRadius = 15;
const points = 5;
const polygon = [];
for (let i = 0; i < points * 2; i++) {
const angle = (i / (points * 2)) * Math.PI * 2 - Math.PI / 2;
const radius = i % 2 === 0 ? outerRadius : innerRadius;
polygon.push([
cx + Math.cos(angle) * radius,
cy + Math.sin(angle) * radius,
]);
}
scatterplot.lassoSelect(polygon);
})
);
// Button 7: Deselect all (wide button)
buttonContainer.appendChild(
createButton('✕ Deselect All', () => {
scatterplot.deselect();
}, true)
);
setNumPoints(numPoints);