需要用到QwtPlotRescaler类,用法如下:
QwtPlotRescaler *plotRescaler = new QwtPlotRescaler(canvas, yLeft, QwtPlotRescaler::Fitting ); plotRescaler->setExpandingDirection(QwtPlotRescaler::ExpandBoth);
其构造函数可以设置参考坐标轴,及其缩放动作,默认其他轴与参考轴的比例为1.0。setExpandingDirection函数可以指定在固定好参考轴后,另一轴拉伸时的模式,QwtPlotRescaler::ExpandBoth参数是指向两边扩展。
在找到这个类前,我有一个手动写法,原理是将QwtPlotZoomer的zoomed(const QRectF &)信号与在它所在的QwtPlot类中编写的槽相连,并重写QwtPlot类中的resizeEvent事件:
void TrackPlot::zoomedSlot(const QRectF &rectF)
{
QPointF centerPoint = rectF.center(); double Ratio = rectF.width() / rectF.height();
double hvRatio = double(canvas()->width()) / double(canvas()->height()); if(Ratio > )
{
double scaleFactor = hvRatio / Ratio > ? hvRatio / Ratio : ; setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.width() / 2.0,
centerPoint.x() + scaleFactor * rectF.width() / 2.0);
double step = axisStepSize(xBottom);
setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.width() / (2.0 * hvRatio),
scaleFactor * centerPoint.y() + scaleFactor * rectF.width() / (2.0 * hvRatio), step);
}
else
{
double scaleFactor = Ratio / hvRatio > ? Ratio / hvRatio : ; setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.height() / 2.0,
centerPoint.y() + scaleFactor * rectF.height() / 2.0);
double step = axisStepSize(yLeft);
setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.height() * hvRatio / 2.0,
centerPoint.x() + scaleFactor * rectF.height() * hvRatio / 2.0, step);
} replot();
} void TrackPlot::resizeEvent(QResizeEvent *ev)
{
QwtPlot::resizeEvent(ev); if(ev->oldSize().width() > )
{
double hRatio = double(ev->size().width()) / double(ev->oldSize().width());
double vRatio = double(ev->size().height()) / double(ev->oldSize().height()); if(hRatio != )
printf(""); double xLowBound = axisScaleDiv(xBottom).lowerBound();
double xUpperBound = axisScaleDiv(xBottom).upperBound();
double yLowBound = axisScaleDiv(yLeft).lowerBound();
double yUpperBound = axisScaleDiv(yLeft).upperBound(); setAxisScale(xBottom, hRatio * xLowBound, hRatio * xUpperBound);
double ystep = axisStepSize(xBottom);
setAxisScale(yLeft, vRatio * yLowBound, vRatio * yUpperBound, ystep); replot();
}
else
{
double minX = axisScaleDiv(xBottom).lowerBound();
double maxX = axisScaleDiv(xBottom).upperBound();
double minY = axisScaleDiv(yLeft).lowerBound();
double maxY = axisScaleDiv(yLeft).upperBound(); QRectF rectF = QRectF(minX, minY, maxX - minX, maxY - minY); QPointF centerPoint = rectF.center(); double Ratio = rectF.width() / rectF.height();
double hvRatio = double(canvas()->width()) / double(canvas()->height()); if(Ratio > )
{
double scaleFactor = hvRatio / Ratio > ? hvRatio / Ratio : ; setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.width() / 2.0,
centerPoint.x() + scaleFactor * rectF.width() / 2.0);
double step = axisStepSize(xBottom);
setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.width() / (2.0 * hvRatio),
scaleFactor * centerPoint.y() + scaleFactor * rectF.width() / (2.0 * hvRatio), step);
}
else
{
double scaleFactor = Ratio / hvRatio > ? Ratio / hvRatio : ; setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.height() / 2.0,
centerPoint.y() + scaleFactor * rectF.height() / 2.0);
double step = axisStepSize(yLeft);
setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.height() * hvRatio / 2.0,
centerPoint.x() + scaleFactor * rectF.height() * hvRatio / 2.0, step);
} replot();
}
}