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

Fix a bug with replotting where Flot would fixate the label sizes,

shuffle the label measuring code around a bit while at it


git-svn-id: https://flot.googlecode.com/svn/trunk@178 1e0a6537-2640-0410-bfb7-f154510ff394
parent aebbf14b
...@@ -581,6 +581,87 @@ ...@@ -581,6 +581,87 @@
} }
} }
function measureLabels(axis, axisOptions) {
var i, labels = [], l;
axis.labelWidth = axisOptions.labelWidth;
axis.labelHeight = axisOptions.labelHeight;
if (axis == axes.xaxis || axis == axes.x2axis) {
// to avoid measuring the widths of the labels, we
// construct fixed-size boxes and put the labels inside
// them, we don't need the exact figures and the
// fixed-size box content is easy to center
if (axis.labelWidth == null)
axis.labelWidth = canvasWidth / 6;
// measure x label heights
if (axis.labelHeight == null) {
labels = [];
for (i = 0; i < axis.ticks.length; ++i) {
l = axis.ticks[i].label;
if (l)
labels.push('<div class="tickLabel" style="float:left;width:' + axis.labelWidth + 'px">' + l + '</div>');
}
if (labels.length > 0) {
var dummyDiv = $('<div style="position:absolute;top:-10000px;width:10000px;font-size:smaller">'
+ labels.join("") + '<div style="clear:left"></div></div>').appendTo(target);
axis.labelHeight = dummyDiv.height();
dummyDiv.remove();
}
}
}
else if (axis.labelWidth == null || axis.labelHeight == null) {
// calculate y label dimensions
for (i = 0; i < axis.ticks.length; ++i) {
l = axis.ticks[i].label;
if (l)
labels.push('<div class="tickLabel">' + l + '</div>');
}
if (labels.length > 0) {
var dummyDiv = $('<div style="position:absolute;top:-10000px;font-size:smaller">'
+ labels.join("") + '</div>').appendTo(target);
if (axis.labelWidth == null)
axis.labelWidth = dummyDiv.width();
if (axis.labelHeight == null)
axis.labelHeight = dummyDiv.find("div").height();
dummyDiv.remove();
}
}
if (axis.labelWidth == null)
axis.labelWidth = 0;
if (axis.labelHeight == null)
axis.labelHeight = 0;
}
function setGridSpacing() {
// get the most space needed around the grid for things
// that may stick out
var maxOutset = options.grid.borderWidth;
for (i = 0; i < series.length; ++i)
maxOutset = Math.max(maxOutset, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = maxOutset;
var margin = options.grid.labelMargin + options.grid.borderWidth;
if (axes.xaxis.labelHeight > 0)
plotOffset.bottom = Math.max(maxOutset, axes.xaxis.labelHeight + margin);
if (axes.yaxis.labelWidth > 0)
plotOffset.left = Math.max(maxOutset, axes.yaxis.labelWidth + margin);
if (axes.x2axis.labelHeight > 0)
plotOffset.top = Math.max(maxOutset, axes.x2axis.labelHeight + margin);
if (axes.y2axis.labelWidth > 0)
plotOffset.right = Math.max(maxOutset, axes.y2axis.labelWidth + margin);
plotWidth = canvasWidth - plotOffset.left - plotOffset.right;
plotHeight = canvasHeight - plotOffset.bottom - plotOffset.top;
}
var axis; var axis;
for (axis in axes) for (axis in axes)
setRange(axes[axis], options[axis]); setRange(axes[axis], options[axis]);
...@@ -589,6 +670,7 @@ ...@@ -589,6 +670,7 @@
for (axis in axes) { for (axis in axes) {
prepareTickGeneration(axes[axis], options[axis]); prepareTickGeneration(axes[axis], options[axis]);
setTicks(axes[axis], options[axis]); setTicks(axes[axis], options[axis]);
measureLabels(axes[axis], options[axis]);
} }
setGridSpacing(); setGridSpacing();
...@@ -882,10 +964,6 @@ ...@@ -882,10 +964,6 @@
axis.tickFormatter = function (v, axis) { return "" + axisOptions.tickFormatter(v, axis); }; axis.tickFormatter = function (v, axis) { return "" + axisOptions.tickFormatter(v, axis); };
else else
axis.tickFormatter = formatter; axis.tickFormatter = formatter;
if (axisOptions.labelWidth != null)
axis.labelWidth = axisOptions.labelWidth;
if (axisOptions.labelHeight != null)
axis.labelHeight = axisOptions.labelHeight;
} }
function setTicks(axis, axisOptions) { function setTicks(axis, axisOptions) {
...@@ -934,91 +1012,6 @@ ...@@ -934,91 +1012,6 @@
} }
} }
function setGridSpacing() {
function measureXLabels(axis) {
// to avoid measuring the widths of the labels, we
// construct fixed-size boxes and put the labels inside
// them, we don't need the exact figures and the
// fixed-size box content is easy to center
if (axis.labelWidth == null)
axis.labelWidth = canvasWidth / 6;
// measure x label heights
if (axis.labelHeight == null) {
labels = [];
for (i = 0; i < axis.ticks.length; ++i) {
l = axis.ticks[i].label;
if (l)
labels.push('<div class="tickLabel" style="float:left;width:' + axis.labelWidth + 'px">' + l + '</div>');
}
axis.labelHeight = 0;
if (labels.length > 0) {
var dummyDiv = $('<div style="position:absolute;top:-10000px;width:10000px;font-size:smaller">'
+ labels.join("") + '<div style="clear:left"></div></div>').appendTo(target);
axis.labelHeight = dummyDiv.height();
dummyDiv.remove();
}
}
}
function measureYLabels(axis) {
if (axis.labelWidth == null || axis.labelHeight == null) {
var i, labels = [], l;
// calculate y label dimensions
for (i = 0; i < axis.ticks.length; ++i) {
l = axis.ticks[i].label;
if (l)
labels.push('<div class="tickLabel">' + l + '</div>');
}
if (labels.length > 0) {
var dummyDiv = $('<div style="position:absolute;top:-10000px;font-size:smaller">'
+ labels.join("") + '</div>').appendTo(target);
if (axis.labelWidth == null)
axis.labelWidth = dummyDiv.width();
if (axis.labelHeight == null)
axis.labelHeight = dummyDiv.find("div").height();
dummyDiv.remove();
}
if (axis.labelWidth == null)
axis.labelWidth = 0;
if (axis.labelHeight == null)
axis.labelHeight = 0;
}
}
// get the most space needed around the grid for things
// that may stick out
var maxOutset = options.grid.borderWidth;
for (i = 0; i < series.length; ++i)
maxOutset = Math.max(maxOutset, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = maxOutset;
var margin = options.grid.labelMargin + options.grid.borderWidth;
measureXLabels(axes.xaxis);
measureYLabels(axes.yaxis);
measureXLabels(axes.x2axis);
measureYLabels(axes.y2axis);
if (axes.xaxis.labelHeight > 0)
plotOffset.bottom = Math.max(maxOutset, axes.xaxis.labelHeight + margin);
if (axes.yaxis.labelWidth > 0)
plotOffset.left = Math.max(maxOutset, axes.yaxis.labelWidth + margin);
if (axes.x2axis.labelHeight > 0)
plotOffset.top = Math.max(maxOutset, axes.x2axis.labelHeight + margin);
if (axes.y2axis.labelWidth > 0)
plotOffset.right = Math.max(maxOutset, axes.y2axis.labelWidth + margin);
plotWidth = canvasWidth - plotOffset.left - plotOffset.right;
plotHeight = canvasHeight - plotOffset.bottom - plotOffset.top;
}
function draw() { function draw() {
if (options.grid.show) if (options.grid.show)
drawGrid(); drawGrid();
......
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