-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbar_updates_no_key.html
More file actions
203 lines (162 loc) · 5.09 KB
/
bar_updates_no_key.html
File metadata and controls
203 lines (162 loc) · 5.09 KB
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
<!DOCTYPE html>
<!-- Modified example from enjalot: http://bl.ocks.org/enjalot/1429426 -->
<html>
<head>
<title>Bar Transition Example</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style type="text/css">
body {
padding: 50px;
font-family: sans-serif;
font-size: 12pt;
}
button {
margin: 5px;
font-size: 15pt;
padding: 3px;
cursor: pointer;
background: white;
}
.selected {
background: pink;
}
input {
margin: 5px;
font-size: 15pt;
padding: 3px;
}
p {
width: 500px;
}
.data1 {
position: absolute;
left: 600px;
top: 400px;
}
.data2 {
position: absolute;
left: 600px;
top: 420px;
}
</style>
</head>
<body>
<h2>Bar Updates with No Key Value</h2>
<p> An illustration of how index is used for data without a key function. DON'T DO THIS.
(Based on <a href="http://bl.ocks.org/enjalot/1429426">an example from enjalot</a>.) THIS IS AN ILLUSTRATION OF HOW YOUR VIS COULD GO WRONG. See instead <a href="bar_updates_key.html">bar_updates_key.html</a>.
<div id="buttons">
<button id="data1">Set Data to data 1</button>
<button id="data2">Set Data to data 2</button>
<br>
<button id="random">Random Data of Size:</button>
<input id="num" value=10 size=2></input>
</div>
<div class="data1"></div>
<div class="data2"></div>
<div id="chart">
</div>
<script type="text/javascript">
var data1 = [
5,
20,
55,
60,
90
];
var data2 = [
35,
80,
35,
80,
20,
15,
10
];
function random(n) {
// created an array of n random numbers.
vals = [];
for(i = 0; i < n; i++) {
vals.push(Math.random() * 100);
}
return vals;
}
var fullwidth = 400;
var fullheight = 400;
var margin = {top: 10, right: 10, bottom: 10, left: 20};
var width = fullwidth - margin.left - margin.right;
var height = fullheight - margin.top - margin.bottom;
//setup the svg
var vis = d3.select("#chart").append("svg");
var svg = vis
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//setup our ui buttons:
d3.select("#data1")
.on("click", function(d,i) {
d3.selectAll("button").classed("selected", false);
d3.select(this).classed("selected", true);
redraw(data1);
});
d3.select("#data2")
.on("click", function(d,i) {
d3.selectAll("button").classed("selected", false);
d3.select(this).classed("selected", true);
redraw(data2);
});
d3.select("#random")
.on("click", function(d,i) {
d3.selectAll("button").classed("selected", false);
d3.select(this).classed("selected", true);
var num = document.getElementById("num").value;
redraw(random(num));
});
// set up the data listing on the right side
d3.select("div.data1").text("Data 1: " +data1.join(", "));
d3.select("div.data2").text("Data 2: " +data2.join(", "));
//make the bars for the first data set.
d3.select("#data1").classed("selected", true);
redraw(data1);
function redraw(data) {
var max = d3.max(data);
xScale = d3.scale.linear()
.domain([0, max])
.range([0, width]);
yScale = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, height], .2);
// THIS IS WHAT NOT TO DO:
var bars = vis.selectAll("rect.bar")
.data(data); // this data is unstructured, so no keys. just index order.
//update -- existing bars get green when we "redraw".
bars
.attr("fill", "green");
//enter - new bars get set to red when we "redraw."
bars.enter()
.append("rect")
.attr("class", "bar")
.attr("fill", "red")
.append("title")
.text(function (d, i) { return i + ":" + d; }); // this is the original i, d pair.
//exit -- remove ones that aren't in the index set
bars.exit()
.transition()
.duration(300)
//.ease("exp")
.attr("width", 0) // shrink them down
.remove();
// transition -- moves bars to new width and new position, based on index.
bars
.transition()
.duration(300)
//.ease("quad")
.attr("width", xScale) // same as calling this on d in a function
.attr("height", yScale.rangeBand())
.attr("transform", function(d,i) {
return "translate(0," + yScale(i) + ")";
});
} // end of draw function
</script>
</body>
</html>