Results 1 to 12 of 12

Thread: QwtPlotZoomer question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QwtPlotZoomer question

    I know it is time consuming to go through the code, but this is what I had done earlier, but didn't work. You can check the code above, but better to check code below, as it has a modified zoomer that causes to go only one stack back on right mouse click, doesn't go back to 0 immediately.

    Please, point it out for me where should I put it in the code....

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qlayout.h>
    3. #include <qlabel.h>
    4. #include <qtime>
    5. #include <qpainter.h>
    6. #include "chartplot.h"
    7.  
    8. class PlotPicker : public QwtPlotPicker
    9. {
    10. public:
    11. PlotPicker(QwtPlot::Axis xAxis,
    12. QwtPlot::Axis yAxis,
    13. QwtPicker::RubberBand rb,
    14. QwtPicker::DisplayMode dm,
    15. QwtPlotCanvas* canvas) : QwtPlotPicker( xAxis, yAxis, rb , dm , canvas)
    16. {
    17. setStateMachine(new QwtPickerTrackerMachine());
    18. setRubberBandPen(QPen(Qt::DotLine));
    19. setRubberBand(QwtPicker::CrossRubberBand);
    20. }
    21.  
    22. private:
    23. QwtText trackerTextF(const QPointF &pos) const
    24. {
    25. QwtText text("X = " + QDateTime::fromTime_t(pos.x()).toString("yyyy.MM.dd. hh:mm:ss") + "\nY = " + QString::number(pos.y()));
    26. QColor bgColor(Qt::lightGray);
    27. bgColor.setAlpha(100);
    28. text.setBackgroundBrush(QBrush(bgColor));
    29. return text;
    30. }
    31.  
    32. };
    33.  
    34. QwtScaleDiv ChartPlot::scaleDiv()
    35. {
    36. QList<double> ticks[QwtScaleDiv::NTickTypes];
    37.  
    38. QList<double> &majorTicks = ticks[QwtScaleDiv::MajorTick];
    39. QList<double> &mediumTicks = ticks[QwtScaleDiv::MediumTick];
    40. QList<double> &minorTicks = ticks[QwtScaleDiv::MinorTick];
    41.  
    42. if (xData.count()<11)
    43. {
    44. foreach (double value, xData)
    45. majorTicks += value;
    46.  
    47. if (xData.count()>1)
    48. {
    49. for (int i = 0; i < xData.count() - 1; i++)
    50. for (int j = 1; j < 6; j++)
    51. mediumTicks += xData[i] + j * (xData[i+1] - xData[i]) / 5;
    52.  
    53. for (int i = 0; i < xData.count() - 1; i++)
    54. for (int j = 1; j < 11; j++)
    55. minorTicks += xData[i] + j * (xData[i+1] - xData[i]) / 10;
    56. }
    57. }
    58. else
    59. {
    60.  
    61. // Quantity-based division
    62.  
    63. // QVector<double> tempXData;
    64. // for (int i=0; i<11; i++)
    65. // {
    66. // int value = (i*(xData.count()-1)/10);
    67. // tempXData.append(xData[value]);
    68. // majorTicks += tempXData[i];
    69. // }
    70.  
    71. // Value-based division
    72. QVector<double> tempXData;
    73. for (int i=0; i<11; i++)
    74. {
    75. tempXData.append(xData[0] + i*(xData[xData.count()-1] - xData[0])/10);
    76. majorTicks += tempXData.last();
    77. }
    78. for (int i = 0; i < tempXData.count() - 1; i++)
    79. for (int j = 1; j < 6; j++)
    80. mediumTicks += tempXData[i] + j * (tempXData[i+1] - tempXData[i]) / 5;
    81. for (int i = 0; i < tempXData.count() - 1; i++)
    82. for (int j = 1; j < 11; j++)
    83. minorTicks += tempXData[i] + j * (tempXData[i+1] - tempXData[i]) / 10;
    84. }
    85.  
    86. return QwtScaleDiv(xData.first(), xData.last(), ticks);
    87. }
    88.  
    89. class Zoomer: public QwtPlotZoomer
    90. {
    91. public:
    92. Zoomer(int xAxis, int yAxis, QwtPlotCanvas *canvas) : QwtPlotZoomer(xAxis, yAxis, canvas)
    93. {
    94. setMousePattern(QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier);
    95. setMousePattern(QwtEventPattern::MouseSelect3, Qt::RightButton);
    96. setZoomBase();
    97. }
    98. };
    99.  
    100. class Grid: public QwtPlotGrid
    101. {
    102. public:
    103. Grid()
    104. {
    105. enableXMin(true);
    106. setMajPen(QPen(Qt::white, 0, Qt::DotLine));
    107. setMinPen(QPen(Qt::lightGray, 0 , Qt::DotLine));
    108. }
    109.  
    110. virtual void updateScaleDiv(const QwtScaleDiv &xMap, const QwtScaleDiv &yMap)
    111. {
    112. QList<double> ticks[QwtScaleDiv::NTickTypes];
    113.  
    114. ticks[QwtScaleDiv::MajorTick] = xMap.ticks(QwtScaleDiv::MajorTick);
    115. ticks[QwtScaleDiv::MinorTick] = xMap.ticks(QwtScaleDiv::MinorTick);
    116.  
    117. QwtPlotGrid::updateScaleDiv(QwtScaleDiv(xMap.lowerBound(), xMap.upperBound(), ticks), yMap);
    118. }
    119. };
    120.  
    121. class TimeScaleDraw: public QwtScaleDraw
    122. {
    123. public:
    124. TimeScaleDraw()
    125. {
    126. setTickLength(QwtScaleDiv::MajorTick, 8);
    127. setTickLength(QwtScaleDiv::MinorTick, 2);
    128. setTickLength(QwtScaleDiv::MediumTick, 4);
    129. setLabelRotation(0);
    130. setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    131. setSpacing(15);
    132. }
    133.  
    134. virtual QwtText label(double value) const
    135. {
    136. QDateTime time;
    137. time = time.fromTime_t(value);
    138. return time.toString("yyyy.MM.dd.\nhh:mm:ss");
    139. }
    140. };
    141.  
    142. class Background: public QwtPlotItem
    143. {
    144. public:
    145. Background()
    146. {
    147. setZ(0.0);
    148. }
    149.  
    150. virtual int rtti() const
    151. {
    152. return QwtPlotItem::Rtti_PlotUserItem;
    153. }
    154.  
    155. virtual void draw(QPainter *painter,
    156. const QwtScaleMap &,
    157. const QwtScaleMap &,
    158. const QRectF &rect) const
    159. {
    160. QColor color(Qt::gray);
    161. color = color.darker(150);
    162. painter->fillRect(rect, color);
    163. }
    164. };
    165.  
    166. class ChartCurve: public QwtPlotCurve
    167. {
    168. public:
    169. ChartCurve(const QString &title) : QwtPlotCurve(title)
    170. {
    171. setRenderHint(QwtPlotItem::RenderAntialiased);
    172. }
    173.  
    174. void setColor(const QColor &color)
    175. {
    176. QColor c = color;
    177. c.setAlpha(150);
    178.  
    179. setPen(c);
    180. setBrush(c);
    181. }
    182. };
    183.  
    184. ChartPlot::ChartPlot(QStandardItemModel *model, bool isSplineUsed, QWidget *parent) : QwtPlot(parent)
    185. {
    186. this->model = model;
    187. splineUsed = isSplineUsed;
    188.  
    189. setAutoReplot(false);
    190.  
    191. plotLayout()->setAlignCanvasToScales(true);
    192.  
    193. signs = new QwtLegend;
    194. signs->setItemMode(QwtLegend::CheckableItem);
    195. insertLegend(signs, QwtPlot::RightLegend);
    196.  
    197. setAxisTitle(QwtPlot::xBottom, "Report time");
    198. setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw());
    199.  
    200. scaleWidget = axisWidget(QwtPlot::xBottom);
    201. const int fmh = QFontMetrics(scaleWidget->font()).height();
    202. scaleWidget->setMinBorderDist(0, fmh / 2);
    203.  
    204. setAxisTitle(QwtPlot::yLeft, "Quantity");
    205.  
    206. grid = new Grid;
    207. grid->attach(this);
    208.  
    209. bg = new Background();
    210. bg->attach(this);
    211.  
    212. countPlot();
    213.  
    214. scaleEngineX = new QwtLinearScaleEngine;
    215. scaleEngineX->setAttribute(QwtScaleEngine::Floating, true);
    216. setAxisScaleEngine(QwtPlot::xBottom, scaleEngineX);
    217. setAxisScaleDiv(QwtPlot::xBottom, scaleDiv());
    218.  
    219. scaleEngineY = new QwtLinearScaleEngine;
    220. scaleEngineY->setAttribute(QwtScaleEngine::IncludeReference, true);
    221. scaleEngineY->setReference(0);
    222. setAxisScaleEngine(QwtPlot::yLeft, scaleEngineY);
    223.  
    224. picker = new PlotPicker(QwtPlot::xBottom,
    225. QwtPlot::yLeft,
    226. QwtPlotPicker::CrossRubberBand,
    227. QwtPicker::ActiveOnly,
    228. this->canvas());
    229. replot();
    230. zoomer = new Zoomer(QwtPlot::xBottom, QwtPlot::yLeft, this->canvas());
    231. zoomer->setZoomBase(false);
    232. connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)), SLOT(showCurve(QwtPlotItem *, bool)));
    233. }
    234.  
    235. void ChartPlot::showCurve(QwtPlotItem *item, const bool &on, const bool &replt)
    236. {
    237. item->setVisible(on);
    238. QWidget *w = legend()->find(item);
    239. if (w && w->inherits("QwtLegendItem")) ((QwtLegendItem *)w)->setChecked(on);
    240. if (replt) replot();
    241. }
    242.  
    243. void ChartPlot::useSpline(bool on, bool refresh)
    244. {
    245. if (on)
    246. {
    247. for (int i=0; i<curve.count(); i++)
    248. {
    249. curveFitter = new QwtSplineCurveFitter;
    250. curve.at(i)->setCurveFitter(curveFitter);
    251. }
    252. }
    253. else
    254. {
    255. for (int i=0; i<curve.count(); i++)
    256. {
    257. curveFitter = new QwtWeedingCurveFitter;
    258. curve.at(i)->setCurveFitter(curveFitter);
    259. }
    260. }
    261. if (refresh) replot();
    262. }
    263.  
    264. void ChartPlot::countPlot()
    265. {
    266. // Store current visibility of curves if they exist, otherwise initialize visibility to true
    267. QList<bool> visible;
    268. if (curve.isEmpty())
    269. {
    270. for (int column = 1; column < model->columnCount(); column++)
    271. visible.append(true);
    272. }
    273. else
    274. {
    275. for (int column = 1; column < model->columnCount(); column++)
    276. {
    277. visible.append(curve[column - 1]->isVisible());
    278. curve[column - 1]->detach();
    279. }
    280. curve.clear();
    281. }
    282. // Read X-axis data from table
    283. for (int row=0; row<model->rowCount(); row++)
    284. {
    285. QDateTime xDateTime = model->item(row, 0)->data(Qt::EditRole).toDateTime();
    286. xData << xDateTime.toTime_t();
    287. }
    288. // Read Y-axis data from table
    289. for (int column = 1; column < model->columnCount(); column++)
    290. {
    291. QVector<double> yData;
    292.  
    293. curve.append(new ChartCurve(model->horizontalHeaderItem(column)->text()));
    294. curve.last()->setStyle(QwtPlotCurve::Lines);
    295. QVariant decoration = model->item(0, column)->data(Qt::DecorationRole);
    296. if (decoration.type() == QVariant::Color)
    297. {
    298. curve.last()->setColor(decoration.value<QColor>());
    299. }
    300. curve.last()->setCurveAttribute(QwtPlotCurve::Fitted, true);
    301. curve.last()->setZ(curve.last()->z() - 1);
    302. curve.last()->attach(this);
    303.  
    304. for (int row=0; row<model->rowCount(); row++)
    305. {
    306. yData << model->item(row, column)->data(Qt::EditRole).toDouble();
    307. }
    308. curve.last()->setSamples(xData, yData);
    309. showCurve(curve.last(), visible[column - 1], false);
    310. }
    311. useSpline(splineUsed, false);
    312. }
    To copy to clipboard, switch view to plain text mode 

    I have tried many combinations of calling setZoomBase() and adding Zoomer to plot, no success.

    Maybe it would be easier to understand if you could just show me the line where I should put it. I even checked your code, and examples (randomplot uses the same code as me), no success.

    Could the solution be inside rescale() of Zoomer class? Please, help, my application is finished, running smoothly, except this one.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QwtPlotZoomer question

    Here is my understanding of zoomer.
    Default scales are 1000 for both the axes. So first you need to setZoomBase() after you do setSamples(). However, when you do zoom in, the autoscales are set to off. This is done implicitly. So we also need to setAutoScale(). The place to do that is in the slot for the signal zoomed(QRectF) of zoomer. Some code to explain it..
    Qt Code:
    1. connect(d_zoomer, SIGNAL(zoomed(QRectF), this, SLOT(autoRescale(QRectF)); //d_zoomer is zoomer's pointer
    To copy to clipboard, switch view to plain text mode 
    function implementation...
    Qt Code:
    1. void myPlot::autoRescale(QRectF)
    2. {
    3. if(d_zoomer->zoomRectIndex()==0) // autorescale only if u are at base... otherwise u defined scales by zoomer right..
    4. {
    5. setAxisAutoScale(yLeft);
    6. setAxisAutoScale(xBottom);
    7. d_zoomer->setZoomBase();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    HTH
    Last edited by pkj; 17th May 2011 at 17:55. Reason: QwtPlotZoomer

  3. The following user says thank you to pkj for this useful post:

    embeddedmz (30th July 2019)

  4. #3
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QwtPlotZoomer question

    Thanks, it could be the reason! I also thought that it could rather be about that zoomer drops the original scaling after zooming. I'm going to give it a try, and let you know about the result.

Similar Threads

  1. QwtPlotZoomer->setZoomBase
    By gronerth in forum Qwt
    Replies: 2
    Last Post: 24th January 2014, 16:01
  2. QwtPlotZoomer very slow
    By locke in forum Qwt
    Replies: 2
    Last Post: 7th July 2010, 16:56
  3. Sorry - more QwtPlotZoomer questions
    By cnbp173 in forum Qwt
    Replies: 4
    Last Post: 7th May 2010, 09:26
  4. QwtPlotZoomer, using two zoomers.
    By jma in forum Qwt
    Replies: 2
    Last Post: 30th September 2008, 08:29
  5. Problem with QwtPlotZoomer
    By seguprasad in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2007, 08:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.