Commit b993ceef authored by olau@iola.dk's avatar olau@iola.dk

Horizontal bars, based on patch from Jason LeBrun

git-svn-id: https://flot.googlecode.com/svn/trunk@141 1e0a6537-2640-0410-bfb7-f154510ff394
parent 74661de1
...@@ -421,6 +421,7 @@ Customizing the data series ...@@ -421,6 +421,7 @@ Customizing the data series
bars: { bars: {
barWidth: number barWidth: number
align: "left" or "center" align: "left" or "center"
horizontal: boolean
} }
lines: { lines: {
...@@ -466,7 +467,11 @@ contrary to most other measures that are specified in pixels. For ...@@ -466,7 +467,11 @@ contrary to most other measures that are specified in pixels. For
instance, for time series the unit is milliseconds so 24 * 60 * 60 * instance, for time series the unit is milliseconds so 24 * 60 * 60 *
1000 produces bars with the width of a day. "align" specifies whether 1000 produces bars with the width of a day. "align" specifies whether
a bar should be left-aligned (default) or centered on top of the value a bar should be left-aligned (default) or centered on top of the value
it represents. it represents. When "horizontal" is on, the bars are drawn
horizontally, i.e. from the 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 coordinates if you've been plotting vertical
bars first.
For lines, "steps" specifies whether two adjacent data points are For lines, "steps" specifies whether two adjacent data points are
connected with a straight (possibly diagonal) line or with first a connected with a straight (possibly diagonal) line or with first a
......
...@@ -56,6 +56,8 @@ Changes: ...@@ -56,6 +56,8 @@ Changes:
- The legend labelFormatter now passes the series in addition to just - The legend labelFormatter now passes the series in addition to just
the label (suggestion by Vincent Lemeltier). the label (suggestion by Vincent Lemeltier).
- Horizontal bars (based on patch by Jason LeBrun).
Bug fixes: Bug fixes:
- Fixed two corner-case bugs when drawing filled curves (report and - Fixed two corner-case bugs when drawing filled curves (report and
......
...@@ -73,7 +73,8 @@ ...@@ -73,7 +73,8 @@
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", // or "center"
horizontal: false // when horizontal, left is now top
}, },
threshold: null, // or { below: number, color: color spec} threshold: null, // or { below: number, color: color spec}
grid: { grid: {
...@@ -344,11 +345,17 @@ ...@@ -344,11 +345,17 @@
} }
if (s.bars.show) { if (s.bars.show) {
// make sure we got room for the bar // 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 = s.bars.align == "left" ? 0 : -s.bars.barWidth/2;
if(s.bars.horizontal) {
ymin += delta;
ymax += delta + s.bars.barWidth;
}
else {
xmin += delta; xmin += delta;
xmax += delta + s.bars.barWidth; xmax += delta + s.bars.barWidth;
} }
}
axisx.datamin = Math.min(axisx.datamin, xmin); axisx.datamin = Math.min(axisx.datamin, xmin);
axisx.datamax = Math.max(axisx.datamax, xmax); axisx.datamax = Math.max(axisx.datamax, xmax);
...@@ -1458,11 +1465,33 @@ ...@@ -1458,11 +1465,33 @@
ctx.restore(); ctx.restore();
} }
function drawBar(x, y, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c) { function drawBar(x, y, barLeft, barRight, offset, fillStyleCallback, axisx, axisy, c, horizontal) {
var drawLeft = true, drawRight = true, var left, right, bottom, top,
drawTop = true, drawBottom = false, drawLeft, drawRight, drawTop, drawBottom;
left = x + barLeft, right = x + barRight,
bottom = 0, top = y; if (horizontal) {
drawBottom = drawRight = drawTop = true;
drawLeft = false;
left = 0;
right = x;
top = y + barLeft;
bottom = y + barRight;
// account for negative bars
if (right < left) {
right = 0;
left = x;
drawLeft = true;
drawRight = false;
}
}
else {
drawLeft = drawRight = drawTop = true;
drawBottom = false;
left = x + barLeft;
right = x + barRight;
bottom = 0;
top = y;
// account for negative bars // account for negative bars
if (top < bottom) { if (top < bottom) {
...@@ -1471,6 +1500,7 @@ ...@@ -1471,6 +1500,7 @@
drawBottom = true; drawBottom = true;
drawTop = false; drawTop = false;
} }
}
// clip // clip
if (right < axisx.min || left > axisx.max || if (right < axisx.min || left > axisx.max ||
...@@ -1546,7 +1576,7 @@ ...@@ -1546,7 +1576,7 @@
for (var i = 0; i < points.length; i += incr) { for (var i = 0; i < points.length; i += incr) {
if (points[i] == null) if (points[i] == null)
continue; continue;
drawBar(points[i], points[i + 1], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx); drawBar(points[i], points[i + 1], barLeft, barRight, offset, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal);
} }
} }
...@@ -1714,9 +1744,12 @@ ...@@ -1714,9 +1744,12 @@
if (x == null) if (x == null)
continue; continue;
// For a bar graph, the cursor must be inside the bar // for a bar graph, the cursor must be inside the bar
if (mx >= x + barLeft && mx <= x + barRight && if (series[i].bars.horizontal ?
my >= Math.min(0, y) && my <= Math.max(0, y)) (mx <= Math.max(0, x) && mx >= Math.min(0, x) &&
my >= y + barLeft && my <= y + barRight) :
(mx >= x + barLeft && mx <= x + barRight &&
my >= Math.min(0, y) && my <= Math.max(0, y)))
item = [i, j / incr]; item = [i, j / incr];
} }
} }
...@@ -1988,7 +2021,7 @@ ...@@ -1988,7 +2021,7 @@
var fillStyle = parseColor(series.color).scale(1, 1, 1, 0.5).toString(); var fillStyle = parseColor(series.color).scale(1, 1, 1, 0.5).toString();
var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2; var barLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
drawBar(point[0], point[1], barLeft, barLeft + series.bars.barWidth, drawBar(point[0], point[1], barLeft, barLeft + series.bars.barWidth,
0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx); 0, function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal);
} }
function setPositionFromEvent(pos, e) { function setPositionFromEvent(pos, e) {
......
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