-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordCloud.tsx
More file actions
73 lines (64 loc) · 1.66 KB
/
WordCloud.tsx
File metadata and controls
73 lines (64 loc) · 1.66 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
import { Dialog } from '@mui/material';
import { scaleLog } from '@visx/scale';
import { Wordcloud as VWordCloud } from '@visx/wordcloud';
import { ANALYTICS_WORDS_CLOUD_MODAL_ID } from '@/config/selectors';
type WordData = {
text: string;
value: number;
};
const colors = ['#143059', '#2F6B9A', '#82a6c2'];
const fixedValueGenerator = () => 0.5;
type Props = {
wordCounts: { [key: string]: number };
open: boolean;
onClose: () => void;
};
function WordCloud({
wordCounts,
open,
onClose,
}: Readonly<Props>): JSX.Element {
const words = Object.entries(wordCounts).map(([text, value]) => ({
text,
value,
}));
const fontScale = scaleLog({
domain: [
Math.min(...words.map((w) => w.value)),
Math.max(...words.map((w) => w.value)),
],
range: [20, 50],
});
const fontSizeSetter = (datum: WordData) => fontScale(datum.value);
return (
<Dialog open={open} onClose={onClose} id={ANALYTICS_WORDS_CLOUD_MODAL_ID}>
<VWordCloud
words={words}
width={600}
height={400}
fontSize={fontSizeSetter}
font="Nunito"
padding={2}
spiral="archimedean"
rotate={0}
random={fixedValueGenerator}
>
{(cloudWords) =>
cloudWords.map((w, i) => (
<text
key={w.text}
color={colors[i % colors.length]}
textAnchor={'middle'}
transform={`translate(${w.x}, ${w.y}) rotate(${w.rotate})`}
fontSize={w.size}
fontFamily={w.font}
>
{w.text}
</text>
))
}
</VWordCloud>
</Dialog>
);
}
export default WordCloud;