Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: round-corners for bars in barchart #305

Merged
merged 1 commit into from
Nov 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/morris.bar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Morris.Bar extends Morris.Grid
size = bottom - top

top -= lastTop if @options.stacked
@drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar'))
@drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar'), @options.barStyle?.opacity, @options.barStyle?.radius)

lastTop += size
else
Expand Down Expand Up @@ -181,7 +181,23 @@ class Morris.Bar extends Morris.Grid
.attr('font-weight', @options.gridTextWeight)
.attr('fill', @options.gridTextColor)

drawBar: (xPos, yPos, width, height, barColor) ->
@raphael.rect(xPos, yPos, width, height)

drawBar: (xPos, yPos, width, height, barColor, opacity = '1', radius = [0,0,0,0]) ->
if Math.max(radius...) > height or (r for r in radius when r is 0).length is 4
path = @raphael.rect(xPos, yPos, width, height)
else
path = @raphael.path @roundedRect(xPos, yPos, width, height, radius)


path
.attr('fill', barColor)
.attr('stroke-width', 0)
.attr('fill-opacity', opacity)

roundedRect: (x, y, w, h, r = [0,0,0,0]) ->
[].
concat(["M", x, r[0] + y, "Q", x, y, x + r[0], y]).
concat(["L", x + w - r[1], y, "Q", x + w, y, x + w, y + r[1]]).
concat(["L", x + w, y + h - r[2], "Q", x + w, y + h, x + w - r[2], y + h]).
concat(["L", x + r[3], y + h, "Q", x, y + h, x, y + h - r[3], "Z"])

23 changes: 23 additions & 0 deletions spec/lib/bar/bar_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ describe 'Morris.Bar', ->
it 'should have text with configured font size', ->
chart = Morris.Bar $.extend {}, defaults
$('#graph').find("text[font-size='12px']").size().should.equal 7

describe 'when setting bar radius', ->
describe 'svg structure', ->
defaults =
element: 'graph'
data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['Y', 'Z']
barStyle: {
radius: [5, 5, 0, 0]
}
it 'should contain a path for each bar', ->
chart = Morris.Bar $.extend {}, defaults
$('#graph').find("path").size().should.equal 9

it 'should use rects if radius is too big', ->
delete defaults.barStyle
chart = Morris.Bar $.extend {
barStyle:
radius: [300, 300, 0, 0]
}, defaults
$('#graph').find("rect").size().should.equal 4