@@ -14,20 +14,38 int main(int argc, char *argv[]) | |||||
14 | // Create chart widget |
|
14 | // Create chart widget | |
15 | QChartView *chartWidget = new QChartView(); |
|
15 | QChartView *chartWidget = new QChartView(); | |
16 |
|
16 | |||
17 |
// |
|
17 | // Add scatter series with simple test data | |
18 | QScatterSeries *scatter = new QScatterSeries(); |
|
18 | QScatterSeries *scatter = new QScatterSeries(); | |
19 |
*scatter << QPointF(0.5, |
|
19 | *scatter << QPointF(0.5, 5.0) | |
20 |
<< QPointF(1.0, |
|
20 | << QPointF(1.0, 4.5) | |
21 |
<< QPointF(1. |
|
21 | << QPointF(1.0, 5.5) | |
22 |
<< QPointF( |
|
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 | chartWidget->addSeries(scatter); |
|
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 | QScatterSeries *scatter2 = new QScatterSeries(); |
|
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 | (*scatter2) << QPointF(i + (qreal)(rand() % 100) / 100.0, |
|
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 | chartWidget->addSeries(scatter2); |
|
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 | // Use the chart widget as the central widget |
|
50 | // Use the chart widget as the central widget | |
33 | QMainWindow w; |
|
51 | QMainWindow w; |
@@ -127,9 +127,6 void ChartPresenter::handleSeriesAdded(QChartSeries* series) | |||||
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 | 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 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
130 | m_chartItems.insert(scatterSeries, scatterPresenter); | |
134 | break; |
|
131 | break; | |
135 | } |
|
132 | } |
@@ -104,6 +104,14 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) | |||||
104 | decorate(i,s,count); |
|
104 | decorate(i,s,count); | |
105 | break; |
|
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 | case QChartSeries::SeriesTypePie: { |
|
115 | case QChartSeries::SeriesTypePie: { | |
108 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
116 | QPieSeries* s = static_cast<QPieSeries*>(series); | |
109 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
117 | PiePresenter* i = static_cast<PiePresenter*>(item); | |
@@ -179,16 +187,16 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, i | |||||
179 | Q_ASSERT(presenter); |
|
187 | Q_ASSERT(presenter); | |
180 | Q_ASSERT(series); |
|
188 | Q_ASSERT(series); | |
181 |
|
189 | |||
182 |
|
|
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 |
|
|
197 | QPen pen(brush, 1); | |
185 | // if(pen != series->pen()){ |
|
198 | pen.setColor(color); | |
186 | // item->setPen(series->pen()); |
|
199 | presenter->m_markerPen = pen; | |
187 | // return; |
|
|||
188 | // } |
|
|||
189 | // pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
|||
190 | // pen.setWidthF(2); |
|
|||
191 | // item->setPen(pen); |
|
|||
192 | } |
|
200 | } | |
193 |
|
201 | |||
194 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
202 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
@@ -5,8 +5,14 | |||||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | QScatterSeriesPrivate::QScatterSeriesPrivate() : |
|
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 | QScatterSeries::QScatterSeries(QObject *parent) : |
|
18 | QScatterSeries::QScatterSeries(QObject *parent) : | |
@@ -54,6 +60,26 QPen QScatterSeries::markerPen() | |||||
54 | return d->m_markerPen; |
|
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 | #include "moc_qscatterseries.cpp" |
|
83 | #include "moc_qscatterseries.cpp" | |
58 |
|
84 | |||
59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
85 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -11,8 +11,21 class QScatterSeriesPrivate; | |||||
11 | class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries |
|
11 | class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries | |
12 | { |
|
12 | { | |
13 | Q_OBJECT |
|
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 | public: |
|
28 | public: | |
15 | //QScatterSeries(QSeriesData *data, QObject *chart); |
|
|||
16 | QScatterSeries(QObject *parent = 0); |
|
29 | QScatterSeries(QObject *parent = 0); | |
17 | ~QScatterSeries(); |
|
30 | ~QScatterSeries(); | |
18 |
|
31 | |||
@@ -25,17 +38,20 public: | |||||
25 | QScatterSeries& operator << (const QPointF &value); |
|
38 | QScatterSeries& operator << (const QPointF &value); | |
26 | void setData(QList<QPointF> data); |
|
39 | void setData(QList<QPointF> data); | |
27 | QList<QPointF> data(); |
|
40 | QList<QPointF> data(); | |
28 |
|
41 | //TODO: insertData? | ||
29 | //TODO? void insertData(int index, QPointF data); |
|
|||
30 |
|
42 | |||
31 | void setMarkerPen(QPen pen); |
|
43 | void setMarkerPen(QPen pen); | |
32 | QPen markerPen(); |
|
44 | QPen markerPen(); | |
33 | // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot |
|
45 | void setMarkerBrush(QBrush brush); | |
34 | //void setMarkerShape(MarkerShape shape); |
|
46 | QBrush markerBrush(); | |
|
47 | void setMarkerShape(MarkerShape shape); | |||
|
48 | MarkerShape markerShape(); | |||
|
49 | // TODO: marker size? | |||
35 |
|
50 | |||
36 | Q_SIGNALS: |
|
51 | Q_SIGNALS: | |
37 | // TODO: move to PIMPL? |
|
52 | // TODO: move to PIMPL for simplicity or does the user ever need these signals? | |
38 | // TODO: more finegrained signaling for performance reasons |
|
53 | // TODO: more finegrained signaling for performance reasons | |
|
54 | // (check QPieSeries implementation with change sets) | |||
39 | void changed(); |
|
55 | void changed(); | |
40 |
|
56 | |||
41 | //public Q_SLOTS: |
|
57 | //public Q_SLOTS: |
@@ -43,31 +43,48 void ScatterPresenter::handleModelChanged() | |||||
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: Optimization: avoid setting on every paint method call? | |
47 | //brush.setColor(QColor(255, 82, 0, 100)); |
|
47 | // The custom settings in series override those defined by the theme | |
48 |
|
|
48 | if (m_series->markerPen().color().isValid()) { | |
49 | if (false) { |
|
49 | painter->setPen(m_series->markerPen()); | |
50 | QPen pen = painter->pen(); |
|
50 | painter->setBrush(m_series->markerBrush()); | |
51 | QBrush brush = pen.brush(); |
|
51 | } else { | |
52 | brush.setColor(m_series->markerPen().color()); |
|
52 | painter->setPen(m_markerPen); | |
53 |
p |
|
53 | painter->setBrush(m_markerBrush); | |
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); |
|
|||
65 | } |
|
54 | } | |
66 |
|
55 | |||
|
56 | int shape = m_series->markerShape(); | |||
|
57 | ||||
67 | for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { |
|
58 | for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { | |
68 | if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i)) |
|
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); |
|
60 | // Paint a shape | |
70 | painter->drawPoint(m_scenex.at(i), m_sceney.at(i)); |
|
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 |
General Comments 0
You need to be logged in to leave comments.
Login now