diff --git a/src/xylinechart/axis.cpp b/src/axis.cpp similarity index 100% rename from src/xylinechart/axis.cpp rename to src/axis.cpp diff --git a/src/xylinechart/axis_p.h b/src/axis_p.h similarity index 100% rename from src/xylinechart/axis_p.h rename to src/axis_p.h diff --git a/src/xylinechart/qchart.cpp b/src/qchart.cpp similarity index 95% rename from src/xylinechart/qchart.cpp rename to src/qchart.cpp index 30fa184..0c10220 100644 --- a/src/xylinechart/qchart.cpp +++ b/src/qchart.cpp @@ -14,17 +14,16 @@ QChart::~QChart(){} QChart* QChart::createXYLineChart(const QList& dataset) { - XYLineChart* chart = new XYLineChart(); foreach (const QXYSeries* series,dataset) { - chart->addXYSeries(series); + chart->addXYSeries(series); } return chart; } void QChart::setMargin(int margin) { - m_marginSize=margin; + m_marginSize = margin; } QCHART_END_NAMESPACE diff --git a/src/xylinechart/qchart.h b/src/qchart.h similarity index 82% rename from src/xylinechart/qchart.h rename to src/qchart.h index 9c57036..f3d8353 100644 --- a/src/xylinechart/qchart.h +++ b/src/qchart.h @@ -9,6 +9,15 @@ QCHART_BEGIN_NAMESPACE class QCHART_EXPORT QChart : public QGraphicsItem { +public: + enum DataSeriesType { + DataSeriesTypeLine = 0, + DataSeriesTypeArea, + DataSeriesTypeBar, + DataSeriesTypePie, + DataSeriesTypeScatter, + DataSeriesTypeSpline + }; protected: QChart(QGraphicsItem* parent =0); diff --git a/src/qchartwidget.cpp b/src/qchartwidget.cpp new file mode 100644 index 0000000..b07bee8 --- /dev/null +++ b/src/qchartwidget.cpp @@ -0,0 +1,53 @@ +#include "qchartwidget.h" +#include "qxyseries.h" +#include "xylinechart_p.h" +#include +#include + +QCHART_BEGIN_NAMESPACE + +class QChartWidgetPrivate +{ +public: + QChartWidgetPrivate(QWidget *parent) : m_view(0), m_scene(0), m_chart(0) { + m_scene = new QGraphicsScene(); + m_view = new QGraphicsView(m_scene, parent); + m_view->resize(490, 300); + m_view->show(); + } + ~QChartWidgetPrivate() { + delete m_view; + delete m_scene; + } + + QGraphicsView *m_view; + QGraphicsScene *m_scene; + QChart* m_chart; +}; + +QChartWidget::QChartWidget(QWidget *parent) : + QWidget(parent), + d(new QChartWidgetPrivate(this)) +{ + setMinimumSize(d->m_view->size()); +} + +QChartWidget::~QChartWidget() +{ + delete d; +} + +void QChartWidget::addDataSeries( + QChart::DataSeriesType dataSeriesType, + QList dataset) +{ + // TODO: implement management of several data series of different types + + d->m_chart = QChart::createXYLineChart(dataset); + d->m_scene->addItem(d->m_chart); + d->m_chart->setSize(this->size()); +} + +#include "moc_qchartwidget.cpp" + +QCHART_END_NAMESPACE diff --git a/src/qchartwidget.h b/src/qchartwidget.h new file mode 100644 index 0000000..4679f16 --- /dev/null +++ b/src/qchartwidget.h @@ -0,0 +1,36 @@ +#ifndef QCHARTWIDGET_H +#define QCHARTWIDGET_H + +#include "qchartconfig.h" +#include "qchart.h" +#include + +QCHART_BEGIN_NAMESPACE + +class QXYSeries; +class QChartWidgetPrivate; + +class QCHART_EXPORT QChartWidget : public QWidget +{ + Q_OBJECT +public: + explicit QChartWidget(QWidget *parent = 0); + ~QChartWidget(); + // TODO: replace QXYSeries list with a charts data API + // TODO: return QChartDataSeries + void addDataSeries(QChart::DataSeriesType dataSeriesType, QList dataset); + +signals: + +public slots: + +private: + friend class QChartWidgetPrivate; + Q_DISABLE_COPY(QChartWidget) +// Q_DECLARE_PRIVATE(QChartWidget) + QChartWidgetPrivate * const d; +}; + +QCHART_END_NAMESPACE + +#endif // QCHARTWIDGET_H diff --git a/src/src.pro b/src/src.pro index 509e325..1cb11ea 100644 --- a/src/src.pro +++ b/src/src.pro @@ -4,27 +4,28 @@ QT += core \ gui CONFIG += debug_and_release CONFIG(debug, debug|release):TARGET = QChartd + SOURCES += \ - xylinechart/qchartwidget.cpp \ - xylinechart/qchart.cpp \ + xylinechart/qchartgraphicswidget.cpp \ xylinechart/qxyseries.cpp \ - xylinechart/axis.cpp \ xylinechart/xylinechart.cpp \ xylinechart/xygrid.cpp \ - xylinechart/xyplotdata.cpp + xylinechart/xyplotdata.cpp \ + qchart.cpp \ + axis.cpp \ + qchartwidget.cpp PRIVATE_HEADERS += \ xylinechart/xylinechart_p.h \ - xylinechart/axis_p.h \ xylinechart/xygrid_p.h \ - xylinechart/xyplotdata_p.h - + xylinechart/xyplotdata_p.h \ + axis_p.h PUBLIC_HEADERS += \ - xylinechart/qchart.h \ xylinechart/qxyseries.h \ - xylinechart/qchartwidget.h \ - qchartconfig.h - + xylinechart/qchartgraphicswidget.h \ + qchart.h \ + qchartwidget.h \ + qchartconfig.h HEADERS += $$PUBLIC_HEADERS HEADERS += $$PRIVATE_HEADERS diff --git a/src/xylinechart/qchartwidget.cpp b/src/xylinechart/qchartgraphicswidget.cpp similarity index 73% rename from src/xylinechart/qchartwidget.cpp rename to src/xylinechart/qchartgraphicswidget.cpp index 1c39c6b..0fa6065 100644 --- a/src/xylinechart/qchartwidget.cpp +++ b/src/xylinechart/qchartgraphicswidget.cpp @@ -1,38 +1,38 @@ -#include "qchartwidget.h" +#include "qchartgraphicswidget.h" #include "qchart.h" QCHART_BEGIN_NAMESPACE -class QChartWidgetPrivate +class QChartGraphicsWidgetPrivate { public: QChart* chart; //Q_DECLARE_PUBLIC(ChartWidget) - QChartWidgetPrivate() {} + QChartGraphicsWidgetPrivate() {} }; //////////////////////////////////////////////////////////////////////////// -QChartWidget::QChartWidget(QGraphicsItem *parent,Qt::WindowFlags wFlags) +QChartGraphicsWidget::QChartGraphicsWidget(QGraphicsItem *parent,Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), - d_ptr(new QChartWidgetPrivate()) + d_ptr(new QChartGraphicsWidgetPrivate()) { } -QChartWidget::~QChartWidget() +QChartGraphicsWidget::~QChartGraphicsWidget() { delete d_ptr; } -void QChartWidget::addChart(QChart *chart) +void QChartGraphicsWidget::addChart(QChart *chart) { d_ptr->chart=chart; chart->setParentItem(this); } -void QChartWidget::setGeometry(const QRectF & rect) +void QChartGraphicsWidget::setGeometry(const QRectF & rect) { d_ptr->chart->setSize(rect.size()); QGraphicsWidget::setGeometry(rect); diff --git a/src/xylinechart/qchartwidget.h b/src/xylinechart/qchartgraphicswidget.h similarity index 59% rename from src/xylinechart/qchartwidget.h rename to src/xylinechart/qchartgraphicswidget.h index 716b711..94b9373 100644 --- a/src/xylinechart/qchartwidget.h +++ b/src/xylinechart/qchartgraphicswidget.h @@ -1,5 +1,5 @@ -#ifndef QCHARTWIDGET_H -#define QCHARTWIDGET_H +#ifndef QCHARTGRAPHICSWIDGET_H +#define QCHARTGRAPHICSWIDGET_H #include #include @@ -9,14 +9,14 @@ class QGraphicsItem; QCHART_BEGIN_NAMESPACE class QChart; -class QChartWidgetPrivate; +class QChartGraphicsWidgetPrivate; -class QCHART_EXPORT QChartWidget : public QGraphicsWidget +class QCHART_EXPORT QChartGraphicsWidget : public QGraphicsWidget { public: - explicit QChartWidget(QGraphicsItem *parent = 0,Qt::WindowFlags wFlags = 0); - ~QChartWidget(); + explicit QChartGraphicsWidget(QGraphicsItem *parent = 0,Qt::WindowFlags wFlags = 0); + ~QChartGraphicsWidget(); QChart* chart() const; void addChart(QChart* chart); @@ -24,9 +24,9 @@ public: virtual void setGeometry(const QRectF & rect); private: - Q_DISABLE_COPY(QChartWidget) - Q_DECLARE_PRIVATE(QChartWidget) - QChartWidgetPrivate * const d_ptr; + Q_DISABLE_COPY(QChartGraphicsWidget) + Q_DECLARE_PRIVATE(QChartGraphicsWidget) + QChartGraphicsWidgetPrivate * const d_ptr; }; QCHART_END_NAMESPACE diff --git a/src/xylinechart/qxyseries.cpp b/src/xylinechart/qxyseries.cpp index 84853de..7c32022 100644 --- a/src/xylinechart/qxyseries.cpp +++ b/src/xylinechart/qxyseries.cpp @@ -1,7 +1,9 @@ #include "qxyseries.h" +QCHART_BEGIN_NAMESPACE + QXYSeries::QXYSeries(): -m_color(Qt::black) + m_color(Qt::black) { } @@ -57,3 +59,5 @@ QDebug operator<< (QDebug debug, const QXYSeries series) } return debug.space(); } + +QCHART_END_NAMESPACE diff --git a/src/xylinechart/qxyseries.h b/src/xylinechart/qxyseries.h index 1ca9074..3d98f74 100644 --- a/src/xylinechart/qxyseries.h +++ b/src/xylinechart/qxyseries.h @@ -1,10 +1,13 @@ #ifndef QXYSERIES_H_ #define QXYSERIES_H_ + +#include "qchartconfig.h" #include #include +QCHART_BEGIN_NAMESPACE -class QXYSeries +class QCHART_EXPORT QXYSeries { public: QXYSeries(); @@ -25,4 +28,6 @@ private: }; +QCHART_END_NAMESPACE + #endif diff --git a/test/chartwidgettest/chartwidgettest.pro b/test/chartwidgettest/chartwidgettest.pro index fce9129..2457fd6 100644 --- a/test/chartwidgettest/chartwidgettest.pro +++ b/test/chartwidgettest/chartwidgettest.pro @@ -7,22 +7,23 @@ contains(QT_MAJOR_VERSION, 5) { } CONFIG += charts -CHARTS += widget OBJECTS_DIR = tmp MOC_DIR = tmp SOURCES += main.cpp \ mainwidget.cpp \ - qscatterseries.cpp \ - qchartwidget.cpp \ - qseriespointgraphicsitem.cpp +# qscatterseries.cpp \ +# qseriespointgraphicsitem.cpp \ + dataseriedialog.cpp HEADERS += \ mainwidget.h \ - qscatterseries.h \ - qchartwidget.h \ - qseriespointgraphicsitem.h +# qscatterseries.h \ +# qseriespointgraphicsitem.h \ + dataseriedialog.h + + diff --git a/test/chartwidgettest/dataseriedialog.cpp b/test/chartwidgettest/dataseriedialog.cpp new file mode 100644 index 0000000..7635875 --- /dev/null +++ b/test/chartwidgettest/dataseriedialog.cpp @@ -0,0 +1,24 @@ +#include "dataseriedialog.h" +#include +#include +#include + +DataSerieDialog::DataSerieDialog(QWidget *parent) : + QDialog(parent) +{ + QDialogButtonBox *addSeriesBox = new QDialogButtonBox(Qt::Vertical, this); + addSeriesBox->addButton("Line", QDialogButtonBox::AcceptRole); + addSeriesBox->addButton("Area", QDialogButtonBox::AcceptRole); + addSeriesBox->addButton("Bar", QDialogButtonBox::AcceptRole); + addSeriesBox->addButton("Pie", QDialogButtonBox::AcceptRole); + addSeriesBox->addButton("Scatter", QDialogButtonBox::AcceptRole); + addSeriesBox->addButton("Spline", QDialogButtonBox::AcceptRole); + connect(addSeriesBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(addSeries(QAbstractButton *))); + this->setFixedSize(addSeriesBox->sizeHint()); +} + +void DataSerieDialog::addSeries(QAbstractButton *button) +{ + addSeries(button->text().toAscii()); + accept(); +} diff --git a/test/chartwidgettest/dataseriedialog.h b/test/chartwidgettest/dataseriedialog.h new file mode 100644 index 0000000..12f4c20 --- /dev/null +++ b/test/chartwidgettest/dataseriedialog.h @@ -0,0 +1,21 @@ +#ifndef DATASERIEDIALOG_H +#define DATASERIEDIALOG_H + +#include + +class QAbstractButton; + +class DataSerieDialog : public QDialog +{ + Q_OBJECT +public: + explicit DataSerieDialog(QWidget *parent = 0); + +signals: + void addSeries(QString series); + +public slots: + void addSeries(QAbstractButton *button); +}; + +#endif // DATASERIEDIALOG_H diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 8d8cd4f..74b342d 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -1,6 +1,6 @@ #include "mainwidget.h" -#include "qchartwidget.h" -//#include +#include "dataseriedialog.h" +#include #include #include #include @@ -13,6 +13,8 @@ #include #include +QCHART_USE_NAMESPACE + MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { @@ -22,15 +24,18 @@ MainWidget::MainWidget(QWidget *parent) : // Chart type // TODO: How about multiple types? // Should the type be a property of a graph instead of the chart? - QComboBox *chartTypeCombo = new QComboBox(this); - chartTypeCombo->addItem("Line"); - chartTypeCombo->addItem("Area"); - chartTypeCombo->addItem("Bar"); - chartTypeCombo->addItem("Pie"); - chartTypeCombo->addItem("Scatter"); - chartTypeCombo->addItem("Spline"); - connect(chartTypeCombo, SIGNAL(currentIndexChanged(int)), - this, SLOT(chartTypeChanged(int))); +// QComboBox *chartTypeCombo = new QComboBox(this); +// chartTypeCombo->addItem("Line"); +// chartTypeCombo->addItem("Area"); +// chartTypeCombo->addItem("Bar"); +// chartTypeCombo->addItem("Pie"); +// chartTypeCombo->addItem("Scatter"); +// chartTypeCombo->addItem("Spline"); +// connect(chartTypeCombo, SIGNAL(currentIndexChanged(int)), +// this, SLOT(chartTypeChanged(int))); + + QPushButton *addSeriesButton = new QPushButton("Add series"); + connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); // Test data selector QComboBox *testDataCombo = new QComboBox(this); @@ -78,8 +83,10 @@ MainWidget::MainWidget(QWidget *parent) : QGridLayout *grid = new QGridLayout(); QHBoxLayout *hbox = new QHBoxLayout(); - grid->addWidget(new QLabel("Chart type:"), 0, 0); - grid->addWidget(chartTypeCombo, 0, 1); + //grid->addWidget(new QLabel("Add series:"), 0, 0); + grid->addWidget(addSeriesButton, 0, 1); +// grid->addWidget(new QLabel("Chart type:"), 0, 0); +// grid->addWidget(chartTypeCombo, 0, 1); grid->addWidget(new QLabel("Data:"), 1, 0); grid->addWidget(testDataCombo, 1, 1); grid->addWidget(new QLabel("Background:"), 2, 0); @@ -108,15 +115,39 @@ MainWidget::MainWidget(QWidget *parent) : testDataChanged(0); } +void MainWidget::addSeries() +{ + DataSerieDialog dialog(this); + connect(&dialog, SIGNAL(addSeries(QString)), this, SLOT(addSeries(QString))); + dialog.exec(); +} + +void MainWidget::addSeries(QString series) +{ + if (series == "Scatter") { + QXYSeries* series0 = new QXYSeries(); + series0->setColor(Qt::blue); + for (int x = 0; x < 100; x++) + series0->add(x, abs(sin(3.14159265358979 / 50 * x) * 100)); + QList dataset; + dataset << series0; + m_chartWidget->addDataSeries(QChart::DataSeriesTypeScatter, dataset); + //m_chartWidget->addDataSeries(dataset); + } else { + // TODO + qDebug() << "addSeries: " << series; + } +} + void MainWidget::chartTypeChanged(int itemIndex) { // TODO: change chart type switch (itemIndex) { case 4: - m_chartWidget->setType(4); + //m_chartWidget->setType(4); break; default: { - m_chartWidget->setType(0); + //m_chartWidget->setType(0); break; } } @@ -126,37 +157,36 @@ void MainWidget::testDataChanged(int itemIndex) { qDebug() << "testDataChanged: " << itemIndex; - switch (itemIndex) { - case 0: { - QList data; - for (int x = 0; x < 20; x++) { - data.append(QChartDataPoint() << x << x / 2); - } - m_chartWidget->setData(data); - break; - } - case 1: { - QList data; - for (int x = 0; x < 100; x++) { - data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); - } - m_chartWidget->setData(data); - break; - } - case 2: { - QList data; - for (int x = 0; x < 1000; x++) { - data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); - data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); - data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); - } - m_chartWidget->setData(data); - break; - } - default: - break; - } - +// switch (itemIndex) { +// case 0: { +// QList data; +// for (int x = 0; x < 20; x++) { +// data.append(QChartDataPoint() << x << x / 2); +// } +// m_chartWidget->setData(data); +// break; +// } +// case 1: { +// QList data; +// for (int x = 0; x < 100; x++) { +// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); +// } +// m_chartWidget->setData(data); +// break; +// } +// case 2: { +// QList data; +// for (int x = 0; x < 1000; x++) { +// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); +// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); +// data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); +// } +// m_chartWidget->setData(data); +// break; +// } +// default: +// break; +// } } void MainWidget::backgroundChanged(int itemIndex) diff --git a/test/chartwidgettest/mainwidget.h b/test/chartwidgettest/mainwidget.h index b482d53..d0c2591 100644 --- a/test/chartwidgettest/mainwidget.h +++ b/test/chartwidgettest/mainwidget.h @@ -1,12 +1,15 @@ #ifndef MAINWIDGET_H #define MAINWIDGET_H +#include +#include #include -class QChartWidget; class QSpinBox; class QCheckBox; +QCHART_USE_NAMESPACE + class MainWidget : public QWidget { Q_OBJECT @@ -17,6 +20,8 @@ signals: private slots: void chartTypeChanged(int itemIndex); + void addSeries(); + void addSeries(QString series); void testDataChanged(int itemIndex); void backgroundChanged(int itemIndex); void autoScaleChanged(int value); diff --git a/test/chartwidgettest/qchartwidget.cpp b/test/chartwidgettest/qchartwidget.cpp deleted file mode 100644 index d145942..0000000 --- a/test/chartwidgettest/qchartwidget.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "qchartwidget.h" -#include "qscatterseries.h" -#include -#include - -QChartWidget::QChartWidget(QWidget *parent) : - QWidget(parent), - m_scatter(0) -{ - m_scene = new QGraphicsScene(); - m_view = new QGraphicsView(m_scene, this); - m_view->resize(490, 300); - m_view->show(); -} - -QChartWidget::~QChartWidget() -{ - delete m_view; - delete m_scene; -} - -void QChartWidget::setType(/*TODO QChart::Type*/ int type) -{ - if (type == 4) { - if (!m_scatter) { - m_scatter = new QScatterSeries(); - m_scene->addItem(m_scatter); - } - } else { - if (m_scatter) { - m_scene->removeItem(m_scatter); - delete m_scatter; - m_scatter = 0; - } - } -} - -void QChartWidget::setData(QList data) -//void QChartWidget::setData(QList data) -{ - // TODO: other chart types... this looks ugly - if (m_scatter) - m_scatter->setData(data); -} diff --git a/test/chartwidgettest/qchartwidget.h b/test/chartwidgettest/qchartwidget.h deleted file mode 100644 index 29261d3..0000000 --- a/test/chartwidgettest/qchartwidget.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef QCHARTWIDGET_H -#define QCHARTWIDGET_H - -#include - -class QGraphicsView; -class QGraphicsScene; -class QScatterSeries; - -// TODO: -#define QChartDataPoint QList - -class QChartWidget : public QWidget -{ - Q_OBJECT -public: - explicit QChartWidget(QWidget *parent = 0); - ~QChartWidget(); - void setType(/*TODO QChart::Type*/ int type); - void setData(QList data); -// void setData(QList data); - -signals: - -public slots: - -private: - QGraphicsView *m_view; - QGraphicsScene *m_scene; - QScatterSeries *m_scatter; -}; - -#endif // QCHARTWIDGET_H