Skip to content

Commit 221c837

Browse files
committed
Tidy up.
- The options namespace should stay flat - no nested config values. - Used single array literal instead of chained Array.concats. - Added default values to config defaults, instead of using default method arguments. - Tweaked spec.
1 parent 415476f commit 221c837

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

lib/morris.bar.coffee

+13-13
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class Morris.Bar extends Morris.Grid
2525
'#edc240'
2626
'#cb4b4b'
2727
'#9440ed'
28-
]
28+
],
29+
barOpacity: 1.0
30+
barRadius: [0, 0, 0, 0]
2931
xLabelMargin: 50
3032

3133
# Do any size-related calculations
@@ -108,7 +110,8 @@ class Morris.Bar extends Morris.Grid
108110
size = bottom - top
109111

110112
top -= lastTop if @options.stacked
111-
@drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar'), @options.barStyle?.opacity, @options.barStyle?.radius)
113+
@drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar'),
114+
@options.barOpacity, @options.barRadius)
112115

113116
lastTop += size
114117
else
@@ -181,23 +184,20 @@ class Morris.Bar extends Morris.Grid
181184
.attr('font-weight', @options.gridTextWeight)
182185
.attr('fill', @options.gridTextColor)
183186

184-
185-
drawBar: (xPos, yPos, width, height, barColor, opacity = '1', radius = [0,0,0,0]) ->
186-
if Math.max(radius...) > height or (r for r in radius when r is 0).length is 4
187+
drawBar: (xPos, yPos, width, height, barColor, opacity, radiusArray) ->
188+
maxRadius = Math.max(radiusArray...)
189+
if maxRadius == 0 or maxRadius > height
187190
path = @raphael.rect(xPos, yPos, width, height)
188191
else
189-
path = @raphael.path @roundedRect(xPos, yPos, width, height, radius)
190-
191-
192+
path = @raphael.path @roundedRect(xPos, yPos, width, height, radiusArray)
192193
path
193194
.attr('fill', barColor)
194195
.attr('stroke-width', 0)
195196
.attr('fill-opacity', opacity)
196197

197198
roundedRect: (x, y, w, h, r = [0,0,0,0]) ->
198-
[].
199-
concat(["M", x, r[0] + y, "Q", x, y, x + r[0], y]).
200-
concat(["L", x + w - r[1], y, "Q", x + w, y, x + w, y + r[1]]).
201-
concat(["L", x + w, y + h - r[2], "Q", x + w, y + h, x + w - r[2], y + h]).
202-
concat(["L", x + r[3], y + h, "Q", x, y + h, x, y + h - r[3], "Z"])
199+
[ "M", x, r[0] + y, "Q", x, y, x + r[0], y,
200+
"L", x + w - r[1], y, "Q", x + w, y, x + w, y + r[1],
201+
"L", x + w, y + h - r[2], "Q", x + w, y + h, x + w - r[2], y + h,
202+
"L", x + r[3], y + h, "Q", x, y + h, x, y + h - r[3], "Z" ]
203203

spec/lib/bar/bar_spec.coffee

+16-19
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,20 @@ describe 'Morris.Bar', ->
5151

5252
describe 'when setting bar radius', ->
5353
describe 'svg structure', ->
54-
defaults =
55-
element: 'graph'
56-
data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
57-
xkey: 'x'
58-
ykeys: ['y', 'z']
59-
labels: ['Y', 'Z']
60-
barStyle: {
61-
radius: [5, 5, 0, 0]
62-
}
63-
it 'should contain a path for each bar', ->
64-
chart = Morris.Bar $.extend {}, defaults
65-
$('#graph').find("path").size().should.equal 9
54+
defaults =
55+
element: 'graph'
56+
data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
57+
xkey: 'x'
58+
ykeys: ['y', 'z']
59+
labels: ['Y', 'Z']
60+
barRadius: [5, 5, 0, 0]
6661

67-
it 'should use rects if radius is too big', ->
68-
delete defaults.barStyle
69-
chart = Morris.Bar $.extend {
70-
barStyle:
71-
radius: [300, 300, 0, 0]
72-
}, defaults
73-
$('#graph').find("rect").size().should.equal 4
62+
it 'should contain a path for each bar', ->
63+
chart = Morris.Bar $.extend {}, defaults
64+
$('#graph').find("path").size().should.equal 9
65+
66+
it 'should use rects if radius is too big', ->
67+
delete defaults.barStyle
68+
chart = Morris.Bar $.extend {}, defaults,
69+
barRadius: [300, 300, 0, 0]
70+
$('#graph').find("rect").size().should.equal 4

0 commit comments

Comments
 (0)