|
| 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); |
0 commit comments