Commit e7634c64 authored by David Schnur's avatar David Schnur

Merge pull request #5 from smashedpumpkin/fix-issue-520

Added a 'right' option for bar alignment.
parents ad02cfbe 40d886f9
...@@ -564,7 +564,7 @@ Customizing the data series ...@@ -564,7 +564,7 @@ Customizing the data series
bars: { bars: {
barWidth: number barWidth: number
align: "left" or "center" align: "left", "right" or "center"
horizontal: boolean horizontal: boolean
} }
...@@ -615,9 +615,9 @@ the y axis if "horizontal" is true), contrary to most other measures ...@@ -615,9 +615,9 @@ the y axis if "horizontal" is true), contrary to most other measures
that are specified in pixels. For instance, for time series the unit that are specified in pixels. For instance, for time series the unit
is milliseconds so 24 * 60 * 60 * 1000 produces bars with the width of is milliseconds so 24 * 60 * 60 * 1000 produces bars with the width of
a day. "align" specifies whether a bar should be left-aligned a day. "align" specifies whether a bar should be left-aligned
(default) or centered on top of the value it represents. When (default), right-aligned or centered on top of the value it represents.
"horizontal" is on, the bars are drawn horizontally, i.e. from the y When "horizontal" is on, the bars are drawn horizontally, i.e. from the
axis instead of the x axis; note that the bar end points are still y axis instead of the x axis; note that the bar end points are still
defined in the same way so you'll probably want to swap the defined in the same way so you'll probably want to swap the
coordinates if you've been plotting vertical bars first. coordinates if you've been plotting vertical bars first.
......
...@@ -110,7 +110,7 @@ ...@@ -110,7 +110,7 @@
barWidth: 1, // in units of the x axis barWidth: 1, // in units of the x axis
fill: true, fill: true,
fillColor: null, fillColor: null,
align: "left", // or "center" align: "left", // "left", "right", or "center"
horizontal: false horizontal: false
}, },
shadowSize: 3 shadowSize: 3
...@@ -668,7 +668,22 @@ ...@@ -668,7 +668,22 @@
if (s.bars.show) { if (s.bars.show) {
// make sure we got room for the bar on the dancing floor // make sure we got room for the bar on the dancing floor
var delta = s.bars.align == "left" ? 0 : -s.bars.barWidth/2; var delta;
switch (s.bars.align) {
case 'left':
delta = 0;
break;
case 'right':
delta = -s.bars.barWidth;
break;
case 'center':
delta = -s.bars.barWidth / 2;
break;
default:
throw 'Invalid bar alignment: ' + s.bars.align;
}
if (s.bars.horizontal) { if (s.bars.horizontal) {
ymin += delta; ymin += delta;
ymax += delta + s.bars.barWidth; ymax += delta + s.bars.barWidth;
...@@ -2210,7 +2225,23 @@ ...@@ -2210,7 +2225,23 @@
// FIXME: figure out a way to add shadows (for instance along the right edge) // FIXME: figure out a way to add shadows (for instance along the right edge)
ctx.lineWidth = series.bars.lineWidth; ctx.lineWidth = series.bars.lineWidth;
ctx.strokeStyle = series.color; ctx.strokeStyle = series.color;
var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
var barLeft;
switch (series.bars.align) {
case 'left':
barLeft = 0;
break;
case 'right':
barLeft = -series.bars.barWidth;
break;
case 'center':
barLeft = -series.bars.barWidth / 2;
break;
default:
throw 'Invalid bar alignment: ' + series.bars.align;
}
var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null; var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis); plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, 0, fillStyleCallback, series.xaxis, series.yaxis);
ctx.restore(); ctx.restore();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment