Ahh! Sorry. You're right, of course. From the source code:
00204 if (d_data->style == HLine)
00205 {
00206 if (d_data->align & (int) Qt::AlignLeft)
00207 dx = r.left() + xLabelDist - tr.x();
00208 else if (d_data->align & (int) Qt::AlignRight)
00209 dx = r.right() - xLabelDist + tr.x();
00210 else
00211 dx = r.left() + r.width() / 2;
00212 }
00204 if (d_data->style == HLine)
00205 {
00206 if (d_data->align & (int) Qt::AlignLeft)
00207 dx = r.left() + xLabelDist - tr.x();
00208 else if (d_data->align & (int) Qt::AlignRight)
00209 dx = r.right() - xLabelDist + tr.x();
00210 else
00211 dx = r.left() + r.width() / 2;
00212 }
To copy to clipboard, switch view to plain text mode
Which is basically what I had (except I assumed r.left() = 0).
The exercise hasn't been a waste, however. In this example, I align the text to a diagonal of a plot, where for each label, x * y = watts:
{
if ((rect.width() > 0) &&
(rect.height() > 0)
) {
// draw the label along a plot diagonal:
// 1. x*y = watts * dx/dv * dy/df
// 2. x/width = y/height
// =>
// 1. x^2 = width/height * watts
// 2. y^2 = height/width * watts
double xscale = fabs(xMap.transform(3) - xMap.transform(0)) / 3;
double yscale = fabs(yMap.transform(600) - yMap.transform(0)) / 600;
double w = watts * xscale * yscale;
int x = xMap.transform(sqrt(w * rect.width() / rect.height()) / xscale);
int y = yMap.transform(sqrt(w * rect.height() / rect.width()) / yscale);
// the following code based on source for QwtPlotMarker::draw()
QRect tr
(QPoint(0,
0), text.
textSize(painter
->font
()));
text.draw(painter, tr);
}
}
void draw(QPainter *painter,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRect &rect) const
{
if ((rect.width() > 0) &&
(rect.height() > 0)
) {
// draw the label along a plot diagonal:
// 1. x*y = watts * dx/dv * dy/df
// 2. x/width = y/height
// =>
// 1. x^2 = width/height * watts
// 2. y^2 = height/width * watts
double xscale = fabs(xMap.transform(3) - xMap.transform(0)) / 3;
double yscale = fabs(yMap.transform(600) - yMap.transform(0)) / 600;
double w = watts * xscale * yscale;
int x = xMap.transform(sqrt(w * rect.width() / rect.height()) / xscale);
int y = yMap.transform(sqrt(w * rect.height() / rect.width()) / yscale);
// the following code based on source for QwtPlotMarker::draw()
QRect tr(QPoint(0, 0), text.textSize(painter->font()));
tr.moveCenter(QPoint(x, y));
text.draw(painter, tr);
}
}
To copy to clipboard, switch view to plain text mode
I think this would be more challenging within the standard API.
Thanks!
Bookmarks