|
| 1 | +/* eslint no-console: 0 */ |
| 2 | + |
| 3 | +import createScatterplot from '../src'; |
| 4 | + |
| 5 | +const canvas1 = document.querySelector('#canvas'); |
| 6 | +canvas1.style.width = '50%'; |
| 7 | + |
| 8 | +const canvasWrapper = canvas1.parentNode; |
| 9 | + |
| 10 | +const canvas2 = document.createElement('canvas'); |
| 11 | +canvas2.style.position = 'absolute'; |
| 12 | +canvas2.style.left = '50%'; |
| 13 | +canvas2.style.width = '50%'; |
| 14 | +canvas2.style.height = '100%'; |
| 15 | +canvasWrapper.appendChild(canvas2); |
| 16 | + |
| 17 | +const numPointsEl = document.querySelector('#num-points'); |
| 18 | +const numPointsValEl = document.querySelector('#num-points-value'); |
| 19 | +const pointSizeEl = document.querySelector('#point-size'); |
| 20 | +const pointSizeValEl = document.querySelector('#point-size-value'); |
| 21 | +const opacityEl = document.querySelector('#opacity'); |
| 22 | +const opacityValEl = document.querySelector('#opacity-value'); |
| 23 | +const resetEl = document.querySelector('#reset'); |
| 24 | +const exampleEl = document.querySelector('#example-background'); |
| 25 | + |
| 26 | +exampleEl.setAttribute('class', 'active'); |
| 27 | +exampleEl.removeAttribute('href'); |
| 28 | + |
| 29 | +[canvas1, canvas2].forEach((canvas) => { |
| 30 | + let { width, height } = canvas.getBoundingClientRect(); |
| 31 | + |
| 32 | + let points = []; |
| 33 | + let numPoints = 25000; |
| 34 | + let pointSize = 3; |
| 35 | + let opacity = 0.66; |
| 36 | + let selection = []; |
| 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 | + pointSize, |
| 59 | + }); |
| 60 | + |
| 61 | + console.log(`Scatterplot v${scatterplot.get('version')}`); |
| 62 | + |
| 63 | + scatterplot.subscribe('select', selectHandler); |
| 64 | + scatterplot.subscribe('deselect', deselectHandler); |
| 65 | + |
| 66 | + const resizeHandler = () => { |
| 67 | + ({ width, height } = canvas.getBoundingClientRect()); |
| 68 | + scatterplot.set({ width, height }); |
| 69 | + }; |
| 70 | + |
| 71 | + window.addEventListener('resize', resizeHandler); |
| 72 | + |
| 73 | + const generatePoints = (num) => |
| 74 | + new Array(num).fill().map(() => [ |
| 75 | + -1 + Math.random() * 2, // x |
| 76 | + -1 + Math.random() * 2, // y |
| 77 | + Math.round(Math.random()), // category |
| 78 | + Math.random(), // value |
| 79 | + ]); |
| 80 | + |
| 81 | + const setNumPoint = (newNumPoints) => { |
| 82 | + numPoints = newNumPoints; |
| 83 | + numPointsEl.value = numPoints; |
| 84 | + numPointsValEl.innerHTML = numPoints; |
| 85 | + points = generatePoints(numPoints); |
| 86 | + scatterplot.draw(points); |
| 87 | + }; |
| 88 | + |
| 89 | + const numPointsInputHandler = (event) => { |
| 90 | + numPointsValEl.innerHTML = `${+event.target |
| 91 | + .value} <em>release to redraw</em>`; |
| 92 | + }; |
| 93 | + |
| 94 | + numPointsEl.addEventListener('input', numPointsInputHandler); |
| 95 | + |
| 96 | + const numPointsChangeHandler = (event) => setNumPoint(+event.target.value); |
| 97 | + |
| 98 | + numPointsEl.addEventListener('change', numPointsChangeHandler); |
| 99 | + |
| 100 | + const setPointSize = (newPointSize) => { |
| 101 | + pointSize = newPointSize; |
| 102 | + pointSizeEl.value = pointSize; |
| 103 | + pointSizeValEl.innerHTML = pointSize; |
| 104 | + scatterplot.set({ pointSize }); |
| 105 | + }; |
| 106 | + |
| 107 | + const pointSizeInputHandler = (event) => setPointSize(+event.target.value); |
| 108 | + |
| 109 | + pointSizeEl.addEventListener('input', pointSizeInputHandler); |
| 110 | + |
| 111 | + const setOpacity = (newOpacity) => { |
| 112 | + opacity = newOpacity; |
| 113 | + opacityEl.value = opacity; |
| 114 | + opacityValEl.innerHTML = opacity; |
| 115 | + scatterplot.set({ opacity }); |
| 116 | + }; |
| 117 | + |
| 118 | + const opacityInputHandler = (event) => setOpacity(+event.target.value); |
| 119 | + |
| 120 | + opacityEl.addEventListener('input', opacityInputHandler); |
| 121 | + |
| 122 | + const resetClickHandler = () => { |
| 123 | + scatterplot.reset(); |
| 124 | + }; |
| 125 | + |
| 126 | + resetEl.addEventListener('click', resetClickHandler); |
| 127 | + |
| 128 | + scatterplot.set({ colorBy: 'category', pointColor: ['#3a78aa', '#aa3a99'] }); |
| 129 | + |
| 130 | + setPointSize(pointSize); |
| 131 | + setOpacity(opacity); |
| 132 | + setNumPoint(numPoints); |
| 133 | +}); |
0 commit comments