Hi there,
since there wasn't a single reply since last summer, this seems to be a rather unpopular feature :-)
Anyway, diggin into the code I found no easy way to implement this - the current API doesn't offer a convenient choice. Anyhow, here's what I've come up with so far:
The current QwtPlotRescaler operates on the chart canvas size:
//! Adjust the plot axes scales
void QwtPlotRescaler::rescale() const
{
// here we get the size of the plot canvas and use this to adjust the axes
const QSize size
= canvas
()->contentsRect
().
size();
rescale( size, size );
}
//! Adjust the plot axes scales
void QwtPlotRescaler::rescale() const
{
// here we get the size of the plot canvas and use this to adjust the axes
const QSize size = canvas()->contentsRect().size();
rescale( size, size );
}
To copy to clipboard, switch view to plain text mode
Now, within the plot renderer, we cannot use the canvas size, so the first change to implement was to make the
// should be public instead of protected
void QwtPlotRescaler::rescale(
const QSize &oldSize,
const QSize &newSize
) const
// should be public instead of protected
void QwtPlotRescaler::rescale(
const QSize &oldSize, const QSize &newSize ) const
To copy to clipboard, switch view to plain text mode
public. So now it can be called from within the plot renderer.
Also, the plot renderer needs to know about the rescaler, so an additional (optional) argument is added.
// render() function of QwtPlotRenderer get's an optional 4th argument
QPainter *,
const QRectF &rect , QwtPlotRescaler
*plotRescaler
= NULL) const;
// render() function of QwtPlotRenderer get's an optional 4th argument
virtual void render(QwtPlot *,
QPainter *, const QRectF &rect , QwtPlotRescaler *plotRescaler = NULL) const;
To copy to clipboard, switch view to plain text mode
Lastly, we call the plotRescaler after having adjusted the layout:
void QwtPlotRenderer
::render( QwtPlot *plot,
QPainter *painter,
const QRectF &plotRect, QwtPlotRescaler
* plotRescaler
) const {
...
// adjust plot axes based on layoutRect dimensions
if (plotRescaler) {
QSize s
= layoutRect.
size().
toSize();
plotRescaler->rescale(s,s);
}
...
void QwtPlotRenderer::render( QwtPlot *plot,
QPainter *painter, const QRectF &plotRect, QwtPlotRescaler * plotRescaler ) const
{
...
// adjust plot axes based on layoutRect dimensions
if (plotRescaler) {
QSize s = layoutRect.size().toSize();
plotRescaler->rescale(s,s);
}
...
To copy to clipboard, switch view to plain text mode
this does the trick (almost). Unfortunately, the desired aspect ratio is only kept approximately. So, probably layoutRect() and contentsRect() are not the same here or the scaling within the plot rescaler still does something different. I'll try to track that down...
@Uwe: do you see a way to get this proportionality feature formally into the QwtPlotRenderer... my variant is certainly a hack and probably not what you would want to have in the official sources, right? :-)
Bye,
Andreas
Added after 10 minutes:
Update: seems things are not so easy after all - QwtPlotRescaler::updateScales() draws upon the scale div objects in the plot - thus using dimensions of the plot object (that will naturally differ) from the target render rectangle and thus there's a lot of garbage when the original plot dimensions do not match those of the target plot... So I guess implementing the whole scale adjustment stuff anew in the plot render will be necessary :-(
Does anyone have a better idea?
-Andreas
Bookmarks