##// END OF EJS Templates
Adds back reimplemnted categories handling
Michal Klocek -
r701:0d46cd22e84d
parent child
Show More
@@ -76,29 +76,31 void Axis::updateLayout(QVector<qreal>& layout)
76 else setLayout(layout);
76 else setLayout(layout);
77 }
77 }
78
78
79 QStringList Axis::createLabels(int ticks, qreal min, qreal max) const
79 bool Axis::createLabels(QStringList& labels,qreal min, qreal max,int ticks) const
80 {
80 {
81 Q_ASSERT(max>=min);
81 Q_ASSERT(max>=min);
82 Q_ASSERT(ticks>1);
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 QChartAxisCategories* categories = m_chartAxis->categories();
84 QChartAxisCategories* categories = m_chartAxis->categories();
89
85
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++) {
90 for(int i=0; i< ticks; i++) {
91 qreal value = min + (i * (max - min)/ (ticks-1));
91 qreal value = min + (i * (max - min)/ (ticks-1));
92 if(categories->count()==0) {
93 labels << QString::number(value,'f',n);
92 labels << QString::number(value,'f',n);
94 }
93 }
94 }
95 else {
95 else {
96
96 for(int i=0; i< ticks; i++) {
97 int value = ceil(min + (i * (max - min)/ (ticks-1)));
97 QString label = categories->label(value);
98 QString label = categories->label(value);
98 labels << label;
99 labels << label;
99 }
100 }
100 }
101 }
101 return labels;
102
103 return category;
102 }
104 }
103
105
104 void Axis::setAxisOpacity(qreal opacity)
106 void Axis::setAxisOpacity(qreal opacity)
@@ -244,7 +246,9 void Axis::setLayout(QVector<qreal>& layout)
244
246
245 if(diff!=0) handleAxisUpdated();
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 QList<QGraphicsItem *> lines = m_grid.childItems();
253 QList<QGraphicsItem *> lines = m_grid.childItems();
250 QList<QGraphicsItem *> labels = m_labels.childItems();
254 QList<QGraphicsItem *> labels = m_labels.childItems();
@@ -265,10 +269,19 void Axis::setLayout(QVector<qreal>& layout)
265 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
269 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
266 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
270 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
267 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
271 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
272
273 if(!categories){
268 labelItem->setText(ticksList.at(i));
274 labelItem->setText(ticksList.at(i));
269 QPointF center = labelItem->boundingRect().center();
275 QPointF center = labelItem->boundingRect().center();
270 labelItem->setTransformOriginPoint(center.x(), center.y());
276 labelItem->setTransformOriginPoint(center.x(), center.y());
271 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
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 if((i+1)%2 && i>1) {
285 if((i+1)%2 && i>1) {
273 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
286 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
274 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
287 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
@@ -65,7 +65,7 private:
65 void updateLayout(QVector<qreal>& layout);
65 void updateLayout(QVector<qreal>& layout);
66 void setLayout(QVector<qreal>& layout);
66 void setLayout(QVector<qreal>& layout);
67
67
68 QStringList createLabels(int ticks, qreal min, qreal max) const;
68 bool createLabels(QStringList& labels,qreal min, qreal max,int ticks) const;
69 void axisSelected();
69 void axisSelected();
70
70
71 private:
71 private:
@@ -13,6 +13,16 QChartAxisCategories::~QChartAxisCategories()
13 // TODO Auto-generated destructor stub
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 void QChartAxisCategories::insert(qreal value,QString label)
26 void QChartAxisCategories::insert(qreal value,QString label)
17 {
27 {
18 m_map.insert(value,label);
28 m_map.insert(value,label);
@@ -1,6 +1,8
1 #ifndef QCHARTAXISCATEGORIES_H_
1 #ifndef QCHARTAXISCATEGORIES_H_
2 #define QCHARTAXISCATEGORIES_H_
2 #define QCHARTAXISCATEGORIES_H_
3
3 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qbarseries.h>
4
6
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
8
@@ -12,6 +14,7 private:
12 public:
14 public:
13 ~QChartAxisCategories();
15 ~QChartAxisCategories();
14
16
17 void insert(QBarCategory& category);
15 void insert(qreal value,QString label);
18 void insert(qreal value,QString label);
16 void remove(qreal value);
19 void remove(qreal value);
17 QString label(qreal value) const;
20 QString label(qreal value) const;
@@ -24,7 +24,6 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
24 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
24 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
25 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
25 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
26 setZValue(ChartPresenter::BarSeriesZValue);
26 setZValue(ChartPresenter::BarSeriesZValue);
27 initAxisLabels();
28 dataChanged();
27 dataChanged();
29 }
28 }
30
29
@@ -174,21 +173,6 void BarChartItem::setLayout(const QVector<QRectF> &layout)
174 update();
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 //handlers
176 //handlers
193
177
194 void BarChartItem::handleModelChanged(int index)
178 void BarChartItem::handleModelChanged(int index)
@@ -204,24 +188,6 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal
204 mDomainMinY = minY;
188 mDomainMinY = minY;
205 mDomainMaxY = maxY;
189 mDomainMaxY = maxY;
206 handleLayoutChanged();
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 void BarChartItem::handleGeometryChanged(const QRectF& rect)
193 void BarChartItem::handleGeometryChanged(const QRectF& rect)
@@ -43,9 +43,6 public:
43
43
44 QRectF geometry() const { return m_rect;}
44 QRectF geometry() const { return m_rect;}
45
45
46 protected:
47 void initAxisLabels();
48
49 public slots:
46 public slots:
50 void handleModelChanged(int index);
47 void handleModelChanged(int index);
51 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
48 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
@@ -6,6 +6,8
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 typedef QStringList QBarCategory;
10
9 class QBarSet;
11 class QBarSet;
10 class BarChartModel;
12 class BarChartModel;
11 class BarCategory;
13 class BarCategory;
@@ -123,8 +123,16 void ChartDataSet::removeAllSeries()
123 }
123 }
124
124
125 //to be removed with PIMPL
125 //to be removed with PIMPL
126 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
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 switch(series->type())
136 switch(series->type())
129 {
137 {
130 case QSeries::SeriesTypeLine:
138 case QSeries::SeriesTypeLine:
@@ -134,11 +142,6 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
134
142
135 QXYSeries* xySeries = static_cast<QXYSeries*>(series);
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 for (int i = 0; i < xySeries->count(); i++)
145 for (int i = 0; i < xySeries->count(); i++)
143 {
146 {
144 qreal x = xySeries->x(i);
147 qreal x = xySeries->x(i);
@@ -148,14 +151,11 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
148 maxX = qMax(maxX, x);
151 maxX = qMax(maxX, x);
149 maxY = qMax(maxY, y);
152 maxY = qMax(maxY, y);
150 }
153 }
151
152 domain->setRange(minX, maxX, minY, maxY);
153 break;
154 break;
154 }
155 }
155 case QSeries::SeriesTypeArea: {
156 case QSeries::SeriesTypeArea: {
156
157
157 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
158 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
158
159 QLineSeries* upperSeries = areaSeries->upperSeries();
159 QLineSeries* upperSeries = areaSeries->upperSeries();
160 QLineSeries* lowerSeries = areaSeries->lowerSeries();
160 QLineSeries* lowerSeries = areaSeries->lowerSeries();
161
161
@@ -163,50 +163,58 void ChartDataSet::calculateDomain(QSeries* series,Domain* domain) const
163 {
163 {
164 qreal x = upperSeries->x(i);
164 qreal x = upperSeries->x(i);
165 qreal y = upperSeries->y(i);
165 qreal y = upperSeries->y(i);
166 domain->setMinX(qMin(domain->minX(),x));
166 minX = qMin(minX, x);
167 domain->setMinY(qMin(domain->minY(),y));
167 minY = qMin(minY, y);
168 domain->setMaxX(qMax(domain->maxX(),x));
168 maxX = qMax(maxX, x);
169 domain->setMaxY(qMax(domain->maxY(),y));
169 maxY = qMax(maxY, y);
170 }
170 }
171 if(lowerSeries) {
171 if(lowerSeries) {
172 for (int i = 0; i < lowerSeries->count(); i++)
172 for (int i = 0; i < lowerSeries->count(); i++)
173 {
173 {
174 qreal x = lowerSeries->x(i);
174 qreal x = lowerSeries->x(i);
175 qreal y = lowerSeries->y(i);
175 qreal y = lowerSeries->y(i);
176 domain->setMinX(qMin(domain->minX(),x));
176 minX = qMin(minX, x);
177 domain->setMinY(qMin(domain->minY(),y));
177 minY = qMin(minY, y);
178 domain->setMaxX(qMax(domain->maxX(),x));
178 maxX = qMax(maxX, x);
179 domain->setMaxY(qMax(domain->maxY(),y));
179 maxY = qMax(maxY, y);
180 }}
180 }}
181 break;
181 break;
182 }
182 }
183 case QSeries::SeriesTypeBar: {
183 case QSeries::SeriesTypeBar: {
184
184 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
185 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
185 qreal x = barSeries->categoryCount();
186 qreal x = barSeries->categoryCount();
186 qreal y = barSeries->max();
187 qreal y = barSeries->max();
187 domain->setMinX(qMin(domain->minX(),x));
188 minX = qMin(minX, x);
188 domain->setMinY(qMin(domain->minY(),y));
189 minY = qMin(minY, y);
189 domain->setMaxX(qMax(domain->maxX(),x));
190 maxX = qMax(maxX, x);
190 domain->setMaxY(qMax(domain->maxY(),y));
191 maxY = qMax(maxY, y);
192 tickXCount = x+1;
193 setupCategories(barSeries);
191 break;
194 break;
192 }
195 }
193 case QSeries::SeriesTypeStackedBar: {
196 case QSeries::SeriesTypeStackedBar: {
197
194 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
198 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
195 qreal x = stackedBarSeries->categoryCount();
199 qreal x = stackedBarSeries->categoryCount();
196 qreal y = stackedBarSeries->maxCategorySum();
200 qreal y = stackedBarSeries->maxCategorySum();
197 domain->setMinX(qMin(domain->minX(),x));
201 minX = qMin(minX, x);
198 domain->setMinY(qMin(domain->minY(),y));
202 minY = qMin(minY, y);
199 domain->setMaxX(qMax(domain->maxX(),x));
203 maxX = qMax(maxX, x);
200 domain->setMaxY(qMax(domain->maxY(),y));
204 maxY = qMax(maxY, y);
205 tickXCount = x+1;
206 setupCategories(stackedBarSeries);
201 break;
207 break;
202 }
208 }
203 case QSeries::SeriesTypePercentBar: {
209 case QSeries::SeriesTypePercentBar: {
210
204 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
211 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
205 qreal x = percentBarSeries->categoryCount();
212 qreal x = percentBarSeries->categoryCount();
206 domain->setMinX(qMin(domain->minX(),x));
213 minX = qMin(minX, x);
207 domain->setMinY(0);
214 maxX = qMax(maxX, x);
208 domain->setMaxX(qMax(domain->maxX(),x));
215 minY = 0;
209 domain->setMaxY(100);
216 maxY = 100;
217 setupCategories(percentBarSeries);
210 break;
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 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
251 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
@@ -8,6 +8,7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11 class QBarSeries;
11
12
12 class ChartDataSet : public QObject
13 class ChartDataSet : public QObject
13 {
14 {
@@ -42,7 +43,8 signals:
42
43
43 private:
44 private:
44 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
45 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
45 void calculateDomain(QSeries* series,Domain* domain) const;
46 void calculateDomain(QSeries* series,Domain* domain);
47 void setupCategories(QBarSeries* series);
46
48
47 private:
49 private:
48 QMap<QSeries*, QChartAxis*> m_seriesAxisMap;
50 QMap<QSeries*, QChartAxis*> m_seriesAxisMap;
@@ -24,11 +24,11 public:
24 BackgroundZValue = -1,
24 BackgroundZValue = -1,
25 ShadesZValue,
25 ShadesZValue,
26 GridZValue,
26 GridZValue,
27 AxisZValue,
28 LineChartZValue,
27 LineChartZValue,
29 BarSeriesZValue,
28 BarSeriesZValue,
30 ScatterSeriesZValue,
29 ScatterSeriesZValue,
31 PieSeriesZValue,
30 PieSeriesZValue,
31 AxisZValue,
32 LegendZValue
32 LegendZValue
33 };
33 };
34
34
General Comments 0
You need to be logged in to leave comments. Login now