##// END OF EJS Templates
Refactors axis handling...
Michal Klocek -
r223:c82178b4ef05
parent child
Show More
@@ -1,320 +1,299
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 m_ticks(4),
13 12 m_type(type),
14 13 m_labelsAngle(0),
15 14 m_shadesEnabled(true),
16 15 m_grid(parent),
17 16 m_shades(parent),
18 17 m_labels(parent)
19 18 {
20 19 //initial initialization
21 20 m_shades.setZValue(0);
22 21 m_grid.setZValue(2);
23 createItems();
24 22 }
25 23
26 24 AxisItem::~AxisItem()
27 25 {
28 26 }
29 27
30 28 QRectF AxisItem::boundingRect() const
31 29 {
32 30 return m_rect;
33 31 }
34 32
35 void AxisItem::createItems()
33 void AxisItem::createItems(int count)
36 34 {
37 for (int i = 0; i <= m_ticks; ++i) {
35 for (int i = 0; i < count; ++i) {
38 36 m_grid.addToGroup(new QGraphicsLineItem(this));
39 37 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
40 38 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
41 39 }
42 40 }
43 41
44 42 void AxisItem::clear()
45 43 {
46 44 foreach(QGraphicsItem* item , m_shades.childItems()) {
47 45 delete item;
48 46 }
49 47
50 48 foreach(QGraphicsItem* item , m_grid.childItems()) {
51 49 delete item;
52 50 }
53 51
54 52 foreach(QGraphicsItem* item , m_labels.childItems()) {
55 53 delete item;
56 54 }
57 55
56 m_thicksList.clear();
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 void AxisItem::updateDomain()
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 const qreal deltaX = m_rect.width() / m_ticks;
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 for (int i = 0; i <= m_ticks; ++i) {
81
80 for (int i = 0; i < count; ++i) {
82 81 int x = i * deltaX + m_rect.left();
83
84 qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks);
85
86 82 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
87 83 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
88
89 84 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
90 labelItem->setText(QString::number(label));
85 labelItem->setText(m_thicksList.at(i));
91 86 QPointF center = labelItem->boundingRect().center();
92 87 labelItem->setTransformOriginPoint(center.x(), center.y());
93 88 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
94 89
95 90 if(i%2){
96 91 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
97 92 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
98 93 rectItem->setOpacity( 0.5 );
99 94 }
100 95 }
101 96 }
102 97 break;
103 98
104 99 case Y_AXIS:
105 100 {
106 const qreal deltaY = m_rect.height()/ m_ticks;
101 const qreal deltaY = m_rect.height()/ (count-1);
107 102
108 103 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
109 104
110 for (int i = 0; i <= m_ticks; ++i) {
111
105 for (int i = 0; i < count; ++i) {
112 106 int y = i * -deltaY + m_rect.bottom();
113
114 qreal label = m_domain.m_minY + (i * m_domain.spanY()/ m_ticks);
115
116 107 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
117 108 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
118
119 109 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
120 labelItem->setText(QString::number(label));
110 labelItem->setText(m_thicksList.at(i));
121 111 QPointF center = labelItem->boundingRect().center();
122 112 labelItem->setTransformOriginPoint(center.x(), center.y());
123 113 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
124
125
126 114 if(i%2){
127 115 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
128 116 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
129 117
130 118 }
131 119 }
132 120 }
133 121 break;
134 122 default:
135 123 qDebug()<<"Unknown axis type";
136 124 break;
137 125 }
138 126 }
139 127
140 void AxisItem::handleAxisChanged(const QChartAxis& axis)
128 void AxisItem::handleAxisUpdate(QChartAxis* axis)
141 129 {
142 if(axis.isAxisVisible()) {
130 if(axis->isAxisVisible()) {
143 131 setAxisOpacity(100);
144 132 }
145 133 else {
146 134 setAxisOpacity(0);
147 135 }
148 136
149 if(axis.isGridVisible()) {
137 if(axis->isGridVisible()) {
150 138 setGridOpacity(100);
151 139 }
152 140 else {
153 141 setGridOpacity(0);
154 142 }
155 143
156 if(axis.isLabelsVisible())
144 if(axis->isLabelsVisible())
157 145 {
158 146 setLabelsOpacity(100);
159 147 }
160 148 else {
161 149 setLabelsOpacity(0);
162 150 }
163 151
164 if(axis.isShadesVisible()) {
165 setShadesOpacity(axis.shadesOpacity());
152 if(axis->isShadesVisible()) {
153 setShadesOpacity(axis->shadesOpacity());
166 154 }
167 155 else {
168 156 setShadesOpacity(0);
169 157 }
170 158
171 switch(axis.labelsOrientation())
172 {
173 case QChartAxis::LabelsOrientationHorizontal:
174 setLabelsAngle(0);
175 break;
176 case QChartAxis::LabelsOrientationVertical:
177 setLabelsAngle(90);
178 break;
179 case QChartAxis::LabelsOrientationSlide:
180 setLabelsAngle(-45);
181 break;
182 default:
183 break;
184 }
185
186 setAxisPen(axis.axisPen());
187 setLabelsPen(axis.labelsPen());
188 setLabelsBrush(axis.labelsBrush());
189 setLabelsFont(axis.labelFont());
190 setGridPen(axis.gridPen());
191 setShadesPen(axis.shadesPen());
192 setShadesBrush(axis.shadesBrush());
193
159 setLabelsAngle(axis->labelsAngle());
160 setAxisPen(axis->axisPen());
161 setLabelsPen(axis->labelsPen());
162 setLabelsBrush(axis->labelsBrush());
163 setLabelsFont(axis->labelFont());
164 setGridPen(axis->gridPen());
165 setShadesPen(axis->shadesPen());
166 setShadesBrush(axis->shadesBrush());
194 167 }
195 168
196 void AxisItem::handleDomainChanged(const Domain& domain)
169 void AxisItem::handleLabelsChanged(const QStringList& labels)
197 170 {
198 m_domain = domain;
199 updateDomain();
171 m_thicksList=labels;
172 QList<QGraphicsItem*> items = m_labels.childItems();
173 if(items.size()!=m_thicksList.size()){
174 clear();
175 m_thicksList=labels;
176 createItems(m_thicksList.size());
177 items = m_labels.childItems();
178 }
179
180 Q_ASSERT(items.size()==m_thicksList.size());
181
182 int i=0;
183 foreach(QGraphicsItem* item, items){
184 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
185 i++;
186 }
200 187 update();
201 188 }
202 189
203 190 void AxisItem::handleGeometryChanged(const QRectF& rect)
204 191 {
205 192 m_rect = rect;
206 updateDomain();
193 updateItem(m_thicksList.size());
207 194 update();
208 195 }
209 196
210 197 void AxisItem::setAxisOpacity(qreal opacity)
211 198 {
212 199 m_axis.setOpacity(opacity);
213 200 }
214 201
215 202 qreal AxisItem::axisOpacity() const
216 203 {
217 204 return m_axis.opacity();
218 205 }
219 206
220 207 void AxisItem::setGridOpacity(qreal opacity)
221 208 {
222 209 m_grid.setOpacity(opacity);
223 210 }
224 211
225 212
226 213 qreal AxisItem::gridOpacity() const
227 214 {
228 215 return m_grid.opacity();
229 216 }
230 217
231 218 void AxisItem::setLabelsOpacity(qreal opacity)
232 219 {
233 220 m_labels.setOpacity(opacity);
234 221 }
235 222
236 223 qreal AxisItem::labelsOpacity() const
237 224 {
238 225 return m_labels.opacity();
239 226 }
240 227
241 228 void AxisItem::setShadesOpacity(qreal opacity)
242 229 {
243 230 m_shades.setOpacity(opacity);
244 231 }
245 232
246 233 qreal AxisItem::shadesOpacity() const
247 234 {
248 235 return m_shades.opacity();
249 236 }
250 237
251 238 void AxisItem::setLabelsAngle(int angle)
252 239 {
253 240 foreach(QGraphicsItem* item , m_labels.childItems()) {
254 241 QPointF center = item->boundingRect().center();
255 242 item->setRotation(angle);
256 243 }
257 244
258 245 m_labelsAngle=angle;
259 246 }
260 247
261 248 void AxisItem::setLabelsPen(const QPen& pen)
262 249 {
263 250 foreach(QGraphicsItem* item , m_labels.childItems()) {
264 251 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
265 252 }
266 253 }
267 254
268 255 void AxisItem::setLabelsBrush(const QBrush& brush)
269 256 {
270 257 foreach(QGraphicsItem* item , m_labels.childItems()) {
271 258 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
272 259 }
273 260 }
274 261
275 262 void AxisItem::setLabelsFont(const QFont& font)
276 263 {
277 264 foreach(QGraphicsItem* item , m_labels.childItems()) {
278 265 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
279 266 }
280 267 }
281 268
282 269 void AxisItem::setShadesBrush(const QBrush& brush)
283 270 {
284 271 foreach(QGraphicsItem* item , m_shades.childItems()) {
285 272 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
286 273 }
287 274 }
288 275
289 276 void AxisItem::setShadesPen(const QPen& pen)
290 277 {
291 278 foreach(QGraphicsItem* item , m_shades.childItems()) {
292 279 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
293 280 }
294 281 }
295 282
296 283 void AxisItem::setAxisPen(const QPen& pen)
297 284 {
298 285 m_axis.setPen(pen);
299 286 }
300 287
301 288 void AxisItem::setGridPen(const QPen& pen)
302 289 {
303 290 foreach(QGraphicsItem* item , m_grid.childItems()) {
304 291 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
305 292 }
306 293 }
307 294
308 void AxisItem::setTicks(int count)
309 {
310 if(count!=m_ticks){
311 clear();
312 m_ticks=count;
313 createItems();
314 }
315 }
316 295
317 296 //TODO "nice numbers algorithm"
318 297 #include "moc_axisitem_p.cpp"
319 298
320 299 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,80 +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 void setTicks(int count);
43 int ticks() const { return m_ticks;}
44
45 42 void setShadesBrush(const QBrush& brush);
46 43 void setShadesPen(const QPen& pen);
47 44
48 45 void setAxisPen(const QPen& pen);
49 46 void setGridPen(const QPen& pen);
50 47
51 48 void setLabelsPen(const QPen& pen);
52 49 void setLabelsBrush(const QBrush& brush);
53 50 void setLabelsFont(const QFont& font);
54 51
55 52 public slots:
56 void handleAxisChanged(const QChartAxis& axis);
57 void handleDomainChanged(const Domain& domain);
53 void handleAxisUpdate(QChartAxis* axis);
54 void handleLabelsChanged(const QStringList& labels);
58 55 void handleGeometryChanged(const QRectF& size);
59 56 protected:
60 void updateDomain();
57 void updateItem(int count);
61 58 private:
62 59 void clear();
63 void createItems();
60 void createItems(int count);
64 61 private:
65 62 AxisType m_type;
66 int m_ticks;
67 Domain m_domain;
68 63 QRectF m_rect;
69 64 int m_labelsAngle;
70 65 bool m_shadesEnabled;
71 66 QGraphicsItemGroup m_grid;
72 67 QGraphicsItemGroup m_shades;
73 68 QGraphicsItemGroup m_labels;
74 69 QGraphicsLineItem m_axis;
70 QStringList m_thicksList;
75 71
76 72 };
77 73
78 74 QTCOMMERCIALCHART_END_NAMESPACE
79 75
80 76 #endif /* AXISITEM_H_ */
@@ -1,163 +1,333
1 1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 3 //series
3 4 #include "qlinechartseries.h"
4 5 #include "qbarchartseries.h"
5 6 #include "qstackedbarchartseries.h"
6 7 #include "qpercentbarchartseries.h"
7 8 #include "qpieseries.h"
8 9 #include "qscatterseries.h"
9 10
10 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 12
12 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent)
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 m_axisX(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
16 m_domainIndex(0),
17 m_axisXInitialized(false)
13 18 {
14 Domain domain;
15 m_domains<<domain;
16 19 }
17 20
18 21 ChartDataSet::~ChartDataSet()
19 22 {
20 23 // TODO Auto-generated destructor stub
21 24 }
22 25
23 const Domain& ChartDataSet::domain() const
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
24 27 {
25 return m_domains[m_domainIndex];
28 int i = m_domainMap.count(axisY);
29 if(i == 0){
30 return Domain();
31 }
32 i = i - m_domainIndex -1;
33 return m_domainMap.values(axisY).at(i);
26 34 }
27 35
28 void ChartDataSet::addSeries(QChartSeries* series)
36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
29 37 {
30 38 // TODO: we should check the series not already added
31 m_chartSeries << series;
32 39 series->setParent(this); // take ownership
33 m_domainIndex = 0;
34 m_domains.resize(1);
35 40
36 Domain& domain = m_domains[m_domainIndex];
41 clearDomains();
42
43 if(axisY==0) axisY = m_axisY;
44
45 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
46
47 QList<Domain> domainList = m_domainMap.values(axisY);
48
49 Q_ASSERT(domainList.size()<=1);
50
51 Domain domain;
52
53 if(domainList.size()>0) domain = domainList.at(0);
37 54
38 55 switch(series->type())
39 56 {
40 57 case QChartSeries::SeriesTypeLine: {
41 58
42 59 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
43 60
44 61 for (int i = 0; i < xyseries->count(); i++)
45 62 {
46 63 qreal x = xyseries->x(i);
47 64 qreal y = xyseries->y(i);
48 65 domain.m_minX = qMin(domain.m_minX,x);
49 66 domain.m_minY = qMin(domain.m_minY,y);
50 67 domain.m_maxX = qMax(domain.m_maxX,x);
51 68 domain.m_maxY = qMax(domain.m_maxY,y);
52 69 }
53 70 break;
54 71 }
55 72 case QChartSeries::SeriesTypeBar: {
56 73 qDebug() << "QChartSeries::SeriesTypeBar";
57 74 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
58 75 qreal x = barSeries->countCategories();
59 76 qreal y = barSeries->max();
60 77 domain.m_minX = qMin(domain.m_minX,x);
61 78 domain.m_minY = qMin(domain.m_minY,y);
62 79 domain.m_maxX = qMax(domain.m_maxX,x);
63 80 domain.m_maxY = qMax(domain.m_maxY,y);
64 81 break;
65 82 }
66 83 case QChartSeries::SeriesTypeStackedBar: {
67 84 qDebug() << "QChartSeries::SeriesTypeStackedBar";
68 85
69 86 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
70 87 qreal x = stackedBarSeries->countCategories();
71 88 qreal y = stackedBarSeries->maxCategorySum();
72 89 domain.m_minX = qMin(domain.m_minX,x);
73 90 domain.m_minY = qMin(domain.m_minY,y);
74 91 domain.m_maxX = qMax(domain.m_maxX,x);
75 92 domain.m_maxY = qMax(domain.m_maxY,y);
76 93 break;
77 94 }
78 95 case QChartSeries::SeriesTypePercentBar: {
79 96 qDebug() << "QChartSeries::SeriesTypePercentBar";
80 97
81 98 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
82 99 qreal x = percentBarSeries->countCategories();
83 100 domain.m_minX = qMin(domain.m_minX,x);
84 101 domain.m_minY = 0;
85 102 domain.m_maxX = qMax(domain.m_maxX,x);
86 103 domain.m_maxY = 100;
87 104 break;
88 105 }
89 106
90 107 case QChartSeries::SeriesTypePie: {
91 108 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
92 109 // TODO: domain stuff
93 110 break;
94 111 }
95 112
96 113 case QChartSeries::SeriesTypeScatter: {
97 114 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
98 115 Q_ASSERT(scatterSeries);
99 116 foreach (QPointF point, scatterSeries->data()) {
100 117 domain.m_minX = qMin(domain.m_minX, point.x());
101 118 domain.m_maxX = qMax(domain.m_maxX, point.x());
102 119 domain.m_minY = qMin(domain.m_minY, point.y());
103 120 domain.m_maxY = qMax(domain.m_maxY, point.y());
104 121 }
105 122 break;
106 123 }
107 124
108 125 default: {
109 126 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
110 127 return;
111 128 break;
112 129 }
113 130
114 131 }
115 132
133 if(!m_domainMap.contains(axisY))
134 {
135 emit axisAdded(axisY);
136 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
137 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
139 }
140 m_domainMap.replace(axisY,domain);
141 m_seriesMap.insert(axisY,series);
142
143 if(!m_axisXInitialized)
144 {
145 emit axisAdded(axisX());
146 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
147 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
149 m_axisXInitialized=true;
150 }
151
152
116 153 emit seriesAdded(series);
117 emit domainChanged(domain);
154 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
155 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
156 emit axisLabelsChanged(axisY,ylabels);
157 emit axisLabelsChanged(axisX(),xlabels);
158 emit seriesDomainChanged(series,domain);
159
160 }
161
162 void ChartDataSet::removeSeries(QChartSeries* series)
163 {
164 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
165 foreach(QChartAxis* axis , keys) {
166 if(m_seriesMap.contains(axis,series)){
167 emit seriesRemoved(series);
168 m_seriesMap.remove(axis,series);
169 //remove axis if no longer there
170 if(!m_seriesMap.contains(axis) && axis != m_axisY){
171 emit axisRemoved(axis);
172 m_domainMap.remove(axis);
173 delete axis;
174 }
175 break;
176 }
177 }
118 178 }
119 179
120 180 bool ChartDataSet::nextDomain()
121 181 {
122 if (m_domainIndex < m_domains.count() - 1) {
182 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
183
184 if (m_domainIndex < limit) {
123 185 m_domainIndex++;
124 emit domainChanged(m_domains[m_domainIndex]);
186 setDomain(m_domainIndex);
125 187 return true;
126 188 }
127 189 else {
128 190 return false;
129 191 }
130 192 }
131 193
132 194 bool ChartDataSet::previousDomain()
133 195 {
134 196 if (m_domainIndex > 0) {
135 197 m_domainIndex--;
136 emit domainChanged(m_domains[m_domainIndex]);
198 setDomain(m_domainIndex);
137 199 return true;
138 200 }
139 201 else {
140 202 return false;
141 203 }
142 204 }
143 205
144 void ChartDataSet::clearDomains()
206 void ChartDataSet::setDomain(int index)
145 207 {
146 if (m_domainIndex > 0) {
147 m_domainIndex = 0;
148 emit domainChanged(m_domains[m_domainIndex]);
208 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
209 foreach (QChartAxis* axis , domainList) {
210 int i = m_domainMap.count(axis) - index -1;
211 Q_ASSERT(i>=0);
212 Domain domain = m_domainMap.values(axis).at(i);
213 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
214 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
215 foreach(QChartSeries* series, seriesList) {
216 emit seriesDomainChanged(series,domain);
217 }
218 emit axisLabelsChanged(axis,labels);
219 }
220
221 Domain domain = m_domainMap.value(axisY());
222 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
223 emit axisLabelsChanged(axisX(),labels);
224 }
225
226 void ChartDataSet::clearDomains(int toIndex)
227 {
228 Q_ASSERT(toIndex>=0);
229
230 m_domainIndex = toIndex;
231
232 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
233
234 foreach (QChartAxis* key , keys)
235 {
236 QList<Domain> domains = m_domainMap.values(key);
237 m_domainMap.remove(key);
238 int i = domains.size() - toIndex - 1;
239 while(i--){
240 domains.removeFirst();
241 }
242 for(int j=domains.size()-1; j>=0 ;j--)
243 m_domainMap.insert(key,domains.at(j));
149 244 }
150 245 }
151 246
152 void ChartDataSet::addDomain(const Domain& domain)
247 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
153 248 {
154 m_domains.resize(m_domainIndex + 1);
155 m_domains << domain;
249 Q_ASSERT(rect.isValid());
250 Q_ASSERT(viewport.isValid());
251
252 clearDomains(m_domainIndex);
253
254 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
255
256 foreach (QChartAxis* axis , domainList){
257 Domain domain(m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height()));
258 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
259 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
260 foreach(QChartSeries* series, seriesList){
261 emit seriesDomainChanged(series,domain);
262 }
263 emit axisLabelsChanged(axis,labels);
264 m_domainMap.insert(axis,domain);
265 }
266
267 Domain domain = m_domainMap.value(axisY());
268 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
269 emit axisLabelsChanged(axisX(),labels);
270
156 271 m_domainIndex++;
272 }
273
274 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
275 {
276 if(series == 0) return m_axisY;
277
278 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
279
280 foreach(QChartAxis* axis , keys) {
281 if(m_seriesMap.contains(axis,series)){
282 return axis;
283 }
284 }
285 return 0;
286 }
287
288 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
289 {
290 Q_ASSERT(max>=min);
157 291
158 emit domainChanged(domain);
292 QStringList labels;
293
294 int ticks = axis->ticksCount();
295
296 for(int i=0; i<= ticks; i++){
297 qreal value = min + (i * (max - min)/ ticks);
298 QString label = axis->axisTickLabel(value);
299 if(label.isEmpty()){
300 labels << QString::number(value);
301 }else{
302 labels << label;
303 }
304 }
305 return labels;
306 }
307
308
309 void ChartDataSet::handleMinChanged(qreal min)
310 {
311
312 }
313
314 void ChartDataSet::handleMaxChanged(qreal max)
315 {
316
317 }
318
319 void ChartDataSet::handleTickChanged(QChartAxis* axis)
320 {
321 Domain domain = m_domainMap.value(axisY());
322 if(axis==axisX()){
323 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
324 emit axisLabelsChanged(axis,labels);
325 }else{
326 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
327 emit axisLabelsChanged(axis,labels);
328 }
159 329 }
160 330
161 331 #include "moc_chartdataset_p.cpp"
162 332
163 333 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,36 +1,59
1 #ifndef CHARTENGINE_P_H_
2 #define CHARTENGINE_P_H_
1 #ifndef CHARTDATASET_P_H_
2 #define CHARTDATASET_P_H_
3 3
4 #include <qchartseries.h>
4 #include "qchartseries.h"
5 5 #include "domain_p.h"
6 6 #include <QVector>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 class QChartAxis;
11
10 12 class ChartDataSet : public QObject
11 13 {
12 14 Q_OBJECT
13 15 public:
14 ChartDataSet(QObject* parent =0);
16 ChartDataSet(QObject* parent=0);
15 17 virtual ~ChartDataSet();
16 18
17 void addSeries(QChartSeries* series);
18 void addDomain(const Domain& domain);
19 void addSeries(QChartSeries* series,QChartAxis *axisY = 0);
20 void removeSeries(QChartSeries* series);
21 void addDomain(const QRectF& rect, const QRectF& viewport);
19 22 bool nextDomain();
20 23 bool previousDomain();
21 void clearDomains();
22 const Domain& domain() const;
24 void clearDomains(int toIndex =0);
25 const Domain domain(QChartAxis *axisY) const;
26 int domainIndex() const {return m_domainIndex;}
27
28 QChartAxis* axisX() const { return m_axisX;};
29 QChartAxis* axisY(QChartSeries* series = 0) const;
23 30
24 31 signals:
25 32 void seriesAdded(QChartSeries* series);
26 void domainChanged(const Domain& domain);
33 void seriesRemoved(QChartSeries* series);
34 void axisAdded(QChartAxis* axis);
35 void axisRemoved(QChartAxis* axis);
36 void axisLabelsChanged(QChartAxis* axis, const QStringList& labels);
37 void seriesDomainChanged(QChartSeries* series,const Domain& domain);
38
39 private slots:
40 void handleMinChanged(qreal min);
41 void handleMaxChanged(qreal max);
42 void handleTickChanged(QChartAxis*);
43
44 private:
45 void setDomain(int index);
46 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
27 47
28 48 private:
29 QList<QChartSeries*> m_chartSeries;
30 QVector<Domain> m_domains;
49 QMultiMap<QChartAxis*, Domain> m_domainMap;
50 QMultiMap<QChartAxis*, QChartSeries*> m_seriesMap;
51 QChartAxis* m_axisX;
52 QChartAxis* m_axisY;
31 53 int m_domainIndex;
54 bool m_axisXInitialized;
32 55 };
33 56
34 57 QTCOMMERCIALCHART_END_NAMESPACE
35 58
36 59 #endif /* CHARTENGINE_P_H_ */
@@ -1,21 +1,24
1 1 #ifndef CHARTITEM_H_
2 2 #define CHARTITEM_H_
3 3
4 4 #include "domain_p.h"
5 5 #include <QGraphicsItem>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 class ChartAnimationManager;
10 10
11 11 class ChartItem : public QGraphicsItem
12 12 {
13 13 enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM};
14 14 public:
15 15 ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent){};
16 //TODO make pure
17 virtual void handleGeometryChanged(const QRectF&){};
18 virtual void handleDomainChanged(const Domain& domain){};
16 19 virtual ~ChartItem(){};
17 20 };
18 21
19 22 QTCOMMERCIALCHART_END_NAMESPACE
20 23
21 24 #endif /* CHARTITEM_H_ */
@@ -1,269 +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 m_axisXItem(new AxisItem(AxisItem::X_AXIS,m_chart)),
30 m_axisYItem(new AxisItem(AxisItem::Y_AXIS,m_chart)),
31 m_domainIndex(0),
32 29 m_marginSize(0),
33 30 m_rect(QRectF(QPoint(0,0),m_chart->size()))
34 31 {
32 createConnections();
35 33 setChartTheme(QChart::ChartThemeDefault);
36 createConnections();
34
37 35 }
38 36
39 37 ChartPresenter::~ChartPresenter()
40 38 {
41 39 }
42 40
43 41 void ChartPresenter::createConnections()
44 42 {
45 43 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
46 44 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
47 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisXItem,SLOT(handleGeometryChanged(const QRectF&)));
48 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisXItem,SLOT(handleDomainChanged(const Domain&)));
49 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisYItem,SLOT(handleGeometryChanged(const QRectF&)));
50 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisYItem,SLOT(handleDomainChanged(const Domain&)));
45 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*)));
46 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
47 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
48 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&)));
49 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
50 }
51
52
53 QRectF ChartPresenter::geometry() const
54 {
55 return m_rect;
51 56 }
52 57
53 58 void ChartPresenter::handleGeometryChanged()
54 59 {
55 60 m_rect = QRectF(QPoint(0,0),m_chart->size());
56 61 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
57 62 Q_ASSERT(m_rect.isValid());
58 63 emit geometryChanged(m_rect);
59 64 }
60 65
61 66 int ChartPresenter::margin() const
62 67 {
63 68 return m_marginSize;
64 69 }
65 70
66 71 void ChartPresenter::setMargin(int margin)
67 72 {
68 73 m_marginSize = margin;
69 74 }
70 75
76 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
77 {
78 AxisItem* item ;
79
80 if(axis==m_dataset->axisX()){
81 item = new AxisItem(AxisItem::X_AXIS,m_chart);
82 }else{
83 item = new AxisItem(AxisItem::Y_AXIS,m_chart);
84 }
85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
86 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
87 m_chartTheme->decorate(axis,item);
88 m_axisItems.insert(axis,item);
89 }
90
91 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
92 {
93 AxisItem* item = m_axisItems.take(axis);
94 Q_ASSERT(item);
95 delete item;
96 }
97
98
71 99 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
72 100 {
73 qDebug() << " ChartPresenter::handleSeriesAdded";
74 101 switch(series->type())
75 102 {
76 103 case QChartSeries::SeriesTypeLine: {
77 104 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
78 105 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
79 106 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
80 107 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
81 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
82 108 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
83 109 m_chartItems.insert(series,item);
84 110 break;
85 111 }
86 112
87 113 case QChartSeries::SeriesTypeBar: {
88 114 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
89 115 BarPresenter* item = new BarPresenter(barSeries->model(),m_chart);
90 116 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
91 117 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
92 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
93 118 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
94 119 m_chartItems.insert(series,item);
95 120 // m_axisXItem->setVisible(false);
96 121 break;
97 122 }
98 123
99 124 case QChartSeries::SeriesTypeStackedBar: {
100 125
101 126 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
102 127 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries->model(),m_chart);
103 128 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
104 129 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
105 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
106 130 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
107 131 m_chartItems.insert(series,item);
108 132 break;
109 133 }
110 134
111 135 case QChartSeries::SeriesTypePercentBar: {
112 136
113 137 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
114 138 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries->model(),m_chart);
115 139 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
116 140 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
117 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
118 141 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
119 142 m_chartItems.insert(series,item);
120 143 break;
121 144 }
122 145 case QChartSeries::SeriesTypeScatter: {
123 146 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
124 147 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
125 148 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
126 149 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
127 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
128 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
129 150 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
130 151 m_chartItems.insert(scatterSeries, scatterPresenter);
131 152 break;
132 153 }
133 154 case QChartSeries::SeriesTypePie: {
134 155 QPieSeries *s = qobject_cast<QPieSeries *>(series);
135 156 PiePresenter* pie = new PiePresenter(m_chart, s);
136 157 m_chartTheme->decorate(pie, s, m_chartItems.count());
137 158 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
138 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
139 159 m_chartItems.insert(series, pie);
140 160 break;
141 161 }
142 162 default: {
143 163 qDebug()<< "Series type" << series->type() << "not implemented.";
144 164 break;
145 165 }
146 166 }
147 167
148 168 if(m_rect.isValid()) emit geometryChanged(m_rect);
149 169 }
150 170
151 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
152 {
153 //TODO:
154 }
155
156 void ChartPresenter::zoomInToRect(const QRectF& rect)
171 void ChartPresenter::handleSeriesRemoved(QChartSeries* series)
157 172 {
158 if(!rect.isValid()) return;
159 QRectF r = rect.normalized();
160 r.translate(-m_marginSize, -m_marginSize);
161 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
162 m_dataset->addDomain(domain);
173 ChartItem* item = m_chartItems.take(series);
174 delete item;
163 175 }
164 176
165 void ChartPresenter::zoomIn()
177 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
166 178 {
167 if (!m_dataset->nextDomain()) {
168 QRectF rect = m_rect;
169 rect.setWidth(rect.width()/2);
170 rect.setHeight(rect.height()/2);
171 rect.moveCenter(m_rect.center());
172 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
173 m_dataset->addDomain(domain);
174 }
179 //TODO:
175 180 }
176 181
177 void ChartPresenter::zoomOut()
182 void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain)
178 183 {
179 m_dataset->previousDomain();
184 m_chartItems.value(series)->handleDomainChanged(domain);
180 185 }
181 186
182 void ChartPresenter::zoomReset()
187 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
183 188 {
184 m_dataset->clearDomains();
189 m_axisItems.value(axis)->handleLabelsChanged(labels);
185 190 }
186 191
187 192 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
188 193 {
189 194 delete m_chartTheme;
190 195
191 196 m_chartTheme = ChartTheme::createTheme(theme);
192 197
193 198 m_chartTheme->decorate(m_chart);
194 199 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
195 200
196 201 int index=0;
197 202 while (i.hasNext()) {
198 203 i.next();
199 204 index++;
200 205 m_chartTheme->decorate(i.value(),i.key(),index);
201 206 }
202 207
203 m_chartTheme->decorate(m_axisX, m_axisXItem);
204 m_chartTheme->decorate(m_axisY, m_axisYItem);
205
208 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
209 while (j.hasNext()) {
210 j.next();
211 m_chartTheme->decorate(j.key(),j.value());
212 }
206 213 }
207 214
208
209 215 QChart::ChartTheme ChartPresenter::chartTheme()
210 216 {
211 217 return m_chartTheme->id();
212 218 }
213 219
214 void ChartPresenter::setDefaultAxisX(const QChartAxis& axis)
215 {
216 //if(m_axisX != axis) {
217 m_axisX = axis;
218 m_axisXItem->handleAxisChanged(m_axisX);
219 //}
220 }
221
222 void ChartPresenter::setDefaultAxisY(const QChartAxis& axis)
223 {
224 // if(m_axisY != axis) {
225 m_axisY = axis;
226 m_axisYItem->handleAxisChanged(m_axisY);
227 //}
228 }
229
230 QChartAxis ChartPresenter::defaultAxisX() const
231 {
232 return m_axisX;
233 }
234
235 QChartAxis ChartPresenter::defaultAxisY() const
236 {
237 return m_axisY;
238 }
239
240 QChartAxis ChartPresenter::axisY(int id) const
241 {
242 return m_axis.value(id);
243 }
244
245 int ChartPresenter::addAxisY(const QChartAxis& axis)
246 {
247 int key =0 ;
248
249 while(m_axis.contains(key)){
250 key++;
251 //TODO overflow
252 }
253
254 m_axis.insert(key,axis);
255 m_axisItems.insert(key,new AxisItem(AxisItem::Y_AXIS,m_chart));
256
257 return key;
258 }
259
260
261 void ChartPresenter::removeAxisY(int id)
262 {
263 m_axis.remove(id);
264 delete m_axisItems.take(id);
265 }
266 220
267 221 #include "moc_chartpresenter_p.cpp"
268 222
269 223 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,61
1 1 #ifndef CHARTPRESENTER_H_
2 2 #define CHARTPRESENTER_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 6 #include "qchartaxis.h"
7 7 #include <QRectF>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class ChartItem;
12 12 class QChartSeries;
13 13 class ChartDataSet;
14 14 //class QChart;
15 15 class Domain;
16 16 class AxisItem;
17 17 class ChartTheme;
18 18
19 19 class ChartPresenter: public QObject
20 20 {
21 21 Q_OBJECT
22 22 public:
23 23 ChartPresenter(QChart* chart,ChartDataSet *dataset);
24 24 virtual ~ChartPresenter();
25 25
26 26 void setMargin(int margin);
27 27 int margin() const;
28 28
29 void zoomInToRect(const QRectF& rectangle);
30 void zoomIn();
31 void zoomOut();
32 void zoomReset();
29 QRectF geometry() const;
33 30
34 31 void setChartTheme(QChart::ChartTheme theme);
35 32 QChart::ChartTheme chartTheme();
36 33
37 void setDefaultAxisX(const QChartAxis& axis);
38 void setDefaultAxisY(const QChartAxis& axis);
39 QChartAxis defaultAxisX() const;
40 QChartAxis defaultAxisY() const;
41 QChartAxis axisY(int id) const;
42 int addAxisY(const QChartAxis& axis);
43 void removeAxisY(int id);
44
45 34 private:
46 35 void createConnections();
47 36
48 37 public slots:
49 38 void handleSeriesAdded(QChartSeries* series);
50 void handleSeriesRemoved(QChartSeries* series){};
39 void handleSeriesRemoved(QChartSeries* series);
40 void handleAxisAdded(QChartAxis* axis);
41 void handleAxisRemoved(QChartAxis* axis);
42 void handleSeriesDomainChanged(QChartSeries* series, const Domain& domain);
43 void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels);
51 44 void handleSeriesChanged(QChartSeries* series);
52 //void handleDomainChanged(Domain oldDomain,Domain newDomain);
53 45 void handleGeometryChanged();
54 46 signals:
55 47 void geometryChanged(const QRectF& rect);
56 48 private:
57 49 QMap<QChartSeries*,ChartItem*> m_chartItems;
58 QMap<int,AxisItem*> m_axisItems;
59 QMap<int,QChartAxis> m_axis;
50 QMap<QChartAxis*,AxisItem*> m_axisItems;
60 51 QChart* m_chart;
61 52 ChartDataSet* m_dataset;
62 QVector<Domain> m_domains;
63 53 ChartTheme *m_chartTheme;
64 QChartAxis m_axisX;
65 AxisItem* m_axisXItem;
66 QChartAxis m_axisY;
67 AxisItem* m_axisYItem;
68 int m_domainIndex;
69 54 int m_marginSize;
70 55 QRectF m_rect;
71 56
72 57 };
73 58
74 59 QTCOMMERCIALCHART_END_NAMESPACE
75 60
76 61 #endif /* CHARTPRESENTER_H_ */
@@ -1,231 +1,229
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4
5 5
6 6 //series
7 7 #include "qbarset.h"
8 8 #include "qbarchartseries.h"
9 9 #include "qstackedbarchartseries.h"
10 10 #include "qpercentbarchartseries.h"
11 11 #include "qlinechartseries.h"
12 12 #include "qscatterseries.h"
13 13 #include "qpieseries.h"
14 14 #include "qpieslice.h"
15 15
16 16 //items
17 17 #include "axisitem_p.h"
18 18 #include "barpresenter.h"
19 19 #include "stackedbarpresenter.h"
20 20 #include "linechartitem_p.h"
21 21 #include "percentbarpresenter.h"
22 22 #include "scatterpresenter_p.h"
23 23 #include "piepresenter.h"
24 24
25 25 //themes
26 26 #include "chartthemevanilla_p.h"
27 27 #include "chartthemeicy_p.h"
28 28 #include "chartthemegrayscale_p.h"
29 29 #include "chartthemescientific_p.h"
30 30
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 /* TODO
35 35 case QChart::ChartThemeUnnamed1:
36 36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
37 37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
38 38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
39 39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
40 40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
41 41
42 42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
43 43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
44 44 */
45 45
46 46 ChartTheme::ChartTheme(QChart::ChartTheme id)
47 47 {
48 48 m_id = id;
49 49 m_seriesColor.append(QRgb(0xff000000));
50 50 m_seriesColor.append(QRgb(0xff707070));
51 51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
53 53 }
54 54
55 55
56 56 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
57 57 {
58 58 switch(theme) {
59 59 case QChart::ChartThemeDefault:
60 60 return new ChartTheme();
61 61 case QChart::ChartThemeVanilla:
62 62 return new ChartThemeVanilla();
63 63 case QChart::ChartThemeIcy:
64 64 return new ChartThemeIcy();
65 65 case QChart::ChartThemeGrayscale:
66 66 return new ChartThemeGrayscale();
67 67 case QChart::ChartThemeScientific:
68 68 return new ChartThemeScientific();
69 69 }
70 70 }
71 71
72 72 void ChartTheme::decorate(QChart* chart)
73 73 {
74 74 QLinearGradient backgroundGradient;
75 75 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
76 76 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
77 77 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
78 78 chart->setChartBackgroundBrush(backgroundGradient);
79 79 }
80 80 //TODO helper to by removed later
81 81 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
82 82 {
83 83 switch(series->type())
84 84 {
85 85 case QChartSeries::SeriesTypeLine: {
86 86 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
87 87 LineChartItem* i = static_cast<LineChartItem*>(item);
88 88 decorate(i,s,count);
89 89 break;
90 90 }
91 91 case QChartSeries::SeriesTypeBar: {
92 92 QBarChartSeries* b = static_cast<QBarChartSeries*>(series);
93 93 BarPresenter* i = static_cast<BarPresenter*>(item);
94 94 decorate(i,b,count);
95 95 break;
96 96 }
97 97 case QChartSeries::SeriesTypeStackedBar: {
98 98 QStackedBarChartSeries* s = static_cast<QStackedBarChartSeries*>(series);
99 99 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
100 100 decorate(i,s,count);
101 101 break;
102 102 }
103 103 case QChartSeries::SeriesTypePercentBar: {
104 104 QPercentBarChartSeries* s = static_cast<QPercentBarChartSeries*>(series);
105 105 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
106 106 decorate(i,s,count);
107 107 break;
108 108 }
109 109 case QChartSeries::SeriesTypeScatter: {
110 110 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
111 111 Q_ASSERT(s);
112 112 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
113 113 Q_ASSERT(i);
114 114 decorate(i, s, count);
115 115 break;
116 116 }
117 117 case QChartSeries::SeriesTypePie: {
118 118 QPieSeries* s = static_cast<QPieSeries*>(series);
119 119 PiePresenter* i = static_cast<PiePresenter*>(item);
120 120 decorate(i,s,count);
121 121 break;
122 122 }
123 123 default:
124 124 qDebug()<<"Wrong item to be decorated by theme";
125 125 break;
126 126 }
127 127
128 128 }
129 129
130 130 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
131 131 {
132 132 QPen pen;
133 133 if(pen != series->pen()){
134 134 item->setPen(series->pen());
135 135 return;
136 136 }
137 137 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
138 138 pen.setWidthF(2);
139 139 item->setPen(pen);
140 140 }
141 141
142 142 void ChartTheme::decorate(BarPresenter* item, QBarChartSeries* series,int count)
143 143 {
144 144 for (int i=0; i<series->countSets(); i++) {
145 145 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
146 146 }
147 147 }
148 148
149 149 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count)
150 150 {
151 151 for (int i=0; i<series->countSets(); i++) {
152 152 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
153 153 }
154 154 }
155 155
156 156 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count)
157 157 {
158 158 for (int i=0; i<series->countSets(); i++) {
159 159 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
160 160 }
161 161 }
162 162
163 163 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
164 164 {
165 165 Q_ASSERT(presenter);
166 166 Q_ASSERT(series);
167 167
168 168 QColor color = m_seriesColor.at(count % m_seriesColor.size());
169 169 // TODO: define alpha in the theme? or in the series?
170 170 color.setAlpha(120);
171 171
172 172 QBrush brush(color, Qt::SolidPattern);
173 173 presenter->m_markerBrush = brush;
174 174
175 175 QPen pen(brush, 1);
176 176 pen.setColor(color);
177 177 presenter->m_markerPen = pen;
178 178 }
179 179
180 180 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
181 181 {
182 182 // create a list of slice colors based on current theme
183 183 int i = 0;
184 184 QList<QColor> colors;
185 185 while (colors.count() < series->count()) {
186 186
187 187 // get base color
188 188 QColor c = m_seriesColor[i++];
189 189 i = i % m_seriesColor.count();
190 190
191 191 // -1 means achromatic color -> cannot manipulate lightness
192 192 // TODO: find a better way to randomize lightness
193 193 if (c.toHsv().hue() == -1)
194 194 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
195 195
196 196 // randomize lightness
197 197 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
198 198 c = c.lighter(f);
199 199
200 200 // find duplicates
201 201 bool isUnique = true;
202 202 foreach (QColor color, colors) {
203 203 if (c == color)
204 204 isUnique = false;
205 205 }
206 206
207 207 // add to array if unique
208 208 //if (isUnique)
209 209 colors << c;
210 210 }
211 211
212 212 // finally update colors
213 213 foreach (QPieSlice* s, series->slices()) {
214 214 s->setPen(QPen(Qt::black)); // TODO: get from theme
215 215 s->setBrush(colors.takeFirst());
216 216 }
217 217 }
218 218
219 219
220 void ChartTheme::decorate(QChartAxis& axis,AxisItem* item)
220 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
221 221 {
222 222 //TODO: dummy defults for now
223
224 axis.setLabelsBrush(Qt::black);
225 axis.setLabelsPen(Qt::NoPen);
226 axis.setShadesPen(Qt::NoPen);
227 axis.setShadesOpacity(0.5);
228 item->handleAxisChanged(axis);
223 axis->setLabelsBrush(Qt::black);
224 axis->setLabelsPen(Qt::NoPen);
225 axis->setShadesPen(Qt::NoPen);
226 axis->setShadesOpacity(0.5);
229 227 }
230 228
231 229 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,52
1 1 #ifndef CHARTTHEME_H
2 2 #define CHARTTHEME_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h"
6 6 #include <QColor>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class ChartItem;
11 11 class QChartSeries;
12 12 class LineChartItem;
13 13 class QLineChartSeries;
14 14 class BarPresenter;
15 15 class QBarChartSeries;
16 16 class StackedBarPresenter;
17 17 class QStackedBarChartSeries;
18 18 class QPercentBarChartSeries;
19 19 class PercentBarPresenter;
20 20 class QScatterSeries;
21 21 class ScatterPresenter;
22 22 class PiePresenter;
23 23 class QPieSeries;
24 24
25 25 class ChartTheme
26 26 {
27 27 protected:
28 28 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
29 29 public:
30 30 static ChartTheme* createTheme(QChart::ChartTheme theme);
31 31 QChart::ChartTheme id() const {return m_id;}
32 32 void decorate(QChart* chart);
33 33 void decorate(ChartItem* item, QChartSeries* series,int count);
34 34 void decorate(LineChartItem* item, QLineChartSeries*, int count);
35 35 void decorate(BarPresenter* item, QBarChartSeries* series,int count);
36 36 void decorate(StackedBarPresenter* item, QStackedBarChartSeries* series,int count);
37 37 void decorate(PercentBarPresenter* item, QPercentBarChartSeries* series,int count);
38 38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
39 39 void decorate(PiePresenter* item, QPieSeries* series, int count);
40 void decorate(QChartAxis& axis,AxisItem* item);
40 void decorate(QChartAxis* axis,AxisItem* item);
41 41
42 42 protected:
43 43 QChart::ChartTheme m_id;
44 44 QColor m_gradientStartColor;
45 45 QColor m_gradientEndColor;
46 46 QList<QColor> m_seriesColor;
47 47
48 48 };
49 49
50 50 QTCOMMERCIALCHART_END_NAMESPACE
51 51
52 52 #endif // CHARTTHEME_H
@@ -1,209 +1,163
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5
6 <<<<<<< HEAD
6 7 //series
7 8 #include "qbarchartseries.h"
8 9 #include "qstackedbarchartseries.h"
9 10 #include "qpercentbarchartseries.h"
10 11 #include "qlinechartseries.h"
11 12 #include "qscatterseries.h"
12 13 //#include "scatterseries_p.h"
13 14 #include "qpieseries.h"
14
15 15 #include <QGraphicsScene>
16 16 #include <QGraphicsSceneResizeEvent>
17 17 #include <QDebug>
18 18
19 19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20 20
21 21 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
22 22 m_backgroundItem(0),
23 23 m_titleItem(0),
24 24 m_dataset(new ChartDataSet(this)),
25 25 m_presenter(new ChartPresenter(this,m_dataset))
26 26 {
27 27 }
28 28
29 29 QChart::~QChart() {}
30 30
31 void QChart::addSeries(QChartSeries* series)
32 {
33 m_dataset->addSeries(series);
34 }
35
36 //TODO on review, is it really needed ??
37 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
38 {
39 QChartSeries *series(0);
40
41 switch (type) {
42 case QChartSeries::SeriesTypeLine: {
43 series = new QLineChartSeries(this);
44 break;
45 }
46 case QChartSeries::SeriesTypeBar: {
47 //series = new BarChartSeries(this);
48 break;
49 }
50 case QChartSeries::SeriesTypeStackedBar: {
51 //series = new StackedBarChartSeries(this);
52 break;
53 }
54 case QChartSeries::SeriesTypePercentBar: {
55 //series = new PercentBarChartSeries(this);
56 break;
57 }
58 case QChartSeries::SeriesTypeScatter: {
59 series = new QScatterSeries(this);
60 break;
61 }
62 case QChartSeries::SeriesTypePie: {
63 series = new QPieSeries(this);
64 break;
65 }
66 default:
67 Q_ASSERT(false);
68 break;
69 }
31 void QChart::addSeries(QChartSeries* series,QChartAxis* axisY)
32 {
33 m_dataset->addSeries(series,axisY);
34 }
70 35
71 addSeries(series);
72 return series;
36 void QChart::removeSeries(QChartSeries* series)
37 {
38 m_dataset->removeSeries(series);
73 39 }
74 40
75 41 void QChart::setChartBackgroundBrush(const QBrush& brush)
76 42 {
77
78 if(!m_backgroundItem) {
79 m_backgroundItem = new QGraphicsRectItem(this);
80 m_backgroundItem->setZValue(-1);
81 }
82
43 createChartBackgroundItem();
83 44 m_backgroundItem->setBrush(brush);
84 45 m_backgroundItem->update();
85 46 }
86 47
87 48 void QChart::setChartBackgroundPen(const QPen& pen)
88 49 {
89
90 if(!m_backgroundItem) {
91 m_backgroundItem = new QGraphicsRectItem(this);
92 m_backgroundItem->setZValue(-1);
93 }
94
50 createChartBackgroundItem();
95 51 m_backgroundItem->setPen(pen);
96 52 m_backgroundItem->update();
97 53 }
98 54
99 55 void QChart::setChartTitle(const QString& title)
100 56 {
101 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
57 createChartTitleItem();
102 58 m_titleItem->setPlainText(title);
103 59 }
104 60
105 61 void QChart::setChartTitleFont(const QFont& font)
106 62 {
107 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
63 createChartTitleItem();
108 64 m_titleItem->setFont(font);
109 65 }
110 66
67 void QChart::createChartBackgroundItem()
68 {
69 if(!m_backgroundItem) {
70 m_backgroundItem = new QGraphicsRectItem(this);
71 m_backgroundItem->setZValue(-1);
72 }
73 }
74
75 void QChart::createChartTitleItem()
76 {
77 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
78 }
79
111 80 int QChart::margin() const
112 81 {
113 82 return m_presenter->margin();
114 83 }
115 84
116 85 void QChart::setMargin(int margin)
117 86 {
118 87 m_presenter->setMargin(margin);
119 88 }
120 89
121 90 void QChart::setChartTheme(QChart::ChartTheme theme)
122 91 {
123 92 m_presenter->setChartTheme(theme);
124 93 }
125 94
126 95 QChart::ChartTheme QChart::chartTheme() const
127 96 {
128 97 return m_presenter->chartTheme();
129 98 }
130 99
131 void QChart::zoomInToRect(const QRectF& rectangle)
100 void QChart::zoomIn()
132 101 {
133 m_presenter->zoomInToRect(rectangle);
102 if (!m_dataset->nextDomain()) {
103 QRectF rect = m_presenter->geometry();
104 rect.setWidth(rect.width()/2);
105 rect.setHeight(rect.height()/2);
106 rect.moveCenter(m_presenter->geometry().center());
107 zoomIn(rect);
108 }
134 109 }
135 110
136 void QChart::zoomIn()
111 void QChart::zoomIn(const QRectF& rect)
137 112 {
138 m_presenter->zoomIn();
113 if(!rect.isValid()) return;
114 QRectF r = rect.normalized();
115 int margin = m_presenter->margin();
116 r.translate(-margin, -margin);
117 m_dataset->addDomain(r,m_presenter->geometry());
139 118 }
140 119
141 120 void QChart::zoomOut()
142 121 {
143 m_presenter->zoomOut();
122 m_dataset->previousDomain();
144 123 }
145 124
146 125 void QChart::zoomReset()
147 126 {
148 m_presenter->zoomReset();
149 }
150
151 void QChart::setDefaultAxisX(const QChartAxis& axis)
152 {
153 m_presenter->setDefaultAxisX(axis);
154 }
155
156 void QChart::setDefaultAxisY(const QChartAxis& axis)
157 {
158 m_presenter->setDefaultAxisY(axis);
159 }
160
161 QChartAxis QChart::defaultAxisX() const
162 {
163 return m_presenter->defaultAxisX();
164 }
165
166 QChartAxis QChart::defaultAxisY() const
167 {
168 return m_presenter->defaultAxisY();
169 }
170
171 int QChart::addAxisY(const QChartAxis& axis)
172 {
173 return m_presenter->addAxisY(axis);
127 m_dataset->clearDomains();
174 128 }
175 129
176 QChartAxis QChart::axisY(int id) const
130 QChartAxis* QChart::axisX() const
177 131 {
178 return m_presenter->axisY(id);
132 return m_dataset->axisX();
179 133 }
180 134
181 void QChart::removeAxisY(int id)
135 QChartAxis* QChart::axisY() const
182 136 {
183 m_presenter->removeAxisY(id);
137 return m_dataset->axisY();
184 138 }
185 139
186 140 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
187 141 {
188 142
189 143 m_rect = QRectF(QPoint(0,0),event->newSize());
190 144 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
191 145
192 146 // recalculate title position
193 147 if (m_titleItem) {
194 148 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
195 149 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
196 150 }
197 151
198 152 //recalculate background gradient
199 153 if (m_backgroundItem) {
200 154 m_backgroundItem->setRect(rect);
201 155 }
202 156
203 157 QGraphicsWidget::resizeEvent(event);
204 158 update();
205 159 }
206 160
207 161 #include "moc_qchart.cpp"
208 162
209 163 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,93 +1,84
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsWidget>
7 7 #include <QLinearGradient>
8 8 #include <QFont>
9 9
10 10 class QGraphicsSceneResizeEvent;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class AxisItem;
15 15 class QChartSeries;
16 16 class PlotDomain;
17 17 class BarPresenter;
18 18 class QChartAxis;
19 19 class ChartTheme;
20 20 class ChartItem;
21 21 class ChartDataSet;
22 22 class ChartPresenter;
23 23
24 // TODO: We don't need to have QChart tied to QGraphicsItem:
25 //class QTCOMMERCIALCHART_EXPORT QChart
26 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
27 // public: QChartGraphicsItem(QChart &chart);
28
29 24 /*!
30 25 * TODO: define the responsibilities
31 26 */
32 27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
33 28 {
34 29 Q_OBJECT
35 30 public:
36 31 enum ChartTheme {
37 32 /*! The default theme follows the GUI style of the Operating System */
38 33 ChartThemeDefault,
39 34 ChartThemeVanilla,
40 35 ChartThemeIcy,
41 36 ChartThemeGrayscale,
42 37 ChartThemeScientific,
43 38 //ChartThemeUnnamed1
44 39 };
45 40
46 41 public:
47 42 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
48 43 ~QChart();
49 44
50 void addSeries(QChartSeries* series);
51
52 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
53 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
54 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
45 void addSeries(QChartSeries* series,QChartAxis *axisY=0);
46 void removeSeries(QChartSeries* series);
55 47
56 48 void setMargin(int margin);
57 49 int margin() const;
58 50 void setChartTheme(QChart::ChartTheme theme);
59 51 QChart::ChartTheme chartTheme() const;
60 52
61 53 void setChartTitle(const QString& title);
62 54 void setChartTitleFont(const QFont& font);
63 55 void setChartBackgroundBrush(const QBrush& brush);
64 56 void setChartBackgroundPen(const QPen& pen);
65 57
66 void zoomInToRect(const QRectF& rectangle);
67 58 void zoomIn();
59 void zoomIn(const QRectF& rect);
68 60 void zoomOut();
69 61 void zoomReset();
70 62
71 void setDefaultAxisX(const QChartAxis& axis);
72 void setDefaultAxisY(const QChartAxis& axis);
73 QChartAxis defaultAxisX() const;
74 QChartAxis defaultAxisY() const;
75 QChartAxis axisY(int id) const;
76 int addAxisY(const QChartAxis& axis);
77 void removeAxisY(int id);
63 QChartAxis* axisX() const;
64 QChartAxis* axisY() const;
78 65
79 66 protected:
80 67 void resizeEvent(QGraphicsSceneResizeEvent *event);
81 68
82 69 private:
70 inline void createChartBackgroundItem();
71 inline void createChartTitleItem();
72
73 private:
83 74 Q_DISABLE_COPY(QChart)
84 75 QGraphicsRectItem* m_backgroundItem;
85 76 QGraphicsTextItem* m_titleItem;
86 77 QRectF m_rect;
87 78 ChartDataSet *m_dataset;
88 79 ChartPresenter *m_presenter;
89 80 };
90 81
91 82 QTCOMMERCIALCHART_END_NAMESPACE
92 83
93 84 #endif
@@ -1,86 +1,154
1 1 #include "qchartaxis.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 QChartAxis::QChartAxis():
5 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
6 6 m_axisVisible(true),
7 7 m_gridVisible(true),
8 8 m_labelsVisible(true),
9 m_labelsAngle(0),
9 10 m_shadesVisible(true),
10 m_shadesOpacity(1.0)
11 m_shadesOpacity(1.0),
12 m_min(0),
13 m_max(0),
14 m_ticksCount(4)
11 15 {
12 // TODO Auto-generated constructor stub
13 16
14 17 }
15 18
16 19 QChartAxis::~QChartAxis()
17 20 {
18 // TODO Auto-generated destructor stub
19 21 }
20 22
21 23 void QChartAxis::setAxisPen(const QPen& pen)
22 24 {
23 25 m_axisPen=pen;
26 emit update(this);
24 27 }
25 28
26 29 void QChartAxis::setAxisVisible(bool visible)
27 30 {
28 31 m_axisVisible=visible;
32 emit update(this);
29 33 }
30 34
31 35 void QChartAxis::setGridVisible(bool visible)
32 36 {
33 37 m_gridVisible=visible;
38 emit update(this);
34 39 }
35 40
36 41 void QChartAxis::setGridPen(const QPen& pen)
37 42 {
38 43 m_gridPen=pen;
44 emit update(this);
39 45 }
40 46
41 47 void QChartAxis::setLabelsVisible(bool visible)
42 48 {
43 49 m_labelsVisible=visible;
50 emit update(this);
44 51 }
45 52
46 53 void QChartAxis::setLabelsPen(const QPen& pen)
47 54 {
48 55 m_labelsPen=pen;
56 emit update(this);
49 57 }
50 58
51 59 void QChartAxis::setLabelsBrush(const QBrush& brush)
52 60 {
53 61 m_labelsBrush=brush;
62 emit update(this);
54 63 }
55 64
56 65 void QChartAxis::setLabelsFont(const QFont& font)
57 66 {
58 67 m_labelsFont=font;
68 emit update(this);
59 69 }
60 70
61 void QChartAxis::setLabelsOrientation(LabelsOrientation orientation)
71 void QChartAxis::setLabelsAngle(int angle)
62 72 {
63 m_labelsOrientation=orientation;
73 m_labelsAngle=angle;
74 emit update(this);
64 75 }
65 76
66 77 void QChartAxis::setShadesVisible(bool visible)
67 78 {
68 79 m_shadesVisible=visible;
80 emit update(this);
69 81 }
70 82
71 83 void QChartAxis::setShadesPen(const QPen& pen)
72 84 {
73 85 m_shadesPen=pen;
86 emit update(this);
74 87 }
75 88
76 89 void QChartAxis::setShadesBrush(const QBrush& brush)
77 90 {
78 91 m_shadesBrush=brush;
92 emit update(this);
79 93 }
80 94
81 95 void QChartAxis::setShadesOpacity(qreal opacity)
82 96 {
83 97 m_shadesOpacity=opacity;
98 emit update(this);
84 99 }
85 100
101 void QChartAxis::setMin(qreal min)
102 {
103 if(m_min!=min){
104 m_min=min;
105 emit minChanged(m_min);
106 }
107 }
108
109 void QChartAxis::setMax(qreal max)
110 {
111 if(m_max!=max){
112 m_max=max;
113 emit maxChanged(m_max);
114 }
115 }
116
117 void QChartAxis::setRange(qreal min, qreal max)
118 {
119 setMin(min);
120 setMax(max);
121 }
122
123 void QChartAxis::setTicksCount(int count)
124 {
125 m_ticksCount=count;
126 emit ticksChanged(this);
127 }
128
129 void QChartAxis::addAxisTickLabel(qreal value,const QString& label)
130 {
131 m_ticks.insert(value,label);
132 emit ticksChanged(this);
133 }
134
135 void QChartAxis::removeAxisTickLabel(qreal value)
136 {
137 m_ticks.remove(value);
138 emit ticksChanged(this);
139 }
140
141 QString QChartAxis::axisTickLabel(qreal value) const
142 {
143 return m_ticks.value(value);
144 }
145
146 void QChartAxis::clearAxisTickLabels()
147 {
148 m_ticks.clear();
149 emit ticksChanged(this);
150 }
151
152 #include "moc_qchartaxis.cpp"
153
86 154 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,103
1 1 #ifndef QCHARTAXIS_H_
2 2 #define QCHARTAXIS_H_
3 3
4 4 #include <qchartglobal.h>
5 5 #include <QPen>
6 6 #include <QFont>
7 7
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 class QTCOMMERCIALCHART_EXPORT QChartAxis
11 class QTCOMMERCIALCHART_EXPORT QChartAxis : public QObject
12 12 {
13 Q_OBJECT
13 14 public:
14 enum LabelsOrientation{ LabelsOrientationHorizontal, LabelsOrientationVertical , LabelsOrientationSlide };
15 QChartAxis(QObject* parent =0);
16 ~QChartAxis();
15 17
16 QChartAxis();
17 virtual ~QChartAxis();
18
19 //axis
18 //axis handling
20 19 bool isAxisVisible() const { return m_axisVisible;};
21 20 void setAxisVisible(bool visible);
22 21 void setAxisPen(const QPen& pen);
23 22 QPen axisPen() const { return m_axisPen;};
24 23 void setAxisBrush(const QBrush& brush);
25 24 QBrush axisBrush() const { return m_axisBrush;};
26 25
27 //grid
26 //grid handling
28 27 bool isGridVisible() const { return m_gridVisible;};
29 28 void setGridVisible(bool visible);
30 29 void setGridPen(const QPen& pen);
31 30 QPen gridPen() const {return m_gridPen;}
32 31
33 //labels
32 //labels handling
34 33 bool isLabelsVisible() const { return m_labelsVisible;};
35 34 void setLabelsVisible(bool visible);
36 35 void setLabelsPen(const QPen& pen);
37 36 QPen labelsPen() const { return m_labelsPen;}
38 37 void setLabelsBrush(const QBrush& brush);
39 38 QBrush labelsBrush() const { return m_labelsBrush;}
40 39 void setLabelsFont(const QFont& font);
41 40 QFont labelFont() const { return m_labelsFont;}
42 void setLabelsOrientation(LabelsOrientation orientation);
43 LabelsOrientation labelsOrientation() const { return m_labelsOrientation;};
41 void setLabelsAngle(int angle);
42 int labelsAngle() const { return m_labelsAngle;};
44 43
45 //shades
44 //shades handling
46 45 bool isShadesVisible() const { return m_shadesVisible;};
47 46 void setShadesVisible(bool visible);
48 47 void setShadesPen(const QPen& pen);
49 48 QPen shadesPen() const { return m_shadesPen;}
50 49 void setShadesBrush(const QBrush& brush);
51 50 QBrush shadesBrush() const { return m_shadesBrush;}
52 51 void setShadesOpacity(qreal opacity);
53 52 qreal shadesOpacity() const { return m_shadesOpacity;}
54 53
55
54 //range handling
55 void setMin(qreal min);
56 qreal min() const { return m_min;};
57 void setMax(qreal max);
58 qreal max() const { return m_max;};
59 void setRange(qreal min, qreal max);
60
61 //ticks handling
62 void setTicksCount(int count);
63 int ticksCount() const { return m_ticksCount;}
64 void addAxisTickLabel(qreal value,const QString& label);
65 void removeAxisTickLabel(qreal value);
66 QString axisTickLabel(qreal value) const ;
67 void clearAxisTickLabels();
68
69 signals:
70 void minChanged(qreal min);
71 void maxChanged(qreal max);
72 //private signal
73 void update(QChartAxis*);
74 void ticksChanged(QChartAxis*);
56 75
57 76 private:
58
59 77 bool m_axisVisible;
60 78 QPen m_axisPen;
61 79 QBrush m_axisBrush;
62 80
63 81 bool m_gridVisible;
64 82 QPen m_gridPen;
65 83
66 84 bool m_labelsVisible;
67 85 QPen m_labelsPen;
68 86 QBrush m_labelsBrush;
69 87 QFont m_labelsFont;
88 int m_labelsAngle;
70 89
71 90 bool m_shadesVisible;
72 91 QPen m_shadesPen;
73 92 QBrush m_shadesBrush;
74
75 93 qreal m_shadesOpacity;
76 94
95 qreal m_min;
96 qreal m_max;
77 97
78 LabelsOrientation m_labelsOrientation;
98 int m_ticksCount;
99 QMap<qreal, QString> m_ticks;
79 100 };
80 101
81 102 QTCOMMERCIALCHART_END_NAMESPACE
82 103 #endif /* QCHARTAXIS_H_ */
@@ -1,241 +1,211
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QGraphicsView>
5 5 #include <QGraphicsScene>
6 6 #include <QRubberBand>
7 7 #include <QResizeEvent>
8 8 #include <QDebug>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 QChartView::QChartView(QWidget *parent) :
13 13 QGraphicsView(parent),
14 14 m_scene(new QGraphicsScene(this)),
15 15 m_chart(new QChart()),
16 16 m_rubberBand(0),
17 17 m_verticalRubberBand(false),
18 18 m_horizonalRubberBand(false)
19 19 {
20 20 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
21 21 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
22 22 setScene(m_scene);
23 23 m_chart->setMargin(50);
24 24 m_scene->addItem(m_chart);
25 25 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
26 26 }
27 27
28 28 QChartView::~QChartView()
29 29 {
30 30 }
31 31
32 32 void QChartView::resizeEvent(QResizeEvent *event)
33 33 {
34 34 m_scene->setSceneRect(0,0,size().width(),size().height());
35 35 m_chart->resize(size());
36 36 QWidget::resizeEvent(event);
37 37 }
38 38
39 void QChartView::addSeries(QChartSeries* series)
39 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
40 40 {
41 m_chart->addSeries(series);
41 m_chart->addSeries(series,axisY);
42 42 }
43 43
44 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
44 void QChartView::zoomIn()
45 45 {
46
47 return m_chart->createSeries(type);
46 m_chart->zoomIn();
48 47 }
49 48
50 void QChartView::zoomInToRect(const QRect& rectangle)
49 void QChartView::zoomIn(const QRect& rect)
51 50 {
52 m_chart->zoomInToRect(rectangle);
53 }
54
55 void QChartView::zoomIn()
56 {
57 m_chart->zoomIn();
51 m_chart->zoomIn(rect);
58 52 }
59 53
60 54 void QChartView::zoomOut()
61 55 {
62 56 m_chart->zoomOut();
63 57 }
64 58
65 59 int QChartView::margin() const
66 60 {
67 61 return m_chart->margin();
68 62 }
69 63
70 64 void QChartView::setChartTitle(const QString& title)
71 65 {
72 66 m_chart->setChartTitle(title);
73 67 }
74 68
75 69 void QChartView::setChartTitleFont(const QFont& font)
76 70 {
77 71 m_chart->setChartTitleFont(font);
78 72 }
79 73
80 74 void QChartView::setChartBackgroundBrush(const QBrush& brush)
81 75 {
82 76 m_chart->setChartBackgroundBrush(brush);
83 77 }
84 78 void QChartView::setChartBackgroundPen(const QPen& pen)
85 79 {
86 80 m_chart->setChartBackgroundPen(pen);
87 81 }
88 82
89 83 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
90 84 {
91 85 switch(policy) {
92 86 case VerticalRubberBand:
93 87 m_verticalRubberBand = true;
94 88 m_horizonalRubberBand = false;
95 89 break;
96 90 case HorizonalRubberBand:
97 91 m_verticalRubberBand = false;
98 92 m_horizonalRubberBand = true;
99 93 break;
100 94 case RectangleRubberBand:
101 95 m_verticalRubberBand = true;
102 96 m_horizonalRubberBand = true;
103 97 break;
104 98 case NoRubberBand:
105 99 default:
106 100 delete m_rubberBand;
107 101 m_rubberBand=0;
108 102 m_horizonalRubberBand = false;
109 103 m_verticalRubberBand = false;
110 104 return;
111 105 }
112 106 if(!m_rubberBand) {
113 107 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
114 108 m_rubberBand->setEnabled(true);
115 109 }
116 110 }
117 111
118 112 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
119 113 {
120 114 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
121 115 if(m_horizonalRubberBand) return HorizonalRubberBand;
122 116 if(m_verticalRubberBand) return VerticalRubberBand;
123 117 return NoRubberBand;
124 118 }
125 119
126 120 void QChartView::mousePressEvent(QMouseEvent *event)
127 121 {
128 122 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
129 123
130 124 int margin = m_chart->margin();
131 125 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
132 126
133 127 if (rect.contains(event->pos())) {
134 128 m_rubberBandOrigin = event->pos();
135 129 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
136 130 m_rubberBand->show();
137 131 event->accept();
138 132 }
139 133 }
140 134 }
141 135
142 136 void QChartView::mouseMoveEvent(QMouseEvent *event)
143 137 {
144 138 if(m_rubberBand && m_rubberBand->isVisible()) {
145 139 int margin = m_chart->margin();
146 140 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
147 141 int width = event->pos().x() - m_rubberBandOrigin.x();
148 142 int height = event->pos().y() - m_rubberBandOrigin.y();
149 143 if(!m_verticalRubberBand) {
150 144 m_rubberBandOrigin.setY(rect.top());
151 145 height = rect.height();
152 146 }
153 147 if(!m_horizonalRubberBand) {
154 148 m_rubberBandOrigin.setX(rect.left());
155 149 width= rect.width();
156 150 }
157 151 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
158 152 }
159 153 else {
160 154 QGraphicsView::mouseMoveEvent(event);
161 155 }
162 156 }
163 157
164 158 void QChartView::mouseReleaseEvent(QMouseEvent *event)
165 159 {
166 160 if(m_rubberBand) {
167 161 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
168 162 m_rubberBand->hide();
169 163 QRect rect = m_rubberBand->geometry();
170 m_chart->zoomInToRect(rect);
164 m_chart->zoomIn(rect);
171 165 event->accept();
172 166 }
173 167
174 168 if(event->button()==Qt::RightButton)
175 169 m_chart->zoomReset();
176 170 }
177 171 else {
178 172 QGraphicsView::mouseReleaseEvent(event);
179 173 }
180 174 }
181 175
182 176 void QChartView::keyPressEvent(QKeyEvent *event)
183 177 {
184 178 switch (event->key()) {
185 179 case Qt::Key_Plus:
186 180 zoomIn();
187 181 break;
188 182 case Qt::Key_Minus:
189 183 zoomOut();
190 184 break;
191 185 default:
192 186 QGraphicsView::keyPressEvent(event);
193 187 break;
194 188 }
195 189 }
196 190
197 191 void QChartView::setChartTheme(QChart::ChartTheme theme)
198 192 {
199 193 m_chart->setChartTheme(theme);
200 194 }
201 195
202 196 QChart::ChartTheme QChartView::chartTheme() const
203 197 {
204 198 return m_chart->chartTheme();
205 199 }
206 200
207 void QChartView::setDefaultAxisX(const QChartAxis& axis)
208 {
209 m_chart->setDefaultAxisX(axis);
210 }
211
212 void QChartView::setDefaultAxisY(const QChartAxis& axis)
201 QChartAxis* QChartView::axisX() const
213 202 {
214 m_chart->setDefaultAxisY(axis);
203 return m_chart->axisX();
215 204 }
216 205
217 QChartAxis QChartView::defaultAxisX() const
206 QChartAxis* QChartView::axisY() const
218 207 {
219 return m_chart->defaultAxisX();
208 return m_chart->axisY();
220 209 }
221 210
222 QChartAxis QChartView::defaultAxisY() const
223 {
224 return m_chart->defaultAxisY();
225 }
226
227 int QChartView::addAxisY(const QChartAxis& axis)
228 {
229 return m_chart->addAxisY(axis);
230 }
231
232 QChartAxis QChartView::axisY(int id) const
233 {
234 return m_chart->axisY(id);
235 }
236
237 void QChartView::removeAxisY(int id)
238 {
239 m_chart->removeAxisY(id);
240 }
241 211 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,78 +1,71
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include "qchart.h"
7 7 #include <QGraphicsView>
8 8
9 9 class QGraphicsScene;
10 10 class QRubberBand;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class QChart;
15 15
16 16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
17 17 {
18 18 public:
19 19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
20 20
21 21 explicit QChartView(QWidget *parent = 0);
22 22 ~QChartView();
23 23
24 24 //implement from QWidget
25 25 void resizeEvent(QResizeEvent *event);
26
27 void addSeries(QChartSeries* series); // takes ownership
28
29 // Convenience function
30 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
26
27 void addSeries(QChartSeries* series,QChartAxis* axisY=0) ;// takes ownership
28 void removeSeries(QChartSeries* series);
31 29
32 30 int margin() const;
33 31
34 32 void setChartTitle(const QString& title);
35 33 void setChartTitleFont(const QFont& font);
36 34 void setChartBackgroundBrush(const QBrush& brush);
37 35 void setChartBackgroundPen(const QPen& pen);
38 36
39 void zoomInToRect(const QRect& rectangle);
40 37 void zoomIn();
38 void zoomIn(const QRect& rect);
41 39 void zoomOut();
42 40
43 41 void setRubberBandPolicy(const RubberBandPolicy );
44 42 RubberBandPolicy rubberBandPolicy() const;
45 43
46 44 void setChartTheme(QChart::ChartTheme theme);
47 45 QChart::ChartTheme chartTheme() const;
48 46
49 void setDefaultAxisX(const QChartAxis& axis);
50 void setDefaultAxisY(const QChartAxis& axis);
51 QChartAxis defaultAxisX() const;
52 QChartAxis defaultAxisY() const;
53 QChartAxis axisY(int id) const;
54 int addAxisY(const QChartAxis& axis);
55 void removeAxisY(int id);
47 QChartAxis* axisX() const;
48 QChartAxis* axisY() const;
56 49
57 50 protected:
58 51 void mousePressEvent(QMouseEvent *event);
59 52 void mouseMoveEvent(QMouseEvent *event);
60 53 void mouseReleaseEvent(QMouseEvent *event);
61 54 void keyPressEvent(QKeyEvent *event);
62 55
63 56
64 57 private:
65 58 QGraphicsScene *m_scene;
66 59 QChart* m_chart;
67 60 QPoint m_rubberBandOrigin;
68 61 QRubberBand* m_rubberBand;
69 62 bool m_verticalRubberBand;
70 63 bool m_horizonalRubberBand;
71 64 Q_DISABLE_COPY(QChartView)
72 65
73 66
74 67 };
75 68
76 69 QTCOMMERCIALCHART_END_NAMESPACE
77 70
78 71 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now