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

Implemented centering of bars

git-svn-id: https://flot.googlecode.com/svn/trunk@85 1e0a6537-2640-0410-bfb7-f154510ff394
parent 1d30a164
...@@ -397,6 +397,7 @@ Customizing the data series ...@@ -397,6 +397,7 @@ Customizing the data series
bars: { bars: {
barWidth: number barWidth: number
align: "left" or "center"
} }
colors: [ color1, color2, ... ] colors: [ color1, color2, ... ]
...@@ -425,7 +426,9 @@ between 0 (fully transparent) and 1 (fully opaque). ...@@ -425,7 +426,9 @@ between 0 (fully transparent) and 1 (fully opaque).
"barWidth" is the width of the bars in units of the x axis, contrary "barWidth" is the width of the bars in units of the x axis, contrary
to most other measures that are specified in pixels. For instance, for to most other measures that are specified in pixels. For instance, for
time series the unit is milliseconds so 24 * 60 * 60 * 1000 produces time series the unit is milliseconds so 24 * 60 * 60 * 1000 produces
bars with the width of a day. bars with the width of a day. "align" specifies whether a bar should
be left-aligned (default) or centered on top of the value it
represents.
The "colors" array specifies a default color theme to get colors for The "colors" array specifies a default color theme to get colors for
the data series from. You can specify as many colors as you like, like the data series from. You can specify as many colors as you like, like
......
...@@ -43,6 +43,9 @@ You can now specify a coordinate as null (like [2, null]) and Flot ...@@ -43,6 +43,9 @@ You can now specify a coordinate as null (like [2, null]) and Flot
will take the other coordinate into account when scaling the axes will take the other coordinate into account when scaling the axes
(based on patch by joebno). (based on patch by joebno).
New option for bars "align". Set it to "center" to center the bars on
the value they represent.
Using the "container" option in legend now overwrites the container Using the "container" option in legend now overwrites the container
element instead of just appending to it (fixes infinite legend bug, element instead of just appending to it (fixes infinite legend bug,
reported by several people, fix by Brad Dewey). reported by several people, fix by Brad Dewey).
......
...@@ -70,7 +70,8 @@ ...@@ -70,7 +70,8 @@
lineWidth: 2, // in pixels lineWidth: 2, // in pixels
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"
}, },
grid: { grid: {
color: "#545454", // primary color used for outline and labels color: "#545454", // primary color used for outline and labels
...@@ -255,8 +256,10 @@ ...@@ -255,8 +256,10 @@
mindelta = 0, maxdelta = 0; mindelta = 0, maxdelta = 0;
// make sure we got room for the bar // make sure we got room for the bar
if (series[i].bars.show) if (series[i].bars.show) {
maxdelta = series[i].bars.barWidth; var mindelta = series[i].bars.align == "left" ? 0 : -series[i].bars.barWidth/2;
maxdelta = mindelta + series[i].bars.barWidth;
}
axisx.used = axisy.used = true; axisx.used = axisy.used = true;
for (var j = 0; j < data.length; ++j) { for (var j = 0; j < data.length; ++j) {
...@@ -267,8 +270,8 @@ ...@@ -267,8 +270,8 @@
// convert to number // convert to number
if (x != null && !isNaN(x = +x)) { if (x != null && !isNaN(x = +x)) {
if (x - mindelta < axisx.datamin) if (x + mindelta < axisx.datamin)
axisx.datamin = x - mindelta; axisx.datamin = x + mindelta;
if (x + maxdelta > axisx.datamax) if (x + maxdelta > axisx.datamax)
axisx.datamax = x + maxdelta; axisx.datamax = x + maxdelta;
} }
...@@ -1291,7 +1294,7 @@ ...@@ -1291,7 +1294,7 @@
} }
function drawSeriesBars(series) { function drawSeriesBars(series) {
function plotBars(data, barWidth, offset, fill, axisx, axisy) { function plotBars(data, deltaLeft, deltaRight, offset, fill, axisx, axisy) {
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
if (data[i] == null) if (data[i] == null)
continue; continue;
...@@ -1301,7 +1304,7 @@ ...@@ -1301,7 +1304,7 @@
// determine the co-ordinates of the bar, account for negative bars having // determine the co-ordinates of the bar, account for negative bars having
// flipped top/bottom and draw/don't draw accordingly // flipped top/bottom and draw/don't draw accordingly
var left = x, right = x + barWidth, bottom = (y < 0 ? y : 0), top = (y < 0 ? 0 : y); var left = x + deltaLeft, right = x + deltaRight, bottom = (y < 0 ? y : 0), top = (y < 0 ? 0 : y);
if (right < axisx.min || left > axisx.max || top < axisy.min || bottom > axisy.max) if (right < axisx.min || left > axisx.max || top < axisy.min || bottom > axisy.max)
continue; continue;
...@@ -1379,7 +1382,8 @@ ...@@ -1379,7 +1382,8 @@
ctx.lineWidth = series.bars.lineWidth; ctx.lineWidth = series.bars.lineWidth;
ctx.strokeStyle = series.color; ctx.strokeStyle = series.color;
setFillStyle(series.bars, series.color); setFillStyle(series.bars, series.color);
plotBars(series.data, series.bars.barWidth, 0, series.bars.fill, series.xaxis, series.yaxis); var deltaLeft = series.bars.align == "left" ? 0 : -series.bars.barWidth/2;
plotBars(series.data, deltaLeft, deltaLeft + series.bars.barWidth, 0, series.bars.fill, 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