-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaterfallPlot.js
171 lines (144 loc) · 3.87 KB
/
waterfallPlot.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
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
// sorts decreasing, to make it increasing:
// sort (function (x, y) {
// return x.a - y.a
// })
function zip(a, b) {
var zipped = [];
// zip
for (var i=0; i<a.length; i++) {
zipped.push({a: a[i], b: b[i] });
}
zipped.sort(function (x, y){
return y.a - x.a;
});
var c = []
var d = []
for (var i=0; i < zipped.length; i++) {
c[i] = zipped[i].a
d[i] = zipped[i].b
}
return [c,d]
}
function makeWaterfall(names, nums) {
var margin = {
top: 100,
right: 100,
bottom: 100,
left: 80
};
var width = 700;
var height = 300;
var maxNum = d3.max(nums)
var zipped = zip(nums, names)
nums = zipped[0]
names = zipped[1]
// Add the svg canvas
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("xmlns", "http://www.w3.org/2000/svg")
.attr("xmlns:xlink", "http://www.w3.org/1999/xlink")
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")")
.attr("fill", "white");
// graph title
var graphTitle = svg.append("text")
.attr("text-anchor", "middle")
.attr("fill","black")
.attr("dy", 0)
.style("font-size", "20px")
.attr("transform", "translate("+ (width/2.2) +","+ -50 +")")
.text("A waterfall plot")
//append everything to this
var plot = svg.append("g")
//set range for data by domain, and scale by range
var xrange = d3.scale.linear()
.domain([0, names.length])
.range([0, width]);
var yrange = d3.scale.linear()
.domain([d3.min(nums), maxNum])
.range([height, 0]);
//set axes for graph
var xAxis = d3.svg.axis()
.scale(xrange)
.orient("bottom")
.tickFormat(function(d,i){ return names[i] })
.tickValues(d3.range(names.length));
var yAxis = d3.svg.axis()
.scale(yrange)
.orient("left")
.tickFormat(d3.format("0.2f"));
// Add the Y Axis
var yAxisBar = plot.append("g")
.attr("class", "y axis")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("transform", function() {
if (d3.min(nums) > 0) {
return "translate(0," + (yrange(0)-height) + ")"
}})
.call(yAxis)
.selectAll("text")
.style("font-size", 13)
// Add the X Axis
var xAxisBar = plot.append("g")
.attr("class", "x axis")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("transform", function() {
if (d3.min(nums) > 0) {
return "translate(0," + yrange(0) + ")"
} else {
return "translate(0," + yrange(0) + ")"
}
})
.call(xAxis)
xAxisBar.selectAll("text")
.style("text-anchor", function(d,i) {
if (nums[i] >= 0) {
return "end"
} else {
return "start"
}
})
.style("font-size", "11px")
.attr("dx", function(d,i) {
if (nums[i] >= 0) {
return "-1em"
} else {
return "1em"
}
})
.attr("transform", "rotate(-90)" )
.attr("dy", 5)
.attr("y", (width/nums.length)/2)
.attr("fill", "#207cc1")
.attr("stroke", "none")
.append("svg:title")
.text(function(d,i) {return names[i]});
plot.selectAll(".tick")
.select("text")
.attr("fill", "black")
.attr("stroke", "none")
//adding chart
var chart = plot.append('g')
.attr('id','bars')
// adding each bar
chart.selectAll('.bar')
.data(nums)
.enter()
.append('rect')
.attr("class", "bar")
.style("stroke", "none")
.attr('height', function(d) {return Math.abs(yrange(d) - yrange(0));})
.attr({
'x':function(d,i) { return xrange(i)},
'y':function(d) { return yrange(Math.max(0,d))} // to determine if we should flip the bar or not
})
.attr("width", width/nums.length - 3)
.style('fill', "#2d5faf")
}