Skip to content

Commit 8aac4e7

Browse files
committed
Add a separate demo for segment encoding
1 parent 2be382e commit 8aac4e7

3 files changed

Lines changed: 220 additions & 3 deletions

File tree

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/* eslint no-console: 0 */
2+
3+
import createScatterplot from '../src';
4+
5+
const canvas = document.querySelector('#canvas');
6+
const numPointsEl = document.querySelector('#num-points');
7+
const numPointsValEl = document.querySelector('#num-points-value');
8+
const pointSizeEl = document.querySelector('#point-size');
9+
const pointSizeValEl = document.querySelector('#point-size-value');
10+
const opacityEl = document.querySelector('#opacity');
11+
const opacityValEl = document.querySelector('#opacity-value');
12+
const clickLassoInitiatorEl = document.querySelector('#click-lasso-initiator');
13+
const resetEl = document.querySelector('#reset');
14+
const exampleEl = document.querySelector(
15+
'#example-connected-points-by-segment'
16+
);
17+
18+
exampleEl.setAttribute('class', 'active');
19+
exampleEl.removeAttribute('href');
20+
21+
let { width, height } = canvas.getBoundingClientRect();
22+
23+
let points = [];
24+
let numPoints = 9000;
25+
let pointSize = 1;
26+
let opacity = 0.33;
27+
let selection = [];
28+
29+
const pointSizeMax = 10;
30+
const lassoMinDelay = 10;
31+
const lassoMinDist = 2;
32+
const showRecticle = true;
33+
const recticleColor = [1, 1, 0.878431373, 0.33];
34+
const showPointConnections = true;
35+
const pointConnectionColor = [1, 1, 1, 0.15];
36+
const pointConnectionSize = 2;
37+
38+
const selectHandler = ({ points: selectedPoints }) => {
39+
console.log('Selected:', selectedPoints);
40+
selection = selectedPoints;
41+
if (selection.length === 1) {
42+
const point = points[selection[0]];
43+
console.log(
44+
`X: ${point[0]}\nY: ${point[1]}\nCategory: ${point[2]}\nValue: ${point[3]}`
45+
);
46+
}
47+
};
48+
49+
const deselectHandler = () => {
50+
console.log('Deselected:', selection);
51+
selection = [];
52+
};
53+
54+
const scatterplot = createScatterplot({
55+
canvas,
56+
width,
57+
height,
58+
lassoMinDelay,
59+
lassoMinDist,
60+
pointSize,
61+
showRecticle,
62+
recticleColor,
63+
showPointConnections,
64+
pointConnectionColor,
65+
pointConnectionSize,
66+
});
67+
68+
console.log(`Scatterplot v${scatterplot.get('version')}`);
69+
70+
scatterplot.subscribe('select', selectHandler);
71+
scatterplot.subscribe('deselect', deselectHandler);
72+
73+
const resizeHandler = () => {
74+
({ width, height } = canvas.getBoundingClientRect());
75+
scatterplot.set({ width, height });
76+
};
77+
78+
window.addEventListener('resize', resizeHandler);
79+
80+
const generatePoints = (num) => {
81+
const numPointsPerGroup = Math.round(num / 3); // 9.000 / 3 => 3000
82+
const numPointsPerStep = Math.round(numPointsPerGroup / 5); // 3000 / 5 => 600
83+
// 600 * 3 => 1800 (lines)
84+
85+
const outPoints = [];
86+
87+
for (let g = 0; g < 3; g++) {
88+
for (let i = 0; i < numPointsPerStep; i++) {
89+
for (let s = 0; s < 5; s++) {
90+
outPoints.push([
91+
-0.6 + g * 0.6 + (Math.random() * 0.3 - 0.15), // x
92+
-0.9 + s * 0.45, // y
93+
g, // category
94+
Math.random(), // value
95+
g * numPointsPerStep + i, // to identify connected points
96+
]);
97+
}
98+
}
99+
}
100+
101+
return outPoints;
102+
};
103+
104+
const setNumPoint = (newNumPoints) => {
105+
numPoints = newNumPoints;
106+
numPointsEl.value = numPoints;
107+
numPointsValEl.innerHTML = numPoints;
108+
points = generatePoints(numPoints);
109+
scatterplot.draw(points);
110+
};
111+
112+
const numPointsInputHandler = (event) => {
113+
numPointsValEl.innerHTML = `${+event.target
114+
.value} <em>release to redraw</em>`;
115+
};
116+
117+
numPointsEl.addEventListener('input', numPointsInputHandler);
118+
119+
const numPointsChangeHandler = (event) => setNumPoint(+event.target.value);
120+
121+
numPointsEl.addEventListener('change', numPointsChangeHandler);
122+
123+
const getPointSizeRange = (basePointSize) =>
124+
Array(100)
125+
.fill()
126+
.map((x, i) => 1 + (i / 99) * (basePointSize * pointSizeMax - 1));
127+
128+
const setPointSize = (newPointSize) => {
129+
pointSize = newPointSize;
130+
pointSizeEl.value = pointSize;
131+
pointSizeValEl.innerHTML = pointSize;
132+
scatterplot.set({ pointSize: getPointSizeRange(pointSize) });
133+
};
134+
135+
const pointSizeInputHandler = (event) => setPointSize(+event.target.value);
136+
137+
pointSizeEl.addEventListener('input', pointSizeInputHandler);
138+
139+
const setOpacity = (newOpacity) => {
140+
opacity = newOpacity;
141+
opacityEl.value = opacity;
142+
opacityValEl.innerHTML = opacity;
143+
scatterplot.set({ opacity });
144+
};
145+
146+
const opacityInputHandler = (event) => setOpacity(+event.target.value);
147+
148+
opacityEl.addEventListener('input', opacityInputHandler);
149+
150+
const clickLassoInitiatorChangeHandler = (event) => {
151+
scatterplot.set({
152+
lassoInitiator: event.target.checked,
153+
});
154+
};
155+
156+
clickLassoInitiatorEl.addEventListener(
157+
'change',
158+
clickLassoInitiatorChangeHandler
159+
);
160+
161+
const resetClickHandler = () => {
162+
scatterplot.reset();
163+
};
164+
165+
resetEl.addEventListener('click', resetClickHandler);
166+
167+
scatterplot.set({
168+
colorBy: 'valueZ',
169+
sizeBy: 'valueW',
170+
pointColor: [
171+
[255, 128, 203, 128],
172+
[87, 199, 255, 128],
173+
[238, 228, 98, 128],
174+
],
175+
pointConnectionColorBy: 'segment',
176+
pointConnectionColor: Array(200)
177+
.fill()
178+
.map((v, i) => [i + 55, i + 55, i + 55, 10]),
179+
pointConnectionSizeBy: 'segment',
180+
pointConnectionSize: Array(20)
181+
.fill()
182+
.map((v, i) => 1 + i / 4),
183+
pointConnectionOpacityBy: 'segment',
184+
pointConnectionOpacity: Array(64)
185+
.fill()
186+
.map((v, i) => (i + 8) / 256),
187+
});
188+
189+
setPointSize(pointSize);
190+
setOpacity(opacity);
191+
setNumPoint(numPoints);

example/index.html

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@
176176
text-decoration-color: rgba(255, 255, 255, 1);
177177
}
178178

179+
.smaller {
180+
font-size: 0.9em;
181+
}
182+
179183
.flex-c {
180184
display: flex;
181185
}
@@ -306,7 +310,9 @@
306310
}
307311

308312
#examples li a.active {
309-
font-style: italic;
313+
color: #80c8ff;
314+
font-weight: bold;
315+
text-decoration: none;
310316
}
311317

312318
#num-points-value {
@@ -440,7 +446,16 @@ <h1 id="title">Regl Scatterplot</h1>
440446
</li>
441447
<li>
442448
<a id="example-connected-points" href="connected-points.html"
443-
>Connected Points</a
449+
>Point Connections</a
450+
>
451+
</li>
452+
<li>
453+
<a
454+
id="example-connected-points-by-segment"
455+
href="connected-points-by-segment.html"
456+
>Point Connections<br /><span class="smaller"
457+
>(encoded by line segment)</span
458+
></a
444459
>
445460
</li>
446461
<li>
@@ -450,7 +465,12 @@ <h1 id="title">Regl Scatterplot</h1>
450465
</li>
451466
</ul>
452467
<hr />
453-
<a class="button-like" href="https://github.com/flekschas/regl-scatterplot" target="_blank">Source code</a>
468+
<a
469+
class="button-like"
470+
href="https://github.com/flekschas/regl-scatterplot"
471+
target="_blank"
472+
>Source code</a
473+
>
454474
</aside>
455475
</header>
456476
<div id="parent-wrapper">

webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = (env, argv) => ({
99
transition: './example/transition.js',
1010
sizeEncoding: './example/size-encoding.js',
1111
connectedPoints: './example/connected-points.js',
12+
connectedPointsBySegment: './example/connected-points-by-segments.js',
1213
performanceMode: './example/performance-mode.js',
1314
},
1415
output: {
@@ -66,6 +67,11 @@ module.exports = (env, argv) => ({
6667
filename: 'connected-points.html',
6768
chunks: ['connectedPoints'],
6869
}),
70+
new HtmlWebpackPlugin({
71+
template: 'example/index.html',
72+
filename: 'connected-points-by-segment.html',
73+
chunks: ['connectedPointsBySegment'],
74+
}),
6975
new HtmlWebpackPlugin({
7076
template: 'example/index.html',
7177
filename: 'performance-mode.html',

0 commit comments

Comments
 (0)