-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex3.html
266 lines (221 loc) · 10 KB
/
index3.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat:200,300,400,500,600" rel="stylesheet">
<title>Line Chart Raw Count Example</title>
<style>
.label
{
font-family: 'Montserrat',sans-serif;
font-weight: 500;
font-size: 15px;
color:black;
}
.container{
float:left;
}
</style>
</head>
<body>
<div class="container" id="franchiseLine"></div>
<div class="container" id="legendDiv"></div>
<script>
let margin = {top: 2, right: 30, bottom: 30, left: 30},
width = 600
height= 600
chartWidth = width - margin.left - margin.right,
chartHeight = height - margin.top - margin.bottom;
let myGraph = d3.select("#franchiseLine")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("id", "rawLineChart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
let legends = d3.select("#legendDiv")
.append("svg")
.attr('width', 500)
.attr('height',400)
.append('g')
.attr('transform', 'translate(0, 30)')
d3.csv("franchises_small.csv",d3.autoType).then((data)=>{
// const filterValues = ["The Lord of the Rings", "The Hobbit"]
// data = data.filter( d => { return filterValues.indexOf(d['Franchise']) !== -1})
console.log(data);
let dateExtent = d3.extent(data, d=>d.Year);
console.log(dateExtent)
let countMax = d3.max(data, d=>d.Count)
let dateScale = d3.scaleLinear().domain(dateExtent).range([margin.left, chartWidth]);
let countScale = d3.scaleLinear().domain([0, countMax]).range([chartHeight, 0]);
let yAxis = d3.axisLeft(countScale).ticks();
let xAxis = d3.axisBottom(dateScale)
.tickFormat(d3.format("d"))
.ticks(20)
myGraph.append("g")
.attr("transform","translate(" + margin.left + "," + margin.top + ")")
.call(yAxis);
myGraph.append("g")
.attr("transform", "translate("+ margin.left + "," + chartHeight + ")")
.call(xAxis);
//Choose colors for each franchise
let names = [...new Set(data.map(d => d.Franchise))];
console.log(names)
let colorScale = d3.scaleOrdinal().domain(names)
.range(['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffbe33','#a65628','#f781bf','#4ec0c2','#ecf235'])
let sumstat = d3.group(data, d=>d['Franchise'])
let keyValue = Array.from(sumstat, ([key, value]) => ({key, value}))
//Line function to chart the line graph
let line = d3.line()
.x(function(d){ return dateScale(+d['Year'])})
.y(function(d){ return countScale(+d['Count'])})
let tooltip = d3.select("body")
.data(keyValue)
.append("div")
.attr("class","tooltip")
.style("position", "absolute")
//.style("font-family", "'Open Sans', sans-serif")
//.style("font-size", "12px")
.style("z-index", "10")
.style("background-color", "transparent")
//.style("color","black")
//.style("border", "solid")
//.style("border-color", "#A89ED6")
.style("padding", "5px")
.style("border-radius", "2px")
.style("visibility", "hidden");
tooltip;
console.log(keyValue)
// Draw the line
let svg = d3.select("#rawLineChart")
svg.selectAll(".line")
.data(keyValue)
.enter()
.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.append("path")
.attr("fill", "none")
.attr("stroke", function(d){ return colorScale(d.key) })
.attr("stroke-width", 1.5)
.attr("d", d => line(d.value))
.on("mouseover", function(d) {
tooltip.style("visibility", "visible")
.text(d.key)
})
.on("mousemove", function(event,d) {
let [x, y] = d3.pointer(event);
tooltip.style("top", (y+30 ) + "px")
.style("left", (x+50 ) + "px")
.text(d.key)
})
.on("mouseout", function(d) {
tooltip.style("visibility", "hidden");
})
//add x-axis label
myGraph.append("text")
.attr("class","label")
.attr("text-anchor", "end")
.attr("x", 600)
.attr("y", 610)
.text("Year");
//add y-axis label
myGraph.append("text")
.attr("class","label")
.attr("text-anchor", "end")
.attr("y", 15)
.attr("x", 20)
.text("Count");
// //Add svgs for each chart
// let svg = d3.select("#myDataViz")
// .selectAll("uniqueChart")
// .data(keyValue)
// .enter()
// .append("svg")
// .attr("width",width)
// .attr("height",height)
// .append("g").attr("transform",
// "translate(" + margin.left + "," + margin.top + ")")
// .attr("id", function(d){ return(d.key)});
//
// //Put x and y axis
// svg.append("g")
// .call(yAxis);
// svg.append("g")
// .attr("transform", "translate(0," + chartHeight + ")")
// .call(d3.axisBottom(dateScale));
//
//
// svg.append("path")
// .attr("class", "mean")
// .attr("d", line(means))
// .style("stroke","lightgrey")
// .style("stroke-width",2)
// .style("fill", "none");
//
// // Draw the line for each Airport
// svg.append("path")
// .data(keyValue)
// .attr("fill", "none")
// //.attr("stroke", function(d){ return colorScale(d.key)})
// .attr("stroke", d=> {
// return(colorScale(d.key));
// })
// .attr("stroke-width", 1.5)
// .attr("d", d=>line(d.value));
//
// //Write the name for each Chart
// svg.append("text")
// .attr("class","airportName")
// .attr("text-anchor", "start")
// .attr("y", -5)
// .attr("x", 0)
// .text(function(d){ return((d.key).split(' ').slice(0,2).join(' '))})
// .style("fill", function(d){ return colorScale(d.key) })
//
let title = legends.append('text')
.attr('x', 0)
.attr('y', 0)
.attr('fill', 'black')
.attr('font-family', 'Helvetica Neue, Arial')
.attr('font-weight', 'bold')
.attr('font-size', '12px')
.text('Names of Fanfictions');
/* let entries = legends
.data(keyValue)
.append('circle')
.attr('cx',5) // <-- offset symbol x-position by radius
.attr('r', 5)
.style('fill', d=>colorScale(d.key));
let labels = entries.data(keyValue)
.append('text')
.attr('x',14)
.attr('y',4)
.attr('fill', 'black')
.attr('font-family', 'Helvetica Neue, Arial')
.attr('font-size', '11px')
.text(d=>d.key);*/
legends.append("circle").attr("cx",10).attr("cy",30).attr("r", 6).style("fill", "#e41a1c")
legends.append("circle").attr("cx",10).attr("cy",60).attr("r", 6).style("fill", "#377eb8")
legends.append("circle").attr("cx",10).attr("cy",90).attr("r", 6).style("fill", "#4daf4a")
legends.append("circle").attr("cx",10).attr("cy",120).attr("r", 6).style("fill", "#984ea3")
legends.append("circle").attr("cx",10).attr("cy",150).attr("r", 6).style("fill", "#ff7f00")
legends.append("circle").attr("cx",10).attr("cy",180).attr("r", 6).style("fill", "#ffbe33")
legends.append("circle").attr("cx",10).attr("cy",210).attr("r", 6).style("fill", "#a65628")
legends.append("circle").attr("cx",10).attr("cy",240).attr("r", 6).style("fill", "#f781bf")
legends.append("circle").attr("cx",10).attr("cy",270).attr("r", 6).style("fill", "#4ec0c2")
legends.append("circle").attr("cx",10).attr("cy",300).attr("r", 6).style("fill", "#ecf235")
legends.append("text").attr("x", 20).attr("y", 30).text("Harry Potter").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 60).text("Twilight").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 90).text("Star Trek").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 120).text("Star Wars Universe").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 150).text("Marvel").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 180).text("DC Universe").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 210).text("Middle Earth Total").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 240).text("The Lord of the Rings").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 270).text("The Hobbit").style("font-size", "15px").attr("alignment-baseline","middle")
legends.append("text").attr("x", 20).attr("y", 300).text("Pokemon").style("font-size", "15px").attr("alignment-baseline","middle")
});
</script>
</body>
</html>