setData() method for Morris.Donut.

Fixes #211
This commit is contained in:
Olly Smith
2013-11-09 19:53:50 +00:00
parent 88e3fe6211
commit c07222445c
4 changed files with 44 additions and 20 deletions

View File

@@ -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])
@@ -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: ->
@@ -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]
@@ -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

View File

@@ -1636,8 +1636,7 @@
this.resizeHandler = __bind(this.resizeHandler, this);
this.select = __bind(this.select, this);
this.click = __bind(this.click, this);
var row,
_this = this;
var _this = this;
if (!(this instanceof Morris.Donut)) {
return new Morris.Donut(options);
}
@@ -1653,17 +1652,6 @@
if (options.data === void 0 || options.data.length === 0) {
return;
}
this.data = options.data;
this.values = (function() {
var _i, _len, _ref, _results;
_ref = this.data;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
_results.push(parseFloat(row.value));
}
return _results;
}).call(this);
this.raphael = new Raphael(this.el[0]);
if (this.options.resize) {
$(window).bind('resize', function(evt) {
@@ -1673,7 +1661,7 @@
return _this.timeoutId = window.setTimeout(_this.resizeHandler, 100);
});
}
this.redraw();
this.setData(options.data);
}
Donut.prototype.redraw = function() {
@@ -1722,6 +1710,22 @@
return _results;
};
Donut.prototype.setData = function(data) {
var row;
this.data = data;
this.values = (function() {
var _i, _len, _ref, _results;
_ref = this.data;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
row = _ref[_i];
_results.push(parseFloat(row.value));
}
return _results;
}).call(this);
return this.redraw();
};
Donut.prototype.click = function(idx) {
return this.fire('click', idx, this.data[idx]);
};

4
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -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 },
@@ -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