@@ -140,6 +140,7 void ChartPresenter::handleSeriesAdded(QChartSeries* series) | |||||
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); |
@@ -164,13 +164,45 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,i | |||||
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 = |
|
201 | slice.m_color = colors.at(i); | |
172 |
s |
|
202 | slices << slice; | |
173 | } |
|
203 | } | |
|
204 | ||||
|
205 | series->set(slices); | |||
174 | } |
|
206 | } | |
175 |
|
207 | |||
176 |
|
208 |
General Comments 0
You need to be logged in to leave comments.
Login now