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