@@ -1,263 +1,267 | |||||
1 | #include "charttheme_p.h" |
|
1 | #include "charttheme_p.h" | |
2 | #include "qchart.h" |
|
2 | #include "qchart.h" | |
3 | #include "qchartaxis.h" |
|
3 | #include "qchartaxis.h" | |
4 | #include <QTime> |
|
4 | #include <QTime> | |
5 |
|
5 | |||
6 | //series |
|
6 | //series | |
7 | #include "qbarset.h" |
|
7 | #include "qbarset.h" | |
8 | #include "qbarseries.h" |
|
8 | #include "qbarseries.h" | |
9 | #include "qstackedbarseries.h" |
|
9 | #include "qstackedbarseries.h" | |
10 | #include "qpercentbarseries.h" |
|
10 | #include "qpercentbarseries.h" | |
11 | #include "qlinechartseries.h" |
|
11 | #include "qlinechartseries.h" | |
12 | #include "qscatterseries.h" |
|
12 | #include "qscatterseries.h" | |
13 | #include "qpieseries.h" |
|
13 | #include "qpieseries.h" | |
14 | #include "qpieslice.h" |
|
14 | #include "qpieslice.h" | |
15 |
|
15 | |||
16 | //items |
|
16 | //items | |
17 | #include "axisitem_p.h" |
|
17 | #include "axisitem_p.h" | |
18 | #include "barpresenter.h" |
|
18 | #include "barpresenter.h" | |
19 | #include "stackedbarpresenter.h" |
|
19 | #include "stackedbarpresenter.h" | |
20 | #include "linechartitem_p.h" |
|
20 | #include "linechartitem_p.h" | |
21 | #include "percentbarpresenter.h" |
|
21 | #include "percentbarpresenter.h" | |
22 | #include "scatterpresenter_p.h" |
|
22 | #include "scatterpresenter_p.h" | |
23 | #include "piepresenter.h" |
|
23 | #include "piepresenter.h" | |
24 |
|
24 | |||
25 | //themes |
|
25 | //themes | |
26 | #include "chartthemevanilla_p.h" |
|
26 | #include "chartthemevanilla_p.h" | |
27 | #include "chartthemeicy_p.h" |
|
27 | #include "chartthemeicy_p.h" | |
28 | #include "chartthemegrayscale_p.h" |
|
28 | #include "chartthemegrayscale_p.h" | |
29 | #include "chartthemescientific_p.h" |
|
29 | #include "chartthemescientific_p.h" | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
33 |
|
33 | |||
34 | /* TODO |
|
34 | /* TODO | |
35 | case QChart::ChartThemeUnnamed1: |
|
35 | case QChart::ChartThemeUnnamed1: | |
36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); |
|
36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); | |
37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); |
|
37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); | |
38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); |
|
38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); | |
39 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); |
|
39 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); | |
40 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); |
|
40 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); | |
41 |
|
41 | |||
42 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); |
|
42 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); | |
43 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
43 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); | |
44 | */ |
|
44 | */ | |
45 |
|
45 | |||
46 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
46 | ChartTheme::ChartTheme(QChart::ChartTheme id) | |
47 | { |
|
47 | { | |
48 | m_id = id; |
|
48 | m_id = id; | |
49 | m_seriesColor.append(QRgb(0xff000000)); |
|
49 | m_seriesColor.append(QRgb(0xff000000)); | |
50 | m_seriesColor.append(QRgb(0xff707070)); |
|
50 | m_seriesColor.append(QRgb(0xff707070)); | |
51 | m_gradientStartColor = QColor(QRgb(0xffffffff)); |
|
51 | m_gradientStartColor = QColor(QRgb(0xffffffff)); | |
52 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
52 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); | |
53 |
|
53 | |||
54 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
54 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
58 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | |
59 | { |
|
59 | { | |
60 | switch(theme) { |
|
60 | switch(theme) { | |
61 | case QChart::ChartThemeDefault: |
|
61 | case QChart::ChartThemeDefault: | |
62 | return new ChartThemeIcy(); |
|
62 | return new ChartThemeIcy(); | |
63 | case QChart::ChartThemeVanilla: |
|
63 | case QChart::ChartThemeVanilla: | |
64 | return new ChartThemeVanilla(); |
|
64 | return new ChartThemeVanilla(); | |
65 | case QChart::ChartThemeIcy: |
|
65 | case QChart::ChartThemeIcy: | |
66 | return new ChartThemeIcy(); |
|
66 | return new ChartThemeIcy(); | |
67 | case QChart::ChartThemeGrayscale: |
|
67 | case QChart::ChartThemeGrayscale: | |
68 | return new ChartThemeGrayscale(); |
|
68 | return new ChartThemeGrayscale(); | |
69 | case QChart::ChartThemeScientific: |
|
69 | case QChart::ChartThemeScientific: | |
70 | return new ChartThemeScientific(); |
|
70 | return new ChartThemeScientific(); | |
71 | } |
|
71 | } | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | void ChartTheme::decorate(QChart* chart) |
|
74 | void ChartTheme::decorate(QChart* chart) | |
75 | { |
|
75 | { | |
76 | QLinearGradient backgroundGradient; |
|
76 | QLinearGradient backgroundGradient; | |
77 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); |
|
77 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); | |
78 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); |
|
78 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); | |
79 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
79 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
80 | chart->setChartBackgroundBrush(backgroundGradient); |
|
80 | chart->setChartBackgroundBrush(backgroundGradient); | |
81 | } |
|
81 | } | |
82 | //TODO helper to by removed later |
|
82 | //TODO helper to by removed later | |
83 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) |
|
83 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) | |
84 | { |
|
84 | { | |
85 | switch(series->type()) |
|
85 | switch(series->type()) | |
86 | { |
|
86 | { | |
87 | case QChartSeries::SeriesTypeLine: { |
|
87 | case QChartSeries::SeriesTypeLine: { | |
88 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); |
|
88 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); | |
89 | LineChartItem* i = static_cast<LineChartItem*>(item); |
|
89 | LineChartItem* i = static_cast<LineChartItem*>(item); | |
90 | decorate(i,s,count); |
|
90 | decorate(i,s,count); | |
91 | break; |
|
91 | break; | |
92 | } |
|
92 | } | |
93 | case QChartSeries::SeriesTypeBar: { |
|
93 | case QChartSeries::SeriesTypeBar: { | |
94 | QBarSeries* b = static_cast<QBarSeries*>(series); |
|
94 | QBarSeries* b = static_cast<QBarSeries*>(series); | |
95 | BarPresenter* i = static_cast<BarPresenter*>(item); |
|
95 | BarPresenter* i = static_cast<BarPresenter*>(item); | |
96 | decorate(i,b,count); |
|
96 | decorate(i,b,count); | |
97 | break; |
|
97 | break; | |
98 | } |
|
98 | } | |
99 | case QChartSeries::SeriesTypeStackedBar: { |
|
99 | case QChartSeries::SeriesTypeStackedBar: { | |
100 | QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series); |
|
100 | QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series); | |
101 | StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item); |
|
101 | StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item); | |
102 | decorate(i,s,count); |
|
102 | decorate(i,s,count); | |
103 | break; |
|
103 | break; | |
104 | } |
|
104 | } | |
105 | case QChartSeries::SeriesTypePercentBar: { |
|
105 | case QChartSeries::SeriesTypePercentBar: { | |
106 | QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series); |
|
106 | QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series); | |
107 | PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item); |
|
107 | PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item); | |
108 | decorate(i,s,count); |
|
108 | decorate(i,s,count); | |
109 | break; |
|
109 | break; | |
110 | } |
|
110 | } | |
111 | case QChartSeries::SeriesTypeScatter: { |
|
111 | case QChartSeries::SeriesTypeScatter: { | |
112 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); |
|
112 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); | |
113 | Q_ASSERT(s); |
|
113 | Q_ASSERT(s); | |
114 | ScatterPresenter* i = static_cast<ScatterPresenter*>(item); |
|
114 | ScatterPresenter* i = static_cast<ScatterPresenter*>(item); | |
115 | Q_ASSERT(i); |
|
115 | Q_ASSERT(i); | |
116 | decorate(i, s, count); |
|
116 | decorate(i, s, count); | |
117 | break; |
|
117 | break; | |
118 | } |
|
118 | } | |
119 | case QChartSeries::SeriesTypePie: { |
|
119 | case QChartSeries::SeriesTypePie: { | |
120 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
120 | QPieSeries* s = static_cast<QPieSeries*>(series); | |
121 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
121 | PiePresenter* i = static_cast<PiePresenter*>(item); | |
122 | decorate(i,s,count); |
|
122 | decorate(i,s,count); | |
123 | break; |
|
123 | break; | |
124 | } |
|
124 | } | |
125 | default: |
|
125 | default: | |
126 | qDebug()<<"Wrong item to be decorated by theme"; |
|
126 | qDebug()<<"Wrong item to be decorated by theme"; | |
127 | break; |
|
127 | break; | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | } |
|
130 | } | |
131 |
|
131 | |||
132 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) |
|
132 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) | |
133 | { |
|
133 | { | |
134 | QPen pen; |
|
134 | QPen pen; | |
135 | if(pen != series->pen()){ |
|
135 | if(pen != series->pen()){ | |
136 | item->setPen(series->pen()); |
|
136 | item->setPen(series->pen()); | |
137 | return; |
|
137 | return; | |
138 | } |
|
138 | } | |
139 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
139 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | |
140 | pen.setWidthF(2); |
|
140 | pen.setWidthF(2); | |
141 | item->setPen(pen); |
|
141 | item->setPen(pen); | |
142 | } |
|
142 | } | |
143 |
|
143 | |||
144 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) |
|
144 | void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) | |
145 | { |
|
145 | { | |
146 | for (int i=0; i<series->countSets(); i++) { |
|
146 | for (int i=0; i<series->countSets(); i++) { | |
147 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); |
|
147 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | |
148 | } |
|
148 | } | |
149 | } |
|
149 | } | |
150 |
|
150 | |||
151 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count) |
|
151 | void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count) | |
152 | { |
|
152 | { | |
153 | for (int i=0; i<series->countSets(); i++) { |
|
153 | for (int i=0; i<series->countSets(); i++) { | |
154 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); |
|
154 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | |
155 | } |
|
155 | } | |
156 | } |
|
156 | } | |
157 |
|
157 | |||
158 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count) |
|
158 | void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count) | |
159 | { |
|
159 | { | |
160 | for (int i=0; i<series->countSets(); i++) { |
|
160 | for (int i=0; i<series->countSets(); i++) { | |
161 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); |
|
161 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | |
162 | } |
|
162 | } | |
163 | } |
|
163 | } | |
164 |
|
164 | |||
165 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) |
|
165 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) | |
166 | { |
|
166 | { | |
167 | Q_ASSERT(presenter); |
|
167 | Q_ASSERT(presenter); | |
168 | Q_ASSERT(series); |
|
168 | Q_ASSERT(series); | |
169 |
|
169 | |||
170 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
170 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); | |
171 | // TODO: define alpha in the theme? or in the series? |
|
171 | // TODO: define alpha in the theme? or in the series? | |
172 | //color.setAlpha(120); |
|
172 | //color.setAlpha(120); | |
173 |
|
173 | |||
174 | QBrush brush(color, Qt::SolidPattern); |
|
174 | QBrush brush(color, Qt::SolidPattern); | |
175 | presenter->m_markerBrush = brush; |
|
175 | presenter->m_markerBrush = brush; | |
176 |
|
176 | |||
177 | QPen pen(brush, 3); |
|
177 | QPen pen(brush, 3); | |
178 | pen.setColor(color); |
|
178 | pen.setColor(color); | |
179 | presenter->m_markerPen = pen; |
|
179 | presenter->m_markerPen = pen; | |
180 | } |
|
180 | } | |
181 |
|
181 | |||
182 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
182 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) | |
183 | { |
|
183 | { | |
184 | // create a list of slice colors based on current theme |
|
184 | // create a list of slice colors based on current theme | |
185 | int i = 0; |
|
185 | int i = 0; | |
186 | QList<QColor> colors; |
|
186 | QList<QColor> colors; | |
187 | while (colors.count() < series->count()) { |
|
187 | while (colors.count() < series->count()) { | |
188 |
|
188 | |||
189 | // get base color |
|
189 | // get base color | |
190 | QColor c = m_seriesColor[i++]; |
|
190 | QColor c = m_seriesColor[i++]; | |
191 | i = i % m_seriesColor.count(); |
|
191 | i = i % m_seriesColor.count(); | |
192 |
|
192 | |||
|
193 | // dont use black colors... looks bad | |||
|
194 | if (c == Qt::black) | |||
|
195 | continue; | |||
|
196 | ||||
193 | // by default use the "raw" theme color |
|
197 | // by default use the "raw" theme color | |
194 | if (!colors.contains(c)) { |
|
198 | if (!colors.contains(c)) { | |
195 | colors << c; |
|
199 | colors << c; | |
196 | continue; |
|
200 | continue; | |
197 | } |
|
201 | } | |
198 | // ...ok we need to generate something that looks like the same color |
|
202 | // ...ok we need to generate something that looks like the same color | |
199 | // but different lightness |
|
203 | // but different lightness | |
200 |
|
204 | |||
201 | int tryCount = 0; |
|
205 | int tryCount = 0; | |
202 | while (tryCount++ < 100) { |
|
206 | while (tryCount++ < 100) { | |
203 |
|
207 | |||
204 | // find maximum value we can raise the lightness |
|
208 | // find maximum value we can raise the lightness | |
205 | int lMax = 255; |
|
209 | int lMax = 255; | |
206 | if (lMax > 255 - c.red()) |
|
210 | if (lMax > 255 - c.red()) | |
207 | lMax = 255 - c.red(); |
|
211 | lMax = 255 - c.red(); | |
208 | if (lMax > 255 - c.green()) |
|
212 | if (lMax > 255 - c.green()) | |
209 | lMax = 255 - c.green(); |
|
213 | lMax = 255 - c.green(); | |
210 | if (lMax > 255 - c.blue()) |
|
214 | if (lMax > 255 - c.blue()) | |
211 | lMax = 255 - c.blue(); |
|
215 | lMax = 255 - c.blue(); | |
212 |
|
216 | |||
213 | // find maximum value we can make it darker |
|
217 | // find maximum value we can make it darker | |
214 | int dMax = 255; |
|
218 | int dMax = 255; | |
215 | if (dMax > c.red()) |
|
219 | if (dMax > c.red()) | |
216 | dMax = c.red(); |
|
220 | dMax = c.red(); | |
217 | if (dMax > c.green()) |
|
221 | if (dMax > c.green()) | |
218 | dMax = c.green(); |
|
222 | dMax = c.green(); | |
219 | if (dMax > c.blue()) |
|
223 | if (dMax > c.blue()) | |
220 | dMax = c.blue(); |
|
224 | dMax = c.blue(); | |
221 |
|
225 | |||
222 | int max = dMax + lMax; |
|
226 | int max = dMax + lMax; | |
223 | if (max == 0) { |
|
227 | if (max == 0) { | |
224 | // no room to make color lighter or darker... |
|
228 | // no room to make color lighter or darker... | |
225 | qDebug() << "cannot generate a color for pie!"; |
|
229 | qDebug() << "cannot generate a color for pie!"; | |
226 | break; |
|
230 | break; | |
227 | } |
|
231 | } | |
228 |
|
232 | |||
229 | // generate random color |
|
233 | // generate random color | |
230 | int r = c.red() - dMax; |
|
234 | int r = c.red() - dMax; | |
231 | int g = c.green() - dMax; |
|
235 | int g = c.green() - dMax; | |
232 | int b = c.blue() - dMax; |
|
236 | int b = c.blue() - dMax; | |
233 | int d = qrand() % max; |
|
237 | int d = qrand() % max; | |
234 | c.setRgb(r+d, g+d, b+d); |
|
238 | c.setRgb(r+d, g+d, b+d); | |
235 |
|
239 | |||
236 | // found a unique color? |
|
240 | // found a unique color? | |
237 | if (!colors.contains(c)) |
|
241 | if (!colors.contains(c)) | |
238 | break; |
|
242 | break; | |
239 | } |
|
243 | } | |
240 |
|
244 | |||
241 | qDebug() << "generated a color for pie" << c; |
|
245 | qDebug() << "generated a color for pie" << c; | |
242 | colors << c; |
|
246 | colors << c; | |
243 | } |
|
247 | } | |
244 |
|
248 | |||
245 | // finally update colors |
|
249 | // finally update colors | |
246 | foreach (QPieSlice* s, series->slices()) { |
|
250 | foreach (QPieSlice* s, series->slices()) { | |
247 | QColor c = colors.takeFirst(); |
|
251 | QColor c = colors.takeFirst(); | |
248 | s->setPen(c); |
|
252 | s->setPen(c); | |
249 | s->setBrush(c); |
|
253 | s->setBrush(c); | |
250 | } |
|
254 | } | |
251 | } |
|
255 | } | |
252 |
|
256 | |||
253 |
|
257 | |||
254 | void ChartTheme::decorate(QChartAxis* axis,AxisItem* item) |
|
258 | void ChartTheme::decorate(QChartAxis* axis,AxisItem* item) | |
255 | { |
|
259 | { | |
256 | //TODO: dummy defults for now |
|
260 | //TODO: dummy defults for now | |
257 | axis->setLabelsBrush(Qt::black); |
|
261 | axis->setLabelsBrush(Qt::black); | |
258 | axis->setLabelsPen(Qt::NoPen); |
|
262 | axis->setLabelsPen(Qt::NoPen); | |
259 | axis->setShadesPen(Qt::NoPen); |
|
263 | axis->setShadesPen(Qt::NoPen); | |
260 | axis->setShadesOpacity(0.5); |
|
264 | axis->setShadesOpacity(0.5); | |
261 | } |
|
265 | } | |
262 |
|
266 | |||
263 | QTCOMMERCIALCHART_END_NAMESPACE |
|
267 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now