Refactor path drawing.

This commit is contained in:
Olly Smith 2012-02-28 21:27:33 +00:00
parent 8da7935d8e
commit dc0a34dcbb
4 changed files with 46 additions and 51 deletions

View file

@ -83,7 +83,7 @@
xkey: 'period',
ykeys: ['licensed', 'sorned'],
labels: ['Licensed', 'SORN'],
lineDrawer: 'straight'
smooth: false
});
});
</script>

View file

@ -50,7 +50,7 @@ class Morris.Line
hoverOpacity: 0.95
hoverLabelColor: '#444'
hoverFontSize: 12
lineDrawer: 'spline'
smooth: true
# Do any necessary pre-processing for a new dataset
#
@ -142,9 +142,6 @@ class Morris.Line
for i in [seriesCoords.length-1..0]
coords = seriesCoords[i]
if coords.length > 1
if @options.lineDrawer == 'straight'
path = @createStraightPath coords, @options.marginTop, left, @options.marginTop + height, left + width
else
path = @createPath coords, @options.marginTop, left, @options.marginTop + height, left + width
@r.path(path)
.attr('stroke', @options.lineColors[i])
@ -242,6 +239,7 @@ class Morris.Line
#
createPath: (coords, top, left, bottom, right) ->
path = ""
if @options.smooth
grads = @gradients coords
for i in [0..coords.length-1]
c = coords[i]
@ -257,18 +255,8 @@ class Morris.Line
x2 = c.x - ix
y2 = Math.min(bottom, c.y - ix * g)
path += "C#{x1},#{y1},#{x2},#{y2},#{c.x},#{c.y}"
return path
# creates a straight path for a data series
#
createStraightPath: (coords, top, left, bottom, right) ->
path = ""
for i in [0..coords.length-1]
c = coords[i]
if i is 0
path += "M#{c.x},#{c.y}"
else
path += "L#{c.x},#{c.y}"
path = "M" + $.map(coords, (c) -> "#{c.x},#{c.y}").join("L")
return path
# calculate a gradient at each point for a series of points

View file

@ -8,7 +8,7 @@
function Line(options) {
if (!(this instanceof Morris.Line)) return new Morris.Line(options);
this.el = $(document.getElementById(options.element));
this.options = $.extend($.extend({}, this.defaults), options);
this.options = $.extend({}, this.defaults, options);
if (this.options.data === void 0 || this.options.data.length === 0) return;
this.el.addClass('graph-initialised');
this.precalc();
@ -37,7 +37,8 @@
hoverBorderWidth: 2,
hoverOpacity: 0.95,
hoverLabelColor: '#444',
hoverFontSize: 12
hoverFontSize: 12,
smooth: true
};
Line.prototype.precalc = function() {
@ -250,6 +251,7 @@
Line.prototype.createPath = function(coords, top, left, bottom, right) {
var c, g, grads, i, ix, lc, lg, path, x1, x2, y1, y2, _ref;
path = "";
if (this.options.smooth) {
grads = this.gradients(coords);
for (i = 0, _ref = coords.length - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
c = coords[i];
@ -267,6 +269,11 @@
path += "C" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + c.x + "," + c.y;
}
}
} else {
path = "M" + $.map(coords, function(c) {
return "" + c.x + "," + c.y;
}).join("L");
}
return path;
};

2
morris.min.js vendored

File diff suppressed because one or more lines are too long