##// END OF EJS Templates
Adds missing scatter intercation implementation...
Michal Klocek -
r541:4eee071cd5a1
parent child
Show More
@@ -1,51 +1,38
1 #include "mainwindow.h"
1 #include "mainwindow.h"
2 #include <qchartglobal.h>
2 #include <qchartglobal.h>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qchartaxis.h>
4 #include <QDebug>
5 #include <QDebug>
5
6
6 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
7
8
8 MainWindow::MainWindow(QWidget *parent)
9 MainWindow::MainWindow(QWidget *parent)
9 : QMainWindow(parent)
10 : QMainWindow(parent)
10 {
11 {
11 QChartView *chartView = new QChartView(this);
12 QChartView *chartView = new QChartView(this);
12 chartView->setChartTitle("Click to play with points");
13 chartView->setChartTitle("Click to remove scatter point");
13 chartView->setRenderHint(QPainter::Antialiasing);
14 chartView->setRenderHint(QPainter::Antialiasing);
14 setCentralWidget(chartView);
15 setCentralWidget(chartView);
15
16
16 m_scatter = new QScatterSeries();
17 m_scatter = new QScatterSeries();
17 for(qreal x(0.5); x <= 4.0; x += 0.5) {
18 for(qreal x(0.5); x <= 4.0; x += 0.5) {
18 for(qreal y(0.5); y <= 4.0; y += 0.5) {
19 for(qreal y(0.5); y <= 4.0; y += 0.5) {
19 *m_scatter << QPointF(x, y);
20 *m_scatter << QPointF(x, y);
20 }
21 }
21 }
22 }
22 chartView->addSeries(m_scatter);
23
23
24 // Add two more series
24 chartView->addSeries(m_scatter);
25 m_scatter2 = new QScatterSeries();
25 chartView->axisX()->setRange(0,4.5);
26 chartView->addSeries(m_scatter2);
26 chartView->axisY()->setRange(0,4.5);
27 m_scatter3 = new QScatterSeries();
28 chartView->addSeries(m_scatter3);
29
27
30 connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(clickPoint(QPointF)));
28 connect(m_scatter, SIGNAL(clicked(const QPointF&)), this, SLOT(handleClickedPoint(const QPointF&)));
31 }
29 }
32
30
33 MainWindow::~MainWindow()
31 MainWindow::~MainWindow()
34 {
32 {
35 }
33 }
36
34
37 void MainWindow::clickPoint(QPointF coordinate)
35 void MainWindow::handleClickedPoint(const QPointF& point)
38 {
36 {
39 // Remove the clicked point from the series and add points to the two other series we have
37 m_scatter->remove(point);
40 //TODO: fix me
41 /*
42 int index = m_scatter->closestPoint(coordinate);
43 QPointF point = m_scatter->data().at(index);
44 Q_ASSERT(m_scatter->removeAt(index));
45 point.rx() += 0.25;
46 point.ry() += 0.25;
47 *m_scatter2 << point;
48 point.ry() -= 0.25;
49 *m_scatter3 << point;
50 */
51 }
38 }
@@ -1,27 +1,27
1 #ifndef MAINWINDOW_H
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
2 #define MAINWINDOW_H
3
3
4 #include <QtGui/QMainWindow>
4 #include <QtGui/QMainWindow>
5 #include <qchartglobal.h>
5 #include <qchartglobal.h>
6 #include <qscatterseries.h>
6 #include <qscatterseries.h>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 class MainWindow : public QMainWindow
10 class MainWindow : public QMainWindow
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13
13
14 public:
14 public:
15 MainWindow(QWidget *parent = 0);
15 MainWindow(QWidget *parent = 0);
16 ~MainWindow();
16 ~MainWindow();
17
17
18 private Q_SLOTS:
18 private Q_SLOTS:
19 void clickPoint(QPointF coordinate);
19 void handleClickedPoint(const QPointF& point);
20
20
21 private:
21 private:
22 QScatterSeries *m_scatter;
22 QScatterSeries *m_scatter;
23 QScatterSeries *m_scatter2;
23 QScatterSeries *m_scatter2;
24 QScatterSeries *m_scatter3;
24 QScatterSeries *m_scatter3;
25 };
25 };
26
26
27 #endif // MAINWINDOW_H
27 #endif // MAINWINDOW_H
@@ -1,310 +1,316
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "qchartaxis.h"
4 #include "qchartaxis.h"
5 #include <QTime>
5 #include <QTime>
6
6
7 //series
7 //series
8 #include "qbarset.h"
8 #include "qbarset.h"
9 #include "qbarseries.h"
9 #include "qbarseries.h"
10 #include "qstackedbarseries.h"
10 #include "qstackedbarseries.h"
11 #include "qpercentbarseries.h"
11 #include "qpercentbarseries.h"
12 #include "qlineseries.h"
12 #include "qlineseries.h"
13 #include "qareaseries.h"
13 #include "qareaseries.h"
14 #include "qscatterseries.h"
14 #include "qscatterseries.h"
15 #include "qpieseries.h"
15 #include "qpieseries.h"
16 #include "qpieslice.h"
16 #include "qpieslice.h"
17 #include "qsplineseries.h"
17 #include "qsplineseries.h"
18
18
19 //items
19 //items
20 #include "axisitem_p.h"
20 #include "axisitem_p.h"
21 #include "barpresenter_p.h"
21 #include "barpresenter_p.h"
22 #include "stackedbarpresenter_p.h"
22 #include "stackedbarpresenter_p.h"
23 #include "percentbarpresenter_p.h"
23 #include "percentbarpresenter_p.h"
24 #include "linechartitem_p.h"
24 #include "linechartitem_p.h"
25 #include "areachartitem_p.h"
25 #include "areachartitem_p.h"
26 #include "scatterchartitem_p.h"
26 #include "scatterchartitem_p.h"
27 #include "piepresenter_p.h"
27 #include "piepresenter_p.h"
28 #include "splinechartitem_p.h"
28 #include "splinechartitem_p.h"
29
29
30 //themes
30 //themes
31 #include "chartthemedefault_p.h"
31 #include "chartthemedefault_p.h"
32 #include "chartthemevanilla_p.h"
32 #include "chartthemevanilla_p.h"
33 #include "chartthemeicy_p.h"
33 #include "chartthemeicy_p.h"
34 #include "chartthemegrayscale_p.h"
34 #include "chartthemegrayscale_p.h"
35 #include "chartthemescientific_p.h"
35 #include "chartthemescientific_p.h"
36
36
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 ChartTheme::ChartTheme(QChart::ChartTheme id)
40 ChartTheme::ChartTheme(QChart::ChartTheme id)
41 {
41 {
42 m_id = id;
42 m_id = id;
43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 }
44 }
45
45
46
46
47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
48 {
48 {
49 switch(theme) {
49 switch(theme) {
50 case QChart::ChartThemeVanilla:
50 case QChart::ChartThemeVanilla:
51 return new ChartThemeVanilla();
51 return new ChartThemeVanilla();
52 case QChart::ChartThemeIcy:
52 case QChart::ChartThemeIcy:
53 return new ChartThemeIcy();
53 return new ChartThemeIcy();
54 case QChart::ChartThemeGrayscale:
54 case QChart::ChartThemeGrayscale:
55 return new ChartThemeGrayscale();
55 return new ChartThemeGrayscale();
56 case QChart::ChartThemeScientific:
56 case QChart::ChartThemeScientific:
57 return new ChartThemeScientific();
57 return new ChartThemeScientific();
58 default:
58 default:
59 return new ChartThemeDefault();
59 return new ChartThemeDefault();
60 }
60 }
61 }
61 }
62
62
63 void ChartTheme::decorate(QChart* chart)
63 void ChartTheme::decorate(QChart* chart)
64 {
64 {
65 chart->setChartBackgroundBrush(m_backgroundGradient);
65 chart->setChartBackgroundBrush(m_backgroundGradient);
66 }
66 }
67
67
68 void ChartTheme::decorate(QLegend* legend)
68 void ChartTheme::decorate(QLegend* legend)
69 {
69 {
70 legend->setBackgroundBrush(m_backgroundGradient);
70 legend->setBackgroundBrush(m_backgroundGradient);
71 }
71 }
72
72
73 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
73 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
74 {
74 {
75 QPen pen;
75 QPen pen;
76 QBrush brush;
76 QBrush brush;
77
77
78 if (pen != series->pen()){
78 if (pen != series->pen()){
79 item->setPen(series->pen());
79 item->setPen(series->pen());
80 } else {
80 } else {
81 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
81 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
82 pen.setWidthF(2);
82 pen.setWidthF(2);
83 item->setPen(pen);
83 item->setPen(pen);
84 }
84 }
85
85
86 if (brush != series->brush()) {
86 if (brush != series->brush()) {
87 item->setBrush(series->brush());
87 item->setBrush(series->brush());
88 } else {
88 } else {
89 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
89 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
90 item->setBrush(brush);
90 item->setBrush(brush);
91 }
91 }
92 }
92 }
93
93
94
94
95 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index)
95 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index)
96 {
96 {
97 QPen pen;
97 QPen pen;
98 if(pen != series->pen()){
98 if(pen != series->pen()){
99 item->setLinePen(series->pen());
99 item->setLinePen(series->pen());
100 return;
100 return;
101 }
101 }
102 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
102 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
103 pen.setWidthF(2);
103 pen.setWidthF(2);
104 item->setLinePen(pen);
104 item->setLinePen(pen);
105 }
105 }
106
106
107 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
107 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
108 {
108 {
109 QList<QBarSet*> sets = series->barSets();
109 QList<QBarSet*> sets = series->barSets();
110 for (int i=0; i<sets.count(); i++) {
110 for (int i=0; i<sets.count(); i++) {
111 qreal pos = 0.5;
111 qreal pos = 0.5;
112 if (sets.count() > 1)
112 if (sets.count() > 1)
113 pos = (qreal) i / (qreal) (sets.count() - 1);
113 pos = (qreal) i / (qreal) (sets.count() - 1);
114 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
114 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
115 sets.at(i)->setBrush(QBrush(c));
115 sets.at(i)->setBrush(QBrush(c));
116
116
117 // Pick label color as far as possible from bar color (within gradient).
117 // Pick label color as far as possible from bar color (within gradient).
118 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
118 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
119 // TODO: better picking of label color?
119 // TODO: better picking of label color?
120 if (pos < 0.3) {
120 if (pos < 0.3) {
121 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
121 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
122 } else {
122 } else {
123 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
123 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
124 }
124 }
125 sets.at(i)->setFloatingValuePen(QPen(c));
125 sets.at(i)->setFloatingValuePen(QPen(c));
126 }
126 }
127 }
127 }
128
128
129 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
129 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
130 {
130 {
131 QList<QBarSet*> sets = series->barSets();
131 QList<QBarSet*> sets = series->barSets();
132 for (int i=0; i<sets.count(); i++) {
132 for (int i=0; i<sets.count(); i++) {
133 qreal pos = 0.5;
133 qreal pos = 0.5;
134 if (sets.count() > 1)
134 if (sets.count() > 1)
135 pos = (qreal) i / (qreal) (sets.count() - 1);
135 pos = (qreal) i / (qreal) (sets.count() - 1);
136 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
136 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
137 sets.at(i)->setBrush(QBrush(c));
137 sets.at(i)->setBrush(QBrush(c));
138
138
139 if (pos < 0.3) {
139 if (pos < 0.3) {
140 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
140 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
141 } else {
141 } else {
142 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
142 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
143 }
143 }
144 sets.at(i)->setFloatingValuePen(QPen(c));
144 sets.at(i)->setFloatingValuePen(QPen(c));
145 }
145 }
146 }
146 }
147
147
148 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
148 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
149 {
149 {
150 QList<QBarSet*> sets = series->barSets();
150 QList<QBarSet*> sets = series->barSets();
151 for (int i=0; i<sets.count(); i++) {
151 for (int i=0; i<sets.count(); i++) {
152 qreal pos = 0.5;
152 qreal pos = 0.5;
153 if (sets.count() > 1)
153 if (sets.count() > 1)
154 pos = (qreal) i / (qreal) (sets.count() - 1);
154 pos = (qreal) i / (qreal) (sets.count() - 1);
155 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
155 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
156 sets.at(i)->setBrush(QBrush(c));
156 sets.at(i)->setBrush(QBrush(c));
157
157
158 if (pos < 0.3) {
158 if (pos < 0.3) {
159 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
159 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
160 } else {
160 } else {
161 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
161 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
162 }
162 }
163 sets.at(i)->setFloatingValuePen(QPen(c));
163 sets.at(i)->setFloatingValuePen(QPen(c));
164 }
164 }
165 }
165 }
166
166
167 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index)
167 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index)
168 {
168 {
169 Q_ASSERT(item);
169 Q_ASSERT(item);
170 Q_ASSERT(series);
170 Q_ASSERT(series);
171
171
172 // Use a base color for brush
172 QPen pen;
173 item->setBrush(m_seriesColors.at(index % m_seriesColors.size()));
173 QBrush brush;
174
174
175 // Take pen near from gradient start, effectively using a lighter color for outline
175 if (pen == series->pen()) {
176 QPen pen(QBrush(Qt::SolidPattern), 3);
176 pen.setColor(colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1.0));
177 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
177 pen.setWidthF(2);
178 item->setPen(pen);
178 series->setPen(pen);
179 }
180
181 if (brush == series->brush()) {
182 QBrush brush(m_seriesColors.at(count % m_seriesColors.size()));
183 series->setBrush(brush);
184 }
179 }
185 }
180
186
181 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
187 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
182 {
188 {
183 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
189 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
184 for (int i(0); i < series->slices().count(); i++) {
190 for (int i(0); i < series->slices().count(); i++) {
185 qreal pos = (qreal) i / (qreal) series->count();
191 qreal pos = (qreal) i / (qreal) series->count();
186 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
192 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
187 series->slices().at(i)->setSlicePen(penColor);
193 series->slices().at(i)->setSlicePen(penColor);
188 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
194 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
189 series->slices().at(i)->setSliceBrush(brushColor);
195 series->slices().at(i)->setSliceBrush(brushColor);
190 }
196 }
191 }
197 }
192
198
193
199
194 void ChartTheme::decorate(QChartAxis* axis, AxisItem* item)
200 void ChartTheme::decorate(QChartAxis* axis, AxisItem* item)
195 {
201 {
196 //TODO: dummy defults for now
202 //TODO: dummy defults for now
197 axis->setLabelsBrush(Qt::black);
203 axis->setLabelsBrush(Qt::black);
198 axis->setLabelsPen(Qt::NoPen);
204 axis->setLabelsPen(Qt::NoPen);
199 axis->setShadesPen(Qt::NoPen);
205 axis->setShadesPen(Qt::NoPen);
200 axis->setShadesOpacity(0.5);
206 axis->setShadesOpacity(0.5);
201 }
207 }
202
208
203 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index)
209 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index)
204 {
210 {
205 Q_ASSERT(item);
211 Q_ASSERT(item);
206 Q_ASSERT(series);
212 Q_ASSERT(series);
207
213
208 QPen pen;
214 QPen pen;
209
215
210 if(pen != series->pen()){
216 if(pen != series->pen()){
211 item->setLinePen(series->pen());
217 item->setLinePen(series->pen());
212 }else{
218 }else{
213 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
219 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
214 pen.setWidthF(series->pen().widthF());
220 pen.setWidthF(series->pen().widthF());
215 item->setLinePen(series->pen());
221 item->setLinePen(series->pen());
216 }
222 }
217
223
218 // QColor color = m_seriesColors.at(index % m_seriesColors.size());
224 // QColor color = m_seriesColors.at(index % m_seriesColors.size());
219 // TODO: define alpha in the theme? or in the series?
225 // TODO: define alpha in the theme? or in the series?
220 //color.setAlpha(120);
226 //color.setAlpha(120);
221
227
222 // QBrush brush(color, Qt::SolidPattern);
228 // QBrush brush(color, Qt::SolidPattern);
223 // presenter->m_markerBrush = brush;
229 // presenter->m_markerBrush = brush;
224
230
225 // QPen pen(brush, 3);
231 // QPen pen(brush, 3);
226 // pen.setColor(color);
232 // pen.setColor(color);
227 // presenter->m_markerPen = pen;
233 // presenter->m_markerPen = pen;
228 }
234 }
229
235
230 void ChartTheme::generateSeriesGradients()
236 void ChartTheme::generateSeriesGradients()
231 {
237 {
232 // Generate gradients in HSV color space
238 // Generate gradients in HSV color space
233 foreach (QColor color, m_seriesColors) {
239 foreach (QColor color, m_seriesColors) {
234 QLinearGradient g;
240 QLinearGradient g;
235 qreal h = color.hsvHueF();
241 qreal h = color.hsvHueF();
236 qreal s = color.hsvSaturationF();
242 qreal s = color.hsvSaturationF();
237
243
238 // TODO: tune the algorithm to give nice results with most base colors defined in
244 // TODO: tune the algorithm to give nice results with most base colors defined in
239 // most themes. The rest of the gradients we can define manually in theme specific
245 // most themes. The rest of the gradients we can define manually in theme specific
240 // implementation.
246 // implementation.
241 QColor start = color;
247 QColor start = color;
242 start.setHsvF(h, 0.05, 0.95);
248 start.setHsvF(h, 0.05, 0.95);
243 g.setColorAt(0.0, start);
249 g.setColorAt(0.0, start);
244
250
245 g.setColorAt(0.5, color);
251 g.setColorAt(0.5, color);
246
252
247 QColor end = color;
253 QColor end = color;
248 end.setHsvF(h, s, 0.25);
254 end.setHsvF(h, s, 0.25);
249 g.setColorAt(1.0, end);
255 g.setColorAt(1.0, end);
250
256
251 m_seriesGradients << g;
257 m_seriesGradients << g;
252 }
258 }
253 }
259 }
254
260
255
261
256 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
262 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
257 {
263 {
258 Q_ASSERT(pos >=0.0 && pos <= 1.0);
264 Q_ASSERT(pos >=0.0 && pos <= 1.0);
259 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
265 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
260 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
266 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
261 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
267 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
262 QColor c;
268 QColor c;
263 c.setRgbF(r, g, b);
269 c.setRgbF(r, g, b);
264 return c;
270 return c;
265 }
271 }
266
272
267 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
273 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
268 {
274 {
269 Q_ASSERT(pos >=0 && pos <= 1.0);
275 Q_ASSERT(pos >=0 && pos <= 1.0);
270
276
271 // another possibility:
277 // another possibility:
272 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
278 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
273
279
274 QGradientStops stops = gradient.stops();
280 QGradientStops stops = gradient.stops();
275 int count = stops.count();
281 int count = stops.count();
276
282
277 // find previous stop relative to position
283 // find previous stop relative to position
278 QGradientStop prev = stops.first();
284 QGradientStop prev = stops.first();
279 for (int i=0; i<count; i++) {
285 for (int i=0; i<count; i++) {
280 QGradientStop stop = stops.at(i);
286 QGradientStop stop = stops.at(i);
281 if (pos > stop.first)
287 if (pos > stop.first)
282 prev = stop;
288 prev = stop;
283
289
284 // given position is actually a stop position?
290 // given position is actually a stop position?
285 if (pos == stop.first) {
291 if (pos == stop.first) {
286 //qDebug() << "stop color" << pos;
292 //qDebug() << "stop color" << pos;
287 return stop.second;
293 return stop.second;
288 }
294 }
289 }
295 }
290
296
291 // find next stop relative to position
297 // find next stop relative to position
292 QGradientStop next = stops.last();
298 QGradientStop next = stops.last();
293 for (int i=count-1; i>=0; i--) {
299 for (int i=count-1; i>=0; i--) {
294 QGradientStop stop = stops.at(i);
300 QGradientStop stop = stops.at(i);
295 if (pos < stop.first)
301 if (pos < stop.first)
296 next = stop;
302 next = stop;
297 }
303 }
298
304
299 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
305 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
300
306
301 qreal range = next.first - prev.first;
307 qreal range = next.first - prev.first;
302 qreal posDelta = pos - prev.first;
308 qreal posDelta = pos - prev.first;
303 qreal relativePos = posDelta / range;
309 qreal relativePos = posDelta / range;
304
310
305 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
311 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
306
312
307 return colorAt(prev.second, next.second, relativePos);
313 return colorAt(prev.second, next.second, relativePos);
308 }
314 }
309
315
310 QTCOMMERCIALCHART_END_NAMESPACE
316 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,108 +1,103
1 #include "qscatterseries.h"
1 #include "qscatterseries.h"
2 #include "qchart.h"
2 #include "qchart.h"
3
3
4 /*!
4 /*!
5 \class QScatterSeries
5 \class QScatterSeries
6 \brief The QScatterSeries class is used for making scatter charts.
6 \brief The QScatterSeries class is used for making scatter charts.
7
7
8 \mainclass
8 \mainclass
9
9
10 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
10 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
11 and the vertical axis.
11 and the vertical axis.
12
12
13 \image scatterchart.png
13 \image scatterchart.png
14
14
15 Creating basic scatter chart is simple:
15 Creating basic scatter chart is simple:
16 \code
16 \code
17 QScatterSeries* series = new QScatterSeries();
17 QScatterSeries* series = new QScatterSeries();
18 series->add(0, 6);
18 series->add(0, 6);
19 series->add(2, 4);
19 series->add(2, 4);
20 ...
20 ...
21 chartView->addSeries(series);
21 chartView->addSeries(series);
22 \endcode
22 \endcode
23 */
23 */
24
24
25 /*!
25 /*!
26 \enum QScatterSeries::MarkerShape
26 \enum QScatterSeries::MarkerShape
27
27
28 This enum describes the shape used when rendering marker items.
28 This enum describes the shape used when rendering marker items.
29
29
30 \value MarkerShapeDefault
31 \value MarkerShapeX
32 \value MarkerShapeRectangle
33 \value MarkerShapeRoundedRectangle
34 \value MarkerShapeTiltedRectangle
35 \value MarkerShapeTriangle
36 \value MarkerShapeCircle
30 \value MarkerShapeCircle
31 \value MarkerShapeRectangle
37 */
32 */
38
33
39 /*!
34 /*!
40 \fn QChartSeriesType QScatterSeries::type() const
35 \fn QChartSeriesType QScatterSeries::type() const
41 \brief Returns QChartSeries::SeriesTypeScatter.
36 \brief Returns QChartSeries::SeriesTypeScatter.
42 \sa QSeries, QSeriesType
37 \sa QSeries, QSeriesType
43 */
38 */
44
39
45 /*!
40 /*!
46 \fn void QScatterSeries::clicked(const QPointF& point)
41 \fn void QScatterSeries::clicked(const QPointF& point)
47 \brief Signal is emitted when user clicks the \a point on scatter chart.
42 \brief Signal is emitted when user clicks the \a point on scatter chart.
48 */
43 */
49
44
50 QTCOMMERCIALCHART_BEGIN_NAMESPACE
45 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51
46
52 /*!
47 /*!
53 Constructs a series object which is a child of \a parent.
48 Constructs a series object which is a child of \a parent.
54 */
49 */
55 QScatterSeries::QScatterSeries(QObject *parent) :
50 QScatterSeries::QScatterSeries(QObject *parent) :
56 QXYSeries(parent),
51 QXYSeries(parent),
57 m_shape(QScatterSeries::MarkerShapeDefault),
52 m_shape(QScatterSeries::MarkerShapeCircle),
58 m_size(9.0)
53 m_size(9.0)
59 {
54 {
60 }
55 }
61
56
62 /*!
57 /*!
63 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
58 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
64 */
59 */
65 QScatterSeries::~QScatterSeries()
60 QScatterSeries::~QScatterSeries()
66 {
61 {
67 }
62 }
68
63
69 /*!
64 /*!
70 Returns the shape used for drawing markers.
65 Returns the shape used for drawing markers.
71 */
66 */
72 QScatterSeries::MarkerShape QScatterSeries::shape() const
67 QScatterSeries::MarkerShape QScatterSeries::shape() const
73 {
68 {
74 return (QScatterSeries::MarkerShape) m_shape;
69 return (QScatterSeries::MarkerShape) m_shape;
75 }
70 }
76
71
77 /*!
72 /*!
78 Overrides the default shape of the marker items with a user defined \a shape. The default shape
73 Overrides the default shape of the marker items with a user defined \a shape. The default shape
79 is defined by chart theme setting.
74 is defined by chart theme setting.
80 */
75 */
81 void QScatterSeries::setShape(MarkerShape shape)
76 void QScatterSeries::setShape(MarkerShape shape)
82 {
77 {
83 m_shape = shape;
78 m_shape = shape;
84 emit updated();
79 emit updated();
85 }
80 }
86
81
87 /*!
82 /*!
88 Returns the size of the marker items.
83 Returns the size of the marker items.
89 */
84 */
90 qreal QScatterSeries::size() const
85 qreal QScatterSeries::size() const
91 {
86 {
92 return m_size;
87 return m_size;
93 }
88 }
94
89
95 /*!
90 /*!
96 Set the \a size of the marker items. The default size is 9.0.
91 Set the \a size of the marker items. The default size is 9.0.
97 */
92 */
98 void QScatterSeries::setSize(qreal size)
93 void QScatterSeries::setSize(qreal size)
99 {
94 {
100 m_size = size;
95 m_size = size;
101 emit updated();
96 emit updated();
102 }
97 }
103
98
104
99
105
100
106 #include "moc_qscatterseries.cpp"
101 #include "moc_qscatterseries.cpp"
107
102
108 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,45
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qxyseries.h"
5 #include "qxyseries.h"
6 #include <QRectF>
6 #include <QRectF>
7 #include <QColor>
7 #include <QColor>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 class QScatterSeriesPrivate;
10 class QScatterSeriesPrivate;
11
11
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15
15
16 public:
16 public:
17 enum MarkerShape {
17 enum MarkerShape {
18 // TODO: to be defined by the graphics design
18 MarkerShapeCircle,
19 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
19 MarkerShapeRectangle
20 MarkerShapeDefault = 0,
21 MarkerShapeX,
22 MarkerShapeRectangle,
23 MarkerShapeRoundedRectangle,
24 MarkerShapeTiltedRectangle,
25 MarkerShapeTriangle,
26 MarkerShapeCircle
27 };
20 };
28
21
29 public:
22 public:
30 QScatterSeries(QObject *parent = 0);
23 QScatterSeries(QObject *parent = 0);
31 ~QScatterSeries();
24 ~QScatterSeries();
32
25
33 public: // from QChartSeries
26 public: // from QChartSeries
34 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
27 QSeriesType type() const { return QSeries::SeriesTypeScatter; }
35
28
36 public:
29 public:
37 MarkerShape shape() const;
30 MarkerShape shape() const;
38 void setShape(MarkerShape shape);
31 void setShape(MarkerShape shape);
39 qreal size() const;
32 qreal size() const;
40 void setSize(qreal size);
33 void setSize(qreal size);
41
34
42 signals:
35 signals:
43 void clicked(const QPointF& point);
36 void clicked(const QPointF& point);
44
37
45 private:
38 private:
46 MarkerShape m_shape;
39 MarkerShape m_shape;
47 qreal m_size;
40 qreal m_size;
48 };
41 };
49
42
50 QTCOMMERCIALCHART_END_NAMESPACE
43 QTCOMMERCIALCHART_END_NAMESPACE
51
44
52 #endif // QSCATTERSERIES_H
45 #endif // QSCATTERSERIES_H
@@ -1,185 +1,177
1 #include "scatterchartitem_p.h"
1 #include "scatterchartitem_p.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
16 XYChartItem(series,parent),
16 XYChartItem(series,parent),
17 m_series(series),
17 m_series(series),
18 m_items(this),
18 m_items(this),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
19 m_shape(QScatterSeries::MarkerShapeRectangle),
20 m_size(10)
20 m_size(10)
21
21
22 {
22 {
23 Q_ASSERT(parent);
23 Q_ASSERT(parent);
24 Q_ASSERT(series);
24 Q_ASSERT(series);
25
25
26 connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
27
28
28 setZValue(ChartPresenter::ScatterSeriesZValue);
29 setZValue(ChartPresenter::ScatterSeriesZValue);
29 setFlags(QGraphicsItem::ItemHasNoContents);
30 setFlags(QGraphicsItem::ItemHasNoContents);
30 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
31
32
32 handleUpdated();
33 handleUpdated();
33
34
35 m_items.setHandlesChildEvents(false);
36
34 // TODO: how to draw a drop shadow?
37 // TODO: how to draw a drop shadow?
35 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
36 // dropShadow->setOffset(2.0);
39 // dropShadow->setOffset(2.0);
37 // dropShadow->setBlurRadius(2.0);
40 // dropShadow->setBlurRadius(2.0);
38 // setGraphicsEffect(dropShadow);
41 // setGraphicsEffect(dropShadow);
39 }
42 }
40
43
41
44
42 QRectF ScatterChartItem::boundingRect() const
45 QRectF ScatterChartItem::boundingRect() const
43 {
46 {
44 return m_rect;
47 return m_rect;
45 }
48 }
46
49
47 void ScatterChartItem::createPoints(int count)
50 void ScatterChartItem::createPoints(int count)
48 {
51 {
49 for (int i = 0; i < count; ++i) {
52 for (int i = 0; i < count; ++i) {
50
53
51 QGraphicsItem *item;
54 QGraphicsItem *item;
52
55
53 switch (m_shape) {
56 switch (m_shape) {
54 case QScatterSeries::MarkerShapeDefault:
57 case QScatterSeries::MarkerShapeCircle:{
55 // Fallthrough, defaults to circle
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
56 case QScatterSeries::MarkerShapeCircle:
59 const QRectF& rect = i->boundingRect();
57 item = new QGraphicsEllipseItem(0,0,m_size,m_size);
60 i->setPos(-rect.width()/2,-rect.height()/2);
58 break;
61 item = new Marker(i,this);
59 case QScatterSeries::MarkerShapeRectangle:
60 item = new QGraphicsRectItem(0,0,m_size,m_size);
61 break;
62 break;
62 case QScatterSeries::MarkerShapeRoundedRectangle:
63 }
63 //m_path.addRoundedRect(x, y, size, size, size / 4.0, size / 4.0);
64 case QScatterSeries::MarkerShapeRectangle:{
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
66 i->setPos(-m_size/2,-m_size/2);
67 item = new Marker(i,this);
64 break;
68 break;
65 case QScatterSeries::MarkerShapeTiltedRectangle:
69 }
66 // TODO: tilt the rectangle
70 default:
67 //m_path.addRect(x, y, size, size);
71 qWarning()<<"Unsupported marker type";
68 //break;
69 case QScatterSeries::MarkerShapeTriangle:
70 //QPolygonF polygon;
71 //polygon << QPointF(0.0, -size) << QPointF(size / 2.0, 0.0) << QPointF(-size / 2, 0.0);
72 // TODO: the position is not exactly right...
73 //m_path.addPolygon(polygon.translated(x + size / 2.0, y + size));
74 break;
72 break;
73
75 }
74 }
76 m_items.addToGroup(item);
75 m_items.addToGroup(item);
77 }
76 }
78 }
77 }
79
78
80 void ScatterChartItem::deletePoints(int count)
79 void ScatterChartItem::deletePoints(int count)
81 {
80 {
82 QList<QGraphicsItem *> items = m_items.childItems();
81 QList<QGraphicsItem *> items = m_items.childItems();
83
82
84 for (int i = 0; i < count; ++i) {
83 for (int i = 0; i < count; ++i) {
85 delete(items.takeLast());
84 delete(items.takeLast());
86 }
85 }
87 }
86 }
88
87
89 /*
88 void ScatterChartItem::markerSelected(Marker* marker)
90
91 void ScatterChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
92 {
89 {
93
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
94 QPointF clickedPoint(
95 m_minX + (event->lastPos().x() / m_clippingRect.width()) * (m_maxX-m_minX),
96 m_maxY - (event->lastPos().y() / m_clippingRect.height()) * (m_maxY-m_minY));
97 emit clicked(clickedPoint);
98
99 }
91 }
100 */
101
92
102 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
103 {
94 {
104 if(points.size()==0) return;
95 if(points.size()==0) return;
105
96
106 int diff = XYChartItem::points().size() - points.size();
97 int diff = XYChartItem::points().size() - points.size();
107
98
108 if(diff>0) {
99 if(diff>0) {
109 deletePoints(diff);
100 deletePoints(diff);
110 }
101 }
111 else if(diff<0) {
102 else if(diff<0) {
112 createPoints(-diff);
103 createPoints(-diff);
113 }
104 }
114
105
115 if(diff!=0) handleUpdated();
106 if(diff!=0) handleUpdated();
116
107
117 QList<QGraphicsItem*> items = m_items.childItems();
108 QList<QGraphicsItem*> items = m_items.childItems();
118
109
119 for(int i=0; i< points.size();i++) {
110 for(int i=0; i< points.size();i++) {
120 QGraphicsItem* item = items.at(i);
111 Marker* item = static_cast<Marker*>(items.at(i));
121 const QPointF& point = points.at(i);
112 const QPointF& point = points.at(i);
122 const QRectF& rect = item->boundingRect();
113 const QRectF& rect = item->boundingRect();
114 item->setIndex(i);
123 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
115 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
124 if(!clipRect().contains(point)) {
116 if(!clipRect().contains(point)) {
125 item->setVisible(false);
117 item->setVisible(false);
126 }
118 }
127 else {
119 else {
128 item->setVisible(true);
120 item->setVisible(true);
129 }
121 }
130 }
122 }
131
123
132 prepareGeometryChange();
124 prepareGeometryChange();
133 m_rect = clipRect();
125 m_rect = clipRect();
134 XYChartItem::setGeometry(points);
126 XYChartItem::setGeometry(points);
135 }
127 }
136
128
137
129
138 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
130 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
139 {
131 {
140 Q_UNUSED(painter);
132 Q_UNUSED(painter);
141 Q_UNUSED(option);
133 Q_UNUSED(option);
142 Q_UNUSED(widget);
134 Q_UNUSED(widget);
143 }
135 }
144
136
145 void ScatterChartItem::setPen(const QPen& pen)
137 void ScatterChartItem::setPen(const QPen& pen)
146 {
138 {
147 foreach(QGraphicsItem* item , m_items.childItems()) {
139 foreach(QGraphicsItem* item , m_items.childItems()) {
148 static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen);
140 static_cast<Marker*>(item)->setPen(pen);
149 }
141 }
150 }
142 }
151
143
152 void ScatterChartItem::setBrush(const QBrush& brush)
144 void ScatterChartItem::setBrush(const QBrush& brush)
153 {
145 {
154 foreach(QGraphicsItem* item , m_items.childItems()) {
146 foreach(QGraphicsItem* item , m_items.childItems()) {
155 static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush);
147 static_cast<Marker*>(item)->setBrush(brush);
156 }
148 }
157 }
149 }
158
150
159 void ScatterChartItem::handleUpdated()
151 void ScatterChartItem::handleUpdated()
160 {
152 {
161
153
162 int count = m_items.childItems().count();
154 int count = m_items.childItems().count();
163
155
164 if(count==0) return;
156 if(count==0) return;
165
157
166 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
158 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
167
159
168 //TODO: only rewrite on size change
160 //TODO: only rewrite on size change
169
161
170 m_size = m_series->size();
162 m_size = m_series->size();
171 m_shape = m_series->shape();
163 m_shape = m_series->shape();
172
164
173 if(recreate){
165 if(recreate){
174 deletePoints(count);
166 deletePoints(count);
175 createPoints(count);
167 createPoints(count);
176 }
168 }
177
169
178 setPen(m_series->pen());
170 setPen(m_series->pen());
179 setBrush(m_series->brush());
171 setBrush(m_series->brush());
180
172
181 }
173 }
182
174
183 #include "moc_scatterchartitem_p.cpp"
175 #include "moc_scatterchartitem_p.cpp"
184
176
185 QTCOMMERCIALCHART_END_NAMESPACE
177 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,51 +1,122
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 "xychartitem_p.h"
5 #include "xychartitem_p.h"
6 #include <QGraphicsEllipseItem>
6 #include <QGraphicsEllipseItem>
7 #include <QPen>
7 #include <QPen>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QScatterSeries;
11 class QScatterSeries;
12 class Marker;
12
13
13 class ScatterChartItem : public XYChartItem
14 class ScatterChartItem : public XYChartItem
14 {
15 {
15 Q_OBJECT
16 Q_OBJECT
16 public:
17 public:
17 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
18
19
19 public:
20 public:
20 //from QGraphicsItem
21 //from QGraphicsItem
21 QRectF boundingRect() const;
22 QRectF boundingRect() const;
22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23
24
24 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
25 void setBrush(const QBrush& brush);
26 void setBrush(const QBrush& brush);
26
27
28 void markerSelected(Marker* item);
29
27 signals:
30 signals:
28 void clicked(QPointF coordinates);
31 void clicked(const QPointF& point);
29
32
30 public slots:
33 public slots:
31 void handleUpdated();
34 void handleUpdated();
32
35
33 private:
36 private:
34 void createPoints(int count);
37 void createPoints(int count);
35 void deletePoints(int count);
38 void deletePoints(int count);
36
39
37 protected:
40 protected:
38 virtual void setGeometry(QVector<QPointF>& points);
41 virtual void setGeometry(QVector<QPointF>& points);
39
42
40 private:
43 private:
41 QScatterSeries *m_series;
44 QScatterSeries *m_series;
42 QGraphicsItemGroup m_items;
45 QGraphicsItemGroup m_items;
43 int m_shape;
46 int m_shape;
44 int m_size;
47 int m_size;
45 QRectF m_rect;
48 QRectF m_rect;
46
49
47 };
50 };
48
51
52
53 class Marker: public QAbstractGraphicsShapeItem
54 {
55
56 public:
57
58 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
59 {
60 };
61
62 ~Marker()
63 {
64 delete m_item;
65 }
66
67 void setIndex(int index)
68 {
69 m_index=index;
70 }
71
72 int index() const
73 {
74 return m_index;
75 }
76
77 QPainterPath shape() const
78 {
79 return m_item->shape();
80 }
81
82 QRectF boundingRect() const
83 {
84 return m_item->boundingRect();
85 }
86
87 bool contains(const QPointF &point) const
88 {
89 return m_item->contains(point);
90 }
91
92 void setPen(const QPen& pen)
93 {
94 m_item->setPen(pen);
95 }
96
97 void setBrush(const QBrush& brush)
98 {
99 m_item->setBrush(brush);
100 }
101
102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
104 m_item->paint(painter,option,widget);
105 }
106
107 protected:
108
109 void mousePressEvent( QGraphicsSceneMouseEvent * event )
110 {
111 m_parent->markerSelected(this);
112 }
113
114 private:
115 QAbstractGraphicsShapeItem* m_item;
116 ScatterChartItem* m_parent;
117 int m_index;
118 };
119
49 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
50
121
51 #endif // SCATTERPRESENTER_H
122 #endif // SCATTERPRESENTER_H
@@ -1,286 +1,291
1 #include "qxyseries.h"
1 #include "qxyseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QXYSeries
6 \class QXYSeries
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
7 \brief The QXYSeries class is a base class for line, spline and scatter series.
8 */
8 */
9
9
10 /*!
10 /*!
11 \fn QPen QXYSeries::pen() const
11 \fn QPen QXYSeries::pen() const
12 \brief Returns pen used to draw points for series.
12 \brief Returns pen used to draw points for series.
13 \sa setPen()
13 \sa setPen()
14 */
14 */
15
15
16 /*!
16 /*!
17 \fn QBrush QXYSeries::brush() const
17 \fn QBrush QXYSeries::brush() const
18 \brief Returns brush used to draw points for series.
18 \brief Returns brush used to draw points for series.
19 \sa setBrush()
19 \sa setBrush()
20 */
20 */
21
21
22 /*!
22 /*!
23 \fn void QXYSeries::pointReplaced(int index)
23 \fn void QXYSeries::pointReplaced(int index)
24 \brief \internal \a index
24 \brief \internal \a index
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QXYSeries::pointAdded(int index)
28 \fn void QXYSeries::pointAdded(int index)
29 \brief \internal \a index
29 \brief \internal \a index
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn void QXYSeries::pointRemoved(int index)
33 \fn void QXYSeries::pointRemoved(int index)
34 \brief \internal \a index
34 \brief \internal \a index
35 */
35 */
36
36
37 /*!
37 /*!
38 \fn void QXYSeries::updated()
38 \fn void QXYSeries::updated()
39 \brief \internal
39 \brief \internal
40 */
40 */
41
41
42 /*!
42 /*!
43 Constructs empty series object which is a child of \a parent.
43 Constructs empty series object which is a child of \a parent.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
44 When series object is added to QChartView or QChart instance ownerships is transfered.
45 */
45 */
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
46 QXYSeries::QXYSeries(QObject* parent):QSeries(parent)
47 {
47 {
48 m_model = NULL;
48 m_model = NULL;
49 m_mapX = -1;
49 m_mapX = -1;
50 m_mapY = -1;
50 m_mapY = -1;
51 m_mapOrientation = Qt::Vertical;
51 m_mapOrientation = Qt::Vertical;
52 // m_mapYOrientation = Qt::Vertical;
52 // m_mapYOrientation = Qt::Vertical;
53 }
53 }
54 /*!
54 /*!
55 Destroys the object. Series added to QChartView or QChart instances are owned by those,
55 Destroys the object. Series added to QChartView or QChart instances are owned by those,
56 and are deleted when mentioned object are destroyed.
56 and are deleted when mentioned object are destroyed.
57 */
57 */
58 QXYSeries::~QXYSeries()
58 QXYSeries::~QXYSeries()
59 {
59 {
60 }
60 }
61
61
62 /*!
62 /*!
63 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
63 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
64 */
64 */
65 void QXYSeries::add(qreal x,qreal y)
65 void QXYSeries::add(qreal x,qreal y)
66 {
66 {
67 Q_ASSERT(m_x.size() == m_y.size());
67 Q_ASSERT(m_x.size() == m_y.size());
68 m_x<<x;
68 m_x<<x;
69 m_y<<y;
69 m_y<<y;
70 emit pointAdded(m_x.size()-1);
70 emit pointAdded(m_x.size()-1);
71 }
71 }
72
72
73 /*!
73 /*!
74 This is an overloaded function.
74 This is an overloaded function.
75 Adds data \a point to the series. Points are connected with lines on the chart.
75 Adds data \a point to the series. Points are connected with lines on the chart.
76 */
76 */
77 void QXYSeries::add(const QPointF& point)
77 void QXYSeries::add(const QPointF& point)
78 {
78 {
79 add(point.x(),point.y());
79 add(point.x(),point.y());
80 }
80 }
81
81
82 /*!
82 /*!
83 This is an overloaded function.
83 This is an overloaded function.
84 Adds list of data \a points to the series. Points are connected with lines on the chart.
84 Adds list of data \a points to the series. Points are connected with lines on the chart.
85 */
85 */
86 void QXYSeries::add(const QList<QPointF> points)
86 void QXYSeries::add(const QList<QPointF> points)
87 {
87 {
88 foreach(const QPointF& point , points) {
88 foreach(const QPointF& point , points) {
89 add(point.x(),point.y());
89 add(point.x(),point.y());
90 }
90 }
91 }
91 }
92
92
93 /*!
93 /*!
94 Modifies \a y value for given \a x a value.
94 Modifies \a y value for given \a x a value.
95 */
95 */
96 void QXYSeries::replace(qreal x,qreal y)
96 void QXYSeries::replace(qreal x,qreal y)
97 {
97 {
98 int index = m_x.indexOf(x);
98 int index = m_x.indexOf(x);
99 m_x[index]=x;
99 m_x[index]=x;
100 m_y[index]=y;
100 m_y[index]=y;
101 emit pointReplaced(index);
101 emit pointReplaced(index);
102 }
102 }
103
103
104 /*!
104 /*!
105 This is an overloaded function.
105 This is an overloaded function.
106 Replaces current y value of for given \a point x value with \a point y value.
106 Replaces current y value of for given \a point x value with \a point y value.
107 */
107 */
108 void QXYSeries::replace(const QPointF& point)
108 void QXYSeries::replace(const QPointF& point)
109 {
109 {
110 replace(point.x(),point.y());
110 replace(point.x(),point.y());
111 }
111 }
112
112
113 /*!
113 /*!
114 Removes current \a x and y value.
114 Removes current \a x and \a y value.
115 */
115 */
116 void QXYSeries::remove(qreal x)
116 void QXYSeries::remove(qreal x,qreal y)
117 {
117 {
118 int index = m_x.indexOf(x);
118 int index =-1;
119 do{
120 index = m_x.indexOf(x,index+1);
121 }while(index !=-1 && m_y.at(index)!=y);
122
123 if(index==-1) return;
119 emit pointRemoved(index);
124 emit pointRemoved(index);
120 m_x.remove(index);
125 m_x.remove(index);
121 m_y.remove(index);
126 m_y.remove(index);
122 }
127 }
123
128
124 /*!
129 /*!
125 Removes current \a point x value. Note \a point y value is ignored.
130 Removes current \a point x value. Note \a point y value is ignored.
126 */
131 */
127 void QXYSeries::remove(const QPointF& point)
132 void QXYSeries::remove(const QPointF& point)
128 {
133 {
129 remove(point.x());
134 remove(point.x(),point.y());
130 }
135 }
131
136
132 /*!
137 /*!
133 Removes all data points from the series.
138 Removes all data points from the series.
134 */
139 */
135 void QXYSeries::removeAll()
140 void QXYSeries::removeAll()
136 {
141 {
137 m_x.clear();
142 m_x.clear();
138 m_y.clear();
143 m_y.clear();
139 }
144 }
140
145
141 /*!
146 /*!
142 \internal \a pos
147 \internal \a pos
143 */
148 */
144 qreal QXYSeries::x(int pos) const
149 qreal QXYSeries::x(int pos) const
145 {
150 {
146 if (m_model)
151 if (m_model)
147 if (m_mapOrientation == Qt::Vertical)
152 if (m_mapOrientation == Qt::Vertical)
148 // consecutive data is read from model's column
153 // consecutive data is read from model's column
149 return m_model->data(m_model->index(pos, m_mapX), Qt::DisplayRole).toDouble();
154 return m_model->data(m_model->index(pos, m_mapX), Qt::DisplayRole).toDouble();
150 else
155 else
151 // consecutive data is read from model's row
156 // consecutive data is read from model's row
152 return m_model->data(m_model->index(m_mapX, pos), Qt::DisplayRole).toDouble();
157 return m_model->data(m_model->index(m_mapX, pos), Qt::DisplayRole).toDouble();
153 else
158 else
154 // model is not specified, return the data from series' internal data store
159 // model is not specified, return the data from series' internal data store
155 return m_x.at(pos);
160 return m_x.at(pos);
156 }
161 }
157
162
158 /*!
163 /*!
159 \internal \a pos
164 \internal \a pos
160 */
165 */
161 qreal QXYSeries::y(int pos) const
166 qreal QXYSeries::y(int pos) const
162 {
167 {
163 if (m_model)
168 if (m_model)
164 if (m_mapOrientation == Qt::Vertical)
169 if (m_mapOrientation == Qt::Vertical)
165 // consecutive data is read from model's column
170 // consecutive data is read from model's column
166 return m_model->data(m_model->index(pos, m_mapY), Qt::DisplayRole).toDouble();
171 return m_model->data(m_model->index(pos, m_mapY), Qt::DisplayRole).toDouble();
167 else
172 else
168 // consecutive data is read from model's row
173 // consecutive data is read from model's row
169 return m_model->data(m_model->index(m_mapY, pos), Qt::DisplayRole).toDouble();
174 return m_model->data(m_model->index(m_mapY, pos), Qt::DisplayRole).toDouble();
170 else
175 else
171 // model is not specified, return the data from series' internal data store
176 // model is not specified, return the data from series' internal data store
172 return m_y.at(pos);
177 return m_y.at(pos);
173 }
178 }
174
179
175 /*!
180 /*!
176 Returns number of data points within series.
181 Returns number of data points within series.
177 */
182 */
178 int QXYSeries::count() const
183 int QXYSeries::count() const
179 {
184 {
180 Q_ASSERT(m_x.size() == m_y.size());
185 Q_ASSERT(m_x.size() == m_y.size());
181
186
182 if (m_model)
187 if (m_model)
183 if (m_mapOrientation == Qt::Vertical)
188 if (m_mapOrientation == Qt::Vertical)
184 // data is in a column, so return the number of items in single column
189 // data is in a column, so return the number of items in single column
185 return m_model->rowCount();
190 return m_model->rowCount();
186 else
191 else
187 // data is in a row, so return the number of items in single row
192 // data is in a row, so return the number of items in single row
188 m_model->columnCount();
193 m_model->columnCount();
189 else
194 else
190 // model is not specified, return the number of points in the series internal data store
195 // model is not specified, return the number of points in the series internal data store
191 return m_x.size();
196 return m_x.size();
192 }
197 }
193
198
194 /*!
199 /*!
195 Returns the data points of the series.
200 Returns the data points of the series.
196 */
201 */
197 QList<QPointF> QXYSeries::data()
202 QList<QPointF> QXYSeries::data()
198 {
203 {
199 QList<QPointF> data;
204 QList<QPointF> data;
200 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
205 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
201 data.append(QPointF(m_x.at(i), m_y.at(i)));
206 data.append(QPointF(m_x.at(i), m_y.at(i)));
202 return data;
207 return data;
203 }
208 }
204
209
205
210
206 /*!
211 /*!
207 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
212 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
208 pen from chart theme is used.
213 pen from chart theme is used.
209 \sa QChart::setChartTheme()
214 \sa QChart::setChartTheme()
210 */
215 */
211 void QXYSeries::setPen(const QPen& pen)
216 void QXYSeries::setPen(const QPen& pen)
212 {
217 {
213 if(pen!=m_pen){
218 if(pen!=m_pen){
214 m_pen=pen;
219 m_pen=pen;
215 emit updated();
220 emit updated();
216 }
221 }
217 }
222 }
218
223
219 /*!
224 /*!
220 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
225 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
221 from chart theme setting is used.
226 from chart theme setting is used.
222 \sa QChart::setChartTheme()
227 \sa QChart::setChartTheme()
223 */
228 */
224
229
225 void QXYSeries::setBrush(const QBrush& brush)
230 void QXYSeries::setBrush(const QBrush& brush)
226 {
231 {
227 if(brush!=m_brush){
232 if(brush!=m_brush){
228 m_brush=brush;
233 m_brush=brush;
229 emit updated();
234 emit updated();
230 }
235 }
231 }
236 }
232
237
233
238
234 /*!
239 /*!
235 Stream operator for adding a data \a point to the series.
240 Stream operator for adding a data \a point to the series.
236 \sa add()
241 \sa add()
237 */
242 */
238
243
239 QXYSeries& QXYSeries::operator<< (const QPointF &point)
244 QXYSeries& QXYSeries::operator<< (const QPointF &point)
240 {
245 {
241 add(point);
246 add(point);
242 return *this;
247 return *this;
243 }
248 }
244
249
245
250
246 /*!
251 /*!
247 Stream operator for adding a list of \a points to the series.
252 Stream operator for adding a list of \a points to the series.
248 \sa add()
253 \sa add()
249 */
254 */
250
255
251 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
256 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
252 {
257 {
253 add(points);
258 add(points);
254 return *this;
259 return *this;
255 }
260 }
256
261
257
262
258 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
263 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
259 {
264 {
260 emit pointReplaced(topLeft.row());
265 emit pointReplaced(topLeft.row());
261 }
266 }
262
267
263 bool QXYSeries::setModel(QAbstractItemModel* model) {
268 bool QXYSeries::setModel(QAbstractItemModel* model) {
264 m_model = model;
269 m_model = model;
265 // for (int i = 0; i < m_model->rowCount(); i++)
270 // for (int i = 0; i < m_model->rowCount(); i++)
266 // emit pointAdded(i);
271 // emit pointAdded(i);
267 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
272 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
268 // connect(m_model,SIGNAL(), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
273 // connect(m_model,SIGNAL(), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
269 }
274 }
270
275
271 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
276 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
272 {
277 {
273 m_mapX = modelX;
278 m_mapX = modelX;
274 m_mapY = modelY;
279 m_mapY = modelY;
275 m_mapOrientation = orientation;
280 m_mapOrientation = orientation;
276 }
281 }
277
282
278 //void QXYSeries::setModelMappingY(int modelLineIndex, Qt::Orientation orientation)
283 //void QXYSeries::setModelMappingY(int modelLineIndex, Qt::Orientation orientation)
279 //{
284 //{
280 // m_mapY = modelLineIndex;
285 // m_mapY = modelLineIndex;
281 // m_mapYOrientation = orientation;
286 // m_mapYOrientation = orientation;
282 //}
287 //}
283
288
284 #include "moc_qxyseries.cpp"
289 #include "moc_qxyseries.cpp"
285
290
286 QTCOMMERCIALCHART_END_NAMESPACE
291 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,73 +1,73
1 #ifndef QXYSERIES_H_
1 #ifndef QXYSERIES_H_
2 #define QXYSERIES_H_
2 #define QXYSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qseries.h"
5 #include "qseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
12 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 protected:
15 protected:
16 QXYSeries(QObject* parent=0);
16 QXYSeries(QObject* parent=0);
17 virtual ~QXYSeries();
17 virtual ~QXYSeries();
18
18
19 public:
19 public:
20 void add(qreal x, qreal y);
20 void add(qreal x, qreal y);
21 void add(const QPointF& point);
21 void add(const QPointF& point);
22 void add(const QList<QPointF> points);
22 void add(const QList<QPointF> points);
23 void replace(qreal x,qreal y);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
24 void replace(const QPointF& point);
25 void remove(qreal x);
25 void remove(qreal x, qreal y);
26 void remove(const QPointF& point);
26 void remove(const QPointF& point);
27 void removeAll();
27 void removeAll();
28
28
29 int count() const;
29 int count() const;
30 qreal x(int pos) const;
30 qreal x(int pos) const;
31 qreal y(int pos) const;
31 qreal y(int pos) const;
32 QList<QPointF> data();
32 QList<QPointF> data();
33
33
34 QXYSeries& operator << (const QPointF &point);
34 QXYSeries& operator << (const QPointF &point);
35 QXYSeries& operator << (const QList<QPointF> points);
35 QXYSeries& operator << (const QList<QPointF> points);
36
36
37 void setPen(const QPen& pen);
37 void setPen(const QPen& pen);
38 QPen pen() const {return m_pen;}
38 QPen pen() const {return m_pen;}
39 void setBrush(const QBrush& pen);
39 void setBrush(const QBrush& pen);
40 QBrush brush() const {return m_brush;}
40 QBrush brush() const {return m_brush;}
41
41
42 bool setModel(QAbstractItemModel* model);
42 bool setModel(QAbstractItemModel* model);
43 QAbstractItemModel* model() {return m_model;}
43 QAbstractItemModel* model() {return m_model;}
44
44
45 void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
45 void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
46 // void setModelMappingY(int modelLineIndex, Qt::Orientation orientation = Qt::Vertical);
47
47
48 private slots:
48 private slots:
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50
50
51 signals:
51 signals:
52 void updated();
52 void updated();
53 void pointReplaced(int index);
53 void pointReplaced(int index);
54 void pointRemoved(int index);
54 void pointRemoved(int index);
55 void pointAdded(int index);
55 void pointAdded(int index);
56
56
57 protected:
57 protected:
58 QVector<qreal> m_x;
58 QVector<qreal> m_x;
59 QVector<qreal> m_y;
59 QVector<qreal> m_y;
60
60
61 QPen m_pen;
61 QPen m_pen;
62 QBrush m_brush;
62 QBrush m_brush;
63
63
64 QAbstractItemModel* m_model;
64 QAbstractItemModel* m_model;
65 int m_mapX;
65 int m_mapX;
66 Qt::Orientation m_mapOrientation;
66 Qt::Orientation m_mapOrientation;
67 int m_mapY;
67 int m_mapY;
68 // Qt::Orientation m_mapYOrientation;
68 // Qt::Orientation m_mapYOrientation;
69 };
69 };
70
70
71 QTCOMMERCIALCHART_END_NAMESPACE
71 QTCOMMERCIALCHART_END_NAMESPACE
72
72
73 #endif
73 #endif
General Comments 0
You need to be logged in to leave comments. Login now