Skip to content

Commit 89542b2

Browse files
committed
🔄 refactor: move chart plotting code to a separate directory for cleaner code
1 parent 1e287ca commit 89542b2

File tree

4 files changed

+242
-237
lines changed

4 files changed

+242
-237
lines changed

‎client/src/components/charts/ContestsUserNumStackedArea.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ReactEcharts from "echarts-for-react";
22

33
const ContestsUserNumStackedArea = ({ contests }) => {
44
contests.sort((a, b) => new Date(a.startTime) - new Date(b.startTime));
5-
console.log(contests);
5+
// console.log(contests);
66
const titles = contests.map((contest) =>
77
contest.title.replace(/eekly Contest /g, "")
88
);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import ReactEcharts from "echarts-for-react";
2+
3+
const QuestionFinishedChart = ({ questionsRaw }) => {
4+
// console.log("QuestionFinishedChart questionsRaw=", questionsRaw);
5+
const questions = [...questionsRaw].sort((a, b) =>
6+
a.credit === b.credit ? a.qi - b.qi : a.credit - b.credit
7+
);
8+
9+
const real_time_count = [["Minute", "Question", "Count"]];
10+
for (let i = 1; i <= questions.length; i++) {
11+
for (let j = 1; j <= questions[0].real_time_count?.length; j++) {
12+
real_time_count.push([
13+
j,
14+
`Q${i}`,
15+
questions[i - 1].real_time_count[j - 1],
16+
]);
17+
}
18+
}
19+
20+
const questionsId = ["Q1", "Q2", "Q3", "Q4"];
21+
const datasetWithFilters = [];
22+
const seriesList = [];
23+
24+
questionsId.forEach((id) => {
25+
const datasetId = "dataset_" + id;
26+
datasetWithFilters.push({
27+
id: datasetId,
28+
fromDatasetId: "dataset_raw",
29+
transform: {
30+
type: "filter",
31+
config: {
32+
and: [{ dimension: "Question", "=": id }],
33+
},
34+
},
35+
});
36+
seriesList.push({
37+
type: "line",
38+
datasetId: datasetId,
39+
showSymbol: false,
40+
name: id,
41+
endLabel: {
42+
show: true,
43+
formatter: function (params) {
44+
return params.value[1] + ": " + params.value[2];
45+
},
46+
},
47+
labelLayout: {
48+
moveOverlap: "shiftY",
49+
},
50+
emphasis: {
51+
focus: "series",
52+
},
53+
encode: {
54+
x: "Minute",
55+
y: "Count",
56+
label: ["Question", "Count"],
57+
itemName: "Minute",
58+
tooltip: ["Count"],
59+
},
60+
});
61+
});
62+
63+
const option = {
64+
animation: true,
65+
animationDuration: 10000,
66+
dataset: [
67+
{
68+
id: "dataset_raw",
69+
source: real_time_count,
70+
},
71+
...datasetWithFilters,
72+
],
73+
title: {
74+
text: "Question Finished Count",
75+
x: "center",
76+
},
77+
tooltip: {
78+
order: "valueDesc",
79+
trigger: "axis",
80+
},
81+
xAxis: {
82+
type: "category",
83+
name: "Minute",
84+
},
85+
yAxis: {
86+
name: "Accepted",
87+
// axisLabel: {
88+
// rotate: 45,
89+
// margin: 1
90+
// }
91+
},
92+
grid: {
93+
left: "70em",
94+
right: "70em",
95+
},
96+
series: seriesList,
97+
};
98+
99+
// console.log("question option= ", option);
100+
return (
101+
<ReactEcharts
102+
option={option}
103+
// theme="dark"
104+
// lazyUpdate={true}
105+
// opts={{renderer: "svg"}}
106+
style={{
107+
height: "25em",
108+
}}
109+
/>
110+
);
111+
};
112+
113+
export default QuestionFinishedChart;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import ReactEcharts from "echarts-for-react";
2+
3+
const RealTimeRankChart = ({ user, rankList }) => {
4+
if (!rankList) return null;
5+
6+
const realTimeRank = [["Minute", "Username", "Rank"]];
7+
for (let j = 1; j <= rankList.length; j++) {
8+
realTimeRank.push([j, user.username, rankList[j - 1]]);
9+
}
10+
11+
const users = [user.username];
12+
const datasetWithFilters = [];
13+
const seriesList = [];
14+
15+
// console.log("users", users);
16+
// console.log("realTimeRank", realTimeRank);
17+
18+
users.forEach((username) => {
19+
const datasetId = "dataset_" + username;
20+
datasetWithFilters.push({
21+
id: datasetId,
22+
fromDatasetId: "dataset_raw",
23+
transform: {
24+
type: "filter",
25+
config: {
26+
and: [{ dimension: "Username", "=": username }],
27+
},
28+
},
29+
});
30+
seriesList.push({
31+
type: "line",
32+
datasetId: datasetId,
33+
showSymbol: false,
34+
name: username,
35+
endLabel: {
36+
show: true,
37+
formatter: function (params) {
38+
return params.value[1] + ": " + params.value[2];
39+
},
40+
},
41+
labelLayout: {
42+
moveOverlap: "shiftY",
43+
},
44+
emphasis: {
45+
focus: "series",
46+
},
47+
encode: {
48+
x: "Minute",
49+
y: "Rank",
50+
label: ["Username", "Rank"],
51+
itemName: "Minute",
52+
tooltip: ["Rank"],
53+
},
54+
});
55+
});
56+
57+
const option = {
58+
animationDuration: 10000,
59+
dataset: [
60+
{
61+
id: "dataset_raw",
62+
source: realTimeRank,
63+
},
64+
...datasetWithFilters,
65+
],
66+
title: {
67+
text: "User Real Time Rank",
68+
x: "center",
69+
},
70+
tooltip: {
71+
order: "valueDesc",
72+
trigger: "axis",
73+
},
74+
xAxis: {
75+
type: "category",
76+
name: "Minute",
77+
},
78+
yAxis: {
79+
name: "Rank",
80+
axisLabel: {
81+
rotate: 45,
82+
margin: 1,
83+
},
84+
},
85+
grid: {
86+
// left:"70em",
87+
right: "70em",
88+
},
89+
series: seriesList,
90+
};
91+
92+
// console.log("realTimeRank option=", option);
93+
94+
return (
95+
<ReactEcharts
96+
option={option}
97+
// theme="dark"
98+
// lazyUpdate={true}
99+
// opts={{renderer: "svg"}}
100+
style={{ height: "25em" }}
101+
/>
102+
);
103+
};
104+
105+
export default RealTimeRankChart;

0 commit comments

Comments
 (0)