##// END OF EJS Templates
bug fix in bar charts. Crashed, if layout was set before data. Also integrated to test app.
sauimone -
r165:2ff4f264aa68
parent child
Show More
@@ -13,7 +13,7 BarChartModel::BarChartModel(QObject *parent) :
13
13
14 BarChartModel::~BarChartModel()
14 BarChartModel::~BarChartModel()
15 {
15 {
16 qDebug() << "BarChartModel::~BarChartModel";
16 // qDebug() << "BarChartModel::~BarChartModel";
17 foreach (DataContainer* c, mDataModel) {
17 foreach (DataContainer* c, mDataModel) {
18 delete c;
18 delete c;
19 }
19 }
@@ -21,7 +21,7 BarChartModel::~BarChartModel()
21
21
22 int BarChartModel::addData(QList<qreal> data)
22 int BarChartModel::addData(QList<qreal> data)
23 {
23 {
24 qDebug() << "BarChartModel::addData" << data.count();
24 // qDebug() << "BarChartModel::addData" << data.count();
25 DataContainer* c = new DataContainer(data,mRunningId);
25 DataContainer* c = new DataContainer(data,mRunningId);
26 mDataModel.append(c);
26 mDataModel.append(c);
27 mRunningId++;
27 mRunningId++;
@@ -30,7 +30,7 int BarChartModel::addData(QList<qreal> data)
30
30
31 void BarChartModel::removeData(int id)
31 void BarChartModel::removeData(int id)
32 {
32 {
33 qDebug() << "BarChartModel::removeData";
33 // qDebug() << "BarChartModel::removeData";
34 foreach(DataContainer* c, mDataModel) {
34 foreach(DataContainer* c, mDataModel) {
35 if (c->mId == id) {
35 if (c->mId == id) {
36 mDataModel.removeOne(c);
36 mDataModel.removeOne(c);
@@ -41,13 +41,13 void BarChartModel::removeData(int id)
41
41
42 int BarChartModel::countRows()
42 int BarChartModel::countRows()
43 {
43 {
44 qDebug() << "BarChartModel::countRows";
44 // qDebug() << "BarChartModel::countRows";
45 return mDataModel.count();
45 return mDataModel.count();
46 }
46 }
47
47
48 int BarChartModel::countColumns()
48 int BarChartModel::countColumns()
49 {
49 {
50 qDebug() << "BarChartModel::countColumns";
50 // qDebug() << "BarChartModel::countColumns";
51 int count(0);
51 int count(0);
52 for (int i=0; i<mDataModel.count(); i++){
52 for (int i=0; i<mDataModel.count(); i++){
53 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
53 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
@@ -61,16 +61,14 int BarChartModel::countColumns()
61
61
62 int BarChartModel::countTotalItems()
62 int BarChartModel::countTotalItems()
63 {
63 {
64 qDebug() << "BarChartModel::countTotalItems";
64 // qDebug() << "BarChartModel::countTotalItems";
65 int total = mDataModel.count() * countColumns();
65 int total = mDataModel.count() * countColumns();
66 qDebug() << "BarChartModel::countTotalItems datamodel count" << mDataModel.count();
67 qDebug() << "BarChartModel::countTotalItems countColumns count" << countColumns();
68 return total;
66 return total;
69 }
67 }
70
68
71 int BarChartModel::min()
69 int BarChartModel::min()
72 {
70 {
73 qDebug() << "BarChartModel::min";
71 // qDebug() << "BarChartModel::min";
74 Q_ASSERT(mDataModel.count() > 0);
72 Q_ASSERT(mDataModel.count() > 0);
75 // TODO: make min and max members and update them when data changes.
73 // TODO: make min and max members and update them when data changes.
76 // This is slower since they are checked every time, even if data is same since previous call.
74 // This is slower since they are checked every time, even if data is same since previous call.
@@ -90,7 +88,7 int BarChartModel::min()
90
88
91 int BarChartModel::max()
89 int BarChartModel::max()
92 {
90 {
93 qDebug() << "BarChartModel::max";
91 // qDebug() << "BarChartModel::max";
94 Q_ASSERT(mDataModel.count() > 0);
92 Q_ASSERT(mDataModel.count() > 0);
95
93
96 // TODO: make min and max members and update them when data changes.
94 // TODO: make min and max members and update them when data changes.
@@ -112,7 +110,7 int BarChartModel::max()
112
110
113 qreal BarChartModel::valueAt(int series, int item)
111 qreal BarChartModel::valueAt(int series, int item)
114 {
112 {
115 qDebug() << "BarChartModel::valueAt" << series << item;
113 // qDebug() << "BarChartModel::valueAt" << series << item;
116 if ((series < 0) || (series >= mDataModel.count())) {
114 if ((series < 0) || (series >= mDataModel.count())) {
117 // No series, no value.
115 // No series, no value.
118 return 0;
116 return 0;
@@ -121,13 +119,13 qreal BarChartModel::valueAt(int series, int item)
121 return 0;
119 return 0;
122 }
120 }
123
121
124 qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item);
122 // qDebug() << "ValueAt" << series << item << "=" << mDataModel.at(series)->valueAt(item);
125 return mDataModel.at(series)->valueAt(item);
123 return mDataModel.at(series)->valueAt(item);
126 }
124 }
127
125
128 qreal BarChartModel::columnSum(int column)
126 qreal BarChartModel::columnSum(int column)
129 {
127 {
130 qDebug() << "BarChartModel::columnSum";
128 // qDebug() << "BarChartModel::columnSum";
131 int sum(0);
129 int sum(0);
132 int count = mDataModel.count(); // Count rows
130 int count = mDataModel.count(); // Count rows
133
131
@@ -141,7 +139,7 qreal BarChartModel::columnSum(int column)
141
139
142 qreal BarChartModel::maxColumnSum()
140 qreal BarChartModel::maxColumnSum()
143 {
141 {
144 qDebug() << "BarChartModel::maxColumnSum";
142 // qDebug() << "BarChartModel::maxColumnSum";
145 int max = INT_MIN;
143 int max = INT_MIN;
146 int count = countColumns();
144 int count = countColumns();
147
145
@@ -20,6 +20,11 void BarGroup::layoutChanged()
20 return;
20 return;
21 }
21 }
22
22
23 if (childItems().count() == 0) {
24 qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!";
25 return;
26 }
27
23 // TODO: better way to auto-layout?
28 // TODO: better way to auto-layout?
24 // Use reals for accurancy (we might get some compiler warnings... :)
29 // Use reals for accurancy (we might get some compiler warnings... :)
25 int itemCount = mModel.countColumns();
30 int itemCount = mModel.countColumns();
@@ -25,7 +25,7 void BarGroupBase::setSeparatorsVisible(bool visible)
25
25
26 void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
26 void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
27 {
27 {
28 qDebug() << "BarGroupBase::paint" << childItems().count();
28 // qDebug() << "BarGroupBase::paint" << childItems().count();
29 if (!mLayoutSet) {
29 if (!mLayoutSet) {
30 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
30 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
31 return;
31 return;
@@ -42,13 +42,7 QRectF BarGroupBase::boundingRect() const
42 {
42 {
43 return QRectF(0,0,mWidth,mHeight);
43 return QRectF(0,0,mWidth,mHeight);
44 }
44 }
45 /*
45
46 void BarGroupBase::themeChanged(ChartTheme *theme)
47 {
48 qDebug() << "BarGroupBase::themeChanged"
49 // mTheme = theme;
50 }
51 */
52 void BarGroupBase::setBarWidth( int w )
46 void BarGroupBase::setBarWidth( int w )
53 {
47 {
54 mBarDefaultWidth = w;
48 mBarDefaultWidth = w;
@@ -56,7 +50,7 void BarGroupBase::setBarWidth( int w )
56
50
57 int BarGroupBase::addColor( QColor color )
51 int BarGroupBase::addColor( QColor color )
58 {
52 {
59 qDebug() << "BarGroupBase::addColor";
53 // qDebug() << "BarGroupBase::addColor";
60 int colorIndex = mColors.count();
54 int colorIndex = mColors.count();
61 mColors.append(color);
55 mColors.append(color);
62 return colorIndex;
56 return colorIndex;
@@ -64,13 +58,13 int BarGroupBase::addColor( QColor color )
64
58
65 void BarGroupBase::resetColors()
59 void BarGroupBase::resetColors()
66 {
60 {
67 qDebug() << "BarGroupBase::resetColors";
61 // qDebug() << "BarGroupBase::resetColors";
68 mColors.clear();
62 mColors.clear();
69 }
63 }
70
64
71 void BarGroupBase::dataChanged()
65 void BarGroupBase::dataChanged()
72 {
66 {
73 qDebug() << "BarGroupBase::dataChanged";
67 // qDebug() << "BarGroupBase::dataChanged";
74
68
75 // Delete old bars
69 // Delete old bars
76 foreach (QGraphicsItem* item, childItems()) {
70 foreach (QGraphicsItem* item, childItems()) {
@@ -108,19 +102,19 void BarGroupBase::dataChanged()
108
102
109 void BarGroupBase::handleModelChanged(int index)
103 void BarGroupBase::handleModelChanged(int index)
110 {
104 {
111 qDebug() << "BarGroupBase::handleModelChanged" << index;
105 // qDebug() << "BarGroupBase::handleModelChanged" << index;
112 dataChanged();
106 dataChanged();
113 }
107 }
114
108
115 void BarGroupBase::handleDomainChanged(const Domain& domain)
109 void BarGroupBase::handleDomainChanged(const Domain& domain)
116 {
110 {
117 qDebug() << "BarGroupBase::handleDomainChanged";
111 // qDebug() << "BarGroupBase::handleDomainChanged";
118 dataChanged();
112 dataChanged();
119 }
113 }
120
114
121 void BarGroupBase::handleGeometryChanged(const QRectF& rect)
115 void BarGroupBase::handleGeometryChanged(const QRectF& rect)
122 {
116 {
123 qDebug() << "BarGroupBase::handleGeometryChanged";
117 // qDebug() << "BarGroupBase::handleGeometryChanged";
124 mWidth = rect.width();
118 mWidth = rect.width();
125 mHeight = rect.height();
119 mHeight = rect.height();
126 layoutChanged();
120 layoutChanged();
@@ -21,6 +21,11 void PercentBarGroup::layoutChanged()
21 return;
21 return;
22 }
22 }
23
23
24 if (childItems().count() == 0) {
25 qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!";
26 return;
27 }
28
24 // TODO: better way to auto-layout
29 // TODO: better way to auto-layout
25 // Use reals for accurancy (we might get some compiler warnings... :)
30 // Use reals for accurancy (we might get some compiler warnings... :)
26 int count = mModel.countColumns();
31 int count = mModel.countColumns();
@@ -28,7 +33,6 void PercentBarGroup::layoutChanged()
28 qreal tW = mWidth;
33 qreal tW = mWidth;
29 qreal tC = count+1;
34 qreal tC = count+1;
30 qreal xStep = (tW/tC);
35 qreal xStep = (tW/tC);
31 // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
32 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
36 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
33 int labelIndex = mModel.countColumns() * mModel.countRows();
37 int labelIndex = mModel.countColumns() * mModel.countRows();
34
38
@@ -26,6 +26,11 void StackedBarGroup::layoutChanged()
26 return;
26 return;
27 }
27 }
28
28
29 if (childItems().count() == 0) {
30 qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!";
31 return;
32 }
33
29 // TODO: better way to auto-layout
34 // TODO: better way to auto-layout
30 // Use reals for accurancy (we might get some compiler warnings... :)
35 // Use reals for accurancy (we might get some compiler warnings... :)
31 // TODO: use temp variable for column count...
36 // TODO: use temp variable for column count...
@@ -14,6 +14,8 DataSerieDialog::DataSerieDialog(QString defaultType, QWidget *parent) :
14 m_seriesTypeCombo->addItem("Line");
14 m_seriesTypeCombo->addItem("Line");
15 m_seriesTypeCombo->addItem("Area");
15 m_seriesTypeCombo->addItem("Area");
16 m_seriesTypeCombo->addItem("Bar");
16 m_seriesTypeCombo->addItem("Bar");
17 m_seriesTypeCombo->addItem("StackedBar");
18 m_seriesTypeCombo->addItem("PercentBar");
17 m_seriesTypeCombo->addItem("Pie");
19 m_seriesTypeCombo->addItem("Pie");
18 m_seriesTypeCombo->addItem("Scatter");
20 m_seriesTypeCombo->addItem("Scatter");
19 m_seriesTypeCombo->addItem("Spline");
21 m_seriesTypeCombo->addItem("Spline");
@@ -31,6 +33,7 DataSerieDialog::DataSerieDialog(QString defaultType, QWidget *parent) :
31 m_testDataCombo->addItem("linear, 1M");
33 m_testDataCombo->addItem("linear, 1M");
32 m_testDataCombo->addItem("SIN");
34 m_testDataCombo->addItem("SIN");
33 m_testDataCombo->addItem("SIN + random");
35 m_testDataCombo->addItem("SIN + random");
36 m_testDataCombo->addItem("Table, 5 series");
34 m_testDataCombo->addItem("TODO From file...");
37 m_testDataCombo->addItem("TODO From file...");
35 m_testDataCombo->addItem("TODO From URL...");
38 m_testDataCombo->addItem("TODO From URL...");
36
39
@@ -5,6 +5,7
5 #include <qlinechartseries.h>
5 #include <qlinechartseries.h>
6 #include <barchartseries.h>
6 #include <barchartseries.h>
7 #include <stackedbarchartseries.h>
7 #include <stackedbarchartseries.h>
8 #include <percentbarchartseries.h>
8 #include <QPushButton>
9 #include <QPushButton>
9 #include <QComboBox>
10 #include <QComboBox>
10 #include <QSpinBox>
11 #include <QSpinBox>
@@ -177,6 +178,12 void MainWidget::addSeries(QString series, QString data)
177 QList<qreal> x;
178 QList<qreal> x;
178 QList<qreal> y;
179 QList<qreal> y;
179
180
181 QList<qreal> data0;
182 QList<qreal> data1;
183 QList<qreal> data2;
184 QList<qreal> data3;
185 QList<qreal> data4;
186
180 if (data == "linear") {
187 if (data == "linear") {
181 for (int i = 0; i < 20; i++) {
188 for (int i = 0; i < 20; i++) {
182 x.append(i);
189 x.append(i);
@@ -202,6 +209,13 void MainWidget::addSeries(QString series, QString data)
202 x.append(i + (rand() % 5));
209 x.append(i + (rand() % 5));
203 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
210 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
204 }
211 }
212 } else if (data == "Table, 5 series"){
213 // Create some test data to chart
214 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
215 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
216 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
217 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
218 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
205 } else {
219 } else {
206 // TODO: check if data has a valid file name
220 // TODO: check if data has a valid file name
207 Q_ASSERT(false);
221 Q_ASSERT(false);
@@ -229,36 +243,38 void MainWidget::addSeries(QString series, QString data)
229 series0->add(x.at(i), y.at(i));
243 series0->add(x.at(i), y.at(i));
230 m_chartWidget->addSeries(series0);
244 m_chartWidget->addSeries(series0);
231 newSeries = series0;
245 newSeries = series0;
232 } else {
246 } else if (series == "Bar") {
233 // TODO
247 qDebug() << "Bar chart series";
234 }
248 BarChartSeries* series0 = new BarChartSeries(this);
235
249 series0->addData(data0);
236 // BarChart
250 series0->addData(data1);
237 if (series == "Bar") {
251 series0->addData(data2);
238 // This is the another way of creating series. Should we create test cases for both ways, if we support them?
252 series0->addData(data3);
253 series0->addData(data4);
254 m_chartWidget->addSeries(series0);
255 newSeries = series0;
256 } else if (series == "StackedBar") {
239 qDebug() << "Bar chart series";
257 qDebug() << "Bar chart series";
240 StackedBarChartSeries* series0 = new StackedBarChartSeries(this);
258 StackedBarChartSeries* series0 = new StackedBarChartSeries(this);
241
242 // Create some test data to chart
243 QList<qreal> data0;
244 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
245 QList<qreal> data1;
246 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
247 QList<qreal> data2;
248 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
249 QList<qreal> data3;
250 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
251 QList<qreal> data4;
252 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
253
254 series0->addData(data0);
259 series0->addData(data0);
255 series0->addData(data1);
260 series0->addData(data1);
256 series0->addData(data2);
261 series0->addData(data2);
257 series0->addData(data3);
262 series0->addData(data3);
258 series0->addData(data4);
263 series0->addData(data4);
259
260 m_chartWidget->addSeries(series0);
264 m_chartWidget->addSeries(series0);
261 newSeries = series0;
265 newSeries = series0;
266 } else if (series == "PercentBar") {
267 qDebug() << "Bar chart series";
268 PercentBarChartSeries* series0 = new PercentBarChartSeries(this);
269 series0->addData(data0);
270 series0->addData(data1);
271 series0->addData(data2);
272 series0->addData(data3);
273 series0->addData(data4);
274 m_chartWidget->addSeries(series0);
275 newSeries = series0;
276 } else {
277 qDebug() << "Something weird going on in MainWidget::addSeries";
262 }
278 }
263
279
264 setCurrentSeries(newSeries);
280 setCurrentSeries(newSeries);
@@ -277,6 +293,12 void MainWidget::setCurrentSeries(QChartSeries *series)
277 case QChartSeries::SeriesTypeBar:
293 case QChartSeries::SeriesTypeBar:
278 qDebug() << "setCurrentSeries (bar)";
294 qDebug() << "setCurrentSeries (bar)";
279 break;
295 break;
296 case QChartSeries::SeriesTypeStackedBar:
297 qDebug() << "setCurrentSeries (Stackedbar)";
298 break;
299 case QChartSeries::SeriesTypePercentBar:
300 qDebug() << "setCurrentSeries (Percentbar)";
301 break;
280 default:
302 default:
281 Q_ASSERT(false);
303 Q_ASSERT(false);
282 break;
304 break;
General Comments 0
You need to be logged in to leave comments. Login now