|
| 1 | +/* eslint no-console: 0 */ |
| 2 | + |
| 3 | +import createScatterplot from '../src'; |
| 4 | +import { saveAsPng } from './utils'; |
| 5 | + |
| 6 | +const canvas = document.querySelector('#canvas'); |
| 7 | +const numPointsEl = document.querySelector('#num-points'); |
| 8 | +const numPointsValEl = document.querySelector('#num-points-value'); |
| 9 | +const pointSizeEl = document.querySelector('#point-size'); |
| 10 | +const pointSizeValEl = document.querySelector('#point-size-value'); |
| 11 | +const opacityEl = document.querySelector('#opacity'); |
| 12 | +const opacityValEl = document.querySelector('#opacity-value'); |
| 13 | +const clickLassoInitiatorEl = document.querySelector('#click-lasso-initiator'); |
| 14 | +const resetEl = document.querySelector('#reset'); |
| 15 | +const exportEl = document.querySelector('#export'); |
| 16 | +const exampleEl = document.querySelector('#example-basic'); |
| 17 | + |
| 18 | +const canvasWrapper = canvas.parentNode; |
| 19 | +canvasWrapper.style.padding = '15rem 0'; |
| 20 | +canvasWrapper.style.height = '100vh'; |
| 21 | +canvasWrapper.parentNode.style.overflow = 'auto'; |
| 22 | + |
| 23 | +exampleEl.setAttribute('class', 'active'); |
| 24 | +exampleEl.removeAttribute('href'); |
| 25 | + |
| 26 | +let points = []; |
| 27 | +let numPoints = 100000; |
| 28 | +let pointSize = 4; |
| 29 | +let opacity = 0.33; |
| 30 | +let selection = []; |
| 31 | + |
| 32 | +const reticleColor = [1, 1, 0.878431373, 0.33]; |
| 33 | + |
| 34 | +const selectHandler = ({ points: selectedPoints }) => { |
| 35 | + selection = selectedPoints; |
| 36 | + if (selection.length === 1) { |
| 37 | + const point = points[selection[0]]; |
| 38 | + console.log( |
| 39 | + `Selected: ${selectedPoints}`, |
| 40 | + `X: ${point[0]}\nY: ${point[1]}\nCategory: ${point[2]}\nValue: ${point[3]}` |
| 41 | + ); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +const deselectHandler = () => { |
| 46 | + selection = []; |
| 47 | +}; |
| 48 | + |
| 49 | +const scatterplot = createScatterplot({ |
| 50 | + canvas, |
| 51 | + pointSize, |
| 52 | + showReticle: true, |
| 53 | + reticleColor, |
| 54 | + lassoInitiator: true, |
| 55 | +}); |
| 56 | + |
| 57 | +exportEl.addEventListener('click', () => saveAsPng(scatterplot)); |
| 58 | + |
| 59 | +console.log(`Scatterplot v${scatterplot.get('version')}`); |
| 60 | + |
| 61 | +scatterplot.subscribe('select', selectHandler); |
| 62 | +scatterplot.subscribe('deselect', deselectHandler); |
| 63 | + |
| 64 | +const generatePoints = (num) => |
| 65 | + new Array(num).fill().map(() => [ |
| 66 | + -0.8 + Math.random() * 1.6, // x |
| 67 | + -0.8 + Math.random() * 1.6, // y |
| 68 | + Math.round(Math.random()), // category |
| 69 | + Math.random(), // value |
| 70 | + ]); |
| 71 | + |
| 72 | +const setNumPoint = (newNumPoints) => { |
| 73 | + numPoints = newNumPoints; |
| 74 | + numPointsEl.value = numPoints; |
| 75 | + numPointsValEl.innerHTML = numPoints; |
| 76 | + points = generatePoints(numPoints); |
| 77 | + scatterplot.draw(points); |
| 78 | +}; |
| 79 | + |
| 80 | +const numPointsInputHandler = (event) => { |
| 81 | + numPointsValEl.innerHTML = `${+event.target |
| 82 | + .value} <em>release to redraw</em>`; |
| 83 | +}; |
| 84 | + |
| 85 | +numPointsEl.addEventListener('input', numPointsInputHandler); |
| 86 | + |
| 87 | +const numPointsChangeHandler = (event) => setNumPoint(+event.target.value); |
| 88 | + |
| 89 | +numPointsEl.addEventListener('change', numPointsChangeHandler); |
| 90 | + |
| 91 | +const setPointSize = (newPointSize) => { |
| 92 | + pointSize = newPointSize; |
| 93 | + pointSizeEl.value = pointSize; |
| 94 | + pointSizeValEl.innerHTML = pointSize; |
| 95 | + scatterplot.set({ pointSize }); |
| 96 | +}; |
| 97 | + |
| 98 | +const pointSizeInputHandler = (event) => setPointSize(+event.target.value); |
| 99 | + |
| 100 | +pointSizeEl.addEventListener('input', pointSizeInputHandler); |
| 101 | + |
| 102 | +const setOpacity = (newOpacity) => { |
| 103 | + opacity = newOpacity; |
| 104 | + opacityEl.value = opacity; |
| 105 | + opacityValEl.innerHTML = opacity; |
| 106 | + scatterplot.set({ opacity }); |
| 107 | +}; |
| 108 | + |
| 109 | +const opacityInputHandler = (event) => setOpacity(+event.target.value); |
| 110 | + |
| 111 | +opacityEl.addEventListener('input', opacityInputHandler); |
| 112 | + |
| 113 | +const clickLassoInitiatorChangeHandler = (event) => { |
| 114 | + scatterplot.set({ |
| 115 | + lassoInitiator: event.target.checked, |
| 116 | + }); |
| 117 | +}; |
| 118 | + |
| 119 | +clickLassoInitiatorEl.addEventListener( |
| 120 | + 'change', |
| 121 | + clickLassoInitiatorChangeHandler |
| 122 | +); |
| 123 | +clickLassoInitiatorEl.checked = scatterplot.get('lassoInitiator'); |
| 124 | + |
| 125 | +const resetClickHandler = () => { |
| 126 | + scatterplot.reset(); |
| 127 | +}; |
| 128 | + |
| 129 | +resetEl.addEventListener('click', resetClickHandler); |
| 130 | + |
| 131 | +setPointSize(pointSize); |
| 132 | +setOpacity(opacity); |
| 133 | +setNumPoint(numPoints); |
0 commit comments