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