Skip to content

Commit

Permalink
setData() method for Morris.Donut.
Browse files Browse the repository at this point in the history
Fixes #211
  • Loading branch information
oesmith committed Nov 9, 2013
1 parent 88e3fe6 commit c072224
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
11 changes: 8 additions & 3 deletions lib/morris.donut.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class Morris.Donut extends Morris.EventEmitter
# bail if there's no data
if options.data is undefined or options.data.length is 0
return
@data = options.data
@values = (parseFloat(row.value) for row in @data)

@raphael = new Raphael(@el[0])

Expand All @@ -55,7 +53,7 @@ class Morris.Donut extends Morris.EventEmitter
window.clearTimeout @timeoutId
@timeoutId = window.setTimeout @resizeHandler, 100

@redraw()
@setData options.data

# Clear and redraw the chart.
redraw: ->
Expand Down Expand Up @@ -98,6 +96,11 @@ class Morris.Donut extends Morris.EventEmitter
break
idx += 1

setData: (data) ->
@data = data
@values = (parseFloat(row.value) for row in @data)
@redraw()

# @private
click: (idx) =>
@fire 'click', idx, @data[idx]
Expand All @@ -110,6 +113,8 @@ class Morris.Donut extends Morris.EventEmitter
row = @data[idx]
@setLabels(row.label, @options.formatter(row.value, row))



# @private
setLabels: (label1, label2) ->
inner = (Math.min(@el.width() / 2, @el.height() / 2) - 10) * 2 / 3
Expand Down
32 changes: 18 additions & 14 deletions morris.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions morris.min.js

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion spec/lib/donut/donut_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe 'Morris.Donut', ->
$('#graph').find("text").size().should.equal 2

describe 'svg attributes', ->
defaults =
defaults =
element: 'graph'
data: [ {label: 'Jam', value: 25 },
Expand Down Expand Up @@ -59,3 +58,19 @@ describe 'Morris.Donut', ->
it 'should have a path with stroke-width 2', ->
chart = Morris.Donut $.extend {}, defaults
$('#graph').find("path[stroke-width='2']").size().should.equal 4

describe 'setData', ->
defaults =
element: 'graph'
data: [ {label: 'One', value: 25 }, {label: "Two", value: 30} ]
colors: ['#ff0000', '#00ff00', '#0000ff']

it 'should update the chart', ->
chart = Morris.Donut $.extend {}, defaults
$('#graph').find("path[stroke='#0000ff']").size().should.equal 0
chart.setData [
{ label: 'One', value: 25 }
{ label: 'Two', value: 30 }
{ label: 'Three', value: 35 }
]
$('#graph').find("path[stroke='#0000ff']").size().should.equal 1

0 comments on commit c072224

Please sign in to comment.