##// END OF EJS Templates
Improved bar chart series
sauimone -
r71:1f789ed92d7b
parent child
Show More
@@ -1,3 +1,4
1 #include <QDebug>
1 2 #include "barchartseries.h"
2 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 4
@@ -6,23 +7,29 BarChartSeries::BarChartSeries(QObject *parent)
6 7 {
7 8 }
8 9
9 bool BarChartSeries::setData(QList<int> data)
10 bool BarChartSeries::setData(QAbstractItemModel* model)
10 11 {
11 mData = data;
12 return true;
12 mModel = model;
13 13 }
14 14
15
15 16 int BarChartSeries::min()
16 17 {
17 Q_ASSERT(mData.count() > 0);
18 Q_ASSERT(mModel->rowCount() > 0);
19 Q_ASSERT(mModel->columnCount() > 0);
18 20
19 21 // TODO: make min and max members and update them when data changes.
20 22 // This is slower since they are checked every time, even if data is same since previous call.
21 int min = mData.at(0);
23 QModelIndex modelIndex = mModel->index(0,0);
24 int min = mModel->data(modelIndex).toInt();
22 25
23 for (int i=0; i <mData.count(); i++) {
24 if (mData.at(i) < min) {
25 min = mData.at(i);
26 for (int i=0; i <mModel->rowCount(); i++) {
27 for(int j=0; j<mModel->columnCount(); j++) {
28 modelIndex = mModel->index(i,j);
29 int temp = mModel->data(modelIndex).toInt();
30 if (temp < min) {
31 min = temp;
32 }
26 33 }
27 34 }
28 35 return min;
@@ -30,26 +37,54 int BarChartSeries::min()
30 37
31 38 int BarChartSeries::max()
32 39 {
33 Q_ASSERT(mData.count() > 0);
40 Q_ASSERT(mModel->rowCount() > 0);
41 Q_ASSERT(mModel->columnCount() > 0);
34 42
35 int max = mData.at(0);
43 // TODO: make min and max members and update them when data changes.
44 // This is slower since they are checked every time, even if data is same since previous call.
45 QModelIndex modelIndex = mModel->index(0,0);
46 int max = mModel->data(modelIndex).toInt();
36 47
37 for (int i=0; i <mData.count(); i++) {
38 if (mData.at(i) > max) {
39 max = mData.at(i);
48 for (int i=0; i <mModel->rowCount(); i++) {
49 for(int j=0; j<mModel->columnCount(); j++) {
50 modelIndex = mModel->index(i,j);
51 int temp = mModel->data(modelIndex).toInt();
52 if (temp > max) {
53 max = temp;
54 }
40 55 }
41 56 }
42 57 return max;
43 58 }
44 59
45 int BarChartSeries::count()
60
61 int BarChartSeries::countSeries()
62 {
63 return mModel->rowCount();
64 }
65
66 int BarChartSeries::countItemsInSeries()
46 67 {
47 return mData.count();
68 return mModel->columnCount();
48 69 }
49 70
50 int BarChartSeries::valueAt(int i)
71 int BarChartSeries::countTotalItems()
51 72 {
52 return mData.at(i);
73 return mModel->rowCount() * mModel->columnCount();
53 74 }
54 75
76 int BarChartSeries::valueAt(int series, int item)
77 {
78 QModelIndex index = mModel->index(series,item);
79 return mModel->data(index).toInt();
80 }
81
82
83 void BarChartSeries::chartSizeChanged(QRectF rect)
84 {
85 qDebug() << "barchart size changed:" << rect;
86 }
87
88 #include "moc_barchartseries.cpp"
89
55 90 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,6 +2,8
2 2 #define BARCHARTSERIES_H
3 3
4 4 #include <QList>
5 #include <QRectF>
6 #include <QAbstractItemModel>
5 7 #include "qchartseries.h"
6 8 #include "qchartglobal.h"
7 9
@@ -10,28 +12,32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 12 // Container for series
11 13 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries
12 14 {
13 // TODO:
14 // Q_OBJECT
15 Q_OBJECT
15 16 public:
16 17 BarChartSeries(QObject* parent=0);
17 18
18 19 // from QChartSeries
19 20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
20 21
21 virtual bool setData(QList<int> data);
22 virtual bool setData(QList<qreal> data) {return false;}
23 virtual bool setData(QList<qreal> x, QList<qreal> y) {return false;}
24
22 // TODO: This as dataModel instead of n different setters. (data model itself can accept lists and whatnot)
23 virtual bool setData(QAbstractItemModel* model);
25 24
26 25 // Methods to find out minimum and maximum values of data
27 26 int min();
28 27 int max();
29 int count();
30 int valueAt(int i);
28 int countSeries();
29 int countItemsInSeries(); // Count items in one series.
30 int countTotalItems();
31 int valueAt(int series, int item);
32
33 public Q_SLOTS:
34
35 void chartSizeChanged(QRectF rect);
31 36
32 37 private:
33 38
34 QList<int> mData;
39 //QList<int> mData;
40 QAbstractItemModel* mModel;
35 41 };
36 42
37 43 QTCOMMERCIALCHART_END_NAMESPACE
@@ -4,11 +4,17
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 // TODO: singleton?
8 //BarGroup* BarGroup::mBarGroupInstance = NULL;
9
10 //BarGroup::BarGroup(QGraphicsItem *parent) :
11 // QGraphicsItem(parent)
12 // ,mSeries(series)
7 13 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
8 QGraphicsItem(parent)
9 ,mSeries(series)
10 ,mLayoutSet(false)
11 ,mLayoutDirty(true)
14 QGraphicsItem(parent)
15 ,mSeries(series)
16 ,mLayoutSet(false)
17 ,mLayoutDirty(true)
12 18 {
13 19 dataChanged();
14 20 }
@@ -68,7 +74,8 void BarGroup::dataChanged()
68 74 }
69 75
70 76 // Create new graphic items for bars
71 for (int i=0; i<mSeries.count(); i++) {
77 int totalItems = mSeries.countTotalItems();
78 for (int i=0; i<totalItems; i++) {
72 79 Bar *bar = new Bar(this);
73 80 childItems().append(bar);
74 81 }
@@ -80,7 +87,7 void BarGroup::layoutChanged()
80 87 {
81 88 // Scale bars to new layout
82 89 // Layout for bars:
83 int count = mSeries.count();
90 int count = mSeries.countSeries();
84 91 if (count <= 0) {
85 92 // Nothing to do.
86 93 return;
@@ -92,13 +99,17 void BarGroup::layoutChanged()
92 99 qDebug() << "startpos" << startPos;
93 100
94 101 // Scaling. TODO: better one.
95 for (int i=0; i<count; i++) {
96 int barHeight = mSeries.valueAt(i) * mHeight / mMax;
97 Bar* bar = reinterpret_cast<Bar*> (childItems().at(i));
98
99 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
100 bar->setColor(mColor);
101 bar->setPos(i*posStep+startPos, 0);
102 int itemIndex(0);
103 for (int series = 0; series < count; series++) {
104 for (int item=0; item < count; item++) {
105 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
106 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
107
108 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
109 bar->setColor(mColor);
110 bar->setPos(itemIndex*posStep+startPos, 0);
111 itemIndex++;
112 }
102 113 }
103 114 mLayoutDirty = true;
104 115 }
@@ -1,7 +1,7
1 1 #ifndef QBARCHART_H
2 2 #define QBARCHART_H
3 3
4 #include <QGraphicsItem>
4 #include <QGraphicsObject>
5 5
6 6 #include "bar.h"
7 7 //#include "qbarchartview.h"
@@ -10,11 +10,25
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 // TODO: Better name for this? The function of this class is to know where each bar in series is laid out.
13 // Class has knowledge of single series of data (owns vs reference?)
13 //class BarGroup : public QGraphicsItemGroup
14
14 15 class BarGroup : public QGraphicsItem
15 16 {
17 /* // TODO: implement as singleton?
18 private:
19 static BarGroup* mBarGroupInstance;
16 20
17 21 public:
22 static BarGroup* instance()
23 {
24 if (mBarGroupInstance == NULL) {
25 mBarGroupInstance = new BarGroup();
26 }
27 return mBarGroupInstance;
28 }
29 private:
30 */
31 public:
18 32 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
19 33
20 34 // Layout "api"
@@ -84,11 +84,9 void QChart::addSeries(QChartSeries* series)
84 84
85 85 qDebug() << "barSeries added";
86 86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
87 connect(this, SIGNAL(sizeChanged(QRectF)),
88 barSeries, SLOT(chartSizeChanged(QRectF)));
87 89
88 // Who owns the series?
89 BarGroup* group = new BarGroup(*barSeries, this);
90 scene()->addItem(group);
91 m_BarGroupItems.append(group); // If we need to access group later
92 90 break;
93 91 }
94 92 case QChartSeries::SeriesTypeScatter: {
@@ -78,7 +78,6 private:
78 78 int m_plotDataIndex;
79 79 int m_marginSize;
80 80 QList<QColor> m_themeColors;
81 QList<BarGroup*> m_BarGroupItems;
82 81 };
83 82
84 83 QTCOMMERCIALCHART_END_NAMESPACE
@@ -8,6 +8,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent)
10 10 {
11 qDebug() << "QChartSeries::create";
11 12 // TODO: Other types
12 13 switch (type) {
13 14 case QChartSeries::SeriesTypeLine: {
@@ -29,4 +30,5 QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent)
29 30 }
30 31 }
31 32
33 #include "moc_qchartseries.cpp"
32 34 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,13 +3,13
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QObject>
6 #include <QAbstractItemModel>
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9
9 10 class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject
10 11 {
11 //TODO:
12 //Q_OBJECT
12 Q_OBJECT
13 13 public:
14 14 enum QChartSeriesType {
15 15 SeriesTypeLine = 0,
@@ -35,6 +35,10 public:
35 35 virtual bool setData(QList<int> data) { return false; }
36 36 virtual bool setData(QList<qreal> data) { return false; }
37 37 virtual bool setData(QList<qreal> x, QList<qreal> y){ return false; }
38
39 // Prototype for data model. TODO: remove the other setData methods and use something like this for now?
40 virtual bool setData(QAbstractItemModel* model) { return false; }
41
38 42 };
39 43
40 44 QTCOMMERCIALCHART_END_NAMESPACE
@@ -303,7 +303,7 void MainWidget::yMaxChanged(int value)
303 303 void MainWidget::changeChartTheme(int themeIndex)
304 304 {
305 305 qDebug() << "changeChartTheme: " << themeIndex;
306 m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
306 // m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
307 307 }
308 308
309 309 void MainWidget::setPieSizeFactor(double size)
General Comments 0
You need to be logged in to leave comments. Login now