-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineChart.js
138 lines (118 loc) · 4.18 KB
/
lineChart.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
/**
* Linechart function object.
* @param {} deets
*/
function lineChart(deets){
var data = d3.nest()
.key(function(d) { return parseInt(d.time); })
.rollup(function(v) { if(state.view == "real_estate") {
return d3.mean(v, function(d) { return d.value;})
} else {return d3.sum(v, function(d) { return d.value; });}})
.entries(deets)
.sort((a,b) => { return d3.ascending(parseInt(a.key), parseInt(b.key))});
// setting constants
var width = 600;
var height = 400;
var margin = {
top: 40,
bottom: 75,
left: 75,
right: 30
};
var mintime = d3.min(data, function(d){return parseInt(d.key);});
var maxtime = d3.max(data, function(d){return parseInt(d.key);});;
var minvalue = 0;
var maxvalue = d3.max(data, function(d){return d.value;});
//setting table labels
var title = "";
var x_axis_label = "";
var y_axis_label = "";
if(state["view"] == "crime"){
title = "Crime By Hour in Boston";
y_axis_label = "Crime Count";
x_axis_label = "Hour of Day";
} else if (state["view"] == "real_estate") {
title = "Real Estate Prices over Time";
y_axis_label = "Average Price Per Square Feet";
x_axis_label = "Year";
} else if (state["view"] == "demographic") {
title = "Bachelor's Degrees Over Time";
y_axis_label = "Bachelor's Degrees Per Year";
x_axis_label = "Year";
}
// removing labels
d3.select(".x_axis_label").remove();
d3.select(".y_axis_label").remove();
d3.select(".title").remove();
var svg = d3.select('#vis3')
.attr("class", "vis3")
.attr('width' , width)
.attr('height', height)
// .style('background', '#efefef');
// appending svg
var chartGroup = d3.select('#vis3').append('g')
.append('svg')
.attr('transform','translate(' + margin.left +',' + margin.top + ')')
// Adding Graph Title
chartGroup.append("text")
.attr("x", width/2)
.attr("y", margin.top - 20)
.attr('class', 'title')
.attr("text-anchor", "middle")
.style("font-size", "20px")
.style("text-decoration", "underline")
.text(title);
// x scale
var xScale = d3.scaleLinear()
.domain([mintime, maxtime])
.range([0, width - 100]);
// y scale
var yScale = d3.scaleLinear()
.domain([minvalue, maxvalue])
.range([height - margin.bottom - margin.top, 0]);
var xAxis = d3.axisBottom(xScale);
var yAxis = d3.axisLeft(yScale);
chartGroup.append('g')
.attr('class', 'x_axis')
.attr('transform', 'translate('+ margin.left+', ' + (height - margin.bottom) + ')')
.call(xAxis)
.style("font-size", "14px");
// Adding X-Axis Label
chartGroup.append("text")
.attr("x", width/2)
.attr("y", height - margin.bottom + 40)
.attr("class", "x_axis_label")
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text(x_axis_label);
// Setting the Y axis labels
chartGroup.append('g')
.attr('class', 'y_axis')
.attr('transform', 'translate('+ margin.left +', ' + margin.top+')')
.call(yAxis)
.style("font-size", "14px");
// Adding Y-Axis Label
chartGroup.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("y", margin.left - 60)
.attr("class", "y_axis_label")
.style("font-size", "16px")
.attr("text-anchor", "middle")
.text(y_axis_label);
var paths = chartGroup
.datum(data)
.append("path")
.attr("class","derp")
.attr("fill", "none")
.attr("stroke", d => {
return state.colors[state["view"]]})
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(function (d) {
return xScale(parseInt(d.key)) + margin.left; })
.y(function (d) {
return yScale(d.value) + margin.top; }));
paths.exit().remove();
chartGroup.exit().remove()
}