##// END OF EJS Templates
Pie pen now uses gradient start color
Tero Ahola -
r510:abde8123ae71
parent child
Show More
@@ -1,331 +1,332
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QTime>
5 5
6 6 //series
7 7 #include "qbarset.h"
8 8 #include "qbarseries.h"
9 9 #include "qstackedbarseries.h"
10 10 #include "qpercentbarseries.h"
11 11 #include "qlineseries.h"
12 12 #include "qareaseries.h"
13 13 #include "qscatterseries.h"
14 14 #include "qpieseries.h"
15 15 #include "qpieslice.h"
16 16 #include "qsplineseries.h"
17 17
18 18 //items
19 19 #include "axisitem_p.h"
20 20 #include "barpresenter_p.h"
21 21 #include "stackedbarpresenter_p.h"
22 22 #include "percentbarpresenter_p.h"
23 23 #include "linechartitem_p.h"
24 24 #include "areachartitem_p.h"
25 25 #include "scatterchartitem_p.h"
26 26 #include "piepresenter_p.h"
27 27 #include "splinechartitem_p.h"
28 28
29 29 //themes
30 30 #include "chartthemedefault_p.h"
31 31 #include "chartthemevanilla_p.h"
32 32 #include "chartthemeicy_p.h"
33 33 #include "chartthemegrayscale_p.h"
34 34 #include "chartthemescientific_p.h"
35 35
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 39 ChartTheme::ChartTheme(QChart::ChartTheme id)
40 40 {
41 41 m_id = id;
42 42 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
43 43 }
44 44
45 45
46 46 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
47 47 {
48 48 switch(theme) {
49 49 case QChart::ChartThemeVanilla:
50 50 return new ChartThemeVanilla();
51 51 case QChart::ChartThemeIcy:
52 52 return new ChartThemeIcy();
53 53 case QChart::ChartThemeGrayscale:
54 54 return new ChartThemeGrayscale();
55 55 case QChart::ChartThemeScientific:
56 56 return new ChartThemeScientific();
57 57 default:
58 58 return new ChartThemeDefault();
59 59 }
60 60 }
61 61
62 62 void ChartTheme::decorate(QChart* chart)
63 63 {
64 64 chart->setChartBackgroundBrush(m_backgroundGradient);
65 65 }
66 66 //TODO helper to by removed later
67 67 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
68 68 {
69 69 switch(series->type())
70 70 {
71 71 case QSeries::SeriesTypeLine: {
72 72 QLineSeries* s = static_cast<QLineSeries*>(series);
73 73 LineChartItem* i = static_cast<LineChartItem*>(item);
74 74 decorate(i,s,count);
75 75 break;
76 76 }
77 77 case QSeries::SeriesTypeArea: {
78 78 QAreaSeries* s = static_cast<QAreaSeries*>(series);
79 79 AreaChartItem* i = static_cast<AreaChartItem*>(item);
80 80 decorate(i,s,count);
81 81 break;
82 82 }
83 83 case QSeries::SeriesTypeBar: {
84 84 QBarSeries* b = static_cast<QBarSeries*>(series);
85 85 BarPresenter* i = static_cast<BarPresenter*>(item);
86 86 decorate(i,b,count);
87 87 break;
88 88 }
89 89 case QSeries::SeriesTypeStackedBar: {
90 90 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
91 91 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
92 92 decorate(i,s,count);
93 93 break;
94 94 }
95 95 case QSeries::SeriesTypePercentBar: {
96 96 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
97 97 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
98 98 decorate(i,s,count);
99 99 break;
100 100 }
101 101 case QSeries::SeriesTypeScatter: {
102 102 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
103 103 Q_ASSERT(s);
104 104 ScatterChartItem* i = static_cast<ScatterChartItem*>(item);
105 105 Q_ASSERT(i);
106 106 decorate(i, s, count);
107 107 break;
108 108 }
109 109 case QSeries::SeriesTypePie: {
110 110 QPieSeries* s = static_cast<QPieSeries*>(series);
111 111 PiePresenter* i = static_cast<PiePresenter*>(item);
112 112 decorate(i,s,count);
113 113 break;
114 114 }
115 115 default:
116 116 qDebug()<<"Wrong item to be decorated by theme";
117 117 break;
118 118 }
119 119
120 120 }
121 121
122 122 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count)
123 123 {
124 124 QPen pen;
125 125 QBrush brush;
126 126
127 127 if(pen != series->pen()){
128 128 item->setPen(series->pen());
129 129 }else{
130 130 pen.setColor(m_seriesColors.at(count%m_seriesColors.size()));
131 131 pen.setWidthF(2);
132 132 item->setPen(pen);
133 133 }
134 134
135 135 if(brush != series->brush()){
136 136 item->setBrush(series->brush());
137 137 }else{
138 138 QBrush brush(m_seriesColors.at(count%m_seriesColors.size()));
139 139 item->setBrush(brush);
140 140 }
141 141 }
142 142
143 143
144 144 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
145 145 {
146 146 QPen pen;
147 147 if(pen != series->pen()){
148 148 item->setLinePen(series->pen());
149 149 return;
150 150 }
151 151 pen.setColor(m_seriesColors.at(count%m_seriesColors.size()));
152 152 pen.setWidthF(2);
153 153 item->setLinePen(pen);
154 154 }
155 155
156 156 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
157 157 {
158 158 QList<QBarSet*> sets = series->barSets();
159 159 for (int i=0; i<sets.count(); i++) {
160 160 qreal pos = (qreal) i / (qreal) sets.count();
161 161 QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos);
162 162 sets.at(i)->setBrush(QBrush(c));
163 163 }
164 164 }
165 165
166 166 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
167 167 {
168 168 QList<QBarSet*> sets = series->barSets();
169 169 for (int i=0; i<sets.count(); i++) {
170 170 qreal pos = (qreal) i / (qreal) sets.count();
171 171 QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos);
172 172 sets.at(i)->setBrush(QBrush(c));
173 173 }
174 174 }
175 175
176 176 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
177 177 {
178 178 QList<QBarSet*> sets = series->barSets();
179 179 for (int i=0; i<sets.count(); i++) {
180 180 qreal pos = (qreal) i / (qreal) sets.count();
181 181 QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos);
182 182 sets.at(i)->setBrush(QBrush(c));
183 183 }
184 184 }
185 185
186 186 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int count)
187 187 {
188 188 Q_ASSERT(item);
189 189 Q_ASSERT(series);
190 190
191 191 QColor color = m_seriesColors.at(count % m_seriesColors.size());
192 192 // TODO: define alpha in the theme? or in the series?
193 193 //color.setAlpha(120);
194 194
195 195 QBrush brush(color, Qt::SolidPattern);
196 196 item->setBrush(Qt::blue);
197 197
198 198 QPen pen(brush, 3);
199 199 pen.setColor(color);
200 200 item->setPen(pen);
201 201 }
202 202
203 203 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int count)
204 204 {
205 205 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
206 206 for (int i(0); i < series->slices().count(); i++) {
207 207 qreal pos = (qreal) i / (qreal) series->count();
208 QColor c = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos);
209 series->slices().at(i)->setSlicePen(c);
210 series->slices().at(i)->setSliceBrush(c);
208 QColor penColor = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 0.1);
209 series->slices().at(i)->setSlicePen(penColor);
210 QColor brushColor = colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), pos);
211 series->slices().at(i)->setSliceBrush(brushColor);
211 212 }
212 213 }
213 214
214 215
215 216 void ChartTheme::decorate(QChartAxis* axis, AxisItem* item)
216 217 {
217 218 //TODO: dummy defults for now
218 219 axis->setLabelsBrush(Qt::black);
219 220 axis->setLabelsPen(Qt::NoPen);
220 221 axis->setShadesPen(Qt::NoPen);
221 222 axis->setShadesOpacity(0.5);
222 223 }
223 224
224 225 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int count)
225 226 {
226 227 Q_ASSERT(item);
227 228 Q_ASSERT(series);
228 229
229 230 QPen pen;
230 231
231 232 if(pen != series->pen()){
232 233 item->setLinePen(series->pen());
233 234 }else{
234 235 pen.setColor(m_seriesColors.at(count%m_seriesColors.size()));
235 236 pen.setWidthF(series->pen().widthF());
236 237 item->setLinePen(series->pen());
237 238 }
238 239
239 240 // QColor color = m_seriesColors.at(count % m_seriesColors.size());
240 241 // TODO: define alpha in the theme? or in the series?
241 242 //color.setAlpha(120);
242 243
243 244 // QBrush brush(color, Qt::SolidPattern);
244 245 // presenter->m_markerBrush = brush;
245 246
246 247 // QPen pen(brush, 3);
247 248 // pen.setColor(color);
248 249 // presenter->m_markerPen = pen;
249 250 }
250 251
251 252 void ChartTheme::generateSeriesGradients()
252 253 {
253 254 // Generate gradients in HSV color space
254 255 foreach (QColor color, m_seriesColors) {
255 256 QLinearGradient g;
256 257 qreal h = color.hsvHueF();
257 258 qreal s = color.hsvSaturationF();
258 259
259 260 // TODO: tune the algorithm to give nice results with most base colors defined in
260 261 // most themes. The rest of the gradients we can define manually in theme specific
261 262 // implementation.
262 263 QColor start = color;
263 264 start.setHsvF(h, 0.05, 0.95);
264 265 g.setColorAt(0.0, start);
265 266
266 267 g.setColorAt(0.5, color);
267 268
268 269 QColor end = color;
269 270 end.setHsvF(h, s, 0.25);
270 271 g.setColorAt(1.0, end);
271 272
272 273 m_seriesGradients << g;
273 274 }
274 275 }
275 276
276 277
277 278 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
278 279 {
279 280 Q_ASSERT(pos >=0.0 && pos <= 1.0);
280 281 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
281 282 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
282 283 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
283 284 QColor c;
284 285 c.setRgbF(r, g, b);
285 286 return c;
286 287 }
287 288
288 289 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
289 290 {
290 291 Q_ASSERT(pos >=0 && pos <= 1.0);
291 292
292 293 // another possibility:
293 294 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
294 295
295 296 QGradientStops stops = gradient.stops();
296 297 int count = stops.count();
297 298
298 299 // find previous stop relative to position
299 300 QGradientStop prev = stops.first();
300 301 for (int i=0; i<count; i++) {
301 302 QGradientStop stop = stops.at(i);
302 303 if (pos > stop.first)
303 304 prev = stop;
304 305
305 306 // given position is actually a stop position?
306 307 if (pos == stop.first) {
307 308 //qDebug() << "stop color" << pos;
308 309 return stop.second;
309 310 }
310 311 }
311 312
312 313 // find next stop relative to position
313 314 QGradientStop next = stops.last();
314 315 for (int i=count-1; i>=0; i--) {
315 316 QGradientStop stop = stops.at(i);
316 317 if (pos < stop.first)
317 318 next = stop;
318 319 }
319 320
320 321 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
321 322
322 323 qreal range = next.first - prev.first;
323 324 qreal posDelta = pos - prev.first;
324 325 qreal relativePos = posDelta / range;
325 326
326 327 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
327 328
328 329 return colorAt(prev.second, next.second, relativePos);
329 330 }
330 331
331 332 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,369 +1,369
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include <qpieseries.h>
4 4 #include <qscatterseries.h>
5 5 #include <qlineseries.h>
6 6 #include <qareaseries.h>
7 7 #include <qsplineseries.h>
8 8 #include <qbarset.h>
9 9 #include <qbarseries.h>
10 10 #include <qstackedbarseries.h>
11 11 #include <qpercentbarseries.h>
12 12 #include <QPushButton>
13 13 #include <QComboBox>
14 14 #include <QSpinBox>
15 15 #include <QCheckBox>
16 16 #include <QGridLayout>
17 17 #include <QHBoxLayout>
18 18 #include <QLabel>
19 19 #include <QSpacerItem>
20 20 #include <QMessageBox>
21 21 #include <cmath>
22 22 #include <QDebug>
23 23 #include <QStandardItemModel>
24 24
25 25
26 26 QTCOMMERCIALCHART_USE_NAMESPACE
27 27
28 28 MainWidget::MainWidget(QWidget *parent) :
29 29 QWidget(parent),
30 30 m_addSerieDialog(0),
31 31 m_chartView(0)
32 32 {
33 33 m_chartView = new QChartView(this);
34 34 m_chartView->setRubberBandPolicy(QChartView::HorizonalRubberBand);
35 35
36 36 // Grid layout for the controls for configuring the chart widget
37 37 QGridLayout *grid = new QGridLayout();
38 38 QPushButton *addSeriesButton = new QPushButton("Add series");
39 39 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
40 40 grid->addWidget(addSeriesButton, 0, 1);
41 41 initBackroundCombo(grid);
42 42 initScaleControls(grid);
43 43 initThemeCombo(grid);
44 44 initCheckboxes(grid);
45 45
46 46 // add row with empty label to make all the other rows static
47 47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
48 48 grid->setRowStretch(grid->rowCount() - 1, 1);
49 49
50 50 // Another grid layout as a main layout
51 51 QGridLayout *mainLayout = new QGridLayout();
52 52 mainLayout->addLayout(grid, 0, 0);
53 53
54 54 // Add layouts and the chart widget to the main layout
55 55 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
56 56 setLayout(mainLayout);
57 57 }
58 58
59 59 // Combo box for selecting the chart's background
60 60 void MainWidget::initBackroundCombo(QGridLayout *grid)
61 61 {
62 62 QComboBox *backgroundCombo = new QComboBox(this);
63 63 backgroundCombo->addItem("Color");
64 64 backgroundCombo->addItem("Gradient");
65 65 backgroundCombo->addItem("Image");
66 66 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
67 67 this, SLOT(backgroundChanged(int)));
68 68
69 69 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
70 70 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
71 71 }
72 72
73 73 // Scale related controls (auto-scale vs. manual min-max values)
74 74 void MainWidget::initScaleControls(QGridLayout *grid)
75 75 {
76 76 m_autoScaleCheck = new QCheckBox("Automatic scaling");
77 77 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
78 78 // Allow setting also non-sense values (like -2147483648 and 2147483647)
79 79 m_xMinSpin = new QSpinBox();
80 80 m_xMinSpin->setMinimum(INT_MIN);
81 81 m_xMinSpin->setMaximum(INT_MAX);
82 82 m_xMinSpin->setValue(0);
83 83 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
84 84 m_xMaxSpin = new QSpinBox();
85 85 m_xMaxSpin->setMinimum(INT_MIN);
86 86 m_xMaxSpin->setMaximum(INT_MAX);
87 87 m_xMaxSpin->setValue(10);
88 88 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
89 89 m_yMinSpin = new QSpinBox();
90 90 m_yMinSpin->setMinimum(INT_MIN);
91 91 m_yMinSpin->setMaximum(INT_MAX);
92 92 m_yMinSpin->setValue(0);
93 93 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
94 94 m_yMaxSpin = new QSpinBox();
95 95 m_yMaxSpin->setMinimum(INT_MIN);
96 96 m_yMaxSpin->setMaximum(INT_MAX);
97 97 m_yMaxSpin->setValue(10);
98 98 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
99 99
100 100 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
101 101 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
102 102 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
103 103 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
104 104 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
105 105 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
106 106 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
107 107 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
108 108 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
109 109
110 110 m_autoScaleCheck->setChecked(true);
111 111 }
112 112
113 113 // Combo box for selecting theme
114 114 void MainWidget::initThemeCombo(QGridLayout *grid)
115 115 {
116 116 QComboBox *chartTheme = new QComboBox();
117 117 chartTheme->addItem("Default");
118 118 chartTheme->addItem("Vanilla");
119 119 chartTheme->addItem("Icy");
120 120 chartTheme->addItem("Grayscale");
121 121 chartTheme->addItem("Scientific");
122 122 chartTheme->addItem("Unnamed1");
123 123 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
124 124 this, SLOT(changeChartTheme(int)));
125 125 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
126 126 grid->addWidget(chartTheme, 8, 1);
127 127 }
128 128
129 129 // Different check boxes for customizing chart
130 130 void MainWidget::initCheckboxes(QGridLayout *grid)
131 131 {
132 132 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
133 133 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
134 134 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
135 135 zoomCheckBox->setChecked(true);
136 136 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
137 137
138 138 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
139 139 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
140 140 aliasCheckBox->setChecked(false);
141 141 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
142 142 }
143 143
144 144 void MainWidget::antiAliasToggled(bool enabled)
145 145 {
146 146 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
147 147 }
148 148
149 149 void MainWidget::addSeries()
150 150 {
151 151 if (!m_addSerieDialog) {
152 152 m_addSerieDialog = new DataSerieDialog(this);
153 153 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
154 154 this, SLOT(addSeries(QString, int, int, QString, bool)));
155 155 }
156 156 m_addSerieDialog->exec();
157 157 }
158 158
159 159 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
160 160 {
161 161 // TODO: dataCharacteristics
162 162 QList<RealList> testData;
163 163 for (int j(0); j < columnCount; j++) {
164 164 QList <qreal> newColumn;
165 165 for (int i(0); i < rowCount; i++) {
166 166 if (dataCharacteristics == "Sin") {
167 167 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
168 168 } else if (dataCharacteristics == "Sin + random") {
169 169 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
170 170 } else if (dataCharacteristics == "Random") {
171 newColumn.append(rand() % 5);
171 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
172 172 } else if (dataCharacteristics == "Linear") {
173 173 //newColumn.append(i * (j + 1.0));
174 174 // TODO: temporary hack to make pie work; prevent zero values:
175 175 newColumn.append(i * (j + 1.0) + 0.1);
176 176 } else { // "constant"
177 177 newColumn.append((j + 1.0));
178 178 }
179 179 }
180 180 testData.append(newColumn);
181 181 }
182 182 return testData;
183 183 }
184 184
185 185 QStringList MainWidget::generateLabels(int count)
186 186 {
187 187 QStringList result;
188 188 for (int i(0); i < count; i++)
189 189 result.append("label" + QString::number(i));
190 190 return result;
191 191 }
192 192
193 193 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
194 194 {
195 195 qDebug() << "addSeries: " << seriesName
196 196 << " columnCount: " << columnCount
197 197 << " rowCount: " << rowCount
198 198 << " dataCharacteristics: " << dataCharacteristics
199 199 << " labels enabled: " << labelsEnabled;
200 200 m_defaultSeriesName = seriesName;
201 201
202 202 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
203 203
204 204 // Line series and scatter series use similar data
205 205 if (seriesName == "Line") {
206 206 for (int j(0); j < data.count(); j ++) {
207 207 QList<qreal> column = data.at(j);
208 208 QLineSeries *series = new QLineSeries();
209 209 for (int i(0); i < column.count(); i++) {
210 210 series->add(i, column.at(i));
211 211 }
212 212 m_chartView->addSeries(series);
213 213 setCurrentSeries(series);
214 214 }
215 215 } if (seriesName == "Area") {
216 216 // TODO: lower series for the area?
217 217 for (int j(0); j < data.count(); j ++) {
218 218 QList<qreal> column = data.at(j);
219 219 QLineSeries *lineSeries = new QLineSeries();
220 220 for (int i(0); i < column.count(); i++) {
221 221 lineSeries->add(i, column.at(i));
222 222 }
223 223 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
224 224 m_chartView->addSeries(areaSeries);
225 225 setCurrentSeries(areaSeries);
226 226 }
227 227 } else if (seriesName == "Scatter") {
228 228 for (int j(0); j < data.count(); j++) {
229 229 QList<qreal> column = data.at(j);
230 230 QScatterSeries *series = new QScatterSeries();
231 231 for (int i(0); i < column.count(); i++) {
232 232 (*series) << QPointF(i, column.at(i));
233 233 }
234 234 m_chartView->addSeries(series);
235 235 setCurrentSeries(series);
236 236 }
237 237 } else if (seriesName == "Pie") {
238 238 QStringList labels = generateLabels(rowCount);
239 239 for (int j(0); j < data.count(); j++) {
240 240 QPieSeries *series = new QPieSeries();
241 241 QList<qreal> column = data.at(j);
242 242 for (int i(0); i < column.count(); i++) {
243 243 series->add(column.at(i), labels.at(i));
244 244 }
245 245 m_chartView->addSeries(series);
246 246 setCurrentSeries(series);
247 247 }
248 248 } else if (seriesName == "Bar"
249 249 || seriesName == "Stacked bar"
250 250 || seriesName == "Percent bar") {
251 251 QStringList category;
252 252 QStringList labels = generateLabels(rowCount);
253 253 foreach(QString label, labels)
254 254 category << label;
255 255 QBarSeries* series = 0;
256 256 if (seriesName == "Bar")
257 257 series = new QBarSeries(category, this);
258 258 else if (seriesName == "Stacked bar")
259 259 series = new QStackedBarSeries(category, this);
260 260 else
261 261 series = new QPercentBarSeries(category, this);
262 262
263 263 for (int j(0); j < data.count(); j++) {
264 264 QList<qreal> column = data.at(j);
265 265 QBarSet *set = new QBarSet("set" + QString::number(j));
266 266 for (int i(0); i < column.count(); i++) {
267 267 *set << column.at(i);
268 268 }
269 269 series->addBarSet(set);
270 270 }
271 271
272 272 // TODO: new implementation of setFloatingValuesEnabled with signals
273 273 //series->setFloatingValuesEnabled(true);
274 274 series->setToolTipEnabled(true);
275 275 series->setSeparatorsVisible(false);
276 276 m_chartView->addSeries(series);
277 277 setCurrentSeries(series);
278 278 } else if (seriesName == "Spline") {
279 279 for (int j(0); j < data.count(); j ++) {
280 280 QList<qreal> column = data.at(j);
281 281 QSplineSeries *series = new QSplineSeries();
282 282 for (int i(0); i < column.count(); i++) {
283 283 series->add(i, column.at(i));
284 284 }
285 285 m_chartView->addSeries(series);
286 286 setCurrentSeries(series);
287 287 }
288 288
289 289 // TODO: area
290 290 }
291 291 }
292 292
293 293 void MainWidget::setCurrentSeries(QSeries *series)
294 294 {
295 295 if (series) {
296 296 m_currentSeries = series;
297 297 switch (m_currentSeries->type()) {
298 298 case QSeries::SeriesTypeLine:
299 299 break;
300 300 case QSeries::SeriesTypeScatter:
301 301 break;
302 302 case QSeries::SeriesTypePie:
303 303 break;
304 304 case QSeries::SeriesTypeBar:
305 305 qDebug() << "setCurrentSeries (bar)";
306 306 break;
307 307 case QSeries::SeriesTypeStackedBar:
308 308 qDebug() << "setCurrentSeries (Stackedbar)";
309 309 break;
310 310 case QSeries::SeriesTypePercentBar:
311 311 qDebug() << "setCurrentSeries (Percentbar)";
312 312 break;
313 313 case QSeries::SeriesTypeSpline:
314 314 break;
315 315 default:
316 316 Q_ASSERT(false);
317 317 break;
318 318 }
319 319 }
320 320 }
321 321
322 322 void MainWidget::backgroundChanged(int itemIndex)
323 323 {
324 324 qDebug() << "backgroundChanged: " << itemIndex;
325 325 }
326 326
327 327 void MainWidget::autoScaleChanged(int value)
328 328 {
329 329 if (value) {
330 330 // TODO: enable auto scaling
331 331 } else {
332 332 // TODO: set scaling manually (and disable auto scaling)
333 333 }
334 334
335 335 m_xMinSpin->setEnabled(!value);
336 336 m_xMaxSpin->setEnabled(!value);
337 337 m_yMinSpin->setEnabled(!value);
338 338 m_yMaxSpin->setEnabled(!value);
339 339 }
340 340
341 341 void MainWidget::xMinChanged(int value)
342 342 {
343 343 qDebug() << "xMinChanged: " << value;
344 344 }
345 345
346 346 void MainWidget::xMaxChanged(int value)
347 347 {
348 348 qDebug() << "xMaxChanged: " << value;
349 349 }
350 350
351 351 void MainWidget::yMinChanged(int value)
352 352 {
353 353 qDebug() << "yMinChanged: " << value;
354 354 }
355 355
356 356 void MainWidget::yMaxChanged(int value)
357 357 {
358 358 qDebug() << "yMaxChanged: " << value;
359 359 }
360 360
361 361 void MainWidget::changeChartTheme(int themeIndex)
362 362 {
363 363 qDebug() << "changeChartTheme: " << themeIndex;
364 364 m_chartView->setChartTheme((QChart::ChartTheme) themeIndex);
365 365 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
366 366 QSize s = size();
367 367 s.setWidth(s.width()+1);
368 368 resize(s);
369 369 }
General Comments 0
You need to be logged in to leave comments. Login now