-
Notifications
You must be signed in to change notification settings - Fork 4
/
svg.html
162 lines (143 loc) · 4.59 KB
/
svg.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
<!DOCTYPE HTML>
<head>
<title>svg examples</title>
<script type="text/javascript" src="lib/d3.v2.min.js"></script>
<script type="text/javascript" src="lib/underscore.js"></script>
<script type="text/javascript" src="lib/zepto.js"></script>
<script type="text/javascript">
var map = _.map;
</script>
</head>
<body>
<button id="switch">Switch</button>
<script type="text/javascript">
$(function() {
var w = 700,
h = 300,
p = [20, 50, 30, 20],
x = d3.scale.ordinal().rangeRoundBands([0, w - p[1] - p[3]]),
y = d3.scale.linear().range([0, h - p[0] - p[2]]),
z = d3.scale.ordinal().range(["darkgray", "lightblue"]),
parse = d3.time.format("%b %e").parse,
format = d3.time.format("%b %e"),
swap = true;
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + p[3] + "," + (h - p[2]) + ")");
var renderOne = function(file) {
d3.json(file, function(rData) {
var data = rData[0];
var stacks = d3.layout.stack()(_(data).map(function(row) {
return row;
}));
rerender(data, stacks);
});
};
d3.json("revenue.json", function(rData) {
var data = rData[0];
var stacks = d3.layout.stack()(_(data).map(function(row) {
return row;
}));
render(data, stacks);
});
$('#switch').click(function() {
if (swap) {
renderOne("rev-two.json");
swap = false;
} else {
renderOne("revenue.json");
swap = true;
}
});
var render = function(data, stacks) {
// Compute the x-domain and y-domain.
y.domain([0, d3.max(_(data).map(function(d) {
return d3.max(d, function(e, i) {
return data["total"][i].y + data["gross"][i].y
})}))*1.1]);
x.domain(_(data["total"]).map(function(d) { return d.x; }));
// Add a group for each bar.
var bar = svg.selectAll("g.bar")
.data(stacks)
.enter().append("svg:g")
.attr("class", "bar")
.style("fill", function(d, i) { return z(i); });
// Add a rect for each date.
var rect = bar.selectAll("rect")
.data(Object)
.enter().append("svg:rect")
.attr("x", function(d) { return x(d.x) + 1; })
.attr("y", function(d) { return -y(d.y) - y(d.y0); })
.attr("height", function(d) { return y(d.y); })
.attr("width", x.rangeBand()-1);
// Add a label per date.
var label = svg.selectAll("text.xlabel")
.data(x.domain())
.enter().append("svg:text")
.attr("x", function(d) { return x(d) + x.rangeBand() / 2; })
.attr("y", 6)
.attr("text-anchor", "middle")
.attr("dy", ".71em")
.attr("class", "xlabel")
.text(function(d) { return format(new Date(d)) });
// Add y-axis rules.
var rule = svg.selectAll("g.rule")
.data(y.ticks(3))
.enter().append("svg:g")
.attr("class", "rule")
.attr("transform", function(d) { return "translate(0," + -y(d) + ")"; });
rule.append("svg:line")
.attr("x2", w - p[1] - p[3])
.attr("class", "yline")
.style("stroke", function(d) { return d ? "#ccc" : "#000"; })
.style("stroke-opacity", function(d) { return d ? .7 : null; });
rule.append("svg:text")
.attr("x", 0)
.attr("dy", "1em")
.attr("class", "ylabel")
.text(d3.format(",d"));
};
var rerender = function(data, stacks) {
// Compute the x-domain and y-domain.
y.domain([0, d3.max(_(data).map(function(d) {
return d3.max(d, function(e, i) {
return data["total"][i].y + data["gross"][i].y
})}))*1.1]);
x.domain(_(data["total"]).map(function(d) { return d.x; }));
// Add a group for each bar.
var bar = svg.selectAll("g.bar")
.data(stacks);
// Add a rect for each date.
bar.selectAll("rect")
.data(Object)
.transition()
.duration(1000)
.attr("x", function(d) { return x(d.x) + 1; })
.attr("y", function(d) { return -y(d.y) - y(d.y0); })
.attr("height", function(d) { return y(d.y); })
.attr("width", x.rangeBand()-1);
// Add a label per date.
svg.selectAll("text.xlabel")
.data(x.domain())
.transition()
.duration(1000)
.attr("x", function(d) { return x(d) + x.rangeBand() / 2; })
.attr("y", 6)
.text(function(d) { return format(new Date(d)) });
// Add y-axis rules.
svg.selectAll("g.rule")
.data(y.ticks(3))
.transition()
.duration(1000)
.attr("transform", function(d) { return "translate(0," + -y(d) + ")"; });
svg.selectAll("line.yline")
.attr("x2", w - p[1] - p[3])
.style("stroke", function(d) { return d ? "#ccc" : "#000"; })
.style("stroke-opacity", function(d) { return d ? .7 : null; });
svg.selectAll("text.ylabel")
.text(d3.format(",d"));
};
});
</script>