PDA

View Full Version : Autoscale axis with QwtPlotTradingCurve and set min x axis value



olivier1978
21st April 2014, 12:59
Hi all,

I am new to Qwt. I am able to draw a simple candlestick plot and add some surves for my own indicators. To do so, I have started from the example stockchart.
As in the example, I also have a zoom:


class Zoomer: public QwtPlotZoomer
{
public:
Zoomer( QWidget *canvas ): QwtPlotZoomer( canvas )
{
setRubberBandPen( QColor( Qt::darkGreen ) );
setTrackerMode( QwtPlotPicker::AlwaysOn );
}

protected:
virtual QwtText trackerTextF( const QPointF &pos ) const
{
const QDateTime dt = QwtDate::toDateTime( pos.x() );

QString s;
s += QwtDate::toString( QwtDate::toDateTime( pos.x() ), "MMM dd hh:mm ", QwtDate::FirstThursday );

QwtText text( s );
text.setColor( Qt::white );

QColor c = rubberBandPen().color();
text.setBorderPen( QPen( c ) );
text.setBorderRadius( 6 );
c.setAlpha( 170 );
text.setBackgroundBrush( c );

return text;
}
};

And in my Plot constructor:

PlotWidget::PlotWidget(QWidget *parent):
QwtPlot(parent)
{
// LeftButton for the zooming
// RightButton: zoom out by 1
// Ctrl+RighButton: zoom out to full size
Zoomer* zoomer = new Zoomer( canvas() );
zoomer->setMousePattern( QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier );
zoomer->setMousePattern( QwtEventPattern::MouseSelect3, Qt::RightButton );
zoomer->setKeyPattern( QwtEventPattern::KeyHome, Qt::Key_Home );
// MidButton for the panning
QwtPlotPanner *panner = new QwtPlotPanner( canvas() );
panner->setMouseButton( Qt::MidButton );

_scaleDraw = new DateScaleDraw( Qt::UTC );
_scaleEngine = new QwtDateScaleEngine( Qt::UTC );

setAxisScaleDraw( QwtPlot::xBottom, _scaleDraw );
setAxisScaleEngine( QwtPlot::xBottom, _scaleEngine );
setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
}

OK. Nothing new ... The problems are with the zoom.

* If I press the home key, I go back to 1 january 1970 (begin of UTC time?). How can I say that the first date in my plot is for instance 1st april 2014 and disallow the user to go before?
* When I select a zoom region, the values on the y axis are not scaled according to the content, and thus, the plot display "flat" curves. How can I fix this? The same also applies to Ctrl+Right clic.

Hope you could help

Best regards

olivier1978
21st April 2014, 20:01
Problem solved. Had to call

_zoomer->setZoomBase();
and everything works like a charm.