##// END OF EJS Templates
Scatter series marker visuals
Tero Ahola -
r195:3f425cc48265
parent child
Show More
@@ -14,20 +14,38 int main(int argc, char *argv[])
14 14 // Create chart widget
15 15 QChartView *chartWidget = new QChartView();
16 16
17 // Create scatter series with simple test data
17 // Add scatter series with simple test data
18 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);
19 *scatter << QPointF(0.5, 5.0)
20 << QPointF(1.0, 4.5)
21 << QPointF(1.0, 5.5)
22 << QPointF(1.5, 5.0)
23 << QPointF(2.0, 4.5)
24 << QPointF(2.0, 5.5)
25 << QPointF(2.5, 5.0);
23 26 chartWidget->addSeries(scatter);
24 27
25 // Add another scatter series with more complex data with random component
28 // Add another scatter series
29 // - more data with random component
26 30 QScatterSeries *scatter2 = new QScatterSeries();
27 for (qreal i(0.0); i < 20; i += 0.5)
31 for (qreal i(0.0); i < 20; i += 0.05) {
28 32 (*scatter2) << QPointF(i + (qreal)(rand() % 100) / 100.0,
29 i + (qreal)(rand() % 100) / 100.0);
33 i + (qreal)(rand() % 100) / 100.0);
34 }
30 35 chartWidget->addSeries(scatter2);
36 // Custom pen and brush (not those defined by the chart theme)
37 // - uses opaque color
38 QColor color("#2685BF");
39 color.setAlpha(80);
40 QBrush brush(Qt::SolidPattern);
41 brush.setColor(color);
42 scatter2->setMarkerBrush(brush);
43 QPen pen;
44 pen.setColor(color);
45 pen.setWidth(2);
46 scatter2->setMarkerPen(pen);
47 // use a rectangle as the marker shape
48 scatter2->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
31 49
32 50 // Use the chart widget as the central widget
33 51 QMainWindow w;
@@ -127,9 +127,6 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
127 127 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
128 128 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
129 129 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
130 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
131 // scatterSeries->d->setParentItem(this);
132 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
133 130 m_chartItems.insert(scatterSeries, scatterPresenter);
134 131 break;
135 132 }
@@ -104,6 +104,14 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
104 104 decorate(i,s,count);
105 105 break;
106 106 }
107 case QChartSeries::SeriesTypeScatter: {
108 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
109 Q_ASSERT(s);
110 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
111 Q_ASSERT(i);
112 decorate(i, s, count);
113 break;
114 }
107 115 case QChartSeries::SeriesTypePie: {
108 116 QPieSeries* s = static_cast<QPieSeries*>(series);
109 117 PiePresenter* i = static_cast<PiePresenter*>(item);
@@ -179,16 +187,16 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, i
179 187 Q_ASSERT(presenter);
180 188 Q_ASSERT(series);
181 189
182 presenter->m_markerPen.setColor(m_seriesColor.at(count % m_seriesColor.size()));
190 QColor color = m_seriesColor.at(count % m_seriesColor.size());
191 // TODO: define alpha in the theme? or in the series?
192 color.setAlpha(120);
193
194 QBrush brush(color, Qt::SolidPattern);
195 presenter->m_markerBrush = brush;
183 196
184 // QPen pen;
185 // if(pen != series->pen()){
186 // item->setPen(series->pen());
187 // return;
188 // }
189 // pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
190 // pen.setWidthF(2);
191 // item->setPen(pen);
197 QPen pen(brush, 1);
198 pen.setColor(color);
199 presenter->m_markerPen = pen;
192 200 }
193 201
194 202 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
@@ -5,8 +5,14
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 QScatterSeriesPrivate::QScatterSeriesPrivate() :
8 m_data(QList<QPointF>())
8 m_data(QList<QPointF>()),
9 m_markerPen(QPen()),
10 m_markerBrush(QBrush()),
11 m_markerShape(QScatterSeries::MarkerShapeDefault)
9 12 {
13 // Initialize pen color to invalid to use a theme color by default
14 m_markerPen.setColor(QColor::Invalid);
15 m_markerBrush.setColor(QColor::Invalid);
10 16 }
11 17
12 18 QScatterSeries::QScatterSeries(QObject *parent) :
@@ -54,6 +60,26 QPen QScatterSeries::markerPen()
54 60 return d->m_markerPen;
55 61 }
56 62
63 void QScatterSeries::setMarkerBrush(QBrush brush)
64 {
65 d->m_markerBrush = brush;
66 }
67
68 QBrush QScatterSeries::markerBrush()
69 {
70 return d->m_markerBrush;
71 }
72
73 void QScatterSeries::setMarkerShape(MarkerShape shape)
74 {
75 d->m_markerShape = shape;
76 }
77
78 QScatterSeries::MarkerShape QScatterSeries::markerShape()
79 {
80 return (QScatterSeries::MarkerShape) d->m_markerShape;
81 }
82
57 83 #include "moc_qscatterseries.cpp"
58 84
59 85 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,8 +11,21 class QScatterSeriesPrivate;
11 11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 12 {
13 13 Q_OBJECT
14
15 public:
16 enum MarkerShape {
17 // TODO: to be defined by the graphics design
18 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
19 MarkerShapeDefault = 0,
20 MarkerShapePoint,
21 MarkerShapeX,
22 MarkerShapeRectangle,
23 MarkerShapeTiltedRectangle,
24 MarkerShapeTriangle,
25 MarkerShapeCircle
26 };
27
14 28 public:
15 //QScatterSeries(QSeriesData *data, QObject *chart);
16 29 QScatterSeries(QObject *parent = 0);
17 30 ~QScatterSeries();
18 31
@@ -25,17 +38,20 public:
25 38 QScatterSeries& operator << (const QPointF &value);
26 39 void setData(QList<QPointF> data);
27 40 QList<QPointF> data();
28
29 //TODO? void insertData(int index, QPointF data);
41 //TODO: insertData?
30 42
31 43 void setMarkerPen(QPen pen);
32 44 QPen markerPen();
33 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
34 //void setMarkerShape(MarkerShape shape);
45 void setMarkerBrush(QBrush brush);
46 QBrush markerBrush();
47 void setMarkerShape(MarkerShape shape);
48 MarkerShape markerShape();
49 // TODO: marker size?
35 50
36 51 Q_SIGNALS:
37 // TODO: move to PIMPL?
52 // TODO: move to PIMPL for simplicity or does the user ever need these signals?
38 53 // TODO: more finegrained signaling for performance reasons
54 // (check QPieSeries implementation with change sets)
39 55 void changed();
40 56
41 57 //public Q_SLOTS:
@@ -43,31 +43,48 void ScatterPresenter::handleModelChanged()
43 43
44 44 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
45 45 {
46 // TODO: The opacity should be user definable?
47 //brush.setColor(QColor(255, 82, 0, 100));
48 //if (m_series->markerPen().isValid()) {
49 if (false) {
50 QPen pen = painter->pen();
51 QBrush brush = pen.brush();
52 brush.setColor(m_series->markerPen().color());
53 pen.setBrush(brush);
54 pen.setWidth(4);
55 painter->setPen(pen);
56 }
57 else {
58 // TODO: fix this
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);
46 // TODO: Optimization: avoid setting on every paint method call?
47 // The custom settings in series override those defined by the theme
48 if (m_series->markerPen().color().isValid()) {
49 painter->setPen(m_series->markerPen());
50 painter->setBrush(m_series->markerBrush());
51 } else {
52 painter->setPen(m_markerPen);
53 painter->setBrush(m_markerBrush);
65 54 }
66 55
56 int shape = m_series->markerShape();
57
67 58 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
68 59 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
69 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
70 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
60 // Paint a shape
61 switch (shape) {
62 case QScatterSeries::MarkerShapeDefault:
63 // Fallthrough, defaults to circle
64 case QScatterSeries::MarkerShapeCircle:
65 painter->drawChord(m_scenex.at(i), m_sceney.at(i), 9, 9, 0, 5760);
66 break;
67 case QScatterSeries::MarkerShapePoint:
68 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
69 break;
70 case QScatterSeries::MarkerShapeRectangle:
71 painter->drawRect(m_scenex.at(i), m_sceney.at(i), 9, 9);
72 break;
73 case QScatterSeries::MarkerShapeTiltedRectangle:
74 // TODO:
75 static const QPointF points[4] = {
76 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
77 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
78 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
79 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
80 };
81 painter->drawPolygon(points, 4);
82 break;
83 default:
84 // TODO: implement the rest of the shapes
85 Q_ASSERT(false);
86 break;
87 }
71 88 }
72 89 }
73 90
@@ -40,6 +40,7 public:
40 40 QList<qreal> m_sceney;
41 41 Domain m_visibleChartArea;
42 42 QPen m_markerPen;
43 QBrush m_markerBrush;
43 44 };
44 45
45 46 QTCOMMERCIALCHART_END_NAMESPACE
@@ -18,6 +18,8 public:
18 18 public:
19 19 QList<QPointF> m_data;
20 20 QPen m_markerPen;
21 QBrush m_markerBrush;
22 int m_markerShape;
21 23 };
22 24
23 25 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now