-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreal_visitor_spending.html
203 lines (164 loc) · 6.43 KB
/
real_visitor_spending.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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>New Visualization</title>
<script type="text/javascript" src="dat.gui.min.js"></script>
<script type="text/javascript" src="../../d3.v2.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.svg.js"></script>
<script type="text/javascript">
function export_svg() {
$('svg').svg();
var xml = $('svg').svg('get').toSVG();
$('#svgexport').html(xml.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'));
}
</script>
<!-- Styles don't flow into exported SVG, so only use for web -->
<style>
svg {border: 1px solid gray;}
</style>
</head>
<body>
<script type="text/javascript">
var gui = new dat.GUI();
var dat_gui_ranges = {
scale : [0.01, 3],
w : [0, 800],
h : [0, 1200],
t_margin : [0, 100],
r_margin : [0, 100],
b_margin : [0, 100],
l_margin : [0, 100],
tick_length : [0, 20],
y_axis_fontsize : [0, 20],
x_axis_font_rotation: [0, 360],
fill_opacity : [0,1],
point_size :[0,20],
}
//put all hard coded values in this object
var params = {
scale: 1.7,
w: 555,
h: 468,
t_margin: 20,
r_margin: 20,
b_margin: 85,
l_margin: 50,
bar_width: 50,
tick_length: 7,
x_axis_color: "gray",
x_axis_fontcolor: "gray",
x_axis_fontsize: 10,
x_axis_font_rotation: 320,
y_axis_color: "gray",
y_axis_fontcolor: "gray",
y_axis_fontsize: 10,
area_color: "#224970",
fill_opacity: 0.2,
point_size :2.5,
//just to store for redraw
viz_data: [],
};
function left_boundary() { return params.l_margin; }
function right_boundary() { return params.w * params.scale - params.r_margin; }
function top_boundary() { return params.t_margin; }
function bottom_boundary() { return params.h * params.scale - params.b_margin; }
function pf(num) {
return parseFloat(num.replace(",", ""));
}
function init_viz_element(svg, elem_with_class, data) {
var elem_parts = elem_with_class.split(".");
var elem = elem_parts[0];
var elem_class = elem_parts[1];
var viz_element = svg.selectAll(elem_with_class).data(data);
viz_element.enter().append(elem).attr("class", elem_class);
viz_element.exit().remove();
return viz_element;
}
function draw(data) {
//store things for next draw iteration
params.viz_data = data;
var max_y_val = 20000; // replace with actual value from data
//preparing SVG Area
var svg = d3.selectAll("svg");
svg.
attr("height", function(d,i) {return params.h * params.scale}).
attr("width", function(d) {return params.w * params.scale}).
attr("fill", 'green')
.data([data]);
var headers = d3.keys(data[0]); //alert(headers);
var categories = headers.slice(1); //alert(categories.toSource());
var first_year = d3.min(data, function(row) {return row[headers[0]]});
var last_year = d3.max(data, function(row) {return row[headers[0]]});
var x = d3.scale.linear().domain([first_year,last_year]).range([left_boundary(), right_boundary() ]);
var y = d3.scale.linear().domain([0, max_y_val]).range([bottom_boundary(), top_boundary() ]);
var line = init_viz_element(svg, "path.line", function(d) { return [d]; })
.attr("d", d3.svg.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.spending); })
)
.attr("stroke", params.area_color)
.attr("fill", "none");
//------ x Axis -------------------------------------------------------------------
var xaxis = init_viz_element(svg, "line.xaxis", ["dummy_data"])
.attr("y1", bottom_boundary())
.attr("y2", bottom_boundary())
.attr("x1", function(d) {return left_boundary()})
.attr("x2", function(d) {return right_boundary()})
.attr("stroke", params.x_axis_color);
var xticks = init_viz_element(svg, "line.xtick", x.ticks(30))
.attr("y1", bottom_boundary())
.attr("y2", bottom_boundary() + params.tick_length )
.attr("x1", function(d) {return x(d)})
.attr("x2", function(d) {return x(d)})
.attr("stroke", params.x_axis_color);
var xtick_labels = init_viz_element(svg, "text.xtick_label", x.ticks(30))
.attr("x", function(d) { return x(d)})
.attr("y", bottom_boundary())
.attr("fill", params.x_axis_fontcolor)
.attr("font-size", params.x_axis_fontsize+"px")
.attr("transform", function(d) {return "rotate("+params.x_axis_font_rotation +" "+ (x(d)+params.tick_length*4).toString() + "," + bottom_boundary() + ")"; })
.attr("text-anchor", "end")
.text(function(d) {return d; });
//------ x Axis End -------------------------------------------------------------------
//------ y Axis -------------------------------------------------------------------
var yaxis = init_viz_element(svg, "line.yaxis", ["dummy_data"])
.attr("x1", left_boundary())
.attr("x2", left_boundary())
.attr("y1", function(d) {return top_boundary()})
.attr("y2", function(d) {return bottom_boundary()})
.attr("stroke", params.y_axis_color);
var yticks = init_viz_element(svg, "line.ytick", y.ticks(10))
.attr("y1", function(d) {return y(d)})
.attr("y2", function(d) {return y(d)})
.attr("x1", left_boundary() - params.tick_length)
.attr("x2", left_boundary() )
.attr("stroke", params.y_axis_color);
var ytick_labels = init_viz_element(svg, "text.ytick_label", y.ticks(10))
.attr("y", function(d) { return y(d) + (params.y_axis_fontsize / 3.5)})
.attr("x", left_boundary() - params.tick_length * 1.5)
.attr("fill", params.y_axis_fontcolor)
.attr("font-size", params.y_axis_fontsize+"px")
.attr("text-anchor", "end")
.text(function(d) {return d; });
//------ y Axis End -------------------------------------------------------------------
}
//read data once and start do initial draw
d3.csv("visitor_spending.csv", draw);
//all dat.gui changes should trigger a redraw
// generate the dat.gui control for any numerical ranges
d3.entries(dat_gui_ranges).forEach(function(elem) {
var attr = elem.key;
var range = elem.value;
gui.add(params, attr, range[0], range[1]).onChange(function() { draw(params.viz_data); });
} );
//add color or custom controls here
</script>
<h1>Chart Title</h1>
<div id= "chart_div">
<svg></svg>
</div>
<button onclick="export_svg();" />export</button>
<pre><div id="svgexport">export will go here</div></pre>
</body>
</html>