-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbar_updates_key_sorted.html
More file actions
191 lines (155 loc) · 5.41 KB
/
bar_updates_key_sorted.html
File metadata and controls
191 lines (155 loc) · 5.41 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
<!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 {
width: 200px;
position: absolute;
left: 600px;
top: 300px;
}
.data2 {
width: 200px;
position: absolute;
left: 600px;
top: 450px;
}
</style>
</head>
<body>
<h2> Using Keys to Retain Bar Identity </h2>
<p> An illustration of the use of the key function, to retain object consistency between updates. Bars that existed already will stay and labels (mouseover titles) will be correct.
(Based on <a href="http://bl.ocks.org/enjalot/1429426">an example from enjalot</a>.)
<div id="buttons">
<button id="data1">Set Data to data 1</button>
<button id="data2">Set Data to data 2</button>
</div>
<div id="chart">
</div>
<div class="data1"></div>
<div class="data2"></div>
<script type="text/javascript">
var data1 = [
{country: "Belgium", value: 5}, // in data set 2
{country: "USA", value: 20}, // in data set 2
{country: "China", value: 55}, // in data set 2
{country: "Russia", value: 15},
{country: "France", value: 60}, // in data set 2
{country: "Chile", value: 89}
];
var data2 = [
{country: "Belgium", value: 5}, // in data set 1
{country: "USA", value: 20}, // in data set 1
{country: "Spain", value: 35},
{country: "China", value: 55}, // in data set 1
{country: "UK", value: 90},
{country: "Brazil", value: 40},
{country: "France", value: 60}, // in data set 1
{country: "Canada", value: 39},
{country: "Argentina", value: 99}
];
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.select(this).classed("selected", true);
d3.select("#data2").classed("selected", false);
redraw(data1);
});
d3.select("#data2")
.on("click", function(d,i) {
d3.select(this).classed("selected", true);
d3.select("#data1").classed("selected", false);
redraw(data2);
});
d3.select("div.data1").text("Data 1: " +JSON.stringify(data1));
d3.select("div.data2").text("Data 2: " +JSON.stringify(data2));
//make the bars for the first data set. They will be red at first.
d3.select("#data1").classed("selected", true);
// Call the draw function for the first data set view, data1. This is initial view.
redraw(data1);
// This function is used to draw and update the data. It takes different data each time.
function redraw(data) {
var max = d3.max(data, function(d) {return d.value;});
data.sort(function(a,b) {
return d3.descending(a.value, b.value);
});
xScale = d3.scale.linear()
.domain([0, max]) // calculated above
.range([0, width]);
yScale = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, height], .2);
// data join:
var bars = vis.selectAll("rect.bar")
.data(data, function (d) { return d.country;}); // key function!
//.data(data);
//update -- existing bars get green when we "redraw". We don't change labels.
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 d.country + ":" + d.value; });
//exit -- remove ones that aren't in the index set
bars.exit()
.transition()
.duration(300)
.attr("width", 0)
.remove();
// update transition on all existing ones now -- move the bars to proper widths and location
bars
.transition()
.duration(1000)
.attr("width", function(d) {
return xScale(d.value);
})
.attr("height", yScale.rangeBand())
.attr("fill", "green") // -- turns them all to green at the end.
.attr("transform", function(d,i) {
return "translate(0," + yScale(i) + ")";
});
} // end of draw function
</script>
</body>
</html>