/
bar.js
205 lines (180 loc) · 6.2 KB
/
bar.js
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
204
205
define(function(require){
'use strict';
var d3 = require('d3');
/**
* @typdef D3Selection
* @type Array[]
*/
/**
* @fileOverview Bar Chart reusable API class that renders a
* simple and configurable bar chart.
*
* @exports chart/bar
* @requires d3
*/
return function module(){
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960,
height = 500,
gap = 2,
data,
chartWidth, chartHeight,
xScale, yScale,
xAxis, yAxis,
svg,
// extractors
getLetter = function(d) { return d.letter; },
getFrequency = function(d) { return d.frequency; };
/**
* This function creates the graph using the selection as container
* @param {D3Selection} _selection A d3 selection that represents
* the container(s) where the chart(s) will be rendered
*/
function exports(_selection){
/* @param {object} _data The data to attach and generate the chart */
_selection.each(function(_data){
chartWidth = width - margin.left - margin.right;
chartHeight = height - margin.top - margin.bottom;
data = _data;
buildScales();
buildAxis();
buildSVG(this);
drawBars();
drawAxis();
});
}
/**
* Creates the d3 x and y axis, setting orientations
* @private
*/
function buildAxis(){
xAxis = d3.svg.axis()
.scale(xScale)
.orient('bottom');
yAxis = d3.svg.axis()
.scale(yScale)
.orient('left')
.ticks(10, '%');
}
/**
* Builds containers for the chart, the axis and a wrapper for all of them
* Also applies the Margin convention
* @private
*/
function buildContainerGroups(){
var container = svg.append('g')
.classed('container-group', true)
.attr({
transform: 'translate(' + margin.left + ',' + margin.top + ')'
});
container.append('g').classed('chart-group', true);
container.append('g').classed('x-axis-group axis', true);
container.append('g').classed('y-axis-group axis', true);
}
/**
* Creates the x and y scales of the graph
* @private
*/
function buildScales(){
xScale = d3.scale.ordinal()
.domain(data.map(getLetter))
.rangeRoundBands([0, chartWidth], 0.1);
yScale = d3.scale.linear()
.domain([0, d3.max(data, getFrequency)])
.range([chartHeight, 0]);
}
/**
* @param {HTMLElement} container DOM element that will work as the container of the graph
* @private
*/
function buildSVG(container){
if (!svg) {
svg = d3.select(container)
.append('svg')
.classed('bar-chart', true);
buildContainerGroups();
}
svg.transition().attr({
width: width + margin.left + margin.right,
height: height + margin.top + margin.bottom
});
}
/**
* @description
* Draws the x and y axis on the svg object within their
* respective groups
* @private
*/
function drawAxis(){
svg.select('.x-axis-group.axis')
.attr('transform', 'translate(0,' + chartHeight + ')')
.call(xAxis);
svg.select('.y-axis-group.axis')
.call(yAxis);
}
/**
* Draws the bar elements within the chart group
* @private
*/
function drawBars(){
var gapSize = xScale.rangeBand() / 100 * gap,
barW = xScale.rangeBand() - gapSize,
bars = svg.select('.chart-group').selectAll('.bar').data(data);
// Enter
bars.enter()
.append('rect')
.classed('bar', true)
.attr({
width: barW,
x: chartWidth, // Initially drawing the bars at the end of Y axis
y: function(d) { return yScale(d.frequency); },
height: function(d) { return chartHeight - yScale(d.frequency); }
});
// Update
bars
.attr({
width: barW,
x: function(d) { return xScale(d.letter) + gapSize/2; },
y: function(d) { return yScale(d.frequency); },
height: function(d) { return chartHeight - yScale(d.frequency); }
});
// Exit
bars.exit()
.style({ opacity: 0 }).remove();
}
/**
* Gets or Sets the margin of the chart
* @param {object} _x Margin object to get/set
* @return { margin | module} Current margin or Bar Chart module to chain calls
* @public
*/
exports.margin = function(_x) {
if (!arguments.length) return margin;
margin = _x;
return this;
};
/**
* Gets or Sets the width of the chart
* @param {number} _x Desired width for the graph
* @return { width | module} Current width or Bar Chart module to chain calls
* @public
*/
exports.width = function(_x) {
if (!arguments.length) return width;
width = _x;
return this;
};
/**
* Gets or Sets the height of the chart
* @param {number} _x Desired width for the graph
* @return { height | module} Current height or Bar Char module to chain calls
* @public
*/
exports.height = function(_x) {
if (!arguments.length) return height;
height = _x;
return this;
};
return exports;
};
});