##// END OF EJS Templates
Drafting pie theme stuff
Jani Honkonen -
r166:340263587b9e
parent child
Show More
@@ -1,263 +1,264
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "barchartseries.h"
7 #include "barchartseries.h"
8 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
9 #include "percentbarchartseries.h"
9 #include "percentbarchartseries.h"
10 #include "qlinechartseries.h"
10 #include "qlinechartseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 //items
13 //items
14 #include "axisitem_p.h"
14 #include "axisitem_p.h"
15 #include "bargroup.h"
15 #include "bargroup.h"
16 #include "stackedbargroup.h"
16 #include "stackedbargroup.h"
17 #include "linechartitem_p.h"
17 #include "linechartitem_p.h"
18 #include "percentbargroup.h"
18 #include "percentbargroup.h"
19 #include "linechartanimationitem_p.h"
19 #include "linechartanimationitem_p.h"
20 #include "piepresenter.h"
20 #include "piepresenter.h"
21 #include "scatterpresenter.h"
21 #include "scatterpresenter.h"
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
25 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 m_chart(chart),
26 m_chart(chart),
27 m_dataset(dataset),
27 m_dataset(dataset),
28 m_chartTheme(0),
28 m_chartTheme(0),
29 m_domainIndex(0),
29 m_domainIndex(0),
30 m_marginSize(0),
30 m_marginSize(0),
31 m_axisX(new QChartAxis(this)),
31 m_axisX(new QChartAxis(this)),
32 m_axisY(new QChartAxis(this)),
32 m_axisY(new QChartAxis(this)),
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
34 {
34 {
35 setChartTheme(QChart::ChartThemeDefault);
35 setChartTheme(QChart::ChartThemeDefault);
36 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
36 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
37 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
37 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
38 createConnections();
38 createConnections();
39 }
39 }
40
40
41 ChartPresenter::~ChartPresenter()
41 ChartPresenter::~ChartPresenter()
42 {
42 {
43 }
43 }
44
44
45 void ChartPresenter::createConnections()
45 void ChartPresenter::createConnections()
46 {
46 {
47 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
47 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
48 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
48 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
49
49
50 QMapIterator<QChartAxis*,AxisItem*> i(m_axisItems);
50 QMapIterator<QChartAxis*,AxisItem*> i(m_axisItems);
51
51
52 while (i.hasNext()) {
52 while (i.hasNext()) {
53 i.next();
53 i.next();
54 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&)));
54 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&)));
55 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&)));
55 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&)));
56 }
56 }
57 }
57 }
58
58
59 void ChartPresenter::handleGeometryChanged()
59 void ChartPresenter::handleGeometryChanged()
60 {
60 {
61 m_rect = QRectF(QPoint(0,0),m_chart->size());
61 m_rect = QRectF(QPoint(0,0),m_chart->size());
62 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
62 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
63 Q_ASSERT(m_rect.isValid());
63 Q_ASSERT(m_rect.isValid());
64 emit geometryChanged(m_rect);
64 emit geometryChanged(m_rect);
65 }
65 }
66
66
67 int ChartPresenter::margin() const
67 int ChartPresenter::margin() const
68 {
68 {
69 return m_marginSize;
69 return m_marginSize;
70 }
70 }
71
71
72 void ChartPresenter::setMargin(int margin)
72 void ChartPresenter::setMargin(int margin)
73 {
73 {
74 m_marginSize = margin;
74 m_marginSize = margin;
75 }
75 }
76
76
77 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
77 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
78 {
78 {
79 switch(series->type())
79 switch(series->type())
80 {
80 {
81 case QChartSeries::SeriesTypeLine: {
81 case QChartSeries::SeriesTypeLine: {
82 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
82 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
83 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
83 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
84 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
84 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
85 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
86 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
86 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
87 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
87 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
88 m_chartItems.insert(series,item);
88 m_chartItems.insert(series,item);
89 break;
89 break;
90 }
90 }
91
91
92 case QChartSeries::SeriesTypeBar: {
92 case QChartSeries::SeriesTypeBar: {
93 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
93 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
94 BarGroup* item = new BarGroup(*barSeries,m_chart);
94 BarGroup* item = new BarGroup(*barSeries,m_chart);
95 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
95 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
96 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
96 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
97 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
97 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
98 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
98 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
99 m_chartItems.insert(series,item);
99 m_chartItems.insert(series,item);
100 // m_axisXItem->setVisible(false);
100 // m_axisXItem->setVisible(false);
101 break;
101 break;
102 }
102 }
103
103
104 case QChartSeries::SeriesTypeStackedBar: {
104 case QChartSeries::SeriesTypeStackedBar: {
105
105
106 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
106 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
107 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
107 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
108 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
108 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
109 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
109 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
110 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
110 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
111 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
111 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
112 m_chartItems.insert(series,item);
112 m_chartItems.insert(series,item);
113 break;
113 break;
114 }
114 }
115
115
116 case QChartSeries::SeriesTypePercentBar: {
116 case QChartSeries::SeriesTypePercentBar: {
117
117
118 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
118 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
119 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
119 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
120 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
120 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
122 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
123 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
123 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
124 m_chartItems.insert(series,item);
124 m_chartItems.insert(series,item);
125 break;
125 break;
126 }
126 }
127 case QChartSeries::SeriesTypeScatter: {
127 case QChartSeries::SeriesTypeScatter: {
128 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
128 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
129 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
129 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
130 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
130 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
131 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
131 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
132 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
132 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)),
133 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
133 scatterPresenter, SLOT(handleDomainChanged(const Domain&)));
134 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
134 // scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
135 // scatterSeries->d->setParentItem(this);
135 // scatterSeries->d->setParentItem(this);
136 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
136 // scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
137 m_chartItems.insert(scatterSeries, scatterPresenter);
137 m_chartItems.insert(scatterSeries, scatterPresenter);
138 break;
138 break;
139 }
139 }
140 case QChartSeries::SeriesTypePie: {
140 case QChartSeries::SeriesTypePie: {
141 QPieSeries *s = qobject_cast<QPieSeries *>(series);
141 QPieSeries *s = qobject_cast<QPieSeries *>(series);
142 PiePresenter* pie = new PiePresenter(m_chart, s);
142 PiePresenter* pie = new PiePresenter(m_chart, s);
143 m_chartTheme->decorate(pie, s, m_chartItems.count());
143 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
144 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
144 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
145 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
145 m_chartItems.insert(series, pie);
146 m_chartItems.insert(series, pie);
146 break;
147 break;
147 }
148 }
148 default: {
149 default: {
149 qDebug()<< "Series type" << series->type() << "not implemented.";
150 qDebug()<< "Series type" << series->type() << "not implemented.";
150 break;
151 break;
151 }
152 }
152 }
153 }
153
154
154 if(m_rect.isValid()) emit geometryChanged(m_rect);
155 if(m_rect.isValid()) emit geometryChanged(m_rect);
155 }
156 }
156
157
157 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
158 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
158 {
159 {
159 //TODO:
160 //TODO:
160 }
161 }
161
162
162 void ChartPresenter::zoomInToRect(const QRectF& rect)
163 void ChartPresenter::zoomInToRect(const QRectF& rect)
163 {
164 {
164 if(!rect.isValid()) return;
165 if(!rect.isValid()) return;
165 QRectF r = rect.normalized();
166 QRectF r = rect.normalized();
166 r.translate(-m_marginSize, -m_marginSize);
167 r.translate(-m_marginSize, -m_marginSize);
167 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
168 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
168 m_dataset->addDomain(domain);
169 m_dataset->addDomain(domain);
169 }
170 }
170
171
171 void ChartPresenter::zoomIn()
172 void ChartPresenter::zoomIn()
172 {
173 {
173 if (!m_dataset->nextDomain()) {
174 if (!m_dataset->nextDomain()) {
174 QRectF rect = m_rect;
175 QRectF rect = m_rect;
175 rect.setWidth(rect.width()/2);
176 rect.setWidth(rect.width()/2);
176 rect.setHeight(rect.height()/2);
177 rect.setHeight(rect.height()/2);
177 rect.moveCenter(m_rect.center());
178 rect.moveCenter(m_rect.center());
178 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
179 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
179 m_dataset->addDomain(domain);
180 m_dataset->addDomain(domain);
180 }
181 }
181 }
182 }
182
183
183 void ChartPresenter::zoomOut()
184 void ChartPresenter::zoomOut()
184 {
185 {
185 m_dataset->previousDomain();
186 m_dataset->previousDomain();
186 }
187 }
187
188
188 void ChartPresenter::zoomReset()
189 void ChartPresenter::zoomReset()
189 {
190 {
190 m_dataset->clearDomains();
191 m_dataset->clearDomains();
191 }
192 }
192
193
193 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
194 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
194 {
195 {
195 delete m_chartTheme;
196 delete m_chartTheme;
196
197
197 m_chartTheme = ChartTheme::createTheme(theme);
198 m_chartTheme = ChartTheme::createTheme(theme);
198
199
199 m_chartTheme->decorate(m_chart);
200 m_chartTheme->decorate(m_chart);
200 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
201 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
201
202
202 int index=0;
203 int index=0;
203 while (i.hasNext()) {
204 while (i.hasNext()) {
204 i.next();
205 i.next();
205 index++;
206 index++;
206 m_chartTheme->decorate(i.value(),i.key(),index);
207 m_chartTheme->decorate(i.value(),i.key(),index);
207 }
208 }
208 }
209 }
209
210
210
211
211 QChart::ChartTheme ChartPresenter::chartTheme()
212 QChart::ChartTheme ChartPresenter::chartTheme()
212 {
213 {
213 return m_chartTheme->id();
214 return m_chartTheme->id();
214 }
215 }
215
216
216 QChartAxis* ChartPresenter::axisX()
217 QChartAxis* ChartPresenter::axisX()
217 {
218 {
218 return m_axisX;
219 return m_axisX;
219 }
220 }
220
221
221 QChartAxis* ChartPresenter::axisY()
222 QChartAxis* ChartPresenter::axisY()
222 {
223 {
223 return m_axisY;
224 return m_axisY;
224 }
225 }
225
226
226 QChartAxis* ChartPresenter::addAxisX()
227 QChartAxis* ChartPresenter::addAxisX()
227 {
228 {
228 //only one axis
229 //only one axis
229 if(m_axisX==0){
230 if(m_axisX==0){
230 m_axisX = new QChartAxis(this);
231 m_axisX = new QChartAxis(this);
231 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
232 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
232 }
233 }
233 return m_axisX;
234 return m_axisX;
234 }
235 }
235
236
236 QChartAxis* ChartPresenter::addAxisY()
237 QChartAxis* ChartPresenter::addAxisY()
237 {
238 {
238 if(m_axisY==0){
239 if(m_axisY==0){
239 m_axisY = new QChartAxis(this);
240 m_axisY = new QChartAxis(this);
240 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
241 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
241 return m_axisY;
242 return m_axisY;
242 }
243 }
243
244
244 QChartAxis* axis = new QChartAxis(this);
245 QChartAxis* axis = new QChartAxis(this);
245 m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart);
246 m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart);
246 return axis;
247 return axis;
247 }
248 }
248
249
249 void ChartPresenter::removeAxis(QChartAxis* axis)
250 void ChartPresenter::removeAxis(QChartAxis* axis)
250 {
251 {
251 AxisItem* item = m_axisItems.take(axis);
252 AxisItem* item = m_axisItems.take(axis);
252 if(item){
253 if(item){
253 delete item;
254 delete item;
254 delete axis;
255 delete axis;
255 }
256 }
256 //reset pointers to default ones
257 //reset pointers to default ones
257 if(axis == m_axisX) m_axisX=0;
258 if(axis == m_axisX) m_axisX=0;
258 else if(axis == m_axisY) m_axisY=0;
259 else if(axis == m_axisY) m_axisY=0;
259 }
260 }
260
261
261 #include "moc_chartpresenter_p.cpp"
262 #include "moc_chartpresenter_p.cpp"
262
263
263 QTCOMMERCIALCHART_END_NAMESPACE
264 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,177 +1,209
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3
3
4
4
5 //series
5 //series
6 #include "barchartseries.h"
6 #include "barchartseries.h"
7 #include "stackedbarchartseries.h"
7 #include "stackedbarchartseries.h"
8 #include "percentbarchartseries.h"
8 #include "percentbarchartseries.h"
9 #include "qlinechartseries.h"
9 #include "qlinechartseries.h"
10 #include "qpieseries.h"
10 #include "qpieseries.h"
11
11
12 //items
12 //items
13 #include "axisitem_p.h"
13 #include "axisitem_p.h"
14 #include "bargroup.h"
14 #include "bargroup.h"
15 #include "stackedbargroup.h"
15 #include "stackedbargroup.h"
16 #include "linechartitem_p.h"
16 #include "linechartitem_p.h"
17 #include "percentbargroup.h"
17 #include "percentbargroup.h"
18 #include "piepresenter.h"
18 #include "piepresenter.h"
19
19
20 //themes
20 //themes
21 #include "chartthemevanilla_p.h"
21 #include "chartthemevanilla_p.h"
22 #include "chartthemeicy_p.h"
22 #include "chartthemeicy_p.h"
23 #include "chartthemegrayscale_p.h"
23 #include "chartthemegrayscale_p.h"
24 #include "chartthemescientific_p.h"
24 #include "chartthemescientific_p.h"
25
25
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 /* TODO
29 /* TODO
30 case QChart::ChartThemeUnnamed1:
30 case QChart::ChartThemeUnnamed1:
31 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
31 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
32 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
32 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
33 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
33 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
34 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
34 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
35 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
35 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
36
36
37 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
37 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
38 m_gradientEndColor = QColor(QRgb(0xffafafaf));
38 m_gradientEndColor = QColor(QRgb(0xffafafaf));
39 */
39 */
40
40
41 ChartTheme::ChartTheme(QChart::ChartTheme id)
41 ChartTheme::ChartTheme(QChart::ChartTheme id)
42 {
42 {
43 m_id = id;
43 m_id = id;
44 m_seriesColor.append(QRgb(0xff000000));
44 m_seriesColor.append(QRgb(0xff000000));
45 m_seriesColor.append(QRgb(0xff707070));
45 m_seriesColor.append(QRgb(0xff707070));
46 m_gradientStartColor = QColor(QRgb(0xffffffff));
46 m_gradientStartColor = QColor(QRgb(0xffffffff));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
48 }
48 }
49
49
50
50
51 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
51 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
52 {
52 {
53 switch(theme) {
53 switch(theme) {
54 case QChart::ChartThemeDefault:
54 case QChart::ChartThemeDefault:
55 return new ChartTheme();
55 return new ChartTheme();
56 case QChart::ChartThemeVanilla:
56 case QChart::ChartThemeVanilla:
57 return new ChartThemeVanilla();
57 return new ChartThemeVanilla();
58 case QChart::ChartThemeIcy:
58 case QChart::ChartThemeIcy:
59 return new ChartThemeIcy();
59 return new ChartThemeIcy();
60 case QChart::ChartThemeGrayscale:
60 case QChart::ChartThemeGrayscale:
61 return new ChartThemeGrayscale();
61 return new ChartThemeGrayscale();
62 case QChart::ChartThemeScientific:
62 case QChart::ChartThemeScientific:
63 return new ChartThemeScientific();
63 return new ChartThemeScientific();
64 }
64 }
65 }
65 }
66
66
67 void ChartTheme::decorate(QChart* chart)
67 void ChartTheme::decorate(QChart* chart)
68 {
68 {
69 QLinearGradient backgroundGradient;
69 QLinearGradient backgroundGradient;
70 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
70 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
71 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
71 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
72 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
72 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
73 chart->setChartBackgroundBrush(backgroundGradient);
73 chart->setChartBackgroundBrush(backgroundGradient);
74 }
74 }
75 //TODO helper to by removed later
75 //TODO helper to by removed later
76 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
76 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
77 {
77 {
78 switch(series->type())
78 switch(series->type())
79 {
79 {
80 case QChartSeries::SeriesTypeLine: {
80 case QChartSeries::SeriesTypeLine: {
81 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
81 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
82 LineChartItem* i = static_cast<LineChartItem*>(item);
82 LineChartItem* i = static_cast<LineChartItem*>(item);
83 decorate(i,s,count);
83 decorate(i,s,count);
84 break;
84 break;
85 }
85 }
86 case QChartSeries::SeriesTypeBar: {
86 case QChartSeries::SeriesTypeBar: {
87 BarChartSeries* b = static_cast<BarChartSeries*>(series);
87 BarChartSeries* b = static_cast<BarChartSeries*>(series);
88 BarGroup* i = static_cast<BarGroup*>(item);
88 BarGroup* i = static_cast<BarGroup*>(item);
89 decorate(i,b,count);
89 decorate(i,b,count);
90 break;
90 break;
91 }
91 }
92 case QChartSeries::SeriesTypeStackedBar: {
92 case QChartSeries::SeriesTypeStackedBar: {
93 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
93 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
94 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
94 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
95 decorate(i,s,count);
95 decorate(i,s,count);
96 break;
96 break;
97 }
97 }
98 case QChartSeries::SeriesTypePercentBar: {
98 case QChartSeries::SeriesTypePercentBar: {
99 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
99 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
100 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
100 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
101 decorate(i,s,count);
101 decorate(i,s,count);
102 break;
102 break;
103 }
103 }
104 case QChartSeries::SeriesTypePie: {
104 case QChartSeries::SeriesTypePie: {
105 QPieSeries* s = static_cast<QPieSeries*>(series);
105 QPieSeries* s = static_cast<QPieSeries*>(series);
106 PiePresenter* i = static_cast<PiePresenter*>(item);
106 PiePresenter* i = static_cast<PiePresenter*>(item);
107 decorate(i,s,count);
107 decorate(i,s,count);
108 break;
108 break;
109 }
109 }
110 default:
110 default:
111 qDebug()<<"Wrong item to be decorated by theme";
111 qDebug()<<"Wrong item to be decorated by theme";
112 break;
112 break;
113 }
113 }
114
114
115 }
115 }
116
116
117 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
117 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
118 {
118 {
119 QPen pen;
119 QPen pen;
120 if(pen != series->pen()){
120 if(pen != series->pen()){
121 item->setPen(series->pen());
121 item->setPen(series->pen());
122 return;
122 return;
123 }
123 }
124 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
124 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
125 pen.setWidthF(2);
125 pen.setWidthF(2);
126 item->setPen(pen);
126 item->setPen(pen);
127 }
127 }
128
128
129 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
129 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
130 {
130 {
131 for (int i=0; i< m_seriesColor.count(); i++) {
131 for (int i=0; i< m_seriesColor.count(); i++) {
132 item->addColor(m_seriesColor.at(i));
132 item->addColor(m_seriesColor.at(i));
133 }
133 }
134 item->addColor(QColor(255,0,0,128));
134 item->addColor(QColor(255,0,0,128));
135 item->addColor(QColor(255,255,0,128));
135 item->addColor(QColor(255,255,0,128));
136 item->addColor(QColor(0,255,0,128));
136 item->addColor(QColor(0,255,0,128));
137 item->addColor(QColor(0,0,255,128));
137 item->addColor(QColor(0,0,255,128));
138 item->addColor(QColor(255,128,0,128));
138 item->addColor(QColor(255,128,0,128));
139 }
139 }
140
140
141 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
141 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
142 {
142 {
143 for (int i=0; i< m_seriesColor.count(); i++) {
143 for (int i=0; i< m_seriesColor.count(); i++) {
144 item->addColor(m_seriesColor.at(i));
144 item->addColor(m_seriesColor.at(i));
145 }
145 }
146 item->addColor(QColor(255,0,0,128));
146 item->addColor(QColor(255,0,0,128));
147 item->addColor(QColor(255,255,0,128));
147 item->addColor(QColor(255,255,0,128));
148 item->addColor(QColor(0,255,0,128));
148 item->addColor(QColor(0,255,0,128));
149 item->addColor(QColor(0,0,255,128));
149 item->addColor(QColor(0,0,255,128));
150 item->addColor(QColor(255,128,0,128));
150 item->addColor(QColor(255,128,0,128));
151 }
151 }
152
152
153 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
153 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
154 {
154 {
155 for (int i=0; i< m_seriesColor.count(); i++) {
155 for (int i=0; i< m_seriesColor.count(); i++) {
156 item->addColor(m_seriesColor.at(i));
156 item->addColor(m_seriesColor.at(i));
157 }
157 }
158 item->addColor(QColor(255,0,0,128));
158 item->addColor(QColor(255,0,0,128));
159 item->addColor(QColor(255,255,0,128));
159 item->addColor(QColor(255,255,0,128));
160 item->addColor(QColor(0,255,0,128));
160 item->addColor(QColor(0,255,0,128));
161 item->addColor(QColor(0,0,255,128));
161 item->addColor(QColor(0,0,255,128));
162 item->addColor(QColor(255,128,0,128));
162 item->addColor(QColor(255,128,0,128));
163 }
163 }
164
164
165 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
165 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
166 {
166 {
167 // TODO: Does not work well. We need to generate enough different colors
167 // create a list of slice colors based on current theme
168 // based on available theme and not use the same color twice.
168 int i = 0;
169 QList<QColor> colors;
170 while (colors.count() < series->count()) {
171
172 // get base color
173 QColor c = m_seriesColor[i++];
174 i = i % m_seriesColor.count();
175
176 // -1 means achromatic color -> cannot manipulate lightness
177 // TODO: find a better way to randomize lightness
178 if (c.toHsv().hue() == -1)
179 qWarning() << "ChartTheme::decorate() warning: achromatic theme color";
180
181 // randomize lightness
182 qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter
183 c = c.lighter(f);
184
185 // find duplicates
186 bool isUnique = true;
187 foreach (QColor color, colors) {
188 if (c == color)
189 isUnique = false;
190 }
191
192 // add to array if unique
193 //if (isUnique)
194 colors << c;
195 }
196
197 // finally update colors
198 QList<QPieSlice> slices;
169 for (int i=0; i<series->count(); i++) {
199 for (int i=0; i<series->count(); i++) {
170 QPieSlice slice = series->slice(i);
200 QPieSlice slice = series->slice(i);
171 slice.m_color = m_seriesColor.at(i % m_seriesColor.count());
201 slice.m_color = colors.at(i);
172 series->update(i, slice);
202 slices << slice;
173 }
203 }
204
205 series->set(slices);
174 }
206 }
175
207
176
208
177 QTCOMMERCIALCHART_END_NAMESPACE
209 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now