#ifndef CHARTPLOT_H
#define CHARTPLOT_H
#include <qwt_plot.h>
#include <QStandardItemModel>
class ChartCurve;
{
Q_OBJECT
public:
const ChartCurve *getCurve() const
{
return curve;
}
{
this->model = model;
}
{
return this->model;
}
private Q_SLOTS:
private:
ChartCurve *curve;
};
#endif
#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
To copy to clipboard, switch view to plain text mode
#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>
{
public:
TimeScaleDraw
(const QTime &base
) : baseTime
(base
) {
}
virtual QwtText label
(double v
) const {
QTime upTime
= baseTime.
addSecs((int)v
);
return upTime.toString();
}
private:
};
{
public:
Background()
{
setZ(0.0);
}
virtual int rtti() const
{
}
{
c=c.darker(150);
r.setBottom(yMap.transform(500));
r.setTop(yMap.transform(0));
painter->fillRect(r, c);
}
};
{
public:
{
}
void setColor
(const QColor &color
) {
c.setAlpha(150);
setPen(c);
setBrush(c);
}
};
{
setAutoReplot(false);
plotLayout()->setAlignCanvasToScales(true);
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.
*/
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;
}
{
item->setVisible(on);
if (w
&& w
->inherits
("QwtLegendItem")) ((QwtLegendItem *)w
)->setChecked
(on
);
replot();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks