Implement #311: feature - bar max width

This commit is contained in:
sudodoki 2014-01-30 01:57:57 +02:00
parent 7bce55890f
commit e3e50aac93
2 changed files with 31 additions and 1 deletions

View File

@ -92,7 +92,9 @@ class Morris.Bar extends Morris.Grid
groupWidth = @width / @options.data.length groupWidth = @width / @options.data.length
numBars = if @options.stacked? then 1 else @options.ykeys.length numBars = if @options.stacked? then 1 else @options.ykeys.length
barWidth = (groupWidth * @options.barSizeRatio - @options.barGap * (numBars - 1)) / numBars barWidth = (groupWidth * @options.barSizeRatio - @options.barGap * (numBars - 1)) / numBars
leftPadding = groupWidth * (1 - @options.barSizeRatio) / 2 barWidth = Math.min(barWidth, @options.barSize) if @options.barSize
spaceLeft = groupWidth - barWidth * numBars - @options.barGap * (numBars - 1)
leftPadding = spaceLeft / 2
zeroPos = if @ymin <= 0 and @ymax >= 0 then @transY(0) else null zeroPos = if @ymin <= 0 and @ymax >= 0 then @transY(0) else null
@bars = for row, idx in @data @bars = for row, idx in @data
lastTop = 0 lastTop = 0

View File

@ -68,3 +68,31 @@ describe 'Morris.Bar', ->
chart = Morris.Bar $.extend {}, defaults, chart = Morris.Bar $.extend {}, defaults,
barRadius: [300, 300, 0, 0] barRadius: [300, 300, 0, 0]
$('#graph').find("rect").size().should.equal 4 $('#graph').find("rect").size().should.equal 4
describe 'barSize option', ->
describe 'svg attributes', ->
defaults =
element: 'graph'
barSize: 20
data: [
{x: '2011 Q1', y: 3, z: 2, a: 3}
{x: '2011 Q2', y: 2, z: null, a: 1}
{x: '2011 Q3', y: 0, z: 2, a: 4}
{x: '2011 Q4', y: 2, z: 4, a: 3}
],
xkey: 'x'
ykeys: ['y', 'z', 'a']
labels: ['Y', 'Z', 'A']
it 'should calc the width if too narrow for barSize', ->
$('#graph').width('200px')
chart = Morris.Bar $.extend {}, defaults
$('#graph').find("rect").filter((i) ->
parseFloat($(@).attr('width'), 10) < 10
).size().should.equal 11
it 'should set width to @options.barSize if possible', ->
chart = Morris.Bar $.extend {}, defaults
$('#graph').find("rect[width='#{defaults.barSize}']").size().should.equal 11