##// END OF EJS Templates
percent barchart layout fix. signal fix
sauimone -
r850:ce48b0dbab03
parent child
Show More
@@ -41,7 +41,7 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
41 {
41 {
42 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
42 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
43 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
43 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
44 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
44 connect(series, SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
45 setZValue(ChartPresenter::BarSeriesZValue);
45 setZValue(ChartPresenter::BarSeriesZValue);
46 dataChanged();
46 dataChanged();
47 }
47 }
@@ -112,28 +112,22 QVector<QRectF> BarChartItem::calculateLayout()
112 {
112 {
113 QVector<QRectF> layout;
113 QVector<QRectF> layout;
114
114
115 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
115 // Use temporary qreals for accurancy
116 qreal categoryCount = m_series->categoryCount();
116 qreal categoryCount = m_series->categoryCount();
117 qreal setCount = m_series->barsetCount();
117 qreal setCount = m_series->barsetCount();
118
118
119 // Domain:
119 qreal width = geometry().width();
120 qreal width = geometry().width();
120 qreal height = geometry().height();
121 qreal height = geometry().height();
121
122 qreal range = m_domainMaxY - m_domainMinY;
122 qreal max = m_series->max();
123 qreal scale = (height / range);
123
124 // Domain:
125 if (m_domainMaxY > max) {
126 max = m_domainMaxY;
127 }
128
129 qreal scale = (height / max);
130 qreal categoryWidth = width / categoryCount;
124 qreal categoryWidth = width / categoryCount;
131 qreal barWidth = categoryWidth / (setCount+1);
125 qreal barWidth = categoryWidth / (setCount+1);
132
126
133 int itemIndex(0);
127 int itemIndex(0);
134 for (int category = 0; category < categoryCount; category++) {
128 for (int category = 0; category < categoryCount; category++) {
135 qreal xPos = categoryWidth * category + barWidth / 2;
129 qreal xPos = categoryWidth * category + barWidth / 2;
136 qreal yPos = height;
130 qreal yPos = height + scale * m_domainMinY;
137 for (int set = 0; set < setCount; set++) {
131 for (int set = 0; set < setCount; set++) {
138 QBarSet* barSet = m_series->barsetAt(set);
132 QBarSet* barSet = m_series->barsetAt(set);
139
133
@@ -209,6 +203,7 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal
209
203
210 void BarChartItem::handleGeometryChanged(const QRectF &rect)
204 void BarChartItem::handleGeometryChanged(const QRectF &rect)
211 {
205 {
206 prepareGeometryChange();
212 m_clipRect = rect.translated(-rect.topLeft());
207 m_clipRect = rect.translated(-rect.topLeft());
213 m_rect = rect;
208 m_rect = rect;
214 handleLayoutChanged();
209 handleLayoutChanged();
@@ -218,6 +213,10 void BarChartItem::handleGeometryChanged(const QRectF &rect)
218
213
219 void BarChartItem::handleLayoutChanged()
214 void BarChartItem::handleLayoutChanged()
220 {
215 {
216 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
217 // rect size zero.
218 return;
219 }
221 QVector<QRectF> layout = calculateLayout();
220 QVector<QRectF> layout = calculateLayout();
222 applyLayout(layout);
221 applyLayout(layout);
223 update();
222 update();
@@ -152,7 +152,6 qreal BarChartModel::percentageAt(int set, int category) const
152 return value / total;
152 return value / total;
153 }
153 }
154
154
155
156 qreal BarChartModel::categorySum(int category) const
155 qreal BarChartModel::categorySum(int category) const
157 {
156 {
158 qreal sum(0);
157 qreal sum(0);
@@ -165,6 +164,18 qreal BarChartModel::categorySum(int category) const
165 return sum;
164 return sum;
166 }
165 }
167
166
167 qreal BarChartModel::absoluteCategorySum(int category) const
168 {
169 qreal sum(0);
170 int count = m_dataModel.count(); // Count sets
171
172 for (int set = 0; set < count; set++) {
173 if (category < m_dataModel.at(set)->count())
174 sum += qAbs(m_dataModel.at(set)->valueAt(category));
175 }
176 return sum;
177 }
178
168 qreal BarChartModel::maxCategorySum() const
179 qreal BarChartModel::maxCategorySum() const
169 {
180 {
170 qreal max = INT_MIN;
181 qreal max = INT_MIN;
@@ -57,6 +57,7 public:
57 qreal percentageAt(int set, int category) const;
57 qreal percentageAt(int set, int category) const;
58
58
59 qreal categorySum(int category) const;
59 qreal categorySum(int category) const;
60 qreal absoluteCategorySum(int category) const;
60 qreal maxCategorySum() const; // returns maximum sum of sets in all categories.
61 qreal maxCategorySum() const; // returns maximum sum of sets in all categories.
61
62
62 QString categoryName(int category);
63 QString categoryName(int category);
@@ -22,6 +22,7
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
25
26
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
28
@@ -43,14 +44,17 QVector<QRectF> PercentBarChartItem::calculateLayout()
43 qreal xStep = width / categoryCount;
44 qreal xStep = width / categoryCount;
44 qreal xPos = xStep / 2 - barWidth / 2;
45 qreal xPos = xStep / 2 - barWidth / 2;
45
46
47 qreal range = m_domainMaxY - m_domainMinY;
48 qreal domainScale = (height / range);
49
46 int itemIndex(0);
50 int itemIndex(0);
47 for (int category = 0; category < categoryCount; category++) {
51 for (int category = 0; category < categoryCount; category++) {
48 qreal colSum = m_series->categorySum(category);
52 qreal colSum = m_series->categorySum(category);
49 qreal scale = (height / colSum);
53 qreal percentage = (100 / colSum);
50 qreal yPos = height;
54 qreal yPos = height + domainScale * m_domainMinY;
51 for (int set=0; set < m_series->barsetCount(); set++) {
55 for (int set=0; set < m_series->barsetCount(); set++) {
52 QBarSet* barSet = m_series->barsetAt(set);
56 QBarSet* barSet = m_series->barsetAt(set);
53 qreal barHeight = barSet->valueAt(category) * scale;
57 qreal barHeight = barSet->valueAt(category) * percentage * domainScale;
54 Bar* bar = m_bars.at(itemIndex);
58 Bar* bar = m_bars.at(itemIndex);
55 bar->setPen(barSet->pen());
59 bar->setPen(barSet->pen());
56 bar->setBrush(barSet->brush());
60 bar->setBrush(barSet->brush());
@@ -75,7 +75,7 void QBarSeries::appendBarSet(QBarSet *set)
75 m_internalModel->appendBarSet(set);
75 m_internalModel->appendBarSet(set);
76 connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
76 connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
77 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
77 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
78 emit updatedBars();
78 emit restructuredBars();
79 }
79 }
80
80
81 /*!
81 /*!
@@ -87,7 +87,36 void QBarSeries::removeBarSet(QBarSet *set)
87 {
87 {
88 disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
88 disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
89 m_internalModel->removeBarSet(set);
89 m_internalModel->removeBarSet(set);
90 emit updatedBars();
90 emit restructuredBars();
91 }
92
93 /*!
94 Adds a list of barsets to series. Takes ownership of \a sets.
95 Connects the clicked(QString, Qt::MouseButtons) signals
96 of \a sets to this series
97 */
98 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
99 {
100 foreach (QBarSet* barset, sets) {
101 m_internalModel->appendBarSet(barset);
102 connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
103 connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
104 }
105 emit restructuredBars();
106 }
107
108 /*!
109 Removes a list of barsets from series. Releases ownership of \a set. Doesnt delete \a sets.
110 Disconnects the clicked(QString, Qt::MouseButtons) signal
111 of \a sets from this series
112 */
113 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
114 {
115 foreach (QBarSet* barset, sets) {
116 disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
117 m_internalModel->removeBarSet(barset);
118 }
119 emit restructuredBars();
91 }
120 }
92
121
93 void QBarSeries::insertBarSet(int i, QBarSet *set)
122 void QBarSeries::insertBarSet(int i, QBarSet *set)
@@ -220,6 +249,14 qreal QBarSeries::categorySum(int category)
220 }
249 }
221
250
222 /*!
251 /*!
252 \internal \a category
253 */
254 qreal QBarSeries::absoluteCategorySum(int category)
255 {
256 return m_internalModel->absoluteCategorySum(category);
257 }
258
259 /*!
223 \internal
260 \internal
224 */
261 */
225 qreal QBarSeries::maxCategorySum()
262 qreal QBarSeries::maxCategorySum()
@@ -360,7 +397,7 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
360 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
397 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
361 }
398 }
362 }
399 }
363 emit restructuredBar(1);
400 emit restructuredBars();
364 }
401 }
365
402
366 void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end)
403 void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end)
@@ -373,7 +410,7 void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end)
373 {
410 {
374 barsetAt(i)->removeValue(start - m_mapFirst);
411 barsetAt(i)->removeValue(start - m_mapFirst);
375 }
412 }
376 emit restructuredBar(1);
413 emit restructuredBars();
377 }
414 }
378
415
379 void QBarSeries::barsetChanged()
416 void QBarSeries::barsetChanged()
@@ -41,8 +41,10 public:
41
41
42 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
42 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
43
43
44 void appendBarSet(QBarSet *set); // Takes ownership of set
44 void appendBarSet(QBarSet *set); // Takes ownership of set
45 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
45 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
46 void appendBarSets(QList<QBarSet* > sets);
47 void removeBarSets(QList<QBarSet* > sets);
46 void insertBarSet(int i, QBarSet *set);
48 void insertBarSet(int i, QBarSet *set);
47 void insertCategory(int i, QString category);
49 void insertCategory(int i, QString category);
48 void removeCategory(int i);
50 void removeCategory(int i);
@@ -69,6 +71,7 public:
69 qreal valueAt(int set, int category);
71 qreal valueAt(int set, int category);
70 qreal percentageAt(int set, int category);
72 qreal percentageAt(int set, int category);
71 qreal categorySum(int category);
73 qreal categorySum(int category);
74 qreal absoluteCategorySum(int category);
72 qreal maxCategorySum();
75 qreal maxCategorySum();
73 BarChartModel& model();
76 BarChartModel& model();
74 // <--- TO PIMPL
77 // <--- TO PIMPL
@@ -78,7 +81,7 Q_SIGNALS:
78
81
79 //
82 //
80 void updatedBars();
83 void updatedBars();
81 void restructuredBar(int);
84 void restructuredBars();
82
85
83 // TODO: internal signals, these to private implementation.
86 // TODO: internal signals, these to private implementation.
84 // TODO: TO PIMPL --->
87 // TODO: TO PIMPL --->
General Comments 0
You need to be logged in to leave comments. Login now