##// END OF EJS Templates
Work-around for a bug with plot domain list indexes
Tero Ahola -
r50:231fdcb4240b
parent child
Show More
@@ -1,158 +1,159
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qxychartseries.h"
5 #include "qxychartseries.h"
6 #include "xylinechartitem_p.h"
6 #include "xylinechartitem_p.h"
7 #include "xyplotdomain_p.h"
7 #include "xyplotdomain_p.h"
8 #include "axis_p.h"
8 #include "axis_p.h"
9 #include "xygrid_p.h"
9 #include "xygrid_p.h"
10 #include <QGraphicsScene>
10 #include <QGraphicsScene>
11 #include <QDebug>
11 #include <QDebug>
12
12
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14
14
15 class QChartPrivate
15 class QChartPrivate
16 {
16 {
17 public:
17 public:
18
18
19 QChartPrivate(QChart* parent):
19 QChartPrivate(QChart* parent):
20 m_axisX(new Axis(parent)),
20 m_axisX(new Axis(parent)),
21 m_axisY(new Axis(parent)),
21 m_axisY(new Axis(parent)),
22 m_grid(new XYGrid(parent)),
22 m_grid(new XYGrid(parent)),
23 m_plotDataIndex(0),
23 m_plotDataIndex(0),
24 m_marginSize(0){}
24 m_marginSize(0){}
25
25
26 Axis* m_axisX;
26 Axis* m_axisX;
27 Axis* m_axisY;
27 Axis* m_axisY;
28 XYGrid* m_grid;
28 XYGrid* m_grid;
29 QRect m_rect;
29 QRect m_rect;
30 QList<const QChartSeries*> m_series;
30 QList<const QChartSeries*> m_series;
31 QList<XYPlotDomain> m_plotDomainList;
31 QList<XYPlotDomain> m_plotDomainList;
32 QList<XYLineChartItem*> m_xyLineChartItems;
32 QList<XYLineChartItem*> m_xyLineChartItems;
33 QList<QGraphicsItem*> m_items;
33 QList<QGraphicsItem*> m_items;
34 int m_plotDataIndex;
34 int m_plotDataIndex;
35 int m_marginSize;
35 int m_marginSize;
36
36
37 };
37 };
38
38
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40
40
41 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
41 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
42 d(new QChartPrivate(this))
42 d(new QChartPrivate(this))
43 {
43 {
44 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
44 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
45 // set axis
45 // set axis
46 d->m_axisY->rotate(90);
46 d->m_axisY->rotate(90);
47 }
47 }
48
48
49 QChart::~QChart(){}
49 QChart::~QChart(){}
50
50
51 QRectF QChart::boundingRect() const
51 QRectF QChart::boundingRect() const
52 {
52 {
53 return d->m_rect;
53 return d->m_rect;
54 }
54 }
55
55
56 void QChart::addSeries(QChartSeries* series)
56 void QChart::addSeries(QChartSeries* series)
57 {
57 {
58 // TODO: we should check the series not already added
58 // TODO: we should check the series not already added
59
59
60 d->m_series<<series;
60 d->m_series<<series;
61
61
62 switch(series->type())
62 switch(series->type())
63 {
63 {
64 case QChartSeries::SeriesTypeLine: {
64 case QChartSeries::SeriesTypeLine: {
65
65
66 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
66 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
67
67
68 XYPlotDomain domain;
68 XYPlotDomain domain;
69 //TODO "nice numbers algorithm"
69 //TODO "nice numbers algorithm"
70 domain.m_ticksX=4;
70 domain.m_ticksX=4;
71 domain.m_ticksY=4;
71 domain.m_ticksY=4;
72
72
73 for (int i = 0 ; i < xyseries->count() ; i++)
73 for (int i = 0 ; i < xyseries->count() ; i++)
74 {
74 {
75 qreal x = xyseries->x(i);
75 qreal x = xyseries->x(i);
76 qreal y = xyseries->y(i);
76 qreal y = xyseries->y(i);
77 domain.m_minX = qMin(domain.m_minX,x);
77 domain.m_minX = qMin(domain.m_minX,x);
78 domain.m_minY = qMin(domain.m_minY,y);
78 domain.m_minY = qMin(domain.m_minY,y);
79 domain.m_maxX = qMax(domain.m_maxX,x);
79 domain.m_maxX = qMax(domain.m_maxX,x);
80 domain.m_maxY = qMax(domain.m_maxY,y);
80 domain.m_maxY = qMax(domain.m_maxY,y);
81 }
81 }
82
82
83 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
83 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
84 item->updateXYPlotDomain(domain);
84 item->updateXYPlotDomain(domain);
85 d->m_plotDomainList<<domain;
85 d->m_plotDomainList<<domain;
86 d->m_xyLineChartItems<<item;
86 d->m_xyLineChartItems<<item;
87 break;
87 break;
88 }
88 }
89 case QChartSeries::SeriesTypeScatter: {
89 case QChartSeries::SeriesTypeScatter: {
90 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
90 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
91 if (scatter) {
91 if (scatter) {
92 scatter->d->setParentItem(this);
92 scatter->d->setParentItem(this);
93 scene()->addItem(scatter->d);
93 scene()->addItem(scatter->d);
94 }
94 }
95 break;
95 break;
96 }
96 }
97 }
97 }
98 }
98 }
99
99
100 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
100 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
101 {
101 {
102 // TODO: support also other types in addition to scatter
102 // TODO: support also other types in addition to scatter
103 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
103 Q_ASSERT(type == QChartSeries::SeriesTypeScatter);
104 QChartSeries *series = 0;
104 QChartSeries *series = 0;
105
105
106 switch (type) {
106 switch (type) {
107 case QChartSeries::SeriesTypeScatter: {
107 case QChartSeries::SeriesTypeScatter: {
108 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
108 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
109 scatterSeries->d->setParentItem(this);
109 scatterSeries->d->setParentItem(this);
110 break;
110 break;
111 }
111 }
112 default:
112 default:
113 break;
113 break;
114 }
114 }
115
115
116 return series;
116 return series;
117 }
117 }
118
118
119 void QChart::setSize(const QSizeF& size)
119 void QChart::setSize(const QSizeF& size)
120 {
120 {
121 d->m_rect = QRect(QPoint(0,0),size.toSize());
121 d->m_rect = QRect(QPoint(0,0),size.toSize());
122 d->m_rect.adjust(margin(),margin(), -margin(), -margin());
122 d->m_rect.adjust(margin(),margin(), -margin(), -margin());
123 d->m_grid->setPos(d->m_rect.topLeft());
123 d->m_grid->setPos(d->m_rect.topLeft());
124 d->m_grid->setSize(d->m_rect.size());
124 d->m_grid->setSize(d->m_rect.size());
125
125
126 // TODO: calculate the scale
126 // TODO: calculate the scale
127 // TODO: calculate the origo
127 // TODO: calculate the origo
128 // TODO: not sure if emitting a signal here is the best from performance point of view
128 // TODO: not sure if emitting a signal here is the best from performance point of view
129 const qreal xscale = size.width() / 100;
129 const qreal xscale = size.width() / 100;
130 const qreal yscale = size.height() / 100;
130 const qreal yscale = size.height() / 100;
131 emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale);
131 emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale);
132
132
133 for (int i(0); i < d->m_plotDomainList.size(); i++)
133 for (int i(0); i < d->m_plotDomainList.size(); i++)
134 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
134 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
135
135
136 // TODO: line chart items are updated separately as they don't support update
136 // TODO: line chart items are updated separately as they don't support update
137 // via sizeChanged signal
137 // via sizeChanged signal
138 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
138 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
139 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
139 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
140
140
141 if (d->m_plotDomainList.count())
141 d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex));
142 d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex));
142 update();
143 update();
143 }
144 }
144
145
145 int QChart::margin() const
146 int QChart::margin() const
146 {
147 {
147 return d->m_marginSize;
148 return d->m_marginSize;
148 }
149 }
149
150
150 void QChart::setMargin(int margin)
151 void QChart::setMargin(int margin)
151 {
152 {
152 d->m_marginSize = margin;
153 d->m_marginSize = margin;
153 }
154 }
154
155
155 #include "moc_qchart.cpp"
156 #include "moc_qchart.cpp"
156
157
157
158
158 QTCOMMERCIALCHART_END_NAMESPACE
159 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now