Merge branch 'master' of github.com:oesmith/morris.js

Conflicts:
	morris.min.js
This commit is contained in:
Olly Smith 2012-09-26 21:30:25 +01:00
commit 9bb723057f
6 changed files with 43 additions and 34 deletions

View File

@ -1,10 +1,8 @@
language: node_js language: node_js
node_js: node_js:
- 0.6 - 0.8
before_script: before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- "npm install -g grunt" - "npm install -g grunt"
- "npm install" - "npm install"
script: script:
- "DISPLAY=:99.0 grunt" - "grunt coffee mocha"

View File

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

View File

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

View File

@ -283,7 +283,7 @@ class Morris.Line
if coords.length > 1 if coords.length > 1
path = @createPath coords, @options.marginTop, @left, @options.marginTop + @height, @left + @width path = @createPath coords, @options.marginTop, @left, @options.marginTop + @height, @left + @width
@r.path(path) @r.path(path)
.attr('stroke', @options.lineColors[i]) .attr('stroke', @colorForSeries(i))
.attr('stroke-width', @options.lineWidth) .attr('stroke-width', @options.lineWidth)
@seriesPoints = ([] for i in [0..@seriesCoords.length-1]) @seriesPoints = ([] for i in [0..@seriesCoords.length-1])
for i in [@seriesCoords.length-1..0] for i in [@seriesCoords.length-1..0]
@ -292,7 +292,7 @@ class Morris.Line
circle = null circle = null
else else
circle = @r.circle(c.x, c.y, @options.pointSize) circle = @r.circle(c.x, c.y, @options.pointSize)
.attr('fill', @options.lineColors[i]) .attr('fill', @colorForSeries(i))
.attr('stroke-width', 1) .attr('stroke-width', 1)
.attr('stroke', '#ffffff') .attr('stroke', '#ffffff')
@seriesPoints[i].push(circle) @seriesPoints[i].push(circle)
@ -355,7 +355,7 @@ class Morris.Line
@yLabels = [] @yLabels = []
for i in [0..@series.length-1] for i in [0..@series.length-1]
yLabel = @r.text(0, @options.hoverFontSize * 1.5 * (i + 1.5) - @hoverHeight / 2, '') yLabel = @r.text(0, @options.hoverFontSize * 1.5 * (i + 1.5) - @hoverHeight / 2, '')
.attr('fill', @options.lineColors[i]) .attr('fill', @colorForSeries(i))
.attr('font-size', @options.hoverFontSize) .attr('font-size', @options.hoverFontSize)
@yLabels.push(yLabel) @yLabels.push(yLabel)
@hoverSet.push(yLabel) @hoverSet.push(yLabel)
@ -423,6 +423,10 @@ class Morris.Line
yLabelFormat: (label) -> yLabelFormat: (label) ->
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}" "#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
# @private
colorForSeries: (index) ->
@options.lineColors[index % @options.lineColors.length]
# Parse a date into a javascript timestamp # Parse a date into a javascript timestamp
# #

View File

@ -64,7 +64,10 @@
Morris.Donut = (function() { 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) { function Donut(options) {
this.select = __bind(this.select, this); this.select = __bind(this.select, this);
@ -76,9 +79,7 @@
} else { } else {
this.el = $(options.element); this.el = $(options.element);
} }
if (options.colors != null) { this.options = $.extend({}, this.defaults, options);
this.colors = options.colors;
}
if (this.el === null || this.el.length === 0) { if (this.el === null || this.el.length === 0) {
throw new Error("Graph placeholder not found."); throw new Error("Graph placeholder not found.");
} }
@ -112,7 +113,7 @@
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
d = _ref1[_j]; d = _ref1[_j];
next = last + min + C * (d.value / total); 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); seg.render(this.r);
this.segments.push(seg); this.segments.push(seg);
seg.on('hover', this.select); seg.on('hover', this.select);
@ -158,7 +159,7 @@
s.deselect(); s.deselect();
} }
segment.select(); 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) { Donut.prototype.setLabels = function(label1, label2) {
@ -604,7 +605,7 @@
}); });
if (coords.length > 1) { if (coords.length > 1) {
path = this.createPath(coords, this.options.marginTop, this.left, this.options.marginTop + this.height, this.left + this.width); path = this.createPath(coords, this.options.marginTop, this.left, this.options.marginTop + this.height, this.left + this.width);
this.r.path(path).attr('stroke', this.options.lineColors[i]).attr('stroke-width', this.options.lineWidth); this.r.path(path).attr('stroke', this.colorForSeries(i)).attr('stroke-width', this.options.lineWidth);
} }
} }
this.seriesPoints = (function() { this.seriesPoints = (function() {
@ -626,7 +627,7 @@
if (c === null) { if (c === null) {
circle = null; circle = null;
} else { } else {
circle = this.r.circle(c.x, c.y, this.options.pointSize).attr('fill', this.options.lineColors[i]).attr('stroke-width', 1).attr('stroke', '#ffffff'); circle = this.r.circle(c.x, c.y, this.options.pointSize).attr('fill', this.colorForSeries(i)).attr('stroke-width', 1).attr('stroke', '#ffffff');
} }
_results1.push(this.seriesPoints[i].push(circle)); _results1.push(this.seriesPoints[i].push(circle));
} }
@ -688,7 +689,7 @@
this.yLabels = []; this.yLabels = [];
_results = []; _results = [];
for (i = _i = 0, _ref = this.series.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { for (i = _i = 0, _ref = this.series.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
yLabel = this.r.text(0, this.options.hoverFontSize * 1.5 * (i + 1.5) - this.hoverHeight / 2, '').attr('fill', this.options.lineColors[i]).attr('font-size', this.options.hoverFontSize); yLabel = this.r.text(0, this.options.hoverFontSize * 1.5 * (i + 1.5) - this.hoverHeight / 2, '').attr('fill', this.colorForSeries(i)).attr('font-size', this.options.hoverFontSize);
this.yLabels.push(yLabel); this.yLabels.push(yLabel);
_results.push(this.hoverSet.push(yLabel)); _results.push(this.hoverSet.push(yLabel));
} }
@ -781,6 +782,10 @@
return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits; return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits;
}; };
Line.prototype.colorForSeries = function(index) {
return this.options.lineColors[index % this.options.lineColors.length];
};
return Line; return Line;
})(); })();

2
morris.min.js vendored

File diff suppressed because one or more lines are too long