Formatter option for donut values (#75).

This commit is contained in:
Olly Smith 2012-09-20 07:31:31 +01:00
parent 088233d8d2
commit fbe3ce8201
4 changed files with 27 additions and 24 deletions

View File

@ -19,7 +19,8 @@ Morris.Donut({
{value: 15, label: 'bar'},
{value: 10, label: 'baz'},
{value: 5, label: 'A really really long label'}
]
],
formatter: function (x) { return x + "%"}
});
</pre>
</body>

View File

@ -9,18 +9,20 @@
# ]
# });
class Morris.Donut
colors: [
'#0B62A4'
'#3980B5'
'#679DC6'
'#95BBD7'
'#B0CCE1'
'#095791'
'#095085'
'#083E67'
'#052C48'
'#042135'
]
defaults:
colors: [
'#0B62A4'
'#3980B5'
'#679DC6'
'#95BBD7'
'#B0CCE1'
'#095791'
'#095085'
'#083E67'
'#052C48'
'#042135'
],
formatter: Morris.commas
# Create and render a donut chart.
#
@ -33,8 +35,7 @@ class Morris.Donut
else
@el = $ options.element
if options.colors?
@colors = options.colors
@options = $.extend {}, @defaults, options
if @el == null || @el.length == 0
throw new Error("Graph placeholder not found.")
@ -72,7 +73,7 @@ class Morris.Donut
@segments = []
for d in @data
next = last + min + C * (d.value / total)
seg = new Morris.DonutSegment(cx, cy, w*2, w, last, next, @colors[idx % @colors.length], d)
seg = new Morris.DonutSegment(cx, cy, w*2, w, last, next, @options.colors[idx % @options.colors.length], d)
seg.render @r
@segments.push seg
seg.on 'hover', @select
@ -92,7 +93,7 @@ class Morris.Donut
select: (segment) =>
s.deselect() for s in @segments
segment.select()
@setLabels segment.data.label, Morris.commas(segment.data.value)
@setLabels segment.data.label, @options.formatter(segment.data.value)
# @private
setLabels: (label1, label2) ->

View File

@ -64,7 +64,10 @@
Morris.Donut = (function() {
Donut.prototype.colors = ['#0B62A4', '#3980B5', '#679DC6', '#95BBD7', '#B0CCE1', '#095791', '#095085', '#083E67', '#052C48', '#042135'];
Donut.prototype.defaults = {
colors: ['#0B62A4', '#3980B5', '#679DC6', '#95BBD7', '#B0CCE1', '#095791', '#095085', '#083E67', '#052C48', '#042135'],
formatter: Morris.commas
};
function Donut(options) {
this.select = __bind(this.select, this);
@ -76,9 +79,7 @@
} else {
this.el = $(options.element);
}
if (options.colors != null) {
this.colors = options.colors;
}
this.options = $.extend({}, this.defaults, options);
if (this.el === null || this.el.length === 0) {
throw new Error("Graph placeholder not found.");
}
@ -112,7 +113,7 @@
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
d = _ref1[_j];
next = last + min + C * (d.value / total);
seg = new Morris.DonutSegment(cx, cy, w * 2, w, last, next, this.colors[idx % this.colors.length], d);
seg = new Morris.DonutSegment(cx, cy, w * 2, w, last, next, this.options.colors[idx % this.options.colors.length], d);
seg.render(this.r);
this.segments.push(seg);
seg.on('hover', this.select);
@ -158,7 +159,7 @@
s.deselect();
}
segment.select();
return this.setLabels(segment.data.label, Morris.commas(segment.data.value));
return this.setLabels(segment.data.label, this.options.formatter(segment.data.value));
};
Donut.prototype.setLabels = function(label1, label2) {

2
morris.min.js vendored

File diff suppressed because one or more lines are too long