-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.jsx
More file actions
95 lines (83 loc) · 2.84 KB
/
demo.jsx
File metadata and controls
95 lines (83 loc) · 2.84 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
import React, { useState, useMemo, useRef, useEffect } from 'react';
import { createRoot } from 'react-dom/client';
import DotVisualization from './src/DotVisualization.jsx';
const App = () => {
const [hoveredDot, setHoveredDot] = useState(null);
const [clickedDot, setClickedDot] = useState(null);
const [dotStyles, setDotStyles] = useState(new Map());
const [containerSize, setContainerSize] = useState({ width: 640, height: 400 });
const containerRef = useRef(null);
// Measure container size once
useEffect(() => {
if (containerRef.current) {
const rect = containerRef.current.getBoundingClientRect();
setContainerSize({ width: rect.width, height: rect.height });
}
}, []);
// Generate random data that fills the actual container
const data = useMemo(() => {
return Array.from({ length: 150 }, (_, i) => ({
id: i,
x: Math.random() * containerSize.width,
y: Math.random() * containerSize.height,
color: `hsl(${Math.random() * 360}, 70%, 50%)`,
name: `Point ${i}`,
value: Math.round(Math.random() * 100)
}));
}, [containerSize]);
const handleClick = (item) => {
setClickedDot(item);
console.log('Clicked item:', item);
// Desaturate all other dots
const newStyles = new Map();
data.forEach(dot => {
if (dot.id !== item.id) {
newStyles.set(dot.id, {
fill: '#ccc',
stroke: '#999',
'stroke-width': '0.5'
});
}
});
setDotStyles(newStyles);
};
const handleBackgroundClick = () => {
setClickedDot(null);
console.log('Clicked background - resetting styles');
// Reset all styles by clearing the map
setDotStyles(new Map());
};
return (
<div className="demo">
<h1>React Dot Visualization Demo</h1>
<div className="instructions">
<strong>🎯 Try the new features!</strong><br/>
• <strong>Click a dot:</strong> Desaturates all other dots<br/>
• <strong>Click background:</strong> Resets all styles to original colors<br/>
• <strong>Zoom:</strong> Ctrl/Cmd + mouse wheel (or trackpad pinch)<br/>
• <strong>Pan:</strong> Mouse wheel or trackpad scroll<br/>
• <strong>Hover:</strong> Move mouse over dots
</div>
<div className="viz" ref={containerRef}>
<DotVisualization
data={data}
onHover={setHoveredDot}
onLeave={() => setHoveredDot(null)}
onClick={handleClick}
onBackgroundClick={handleBackgroundClick}
dotStyles={dotStyles}
defaultSize={10}
margin={0.05}
/>
</div>
{hoveredDot && (
<div className="hover-info">
{hoveredDot.name}: {hoveredDot.value}
</div>
)}
</div>
);
};
// Render
const root = createRoot(document.getElementById('root'));
root.render(<App />);