-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsize-encoding.js
More file actions
133 lines (108 loc) · 3.23 KB
/
size-encoding.js
File metadata and controls
133 lines (108 loc) · 3.23 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
import { scaleLog } from 'd3-scale';
import { randomExponential } from 'd3-random';
import createScatterplot from '../src';
import createMenu from './menu';
import { checkSupport } from './utils';
const canvas = document.querySelector('#canvas');
let points = [];
let numPoints = 100000;
let pointSize = 2;
let opacity = 1.0;
let selection = [];
const lassoMinDelay = 10;
const lassoMinDist = 2;
const showReticle = true;
const reticleColor = [1, 1, 0.878431373, 0.33];
const selectHandler = ({ points: selectedPoints }) => {
console.log('Selected:', selectedPoints);
selection = selectedPoints;
if (selection.length === 1) {
const point = points[selection[0]];
console.log(
`X: ${point[0]}\nY: ${point[1]}\nCategory: ${point[2]}\nValue: ${point[3]}`
);
}
};
const deselectHandler = () => {
console.log('Deselected:', selection);
selection = [];
};
const scatterplot = createScatterplot({
canvas,
lassoMinDelay,
lassoMinDist,
pointSize,
opacity,
showReticle,
reticleColor,
lassoInitiator: true,
opacityInactiveScale: 0.66,
});
checkSupport(scatterplot);
console.log(`Scatterplot v${scatterplot.get('version')}`);
scatterplot.subscribe('select', selectHandler);
scatterplot.subscribe('deselect', deselectHandler);
const rndA = randomExponential(2);
const rndB = randomExponential(4);
const rndC = randomExponential(5);
const generatePoints = (num) => {
const newPoints = [
...new Array(Math.round((num * 2) / 12)).fill().map(() => [
-1 + (Math.random() * 2 * 1) / 3, // x
-1 + Math.random() * 2, // y
0, // category
rndA(), // value
]),
...new Array(Math.round((num * 4) / 12)).fill().map(() => [
-1 + 2 / 3 + (Math.random() * 2 * 1) / 3, // x
-1 + Math.random() * 2, // y
1, // category
rndB(), // value
]),
...new Array(Math.round((num * 6) / 12)).fill().map(() => [
-1 + 4 / 3 + (Math.random() * 2 * 1) / 3, // x
-1 + Math.random() * 2, // y
2, // category
rndC(), // value
]),
];
const [minVal, maxVal] = newPoints.reduce(
([min, max], point) => [Math.min(min, point[3]), Math.max(max, point[3])],
[Infinity, -Infinity]
);
const valRange = maxVal - minVal;
newPoints.forEach((point) => {
point[3] = (point[3] - minVal) / valRange;
});
return newPoints;
};
const setNumPoints = (newNumPoints) => {
points = generatePoints(newNumPoints);
scatterplot.draw(points);
};
const getPointSizeRange = (basePointSize) => {
const pointSizeScale = scaleLog()
.domain([1, 10])
.range([basePointSize, basePointSize * 10]);
return Array.from(
{ length: 100 }, (_, i) => pointSizeScale(1 + (i / 99) * 9)
);
};
const setPointSize = (newPointSize) => {
scatterplot.set({ pointSize: getPointSizeRange(newPointSize) });
};
const getOpacityRange = (baseOpacity) =>
Array.from({ length: 10 }, (_, i) => ((i + 1) / 10) * baseOpacity);
const setOpacity = (newOpacity) => {
scatterplot.set({ opacity: getOpacityRange(newOpacity) });
};
createMenu({ scatterplot, setNumPoints, setPointSize, setOpacity });
scatterplot.set({
colorBy: 'category',
pointColor: ['#ff80cb', '#57c7ff', '#eee462'],
sizeBy: 'w',
opacityBy: 'w',
});
setPointSize(pointSize);
setOpacity(opacity);
setNumPoints(numPoints);