#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_plot_grid.h>
#include <qwt_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include <qwt_curve_fitter.h>
#include "chartplot.h"
#include <QDebug>
{
public:
Grid()
{
enableXMin(true);
setMajPen
(QPen(Qt
::white,
0, Qt
::DotLine));
setMinPen
(QPen(Qt
::lightGray,
0 , Qt
::DotLine));
}
{
QwtScaleDiv(xMap.
lowerBound(), xMap.
upperBound(), ticks
),
yMap );
}
};
{
public:
TimeScaleDraw(/*const QTime &base*/)/* : baseTime(base)*/
{
setLabelRotation(0);
setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
setSpacing(20);
}
virtual QwtText label
(double v
) const {
// QTime upTime = baseTime.addSecs((int)v);
time = time.fromTime_t(v);
return time.toString("yyyy.MM.dd.\nhh:mm:ss");
}
//private:
// QTime baseTime;
};
{
public:
Background()
{
setZ(0.0);
}
virtual int rtti() const
{
}
{
c=c.darker(150);
// QRectF r = rect;
// r.setBottom(yMap.transform(500));
// r.setTop(yMap.transform(0));
painter->fillRect(rect, 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");
setAxisAutoScale
(QwtPlot::xBottom);
setAxisLabelRotation
(QwtPlot::xBottom,
-45.0);
setAxisLabelAlignment
(QwtPlot::xBottom, Qt
::AlignLeft | Qt
::AlignBottom);
setAxisScaleDraw
(QwtPlot::xBottom,
new TimeScaleDraw
());
scaleWidget->setMinBorderDist(0, fmh / 2);
setAxisTitle
(QwtPlot::yLeft,
"Quantity");
//setAxisScale(QwtPlot::yLeft, 0, 50);
grid->attach(this);
Background *bg = new Background();
bg->attach(this);
for (int column = 1; column < model->columnCount(); column++)
{
curve = new ChartCurve(model->horizontalHeaderItem(column)->text());
QVariant decoration
= model
->item
(0, column
)->data
(Qt
::DecorationRole);
if (decoration.
type() == QVariant::Color) {
curve->setColor(decoration.value<QColor>());
}
curve->setCurveFitter(curveFitter);
curve->setZ(curve->z() - 1);
curve->attach(this);
QVector<double> xData;
QVector<double> yData;
for (int row=0; row<model->rowCount(); row++)
{
QDateTime xDateTime
= model
->item
(row,
0)->data
(Qt
::EditRole).
toDateTime();
xData << xDateTime.toTime_t();
yData << model->item(row, column)->data(Qt::EditRole).toDouble();
}
curve->setSamples(xData, yData);
showCurve(curve, true);
}
}
{
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_plot_grid.h>
#include <qwt_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include <qwt_curve_fitter.h>
#include "chartplot.h"
#include <QDebug>
class Grid: public QwtPlotGrid
{
public:
Grid()
{
enableXMin(true);
setMajPen(QPen(Qt::white, 0, Qt::DotLine));
setMinPen(QPen(Qt::lightGray, 0 , Qt::DotLine));
}
virtual void updateScaleDiv(const QwtScaleDiv &xMap,
const QwtScaleDiv &yMap)
{
QList<double> ticks[QwtScaleDiv::NTickTypes];
ticks[QwtScaleDiv::MajorTick] = xMap.ticks(QwtScaleDiv::MajorTick);
ticks[QwtScaleDiv::MinorTick] = xMap.ticks(QwtScaleDiv::MinorTick);
QwtPlotGrid::updateScaleDiv(
QwtScaleDiv(xMap.lowerBound(), xMap.upperBound(), ticks),
yMap );
}
};
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(/*const QTime &base*/)/* : baseTime(base)*/
{
setTickLength(QwtScaleDiv::MajorTick, 6);
setTickLength(QwtScaleDiv::MinorTick, 0);
setTickLength(QwtScaleDiv::MediumTick, 0);
setLabelRotation(0);
setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
setSpacing(20);
}
virtual QwtText label(double v) const
{
// QTime upTime = baseTime.addSecs((int)v);
QDateTime time;
time = time.fromTime_t(v);
return time.toString("yyyy.MM.dd.\nhh:mm:ss");
}
//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(rect, 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");
setAxisAutoScale(QwtPlot::xBottom);
setAxisLabelRotation(QwtPlot::xBottom, -45.0);
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw());
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, 50);
QwtPlotGrid *grid = new Grid;
grid->attach(this);
Background *bg = new Background();
bg->attach(this);
for (int column = 1; column < model->columnCount(); column++)
{
curve = new ChartCurve(model->horizontalHeaderItem(column)->text());
curve->setStyle(QwtPlotCurve::Lines);
QVariant decoration = model->item(0, column)->data(Qt::DecorationRole);
if (decoration.type() == QVariant::Color)
{
curve->setColor(decoration.value<QColor>());
}
curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
QwtSplineCurveFitter *curveFitter = new QwtSplineCurveFitter();
curve->setCurveFitter(curveFitter);
curve->setZ(curve->z() - 1);
curve->attach(this);
QVector<double> xData;
QVector<double> yData;
for (int row=0; row<model->rowCount(); row++)
{
QDateTime xDateTime = model->item(row, 0)->data(Qt::EditRole).toDateTime();
xData << xDateTime.toTime_t();
yData << model->item(row, column)->data(Qt::EditRole).toDouble();
}
curve->setSamples(xData, yData);
showCurve(curve, true);
}
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