falconium
28th April 2011, 21:25
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:
#ifndef CHARTPLOT_H
#define CHARTPLOT_H
#include <qwt_plot.h>
#include <QStandardItemModel>
class ChartCurve;
class ChartPlot : public QwtPlot
{
Q_OBJECT
public:
ChartPlot(QStandardItemModel *model, QWidget * = 0);
const ChartCurve *getCurve() const
{
return curve;
}
void setModel(QStandardItemModel *model)
{
this->model = model;
}
QStandardItemModel *getModel()
{
return this->model;
}
private Q_SLOTS:
void showCurve(QwtPlotItem *, bool on);
private:
QStandardItemModel *model;
ChartCurve *curve;
};
#endif
SOURCE:
#include <qapplication.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qtime>
#include <qpainter.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include "chartplot.h"
#include <QDebug>
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(const QTime &base) : baseTime(base)
{
}
virtual QwtText label(double v) const
{
QTime upTime = baseTime.addSecs((int)v);
return upTime.toString();
}
private:
QTime baseTime;
};
class Background: public QwtPlotItem
{
public:
Background()
{
setZ(0.0);
}
virtual int rtti() const
{
return QwtPlotItem::Rtti_PlotUserItem;
}
virtual void draw(QPainter *painter,
const QwtScaleMap &, const QwtScaleMap &yMap,
const QRectF &rect) const
{
QColor c(Qt::gray);
c=c.darker(150);
QRectF r = rect;
r.setBottom(yMap.transform(500));
r.setTop(yMap.transform(0));
painter->fillRect(r, c);
}
};
class ChartCurve: public QwtPlotCurve
{
public:
ChartCurve(const QString &title) : QwtPlotCurve(title)
{
setRenderHint(QwtPlotItem::RenderAntialiased);
}
void setColor(const QColor &color)
{
QColor c = color;
c.setAlpha(150);
setPen(c);
setBrush(c);
}
};
ChartPlot::ChartPlot(QStandardItemModel *model, QWidget *parent) : QwtPlot(parent)
{
setAutoReplot(false);
plotLayout()->setAlignCanvasToScales(true);
QwtLegend *legend = new QwtLegend;
legend->setItemMode(QwtLegend::CheckableItem);
insertLegend(legend, QwtPlot::RightLegend);
setAxisTitle(QwtPlot::xBottom, "Report time");
//setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(cpuStat.upTime()));
setAxisScale(QwtPlot::xBottom, 0, 10);
setAxisLabelRotation(QwtPlot::xBottom, -45.0);
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
/*
In situations, when there is a label at the most right position of the
scale, additional space is needed to display the overlapping part
of the label would be taken by reducing the width of scale and canvas.
To avoid this "jumping canvas" effect, we add a permanent margin.
We don't need to do the same for the left border, because there
is enough space for the overlapping label below the left scale.
*/
QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
const int fmh = QFontMetrics(scaleWidget->font()).height();
scaleWidget->setMinBorderDist(0, fmh / 2);
setAxisTitle(QwtPlot::yLeft, "Quantity");
setAxisScale(QwtPlot::yLeft, 0, 100);
Background *bg = new Background();
bg->attach(this);
curve = new ChartCurve("");
curve->setColor(Qt::blue);
curve->setZ(curve->z() - 1);
curve->attach(this);
double xData[model->rowCount()];
double yData[model->rowCount()];
qDebug() <<"ROWCOUNT:"<<model->rowCount();
for (int x=0; x<model->rowCount(); x++)
{
//xData[x] = model->item(x, 0)->data(Qt::EditRole).toDateTime();
xData[x] = x+1;
qDebug()<<"X:"<<xData[x];
}
for (int y=0; y<model->rowCount(); y++)
{
yData[y] = model->item(y, 1)->data(Qt::EditRole).toFloat();
qDebug()<<"Y:"<<yData[y];
}
curve->setRawSamples(xData, yData, model->rowCount());
showCurve(curve, true);
//for (int i = 0; i < 100; i++) xData[100 - 1 - i] = i;
connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)), SLOT(showCurve(QwtPlotItem *, bool)));
}
void ChartPlot::showCurve(QwtPlotItem *item, bool on)
{
item->setVisible(on);
QWidget *w = legend()->find(item);
if (w && w->inherits("QwtLegendItem")) ((QwtLegendItem *)w)->setChecked(on);
replot();
}
I have tried to modify CPU plot to simple x/y function. No success.
Thanks!
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:
#ifndef CHARTPLOT_H
#define CHARTPLOT_H
#include <qwt_plot.h>
#include <QStandardItemModel>
class ChartCurve;
class ChartPlot : public QwtPlot
{
Q_OBJECT
public:
ChartPlot(QStandardItemModel *model, QWidget * = 0);
const ChartCurve *getCurve() const
{
return curve;
}
void setModel(QStandardItemModel *model)
{
this->model = model;
}
QStandardItemModel *getModel()
{
return this->model;
}
private Q_SLOTS:
void showCurve(QwtPlotItem *, bool on);
private:
QStandardItemModel *model;
ChartCurve *curve;
};
#endif
SOURCE:
#include <qapplication.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qtime>
#include <qpainter.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include "chartplot.h"
#include <QDebug>
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(const QTime &base) : baseTime(base)
{
}
virtual QwtText label(double v) const
{
QTime upTime = baseTime.addSecs((int)v);
return upTime.toString();
}
private:
QTime baseTime;
};
class Background: public QwtPlotItem
{
public:
Background()
{
setZ(0.0);
}
virtual int rtti() const
{
return QwtPlotItem::Rtti_PlotUserItem;
}
virtual void draw(QPainter *painter,
const QwtScaleMap &, const QwtScaleMap &yMap,
const QRectF &rect) const
{
QColor c(Qt::gray);
c=c.darker(150);
QRectF r = rect;
r.setBottom(yMap.transform(500));
r.setTop(yMap.transform(0));
painter->fillRect(r, c);
}
};
class ChartCurve: public QwtPlotCurve
{
public:
ChartCurve(const QString &title) : QwtPlotCurve(title)
{
setRenderHint(QwtPlotItem::RenderAntialiased);
}
void setColor(const QColor &color)
{
QColor c = color;
c.setAlpha(150);
setPen(c);
setBrush(c);
}
};
ChartPlot::ChartPlot(QStandardItemModel *model, QWidget *parent) : QwtPlot(parent)
{
setAutoReplot(false);
plotLayout()->setAlignCanvasToScales(true);
QwtLegend *legend = new QwtLegend;
legend->setItemMode(QwtLegend::CheckableItem);
insertLegend(legend, QwtPlot::RightLegend);
setAxisTitle(QwtPlot::xBottom, "Report time");
//setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw(cpuStat.upTime()));
setAxisScale(QwtPlot::xBottom, 0, 10);
setAxisLabelRotation(QwtPlot::xBottom, -45.0);
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
/*
In situations, when there is a label at the most right position of the
scale, additional space is needed to display the overlapping part
of the label would be taken by reducing the width of scale and canvas.
To avoid this "jumping canvas" effect, we add a permanent margin.
We don't need to do the same for the left border, because there
is enough space for the overlapping label below the left scale.
*/
QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
const int fmh = QFontMetrics(scaleWidget->font()).height();
scaleWidget->setMinBorderDist(0, fmh / 2);
setAxisTitle(QwtPlot::yLeft, "Quantity");
setAxisScale(QwtPlot::yLeft, 0, 100);
Background *bg = new Background();
bg->attach(this);
curve = new ChartCurve("");
curve->setColor(Qt::blue);
curve->setZ(curve->z() - 1);
curve->attach(this);
double xData[model->rowCount()];
double yData[model->rowCount()];
qDebug() <<"ROWCOUNT:"<<model->rowCount();
for (int x=0; x<model->rowCount(); x++)
{
//xData[x] = model->item(x, 0)->data(Qt::EditRole).toDateTime();
xData[x] = x+1;
qDebug()<<"X:"<<xData[x];
}
for (int y=0; y<model->rowCount(); y++)
{
yData[y] = model->item(y, 1)->data(Qt::EditRole).toFloat();
qDebug()<<"Y:"<<yData[y];
}
curve->setRawSamples(xData, yData, model->rowCount());
showCurve(curve, true);
//for (int i = 0; i < 100; i++) xData[100 - 1 - i] = i;
connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)), SLOT(showCurve(QwtPlotItem *, bool)));
}
void ChartPlot::showCurve(QwtPlotItem *item, bool on)
{
item->setVisible(on);
QWidget *w = legend()->find(item);
if (w && w->inherits("QwtLegendItem")) ((QwtLegendItem *)w)->setChecked(on);
replot();
}
I have tried to modify CPU plot to simple x/y function. No success.
Thanks!