@@ -76,29 +76,31 void Axis::updateLayout(QVector<qreal>& layout) | |||
|
76 | 76 | else setLayout(layout); |
|
77 | 77 | } |
|
78 | 78 | |
|
79 |
|
|
|
79 | bool Axis::createLabels(QStringList& labels,qreal min, qreal max,int ticks) const | |
|
80 | 80 | { |
|
81 | 81 | Q_ASSERT(max>=min); |
|
82 | 82 | Q_ASSERT(ticks>1); |
|
83 | 83 | |
|
84 | QStringList labels; | |
|
85 | ||
|
86 | int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); | |
|
87 | ||
|
88 | 84 | QChartAxisCategories* categories = m_chartAxis->categories(); |
|
89 | 85 | |
|
90 | for(int i=0; i< ticks; i++) { | |
|
91 | qreal value = min + (i * (max - min)/ (ticks-1)); | |
|
92 |
|
|
|
86 | bool category = categories->count()>0; | |
|
87 | ||
|
88 | if(!category) { | |
|
89 | int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); | |
|
90 | for(int i=0; i< ticks; i++) { | |
|
91 | qreal value = min + (i * (max - min)/ (ticks-1)); | |
|
93 | 92 | labels << QString::number(value,'f',n); |
|
94 | 93 | } |
|
95 | else { | |
|
96 | ||
|
94 | } | |
|
95 | else { | |
|
96 | for(int i=0; i< ticks; i++) { | |
|
97 | int value = ceil(min + (i * (max - min)/ (ticks-1))); | |
|
97 | 98 | QString label = categories->label(value); |
|
98 | 99 | labels << label; |
|
99 | 100 | } |
|
100 | 101 | } |
|
101 | return labels; | |
|
102 | ||
|
103 | return category; | |
|
102 | 104 | } |
|
103 | 105 | |
|
104 | 106 | void Axis::setAxisOpacity(qreal opacity) |
@@ -244,7 +246,9 void Axis::setLayout(QVector<qreal>& layout) | |||
|
244 | 246 | |
|
245 | 247 | if(diff!=0) handleAxisUpdated(); |
|
246 | 248 | |
|
247 | QStringList ticksList = createLabels(layout.size(),m_min,m_max); | |
|
249 | QStringList ticksList; | |
|
250 | ||
|
251 | bool categories = createLabels(ticksList,m_min,m_max,layout.size()); | |
|
248 | 252 | |
|
249 | 253 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
250 | 254 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
@@ -265,10 +269,19 void Axis::setLayout(QVector<qreal>& layout) | |||
|
265 | 269 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
266 | 270 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
267 | 271 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
268 | labelItem->setText(ticksList.at(i)); | |
|
269 | QPointF center = labelItem->boundingRect().center(); | |
|
270 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
271 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); | |
|
272 | ||
|
273 | if(!categories){ | |
|
274 | labelItem->setText(ticksList.at(i)); | |
|
275 | QPointF center = labelItem->boundingRect().center(); | |
|
276 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
277 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); | |
|
278 | }else if(i>0){ | |
|
279 | labelItem->setText(ticksList.at(i)); | |
|
280 | QPointF center = labelItem->boundingRect().center(); | |
|
281 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
|
282 | labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); | |
|
283 | } | |
|
284 | ||
|
272 | 285 | if((i+1)%2 && i>1) { |
|
273 | 286 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
274 | 287 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); |
@@ -65,7 +65,7 private: | |||
|
65 | 65 | void updateLayout(QVector<qreal>& layout); |
|
66 | 66 | void setLayout(QVector<qreal>& layout); |
|
67 | 67 | |
|
68 |
|
|
|
68 | bool createLabels(QStringList& labels,qreal min, qreal max,int ticks) const; | |
|
69 | 69 | void axisSelected(); |
|
70 | 70 | |
|
71 | 71 | private: |
@@ -13,6 +13,16 QChartAxisCategories::~QChartAxisCategories() | |||
|
13 | 13 | // TODO Auto-generated destructor stub |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | void QChartAxisCategories::insert(QBarCategory& category) | |
|
17 | { | |
|
18 | int i=1; | |
|
19 | foreach(QString string , category){ | |
|
20 | m_map.insert(i,string); | |
|
21 | i++; | |
|
22 | } | |
|
23 | emit updated(); | |
|
24 | } | |
|
25 | ||
|
16 | 26 | void QChartAxisCategories::insert(qreal value,QString label) |
|
17 | 27 | { |
|
18 | 28 | m_map.insert(value,label); |
@@ -1,6 +1,8 | |||
|
1 | 1 | #ifndef QCHARTAXISCATEGORIES_H_ |
|
2 | 2 | #define QCHARTAXISCATEGORIES_H_ |
|
3 | ||
|
3 | 4 | #include <qchartglobal.h> |
|
5 | #include <qbarseries.h> | |
|
4 | 6 | |
|
5 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 8 | |
@@ -12,6 +14,7 private: | |||
|
12 | 14 | public: |
|
13 | 15 | ~QChartAxisCategories(); |
|
14 | 16 | |
|
17 | void insert(QBarCategory& category); | |
|
15 | 18 | void insert(qreal value,QString label); |
|
16 | 19 | void remove(qreal value); |
|
17 | 20 | QString label(qreal value) const; |
@@ -24,7 +24,6 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : | |||
|
24 | 24 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); |
|
25 | 25 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); |
|
26 | 26 | setZValue(ChartPresenter::BarSeriesZValue); |
|
27 | initAxisLabels(); | |
|
28 | 27 | dataChanged(); |
|
29 | 28 | } |
|
30 | 29 | |
@@ -174,21 +173,6 void BarChartItem::setLayout(const QVector<QRectF> &layout) | |||
|
174 | 173 | update(); |
|
175 | 174 | } |
|
176 | 175 | |
|
177 | void BarChartItem::initAxisLabels() | |
|
178 | { | |
|
179 | int count = mSeries->categoryCount(); | |
|
180 | if (0 == count) { | |
|
181 | return; | |
|
182 | } | |
|
183 | ||
|
184 | Domain* domain = presenter()->dataSet()->domain(mSeries); | |
|
185 | ||
|
186 | qreal min = 0; | |
|
187 | qreal max = count+1; | |
|
188 | ||
|
189 | domain->setRangeX(min,max,count+1); | |
|
190 | } | |
|
191 | ||
|
192 | 176 | //handlers |
|
193 | 177 | |
|
194 | 178 | void BarChartItem::handleModelChanged(int index) |
@@ -204,24 +188,6 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal | |||
|
204 | 188 | mDomainMinY = minY; |
|
205 | 189 | mDomainMaxY = maxY; |
|
206 | 190 | handleLayoutChanged(); |
|
207 | ||
|
208 | /* | |
|
209 | int count = mSeries->categoryCount(); | |
|
210 | if (0 == count) { | |
|
211 | return; | |
|
212 | } | |
|
213 | ||
|
214 | // Position labels to domain | |
|
215 | qreal min = domain.minX(); | |
|
216 | qreal max = domain.maxX(); | |
|
217 | qreal step = (max-min)/count; | |
|
218 | QChartAxisCategories& categories = mChart->axisX()->categories(); | |
|
219 | categories.clear(); | |
|
220 | for (int i=0; i<count; i++) { | |
|
221 | categories.insert(min,mSeries->categoryName(i)); | |
|
222 | min += step; | |
|
223 | } | |
|
224 | */ | |
|
225 | 191 | } |
|
226 | 192 | |
|
227 | 193 | void BarChartItem::handleGeometryChanged(const QRectF& rect) |
@@ -43,9 +43,6 public: | |||
|
43 | 43 | |
|
44 | 44 | QRectF geometry() const { return m_rect;} |
|
45 | 45 | |
|
46 | protected: | |
|
47 | void initAxisLabels(); | |
|
48 | ||
|
49 | 46 | public slots: |
|
50 | 47 | void handleModelChanged(int index); |
|
51 | 48 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
@@ -6,6 +6,8 | |||
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | typedef QStringList QBarCategory; | |
|
10 | ||
|
9 | 11 | class QBarSet; |
|
10 | 12 | class BarChartModel; |
|
11 | 13 | class BarCategory; |
@@ -123,8 +123,16 void ChartDataSet::removeAllSeries() | |||
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | //to be removed with PIMPL |
|
126 |
void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) |
|
|
126 | void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) | |
|
127 | 127 | { |
|
128 | qreal minX(domain->minX()); | |
|
129 | qreal minY(domain->minY()); | |
|
130 | qreal maxX(domain->maxX()); | |
|
131 | qreal maxY(domain->maxY()); | |
|
132 | int tickXCount(domain->tickXCount()); | |
|
133 | int tickYCount(domain->tickYCount()); | |
|
134 | ||
|
135 | ||
|
128 | 136 | switch(series->type()) |
|
129 | 137 | { |
|
130 | 138 | case QSeries::SeriesTypeLine: |
@@ -134,11 +142,6 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const | |||
|
134 | 142 | |
|
135 | 143 | QXYSeries* xySeries = static_cast<QXYSeries*>(series); |
|
136 | 144 | |
|
137 | qreal minX(domain->minX()); | |
|
138 | qreal minY(domain->minY()); | |
|
139 | qreal maxX(domain->maxX()); | |
|
140 | qreal maxY(domain->maxY()); | |
|
141 | ||
|
142 | 145 | for (int i = 0; i < xySeries->count(); i++) |
|
143 | 146 | { |
|
144 | 147 | qreal x = xySeries->x(i); |
@@ -148,14 +151,11 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const | |||
|
148 | 151 | maxX = qMax(maxX, x); |
|
149 | 152 | maxY = qMax(maxY, y); |
|
150 | 153 | } |
|
151 | ||
|
152 | domain->setRange(minX, maxX, minY, maxY); | |
|
153 | 154 | break; |
|
154 | 155 | } |
|
155 | 156 | case QSeries::SeriesTypeArea: { |
|
156 | 157 | |
|
157 | 158 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
158 | ||
|
159 | 159 | QLineSeries* upperSeries = areaSeries->upperSeries(); |
|
160 | 160 | QLineSeries* lowerSeries = areaSeries->lowerSeries(); |
|
161 | 161 | |
@@ -163,50 +163,58 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const | |||
|
163 | 163 | { |
|
164 | 164 | qreal x = upperSeries->x(i); |
|
165 | 165 | qreal y = upperSeries->y(i); |
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
|
166 | minX = qMin(minX, x); | |
|
167 | minY = qMin(minY, y); | |
|
168 | maxX = qMax(maxX, x); | |
|
169 | maxY = qMax(maxY, y); | |
|
170 | 170 | } |
|
171 | 171 | if(lowerSeries) { |
|
172 | 172 | for (int i = 0; i < lowerSeries->count(); i++) |
|
173 | 173 | { |
|
174 | 174 | qreal x = lowerSeries->x(i); |
|
175 | 175 | qreal y = lowerSeries->y(i); |
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
176 | minX = qMin(minX, x); | |
|
177 | minY = qMin(minY, y); | |
|
178 | maxX = qMax(maxX, x); | |
|
179 | maxY = qMax(maxY, y); | |
|
180 | 180 | }} |
|
181 | 181 | break; |
|
182 | 182 | } |
|
183 | 183 | case QSeries::SeriesTypeBar: { |
|
184 | ||
|
184 | 185 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
185 | 186 | qreal x = barSeries->categoryCount(); |
|
186 | 187 | qreal y = barSeries->max(); |
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
188 | minX = qMin(minX, x); | |
|
189 | minY = qMin(minY, y); | |
|
190 | maxX = qMax(maxX, x); | |
|
191 | maxY = qMax(maxY, y); | |
|
192 | tickXCount = x+1; | |
|
193 | setupCategories(barSeries); | |
|
191 | 194 | break; |
|
192 | 195 | } |
|
193 | 196 | case QSeries::SeriesTypeStackedBar: { |
|
197 | ||
|
194 | 198 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
195 | 199 | qreal x = stackedBarSeries->categoryCount(); |
|
196 | 200 | qreal y = stackedBarSeries->maxCategorySum(); |
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 | minX = qMin(minX, x); | |
|
202 | minY = qMin(minY, y); | |
|
203 | maxX = qMax(maxX, x); | |
|
204 | maxY = qMax(maxY, y); | |
|
205 | tickXCount = x+1; | |
|
206 | setupCategories(stackedBarSeries); | |
|
201 | 207 | break; |
|
202 | 208 | } |
|
203 | 209 | case QSeries::SeriesTypePercentBar: { |
|
210 | ||
|
204 | 211 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
205 | 212 | qreal x = percentBarSeries->categoryCount(); |
|
206 |
|
|
|
207 | domain->setMinY(0); | |
|
208 | domain->setMaxX(qMax(domain->maxX(),x)); | |
|
209 |
|
|
|
213 | minX = qMin(minX, x); | |
|
214 | maxX = qMax(maxX, x); | |
|
215 | minY = 0; | |
|
216 | maxY = 100; | |
|
217 | setupCategories(percentBarSeries); | |
|
210 | 218 | break; |
|
211 | 219 | } |
|
212 | 220 | |
@@ -224,6 +232,20 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const | |||
|
224 | 232 | } |
|
225 | 233 | |
|
226 | 234 | } |
|
235 | ||
|
236 | domain->setRangeX(minX,maxX,tickXCount); | |
|
237 | domain->setRangeY(minY,maxY,tickYCount); | |
|
238 | } | |
|
239 | ||
|
240 | ||
|
241 | void ChartDataSet::setupCategories(QBarSeries* series) | |
|
242 | { | |
|
243 | int count = series->categoryCount(); | |
|
244 | QChartAxisCategories* categories = axisX()->categories(); | |
|
245 | categories->clear(); | |
|
246 | for (int i=1; i<=count; i++) { | |
|
247 | categories->insert(i,series->categoryName(i-1)); | |
|
248 | } | |
|
227 | 249 | } |
|
228 | 250 | |
|
229 | 251 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) |
@@ -8,6 +8,7 | |||
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QChartAxis; |
|
11 | class QBarSeries; | |
|
11 | 12 | |
|
12 | 13 | class ChartDataSet : public QObject |
|
13 | 14 | { |
@@ -42,7 +43,8 signals: | |||
|
42 | 43 | |
|
43 | 44 | private: |
|
44 | 45 | QStringList createLabels(QChartAxis* axis,qreal min, qreal max); |
|
45 |
void calculateDomain(QSeries* series,Domain* domain) |
|
|
46 | void calculateDomain(QSeries* series,Domain* domain); | |
|
47 | void setupCategories(QBarSeries* series); | |
|
46 | 48 | |
|
47 | 49 | private: |
|
48 | 50 | QMap<QSeries*, QChartAxis*> m_seriesAxisMap; |
General Comments 0
You need to be logged in to leave comments.
Login now