Add horizontal charts to chartviewer

This commit is contained in:
Michal Klocek 2012-07-30 18:34:50 +03:00
parent 707af58a67
commit 1a6e0b1256
10 changed files with 266 additions and 54 deletions

View File

@ -21,7 +21,7 @@
#ifndef CHARTS_H
#define CHARTS_H
#include "window.h"
#include "model.h"
#include <QList>
#include <QString>
#include <qchartglobal.h>

View File

@ -1,4 +1,13 @@
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp
SOURCES+= \
linechart.cpp \
scatterchart.cpp \
splinechart.cpp \
piechart.cpp \
verticalstackedbarchart.cpp \
horizontalstackedbarchart.cpp \
verticalbarchart.cpp \
horizontalbarchart.cpp \
areachart.cpp

View File

@ -0,0 +1,60 @@
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include "charts.h"
#include "qchart.h"
#include "qhorizontalbarseries.h"
#include "qbarset.h"
class HorizontalBarChart: public Chart
{
public:
HorizontalBarChart(){
initialize();
}
QString name() { return QObject::tr("HorizontalBarChart"); }
QString category() { return QObject::tr("BarSeries"); }
QString subCategory() { return QObject::tr("Vertical"); }
QChart* createChart(const DataTable& table)
{
QChart* chart = new QChart();
chart->setTitle("Horizontal bar chart");
QHorizontalBarSeries* series = new QHorizontalBarSeries(chart);
for (int i(0); i < table.count(); i++) {
QBarSet *set = new QBarSet("Bar set " + QString::number(i));
foreach (Data data, table[i])
*set << data.first.y();
series->append(set);
}
chart->addSeries(series);
chart->createDefaultAxes();
return chart;
}
};
DECLARE_CHART(HorizontalBarChart)

View File

@ -0,0 +1,60 @@
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include "charts.h"
#include "qchart.h"
#include "qhorizontalstackedbarseries.h"
#include "qbarset.h"
class HorizontalStackedBarChart: public Chart
{
public:
HorizontalStackedBarChart(){
initialize();
}
QString name() { return QObject::tr("HorizontalStackedBarChart"); }
QString category() { return QObject::tr("BarSeries"); }
QString subCategory() { return QObject::tr("Vertical"); }
QChart* createChart(const DataTable& table)
{
QChart* chart = new QChart();
chart->setTitle("Horizontal stacked chart");
QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries(chart);
for (int i(0); i < table.count(); i++) {
QBarSet *set = new QBarSet("Bar set " + QString::number(i));
foreach (Data data, table[i])
*set << data.first.y();
series->append(set);
}
chart->addSeries(series);
chart->createDefaultAxes();
return chart;
}
};
DECLARE_CHART(HorizontalStackedBarChart)

View File

@ -0,0 +1,60 @@
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include "charts.h"
#include "qchart.h"
#include "qbarseries.h"
#include "qbarset.h"
class VerticalBarChart: public Chart
{
public:
VerticalBarChart(){
initialize();
}
QString name() { return QObject::tr("VerticalBarChart"); }
QString category() { return QObject::tr("BarSeries"); }
QString subCategory() { return QObject::tr("Vertical"); }
QChart* createChart(const DataTable& table)
{
QChart* chart = new QChart();
chart->setTitle("Vertical bar chart");
QBarSeries* series = new QBarSeries(chart);
for (int i(0); i < table.count(); i++) {
QBarSet *set = new QBarSet("Bar set " + QString::number(i));
foreach (Data data, table[i])
*set << data.first.y();
series->append(set);
}
chart->addSeries(series);
chart->createDefaultAxes();
return chart;
}
};
DECLARE_CHART(VerticalBarChart)

View File

@ -23,11 +23,11 @@
#include "qstackedbarseries.h"
#include "qbarset.h"
class BarChart: public Chart
class VerticalStackedBarChart: public Chart
{
public:
BarChart(){
VerticalStackedBarChart(){
initialize();
}
@ -40,7 +40,7 @@ public:
QChart* chart = new QChart();
chart->setTitle("Bar chart");
chart->setTitle("Stacked bar chart");
QStackedBarSeries* series = new QStackedBarSeries(chart);
for (int i(0); i < table.count(); i++) {
@ -56,5 +56,5 @@ public:
};
DECLARE_CHART(BarChart)
DECLARE_CHART(VerticalStackedBarChart)

View File

@ -3,5 +3,5 @@ include(charts/charts.pri)
TARGET = chartviewer
QT += opengl
SOURCES += main.cpp window.cpp view.cpp charts.cpp
HEADERS += window.h view.h charts.h
HEADERS += window.h view.h charts.h model.h

67
demos/chartviewer/model.h Normal file
View File

@ -0,0 +1,67 @@
/****************************************************************************
**
** 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$
**
****************************************************************************/
#ifndef MODEL_H_
#define MODEL_H_
#include <QList>
#include <QPair>
#include <QPointF>
#include <QTime>
typedef QPair<QPointF, QString> Data;
typedef QList<Data> DataList;
typedef QList<DataList> DataTable;
class Model
{
private:
Model(){}
public:
static DataTable generateRandomData(int listCount, int valueMax, int valueCount)
{
DataTable dataTable;
// set seed for random stuff
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
// generate random data
for (int i(0); i < listCount; i++) {
DataList dataList;
qreal yValue(0);
for (int j(0); j < valueCount; j++) {
yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
QPointF value(
(j + (qreal) qrand() / (qreal) RAND_MAX)
* ((qreal) valueMax / (qreal) valueCount), yValue);
QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
dataList << Data(value, label);
}
dataTable << dataList;
}
return dataTable;
}
};
#endif

View File

@ -21,17 +21,7 @@
#include "window.h"
#include "view.h"
#include "charts.h"
#include <QChartView>
#include <QPieSeries>
#include <QPieSlice>
#include <QPercentBarSeries>
#include <QStackedBarSeries>
#include <QBarSeries>
#include <QBarSet>
#include <QLineSeries>
#include <QSplineSeries>
#include <QScatterSeries>
#include <QAreaSeries>
#include <QLegend>
#include <QGridLayout>
@ -41,8 +31,6 @@
#include <QCheckBox>
#include <QGroupBox>
#include <QLabel>
#include <QTime>
#include <QBarCategoriesAxis>
#include <QGraphicsScene>
#include <QGraphicsGridLayout>
#include <QGraphicsLinearLayout>
@ -58,7 +46,7 @@ Window::Window(QWidget* parent) :
m_valueCount(7),
m_scene(new QGraphicsScene(this)),
m_view(0),
m_dataTable(generateRandomData(m_listCount, m_valueMax, m_valueCount)),
m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)),
m_form(0),
m_themeComboBox(0),
m_antialiasCheckBox(0),
@ -97,9 +85,8 @@ Window::Window(QWidget* parent) :
Charts::ChartList list = Charts::chartList();
for(int i = 0 ; i < 6 ; ++i)
for(int i = 0 ; i < 9 && i<list.size() ; ++i)
{
if(!(i<list.size()) || list.isEmpty()) break;
QChart* chart = list.at(i)->createChart(m_dataTable);
baseLayout->addItem(chart, i/3, i%3);
m_chartList << chart;
@ -135,31 +122,6 @@ void Window::connectSignals()
QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
}
DataTable Window::generateRandomData(int listCount, int valueMax, int valueCount) const
{
DataTable dataTable;
// set seed for random stuff
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
// generate random data
for (int i(0); i < listCount; i++) {
DataList dataList;
qreal yValue(0);
for (int j(0); j < valueCount; j++) {
yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
QPointF value(
(j + (qreal) qrand() / (qreal) RAND_MAX)
* ((qreal) m_valueMax / (qreal) valueCount), yValue);
QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
dataList << Data(value, label);
}
dataTable << dataList;
}
return dataTable;
}
void Window::createProxyWidgets()
{
m_themeComboBox = createThemeBox();

View File

@ -20,6 +20,7 @@
#ifndef WINDOW_H_
#define WINDOW_H_
#include "model.h"
#include <QMainWindow>
#include <QChartGlobal>
#include <QHash>
@ -35,10 +36,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE
class QChart;
QTCOMMERCIALCHART_END_NAMESPACE
typedef QPair<QPointF, QString> Data;
typedef QList<Data> DataList;
typedef QList<DataList> DataTable;
QTCOMMERCIALCHART_USE_NAMESPACE
@ -51,10 +48,7 @@ public:
private Q_SLOTS:
void updateUI();
private:
DataTable generateRandomData(int listCount,int valueMax,int valueCount) const;
QComboBox* createThemeBox();
QComboBox* createAnimationBox();
QComboBox* createLegendBox();