##// END OF EJS Templates
Barset and barcategory implememtation. Updated test application
sauimone -
r171:d4b8c60ed973
parent child
Show More
@@ -14,7 +14,7 int main(int argc, char *argv[])
14 QMainWindow window;
14 QMainWindow window;
15
15
16 QBarCategory category;
16 QBarCategory category;
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
18
18
19 BarChartSeries* series0 = new BarChartSeries(category);
19 BarChartSeries* series0 = new BarChartSeries(category);
20
20
@@ -14,7 +14,7 int main(int argc, char *argv[])
14 QMainWindow window;
14 QMainWindow window;
15
15
16 QBarCategory category;
16 QBarCategory category;
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
18
18
19 PercentBarChartSeries* series0 = new PercentBarChartSeries(category);
19 PercentBarChartSeries* series0 = new PercentBarChartSeries(category);
20
20
@@ -14,7 +14,7 int main(int argc, char *argv[])
14 QMainWindow window;
14 QMainWindow window;
15
15
16 QBarCategory category;
16 QBarCategory category;
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
18
18
19 StackedBarChartSeries* series0 = new StackedBarChartSeries(category);
19 StackedBarChartSeries* series0 = new StackedBarChartSeries(category);
20
20
@@ -9,67 +9,37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) :
10 BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) :
11 QObject(parent)
11 QObject(parent)
12 ,mRunningId(1)
13 ,mCategory(category)
12 ,mCategory(category)
14 {
13 {
15 }
14 }
16
15
17 BarChartModel::~BarChartModel()
16 QBarCategory& BarChartModel::category()
18 {
17 {
19 // qDebug() << "BarChartModel::~BarChartModel";
18 return mCategory;
20 foreach (DataContainer* c, mDataModel) {
21 delete c;
22 }
23 }
24
25 int BarChartModel::addData(QList<qreal> data)
26 {
27 // qDebug() << "BarChartModel::addData" << data.count();
28 DataContainer* c = new DataContainer(data,mRunningId);
29 mDataModel.append(c);
30 mRunningId++;
31 emit modelUpdated();
32 return mRunningId-1;
33 }
34
35 void BarChartModel::removeData(int id)
36 {
37 // qDebug() << "BarChartModel::removeData";
38 foreach(DataContainer* c, mDataModel) {
39 if (c->mId == id) {
40 mDataModel.removeOne(c);
41 delete c;
42 }
43 }
44 emit modelUpdated();
45 }
19 }
46
20
47 void BarChartModel::addBarSet(QBarSet &set)
21 void BarChartModel::addBarSet(QBarSet &set)
48 {
22 {
49 DataContainer* c = new DataContainer(set.mValues,mRunningId);
23 mDataModel.append(&set);
50 mDataModel.append(c);
51 mRunningId++;
52 }
24 }
53
25
54 void BarChartModel::removeBarSet(QBarSet &set)
26 void BarChartModel::removeBarSet(QBarSet &set)
55 {
27 {
56 // TODO:
28 mDataModel.removeOne(&set);
57 }
29 }
58
30
59
31
60 int BarChartModel::countRows()
32 int BarChartModel::countSets()
61 {
33 {
62 // qDebug() << "BarChartModel::countRows";
63 return mDataModel.count();
34 return mDataModel.count();
64 }
35 }
65
36
66 int BarChartModel::countColumns()
37 int BarChartModel::countCategories()
67 {
38 {
68 // qDebug() << "BarChartModel::countColumns";
69 int count(0);
39 int count(0);
70 for (int i=0; i<mDataModel.count(); i++){
40 for (int i=0; i<mDataModel.count(); i++){
71 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
41 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
72 int temp = mDataModel.at(i)->countColumns();
42 int temp = mDataModel.at(i)->count();
73 if (temp > count) {
43 if (temp > count) {
74 count = temp;
44 count = temp;
75 }
45 }
@@ -79,21 +49,19 int BarChartModel::countColumns()
79
49
80 int BarChartModel::countTotalItems()
50 int BarChartModel::countTotalItems()
81 {
51 {
82 // qDebug() << "BarChartModel::countTotalItems";
52 int total = mDataModel.count() * countCategories();
83 int total = mDataModel.count() * countColumns();
84 return total;
53 return total;
85 }
54 }
86
55
87 qreal BarChartModel::min()
56 qreal BarChartModel::min()
88 {
57 {
89 // qDebug() << "BarChartModel::min";
90 Q_ASSERT(mDataModel.count() > 0);
58 Q_ASSERT(mDataModel.count() > 0);
91 // TODO: make min and max members and update them when data changes.
59 // TODO: make min and max members and update them when data changes.
92 // This is slower since they are checked every time, even if data is same since previous call.
60 // This is slower since they are checked every time, even if data is same since previous call.
93 qreal min = INT_MAX;
61 qreal min = INT_MAX;
94
62
95 for (int i=0; i <mDataModel.count(); i++) {
63 for (int i=0; i <mDataModel.count(); i++) {
96 int itemCount = mDataModel.at(i)->countColumns();
64 int itemCount = mDataModel.at(i)->count();
97 for (int j=0; j<itemCount; j++) {
65 for (int j=0; j<itemCount; j++) {
98 qreal temp = mDataModel.at(i)->valueAt(j);
66 qreal temp = mDataModel.at(i)->valueAt(j);
99 if (temp < min) {
67 if (temp < min) {
@@ -106,7 +74,6 qreal BarChartModel::min()
106
74
107 qreal BarChartModel::max()
75 qreal BarChartModel::max()
108 {
76 {
109 // qDebug() << "BarChartModel::max";
110 Q_ASSERT(mDataModel.count() > 0);
77 Q_ASSERT(mDataModel.count() > 0);
111
78
112 // TODO: make min and max members and update them when data changes.
79 // TODO: make min and max members and update them when data changes.
@@ -114,7 +81,7 qreal BarChartModel::max()
114 qreal max = INT_MIN;
81 qreal max = INT_MIN;
115
82
116 for (int i=0; i <mDataModel.count(); i++) {
83 for (int i=0; i <mDataModel.count(); i++) {
117 int itemCount = mDataModel.at(i)->countColumns();
84 int itemCount = mDataModel.at(i)->count();
118 for (int j=0; j<itemCount; j++) {
85 for (int j=0; j<itemCount; j++) {
119 qreal temp = mDataModel.at(i)->valueAt(j);
86 qreal temp = mDataModel.at(i)->valueAt(j);
120 if (temp > max) {
87 if (temp > max) {
@@ -126,43 +93,39 qreal BarChartModel::max()
126 return max;
93 return max;
127 }
94 }
128
95
129 qreal BarChartModel::valueAt(int series, int item)
96 qreal BarChartModel::valueAt(int set, int category)
130 {
97 {
131 // qDebug() << "BarChartModel::valueAt" << series << item;
98 if ((set < 0) || (set >= mDataModel.count())) {
132 if ((series < 0) || (series >= mDataModel.count())) {
99 // No set, no value.
133 // No series, no value.
134 return 0;
100 return 0;
135 } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) {
101 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
136 // No item, no value.
102 // No category, no value.
137 return 0;
103 return 0;
138 }
104 }
139
105
140 // qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item);
106 return mDataModel.at(set)->valueAt(category);
141 return mDataModel.at(series)->valueAt(item);
142 }
107 }
143
108
144 qreal BarChartModel::columnSum(int column)
109 qreal BarChartModel::categorySum(int column)
145 {
110 {
146 // qDebug() << "BarChartModel::columnSum";
111 qreal sum(0);
147 int sum(0);
148 int count = mDataModel.count(); // Count rows
112 int count = mDataModel.count(); // Count rows
149
113
150 for (int row = 0; row < count; row++) {
114 for (int row = 0; row < count; row++) {
151 if (column < mDataModel.at(row)->countColumns()) {
115 if (column < mDataModel.at(row)->count()) {
152 sum += mDataModel.at(row)->valueAt(column);
116 sum += mDataModel.at(row)->valueAt(column);
153 }
117 }
154 }
118 }
155 return sum;
119 return sum;
156 }
120 }
157
121
158 qreal BarChartModel::maxColumnSum()
122 qreal BarChartModel::maxCategorySum()
159 {
123 {
160 // qDebug() << "BarChartModel::maxColumnSum";
124 qreal max = INT_MIN;
161 int max = INT_MIN;
125 int count = countCategories();
162 int count = countColumns();
163
126
164 for (int col=0; col<count; col++) {
127 for (int col=0; col<count; col++) {
165 int sum = columnSum(col);
128 qreal sum = categorySum(col);
166 if (sum > max) {
129 if (sum > max) {
167 max = sum;
130 max = sum;
168 }
131 }
@@ -17,25 +17,21 class BarChartModel : public QObject //, public QAbstractItemModel
17 Q_OBJECT
17 Q_OBJECT
18 public:
18 public:
19 explicit BarChartModel(QBarCategory &category, QObject *parent = 0);
19 explicit BarChartModel(QBarCategory &category, QObject *parent = 0);
20 ~BarChartModel();
21
22 // TODO: remove these after add and remove QBarSet works.
23 int addData(QList<qreal> data);
24 void removeData(int id);
25
20
21 QBarCategory& category();
26 void addBarSet(QBarSet &set);
22 void addBarSet(QBarSet &set);
27 void removeBarSet(QBarSet &set);
23 void removeBarSet(QBarSet &set);
28
24
29 int countRows(); // Number of series in model
25 int countSets(); // Number of sets in model
30 int countColumns(); // Maximum number of items in series
26 int countCategories(); // Number of categories
31 int countTotalItems(); // Total items in all series. Includes empty items.
27 int countTotalItems(); // Total items in all sets. Includes empty items.
32
28
33 qreal max(); // Maximum value of all series
29 qreal max(); // Maximum value of all sets
34 qreal min(); // Minimum value of all series
30 qreal min(); // Minimum value of all sets
35 qreal valueAt(int series, int item);
31 qreal valueAt(int set, int category);
36
32
37 qreal columnSum(int column);
33 qreal categorySum(int column);
38 qreal maxColumnSum(); // returns maximum sum of items in all columns.
34 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
39
35
40 signals:
36 signals:
41 void modelUpdated();
37 void modelUpdated();
@@ -44,22 +40,7 public slots:
44
40
45 private:
41 private:
46
42
47 // Little helper class.
43 QList<QBarSet*> mDataModel;
48 class DataContainer {
49 public:
50 DataContainer(QList<qreal> data, int id) : mId(id), mData(data) {}
51 int countColumns() { return mData.count(); }
52 qreal valueAt(int item) { return mData.at(item); }
53
54 int mId; // TODO: Is this needed?
55 private:
56 QList<qreal> mData;
57 };
58
59 // Owned. N series. each has a list of values.
60 QList<DataContainer*> mDataModel;
61 int mRunningId;
62 int mMaxColumns; // longest series in datamodel
63 QBarCategory& mCategory;
44 QBarCategory& mCategory;
64
45
65 };
46 };
@@ -8,6 +8,16 BarChartSeries::BarChartSeries(QBarCategory &category, QObject *parent)
8 {
8 {
9 }
9 }
10
10
11 void BarChartSeries::addBarSet(QBarSet &set)
12 {
13 BarChartSeriesBase::addBarSet(set);
14 }
15
16 void BarChartSeries::removeBarSet(QBarSet &set)
17 {
18 BarChartSeriesBase::removeBarSet(set);
19 }
20
11 #include "moc_barchartseries.cpp"
21 #include "moc_barchartseries.cpp"
12
22
13 QTCOMMERCIALCHART_END_NAMESPACE
23 QTCOMMERCIALCHART_END_NAMESPACE
@@ -19,6 +19,11 public:
19 // from BarChartSeriesBase
19 // from BarChartSeriesBase
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
21
21
22 void addBarSet(QBarSet &set);
23 void removeBarSet(QBarSet &set);
24
25 public Q_SLOTS:
26
22 private:
27 private:
23
28
24 BarGroup* mBarGroup;
29 BarGroup* mBarGroup;
@@ -4,6 +4,7
4 #include "bargroup.h"
4 #include "bargroup.h"
5 #include "barchartmodel_p.h"
5 #include "barchartmodel_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include "qbarcategory.h"
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
@@ -11,21 +12,7 BarChartSeriesBase::BarChartSeriesBase(QBarCategory &category, QObject *parent)
11 : QChartSeries(parent)
12 : QChartSeries(parent)
12 ,mModel(new BarChartModel(category, this))
13 ,mModel(new BarChartModel(category, this))
13 {
14 {
14 }
15 mLabels.append(category.items());
15
16 int BarChartSeriesBase::addData(QList<qreal> data)
17 {
18 return mModel->addData(data);
19 }
20
21 void BarChartSeriesBase::removeData(int id)
22 {
23 mModel->removeData(id);
24 }
25
26 void BarChartSeriesBase::setLabels(QList<QString> labels)
27 {
28 mLabels = labels;
29 }
16 }
30
17
31 void BarChartSeriesBase::addBarSet(QBarSet &set)
18 void BarChartSeriesBase::addBarSet(QBarSet &set)
@@ -50,7 +37,7 qreal BarChartSeriesBase::max()
50
37
51 int BarChartSeriesBase::countColumns()
38 int BarChartSeriesBase::countColumns()
52 {
39 {
53 return mModel->countColumns();
40 return mModel->countCategories();
54 }
41 }
55
42
56 qreal BarChartSeriesBase::valueAt(int series, int item)
43 qreal BarChartSeriesBase::valueAt(int series, int item)
@@ -62,7 +49,7 qreal BarChartSeriesBase::valueAt(int series, int item)
62 qreal BarChartSeriesBase::maxColumnSum()
49 qreal BarChartSeriesBase::maxColumnSum()
63 {
50 {
64 // qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel->maxColumnSum();
51 // qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel->maxColumnSum();
65 return mModel->maxColumnSum();
52 return mModel->maxCategorySum();
66 }
53 }
67
54
68 BarChartModel& BarChartSeriesBase::model()
55 BarChartModel& BarChartSeriesBase::model()
@@ -25,15 +25,12 public:
25 // from QChartSeries
25 // from QChartSeries
26 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
26 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
27
27
28 // TODO: These 3 will be removed.
28 protected:
29 int addData(QList<qreal> data);
29 // Used by derived series
30 void removeData(int id);
30 void addBarSet(QBarSet &set);
31 void setLabels(QList<QString> labels);
31 void removeBarSet(QBarSet &set);
32
33 // TODO: Expose these to user in derived class instead of here? Common implementation for all bar charts, but not visible to user
34 void addBarSet(QBarSet &set); // Bob[1,5,6,2,15,4] first value goes to category 1 etc..
35 void removeBarSet(QBarSet &set); //
36
32
33 public:
37 // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil.
34 // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil.
38 qreal min();
35 qreal min();
39 qreal max();
36 qreal max();
@@ -16,7 +16,7 void BarGroup::layoutChanged()
16 // qDebug() << "BarGroup::layoutChanged";
16 // qDebug() << "BarGroup::layoutChanged";
17 // Scale bars to new layout
17 // Scale bars to new layout
18 // Layout for bars:
18 // Layout for bars:
19 if (mModel.countRows() <= 0) {
19 if (mModel.countSets() <= 0) {
20 return;
20 return;
21 }
21 }
22
22
@@ -27,8 +27,8 void BarGroup::layoutChanged()
27
27
28 // TODO: better way to auto-layout?
28 // TODO: better way to auto-layout?
29 // Use reals for accurancy (we might get some compiler warnings... :)
29 // Use reals for accurancy (we might get some compiler warnings... :)
30 int itemCount = mModel.countColumns();
30 int itemCount = mModel.countCategories();
31 int seriesCount = mModel.countRows();
31 int seriesCount = mModel.countSets();
32
32
33 qreal tW = mWidth;
33 qreal tW = mWidth;
34 qreal tH = mHeight;
34 qreal tH = mHeight;
@@ -80,7 +80,7 void BarGroupBase::dataChanged()
80 }
80 }
81
81
82 // TODO: labels from series. This creates just some example labels
82 // TODO: labels from series. This creates just some example labels
83 int count = mModel.countColumns(); // mSeries.countColumns();
83 int count = mModel.countCategories(); // mSeries.countColumns();
84 for (int i=0; i<count; i++) {
84 for (int i=0; i<count; i++) {
85 BarLabel* label = new BarLabel(this);
85 BarLabel* label = new BarLabel(this);
86 // QString text("Label " + QString::number(i));
86 // QString text("Label " + QString::number(i));
@@ -88,7 +88,7 void BarGroupBase::dataChanged()
88 childItems().append(label);
88 childItems().append(label);
89 }
89 }
90
90
91 count = mModel.countColumns() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns
91 count = mModel.countCategories() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns
92 for (int i=0; i<count; i++) {
92 for (int i=0; i<count; i++) {
93 Separator* sep = new Separator(this);
93 Separator* sep = new Separator(this);
94 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
94 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
@@ -11,6 +11,16 PercentBarChartSeries::PercentBarChartSeries(QBarCategory &category, QObject *pa
11 {
11 {
12 }
12 }
13
13
14 void PercentBarChartSeries::addBarSet(QBarSet &set)
15 {
16 BarChartSeriesBase::addBarSet(set);
17 }
18
19 void PercentBarChartSeries::removeBarSet(QBarSet &set)
20 {
21 BarChartSeriesBase::removeBarSet(set);
22 }
23
14 #include "moc_percentbarchartseries.cpp"
24 #include "moc_percentbarchartseries.cpp"
15
25
16 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
@@ -18,6 +18,9 public:
18 // from BarChartSeriesBase
18 // from BarChartSeriesBase
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
20
20
21 void addBarSet(QBarSet &set);
22 void removeBarSet(QBarSet &set);
23
21 public Q_SLOTS:
24 public Q_SLOTS:
22
25
23 private:
26 private:
@@ -16,7 +16,7 void PercentBarGroup::layoutChanged()
16 {
16 {
17 // Scale bars to new layout
17 // Scale bars to new layout
18 // Layout for bars:
18 // Layout for bars:
19 if (mModel.countRows() <= 0) {
19 if (mModel.countSets() <= 0) {
20 // Nothing to do.
20 // Nothing to do.
21 return;
21 return;
22 }
22 }
@@ -28,20 +28,20 void PercentBarGroup::layoutChanged()
28
28
29 // TODO: better way to auto-layout
29 // TODO: better way to auto-layout
30 // Use reals for accurancy (we might get some compiler warnings... :)
30 // Use reals for accurancy (we might get some compiler warnings... :)
31 int count = mModel.countColumns();
31 int count = mModel.countCategories();
32 int itemIndex(0);
32 int itemIndex(0);
33 qreal tW = mWidth;
33 qreal tW = mWidth;
34 qreal tC = count+1;
34 qreal tC = count+1;
35 qreal xStep = (tW/tC);
35 qreal xStep = (tW/tC);
36 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
36 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
37 int labelIndex = mModel.countColumns() * mModel.countRows();
37 int labelIndex = mModel.countCategories() * mModel.countSets();
38
38
39 for (int column = 0; column < mModel.countColumns(); column++) {
39 for (int column = 0; column < mModel.countCategories(); column++) {
40 qreal colSum = mModel.columnSum(column);
40 qreal colSum = mModel.categorySum(column);
41 qreal h = mHeight;
41 qreal h = mHeight;
42 qreal scale = (h / colSum);
42 qreal scale = (h / colSum);
43 qreal yPos = h;
43 qreal yPos = h;
44 for (int row=0; row < mModel.countRows(); row++) {
44 for (int row=0; row < mModel.countSets(); row++) {
45 qreal barHeight = mModel.valueAt(row, column) * scale;
45 qreal barHeight = mModel.valueAt(row, column) * scale;
46 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
46 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
47
47
@@ -63,7 +63,7 void PercentBarGroup::layoutChanged()
63 // Position separators
63 // Position separators
64 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
64 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
65 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
65 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
66 for (int s=0; s < mModel.countColumns() - 1; s++) {
66 for (int s=0; s < mModel.countCategories() - 1; s++) {
67 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
67 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
68 sep->setPos(xPos,0);
68 sep->setPos(xPos,0);
69 sep->setSize(QSizeF(1,mHeight));
69 sep->setSize(QSizeF(1,mHeight));
@@ -10,6 +10,10 void QBarSet::setName(QString name)
10 {
10 {
11 mName = name;
11 mName = name;
12 }
12 }
13 QString QBarSet::name()
14 {
15 return mName;
16 }
13
17
14 QBarSet& QBarSet::operator << (const qreal &value)
18 QBarSet& QBarSet::operator << (const qreal &value)
15 {
19 {
@@ -17,4 +21,15 QBarSet& QBarSet::operator << (const qreal &value)
17 return *this;
21 return *this;
18 }
22 }
19
23
24 int QBarSet::count()
25 {
26 return mValues.count();
27 }
28
29 qreal QBarSet::valueAt(int index)
30 {
31 return mValues.at(index);
32 }
33
34
20 QTCOMMERCIALCHART_END_NAMESPACE
35 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,13 +11,15 public:
11 QBarSet();
11 QBarSet();
12
12
13 void setName(QString name);
13 void setName(QString name);
14 // void setValues(QList<qreal> &values);
14 QString name();
15
16 // TODO:
17 QBarSet& operator << (const qreal &value);
15 QBarSet& operator << (const qreal &value);
18
16
19 // TODO: Hide these from user of QBarSet. (but data model needs access to these)
17 //TODO: What is the way to set a single value to n:th item? Is there need for such functionality?
20 public:
18
19 int count();
20 qreal valueAt(int index);
21
22 private:
21
23
22 QString mName;
24 QString mName;
23 QList<qreal> mValues;
25 QList<qreal> mValues;
@@ -9,6 +9,16 StackedBarChartSeries::StackedBarChartSeries(QBarCategory &category, QObject *pa
9 {
9 {
10 }
10 }
11
11
12 void StackedBarChartSeries::addBarSet(QBarSet &set)
13 {
14 BarChartSeriesBase::addBarSet(set);
15 }
16
17 void StackedBarChartSeries::removeBarSet(QBarSet &set)
18 {
19 BarChartSeriesBase::removeBarSet(set);
20 }
21
12 #include "moc_stackedbarchartseries.cpp"
22 #include "moc_stackedbarchartseries.cpp"
13
23
14 QTCOMMERCIALCHART_END_NAMESPACE
24 QTCOMMERCIALCHART_END_NAMESPACE
@@ -18,6 +18,9 public:
18 // from QChartSeries
18 // from QChartSeries
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
20
20
21 void addBarSet(QBarSet &set);
22 void removeBarSet(QBarSet &set);
23
21 public Q_SLOTS:
24 public Q_SLOTS:
22
25
23 private:
26 private:
@@ -16,12 +16,12 void StackedBarGroup::layoutChanged()
16 // qDebug() << "StackedBarGroup::layoutChanged";
16 // qDebug() << "StackedBarGroup::layoutChanged";
17 // Scale bars to new layout
17 // Scale bars to new layout
18 // Layout for bars:
18 // Layout for bars:
19 if (mModel.countRows() <= 0) {
19 if (mModel.countSets() <= 0) {
20 // Nothing to do.
20 // Nothing to do.
21 return;
21 return;
22 }
22 }
23
23
24 if (mModel.countColumns() == 0) {
24 if (mModel.countCategories() == 0) {
25 // Nothing to do
25 // Nothing to do
26 return;
26 return;
27 }
27 }
@@ -34,20 +34,20 void StackedBarGroup::layoutChanged()
34 // TODO: better way to auto-layout
34 // TODO: better way to auto-layout
35 // Use reals for accurancy (we might get some compiler warnings... :)
35 // Use reals for accurancy (we might get some compiler warnings... :)
36 // TODO: use temp variable for column count...
36 // TODO: use temp variable for column count...
37 qreal maxSum = mModel.maxColumnSum();
37 qreal maxSum = mModel.maxCategorySum();
38 qreal h = mHeight;
38 qreal h = mHeight;
39 qreal scale = (h / maxSum);
39 qreal scale = (h / maxSum);
40
40
41 int itemIndex(0);
41 int itemIndex(0);
42 qreal tW = mWidth;
42 qreal tW = mWidth;
43 qreal tC = mModel.countColumns() + 1;
43 qreal tC = mModel.countCategories() + 1;
44 qreal xStep = (tW/tC);
44 qreal xStep = (tW/tC);
45 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
45 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
46 int labelIndex = mModel.countRows() * mModel.countColumns();
46 int labelIndex = mModel.countSets() * mModel.countCategories();
47
47
48 for (int column = 0; column < mModel.countColumns(); column++) {
48 for (int column = 0; column < mModel.countCategories(); column++) {
49 qreal yPos = h;
49 qreal yPos = h;
50 for (int row=0; row < mModel.countRows(); row++) {
50 for (int row=0; row < mModel.countSets(); row++) {
51 qreal barHeight = mModel.valueAt(row, column) * scale;
51 qreal barHeight = mModel.valueAt(row, column) * scale;
52 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
52 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
53
53
@@ -69,7 +69,7 void StackedBarGroup::layoutChanged()
69 // Position separators
69 // Position separators
70 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
70 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
71 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
71 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
72 for (int s=0; s < mModel.countColumns() - 1; s++) {
72 for (int s=0; s < mModel.countCategories() - 1; s++) {
73 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
73 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
74 sep->setPos(xPos,0);
74 sep->setPos(xPos,0);
75 sep->setSize(QSizeF(1,mHeight));
75 sep->setSize(QSizeF(1,mHeight));
@@ -3,6 +3,8
3 #include "qchartseries.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include <qlinechartseries.h>
5 #include <qlinechartseries.h>
6 #include <qbarset.h>
7 #include <qbarcategory.h>
6 #include <barchartseries.h>
8 #include <barchartseries.h>
7 #include <stackedbarchartseries.h>
9 #include <stackedbarchartseries.h>
8 #include <percentbarchartseries.h>
10 #include <percentbarchartseries.h>
@@ -178,11 +180,11 void MainWidget::addSeries(QString series, QString data)
178 QList<qreal> x;
180 QList<qreal> x;
179 QList<qreal> y;
181 QList<qreal> y;
180
182
181 QList<qreal> data0;
183 QBarSet set0;
182 QList<qreal> data1;
184 QBarSet set1;
183 QList<qreal> data2;
185 QBarSet set2;
184 QList<qreal> data3;
186 QBarSet set3;
185 QList<qreal> data4;
187 QBarSet set4;
186
188
187 if (data == "linear") {
189 if (data == "linear") {
188 for (int i = 0; i < 20; i++) {
190 for (int i = 0; i < 20; i++) {
@@ -211,11 +213,11 void MainWidget::addSeries(QString series, QString data)
211 }
213 }
212 } else if (data == "Table, 5 series"){
214 } else if (data == "Table, 5 series"){
213 // Create some test data to chart
215 // Create some test data to chart
214 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 5;
216 set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
215 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4;
217 set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
216 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3;
218 set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
217 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2;
219 set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
218 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1;
220 set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
219 } else {
221 } else {
220 // TODO: check if data has a valid file name
222 // TODO: check if data has a valid file name
221 Q_ASSERT(false);
223 Q_ASSERT(false);
@@ -245,41 +247,50 void MainWidget::addSeries(QString series, QString data)
245 newSeries = series0;
247 newSeries = series0;
246 } else if (series == "Bar") {
248 } else if (series == "Bar") {
247 qDebug() << "Bar chart series";
249 qDebug() << "Bar chart series";
248 BarChartSeries* series0 = new BarChartSeries(this);
250
249 series0->addData(data0);
251 QBarCategory category;
250 series0->addData(data1);
252 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
251 series0->addData(data2);
253
252 series0->addData(data3);
254 BarChartSeries* series0 = new BarChartSeries(category, this);
253 series0->addData(data4);
255
254 QList<QString> labels;
256 series0->addBarSet(set0);
255 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
257 series0->addBarSet(set1);
256 series0->setLabels(labels);
258 series0->addBarSet(set2);
259 series0->addBarSet(set3);
260 series0->addBarSet(set4);
261
257 m_chartWidget->addSeries(series0);
262 m_chartWidget->addSeries(series0);
258 newSeries = series0;
263 newSeries = series0;
259 } else if (series == "StackedBar") {
264 } else if (series == "StackedBar") {
260 qDebug() << "Bar chart series";
265 qDebug() << "Stacked bar chart series";
261 StackedBarChartSeries* series0 = new StackedBarChartSeries(this);
266
262 series0->addData(data0);
267 QBarCategory category;
263 series0->addData(data1);
268 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
264 series0->addData(data2);
269
265 series0->addData(data3);
270 StackedBarChartSeries* series0 = new StackedBarChartSeries(category, this);
266 series0->addData(data4);
271
267 QList<QString> labels;
272 series0->addBarSet(set0);
268 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
273 series0->addBarSet(set1);
269 series0->setLabels(labels);
274 series0->addBarSet(set2);
275 series0->addBarSet(set3);
276 series0->addBarSet(set4);
277
270 m_chartWidget->addSeries(series0);
278 m_chartWidget->addSeries(series0);
271 newSeries = series0;
279 newSeries = series0;
272 } else if (series == "PercentBar") {
280 } else if (series == "PercentBar") {
273 qDebug() << "Bar chart series";
281 qDebug() << "Percent bar chart series";
274 PercentBarChartSeries* series0 = new PercentBarChartSeries(this);
282
275 series0->addData(data0);
283 QBarCategory category;
276 series0->addData(data1);
284 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
277 series0->addData(data2);
285
278 series0->addData(data3);
286 PercentBarChartSeries* series0 = new PercentBarChartSeries(category, this);
279 series0->addData(data4);
287
280 QList<QString> labels;
288 series0->addBarSet(set0);
281 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
289 series0->addBarSet(set1);
282 series0->setLabels(labels);
290 series0->addBarSet(set2);
291 series0->addBarSet(set3);
292 series0->addBarSet(set4);
293
283 m_chartWidget->addSeries(series0);
294 m_chartWidget->addSeries(series0);
284 newSeries = series0;
295 newSeries = series0;
285 } else {
296 } else {
General Comments 0
You need to be logged in to leave comments. Login now