chartview.cpp
117 lines
| 3.1 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r830 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r246 | #include "chartview.h" | ||
Michal Klocek
|
r632 | #include <QLineSeries> | ||
#include <QScatterSeries> | ||||
#include <QSplineSeries> | ||||
#include <QAreaSeries> | ||||
Michal Klocek
|
r246 | #include <QTime> | ||
Michal Klocek
|
r866 | ChartView::ChartView(QChart* chart,QWidget* parent):QChartView(chart,parent), | ||
m_index(0),m_chart(chart) | ||||
Michal Klocek
|
r246 | { | ||
Michal Klocek
|
r866 | m_chart->setTitle("Charts presenter"); | ||
Michal Klocek
|
r246 | QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); | ||
m_timer.setInterval(3000); | ||||
Michal Klocek
|
r482 | //![1] | ||
Michal Klocek
|
r444 | QLineSeries* series0 = new QLineSeries(this); | ||
Michal Klocek
|
r246 | QPen blue(Qt::blue); | ||
blue.setWidth(3); | ||||
series0->setPen(blue); | ||||
Michal Klocek
|
r482 | |||
Michal Klocek
|
r476 | QScatterSeries* series1 = new QScatterSeries(this); | ||
Michal Klocek
|
r246 | QPen red(Qt::red); | ||
red.setWidth(3); | ||||
series1->setPen(red); | ||||
Michal Klocek
|
r476 | series1->setBrush(Qt::white); | ||
Michal Klocek
|
r482 | |||
Michal Klocek
|
r476 | QSplineSeries* series2 = new QSplineSeries(this); | ||
Michal Klocek
|
r246 | QPen green(Qt::green); | ||
green.setWidth(3); | ||||
series2->setPen(green); | ||||
Michal Klocek
|
r560 | |||
QAreaSeries* series3 = new QAreaSeries(series0); | ||||
QPen yellow(Qt::black); | ||||
yellow.setWidth(3); | ||||
series3->setPen(yellow); | ||||
series3->setBrush(Qt::yellow); | ||||
Michal Klocek
|
r482 | //![1] | ||
Michal Klocek
|
r246 | |||
Michal Klocek
|
r482 | //![2] | ||
Michal Klocek
|
r246 | int numPoints = 10; | ||
for (int x = 0; x <= numPoints; ++x) { | ||||
Michal Klocek
|
r476 | qreal y = qrand() % 100; | ||
Jani Honkonen
|
r796 | series0->append(x,y); | ||
series1->append(x,y); | ||||
series2->append(x,y); | ||||
Michal Klocek
|
r246 | } | ||
Michal Klocek
|
r482 | //![2] | ||
Michal Klocek
|
r246 | |||
Michal Klocek
|
r482 | //![3] | ||
Michal Klocek
|
r246 | m_series<<series0; | ||
Michal Klocek
|
r866 | m_titles<< m_chart->title()+": LineChart"; | ||
Michal Klocek
|
r246 | m_series<<series1; | ||
Michal Klocek
|
r866 | m_titles<< m_chart->title()+": ScatterChart"; | ||
Michal Klocek
|
r246 | m_series<<series2; | ||
Michal Klocek
|
r866 | m_titles<< m_chart->title()+": SplineChart"; | ||
Michal Klocek
|
r560 | m_series<<series3; | ||
Michal Klocek
|
r866 | m_titles<< m_chart->title()+": AreaChart"; | ||
Michal Klocek
|
r482 | //![3] | ||
Michal Klocek
|
r571 | |||
Michal Klocek
|
r866 | m_chart->addSeries(series0); | ||
m_chart->setTitle(m_titles.at(0)); | ||||
Michal Klocek
|
r246 | |||
Michal Klocek
|
r574 | //![4] | ||
Michal Klocek
|
r571 | foreach (QSeries* series, m_series) { | ||
QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&))); | ||||
} | ||||
Michal Klocek
|
r574 | //![4] | ||
Michal Klocek
|
r571 | |||
Michal Klocek
|
r246 | m_timer.start(); | ||
} | ||||
ChartView::~ChartView() | ||||
{ | ||||
Michal Klocek
|
r257 | if(m_series.size()==0) return; | ||
Michal Klocek
|
r866 | m_chart->removeSeries(m_series.at(m_index)); | ||
Michal Klocek
|
r257 | qDeleteAll(m_series); | ||
Michal Klocek
|
r246 | } | ||
Michal Klocek
|
r574 | //![5] | ||
Michal Klocek
|
r246 | void ChartView::handleTimeout() | ||
{ | ||||
if(m_series.size()==0) return; | ||||
Michal Klocek
|
r866 | m_chart->removeSeries(m_series.at(m_index)); | ||
Michal Klocek
|
r246 | m_index++; | ||
m_index=m_index%m_series.size(); | ||||
Michal Klocek
|
r866 | m_chart->addSeries(m_series.at(m_index)); | ||
m_chart->setTitle(m_titles.at(m_index)); | ||||
Michal Klocek
|
r246 | } | ||
Michal Klocek
|
r574 | //![5] | ||
Michal Klocek
|
r571 | |||
Michal Klocek
|
r574 | //![6] | ||
Michal Klocek
|
r571 | void ChartView::handlePointClicked(const QPointF& point) | ||
{ | ||||
Michal Klocek
|
r866 | m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y())); | ||
Michal Klocek
|
r571 | } | ||
Michal Klocek
|
r574 | //![6] | ||