3
3
/** utils service */
4
4
angular . module ( 'charts.barchart' , [ ] ) . service ( 'barchart' , function ( ) {
5
5
6
- var BarChart = function ( addSearch , maxBarWidth , element ) {
6
+ var BarChart = function ( callback , maxBarWidth , element , noItems ) {
7
7
var me = { } ;
8
8
9
9
me . maxBarWidth = maxBarWidth ;
10
- var sortedData , xScale , yScale , chart , rect , gridContainer , labelContainer , barsContainer ;
10
+ var sortedData , xScale , yScale , chart , rect , labelContainer , barsContainer ;
11
11
12
12
var valueLabelWidth = 40 ; // space reserved for value labels (right)
13
13
var barHeight = 15 ; // height of one bar
14
- var barLabelWidth = 130 ; // space reserved for bar labels
14
+ var barLabelWidth = 120 ; // space reserved for bar labels
15
15
var barLabelPadding = 5 ; // padding between bar and bar labels (left)
16
16
var gridLabelHeight = 18 ; // space reserved for grid line labels
17
- var gridChartOffset = 5 ; // space between start of grid and first bar
18
17
19
18
// accessor functions
20
19
var barLabel = function ( d ) { return d . key ; } ;
@@ -27,26 +26,11 @@ angular.module('charts.barchart', []).service('barchart', function () {
27
26
// svg container element
28
27
chart = d3 . select ( "#" + element . context . id ) . append ( "svg" )
29
28
. attr ( 'width' , maxBarWidth + barLabelWidth + valueLabelWidth )
30
- . attr ( 'height' , gridLabelHeight + gridChartOffset + sortedData . length * barHeight ) ;
29
+ . attr ( 'height' , gridLabelHeight + noItems * barHeight ) ;
31
30
32
- // grid line labels
33
- gridContainer = chart . append ( 'g' )
34
- . attr ( 'transform' , 'translate(' + barLabelWidth + ',' + gridLabelHeight + ')' ) ;
35
- gridContainer . selectAll ( "text" ) . data ( xScale . ticks ( 10 ) ) . enter ( ) . append ( "text" )
36
- . attr ( "x" , xScale )
37
- . attr ( "dy" , - 3 )
38
- . attr ( "text-anchor" , "middle" )
39
- . text ( String ) ;
40
- // vertical grid lines
41
- gridContainer . selectAll ( "line" ) . data ( xScale . ticks ( 10 ) ) . enter ( ) . append ( "line" )
42
- . attr ( "x1" , xScale )
43
- . attr ( "x2" , xScale )
44
- . attr ( "y1" , 0 )
45
- . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] + gridChartOffset )
46
- . style ( "stroke" , "#ccc" ) ;
47
31
// bar labels
48
32
labelContainer = chart . append ( 'g' )
49
- . attr ( 'transform' , 'translate(' + ( barLabelWidth - barLabelPadding ) + ',' + ( gridLabelHeight + gridChartOffset ) + ')' ) ;
33
+ . attr ( 'transform' , 'translate(' + ( barLabelWidth - barLabelPadding ) + ',' + ( gridLabelHeight ) + ')' ) ;
50
34
labelContainer . selectAll ( 'text' ) . data ( sortedData ) . enter ( ) . append ( 'text' )
51
35
. attr ( 'y' , yText )
52
36
. attr ( 'stroke' , 'none' )
@@ -56,7 +40,7 @@ angular.module('charts.barchart', []).service('barchart', function () {
56
40
. text ( barLabel ) ;
57
41
// bars
58
42
barsContainer = chart . append ( 'g' )
59
- . attr ( 'transform' , 'translate(' + barLabelWidth + ',' + ( gridLabelHeight + gridChartOffset ) + ')' ) ;
43
+ . attr ( 'transform' , 'translate(' + barLabelWidth + ',' + gridLabelHeight + ')' ) ;
60
44
barsContainer . selectAll ( "rect" ) . data ( sortedData ) . enter ( ) . append ( "rect" )
61
45
. attr ( 'y' , y )
62
46
. attr ( 'height' , yScale . rangeBand ( ) )
@@ -65,7 +49,7 @@ angular.module('charts.barchart', []).service('barchart', function () {
65
49
. attr ( 'fill' , '#428bca' )
66
50
. on ( "click" , function ( d ) {
67
51
var tag = barLabel ( d ) . replace ( '#' , '' ) ;
68
- addSearch ( tag ) ;
52
+ callback ( tag ) ;
69
53
} ) ;
70
54
71
55
// bar value labels
@@ -80,8 +64,8 @@ angular.module('charts.barchart', []).service('barchart', function () {
80
64
. text ( function ( d ) { return d3 . round ( barValue ( d ) , 2 ) ; } ) ;
81
65
// start line
82
66
barsContainer . append ( "line" )
83
- . attr ( "y1" , - gridChartOffset )
84
- . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] + gridChartOffset )
67
+ . attr ( "y1" , 0 )
68
+ . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] )
85
69
. style ( "stroke" , "#000" ) ;
86
70
}
87
71
@@ -105,7 +89,7 @@ angular.module('charts.barchart', []).service('barchart', function () {
105
89
106
90
me . redraw = function ( dataSource ) {
107
91
me . update ( dataSource ) ;
108
-
92
+
109
93
barsContainer . selectAll ( "rect" ) . data ( sortedData ) . enter ( ) . append ( "rect" )
110
94
. attr ( 'y' , y )
111
95
. attr ( 'height' , yScale . rangeBand ( ) )
@@ -114,7 +98,7 @@ angular.module('charts.barchart', []).service('barchart', function () {
114
98
. attr ( 'fill' , 'steelblue' )
115
99
. on ( "click" , function ( d ) {
116
100
var tag = barLabel ( d ) . replace ( '#' , '' ) ;
117
- addSearch ( tag ) ;
101
+ callback ( tag ) ;
118
102
} ) ;
119
103
120
104
barsContainer . selectAll ( "rect" )
@@ -123,7 +107,7 @@ angular.module('charts.barchart', []).service('barchart', function () {
123
107
. attr ( 'y' , y )
124
108
. attr ( 'height' , yScale . rangeBand ( ) )
125
109
. attr ( 'width' , function ( d ) { return xScale ( barValue ( d ) ) ; } ) ;
126
-
110
+
127
111
barsContainer . selectAll ( "rect" )
128
112
. data ( sortedData )
129
113
. exit ( )
@@ -137,7 +121,7 @@ angular.module('charts.barchart', []).service('barchart', function () {
137
121
. attr ( "dy" , ".35em" ) // vertical-align: middle
138
122
. attr ( "text-anchor" , "start" ) // text-align: right
139
123
. attr ( "fill" , "black" )
140
- . attr ( "stroke" , "none " )
124
+ . attr ( "stroke" , "black " )
141
125
. text ( function ( d ) { return d3 . round ( barValue ( d ) , 2 ) ; } ) ;
142
126
143
127
barsContainer . selectAll ( "text" ) . data ( sortedData )
@@ -157,11 +141,6 @@ angular.module('charts.barchart', []).service('barchart', function () {
157
141
. transition ( )
158
142
. remove ( ) ;
159
143
160
- barsContainer . selectAll ( "line" )
161
- . attr ( "y1" , - gridChartOffset )
162
- . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] + gridChartOffset )
163
- . style ( "stroke" , "#000" ) ;
164
-
165
144
labelContainer . selectAll ( "text" ) . data ( sortedData ) . enter ( ) . append ( "text" )
166
145
. attr ( 'y' , yText )
167
146
. attr ( 'stroke' , 'none' )
@@ -185,34 +164,9 @@ angular.module('charts.barchart', []).service('barchart', function () {
185
164
. transition ( )
186
165
. remove ( ) ;
187
166
188
- /** insert, update and change grid text */
189
- var gridText = gridContainer . selectAll ( "text" ) . data ( xScale . ticks ( 10 ) ) ;
190
-
191
- gridText . enter ( ) . insert ( "text" )
192
- . attr ( "x" , xScale )
193
- . attr ( "dy" , - 3 )
194
- . attr ( "text-anchor" , "middle" )
195
- . text ( String ) ;
196
-
197
- gridText . transition ( )
198
- . attr ( "x" , xScale )
199
- . attr ( "dy" , - 3 )
200
- . attr ( "text-anchor" , "middle" )
201
- . text ( String ) ;
202
-
203
- gridText . exit ( ) . remove ( ) ;
204
-
205
- /** insert, update and change grid lines */
206
- gridContainer . selectAll ( "line" ) . data ( xScale . ticks ( 10 ) )
207
- . attr ( "x1" , xScale )
208
- . attr ( "x2" , xScale )
209
- . attr ( "y1" , 0 )
210
- . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] + gridChartOffset )
211
- . style ( "stroke" , "#ccc" ) . exit ( ) . remove ( ) ;
212
-
213
167
barsContainer . append ( "line" )
214
- . attr ( "y1" , - gridChartOffset )
215
- . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] + gridChartOffset )
168
+ . attr ( "y1" , 0 )
169
+ . attr ( "y2" , yScale . rangeExtent ( ) [ 1 ] )
216
170
. style ( "stroke" , "#000" ) ;
217
171
} ;
218
172
0 commit comments