From 681c12da395a9592ca972ae45b20f651d0326495 2012-01-26 09:45:08 From: sauimone Date: 2012-01-26 09:45:08 Subject: [PATCH] Added bar chart example --- diff --git a/example/barchart/barchart.pro b/example/barchart/barchart.pro new file mode 100644 index 0000000..3cc7fa6 --- /dev/null +++ b/example/barchart/barchart.pro @@ -0,0 +1,17 @@ +!include( ../../common.pri ) { + error( "Couldn't find the common.pri file!" ) +} + +!include( ../../integrated.pri ) { + error( "Couldn't find the integrated.pri file !") +} + +TARGET = barchart +TEMPLATE = app +QT += core gui +SOURCES += main.cpp \ + chartwidget.cpp + +HEADERS += \ + chartwidget.h + diff --git a/example/barchart/chartwidget.cpp b/example/barchart/chartwidget.cpp new file mode 100644 index 0000000..d6c7532 --- /dev/null +++ b/example/barchart/chartwidget.cpp @@ -0,0 +1,6 @@ +#include "chartwidget.h" + +ChartWidget::ChartWidget(QWidget *parent) : + QChartView(parent) +{ +} diff --git a/example/barchart/chartwidget.h b/example/barchart/chartwidget.h new file mode 100644 index 0000000..b5625e3 --- /dev/null +++ b/example/barchart/chartwidget.h @@ -0,0 +1,21 @@ +#ifndef CHARTWIDGET_H +#define CHARTWIDGET_H + +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + + +class ChartWidget : public QChartView +{ + Q_OBJECT +public: + explicit ChartWidget(QWidget *parent = 0); + +signals: + +public slots: + +}; + +#endif // CHARTWIDGET_H diff --git a/example/barchart/main.cpp b/example/barchart/main.cpp new file mode 100644 index 0000000..c5f529c --- /dev/null +++ b/example/barchart/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include "chartwidget.h" + +QTCOMMERCIALCHART_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QMainWindow window; + + BarChartSeries* series0 = new BarChartSeries(); + + // Create some test data to chart + QStandardItemModel dataModel(2,10); + QModelIndex index; + index = dataModel.index(0,0); + // Series 1, items 6 to 9 missing. + dataModel.setData(dataModel.index(0,0),1); + dataModel.setData(dataModel.index(0,1),12); + dataModel.setData(dataModel.index(0,2),5); + dataModel.setData(dataModel.index(0,3),8); + dataModel.setData(dataModel.index(0,4),17); + dataModel.setData(dataModel.index(0,5),9); + + // Series 2, some other items missing + dataModel.setData(dataModel.index(1,0),5); + dataModel.setData(dataModel.index(1,3),4); + dataModel.setData(dataModel.index(1,5),7); + dataModel.setData(dataModel.index(1,6),8); + dataModel.setData(dataModel.index(1,8),9); + dataModel.setData(dataModel.index(1,9),9); + + series0->setData(&dataModel); + + ChartWidget* chartWidget = new ChartWidget(&window); + chartWidget->addSeries(series0); + + window.setCentralWidget(chartWidget); + window.resize(400, 300); + window.show(); + + return a.exec(); +} + diff --git a/example/example.pro b/example/example.pro index c358c53..1ce070d 100644 --- a/example/example.pro +++ b/example/example.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += linechart \ zoomlinechart \ - colorlinechart + colorlinechart \ + barchart diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index fff33d8..5f22824 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -23,6 +23,7 @@ void Bar::setPlotDomain(const PlotDomain& data) void Bar::resize( int w, int h ) { + qDebug() << "bar::resize" << w << h; mWidth = w; mHeight = h; } @@ -33,6 +34,7 @@ void Bar::setColor( QColor col ) } void Bar::setPos(qreal x, qreal y) { + qDebug() << "Bar::setpos" << x << y; mXpos = x; mYpos = y; } diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index c35aac2..353926d 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -10,15 +10,30 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE //BarGroup::BarGroup(QGraphicsItem *parent) : // QGraphicsItem(parent) // ,mSeries(series) -BarGroup::BarGroup(BarChartSeries& series, ChartItem *parent) : +BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : ChartItem(parent) ,mSeries(series) ,mLayoutSet(false) ,mLayoutDirty(true) + ,mBarDefaultWidth(10) { dataChanged(); } + +void BarGroup::setSize(const QSize& size) +{ + // TODO: refactor this + qDebug() << "BarGroup::setSize"; + resize(size.width(),size.height()); +} + +void BarGroup::setPlotDomain(const PlotDomain& data) +{ + qDebug() << "BarGroup::setPlotDomain"; +} + + void BarGroup::resize( int w, int h ) { qDebug() << "QBarChart::resize"; @@ -87,27 +102,33 @@ void BarGroup::layoutChanged() { // Scale bars to new layout // Layout for bars: - int count = mSeries.countSeries(); - if (count <= 0) { + if (mSeries.countSeries() <= 0) { // Nothing to do. return; } // Align center + int count = mSeries.countItemsInSeries(); int posStep = (mWidth / (count)); int startPos = (mWidth / count+1); qDebug() << "startpos" << startPos; // Scaling. TODO: better one. int itemIndex(0); - for (int series = 0; series < count; series++) { - for (int item=0; item < count; item++) { + for (int series = 0; series < mSeries.countSeries(); series++) { + for (int item=0; item < mSeries.countItemsInSeries(); item++) { + qDebug() << itemIndex; int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar - bar->setColor(mColor); - bar->setPos(itemIndex*posStep+startPos, 0); + //TODO: test hack + if (0 == series) { + bar->setColor(QColor(255,0,0,128)); + } else { + bar->setColor(QColor(255,255,0,128)); + } + bar->setPos(itemIndex*posStep+startPos + series * mBarDefaultWidth, 0); itemIndex++; } } diff --git a/src/barchart/bargroup.h b/src/barchart/bargroup.h index 23cd6b4..6d69d5d 100644 --- a/src/barchart/bargroup.h +++ b/src/barchart/bargroup.h @@ -27,7 +27,11 @@ public: private: */ public: - explicit BarGroup(BarChartSeries& series, ChartItem *parent = 0); + explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); + + // From ChartItem + virtual void setSize(const QSize& size); + virtual void setPlotDomain(const PlotDomain& data); // Layout "api" void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area diff --git a/src/qchart.cpp b/src/qchart.cpp index bb73d17..cc403a7 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -84,9 +84,9 @@ void QChart::addSeries(QChartSeries* series) qDebug() << "barSeries added"; BarChartSeries* barSeries = static_cast(series); - connect(this, SIGNAL(sizeChanged(QRectF)), - barSeries, SLOT(chartSizeChanged(QRectF))); - + BarGroup* barGroup = new BarGroup(*barSeries,this); + m_chartItems< #include #include +#include + QTCOMMERCIALCHART_USE_NAMESPACE @@ -192,14 +194,29 @@ void MainWidget::addSeries(QString series, QString data) // This is the another way of creating series. Should we create test cases for both ways, if we support them? qDebug() << "Bar chart series"; newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this); - QList barData; - barData << 1; - barData << 12; - barData << 5; - barData << 8; - barData << 17; - barData << 9; - newSeries->setData(barData); + + // Create some test data to chart + QStandardItemModel dataModel(2,10,this); + QModelIndex index; + index = dataModel.index(0,0); + // Series 1, items 6 to 9 missing. + dataModel.setData(dataModel.index(0,0),1); + dataModel.setData(dataModel.index(0,1),12); + dataModel.setData(dataModel.index(0,2),5); + dataModel.setData(dataModel.index(0,3),8); + dataModel.setData(dataModel.index(0,4),17); + dataModel.setData(dataModel.index(0,5),9); + + // Series 2, some other items missing + dataModel.setData(dataModel.index(1,0),5); + dataModel.setData(dataModel.index(1,3),4); + dataModel.setData(dataModel.index(1,5),7); + dataModel.setData(dataModel.index(1,6),8); + dataModel.setData(dataModel.index(1,8),9); + dataModel.setData(dataModel.index(1,9),9); + + newSeries->setData(&dataModel); + m_chartWidget->addSeries(newSeries); }