##// END OF EJS Templates
Bugfix shades not updated aftet tick changed
Michal Klocek -
r241:0019f0f15db3
parent child
Show More
@@ -1,299 +1,298
1 1 #include "axisitem_p.h"
2 2 #include "qchartaxis.h"
3 3 #include <QPainter>
4 4 #include <QDebug>
5 5
6 6 static int label_padding = 5;
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 11 ChartItem(parent),
12 12 m_type(type),
13 13 m_labelsAngle(0),
14 14 m_shadesEnabled(true),
15 15 m_grid(parent),
16 16 m_shades(parent),
17 17 m_labels(parent)
18 18 {
19 19 //initial initialization
20 20 m_shades.setZValue(0);
21 21 m_grid.setZValue(2);
22 22 }
23 23
24 24 AxisItem::~AxisItem()
25 25 {
26 26 }
27 27
28 28 QRectF AxisItem::boundingRect() const
29 29 {
30 30 return m_rect;
31 31 }
32 32
33 33 void AxisItem::createItems(int count)
34 34 {
35 35 for (int i = 0; i < count; ++i) {
36 36 m_grid.addToGroup(new QGraphicsLineItem(this));
37 37 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
38 38 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
39 39 }
40 40 }
41 41
42 42 void AxisItem::clear()
43 43 {
44 44 foreach(QGraphicsItem* item , m_shades.childItems()) {
45 45 delete item;
46 46 }
47 47
48 48 foreach(QGraphicsItem* item , m_grid.childItems()) {
49 49 delete item;
50 50 }
51 51
52 52 foreach(QGraphicsItem* item , m_labels.childItems()) {
53 53 delete item;
54 54 }
55 55
56 56 m_thicksList.clear();
57 57
58 58 }
59 59
60 60 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
61 61 {
62 62
63 63 }
64 64
65 65 void AxisItem::updateItem(int count)
66 66 {
67 67
68 68 QList<QGraphicsItem *> lines = m_grid.childItems();
69 69 QList<QGraphicsItem *> labels = m_labels.childItems();
70 70 QList<QGraphicsItem *> shades = m_shades.childItems();
71 71
72 72 switch (m_type)
73 73 {
74 74 case X_AXIS:
75 75 {
76 76 const qreal deltaX = m_rect.width() / (count-1);
77 77
78 78 m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
79 79
80 80 for (int i = 0; i < count; ++i) {
81 81 int x = i * deltaX + m_rect.left();
82 82 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
83 83 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
84 84 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
85 85 labelItem->setText(m_thicksList.at(i));
86 86 QPointF center = labelItem->boundingRect().center();
87 87 labelItem->setTransformOriginPoint(center.x(), center.y());
88 88 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
89 89
90 90 if(i%2){
91 91 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
92 92 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
93 rectItem->setOpacity( 0.5 );
94 93 }
95 94 }
96 95 }
97 96 break;
98 97
99 98 case Y_AXIS:
100 99 {
101 100 const qreal deltaY = m_rect.height()/ (count-1);
102 101
103 102 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
104 103
105 104 for (int i = 0; i < count; ++i) {
106 105 int y = i * -deltaY + m_rect.bottom();
107 106 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
108 107 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
109 108 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
110 109 labelItem->setText(m_thicksList.at(i));
111 110 QPointF center = labelItem->boundingRect().center();
112 111 labelItem->setTransformOriginPoint(center.x(), center.y());
113 112 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
114 113 if(i%2){
115 114 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
116 115 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
117
118 116 }
119 117 }
120 118 }
121 119 break;
122 120 default:
123 121 qDebug()<<"Unknown axis type";
124 122 break;
125 123 }
126 124 }
127 125
128 126 void AxisItem::handleAxisUpdate(QChartAxis* axis)
129 127 {
130 128 if(axis->isAxisVisible()) {
131 129 setAxisOpacity(100);
132 130 }
133 131 else {
134 132 setAxisOpacity(0);
135 133 }
136 134
137 135 if(axis->isGridVisible()) {
138 136 setGridOpacity(100);
139 137 }
140 138 else {
141 139 setGridOpacity(0);
142 140 }
143 141
144 142 if(axis->isLabelsVisible())
145 143 {
146 144 setLabelsOpacity(100);
147 145 }
148 146 else {
149 147 setLabelsOpacity(0);
150 148 }
151 149
152 150 if(axis->isShadesVisible()) {
153 151 setShadesOpacity(axis->shadesOpacity());
154 152 }
155 153 else {
156 154 setShadesOpacity(0);
157 155 }
158 156
159 157 setLabelsAngle(axis->labelsAngle());
160 158 setAxisPen(axis->axisPen());
161 159 setLabelsPen(axis->labelsPen());
162 160 setLabelsBrush(axis->labelsBrush());
163 161 setLabelsFont(axis->labelFont());
164 162 setGridPen(axis->gridPen());
165 163 setShadesPen(axis->shadesPen());
166 164 setShadesBrush(axis->shadesBrush());
167 165 }
168 166
169 void AxisItem::handleLabelsChanged(const QStringList& labels)
167 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
170 168 {
171 169 m_thicksList=labels;
172 170 QList<QGraphicsItem*> items = m_labels.childItems();
173 171 if(items.size()!=m_thicksList.size()){
174 172 clear();
175 173 m_thicksList=labels;
176 174 createItems(m_thicksList.size());
177 175 items = m_labels.childItems();
176 handleAxisUpdate(axis);
178 177 }
179 178
180 179 Q_ASSERT(items.size()==m_thicksList.size());
181 180
182 181 int i=0;
183 182 foreach(QGraphicsItem* item, items){
184 183 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
185 184 i++;
186 185 }
187 186 update();
188 187 }
189 188
190 189 void AxisItem::handleGeometryChanged(const QRectF& rect)
191 190 {
192 191 m_rect = rect;
193 192 updateItem(m_thicksList.size());
194 193 update();
195 194 }
196 195
197 196 void AxisItem::setAxisOpacity(qreal opacity)
198 197 {
199 198 m_axis.setOpacity(opacity);
200 199 }
201 200
202 201 qreal AxisItem::axisOpacity() const
203 202 {
204 203 return m_axis.opacity();
205 204 }
206 205
207 206 void AxisItem::setGridOpacity(qreal opacity)
208 207 {
209 208 m_grid.setOpacity(opacity);
210 209 }
211 210
212 211
213 212 qreal AxisItem::gridOpacity() const
214 213 {
215 214 return m_grid.opacity();
216 215 }
217 216
218 217 void AxisItem::setLabelsOpacity(qreal opacity)
219 218 {
220 219 m_labels.setOpacity(opacity);
221 220 }
222 221
223 222 qreal AxisItem::labelsOpacity() const
224 223 {
225 224 return m_labels.opacity();
226 225 }
227 226
228 227 void AxisItem::setShadesOpacity(qreal opacity)
229 228 {
230 229 m_shades.setOpacity(opacity);
231 230 }
232 231
233 232 qreal AxisItem::shadesOpacity() const
234 233 {
235 234 return m_shades.opacity();
236 235 }
237 236
238 237 void AxisItem::setLabelsAngle(int angle)
239 238 {
240 239 foreach(QGraphicsItem* item , m_labels.childItems()) {
241 240 QPointF center = item->boundingRect().center();
242 241 item->setRotation(angle);
243 242 }
244 243
245 244 m_labelsAngle=angle;
246 245 }
247 246
248 247 void AxisItem::setLabelsPen(const QPen& pen)
249 248 {
250 249 foreach(QGraphicsItem* item , m_labels.childItems()) {
251 250 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
252 251 }
253 252 }
254 253
255 254 void AxisItem::setLabelsBrush(const QBrush& brush)
256 255 {
257 256 foreach(QGraphicsItem* item , m_labels.childItems()) {
258 257 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
259 258 }
260 259 }
261 260
262 261 void AxisItem::setLabelsFont(const QFont& font)
263 262 {
264 263 foreach(QGraphicsItem* item , m_labels.childItems()) {
265 264 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
266 265 }
267 266 }
268 267
269 268 void AxisItem::setShadesBrush(const QBrush& brush)
270 269 {
271 270 foreach(QGraphicsItem* item , m_shades.childItems()) {
272 271 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
273 272 }
274 273 }
275 274
276 275 void AxisItem::setShadesPen(const QPen& pen)
277 276 {
278 277 foreach(QGraphicsItem* item , m_shades.childItems()) {
279 278 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
280 279 }
281 280 }
282 281
283 282 void AxisItem::setAxisPen(const QPen& pen)
284 283 {
285 284 m_axis.setPen(pen);
286 285 }
287 286
288 287 void AxisItem::setGridPen(const QPen& pen)
289 288 {
290 289 foreach(QGraphicsItem* item , m_grid.childItems()) {
291 290 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
292 291 }
293 292 }
294 293
295 294
296 295 //TODO "nice numbers algorithm"
297 296 #include "moc_axisitem_p.cpp"
298 297
299 298 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,76
1 1 #ifndef AXISITEM_H_
2 2 #define AXISITEM_H_
3 3
4 4 #include "domain_p.h"
5 5 #include "chartitem_p.h"
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11
12 12 class AxisItem : public QObject, public ChartItem
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 enum AxisType{X_AXIS,Y_AXIS};
17 17
18 18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 19 ~AxisItem();
20 20
21 21 //from QGraphicsItem
22 22 QRectF boundingRect() const;
23 23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 24
25 25 AxisType axisType() const {return m_type;};
26 26
27 27 void setAxisOpacity(qreal opacity);
28 28 qreal axisOpacity() const;
29 29
30 30 void setGridOpacity(qreal opacity);
31 31 qreal gridOpacity() const;
32 32
33 33 void setLabelsOpacity(qreal opacity);
34 34 qreal labelsOpacity() const;
35 35
36 36 void setShadesOpacity(qreal opacity);
37 37 qreal shadesOpacity() const;
38 38
39 39 void setLabelsAngle(int angle);
40 40 int labelsAngle()const { return m_labelsAngle; }
41 41
42 42 void setShadesBrush(const QBrush& brush);
43 43 void setShadesPen(const QPen& pen);
44 44
45 45 void setAxisPen(const QPen& pen);
46 46 void setGridPen(const QPen& pen);
47 47
48 48 void setLabelsPen(const QPen& pen);
49 49 void setLabelsBrush(const QBrush& brush);
50 50 void setLabelsFont(const QFont& font);
51 51
52 52 public slots:
53 53 void handleAxisUpdate(QChartAxis* axis);
54 void handleLabelsChanged(const QStringList& labels);
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels);
55 55 void handleGeometryChanged(const QRectF& size);
56 56 protected:
57 57 void updateItem(int count);
58 58 private:
59 59 void clear();
60 60 void createItems(int count);
61 61 private:
62 62 AxisType m_type;
63 63 QRectF m_rect;
64 64 int m_labelsAngle;
65 65 bool m_shadesEnabled;
66 66 QGraphicsItemGroup m_grid;
67 67 QGraphicsItemGroup m_shades;
68 68 QGraphicsItemGroup m_labels;
69 69 QGraphicsLineItem m_axis;
70 70 QStringList m_thicksList;
71 71
72 72 };
73 73
74 74 QTCOMMERCIALCHART_END_NAMESPACE
75 75
76 76 #endif /* AXISITEM_H_ */
@@ -1,334 +1,334
1 1 #include "chartdataset_p.h"
2 2 #include "qchartaxis.h"
3 3 //series
4 4 #include "qlinechartseries.h"
5 5 #include "qbarchartseries.h"
6 6 #include "qstackedbarchartseries.h"
7 7 #include "qpercentbarchartseries.h"
8 8 #include "qpieseries.h"
9 9 #include "qscatterseries.h"
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 14 m_axisX(new QChartAxis(this)),
15 15 m_axisY(new QChartAxis(this)),
16 16 m_domainIndex(0),
17 17 m_axisXInitialized(false)
18 18 {
19 19 }
20 20
21 21 ChartDataSet::~ChartDataSet()
22 22 {
23 23 // TODO Auto-generated destructor stub
24 24 }
25 25
26 26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 27 {
28 28 int i = m_domainMap.count(axisY);
29 29 if(i == 0){
30 30 return Domain();
31 31 }
32 32 i = i - m_domainIndex -1;
33 33 return m_domainMap.values(axisY).at(i);
34 34 }
35 35
36 36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
37 37 {
38 38 // TODO: we should check the series not already added
39 39 series->setParent(this); // take ownership
40 40
41 41 series->setParent(this); // take ownership
42 42 clearDomains();
43 43
44 44 if(axisY==0) axisY = m_axisY;
45 45
46 46 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
47 47
48 48 QList<Domain> domainList = m_domainMap.values(axisY);
49 49
50 50 Q_ASSERT(domainList.size()<=1);
51 51
52 52 Domain domain;
53 53
54 54 if(domainList.size()>0) domain = domainList.at(0);
55 55
56 56 switch(series->type())
57 57 {
58 58 case QChartSeries::SeriesTypeLine: {
59 59
60 60 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
61 61
62 62 for (int i = 0; i < xyseries->count(); i++)
63 63 {
64 64 qreal x = xyseries->x(i);
65 65 qreal y = xyseries->y(i);
66 66 domain.m_minX = qMin(domain.m_minX,x);
67 67 domain.m_minY = qMin(domain.m_minY,y);
68 68 domain.m_maxX = qMax(domain.m_maxX,x);
69 69 domain.m_maxY = qMax(domain.m_maxY,y);
70 70 }
71 71 break;
72 72 }
73 73 case QChartSeries::SeriesTypeBar: {
74 74 qDebug() << "QChartSeries::SeriesTypeBar";
75 75 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
76 76 qreal x = barSeries->countCategories();
77 77 qreal y = barSeries->max();
78 78 domain.m_minX = qMin(domain.m_minX,x);
79 79 domain.m_minY = qMin(domain.m_minY,y);
80 80 domain.m_maxX = qMax(domain.m_maxX,x);
81 81 domain.m_maxY = qMax(domain.m_maxY,y);
82 82 break;
83 83 }
84 84 case QChartSeries::SeriesTypeStackedBar: {
85 85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86 86
87 87 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
88 88 qreal x = stackedBarSeries->countCategories();
89 89 qreal y = stackedBarSeries->maxCategorySum();
90 90 domain.m_minX = qMin(domain.m_minX,x);
91 91 domain.m_minY = qMin(domain.m_minY,y);
92 92 domain.m_maxX = qMax(domain.m_maxX,x);
93 93 domain.m_maxY = qMax(domain.m_maxY,y);
94 94 break;
95 95 }
96 96 case QChartSeries::SeriesTypePercentBar: {
97 97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98 98
99 99 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
100 100 qreal x = percentBarSeries->countCategories();
101 101 domain.m_minX = qMin(domain.m_minX,x);
102 102 domain.m_minY = 0;
103 103 domain.m_maxX = qMax(domain.m_maxX,x);
104 104 domain.m_maxY = 100;
105 105 break;
106 106 }
107 107
108 108 case QChartSeries::SeriesTypePie: {
109 109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 110 // TODO: domain stuff
111 111 break;
112 112 }
113 113
114 114 case QChartSeries::SeriesTypeScatter: {
115 115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 116 Q_ASSERT(scatterSeries);
117 117 foreach (QPointF point, scatterSeries->data()) {
118 118 domain.m_minX = qMin(domain.m_minX, point.x());
119 119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 120 domain.m_minY = qMin(domain.m_minY, point.y());
121 121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 122 }
123 123 break;
124 124 }
125 125
126 126 default: {
127 127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 128 return;
129 129 break;
130 130 }
131 131
132 132 }
133 133
134 134 if(!m_domainMap.contains(axisY))
135 135 {
136 136 emit axisAdded(axisY);
137 137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
138 138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
139 139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
140 140 }
141 141 m_domainMap.replace(axisY,domain);
142 142 m_seriesMap.insert(axisY,series);
143 143
144 144 if(!m_axisXInitialized)
145 145 {
146 146 emit axisAdded(axisX());
147 147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
148 148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
149 149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
150 150 m_axisXInitialized=true;
151 151 }
152 152
153 153
154 154 emit seriesAdded(series);
155 155 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
156 156 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
157 157 emit axisLabelsChanged(axisY,ylabels);
158 158 emit axisLabelsChanged(axisX(),xlabels);
159 159 emit seriesDomainChanged(series,domain);
160 160
161 161 }
162 162
163 163 void ChartDataSet::removeSeries(QChartSeries* series)
164 164 {
165 165 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
166 166 foreach(QChartAxis* axis , keys) {
167 167 if(m_seriesMap.contains(axis,series)){
168 168 emit seriesRemoved(series);
169 169 m_seriesMap.remove(axis,series);
170 170 //remove axis if no longer there
171 171 if(!m_seriesMap.contains(axis) && axis != m_axisY){
172 172 emit axisRemoved(axis);
173 173 m_domainMap.remove(axis);
174 174 delete axis;
175 175 }
176 176 break;
177 177 }
178 178 }
179 179 }
180 180
181 181 bool ChartDataSet::nextDomain()
182 182 {
183 183 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
184 184
185 185 if (m_domainIndex < limit) {
186 186 m_domainIndex++;
187 187 setDomain(m_domainIndex);
188 188 return true;
189 189 }
190 190 else {
191 191 return false;
192 192 }
193 193 }
194 194
195 195 bool ChartDataSet::previousDomain()
196 196 {
197 197 if (m_domainIndex > 0) {
198 198 m_domainIndex--;
199 199 setDomain(m_domainIndex);
200 200 return true;
201 201 }
202 202 else {
203 203 return false;
204 204 }
205 205 }
206 206
207 207 void ChartDataSet::setDomain(int index)
208 208 {
209 209 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
210 210 foreach (QChartAxis* axis , domainList) {
211 211 int i = m_domainMap.count(axis) - index -1;
212 212 Q_ASSERT(i>=0);
213 213 Domain domain = m_domainMap.values(axis).at(i);
214 214 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
215 215 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
216 216 foreach(QChartSeries* series, seriesList) {
217 217 emit seriesDomainChanged(series,domain);
218 218 }
219 219 emit axisLabelsChanged(axis,labels);
220 220 }
221 221
222 222 Domain domain = m_domainMap.value(axisY());
223 223 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
224 224 emit axisLabelsChanged(axisX(),labels);
225 225 }
226 226
227 227 void ChartDataSet::clearDomains(int toIndex)
228 228 {
229 229 Q_ASSERT(toIndex>=0);
230 230
231 231 m_domainIndex = toIndex;
232 232
233 233 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
234 234
235 235 foreach (QChartAxis* key , keys)
236 236 {
237 237 QList<Domain> domains = m_domainMap.values(key);
238 238 m_domainMap.remove(key);
239 239 int i = domains.size() - toIndex - 1;
240 240 while(i--){
241 241 domains.removeFirst();
242 242 }
243 243 for(int j=domains.size()-1; j>=0 ;j--)
244 244 m_domainMap.insert(key,domains.at(j));
245 245 }
246 246 }
247 247
248 248 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
249 249 {
250 250 Q_ASSERT(rect.isValid());
251 251 Q_ASSERT(viewport.isValid());
252 252
253 253 clearDomains(m_domainIndex);
254 254
255 255 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
256 256
257 257 foreach (QChartAxis* axis , domainList){
258 258 Domain domain(m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()));
259 259 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
260 260 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
261 261 foreach(QChartSeries* series, seriesList){
262 262 emit seriesDomainChanged(series,domain);
263 263 }
264 264 emit axisLabelsChanged(axis,labels);
265 265 m_domainMap.insert(axis,domain);
266 266 }
267 267
268 268 Domain domain = m_domainMap.value(axisY());
269 269 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
270 270 emit axisLabelsChanged(axisX(),labels);
271 271
272 272 m_domainIndex++;
273 273 }
274 274
275 275 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
276 276 {
277 277 if(series == 0) return m_axisY;
278 278
279 279 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
280 280
281 281 foreach(QChartAxis* axis , keys) {
282 282 if(m_seriesMap.contains(axis,series)){
283 283 return axis;
284 284 }
285 285 }
286 286 return 0;
287 287 }
288 288
289 289 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
290 290 {
291 291 Q_ASSERT(max>=min);
292 292
293 293 QStringList labels;
294 294
295 int ticks = axis->ticksCount();
295 int ticks = axis->ticksCount()-1;
296 296
297 297 for(int i=0; i<= ticks; i++){
298 298 qreal value = min + (i * (max - min)/ ticks);
299 299 QString label = axis->axisTickLabel(value);
300 300 if(label.isEmpty()){
301 301 labels << QString::number(value);
302 302 }else{
303 303 labels << label;
304 304 }
305 305 }
306 306 return labels;
307 307 }
308 308
309 309
310 310 void ChartDataSet::handleMinChanged(qreal min)
311 311 {
312 312
313 313 }
314 314
315 315 void ChartDataSet::handleMaxChanged(qreal max)
316 316 {
317 317
318 318 }
319 319
320 320 void ChartDataSet::handleTickChanged(QChartAxis* axis)
321 321 {
322 322 Domain domain = m_domainMap.value(axisY());
323 323 if(axis==axisX()){
324 324 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
325 325 emit axisLabelsChanged(axis,labels);
326 326 }else{
327 327 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
328 328 emit axisLabelsChanged(axis,labels);
329 329 }
330 330 }
331 331
332 332 #include "moc_chartdataset_p.cpp"
333 333
334 334 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,223 +1,223
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5 #include "charttheme_p.h"
6 6 //series
7 7 #include "qbarchartseries.h"
8 8 #include "qstackedbarchartseries.h"
9 9 #include "qpercentbarchartseries.h"
10 10 #include "qlinechartseries.h"
11 11 #include "qpieseries.h"
12 12 #include "qscatterseries.h"
13 13 //items
14 14 #include "axisitem_p.h"
15 15 #include "barpresenter.h"
16 16 #include "stackedbarpresenter.h"
17 17 #include "linechartitem_p.h"
18 18 #include "percentbarpresenter.h"
19 19 #include "linechartanimationitem_p.h"
20 20 #include "piepresenter.h"
21 21 #include "scatterpresenter_p.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 26 m_chart(chart),
27 27 m_dataset(dataset),
28 28 m_chartTheme(0),
29 29 m_marginSize(0),
30 30 m_rect(QRectF(QPoint(0,0),m_chart->size()))
31 31 {
32 32 createConnections();
33 33 setChartTheme(QChart::ChartThemeDefault);
34 34
35 35 }
36 36
37 37 ChartPresenter::~ChartPresenter()
38 38 {
39 39 }
40 40
41 41 void ChartPresenter::createConnections()
42 42 {
43 43 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
44 44 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
45 45 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*)));
46 46 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
47 47 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
48 48 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&)));
49 49 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
50 50 }
51 51
52 52
53 53 QRectF ChartPresenter::geometry() const
54 54 {
55 55 return m_rect;
56 56 }
57 57
58 58 void ChartPresenter::handleGeometryChanged()
59 59 {
60 60 m_rect = QRectF(QPoint(0,0),m_chart->size());
61 61 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
62 62 Q_ASSERT(m_rect.isValid());
63 63 emit geometryChanged(m_rect);
64 64 }
65 65
66 66 int ChartPresenter::margin() const
67 67 {
68 68 return m_marginSize;
69 69 }
70 70
71 71 void ChartPresenter::setMargin(int margin)
72 72 {
73 73 m_marginSize = margin;
74 74 }
75 75
76 76 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
77 77 {
78 78 AxisItem* item ;
79 79
80 80 if(axis==m_dataset->axisX()){
81 81 item = new AxisItem(AxisItem::X_AXIS,m_chart);
82 82 }else{
83 83 item = new AxisItem(AxisItem::Y_AXIS,m_chart);
84 84 }
85 85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
86 86 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
87 87 m_chartTheme->decorate(axis,item);
88 88 m_axisItems.insert(axis,item);
89 89 }
90 90
91 91 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
92 92 {
93 93 AxisItem* item = m_axisItems.take(axis);
94 94 Q_ASSERT(item);
95 95 delete item;
96 96 }
97 97
98 98
99 99 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
100 100 {
101 101 switch(series->type())
102 102 {
103 103 case QChartSeries::SeriesTypeLine: {
104 104 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
105 105 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
106 106 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
107 107 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
108 108 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
109 109 m_chartItems.insert(series,item);
110 110 break;
111 111 }
112 112
113 113 case QChartSeries::SeriesTypeBar: {
114 114 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
115 115 BarPresenter* item = new BarPresenter(barSeries->model(),m_chart);
116 116 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
117 117 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
118 118 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
119 119 m_chartItems.insert(series,item);
120 120 // m_axisXItem->setVisible(false);
121 121 break;
122 122 }
123 123
124 124 case QChartSeries::SeriesTypeStackedBar: {
125 125
126 126 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
127 127 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries->model(),m_chart);
128 128 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
129 129 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
130 130 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
131 131 m_chartItems.insert(series,item);
132 132 break;
133 133 }
134 134
135 135 case QChartSeries::SeriesTypePercentBar: {
136 136
137 137 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
138 138 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries->model(),m_chart);
139 139 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
140 140 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
141 141 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
142 142 m_chartItems.insert(series,item);
143 143 break;
144 144 }
145 145 case QChartSeries::SeriesTypeScatter: {
146 146 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
147 147 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
148 148 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
149 149 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
150 150 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
151 151 m_chartItems.insert(scatterSeries, scatterPresenter);
152 152 break;
153 153 }
154 154 case QChartSeries::SeriesTypePie: {
155 155 QPieSeries *s = qobject_cast<QPieSeries *>(series);
156 156 PiePresenter* pie = new PiePresenter(m_chart, s);
157 157 m_chartTheme->decorate(pie, s, m_chartItems.count());
158 158 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
159 159 m_chartItems.insert(series, pie);
160 160 break;
161 161 }
162 162 default: {
163 163 qDebug()<< "Series type" << series->type() << "not implemented.";
164 164 break;
165 165 }
166 166 }
167 167
168 168 if(m_rect.isValid()) emit geometryChanged(m_rect);
169 169 }
170 170
171 171 void ChartPresenter::handleSeriesRemoved(QChartSeries* series)
172 172 {
173 173 ChartItem* item = m_chartItems.take(series);
174 174 delete item;
175 175 }
176 176
177 177 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
178 178 {
179 179 //TODO:
180 180 }
181 181
182 182 void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain)
183 183 {
184 184 m_chartItems.value(series)->handleDomainChanged(domain);
185 185 }
186 186
187 187 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
188 188 {
189 m_axisItems.value(axis)->handleLabelsChanged(labels);
189 m_axisItems.value(axis)->handleLabelsChanged(axis,labels);
190 190 }
191 191
192 192 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
193 193 {
194 194 delete m_chartTheme;
195 195
196 196 m_chartTheme = ChartTheme::createTheme(theme);
197 197
198 198 m_chartTheme->decorate(m_chart);
199 199 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
200 200
201 201 int index=0;
202 202 while (i.hasNext()) {
203 203 i.next();
204 204 index++;
205 205 m_chartTheme->decorate(i.value(),i.key(),index);
206 206 }
207 207
208 208 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
209 209 while (j.hasNext()) {
210 210 j.next();
211 211 m_chartTheme->decorate(j.key(),j.value());
212 212 }
213 213 }
214 214
215 215 QChart::ChartTheme ChartPresenter::chartTheme()
216 216 {
217 217 return m_chartTheme->id();
218 218 }
219 219
220 220
221 221 #include "moc_chartpresenter_p.cpp"
222 222
223 223 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,154 +1,154
1 1 #include "qchartaxis.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
6 6 m_axisVisible(true),
7 7 m_gridVisible(true),
8 8 m_labelsVisible(true),
9 9 m_labelsAngle(0),
10 10 m_shadesVisible(true),
11 11 m_shadesOpacity(1.0),
12 12 m_min(0),
13 13 m_max(0),
14 m_ticksCount(4)
14 m_ticksCount(5)
15 15 {
16 16
17 17 }
18 18
19 19 QChartAxis::~QChartAxis()
20 20 {
21 21 }
22 22
23 23 void QChartAxis::setAxisPen(const QPen& pen)
24 24 {
25 25 m_axisPen=pen;
26 26 emit update(this);
27 27 }
28 28
29 29 void QChartAxis::setAxisVisible(bool visible)
30 30 {
31 31 m_axisVisible=visible;
32 32 emit update(this);
33 33 }
34 34
35 35 void QChartAxis::setGridVisible(bool visible)
36 36 {
37 37 m_gridVisible=visible;
38 38 emit update(this);
39 39 }
40 40
41 41 void QChartAxis::setGridPen(const QPen& pen)
42 42 {
43 43 m_gridPen=pen;
44 44 emit update(this);
45 45 }
46 46
47 47 void QChartAxis::setLabelsVisible(bool visible)
48 48 {
49 49 m_labelsVisible=visible;
50 50 emit update(this);
51 51 }
52 52
53 53 void QChartAxis::setLabelsPen(const QPen& pen)
54 54 {
55 55 m_labelsPen=pen;
56 56 emit update(this);
57 57 }
58 58
59 59 void QChartAxis::setLabelsBrush(const QBrush& brush)
60 60 {
61 61 m_labelsBrush=brush;
62 62 emit update(this);
63 63 }
64 64
65 65 void QChartAxis::setLabelsFont(const QFont& font)
66 66 {
67 67 m_labelsFont=font;
68 68 emit update(this);
69 69 }
70 70
71 71 void QChartAxis::setLabelsAngle(int angle)
72 72 {
73 73 m_labelsAngle=angle;
74 74 emit update(this);
75 75 }
76 76
77 77 void QChartAxis::setShadesVisible(bool visible)
78 78 {
79 79 m_shadesVisible=visible;
80 80 emit update(this);
81 81 }
82 82
83 83 void QChartAxis::setShadesPen(const QPen& pen)
84 84 {
85 85 m_shadesPen=pen;
86 86 emit update(this);
87 87 }
88 88
89 89 void QChartAxis::setShadesBrush(const QBrush& brush)
90 90 {
91 91 m_shadesBrush=brush;
92 92 emit update(this);
93 93 }
94 94
95 95 void QChartAxis::setShadesOpacity(qreal opacity)
96 96 {
97 97 m_shadesOpacity=opacity;
98 98 emit update(this);
99 99 }
100 100
101 101 void QChartAxis::setMin(qreal min)
102 102 {
103 103 if(m_min!=min){
104 104 m_min=min;
105 105 emit minChanged(m_min);
106 106 }
107 107 }
108 108
109 109 void QChartAxis::setMax(qreal max)
110 110 {
111 111 if(m_max!=max){
112 112 m_max=max;
113 113 emit maxChanged(m_max);
114 114 }
115 115 }
116 116
117 117 void QChartAxis::setRange(qreal min, qreal max)
118 118 {
119 119 setMin(min);
120 120 setMax(max);
121 121 }
122 122
123 123 void QChartAxis::setTicksCount(int count)
124 124 {
125 125 m_ticksCount=count;
126 126 emit ticksChanged(this);
127 127 }
128 128
129 129 void QChartAxis::addAxisTickLabel(qreal value,const QString& label)
130 130 {
131 131 m_ticks.insert(value,label);
132 132 emit ticksChanged(this);
133 133 }
134 134
135 135 void QChartAxis::removeAxisTickLabel(qreal value)
136 136 {
137 137 m_ticks.remove(value);
138 138 emit ticksChanged(this);
139 139 }
140 140
141 141 QString QChartAxis::axisTickLabel(qreal value) const
142 142 {
143 143 return m_ticks.value(value);
144 144 }
145 145
146 146 void QChartAxis::clearAxisTickLabels()
147 147 {
148 148 m_ticks.clear();
149 149 emit ticksChanged(this);
150 150 }
151 151
152 152 #include "moc_qchartaxis.cpp"
153 153
154 154 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now