-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekly.js
144 lines (115 loc) · 4.75 KB
/
weekly.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function init() {
// Grab a reference to the dropdown week selection
const weekSelector = d3.select("#selWeeks");
// Use the list of weeks to populate the select options
d3.json("https://raw.githubusercontent.com/weihaolun/Twitter-Sentiment-Analysis/main/Visualization/data_for_visualization/weekly_tweets_counts.json").then((data) => {
console.log("this is the weekly count data", data);
// Roll data by weeks
const dataByWeeks = d3.nest()
.key(function (d) { return d.weeks; })
.entries(data);
console.log("this is rolled by weeks", dataByWeeks);
// Create an array to retrieve weeks names
const weeksNames = [];
for (let i =0; i < dataByWeeks.length; i++) {
weeksNames.push(dataByWeeks[i].key);
}
weeksNames.forEach((weeks) => {
weekSelector
.append("option")
.text(weeks)
.property("value", weeks);
});
// Use the first week from the list to build the initial plots
const firstWeek = weeksNames[0];
// buildWeekdata(fisrtweek)
buildWeekdata(firstWeek);
})
}
init();
// Initialize the weekly dashboard
function weekChanged(newWeek) {
// Fetch new data each time a new sample is selected
buildWeekdata(newWeek);
};
// Data Panel
function buildWeekdata(weeks) {
d3.json("https://raw.githubusercontent.com/weihaolun/twitter-visualization/master/datasources/weekly_tweets_counts.json").then((data) => {
// Roll data by weeks
const dataByWeeks = d3.nest()
.key(function (d) { return d.weeks; })
.entries(data);
// Create array filtered by weeks
const weekArray = dataByWeeks.filter(sampleObj => sampleObj.key == weeks);
// Create array to hold the values only
const theWeekData = weekArray[0].values;
console.log("this is the week's value only", theWeekData);
// Create an array to hold the date range of this week
const weekDateRange = {
"Start": `${theWeekData[0].created_date}`,
"End": `${theWeekData[theWeekData.length-1].created_date}`
}
console.log("this is the date range of the week", weekDateRange);
// Date Range Panel
const RANGEPANEL = d3.select("#weekly-data-range");
RANGEPANEL.html("");
Object.entries(weekDateRange).forEach(([key, value]) => {
RANGEPANEL.append("h6").text(`${key.toUpperCase()}: ${value.toString()}`);
});
// Accumulate the weekly occurred.
var totalWeeklyOccurred = 0
for (var i = 0; i < theWeekData.length; i++) {
totalWeeklyOccurred += theWeekData[i].tweet_count;
}
const totalTweetsOccurred = {
"Weekly Total": `${totalWeeklyOccurred}`
};
console.log("this is the total weekly occurred", totalTweetsOccurred)
// Total Tweets Panel
const OCCURPANEL = d3.select("#total-tweets-occurred");
OCCURPANEL.html("");
Object.entries(totalTweetsOccurred).forEach(([key, value]) => {
OCCURPANEL.append("h6").text(`${key.toUpperCase()}: ${value.toString()}`);
});
// Calculate the average count per day
averageCount = Math.round(totalWeeklyOccurred / 7);
const averageTweets = {
"Average Tweets": `${averageCount}`
};
console.log("this is the average occurred per day", averageTweets)
// Average Tweets Panel
const AVERAGEPANEL = d3.select("#average-tweets");
AVERAGEPANEL.html("");
Object.entries(averageTweets).forEach(([key, value]) => {
AVERAGEPANEL.append("h6").text(`${key.toUpperCase()}: ${value.toString()}`);
});
// Plot the barchart
const theWeekdayList = [];
const eachDayCount = [];
for (let i = 0; i < 7; i++) {
theWeekdayList.push(theWeekData[i].weekday);
eachDayCount.push(theWeekData[i].tweet_count);
}
const weeklyBarTrace = {
x: theWeekdayList,
y: eachDayCount,
type: 'bar',
text: eachDayCount.map(String),
textposition: "auto",
hoverinfo: 'none',
width: 0.6,
marker : {
color: '#b73038',
},
}
const barData = [weeklyBarTrace];
const barLayout = {
title: "<b> Total Tweets Occurred Each Day<b>",
yaxis: {title: "Number Of Tweets"},
paper_bgcolor: "#D7DCDD",
plot_bgcolor: "#D7DCDD",
};
const config = {responsive: true}
Plotly.newPlot("weekly-bar", barData, barLayout, config);
})
}