2012-04-03 07:05:21 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** 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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-21 08:38:48 +00:00
|
|
|
#include "chartview.h"
|
2012-03-16 14:50:27 +00:00
|
|
|
#include <QLineSeries>
|
|
|
|
#include <QScatterSeries>
|
|
|
|
#include <QSplineSeries>
|
|
|
|
#include <QAreaSeries>
|
2012-02-21 08:38:48 +00:00
|
|
|
#include <QTime>
|
|
|
|
|
2012-04-04 07:37:29 +00:00
|
|
|
ChartView::ChartView(QChart* chart,QWidget* parent):QChartView(chart,parent),
|
2012-04-04 08:31:44 +00:00
|
|
|
m_index(-1),m_chart(chart)
|
2012-02-21 08:38:48 +00:00
|
|
|
{
|
2012-04-04 07:37:29 +00:00
|
|
|
m_chart->setTitle("Charts presenter");
|
2012-02-21 08:38:48 +00:00
|
|
|
QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
|
|
|
|
m_timer.setInterval(3000);
|
|
|
|
|
2012-03-07 11:32:18 +00:00
|
|
|
//![1]
|
2012-04-11 10:08:38 +00:00
|
|
|
QLineSeries* series0 = new QLineSeries();
|
2012-02-21 08:38:48 +00:00
|
|
|
QPen blue(Qt::blue);
|
|
|
|
blue.setWidth(3);
|
|
|
|
series0->setPen(blue);
|
2012-03-07 11:32:18 +00:00
|
|
|
|
2012-04-11 10:08:38 +00:00
|
|
|
QScatterSeries* series1 = new QScatterSeries();
|
2012-02-21 08:38:48 +00:00
|
|
|
QPen red(Qt::red);
|
|
|
|
red.setWidth(3);
|
|
|
|
series1->setPen(red);
|
2012-03-07 07:12:37 +00:00
|
|
|
series1->setBrush(Qt::white);
|
2012-03-07 11:32:18 +00:00
|
|
|
|
2012-04-11 10:08:38 +00:00
|
|
|
QSplineSeries* series2 = new QSplineSeries();
|
2012-02-21 08:38:48 +00:00
|
|
|
QPen green(Qt::green);
|
|
|
|
green.setWidth(3);
|
|
|
|
series2->setPen(green);
|
2012-03-13 14:42:20 +00:00
|
|
|
|
|
|
|
QAreaSeries* series3 = new QAreaSeries(series0);
|
|
|
|
QPen yellow(Qt::black);
|
|
|
|
yellow.setWidth(3);
|
|
|
|
series3->setPen(yellow);
|
|
|
|
series3->setBrush(Qt::yellow);
|
2012-03-07 11:32:18 +00:00
|
|
|
//![1]
|
2012-02-21 08:38:48 +00:00
|
|
|
|
2012-03-07 11:32:18 +00:00
|
|
|
//![2]
|
2012-02-21 08:38:48 +00:00
|
|
|
int numPoints = 10;
|
|
|
|
|
|
|
|
for (int x = 0; x <= numPoints; ++x) {
|
2012-03-07 07:12:37 +00:00
|
|
|
qreal y = qrand() % 100;
|
2012-03-29 11:57:19 +00:00
|
|
|
series0->append(x,y);
|
|
|
|
series1->append(x,y);
|
|
|
|
series2->append(x,y);
|
2012-02-21 08:38:48 +00:00
|
|
|
}
|
2012-03-07 11:32:18 +00:00
|
|
|
//![2]
|
2012-02-21 08:38:48 +00:00
|
|
|
|
2012-03-07 11:32:18 +00:00
|
|
|
//![3]
|
2012-02-21 08:38:48 +00:00
|
|
|
m_series<<series0;
|
2012-04-04 07:37:29 +00:00
|
|
|
m_titles<< m_chart->title()+": LineChart";
|
2012-02-21 08:38:48 +00:00
|
|
|
m_series<<series1;
|
2012-04-04 07:37:29 +00:00
|
|
|
m_titles<< m_chart->title()+": ScatterChart";
|
2012-02-21 08:38:48 +00:00
|
|
|
m_series<<series2;
|
2012-04-04 07:37:29 +00:00
|
|
|
m_titles<< m_chart->title()+": SplineChart";
|
2012-03-13 14:42:20 +00:00
|
|
|
m_series<<series3;
|
2012-04-04 07:37:29 +00:00
|
|
|
m_titles<< m_chart->title()+": AreaChart";
|
2012-03-07 11:32:18 +00:00
|
|
|
//![3]
|
2012-03-14 09:07:55 +00:00
|
|
|
|
2012-03-14 09:52:39 +00:00
|
|
|
//![4]
|
2012-04-11 13:01:10 +00:00
|
|
|
foreach (QAbstractSeries* series, m_series) {
|
2012-04-10 14:18:12 +00:00
|
|
|
QObject::connect(series,SIGNAL(clicked(QPointF)),this,SLOT(handlePointClicked(QPointF)));
|
2012-03-14 09:07:55 +00:00
|
|
|
}
|
2012-03-14 09:52:39 +00:00
|
|
|
//![4]
|
2012-02-21 08:38:48 +00:00
|
|
|
m_timer.start();
|
2012-04-04 09:09:57 +00:00
|
|
|
handleTimeout();
|
2012-02-21 08:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChartView::~ChartView()
|
|
|
|
{
|
2012-02-21 12:54:47 +00:00
|
|
|
if(m_series.size()==0) return;
|
2012-04-04 07:37:29 +00:00
|
|
|
m_chart->removeSeries(m_series.at(m_index));
|
2012-04-11 10:08:38 +00:00
|
|
|
m_series.removeLast(); //remove QAreaSeries instance since they will be deleted when QLineSeries instance is gone
|
2012-02-21 12:54:47 +00:00
|
|
|
qDeleteAll(m_series);
|
2012-02-21 08:38:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 09:52:39 +00:00
|
|
|
//![5]
|
2012-02-21 08:38:48 +00:00
|
|
|
void ChartView::handleTimeout()
|
|
|
|
{
|
|
|
|
if(m_series.size()==0) return;
|
2012-04-04 08:31:44 +00:00
|
|
|
if(m_index>=0)
|
2012-04-04 07:37:29 +00:00
|
|
|
m_chart->removeSeries(m_series.at(m_index));
|
2012-02-21 08:38:48 +00:00
|
|
|
m_index++;
|
|
|
|
m_index=m_index%m_series.size();
|
2012-04-04 07:37:29 +00:00
|
|
|
m_chart->addSeries(m_series.at(m_index));
|
|
|
|
m_chart->setTitle(m_titles.at(m_index));
|
2012-02-21 08:38:48 +00:00
|
|
|
}
|
2012-03-14 09:52:39 +00:00
|
|
|
//![5]
|
2012-03-14 09:07:55 +00:00
|
|
|
|
2012-03-14 09:52:39 +00:00
|
|
|
//![6]
|
2012-03-14 09:07:55 +00:00
|
|
|
void ChartView::handlePointClicked(const QPointF& point)
|
|
|
|
{
|
2012-04-04 07:37:29 +00:00
|
|
|
m_chart->setTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y()));
|
2012-03-14 09:07:55 +00:00
|
|
|
}
|
2012-03-14 09:52:39 +00:00
|
|
|
//![6]
|