-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar-chart-labels.html
More file actions
91 lines (74 loc) · 1.96 KB
/
Copy pathbar-chart-labels.html
File metadata and controls
91 lines (74 loc) · 1.96 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
<!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 v2 - Labels</title>
<link rel="stylesheet" type="text/css" href="css/samples.css">
<style type="text/css" media="screen">
div.bar {
background-color: orange;
padding: 5px 10px;
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>Bar Chart v2 - Labels</h1>
<div id="barChartDiv"></div>
</div>
<!-- list will be inserted/appended here -->
</body>
<script src="js/lib/d3.v4.min.js"></script>
<script src="js/get-data.js"></script>
<script>
(function(){
/* global d3 */
var max;
var data = getSalesData();
// set the scales
var xScale = d3.scaleLinear().range([0,100]);
function render() {
var container = d3.select("#barChartDiv");
// format the data
data.forEach(function(d) {
d.sales = +d.sales;
});
max = d3.max(data, function(d) { return d.sales; });
// set scales
xScale.domain([0, d3.max(data, function(d) { return d.sales; })]);
// append the rectangles for the bar chart
var bars = container.selectAll("div")
.data(data)
.enter().append("div")
.attr("class", "bar")
.style("width", function(d) {
return getPercentage(d.sales);
});
bars.append('span')
.attr('class', 'value')
.text(function(d){
return d.sales;
});
}
function getPercentage(d){
return ((d/max) * 100) + '%'; // magic?
}
render();
})();
</script>
</html>