Skip to content

Commit dc63143

Browse files
authored
word similarity graph #88 (#102)
* added faint dotted line on x=y * fixed overlapping x-axis label and legend * added function to give overlapping points a small offset * fixed layout of y-axis label * made it so when hover over two words on the graph with the same he/she values (i.e. dots on to of each other) it shows both the words
1 parent 2c50c9a commit dc63143

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

website/src/components/posts/CollegeAdmissions/essay/EssayContainer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export default function EssayContainer() {
221221
Reset The Essay
222222
</div>
223223
</div>
224-
<div>Similarity of Words to He/She</div>
224+
<div style={{ marginTop: "20px" }}>Similarity of Words to He/She</div>
225225
<ScatterPlot wordsList={wordsList} />
226226
</div>
227227
);

website/src/components/posts/CollegeAdmissions/essay/ScatterPlot.js

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ZAxis,
88
Tooltip,
99
Legend,
10-
//Label,
10+
Label,
1111
Scatter,
1212
LabelList,
1313
ResponsiveContainer,
@@ -42,6 +42,45 @@ export default function ScatterPlot(props) {
4242
wordObj.heScore - wordObj.sheScore <= 5 &&
4343
wordObj.heScore - wordObj.sheScore >= -5
4444
);
45+
46+
// use tooltip to collapse words on top off each other into 1 pop-up
47+
const CustomTooltip = ({ active, payload }) => {
48+
if (active && payload && payload.length) {
49+
// Find all words that match the current heScore and sheScore
50+
const { heScore, sheScore } = payload[0].payload;
51+
const matchingWords = selected.filter(
52+
(wordObj) =>
53+
wordObj.heScore === heScore && wordObj.sheScore === sheScore
54+
);
55+
56+
return (
57+
<div
58+
style={{
59+
background: "white",
60+
padding: "10px",
61+
border: "1px solid #ccc",
62+
}}
63+
>
64+
<p>
65+
<strong>He Similarity:</strong> {heScore}%
66+
</p>
67+
<p>
68+
<strong>She Similarity:</strong> {sheScore}%
69+
</p>
70+
<p>
71+
<strong>Words:</strong>
72+
</p>
73+
<ul>
74+
{matchingWords.map((wordObj, index) => (
75+
<li key={index}>{wordObj.word}</li>
76+
))}
77+
</ul>
78+
</div>
79+
);
80+
}
81+
return null;
82+
};
83+
4584
const listOfScatters = [];
4685
//append heLeaning words
4786
listOfScatters.push(
@@ -69,7 +108,7 @@ export default function ScatterPlot(props) {
69108
<ScatterChart
70109
width={500}
71110
height={300}
72-
margin={{ top: 20, right: 20, bottom: 10, left: 10 }}
111+
margin={{ top: 20, right: 20, bottom: 40, left: 30 }}
73112
>
74113
<XAxis
75114
dataKey="heScore"
@@ -78,7 +117,7 @@ export default function ScatterPlot(props) {
78117
type="number"
79118
domain={[0, 100]}
80119
>
81-
{/* <Label value="Similarity to He" position="bottom" offset={10} /> */}
120+
<Label value="Similarity to He" position="bottom" offset={10} />
82121
</XAxis>
83122

84123
<YAxis
@@ -87,12 +126,30 @@ export default function ScatterPlot(props) {
87126
unit="%"
88127
domain={[0, 100]}
89128
>
90-
{/* <Label value="Similarity to She" angle={270} position="left" /> */}
129+
<Label
130+
value="Similarity to She"
131+
angle={270}
132+
position="left"
133+
offset={10}
134+
dy={-60}
135+
/>
91136
</YAxis>
92137
<ZAxis dataKey="word" name="Word" />
93138
<CartesianGrid strokeDasharray="3 3" />
94-
<Tooltip cursor={{ strokeDasharray: "3 3" }} />
95-
<Legend />
139+
<Tooltip
140+
content={<CustomTooltip />}
141+
cursor={{ strokeDasharray: "3 3" }}
142+
/>
143+
<Scatter
144+
data={[
145+
{ heScore: 0, sheScore: 0 },
146+
{ heScore: 100, sheScore: 100 },
147+
]}
148+
line={{ stroke: "#CCCCCC", strokeDasharray: "3 3" }}
149+
fill="none"
150+
shape="none"
151+
/>
152+
<Legend wrapperStyle={{ marginTop: "100px", marginBottom: "-40px" }} />
96153
{listOfScatters}
97154
</ScatterChart>
98155
</ResponsiveContainer>

website/src/components/posts/posts.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
}
1818

1919
.post-container {
20+
background-color: #fff2c3;
2021
border-radius: 0 25px 0 0;
2122
margin-top: 48px;
2223
}

0 commit comments

Comments
 (0)