-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.html
186 lines (142 loc) · 5.28 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Exit</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style type="text/css">
.bar {
fill: steelblue;
stroke: none;
}
.highlight {
fill: red;
}
.readme {
font-family: Helvetica Neue, Helvetica, Arial sans-serif;
max-width: 960px;
}
.readme .footer {
font-size: 12px;
color: gray;
}
</style>
</head>
<body>
<script type="text/javascript">
//Define our random-number generator function
var _id_ = 0;
function generate() {
return {
index: _id_++,
value: 1000 * Math.random()
};
}
//Generate random data to use for the chart
var data = d3.range(100).map(generate);
//Set variables for desired size of chart
var w = 900,
h = 100,
bw = 9;
//Define a scale for y axis values
var yScale = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return d.value; })])
.range([0, 100]);
//Define a key function to uniquely identify data values
var key = function(d) {
return d.index;
};
//Select the <body> and create a new SVG element
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
//Create a series of 'rect' elements within the SVG
svg.selectAll("rect.bar")
.data(data, function(d) {
return d.index; // <-- Reference key function within data join
})
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d, i) {
return i * bw;
})
.attr("width", bw - 1)
.attr("y", function(d) {
return h - yScale(d.value);
})
.attr("height", function(d) {
return yScale(d.value);
})
.on("mouseover", function(d) {
d3.select(this).classed("highlight", true);
})
.on("mouseout", function(d) {
d3.select(this).classed("highlight", false);
});
//Define a new function that updates all data
function update() {
data.shift();
data.push(generate());
//Re-select bars
var bars = svg.selectAll("rect.bar")
.data(data, key); // <-- …while using key function!
//Add a new rect for the incoming data (one new value)
bars.enter()
.append("rect")
.attr("class", "bar")
.attr("x", w)
.attr("width", bw - 1)
.attr("y", function(d) {
return h - yScale(d.value);
})
.attr("height", function(d) {
return yScale(d.value);
})
.on("mouseover", function(d) {
d3.select(this).classed("highlight", true);
})
.on("mouseout", function(d) {
d3.select(this).classed("highlight", false);
});
//Re-position bars along x-axis with animation
//'bars' now contains both the enter and update sets
bars.transition()
.duration(500)
.attr("x", function(d, i) {
return i * bw;
});
//Re-position and then remove exiting data
bars.exit()
.transition()
.duration(500)
.attr("x", -bw) //Move past the left edge of the SVG frame
.remove(); //Delete this element from the DOM forever
}
//Execute this update function every two seconds
d3.interval(update, 2000);
</script>
<div class="readme">
<h3>Dynamic Bar Chart with D3</h3>
<p>This example will lead you step-by-step to build an animated bar chart with dynamic data.
For each step, you should view source: each new piece of code will be annotated with a comment.</p>
<ul>
<li><a href="00-dom.html">00-dom.html</a> — Simple elements in the DOM</li>
<li><a href="01-svg.html">01-svg.html</a> — Simple demo of SVG elements</li>
<li><a href="02-page-template.html">02-page-template.html</a> — An empty page great for testing D3 code right in the console</li>
<li><a href="03-data-simple.html">03-data-simple.html</a> — Data types and generating random values</li>
<li><a href="04-data-from-csv.html">04-data-from-csv.html</a> — Loading data from CSV and JSON files</li>
<li><a href="05-create-bars.html">05-create-bars.html</a> — Creating SVG elements from data!</li>
<li><a href="06-add-scale.html">06-add-scale.html</a> — Adding a scale for data flexibility</li>
<li><a href="07-add-hover.html">07-add-hover.html</a> — Adding mouse interactivity</li>
<li><a href="08-transition-color.html">08-transition-color.html</a> — An arbitrary color transition</li>
<li><a href="09-data-update.html">09-data-update.html</a> — Updating the data set (without a transition)</li>
<li><a href="10-data-transition.html">10-data-transition.html</a> — Transitioning bars to new values</li>
<li><a href="11-data-transition-with-key.html">11-data-transition-with-key.html</a> — Using a key with a data join</li>
<li><a href="12-exit.html">12-exit.html</a> — Transitioning bars and removing outgoing elements</li>
</ul>
<p class="footer">Code samples developed for the “Introduction to Data Visualization on the Web with D3.js” tutorial at VisWeek 2012 by organizers Jérôme Cukier, Jeff Heer, and Scott Murray.</p>
<p class="footer">Code samples updated to d3v4 by Sean McKenna.</p>
</div>
</body>
</html>