##// END OF EJS Templates
Enabled theme colors in scatter again
Tero Ahola -
r182:0dd3fe4314e9
parent child
Show More
@@ -1,33 +1,39
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <cmath>
3 #include <cmath>
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartview.h>
5 #include <qchartview.h>
6 #include <qscatterseries.h>
6 #include <qscatterseries.h>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
11 {
11 {
12 QApplication a(argc, argv);
12 QApplication a(argc, argv);
13
13
14 // Create widget and scatter series
14 // Create chart widget
15 QChartView *chartWidget = new QChartView();
15 QChartView *chartWidget = new QChartView();
16
17 // Create scatter series with simple test data
16 QScatterSeries *scatter = new QScatterSeries();
18 QScatterSeries *scatter = new QScatterSeries();
19 *scatter << QPointF(0.5, 2.0)
20 << QPointF(1.0, 2.5)
21 << QPointF(1.5, 2.0)
22 << QPointF(2.0, 2.5);
23 chartWidget->addSeries(scatter);
17
24
18 // Add test data to the series
25 // Add another scatter series with more complex data with random component
26 QScatterSeries *scatter2 = new QScatterSeries();
19 for (qreal i(0.0); i < 20; i += 0.5)
27 for (qreal i(0.0); i < 20; i += 0.5)
20 (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0,
28 (*scatter2) << QPointF(i + (qreal)(rand() % 100) / 100.0,
21 i + (qreal)(rand() % 100) / 100.0);
29 i + (qreal)(rand() % 100) / 100.0);
22
30 chartWidget->addSeries(scatter2);
23 // Add series to the chart widget
24 chartWidget->addSeries(scatter);
25
31
26 // Use the chart widget as the central widget
32 // Use the chart widget as the central widget
27 QMainWindow w;
33 QMainWindow w;
28 w.resize(640, 480);
34 w.resize(640, 480);
29 w.setCentralWidget(chartWidget);
35 w.setCentralWidget(chartWidget);
30 w.show();
36 w.show();
31
37
32 return a.exec();
38 return a.exec();
33 }
39 }
@@ -1,271 +1,272
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "barchartseries.h"
7 #include "barchartseries.h"
8 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
9 #include "percentbarchartseries.h"
9 #include "percentbarchartseries.h"
10 #include "qlinechartseries.h"
10 #include "qlinechartseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 //items
13 //items
14 #include "axisitem_p.h"
14 #include "axisitem_p.h"
15 #include "bargroup.h"
15 #include "bargroup.h"
16 #include "stackedbargroup.h"
16 #include "stackedbargroup.h"
17 #include "linechartitem_p.h"
17 #include "linechartitem_p.h"
18 #include "percentbargroup.h"
18 #include "percentbargroup.h"
19 #include "linechartanimationitem_p.h"
19 #include "linechartanimationitem_p.h"
20 #include "piepresenter.h"
20 #include "piepresenter.h"
21 #include "scatterpresenter.h"
21 #include "scatterpresenter.h"
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 m_chart(chart),
26 m_chart(chart),
27 m_dataset(dataset),
27 m_dataset(dataset),
28 m_chartTheme(0),
28 m_chartTheme(0),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,m_chart)),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,m_chart)),
30 m_axisYItem(new AxisItem(AxisItem::Y_AXIS,m_chart)),
30 m_axisYItem(new AxisItem(AxisItem::Y_AXIS,m_chart)),
31 m_domainIndex(0),
31 m_domainIndex(0),
32 m_marginSize(0),
32 m_marginSize(0),
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
34 {
34 {
35 setChartTheme(QChart::ChartThemeDefault);
35 setChartTheme(QChart::ChartThemeDefault);
36 createConnections();
36 createConnections();
37 }
37 }
38
38
39 ChartPresenter::~ChartPresenter()
39 ChartPresenter::~ChartPresenter()
40 {
40 {
41 }
41 }
42
42
43 void ChartPresenter::createConnections()
43 void ChartPresenter::createConnections()
44 {
44 {
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
46 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&)));
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&)));
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&)));
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&)));
50 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisYItem,SLOT(handleDomainChanged(const Domain&)));
51 }
51 }
52
52
53 void ChartPresenter::handleGeometryChanged()
53 void ChartPresenter::handleGeometryChanged()
54 {
54 {
55 m_rect = QRectF(QPoint(0,0),m_chart->size());
55 m_rect = QRectF(QPoint(0,0),m_chart->size());
56 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
56 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
57 Q_ASSERT(m_rect.isValid());
57 Q_ASSERT(m_rect.isValid());
58 emit geometryChanged(m_rect);
58 emit geometryChanged(m_rect);
59 }
59 }
60
60
61 int ChartPresenter::margin() const
61 int ChartPresenter::margin() const
62 {
62 {
63 return m_marginSize;
63 return m_marginSize;
64 }
64 }
65
65
66 void ChartPresenter::setMargin(int margin)
66 void ChartPresenter::setMargin(int margin)
67 {
67 {
68 m_marginSize = margin;
68 m_marginSize = margin;
69 }
69 }
70
70
71 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
71 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
72 {
72 {
73 qDebug() << " ChartPresenter::handleSeriesAdded";
73 qDebug() << " ChartPresenter::handleSeriesAdded";
74 switch(series->type())
74 switch(series->type())
75 {
75 {
76 case QChartSeries::SeriesTypeLine: {
76 case QChartSeries::SeriesTypeLine: {
77 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
77 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
78 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
78 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
79 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
79 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
80 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
80 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&)));
81 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
82 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
82 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
83 m_chartItems.insert(series,item);
83 m_chartItems.insert(series,item);
84 break;
84 break;
85 }
85 }
86
86
87 case QChartSeries::SeriesTypeBar: {
87 case QChartSeries::SeriesTypeBar: {
88 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
88 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
89 BarGroup* item = new BarGroup(barSeries->model(),m_chart);
89 BarGroup* item = new BarGroup(barSeries->model(),m_chart);
90 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
90 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
91 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
91 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&)));
92 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
93 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
93 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
94 m_chartItems.insert(series,item);
94 m_chartItems.insert(series,item);
95 // m_axisXItem->setVisible(false);
95 // m_axisXItem->setVisible(false);
96 break;
96 break;
97 }
97 }
98
98
99 case QChartSeries::SeriesTypeStackedBar: {
99 case QChartSeries::SeriesTypeStackedBar: {
100
100
101 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
101 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
102 StackedBarGroup* item = new StackedBarGroup(stackedBarSeries->model(),m_chart);
102 StackedBarGroup* item = new StackedBarGroup(stackedBarSeries->model(),m_chart);
103 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
103 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
104 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
104 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&)));
105 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
106 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
106 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
107 m_chartItems.insert(series,item);
107 m_chartItems.insert(series,item);
108 break;
108 break;
109 }
109 }
110
110
111 case QChartSeries::SeriesTypePercentBar: {
111 case QChartSeries::SeriesTypePercentBar: {
112
112
113 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
113 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
114 PercentBarGroup* item = new PercentBarGroup(percentBarSeries->model(),m_chart);
114 PercentBarGroup* item = new PercentBarGroup(percentBarSeries->model(),m_chart);
115 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
115 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
116 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
116 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&)));
117 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
118 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
118 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
119 m_chartItems.insert(series,item);
119 m_chartItems.insert(series,item);
120 break;
120 break;
121 }
121 }
122 case QChartSeries::SeriesTypeScatter: {
122 case QChartSeries::SeriesTypeScatter: {
123 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
123 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
124 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
124 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
125 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
125 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
126 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
126 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
127 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
127 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
128 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
128 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
129 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
129 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
130 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
130 // scatterSeries->d->setParentItem(this);
131 // scatterSeries->d->setParentItem(this);
131 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
132 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
132 m_chartItems.insert(scatterSeries, scatterPresenter);
133 m_chartItems.insert(scatterSeries, scatterPresenter);
133 break;
134 break;
134 }
135 }
135 case QChartSeries::SeriesTypePie: {
136 case QChartSeries::SeriesTypePie: {
136 QPieSeries *s = qobject_cast<QPieSeries *>(series);
137 QPieSeries *s = qobject_cast<QPieSeries *>(series);
137 PiePresenter* pie = new PiePresenter(m_chart, s);
138 PiePresenter* pie = new PiePresenter(m_chart, s);
138 m_chartTheme->decorate(pie, s, m_chartItems.count());
139 m_chartTheme->decorate(pie, s, m_chartItems.count());
139 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
140 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
140 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
141 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
141 m_chartItems.insert(series, pie);
142 m_chartItems.insert(series, pie);
142 break;
143 break;
143 }
144 }
144 default: {
145 default: {
145 qDebug()<< "Series type" << series->type() << "not implemented.";
146 qDebug()<< "Series type" << series->type() << "not implemented.";
146 break;
147 break;
147 }
148 }
148 }
149 }
149
150
150 if(m_rect.isValid()) emit geometryChanged(m_rect);
151 if(m_rect.isValid()) emit geometryChanged(m_rect);
151 }
152 }
152
153
153 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
154 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
154 {
155 {
155 //TODO:
156 //TODO:
156 }
157 }
157
158
158 void ChartPresenter::zoomInToRect(const QRectF& rect)
159 void ChartPresenter::zoomInToRect(const QRectF& rect)
159 {
160 {
160 if(!rect.isValid()) return;
161 if(!rect.isValid()) return;
161 QRectF r = rect.normalized();
162 QRectF r = rect.normalized();
162 r.translate(-m_marginSize, -m_marginSize);
163 r.translate(-m_marginSize, -m_marginSize);
163 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
164 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
164 m_dataset->addDomain(domain);
165 m_dataset->addDomain(domain);
165 }
166 }
166
167
167 void ChartPresenter::zoomIn()
168 void ChartPresenter::zoomIn()
168 {
169 {
169 if (!m_dataset->nextDomain()) {
170 if (!m_dataset->nextDomain()) {
170 QRectF rect = m_rect;
171 QRectF rect = m_rect;
171 rect.setWidth(rect.width()/2);
172 rect.setWidth(rect.width()/2);
172 rect.setHeight(rect.height()/2);
173 rect.setHeight(rect.height()/2);
173 rect.moveCenter(m_rect.center());
174 rect.moveCenter(m_rect.center());
174 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
175 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
175 m_dataset->addDomain(domain);
176 m_dataset->addDomain(domain);
176 }
177 }
177 }
178 }
178
179
179 void ChartPresenter::zoomOut()
180 void ChartPresenter::zoomOut()
180 {
181 {
181 m_dataset->previousDomain();
182 m_dataset->previousDomain();
182 }
183 }
183
184
184 void ChartPresenter::zoomReset()
185 void ChartPresenter::zoomReset()
185 {
186 {
186 m_dataset->clearDomains();
187 m_dataset->clearDomains();
187 }
188 }
188
189
189 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
190 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
190 {
191 {
191 delete m_chartTheme;
192 delete m_chartTheme;
192
193
193 m_chartTheme = ChartTheme::createTheme(theme);
194 m_chartTheme = ChartTheme::createTheme(theme);
194
195
195 m_chartTheme->decorate(m_chart);
196 m_chartTheme->decorate(m_chart);
196 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
197 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
197
198
198 int index=0;
199 int index=0;
199 while (i.hasNext()) {
200 while (i.hasNext()) {
200 i.next();
201 i.next();
201 index++;
202 index++;
202 m_chartTheme->decorate(i.value(),i.key(),index);
203 m_chartTheme->decorate(i.value(),i.key(),index);
203 }
204 }
204
205
205 m_chartTheme->decorate(m_axisX, m_axisXItem);
206 m_chartTheme->decorate(m_axisX, m_axisXItem);
206 m_chartTheme->decorate(m_axisY, m_axisYItem);
207 m_chartTheme->decorate(m_axisY, m_axisYItem);
207
208
208 }
209 }
209
210
210
211
211 QChart::ChartTheme ChartPresenter::chartTheme()
212 QChart::ChartTheme ChartPresenter::chartTheme()
212 {
213 {
213 return m_chartTheme->id();
214 return m_chartTheme->id();
214 }
215 }
215
216
216 void ChartPresenter::setDefaultAxisX(const QChartAxis& axis)
217 void ChartPresenter::setDefaultAxisX(const QChartAxis& axis)
217 {
218 {
218 //if(m_axisX != axis) {
219 //if(m_axisX != axis) {
219 m_axisX = axis;
220 m_axisX = axis;
220 m_axisXItem->handleAxisChanged(m_axisX);
221 m_axisXItem->handleAxisChanged(m_axisX);
221 //}
222 //}
222 }
223 }
223
224
224 void ChartPresenter::setDefaultAxisY(const QChartAxis& axis)
225 void ChartPresenter::setDefaultAxisY(const QChartAxis& axis)
225 {
226 {
226 // if(m_axisY != axis) {
227 // if(m_axisY != axis) {
227 m_axisY = axis;
228 m_axisY = axis;
228 m_axisYItem->handleAxisChanged(m_axisY);
229 m_axisYItem->handleAxisChanged(m_axisY);
229 //}
230 //}
230 }
231 }
231
232
232 QChartAxis ChartPresenter::defaultAxisX() const
233 QChartAxis ChartPresenter::defaultAxisX() const
233 {
234 {
234 return m_axisX;
235 return m_axisX;
235 }
236 }
236
237
237 QChartAxis ChartPresenter::defaultAxisY() const
238 QChartAxis ChartPresenter::defaultAxisY() const
238 {
239 {
239 return m_axisY;
240 return m_axisY;
240 }
241 }
241
242
242 QChartAxis ChartPresenter::axisY(int id) const
243 QChartAxis ChartPresenter::axisY(int id) const
243 {
244 {
244 return m_axis.value(id);
245 return m_axis.value(id);
245 }
246 }
246
247
247 int ChartPresenter::addAxisY(const QChartAxis& axis)
248 int ChartPresenter::addAxisY(const QChartAxis& axis)
248 {
249 {
249 int key =0 ;
250 int key =0 ;
250
251
251 while(m_axis.contains(key)){
252 while(m_axis.contains(key)){
252 key++;
253 key++;
253 //TODO overflow
254 //TODO overflow
254 }
255 }
255
256
256 m_axis.insert(key,axis);
257 m_axis.insert(key,axis);
257 m_axisItems.insert(key,new AxisItem(AxisItem::Y_AXIS,m_chart));
258 m_axisItems.insert(key,new AxisItem(AxisItem::Y_AXIS,m_chart));
258
259
259 return key;
260 return key;
260 }
261 }
261
262
262
263
263 void ChartPresenter::removeAxisY(int id)
264 void ChartPresenter::removeAxisY(int id)
264 {
265 {
265 m_axis.remove(id);
266 m_axis.remove(id);
266 delete m_axisItems.take(id);
267 delete m_axisItems.take(id);
267 }
268 }
268
269
269 #include "moc_chartpresenter_p.cpp"
270 #include "moc_chartpresenter_p.cpp"
270
271
271 QTCOMMERCIALCHART_END_NAMESPACE
272 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,220 +1,239
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4
4
5
5
6 //series
6 //series
7 #include "barchartseries.h"
7 #include "barchartseries.h"
8 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
9 #include "percentbarchartseries.h"
9 #include "percentbarchartseries.h"
10 #include "qlinechartseries.h"
10 #include "qlinechartseries.h"
11 #include "qscatterseries.h"
11 #include "qpieseries.h"
12 #include "qpieseries.h"
12
13
13 //items
14 //items
14 #include "axisitem_p.h"
15 #include "axisitem_p.h"
15 #include "bargroup.h"
16 #include "bargroup.h"
16 #include "stackedbargroup.h"
17 #include "stackedbargroup.h"
17 #include "linechartitem_p.h"
18 #include "linechartitem_p.h"
18 #include "percentbargroup.h"
19 #include "percentbargroup.h"
20 #include "scatterpresenter.h"
19 #include "piepresenter.h"
21 #include "piepresenter.h"
20
22
21 //themes
23 //themes
22 #include "chartthemevanilla_p.h"
24 #include "chartthemevanilla_p.h"
23 #include "chartthemeicy_p.h"
25 #include "chartthemeicy_p.h"
24 #include "chartthemegrayscale_p.h"
26 #include "chartthemegrayscale_p.h"
25 #include "chartthemescientific_p.h"
27 #include "chartthemescientific_p.h"
26
28
27
29
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
31
30 /* TODO
32 /* TODO
31 case QChart::ChartThemeUnnamed1:
33 case QChart::ChartThemeUnnamed1:
32 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
34 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
33 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
35 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
34 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
35 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
37
39
38 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
40 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
39 m_gradientEndColor = QColor(QRgb(0xffafafaf));
41 m_gradientEndColor = QColor(QRgb(0xffafafaf));
40 */
42 */
41
43
42 ChartTheme::ChartTheme(QChart::ChartTheme id)
44 ChartTheme::ChartTheme(QChart::ChartTheme id)
43 {
45 {
44 m_id = id;
46 m_id = id;
45 m_seriesColor.append(QRgb(0xff000000));
47 m_seriesColor.append(QRgb(0xff000000));
46 m_seriesColor.append(QRgb(0xff707070));
48 m_seriesColor.append(QRgb(0xff707070));
47 m_gradientStartColor = QColor(QRgb(0xffffffff));
49 m_gradientStartColor = QColor(QRgb(0xffffffff));
48 m_gradientEndColor = QColor(QRgb(0xffafafaf));
50 m_gradientEndColor = QColor(QRgb(0xffafafaf));
49 }
51 }
50
52
51
53
52 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
54 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
53 {
55 {
54 switch(theme) {
56 switch(theme) {
55 case QChart::ChartThemeDefault:
57 case QChart::ChartThemeDefault:
56 return new ChartTheme();
58 return new ChartTheme();
57 case QChart::ChartThemeVanilla:
59 case QChart::ChartThemeVanilla:
58 return new ChartThemeVanilla();
60 return new ChartThemeVanilla();
59 case QChart::ChartThemeIcy:
61 case QChart::ChartThemeIcy:
60 return new ChartThemeIcy();
62 return new ChartThemeIcy();
61 case QChart::ChartThemeGrayscale:
63 case QChart::ChartThemeGrayscale:
62 return new ChartThemeGrayscale();
64 return new ChartThemeGrayscale();
63 case QChart::ChartThemeScientific:
65 case QChart::ChartThemeScientific:
64 return new ChartThemeScientific();
66 return new ChartThemeScientific();
65 }
67 }
66 }
68 }
67
69
68 void ChartTheme::decorate(QChart* chart)
70 void ChartTheme::decorate(QChart* chart)
69 {
71 {
70 QLinearGradient backgroundGradient;
72 QLinearGradient backgroundGradient;
71 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
73 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
72 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
74 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
73 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
75 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
74 chart->setChartBackgroundBrush(backgroundGradient);
76 chart->setChartBackgroundBrush(backgroundGradient);
75 }
77 }
76 //TODO helper to by removed later
78 //TODO helper to by removed later
77 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
79 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
78 {
80 {
79 switch(series->type())
81 switch(series->type())
80 {
82 {
81 case QChartSeries::SeriesTypeLine: {
83 case QChartSeries::SeriesTypeLine: {
82 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
84 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
83 LineChartItem* i = static_cast<LineChartItem*>(item);
85 LineChartItem* i = static_cast<LineChartItem*>(item);
84 decorate(i,s,count);
86 decorate(i,s,count);
85 break;
87 break;
86 }
88 }
87 case QChartSeries::SeriesTypeBar: {
89 case QChartSeries::SeriesTypeBar: {
88 BarChartSeries* b = static_cast<BarChartSeries*>(series);
90 BarChartSeries* b = static_cast<BarChartSeries*>(series);
89 BarGroup* i = static_cast<BarGroup*>(item);
91 BarGroup* i = static_cast<BarGroup*>(item);
90 decorate(i,b,count);
92 decorate(i,b,count);
91 break;
93 break;
92 }
94 }
93 case QChartSeries::SeriesTypeStackedBar: {
95 case QChartSeries::SeriesTypeStackedBar: {
94 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
96 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
95 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
97 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
96 decorate(i,s,count);
98 decorate(i,s,count);
97 break;
99 break;
98 }
100 }
99 case QChartSeries::SeriesTypePercentBar: {
101 case QChartSeries::SeriesTypePercentBar: {
100 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
102 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
101 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
103 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
102 decorate(i,s,count);
104 decorate(i,s,count);
103 break;
105 break;
104 }
106 }
105 case QChartSeries::SeriesTypePie: {
107 case QChartSeries::SeriesTypePie: {
106 QPieSeries* s = static_cast<QPieSeries*>(series);
108 QPieSeries* s = static_cast<QPieSeries*>(series);
107 PiePresenter* i = static_cast<PiePresenter*>(item);
109 PiePresenter* i = static_cast<PiePresenter*>(item);
108 decorate(i,s,count);
110 decorate(i,s,count);
109 break;
111 break;
110 }
112 }
111 default:
113 default:
112 qDebug()<<"Wrong item to be decorated by theme";
114 qDebug()<<"Wrong item to be decorated by theme";
113 break;
115 break;
114 }
116 }
115
117
116 }
118 }
117
119
118 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
120 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
119 {
121 {
120 QPen pen;
122 QPen pen;
121 if(pen != series->pen()){
123 if(pen != series->pen()){
122 item->setPen(series->pen());
124 item->setPen(series->pen());
123 return;
125 return;
124 }
126 }
125 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
127 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
126 pen.setWidthF(2);
128 pen.setWidthF(2);
127 item->setPen(pen);
129 item->setPen(pen);
128 }
130 }
129
131
130 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
132 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
131 {
133 {
132 item->resetColors();
134 item->resetColors();
133 for (int i=0; i<m_seriesColor.count(); i++) {
135 for (int i=0; i<m_seriesColor.count(); i++) {
134 item->addColor(m_seriesColor.at(i));
136 item->addColor(m_seriesColor.at(i));
135 }
137 }
136 item->addColor(QColor(255,0,0,128));
138 item->addColor(QColor(255,0,0,128));
137 item->addColor(QColor(255,255,0,128));
139 item->addColor(QColor(255,255,0,128));
138 item->addColor(QColor(0,255,0,128));
140 item->addColor(QColor(0,255,0,128));
139 item->addColor(QColor(0,0,255,128));
141 item->addColor(QColor(0,0,255,128));
140 item->addColor(QColor(255,128,0,128));
142 item->addColor(QColor(255,128,0,128));
141 }
143 }
142
144
143 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
145 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
144 {
146 {
145 item->resetColors();
147 item->resetColors();
146 for (int i=0; i< m_seriesColor.count(); i++) {
148 for (int i=0; i< m_seriesColor.count(); i++) {
147 item->addColor(m_seriesColor.at(i));
149 item->addColor(m_seriesColor.at(i));
148 }
150 }
149 item->addColor(QColor(255,0,0,128));
151 item->addColor(QColor(255,0,0,128));
150 item->addColor(QColor(255,255,0,128));
152 item->addColor(QColor(255,255,0,128));
151 item->addColor(QColor(0,255,0,128));
153 item->addColor(QColor(0,255,0,128));
152 item->addColor(QColor(0,0,255,128));
154 item->addColor(QColor(0,0,255,128));
153 item->addColor(QColor(255,128,0,128));
155 item->addColor(QColor(255,128,0,128));
154 }
156 }
155
157
156 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
158 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
157 {
159 {
158 item->resetColors();
160 item->resetColors();
159 for (int i=0; i< m_seriesColor.count(); i++) {
161 for (int i=0; i< m_seriesColor.count(); i++) {
160 item->addColor(m_seriesColor.at(i));
162 item->addColor(m_seriesColor.at(i));
161 }
163 }
162 item->addColor(QColor(255,0,0,128));
164 item->addColor(QColor(255,0,0,128));
163 item->addColor(QColor(255,255,0,128));
165 item->addColor(QColor(255,255,0,128));
164 item->addColor(QColor(0,255,0,128));
166 item->addColor(QColor(0,255,0,128));
165 item->addColor(QColor(0,0,255,128));
167 item->addColor(QColor(0,0,255,128));
166 item->addColor(QColor(255,128,0,128));
168 item->addColor(QColor(255,128,0,128));
167 }
169 }
168
170
171 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
172 {
173 Q_ASSERT(presenter);
174 Q_ASSERT(series);
175
176 presenter->m_markerPen.setColor(m_seriesColor.at(count % m_seriesColor.size()));
177
178 // QPen pen;
179 // if(pen != series->pen()){
180 // item->setPen(series->pen());
181 // return;
182 // }
183 // pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
184 // pen.setWidthF(2);
185 // item->setPen(pen);
186 }
187
169 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
188 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
170 {
189 {
171 // create a list of slice colors based on current theme
190 // create a list of slice colors based on current theme
172 int i = 0;
191 int i = 0;
173 QList<QColor> colors;
192 QList<QColor> colors;
174 while (colors.count() < series->count()) {
193 while (colors.count() < series->count()) {
175
194
176 // get base color
195 // get base color
177 QColor c = m_seriesColor[i++];
196 QColor c = m_seriesColor[i++];
178 i = i % m_seriesColor.count();
197 i = i % m_seriesColor.count();
179
198
180 // -1 means achromatic color -> cannot manipulate lightness
199 // -1 means achromatic color -> cannot manipulate lightness
181 // TODO: find a better way to randomize lightness
200 // TODO: find a better way to randomize lightness
182 if (c.toHsv().hue() == -1)
201 if (c.toHsv().hue() == -1)
183 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
202 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
184
203
185 // randomize lightness
204 // randomize lightness
186 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
205 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
187 c = c.lighter(f);
206 c = c.lighter(f);
188
207
189 // find duplicates
208 // find duplicates
190 bool isUnique = true;
209 bool isUnique = true;
191 foreach (QColor color, colors) {
210 foreach (QColor color, colors) {
192 if (c == color)
211 if (c == color)
193 isUnique = false;
212 isUnique = false;
194 }
213 }
195
214
196 // add to array if unique
215 // add to array if unique
197 //if (isUnique)
216 //if (isUnique)
198 colors << c;
217 colors << c;
199 }
218 }
200
219
201 // finally update colors
220 // finally update colors
202 foreach (QPieSliceId id, series->ids()) {
221 foreach (QPieSliceId id, series->ids()) {
203 QPieSlice s = series->slice(id);
222 QPieSlice s = series->slice(id);
204 s.setPen(QPen(Qt::black)); // TODO: get from theme
223 s.setPen(QPen(Qt::black)); // TODO: get from theme
205 s.setBrush(colors.takeFirst());
224 s.setBrush(colors.takeFirst());
206 series->update(s);
225 series->update(s);
207 }
226 }
208 }
227 }
209
228
210
229
211 void ChartTheme::decorate(QChartAxis& axis,AxisItem* item)
230 void ChartTheme::decorate(QChartAxis& axis,AxisItem* item)
212 {
231 {
213 //TODO: dummy defults for now
232 //TODO: dummy defults for now
214
233
215 axis.setLabelsBrush(Qt::black);
234 axis.setLabelsBrush(Qt::black);
216 axis.setLabelsPen(Qt::NoPen);
235 axis.setLabelsPen(Qt::NoPen);
217 item->handleAxisChanged(axis);
236 item->handleAxisChanged(axis);
218 }
237 }
219
238
220 QTCOMMERCIALCHART_END_NAMESPACE
239 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,49 +1,52
1 #ifndef CHARTTHEME_H
1 #ifndef CHARTTHEME_H
2 #define CHARTTHEME_H
2 #define CHARTTHEME_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartItem;
10 class ChartItem;
11 class QChartSeries;
11 class QChartSeries;
12 class LineChartItem;
12 class LineChartItem;
13 class QLineChartSeries;
13 class QLineChartSeries;
14 class BarGroup;
14 class BarGroup;
15 class BarChartSeries;
15 class BarChartSeries;
16 class StackedBarGroup;
16 class StackedBarGroup;
17 class StackedBarChartSeries;
17 class StackedBarChartSeries;
18 class PercentBarChartSeries;
18 class PercentBarChartSeries;
19 class PercentBarGroup;
19 class PercentBarGroup;
20 class QScatterSeries;
21 class ScatterPresenter;
20 class PiePresenter;
22 class PiePresenter;
21 class QPieSeries;
23 class QPieSeries;
22
24
23 class ChartTheme
25 class ChartTheme
24 {
26 {
25 protected:
27 protected:
26 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
28 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
27 public:
29 public:
28 static ChartTheme* createTheme(QChart::ChartTheme theme);
30 static ChartTheme* createTheme(QChart::ChartTheme theme);
29 QChart::ChartTheme id() const {return m_id;}
31 QChart::ChartTheme id() const {return m_id;}
30 void decorate(QChart* chart);
32 void decorate(QChart* chart);
31 void decorate(ChartItem* item, QChartSeries* series,int count);
33 void decorate(ChartItem* item, QChartSeries* series,int count);
32 void decorate(LineChartItem* item, QLineChartSeries*, int count);
34 void decorate(LineChartItem* item, QLineChartSeries*, int count);
33 void decorate(BarGroup* item, BarChartSeries* series,int count);
35 void decorate(BarGroup* item, BarChartSeries* series,int count);
34 void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count);
36 void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count);
35 void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count);
37 void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count);
38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
36 void decorate(PiePresenter* item, QPieSeries* series, int count);
39 void decorate(PiePresenter* item, QPieSeries* series, int count);
37 void decorate(QChartAxis& axis,AxisItem* item);
40 void decorate(QChartAxis& axis,AxisItem* item);
38
41
39 protected:
42 protected:
40 QChart::ChartTheme m_id;
43 QChart::ChartTheme m_id;
41 QColor m_gradientStartColor;
44 QColor m_gradientStartColor;
42 QColor m_gradientEndColor;
45 QColor m_gradientEndColor;
43 QList<QColor> m_seriesColor;
46 QList<QColor> m_seriesColor;
44
47
45 };
48 };
46
49
47 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
48
51
49 #endif // CHARTTHEME_H
52 #endif // CHARTTHEME_H
@@ -1,52 +1,50
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QRectF>
5 #include <QRectF>
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class QScatterSeriesPrivate;
9 class QScatterSeriesPrivate;
10
10
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14 public:
14 public:
15 //QScatterSeries(QSeriesData *data, QObject *chart);
15 //QScatterSeries(QSeriesData *data, QObject *chart);
16 QScatterSeries(QObject *parent = 0);
16 QScatterSeries(QObject *parent = 0);
17 ~QScatterSeries();
17 ~QScatterSeries();
18
18
19 public: // from QChartSeries
19 public: // from QChartSeries
20 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
20 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
21
21
22 public:
22 public:
23 // TODO: the name of the function? addPoint? addData? addValue?
23 // TODO: the name of the function? addPoint? addData? addValue?
24 void addData(QPointF value);
24 void addData(QPointF value);
25 QScatterSeries& operator << (const QPointF &value);
25 QScatterSeries& operator << (const QPointF &value);
26
27 void setData(QList<QPointF> data);
26 void setData(QList<QPointF> data);
28
29 QList<QPointF> data();
27 QList<QPointF> data();
30
28
31 //TODO? void insertData(int index, QPointF data);
29 //TODO? void insertData(int index, QPointF data);
32
30
33 void setMarkerPen(QPen pen);
31 void setMarkerPen(QPen pen);
34 QPen markerPen();
32 QPen markerPen();
35 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
33 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
36 //void setMarkerShape(MarkerShape shape);
34 //void setMarkerShape(MarkerShape shape);
37
35
38 Q_SIGNALS:
36 Q_SIGNALS:
39 // TODO: move to PIMPL?
37 // TODO: move to PIMPL?
40 // TODO: more finegrained signaling for performance reasons
38 // TODO: more finegrained signaling for performance reasons
41 void changed();
39 void changed();
42
40
43 //public Q_SLOTS:
41 //public Q_SLOTS:
44 private:
42 private:
45 Q_DECLARE_PRIVATE(QScatterSeries)
43 Q_DECLARE_PRIVATE(QScatterSeries)
46 Q_DISABLE_COPY(QScatterSeries)
44 Q_DISABLE_COPY(QScatterSeries)
47 QScatterSeriesPrivate *const d;
45 QScatterSeriesPrivate *const d;
48 };
46 };
49
47
50 QTCOMMERCIALCHART_END_NAMESPACE
48 QTCOMMERCIALCHART_END_NAMESPACE
51
49
52 #endif // QSCATTERSERIES_H
50 #endif // QSCATTERSERIES_H
@@ -1,90 +1,94
1 #include "scatterpresenter.h"
1 #include "scatterpresenter.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include <QPen>
3 #include <QPen>
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
10 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
11 ChartItem(parent),
11 ChartItem(parent),
12 m_series(series),
12 m_series(series),
13 m_boundingRect(),
13 m_boundingRect(),
14 //m_markerColor(QColor()),
14 //m_markerColor(QColor()),
15 m_markerColor(QColor(255, 0, 0)),
15 // m_markerColor(QColor(255, 0, 0)),
16 m_visibleChartArea()
16 m_visibleChartArea()
17 {
17 {
18 if (parent)
18 if (parent)
19 m_boundingRect = parent->boundingRect();
19 m_boundingRect = parent->boundingRect();
20
20
21 if (series) {
21 if (series) {
22 connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged()));
22 connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged()));
23 }
23 }
24 }
24 }
25
25
26 void ScatterPresenter::handleDomainChanged(const Domain& domain)
26 void ScatterPresenter::handleDomainChanged(const Domain& domain)
27 {
27 {
28 m_visibleChartArea = domain;
28 m_visibleChartArea = domain;
29 changeGeometry();
29 changeGeometry();
30 }
30 }
31
31
32 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
32 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
33 {
33 {
34 m_boundingRect = rect;
34 m_boundingRect = rect;
35 changeGeometry();
35 changeGeometry();
36 }
36 }
37
37
38 void ScatterPresenter::handleModelChanged()
38 void ScatterPresenter::handleModelChanged()
39 {
39 {
40 // TODO: more fine grained modelChanged signaling
40 // TODO: more fine grained modelChanged signaling
41 changeGeometry();
41 changeGeometry();
42 }
42 }
43
43
44 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
44 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
45 {
45 {
46 // TODO: The opacity should be user definable?
46 // TODO: The opacity should be user definable?
47 //brush.setColor(QColor(255, 82, 0, 100));
47 //brush.setColor(QColor(255, 82, 0, 100));
48 if (m_markerColor.isValid()) {
48 //if (m_series->markerPen().isValid()) {
49 if (false) {
49 QPen pen = painter->pen();
50 QPen pen = painter->pen();
50 QBrush brush = pen.brush();
51 QBrush brush = pen.brush();
51 brush.setColor(m_markerColor);
52 brush.setColor(m_series->markerPen().color());
52 pen.setBrush(brush);
53 pen.setBrush(brush);
53 pen.setWidth(4);
54 pen.setWidth(4);
54 painter->setPen(pen);
55 painter->setPen(pen);
55 }
56 }
56 else {
57 else {
57 //painter->setPen(m_theme.markerPen);
58 // TODO: fix this
58 // brush.setColor(m_theme..lineColor);
59 QPen pen = painter->pen();
60 QBrush brush = pen.brush();
61 brush.setColor(m_markerPen.color());
62 pen.setBrush(brush);
63 pen.setWidth(4);
64 painter->setPen(pen);
59 }
65 }
60
66
61 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
62 // event right after construction or maybe given a size during initialization
63 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
67 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
64 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
68 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
65 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
69 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
66 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
70 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
67 }
71 }
68 }
72 }
69
73
70 void ScatterPresenter::changeGeometry()
74 void ScatterPresenter::changeGeometry()
71 {
75 {
72 if (m_boundingRect.isValid()) {
76 if (m_boundingRect.isValid()) {
73
77
74 prepareGeometryChange();
78 prepareGeometryChange();
75 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
79 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
76 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
80 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
77 m_scenex.clear();
81 m_scenex.clear();
78 m_sceney.clear();
82 m_sceney.clear();
79
83
80 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
84 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
81 foreach (QPointF point, m_series->data()) {
85 foreach (QPointF point, m_series->data()) {
82 m_scenex.append(m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex);
86 m_scenex.append(m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex);
83 m_sceney.append(m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley);
87 m_sceney.append(m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley);
84 }
88 }
85 }
89 }
86 }
90 }
87
91
88 #include "moc_scatterpresenter.cpp"
92 #include "moc_scatterpresenter.cpp"
89
93
90 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,47
1 #ifndef SCATTERPRESENTER_H
1 #ifndef SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
2 #define SCATTERPRESENTER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QObject>
6 #include <QObject>
7 #include <QPen>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class QScatterSeries;
11 class QScatterSeries;
11
12
12 /*!
13 /*!
13 * The "business logic" of scatter series. This is a QObject that does not have a parent QObject.
14 * The "business logic" of scatter series. This is a QObject that does not have a parent QObject.
14 * The QGraphicsItem parent owns the object instead.
15 * The QGraphicsItem parent owns the object instead.
15 */
16 */
16 class ScatterPresenter : public QObject, public ChartItem
17 class ScatterPresenter : public QObject, public ChartItem
17 {
18 {
18 Q_OBJECT
19 Q_OBJECT
19 public:
20 public:
20 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
21 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
21
22
22 public: // from ChartItem
23 public: // from ChartItem
23 QRectF boundingRect() const { return m_boundingRect; }
24 QRectF boundingRect() const { return m_boundingRect; }
24 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
25 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
25
26
26 signals:
27 signals:
27
28
28 public Q_SLOTS:
29 public Q_SLOTS:
29 void handleDomainChanged(const Domain& domain);
30 void handleDomainChanged(const Domain& domain);
30 void handleGeometryChanged(const QRectF& rect);
31 void handleGeometryChanged(const QRectF& rect);
31 void handleModelChanged();
32 void handleModelChanged();
32
33
33 public:
34 public:
34 void changeGeometry();
35 void changeGeometry();
35
36
36 QScatterSeries *m_series;
37 QScatterSeries *m_series;
37 QRectF m_boundingRect;
38 QRectF m_boundingRect;
38 QList<qreal> m_scenex;
39 QList<qreal> m_scenex;
39 QList<qreal> m_sceney;
40 QList<qreal> m_sceney;
40 QColor m_markerColor;
41 Domain m_visibleChartArea;
41 Domain m_visibleChartArea;
42 QPen m_markerPen;
42 };
43 };
43
44
44 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
45
46
46 #endif // SCATTERPRESENTER_H
47 #endif // SCATTERPRESENTER_H
General Comments 0
You need to be logged in to leave comments. Login now