diff --git a/src/plot.cpp b/src/plot.cpp --- a/src/plot.cpp +++ b/src/plot.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2015 Hasan Yavuz Özderya + Copyright © 2017 Hasan Yavuz Özderya This file is part of serialplot. @@ -38,6 +38,7 @@ Plot::Plot(QWidget* parent) : { isAutoScaled = true; symbolSize = 0; + numOfSamples = 1; QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed); @@ -78,7 +79,7 @@ Plot::~Plot() if (snapshotOverlay != NULL) delete snapshotOverlay; } -void Plot::setAxis(bool autoScaled, double yAxisMin, double yAxisMax) +void Plot::setYAxis(bool autoScaled, double yAxisMin, double yAxisMax) { this->isAutoScaled = autoScaled; @@ -92,8 +93,29 @@ void Plot::setAxis(bool autoScaled, doub resetAxes(); } +void Plot::setXAxis(double xMin, double xMax) +{ + _xMin = xMin; + _xMax = xMax; + + zoomer.zoom(0); // unzoom + + // set axis + setAxisScale(QwtPlot::xBottom, xMin, xMax); + replot(); // Note: if we don't replot here scale at startup isn't set correctly + + // reset zoom base + auto base = zoomer.zoomBase(); + base.setLeft(xMin); + base.setRight(xMax); + zoomer.setZoomBase(base); + + onXScaleChanged(); +} + void Plot::resetAxes() { + // reset y axis if (isAutoScaled) { setAxisAutoScale(QwtPlot::yLeft); @@ -103,12 +125,13 @@ void Plot::resetAxes() setAxisScale(QwtPlot::yLeft, yMin, yMax); } + zoomer.setZoomBase(); + replot(); } void Plot::unzoomed() { - setAxisAutoScale(QwtPlot::xBottom); resetAxes(); onXScaleChanged(); } @@ -229,7 +252,10 @@ void Plot::onXScaleChanged() auto sw = axisWidget(QwtPlot::xBottom); auto paintDist = sw->scaleDraw()->scaleMap().pDist(); auto scaleDist = sw->scaleDraw()->scaleMap().sDist(); - int symDisPx = round(paintDist / scaleDist); + auto fullScaleDist = zoomer.zoomBase().width(); + auto zoomRate = fullScaleDist / scaleDist; + float samplesInView = numOfSamples / zoomRate; + int symDisPx = round(paintDist / samplesInView); if (symDisPx < SYMBOL_SHOW_AT_WIDTH) { @@ -273,8 +299,6 @@ void Plot::resizeEvent(QResizeEvent * ev void Plot::onNumOfSamplesChanged(unsigned value) { - auto currentBase = zoomer.zoomBase(); - currentBase.setWidth(value); - zoomer.setZoomBase(currentBase); + numOfSamples = value; onXScaleChanged(); }