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,10 +142,7 @@ 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
path = @createPath coords, @options.marginTop, left, @options.marginTop + height, left + width
@r.path(path)
.attr('stroke', @options.lineColors[i])
.attr('stroke-width', @options.lineWidth)
@ -242,33 +239,24 @@ class Morris.Line
#
createPath: (coords, top, left, bottom, right) ->
path = ""
grads = @gradients coords
for i in [0..coords.length-1]
c = coords[i]
if i is 0
path += "M#{c.x},#{c.y}"
else
g = grads[i]
lc = coords[i - 1]
lg = grads[i - 1]
ix = (c.x - lc.x) / 4
x1 = lc.x + ix
y1 = Math.min(bottom, lc.y + ix * lg)
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}"
if @options.smooth
grads = @gradients coords
for i in [0..coords.length-1]
c = coords[i]
if i is 0
path += "M#{c.x},#{c.y}"
else
g = grads[i]
lc = coords[i - 1]
lg = grads[i - 1]
ix = (c.x - lc.x) / 4
x1 = lc.x + ix
y1 = Math.min(bottom, lc.y + ix * lg)
x2 = c.x - ix
y2 = Math.min(bottom, c.y - ix * g)
path += "C#{x1},#{y1},#{x2},#{y2},#{c.x},#{c.y}"
else
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,22 +251,28 @@
Line.prototype.createPath = function(coords, top, left, bottom, right) {
var c, g, grads, i, ix, lc, lg, path, x1, x2, y1, y2, _ref;
path = "";
grads = this.gradients(coords);
for (i = 0, _ref = coords.length - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
c = coords[i];
if (i === 0) {
path += "M" + c.x + "," + c.y;
} else {
g = grads[i];
lc = coords[i - 1];
lg = grads[i - 1];
ix = (c.x - lc.x) / 4;
x1 = lc.x + ix;
y1 = Math.min(bottom, lc.y + ix * lg);
x2 = c.x - ix;
y2 = Math.min(bottom, c.y - ix * g);
path += "C" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + c.x + "," + c.y;
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];
if (i === 0) {
path += "M" + c.x + "," + c.y;
} else {
g = grads[i];
lc = coords[i - 1];
lg = grads[i - 1];
ix = (c.x - lc.x) / 4;
x1 = lc.x + ix;
y1 = Math.min(bottom, lc.y + ix * lg);
x2 = c.x - ix;
y2 = Math.min(bottom, c.y - ix * g);
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