chartview.cpp
86 lines
| 1.9 KiB
| text/x-c
|
CppLexer
|
r246 | #include "chartview.h" | ||
|
r349 | #include <qlineseries.h> | ||
|
r476 | #include <qscatterseries.h> | ||
#include <qsplineseries.h> | ||||
|
r560 | #include <qareaseries.h> | ||
|
r246 | #include <QTime> | ||
ChartView::ChartView(QWidget* parent):QChartView(parent), | ||||
m_index(0) | ||||
{ | ||||
|
r482 | QTime now = QTime::currentTime(); | ||
qsrand((uint)now.msec()); | ||||
|
r476 | setChartTitle("Three random line charts"); | ||
|
r246 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); | ||
m_timer.setInterval(3000); | ||||
|
r482 | //![1] | ||
|
r444 | QLineSeries* series0 = new QLineSeries(this); | ||
|
r246 | QPen blue(Qt::blue); | ||
blue.setWidth(3); | ||||
series0->setPen(blue); | ||||
|
r482 | |||
|
r476 | QScatterSeries* series1 = new QScatterSeries(this); | ||
|
r246 | QPen red(Qt::red); | ||
red.setWidth(3); | ||||
series1->setPen(red); | ||||
|
r476 | series1->setBrush(Qt::white); | ||
|
r482 | |||
|
r476 | QSplineSeries* series2 = new QSplineSeries(this); | ||
|
r246 | QPen green(Qt::green); | ||
green.setWidth(3); | ||||
series2->setPen(green); | ||||
|
r560 | |||
QAreaSeries* series3 = new QAreaSeries(series0); | ||||
QPen yellow(Qt::black); | ||||
yellow.setWidth(3); | ||||
series3->setPen(yellow); | ||||
series3->setBrush(Qt::yellow); | ||||
|
r482 | //![1] | ||
|
r246 | |||
|
r482 | //![2] | ||
|
r246 | int numPoints = 10; | ||
for (int x = 0; x <= numPoints; ++x) { | ||||
|
r476 | qreal y = qrand() % 100; | ||
series0->add(x,y); | ||||
series1->add(x,y); | ||||
series2->add(x,y); | ||||
|
r246 | } | ||
|
r482 | //![2] | ||
|
r246 | |||
|
r482 | //![3] | ||
|
r246 | m_series<<series0; | ||
|
r476 | m_titles<<chartTitle()+": LineChart"; | ||
|
r246 | m_series<<series1; | ||
|
r476 | m_titles<<chartTitle()+": ScatterChart"; | ||
|
r246 | m_series<<series2; | ||
|
r476 | m_titles<<chartTitle()+": SplineChart"; | ||
|
r560 | m_series<<series3; | ||
m_titles<<chartTitle()+": AreaChart"; | ||||
|
r482 | //![3] | ||
|
r476 | addSeries(series0); | ||
setChartTitle(m_titles.at(0)); | ||||
|
r246 | |||
m_timer.start(); | ||||
} | ||||
ChartView::~ChartView() | ||||
{ | ||||
|
r257 | if(m_series.size()==0) return; | ||
removeSeries(m_series.at(m_index)); | ||||
qDeleteAll(m_series); | ||||
|
r246 | } | ||
|
r482 | //![4] | ||
|
r246 | void ChartView::handleTimeout() | ||
{ | ||||
if(m_series.size()==0) return; | ||||
removeSeries(m_series.at(m_index)); | ||||
m_index++; | ||||
m_index=m_index%m_series.size(); | ||||
addSeries(m_series.at(m_index)); | ||||
|
r476 | setChartTitle(m_titles.at(m_index)); | ||
|
r246 | } | ||
|
r482 | //![4] | ||