-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar-chart-transitions.html
More file actions
146 lines (112 loc) · 2.95 KB
/
Copy pathbar-chart-transitions.html
File metadata and controls
146 lines (112 loc) · 2.95 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Bar Chart v5 - Transitions</title>
<link rel="stylesheet" type="text/css" href="css/samples.css">
<style type="text/css" media="screen">
div.bar {
background-color: orange;
height: 20px;
padding: 5px 0;
margin-bottom: 5px;
color: #000;
font-size: 24px;
font-weight: bold;
}
div:after {
content: ' ';
display:table;
clear: both;
}
div.bar .value {
font-family: Arial;
float:right;
margin-right: 5px;
}
</style>
</head>
<body class="sample">
<div class="container">
<h1>A Bar Chart v5 - Transitions</h1>
<div id="barChartDiv"></div>
</div>
<!-- list will be inserted/appended here -->
</body>
<script src="js/lib/d3.v4.min.js"></script>
<script>
(function(){
/* global d3 */
var container = d3.select('#barChartDiv'),
max,
transDuration = 1000,
enter, update, exit,
firstRender;
// set the scales
var xScale = d3.scaleLinear().rangeRound([0, 100]);
var colorScale = d3.scaleQuantile()
.range(["red", "yellow", "lime"]);
function render(data) {
var trans = d3.transition()
.duration(transDuration)
.ease(d3.easeQuadOut);
// set scales
xScale
.domain([0, d3.max(data, function(d){
return d.sales;
})])
.range([0,100]);
colorScale.domain(xScale.domain());
// append the rectangles for the bar chart
var bar = container.selectAll('div');
update = bar.data(data, function(d){
return d.index;
});
update.select('span')
.text(function(d){
return d.sales;
});
enter = update.enter().append('div');
enter
.style('width', '0%')
.attr('class', 'bar')
.merge(update)
.transition(trans)
.style('background-color', getBgColor)
.styleTween('width', function(d) {
return d3.interpolateString(this.style.width, xScale(d.sales) + '%');
});
enter.append('span')
.attr('class', 'value')
.text(function(d){
return d.sales;
});
setTimeout(function(){
getData(10, [50, 300]);
}, transDuration * 1.5);
}
// accessor function to return color based on "sales" value
function getBgColor(d){
return colorScale(d.sales);
}
function getData(count, range){
var arr = [];
d3.range(count).map(function(val){
var obj = {
index: val,
sales: Math.random() * (range[1]-1) | range[0]
};
arr.push(obj);
});
max = d3.max(arr);
render(arr);
}
function sortDesc(a, b) {
return a.sales < b.sales;
}
// get data for first time
getData(10, [50, 300]);
})();
</script>
</html>