-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-sigma.jsx
More file actions
110 lines (97 loc) · 3.29 KB
/
Copy pathdemo-sigma.jsx
File metadata and controls
110 lines (97 loc) · 3.29 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
import React, { useState, useMemo } from 'react';
import { createRoot } from 'react-dom/client';
import DotVisualizationSigma from './src/DotVisualizationSigma.jsx';
const App = () => {
const [hoveredDot, setHoveredDot] = useState(null);
const [clickedDot, setClickedDot] = useState(null);
const [dotStyles, setDotStyles] = useState(new Map());
// Generate random data in normalized coordinates (Sigma works well with this range)
const data = useMemo(() => {
return Array.from({ length: 150 }, (_, i) => ({
id: i,
x: (Math.random() - 0.5) * 100, // Range from -50 to 50
y: (Math.random() - 0.5) * 100, // Range from -50 to 50
color: `#${Math.floor(Math.random()*16777215).toString(16).padStart(6, '0')}`,
name: `Point ${i}`,
value: Math.round(Math.random() * 100),
size: 3 + Math.random() * 7 // Random size between 3-10
}));
}, []);
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, {
color: '#ccc',
});
} else {
newStyles.set(dot.id, {
color: item.color,
size: 15
});
}
});
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 Sigma Demo</h1>
<div className="sigma-info">
<strong>✨ Sigma.js Version!</strong><br/>
This version uses Sigma.js for WebGL rendering with built-in zoom/pan controls.
Much simpler code and better performance for large datasets.
</div>
<div className="instructions">
<strong>🎯 Try the features!</strong><br/>
• <strong>Click a dot:</strong> Highlights selected dot and dims others<br/>
• <strong>Click background:</strong> Resets all styles to original colors<br/>
• <strong>Zoom:</strong> Mouse wheel or trackpad pinch (built-in Sigma controls)<br/>
• <strong>Pan:</strong> Click and drag to pan around<br/>
• <strong>Hover:</strong> Move mouse over dots
</div>
<div className="viz">
<DotVisualizationSigma
data={data}
onHover={setHoveredDot}
onLeave={() => setHoveredDot(null)}
onClick={handleClick}
onBackgroundClick={handleBackgroundClick}
dotStyles={dotStyles}
defaultSize={5}
/>
</div>
{hoveredDot && (
<div className="hover-info">
{hoveredDot.name}: {hoveredDot.value}
</div>
)}
{clickedDot && (
<div style={{
position: 'fixed',
bottom: '20px',
left: '20px',
background: 'rgba(0,0,0,0.9)',
color: 'white',
padding: '10px',
borderRadius: '4px',
fontSize: '12px',
zIndex: 1000
}}>
Selected: {clickedDot.name} (Value: {clickedDot.value})
</div>
)}
</div>
);
};
// Render
const root = createRoot(document.getElementById('root'));
root.render(<App />);