Hi,

It looks like all the included examples are to difficult to me (moving knobs, time-based changes in charts, etc.).
I simply cannot figure out the basic use of the library.
Can someone please include here a very-very simple example of a class that can be put in a layout as widget and shown that has only X-Y axes as described in the topic. Can someone include a simple example of Y=2X (where X is QDateTime that is converted to double for example).

My source data is in QStandardItemModel. First column is QDateTime (real dates and times of logged events), all the other columns are measured values of different objects - so basically different lines in the chart.


Thank you in advance!

I include the source where I got, but it is just unreliable.

HEADER:
Qt Code:
  1. #ifndef CHARTPLOT_H
  2. #define CHARTPLOT_H
  3.  
  4. #include <qwt_plot.h>
  5. #include <QStandardItemModel>
  6.  
  7. class ChartCurve;
  8.  
  9. class ChartPlot : public QwtPlot
  10. {
  11. Q_OBJECT
  12. public:
  13. ChartPlot(QStandardItemModel *model, QWidget * = 0);
  14. const ChartCurve *getCurve() const
  15. {
  16. return curve;
  17. }
  18.  
  19. void setModel(QStandardItemModel *model)
  20. {
  21. this->model = model;
  22. }
  23.  
  24. QStandardItemModel *getModel()
  25. {
  26. return this->model;
  27. }
  28.  
  29. private Q_SLOTS:
  30. void showCurve(QwtPlotItem *, bool on);
  31.  
  32. private:
  33. ChartCurve *curve;
  34. };
  35.  
  36. #endif
To copy to clipboard, switch view to plain text mode 

SOURCE:
Qt Code:
  1. #include <qapplication.h>
  2. #include <qlayout.h>
  3. #include <qlabel.h>
  4. #include <qtime>
  5. #include <qpainter.h>
  6. #include <qwt_plot_layout.h>
  7. #include <qwt_plot_curve.h>
  8. #include <qwt_scale_draw.h>
  9. #include <qwt_scale_widget.h>
  10. #include <qwt_legend.h>
  11. #include <qwt_legend_item.h>
  12. #include "chartplot.h"
  13. #include <QDebug>
  14.  
  15. class TimeScaleDraw: public QwtScaleDraw
  16. {
  17. public:
  18. TimeScaleDraw(const QTime &base) : baseTime(base)
  19. {
  20. }
  21.  
  22. virtual QwtText label(double v) const
  23. {
  24. QTime upTime = baseTime.addSecs((int)v);
  25. return upTime.toString();
  26. }
  27. private:
  28. QTime baseTime;
  29. };
  30.  
  31. class Background: public QwtPlotItem
  32. {
  33. public:
  34. Background()
  35. {
  36. setZ(0.0);
  37. }
  38.  
  39. virtual int rtti() const
  40. {
  41. return QwtPlotItem::Rtti_PlotUserItem;
  42. }
  43.  
  44. virtual void draw(QPainter *painter,
  45. const QwtScaleMap &, const QwtScaleMap &yMap,
  46. const QRectF &rect) const
  47. {
  48. QColor c(Qt::gray);
  49. c=c.darker(150);
  50. QRectF r = rect;
  51.  
  52. r.setBottom(yMap.transform(500));
  53. r.setTop(yMap.transform(0));
  54. painter->fillRect(r, c);
  55. }
  56. };
  57.  
  58. class ChartCurve: public QwtPlotCurve
  59. {
  60. public:
  61. ChartCurve(const QString &title) : QwtPlotCurve(title)
  62. {
  63. setRenderHint(QwtPlotItem::RenderAntialiased);
  64. }
  65.  
  66. void setColor(const QColor &color)
  67. {
  68. QColor c = color;
  69. c.setAlpha(150);
  70.  
  71. setPen(c);
  72. setBrush(c);
  73. }
  74. };
  75.  
  76. ChartPlot::ChartPlot(QStandardItemModel *model, QWidget *parent) : QwtPlot(parent)
  77. {
  78. setAutoReplot(false);
  79.  
  80. plotLayout()->setAlignCanvasToScales(true);
  81.  
  82. QwtLegend *legend = new QwtLegend;
  83. legend->setItemMode(QwtLegend::CheckableItem);
  84. insertLegend(legend, QwtPlot::RightLegend);
  85.  
  86. setAxisTitle(QwtPlot::xBottom, "Report time");
  87. //setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(cpuStat.upTime()));
  88. setAxisScale(QwtPlot::xBottom, 0, 10);
  89. setAxisLabelRotation(QwtPlot::xBottom, -45.0);
  90. setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
  91.  
  92. /*
  93.   In situations, when there is a label at the most right position of the
  94.   scale, additional space is needed to display the overlapping part
  95.   of the label would be taken by reducing the width of scale and canvas.
  96.   To avoid this "jumping canvas" effect, we add a permanent margin.
  97.   We don't need to do the same for the left border, because there
  98.   is enough space for the overlapping label below the left scale.
  99.   */
  100.  
  101. QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
  102. const int fmh = QFontMetrics(scaleWidget->font()).height();
  103. scaleWidget->setMinBorderDist(0, fmh / 2);
  104.  
  105. setAxisTitle(QwtPlot::yLeft, "Quantity");
  106. setAxisScale(QwtPlot::yLeft, 0, 100);
  107.  
  108. Background *bg = new Background();
  109. bg->attach(this);
  110.  
  111. curve = new ChartCurve("");
  112. curve->setColor(Qt::blue);
  113. curve->setZ(curve->z() - 1);
  114. curve->attach(this);
  115.  
  116. double xData[model->rowCount()];
  117. double yData[model->rowCount()];
  118. qDebug() <<"ROWCOUNT:"<<model->rowCount();
  119. for (int x=0; x<model->rowCount(); x++)
  120. {
  121. //xData[x] = model->item(x, 0)->data(Qt::EditRole).toDateTime();
  122. xData[x] = x+1;
  123. qDebug()<<"X:"<<xData[x];
  124. }
  125. for (int y=0; y<model->rowCount(); y++)
  126. {
  127. yData[y] = model->item(y, 1)->data(Qt::EditRole).toFloat();
  128. qDebug()<<"Y:"<<yData[y];
  129. }
  130.  
  131. curve->setRawSamples(xData, yData, model->rowCount());
  132. showCurve(curve, true);
  133.  
  134. //for (int i = 0; i < 100; i++) xData[100 - 1 - i] = i;
  135.  
  136. connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)), SLOT(showCurve(QwtPlotItem *, bool)));
  137. }
  138.  
  139. void ChartPlot::showCurve(QwtPlotItem *item, bool on)
  140. {
  141. item->setVisible(on);
  142. QWidget *w = legend()->find(item);
  143. if (w && w->inherits("QwtLegendItem")) ((QwtLegendItem *)w)->setChecked(on);
  144.  
  145. replot();
  146. }
To copy to clipboard, switch view to plain text mode 

I have tried to modify CPU plot to simple x/y function. No success.

Thanks!