##// END OF EJS Templates
Default theme background, removed extra themes
Tero Ahola -
r692:339ba14ee2ea
parent child
Show More
@@ -1,247 +1,245
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qpieseries.h>
6 6 #include <qpieslice.h>
7 7 #include <qbarseries.h>
8 8 #include <qpercentbarseries.h>
9 9 #include <qstackedbarseries.h>
10 10 #include <qbarset.h>
11 11 #include <QGridLayout>
12 12 #include <QFormLayout>
13 13 #include <QComboBox>
14 14 #include <QSpinBox>
15 15 #include <QCheckBox>
16 16 #include <QGroupBox>
17 17 #include <QLabel>
18 18 #include <QTime>
19 19 #include <qlineseries.h>
20 20 #include <qsplineseries.h>
21 21 #include <qscatterseries.h>
22 22 #include <qareaseries.h>
23 23
24 24 QTCOMMERCIALCHART_USE_NAMESPACE
25 25
26 26 typedef QPair<QPointF, QString> Data;
27 27 typedef QList<Data> DataList;
28 28 typedef QList<DataList> DataTable;
29 29
30 30
31 31 class MainWidget : public QWidget
32 32 {
33 33 Q_OBJECT
34 34
35 35 public:
36 36 explicit MainWidget(QWidget* parent = 0)
37 37 :QWidget(parent)
38 38 {
39 39 // set seed for random stuff
40 40 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
41 41
42 42 // generate random data
43 43 int listCount = 3;
44 44 int valueMax = 100;
45 45 int valueCount = 11;
46 46 for (int i(0); i < listCount; i++) {
47 47 DataList dataList;
48 48 for (int j(0); j < valueCount; j++) {
49 49 QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax);
50 50 QString label = QString::number(i) + ":" + QString::number(j);
51 51 dataList << Data(value, label);
52 52 }
53 53 m_dataTable << dataList;
54 54 }
55 55
56 56 // create layout
57 57 QGridLayout* baseLayout = new QGridLayout();
58 58
59 59 // settings layout
60 60 m_themeComboBox = new QComboBox();
61 61 m_themeComboBox->addItem("Default", QChart::ChartThemeDefault);
62 62 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
63 63 m_themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
64 64 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
65 65 m_themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
66 66 m_themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
67 m_themeComboBox->addItem("Vanilla", QChart::ChartThemeVanilla);
68 67 m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy);
69 m_themeComboBox->addItem("Grayscale", QChart::ChartThemeGrayscale);
70 68 m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific);
71 69 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme()));
72 70 QCheckBox *antialiasCheckBox = new QCheckBox("Anti aliasing");
73 71 connect(antialiasCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAntialiasing(bool)));
74 72 QCheckBox *animatedCheckBox = new QCheckBox("Animated");
75 73 connect(animatedCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAnimations(bool)));
76 74 QHBoxLayout *settingsLayout = new QHBoxLayout();
77 75 settingsLayout->addWidget(new QLabel("Theme:"));
78 76 settingsLayout->addWidget(m_themeComboBox);
79 77 settingsLayout->addWidget(antialiasCheckBox);
80 78 settingsLayout->addWidget(animatedCheckBox);
81 79 settingsLayout->addStretch();
82 80 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
83 81
84 82 // area chart
85 83 QChartView *chart = new QChartView();
86 84 chart->setChartTitle("Area chart");
87 85 baseLayout->addWidget(chart, 1, 0);
88 86 {
89 87 for (int i(0); i < m_dataTable.count(); i++) {
90 88 QLineSeries *series1 = new QLineSeries(chart);
91 89 QLineSeries *series2 = new QLineSeries(chart);
92 90 foreach (Data data, m_dataTable[i]) {
93 91 series1->add(data.first);
94 92 series2->add(QPointF(data.first.x(), 0.0));
95 93 }
96 94 QAreaSeries *area = new QAreaSeries(series1, series2);
97 95 chart->addSeries(area);
98 96 }
99 97 }
100 98 m_charts << chart;
101 99
102 100 // bar chart
103 101 chart = new QChartView();
104 102 chart->setChartTitle("bar chart");
105 103 baseLayout->addWidget(chart, 1, 1);
106 104 {
107 105 QStringList categories;
108 106 // TODO: categories
109 107 for (int i(0); i < valueCount; i++)
110 108 categories << QString::number(i);
111 109 // QBarSeries* series = new QBarSeries(categories, chart);
112 110 // QPercentBarSeries* series = new QPercentBarSeries(categories, chart);
113 111 QStackedBarSeries* series = new QStackedBarSeries(categories, chart);
114 112 for (int i(0); i < m_dataTable.count(); i++) {
115 113 QBarSet *set = new QBarSet("Set" + QString::number(i));
116 114 foreach (Data data, m_dataTable[i])
117 115 *set << data.first.y();
118 116 series->addBarSet(set);
119 117 }
120 118 chart->addSeries(series);
121 119 }
122 120 m_charts << chart;
123 121
124 122 // line chart
125 123 chart = new QChartView();
126 124 chart->setChartTitle("line chart");
127 125 baseLayout->addWidget(chart, 1, 2);
128 126 foreach (DataList list, m_dataTable) {
129 127 QLineSeries *series = new QLineSeries(chart);
130 128 foreach (Data data, list)
131 129 series->add(data.first);
132 130 chart->addSeries(series);
133 131 }
134 132 m_charts << chart;
135 133
136 134 // pie chart
137 135 chart = new QChartView();
138 136 chart->setChartTitle("pie chart");
139 137 baseLayout->addWidget(chart, 2, 0);
140 138 qreal pieSize = 1.0 / m_dataTable.count();
141 139 for (int i=0; i<m_dataTable.count(); i++) {
142 140 QPieSeries *series = new QPieSeries(chart);
143 141 foreach (Data data, m_dataTable[i])
144 142 series->add(data.first.y(), data.second);
145 143 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
146 144 series->setPieSize(pieSize);
147 145 series->setPiePosition(hPos, 0.5);
148 146 chart->addSeries(series);
149 147 }
150 148 m_charts << chart;
151 149
152 150 // spine chart
153 151 chart = new QChartView();
154 152 chart->setChartTitle("spline chart");
155 153 baseLayout->addWidget(chart, 2, 1);
156 154 foreach (DataList list, m_dataTable) {
157 155 QSplineSeries *series = new QSplineSeries(chart);
158 156 foreach (Data data, list)
159 157 series->add(data.first);
160 158 chart->addSeries(series);
161 159 }
162 160 m_charts << chart;
163 161
164 162 // scatter chart
165 163 chart = new QChartView();
166 164 chart->setChartTitle("scatter chart");
167 165 baseLayout->addWidget(chart, 2, 2);
168 166 foreach (DataList list, m_dataTable) {
169 167 QScatterSeries *series = new QScatterSeries(chart);
170 168 foreach (Data data, list)
171 169 series->add(data.first);
172 170 chart->addSeries(series);
173 171 }
174 172 m_charts << chart;
175 173
176 174 setLayout(baseLayout);
177 175 }
178 176
179 177 public Q_SLOTS:
180 178
181 179 void updateTheme()
182 180 {
183 181 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
184 182 foreach (QChartView *chart, m_charts)
185 183 chart->setChartTheme(theme);
186 184
187 185 QPalette pal = window()->palette();
188 186 if (theme == QChart::ChartThemeLight) {
189 187 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
190 188 pal.setColor(QPalette::WindowText, QRgb(0x404044));
191 189 } else if (theme == QChart::ChartThemeDark) {
192 190 pal.setColor(QPalette::Window, QRgb(0x121218));
193 191 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
194 192 } else if (theme == QChart::ChartThemeBlueCerulean) {
195 193 pal.setColor(QPalette::Window, QRgb(0x40434a));
196 194 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
197 195 } else if (theme == QChart::ChartThemeBrownSand) {
198 196 pal.setColor(QPalette::Window, QRgb(0x9e8965));
199 197 pal.setColor(QPalette::WindowText, QRgb(0x404044));
200 198 } else if (theme == QChart::ChartThemeBlueNcs) {
201 199 pal.setColor(QPalette::Window, QRgb(0x018bba));
202 200 pal.setColor(QPalette::WindowText, QRgb(0x404044));
203 201 } else {
204 202 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
205 203 pal.setColor(QPalette::WindowText, QRgb(0x404044));
206 204 }
207 205 window()->setPalette(pal);
208 206 }
209 207
210 208 void updateAntialiasing(bool enabled)
211 209 {
212 210 foreach (QChartView *chart, m_charts)
213 211 chart->setRenderHint(QPainter::Antialiasing, enabled);
214 212 }
215 213
216 214 void updateAnimations(bool animated)
217 215 {
218 216 QChart::AnimationOptions options = QChart::NoAnimation;
219 217 if (animated)
220 218 options = QChart::AllAnimations;
221 219
222 220 foreach (QChartView *chart, m_charts)
223 221 chart->setAnimationOptions(options);
224 222 }
225 223
226 224 private:
227 225 QList<QChartView*> m_charts;
228 226 QComboBox *m_themeComboBox;
229 227 DataTable m_dataTable;
230 228 };
231 229
232 230 int main(int argc, char *argv[])
233 231 {
234 232 QApplication a(argc, argv);
235 233
236 234 QMainWindow window;
237 235
238 236 MainWidget* widget = new MainWidget();
239 237
240 238 window.setCentralWidget(widget);
241 239 window.resize(900, 600);
242 240 window.show();
243 241
244 242 return a.exec();
245 243 }
246 244
247 245 #include "main.moc"
@@ -1,610 +1,608
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qpieseries.h>
6 6 #include <qpieslice.h>
7 7 #include <QGridLayout>
8 8 #include <QFormLayout>
9 9 #include <QComboBox>
10 10 #include <QSpinBox>
11 11 #include <QCheckBox>
12 12 #include <QGroupBox>
13 13 #include <QLabel>
14 14 #include <QPushButton>
15 15 #include <QColorDialog>
16 16 #include <QFontDialog>
17 17
18 18 QTCOMMERCIALCHART_USE_NAMESPACE
19 19
20 20 class PenTool : public QWidget
21 21 {
22 22 Q_OBJECT
23 23
24 24 public:
25 25 explicit PenTool(QString title, QWidget *parent = 0)
26 26 :QWidget(parent)
27 27 {
28 28 setWindowTitle(title);
29 29 setWindowFlags(Qt::Tool);
30 30
31 31 m_colorButton = new QPushButton();
32 32
33 33 m_widthSpinBox = new QDoubleSpinBox();
34 34
35 35 m_styleCombo = new QComboBox();
36 36 m_styleCombo->addItem("NoPen");
37 37 m_styleCombo->addItem("SolidLine");
38 38 m_styleCombo->addItem("DashLine");
39 39 m_styleCombo->addItem("DotLine");
40 40 m_styleCombo->addItem("DashDotLine");
41 41 m_styleCombo->addItem("DashDotDotLine");
42 42
43 43 m_capStyleCombo = new QComboBox();
44 44 m_capStyleCombo->addItem("FlatCap", Qt::FlatCap);
45 45 m_capStyleCombo->addItem("SquareCap", Qt::SquareCap);
46 46 m_capStyleCombo->addItem("RoundCap", Qt::RoundCap);
47 47
48 48 m_joinStyleCombo = new QComboBox();
49 49 m_joinStyleCombo->addItem("MiterJoin", Qt::MiterJoin);
50 50 m_joinStyleCombo->addItem("BevelJoin", Qt::BevelJoin);
51 51 m_joinStyleCombo->addItem("RoundJoin", Qt::RoundJoin);
52 52 m_joinStyleCombo->addItem("SvgMiterJoin", Qt::SvgMiterJoin);
53 53
54 54 QFormLayout *layout = new QFormLayout();
55 55 layout->addRow("Color", m_colorButton);
56 56 layout->addRow("Width", m_widthSpinBox);
57 57 layout->addRow("Style", m_styleCombo);
58 58 layout->addRow("Cap style", m_capStyleCombo);
59 59 layout->addRow("Join style", m_joinStyleCombo);
60 60 setLayout(layout);
61 61
62 62 connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog()));
63 63 connect(m_widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateWidth(double)));
64 64 connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle(int)));
65 65 connect(m_capStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateCapStyle(int)));
66 66 connect(m_joinStyleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateJoinStyle(int)));
67 67 }
68 68
69 69 void setPen(QPen pen)
70 70 {
71 71 m_pen = pen;
72 72 m_colorButton->setText(m_pen.color().name());
73 73 m_widthSpinBox->setValue(m_pen.widthF());
74 74 m_styleCombo->setCurrentIndex(m_pen.style()); // index matches the enum
75 75 m_capStyleCombo->setCurrentIndex(m_capStyleCombo->findData(m_pen.capStyle()));
76 76 m_joinStyleCombo->setCurrentIndex(m_joinStyleCombo->findData(m_pen.joinStyle()));
77 77 }
78 78
79 79 QPen pen() const
80 80 {
81 81 return m_pen;
82 82 }
83 83
84 84 QString name()
85 85 {
86 86 return name(m_pen);
87 87 }
88 88
89 89 static QString name(const QPen &pen)
90 90 {
91 91 return pen.color().name() + ":" + QString::number(pen.widthF());
92 92 }
93 93
94 94 Q_SIGNALS:
95 95 void changed();
96 96
97 97 public Q_SLOTS:
98 98
99 99 void showColorDialog()
100 100 {
101 101 QColorDialog dialog(m_pen.color());
102 102 dialog.show();
103 103 dialog.exec();
104 104 m_pen.setColor(dialog.selectedColor());
105 105 m_colorButton->setText(m_pen.color().name());
106 106 emit changed();
107 107 }
108 108
109 109 void updateWidth(double width)
110 110 {
111 111 if (width != m_pen.widthF()) {
112 112 m_pen.setWidthF(width);
113 113 emit changed();
114 114 }
115 115 }
116 116
117 117 void updateStyle(int style)
118 118 {
119 119 if (m_pen.style() != style) {
120 120 m_pen.setStyle((Qt::PenStyle) style);
121 121 emit changed();
122 122 }
123 123 }
124 124
125 125 void updateCapStyle(int index)
126 126 {
127 127 Qt::PenCapStyle capStyle = (Qt::PenCapStyle) m_capStyleCombo->itemData(index).toInt();
128 128 if (m_pen.capStyle() != capStyle) {
129 129 m_pen.setCapStyle(capStyle);
130 130 emit changed();
131 131 }
132 132 }
133 133
134 134 void updateJoinStyle(int index)
135 135 {
136 136 Qt::PenJoinStyle joinStyle = (Qt::PenJoinStyle) m_joinStyleCombo->itemData(index).toInt();
137 137 if (m_pen.joinStyle() != joinStyle) {
138 138 m_pen.setJoinStyle(joinStyle);
139 139 emit changed();
140 140 }
141 141 }
142 142
143 143 private:
144 144 QPen m_pen;
145 145 QPushButton *m_colorButton;
146 146 QDoubleSpinBox *m_widthSpinBox;
147 147 QComboBox *m_styleCombo;
148 148 QComboBox *m_capStyleCombo;
149 149 QComboBox *m_joinStyleCombo;
150 150 };
151 151
152 152 class BrushTool : public QWidget
153 153 {
154 154 Q_OBJECT
155 155
156 156 public:
157 157 explicit BrushTool(QString title, QWidget *parent = 0)
158 158 :QWidget(parent)
159 159 {
160 160 setWindowTitle(title);
161 161 setWindowFlags(Qt::Tool);
162 162
163 163 m_colorButton = new QPushButton();
164 164 m_styleCombo = new QComboBox();
165 165 m_styleCombo->addItem("Nobrush", Qt::NoBrush);
166 166 m_styleCombo->addItem("Solidpattern", Qt::SolidPattern);
167 167 m_styleCombo->addItem("Dense1pattern", Qt::Dense1Pattern);
168 168 m_styleCombo->addItem("Dense2attern", Qt::Dense2Pattern);
169 169 m_styleCombo->addItem("Dense3Pattern", Qt::Dense3Pattern);
170 170 m_styleCombo->addItem("Dense4Pattern", Qt::Dense4Pattern);
171 171 m_styleCombo->addItem("Dense5Pattern", Qt::Dense5Pattern);
172 172 m_styleCombo->addItem("Dense6Pattern", Qt::Dense6Pattern);
173 173 m_styleCombo->addItem("Dense7Pattern", Qt::Dense7Pattern);
174 174 m_styleCombo->addItem("HorPattern", Qt::HorPattern);
175 175 m_styleCombo->addItem("VerPattern", Qt::VerPattern);
176 176 m_styleCombo->addItem("CrossPattern", Qt::CrossPattern);
177 177 m_styleCombo->addItem("BDiagPattern", Qt::BDiagPattern);
178 178 m_styleCombo->addItem("FDiagPattern", Qt::FDiagPattern);
179 179 m_styleCombo->addItem("DiagCrossPattern", Qt::DiagCrossPattern);
180 180
181 181 QFormLayout *layout = new QFormLayout();
182 182 layout->addRow("Color", m_colorButton);
183 183 layout->addRow("Style", m_styleCombo);
184 184 setLayout(layout);
185 185
186 186 connect(m_colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog()));
187 187 connect(m_styleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateStyle()));
188 188 }
189 189
190 190 void setBrush(QBrush brush)
191 191 {
192 192 m_brush = brush;
193 193 m_colorButton->setText(m_brush.color().name());
194 194 m_styleCombo->setCurrentIndex(m_brush.style()); // index matches the enum
195 195 }
196 196
197 197 QBrush brush() const
198 198 {
199 199 return m_brush;
200 200 }
201 201
202 202 QString name()
203 203 {
204 204 return name(m_brush);
205 205 }
206 206
207 207 static QString name(const QBrush &brush)
208 208 {
209 209 return brush.color().name();
210 210 }
211 211
212 212 Q_SIGNALS:
213 213 void changed();
214 214
215 215 public Q_SLOTS:
216 216
217 217 void showColorDialog()
218 218 {
219 219 QColorDialog dialog(m_brush.color());
220 220 dialog.show();
221 221 dialog.exec();
222 222 m_brush.setColor(dialog.selectedColor());
223 223 m_colorButton->setText(m_brush.color().name());
224 224 emit changed();
225 225 }
226 226
227 227 void updateStyle()
228 228 {
229 229 Qt::BrushStyle style = (Qt::BrushStyle) m_styleCombo->itemData(m_styleCombo->currentIndex()).toInt();
230 230 if (m_brush.style() != style) {
231 231 m_brush.setStyle(style);
232 232 emit changed();
233 233 }
234 234 }
235 235
236 236 private:
237 237 QBrush m_brush;
238 238 QPushButton *m_colorButton;
239 239 QComboBox *m_styleCombo;
240 240 };
241 241
242 242 class CustomSlice : public QPieSlice
243 243 {
244 244 Q_OBJECT
245 245 public:
246 246 CustomSlice(qreal value, QString label)
247 247 :QPieSlice(value, label)
248 248 {
249 249 connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter()));
250 250 connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave()));
251 251 }
252 252
253 253 public:
254 254 QBrush originalBrush()
255 255 {
256 256 return m_originalBrush;
257 257 }
258 258
259 259 public Q_SLOTS:
260 260
261 261 void handleHoverEnter()
262 262 {
263 263 QBrush brush = this->sliceBrush();
264 264 m_originalBrush = brush;
265 265 brush.setColor(brush.color().lighter());
266 266 setSliceBrush(brush);
267 267 }
268 268
269 269 void handleHoverLeave()
270 270 {
271 271 setSliceBrush(m_originalBrush);
272 272 }
273 273
274 274 private:
275 275 QBrush m_originalBrush;
276 276 };
277 277
278 278 class MainWidget : public QWidget
279 279 {
280 280 Q_OBJECT
281 281
282 282 public:
283 283 explicit MainWidget(QWidget* parent = 0)
284 284 :QWidget(parent),
285 285 m_slice(0)
286 286 {
287 287 // create chart
288 288 m_chartView = new QChartView();
289 289 m_chartView->setChartTitle("Piechart customization");
290 290 m_chartView->setAnimationOptions(QChart::AllAnimations);
291 291
292 292 // create series
293 293 m_series = new QPieSeries();
294 294 *m_series << new CustomSlice(10.0, "Slice 1");
295 295 *m_series << new CustomSlice(20.0, "Slice 2");
296 296 *m_series << new CustomSlice(30.0, "Slice 3");
297 297 *m_series << new CustomSlice(40.0, "Slice 4");
298 298 *m_series << new CustomSlice(50.0, "Slice 5");
299 299 m_chartView->addSeries(m_series);
300 300
301 301 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
302 302
303 303 // chart settings
304 304 m_themeComboBox = new QComboBox();
305 305 m_themeComboBox->addItem("Default", QChart::ChartThemeDefault);
306 306 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
307 307 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
308 308 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
309 309 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
310 310 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
311 m_themeComboBox->addItem("Vanilla", QChart::ChartThemeVanilla);
312 311 m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy);
313 m_themeComboBox->addItem("Grayscale", QChart::ChartThemeGrayscale);
314 312 m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific);
315 313
316 314 m_aaCheckBox = new QCheckBox();
317 315 m_animationsCheckBox = new QCheckBox();
318 316 m_animationsCheckBox->setCheckState(Qt::Checked);
319 317
320 318 QFormLayout* chartSettingsLayout = new QFormLayout();
321 319 chartSettingsLayout->addRow("Theme", m_themeComboBox);
322 320 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
323 321 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
324 322 QGroupBox* chartSettings = new QGroupBox("Chart");
325 323 chartSettings->setLayout(chartSettingsLayout);
326 324
327 325 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
328 326 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
329 327 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
330 328
331 329 // series settings
332 330 m_hPosition = new QDoubleSpinBox();
333 331 m_hPosition->setMinimum(0.0);
334 332 m_hPosition->setMaximum(1.0);
335 333 m_hPosition->setSingleStep(0.1);
336 334 m_hPosition->setValue(m_series->pieHorizontalPosition());
337 335
338 336 m_vPosition = new QDoubleSpinBox();
339 337 m_vPosition->setMinimum(0.0);
340 338 m_vPosition->setMaximum(1.0);
341 339 m_vPosition->setSingleStep(0.1);
342 340 m_vPosition->setValue(m_series->pieVerticalPosition());
343 341
344 342 m_sizeFactor = new QDoubleSpinBox();
345 343 m_sizeFactor->setMinimum(0.0);
346 344 m_sizeFactor->setMaximum(1.0);
347 345 m_sizeFactor->setSingleStep(0.1);
348 346 m_sizeFactor->setValue(m_series->pieSize());
349 347
350 348 m_startAngle = new QDoubleSpinBox();
351 349 m_startAngle->setMinimum(0.0);
352 350 m_startAngle->setMaximum(360);
353 351 m_startAngle->setValue(m_series->pieStartAngle());
354 352 m_startAngle->setSingleStep(1);
355 353
356 354 m_endAngle = new QDoubleSpinBox();
357 355 m_endAngle->setMinimum(0.0);
358 356 m_endAngle->setMaximum(360);
359 357 m_endAngle->setValue(m_series->pieEndAngle());
360 358 m_endAngle->setSingleStep(1);
361 359
362 360 QPushButton *addSlice = new QPushButton("Add slice");
363 361 QPushButton *insertSlice = new QPushButton("Insert slice");
364 362
365 363 QFormLayout* seriesSettingsLayout = new QFormLayout();
366 364 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
367 365 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
368 366 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
369 367 seriesSettingsLayout->addRow("Start angle", m_startAngle);
370 368 seriesSettingsLayout->addRow("End angle", m_endAngle);
371 369 seriesSettingsLayout->addRow(addSlice);
372 370 seriesSettingsLayout->addRow(insertSlice);
373 371 QGroupBox* seriesSettings = new QGroupBox("Series");
374 372 seriesSettings->setLayout(seriesSettingsLayout);
375 373
376 374 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
377 375 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
378 376 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
379 377 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
380 378 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
381 379 connect(addSlice, SIGNAL(clicked()), this, SLOT(addSlice()));
382 380 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
383 381
384 382 // slice settings
385 383 m_sliceName = new QLabel("<click a slice>");
386 384 m_sliceValue = new QDoubleSpinBox();
387 385 m_sliceValue->setMaximum(1000);
388 386 m_sliceLabelVisible = new QCheckBox();
389 387 m_sliceLabelArmFactor = new QDoubleSpinBox();
390 388 m_sliceLabelArmFactor->setSingleStep(0.01);
391 389 m_sliceExploded = new QCheckBox();
392 390 m_sliceExplodedFactor = new QDoubleSpinBox();
393 391 m_sliceExplodedFactor->setSingleStep(0.01);
394 392 m_pen = new QPushButton();
395 393 m_penTool = new PenTool("Slice pen", this);
396 394 m_brush = new QPushButton();
397 395 m_brushTool = new BrushTool("Slice brush", this);
398 396 m_font = new QPushButton();
399 397 m_labelArmPen = new QPushButton();
400 398 m_labelArmPenTool = new PenTool("Label arm pen", this);
401 399 QPushButton *removeSlice = new QPushButton("Remove slice");
402 400
403 401 QFormLayout* sliceSettingsLayout = new QFormLayout();
404 402 sliceSettingsLayout->addRow("Selected", m_sliceName);
405 403 sliceSettingsLayout->addRow("Value", m_sliceValue);
406 404 sliceSettingsLayout->addRow("Pen", m_pen);
407 405 sliceSettingsLayout->addRow("Brush", m_brush);
408 406 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
409 407 sliceSettingsLayout->addRow("Label font", m_font);
410 408 sliceSettingsLayout->addRow("Label arm pen", m_labelArmPen);
411 409 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
412 410 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
413 411 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
414 412 sliceSettingsLayout->addRow(removeSlice);
415 413 QGroupBox* sliceSettings = new QGroupBox("Slice");
416 414 sliceSettings->setLayout(sliceSettingsLayout);
417 415
418 416 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
419 417 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
420 418 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
421 419 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
422 420 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
423 421 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
424 422 connect(m_labelArmPen, SIGNAL(clicked()), m_labelArmPenTool, SLOT(show()));
425 423 connect(m_labelArmPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
426 424 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
427 425 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
428 426 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
429 427 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
430 428 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
431 429 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
432 430
433 431 // create main layout
434 432 QVBoxLayout *settingsLayout = new QVBoxLayout();
435 433 settingsLayout->addWidget(chartSettings);
436 434 settingsLayout->addWidget(seriesSettings);
437 435 settingsLayout->addWidget(sliceSettings);
438 436 settingsLayout->addStretch();
439 437
440 438 QGridLayout* baseLayout = new QGridLayout();
441 439 baseLayout->addLayout(settingsLayout, 0, 0);
442 440 baseLayout->addWidget(m_chartView, 0, 1);
443 441 setLayout(baseLayout);
444 442
445 443 updateSerieSettings();
446 444 }
447 445
448 446 public Q_SLOTS:
449 447
450 448 void updateChartSettings()
451 449 {
452 450 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
453 451 m_chartView->setChartTheme(theme);
454 452 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
455 453
456 454 if (m_animationsCheckBox->checkState() == Qt::Checked)
457 455 m_chartView->setAnimationOptions(QChart::AllAnimations);
458 456 else
459 457 m_chartView->setAnimationOptions(QChart::NoAnimation);
460 458 }
461 459
462 460 void updateSerieSettings()
463 461 {
464 462 m_series->setPiePosition(m_hPosition->value(), m_vPosition->value());
465 463 m_series->setPieSize(m_sizeFactor->value());
466 464 m_series->setPieStartAngle(m_startAngle->value());
467 465 m_series->setPieEndAngle(m_endAngle->value());
468 466 }
469 467
470 468 void updateSliceSettings()
471 469 {
472 470 if (!m_slice)
473 471 return;
474 472
475 473 m_slice->setValue(m_sliceValue->value());
476 474
477 475 m_slice->setSlicePen(m_penTool->pen());
478 476 m_slice->setSliceBrush(m_brushTool->brush());
479 477
480 478 m_slice->setLabelArmPen(m_labelArmPenTool->pen());
481 479 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
482 480 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
483 481
484 482 m_slice->setExploded(m_sliceExploded->isChecked());
485 483 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
486 484 }
487 485
488 486 void handleSliceClicked(QPieSlice* slice)
489 487 {
490 488 m_slice = static_cast<CustomSlice*>(slice);
491 489
492 490 // name
493 491 m_sliceName->setText(slice->label());
494 492
495 493 // value
496 494 m_sliceValue->blockSignals(true);
497 495 m_sliceValue->setValue(slice->value());
498 496 m_sliceValue->blockSignals(false);
499 497
500 498 // pen
501 499 m_pen->setText(PenTool::name(m_slice->slicePen()));
502 500 m_penTool->setPen(m_slice->slicePen());
503 501
504 502 // brush
505 503 m_brush->setText(m_slice->originalBrush().color().name());
506 504 m_brushTool->setBrush(m_slice->originalBrush());
507 505
508 506 // label
509 507 m_labelArmPen->setText(PenTool::name(m_slice->labelArmPen()));
510 508 m_labelArmPenTool->setPen(m_slice->labelArmPen());
511 509 m_font->setText(slice->labelFont().toString());
512 510 m_sliceLabelVisible->blockSignals(true);
513 511 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
514 512 m_sliceLabelVisible->blockSignals(false);
515 513 m_sliceLabelArmFactor->blockSignals(true);
516 514 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
517 515 m_sliceLabelArmFactor->blockSignals(false);
518 516
519 517 // exploded
520 518 m_sliceExploded->blockSignals(true);
521 519 m_sliceExploded->setChecked(slice->isExploded());
522 520 m_sliceExploded->blockSignals(false);
523 521 m_sliceExplodedFactor->blockSignals(true);
524 522 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
525 523 m_sliceExplodedFactor->blockSignals(false);
526 524 }
527 525
528 526 void showFontDialog()
529 527 {
530 528 if (!m_slice)
531 529 return;
532 530
533 531 QFontDialog dialog(m_slice->labelFont());
534 532 dialog.show();
535 533 dialog.exec();
536 534
537 535 m_slice->setLabelFont(dialog.currentFont());
538 536 m_font->setText(dialog.currentFont().toString());
539 537 }
540 538
541 539 void addSlice()
542 540 {
543 541 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
544 542 }
545 543
546 544 void insertSlice()
547 545 {
548 546 if (!m_slice)
549 547 return;
550 548
551 549 int i = m_series->slices().indexOf(m_slice);
552 550
553 551 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
554 552 }
555 553
556 554 void removeSlice()
557 555 {
558 556 if (!m_slice)
559 557 return;
560 558
561 559 m_series->remove(m_slice);
562 560 m_slice = 0;
563 561 }
564 562
565 563 private:
566 564 QComboBox *m_themeComboBox;
567 565 QCheckBox *m_aaCheckBox;
568 566 QCheckBox *m_animationsCheckBox;
569 567
570 568 QChartView* m_chartView;
571 569 QPieSeries* m_series;
572 570 CustomSlice* m_slice;
573 571
574 572 QDoubleSpinBox* m_hPosition;
575 573 QDoubleSpinBox* m_vPosition;
576 574 QDoubleSpinBox* m_sizeFactor;
577 575 QDoubleSpinBox* m_startAngle;
578 576 QDoubleSpinBox* m_endAngle;
579 577
580 578 QLabel* m_sliceName;
581 579 QDoubleSpinBox* m_sliceValue;
582 580 QCheckBox* m_sliceLabelVisible;
583 581 QDoubleSpinBox* m_sliceLabelArmFactor;
584 582 QCheckBox* m_sliceExploded;
585 583 QDoubleSpinBox* m_sliceExplodedFactor;
586 584 QPushButton *m_brush;
587 585 BrushTool *m_brushTool;
588 586 QPushButton *m_pen;
589 587 PenTool *m_penTool;
590 588 QPushButton *m_font;
591 589 QPushButton *m_labelArmPen;
592 590 PenTool *m_labelArmPenTool;
593 591 };
594 592
595 593 int main(int argc, char *argv[])
596 594 {
597 595 QApplication a(argc, argv);
598 596
599 597 QMainWindow window;
600 598
601 599 MainWidget* widget = new MainWidget();
602 600
603 601 window.setCentralWidget(widget);
604 602 window.resize(900, 600);
605 603 window.show();
606 604
607 605 return a.exec();
608 606 }
609 607
610 608 #include "main.moc"
@@ -1,114 +1,114
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qpieseries.h>
6 6 #include <qpieslice.h>
7 7 #include <QTime>
8 8
9 9 QTCOMMERCIALCHART_USE_NAMESPACE
10 10
11 11 class DrilldownSlice : public QPieSlice
12 12 {
13 13 Q_OBJECT
14 14
15 15 public:
16 16 DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries)
17 17 :m_drilldownSeries(drilldownSeries),
18 18 m_prefix(prefix)
19 19 {
20 20 setValue(value);
21 21 setLabelVisible(true);
22 22 updateLabel();
23 23 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
24 24 }
25 25
26 26 QSeries* drilldownSeries() const { return m_drilldownSeries; }
27 27
28 28 public Q_SLOTS:
29 29 void updateLabel()
30 30 {
31 31 QString label = m_prefix;
32 32 label += " " + QString::number(this->value())+ "e (";
33 33 label += QString::number(this->percentage()*100, 'f', 1) + "%)";
34 34 setLabel(label);
35 35 }
36 36
37 37 private:
38 38 QSeries* m_drilldownSeries;
39 39 QString m_prefix;
40 40 };
41 41
42 42 class DrilldownChart : public QChartView
43 43 {
44 44 Q_OBJECT
45 45 public:
46 46 explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {}
47 47
48 48 void changeSeries(QSeries* series)
49 49 {
50 50 // NOTE: if the series is owned by the chart it will be deleted
51 51 // here the "window" owns the series...
52 52 if (m_currentSeries)
53 53 removeSeries(m_currentSeries);
54 54 m_currentSeries = series;
55 55 addSeries(series);
56 56 setChartTitle(series->title());
57 57 }
58 58
59 59 public Q_SLOTS:
60 60 void handleSliceClicked(QPieSlice* slice)
61 61 {
62 62 DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice);
63 63 changeSeries(drilldownSlice->drilldownSeries());
64 64 }
65 65
66 66 private:
67 67 QSeries* m_currentSeries;
68 68 };
69 69
70 70 int main(int argc, char *argv[])
71 71 {
72 72 QApplication a(argc, argv);
73 73
74 74 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
75 75
76 76 QMainWindow window;
77 77
78 78 DrilldownChart* drilldownChart = new DrilldownChart(&window);
79 79 drilldownChart->setRenderHint(QPainter::Antialiasing);
80 drilldownChart->setChartTheme(QChart::ChartThemeVanilla);
80 drilldownChart->setChartTheme(QChart::ChartThemeLight);
81 81 drilldownChart->setAnimationOptions(QChart::AllAnimations);
82 82
83 83 QPieSeries* yearSeries = new QPieSeries(&window);
84 84 yearSeries->setTitle("Sales by year - All");
85 85
86 86 QList<QString> months;
87 87 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
88 88 QList<QString> names;
89 89 names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob";
90 90
91 91 foreach (QString name, names) {
92 92 QPieSeries* series = new QPieSeries(&window);
93 93 series->setTitle("Sales by month - " + name);
94 94
95 95 foreach (QString month, months)
96 96 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
97 97
98 98 QObject::connect(series, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
99 99
100 100 *yearSeries << new DrilldownSlice(series->total(), name, series);
101 101 }
102 102
103 103 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*)));
104 104
105 105 drilldownChart->changeSeries(yearSeries);
106 106
107 107 window.setCentralWidget(drilldownChart);
108 108 window.resize(800, 600);
109 109 window.show();
110 110
111 111 return a.exec();
112 112 }
113 113
114 114 #include "main.moc"
@@ -1,49 +1,47
1 1 #ifndef DECLARATIVECHART_H
2 2 #define DECLARATIVECHART_H
3 3
4 4 #include <QtCore/QtGlobal>
5 5 #include <QDeclarativeItem>
6 6 #include <qchart.h>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class DeclarativeChart : public QDeclarativeItem
11 11 // TODO: for QTQUICK2: extend QQuickPainterItem instead
12 12 //class DeclarativeChart : public QQuickPaintedItem, public Chart
13 13 {
14 14 Q_OBJECT
15 15 Q_ENUMS(ChartTheme)
16 16 Q_PROPERTY(ChartTheme theme READ theme WRITE setTheme)
17 17
18 18 public:
19 19 enum ChartTheme {
20 20 ThemeDefault,
21 21 ThemeLight,
22 22 ThemeBlueCerulean,
23 23 ThemeDark,
24 24 ThemeBrownSand,
25 25 ThemeBlueNcs,
26 ThemeVanilla,
27 26 ThemeIcy,
28 ThemeGrayscale,
29 27 ThemeScientific
30 28 };
31 29 DeclarativeChart(QDeclarativeItem *parent = 0);
32 30
33 31 public: // From QDeclarativeItem/QGraphicsItem
34 32 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
35 33 void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
36 34
37 35 public:
38 36 void setTheme(ChartTheme theme) {m_chart->setChartTheme((QChart::ChartTheme) theme);}
39 37 ChartTheme theme();
40 38
41 39 public:
42 40 // Extending QChart with DeclarativeChart is not possible because QObject does not support
43 41 // multi inheritance, so we now have a QChart as a member instead
44 42 QChart *m_chart;
45 43 };
46 44
47 45 QTCOMMERCIALCHART_END_NAMESPACE
48 46
49 47 #endif // DECLARATIVECHART_H
@@ -1,375 +1,369
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 3 #include "qchartview.h"
4 4 #include "qlegend.h"
5 5 #include "qchartaxis.h"
6 6 #include <QTime>
7 7
8 8 //series
9 9 #include "qbarset.h"
10 10 #include "qbarseries.h"
11 11 #include "qstackedbarseries.h"
12 12 #include "qpercentbarseries.h"
13 13 #include "qlineseries.h"
14 14 #include "qareaseries.h"
15 15 #include "qscatterseries.h"
16 16 #include "qpieseries.h"
17 17 #include "qpieslice.h"
18 18 #include "qsplineseries.h"
19 19
20 20 //items
21 21 #include "axisitem_p.h"
22 22 #include "barchartitem_p.h"
23 23 #include "stackedbarchartitem_p.h"
24 24 #include "percentbarchartitem_p.h"
25 25 #include "linechartitem_p.h"
26 26 #include "areachartitem_p.h"
27 27 #include "scatterchartitem_p.h"
28 28 #include "piechartitem_p.h"
29 29 #include "splinechartitem_p.h"
30 30
31 31 //themes
32 32 #include "chartthemedefault_p.h"
33 33 #include "chartthemelight_p.h"
34 34 #include "chartthemebluecerulean_p.h"
35 35 #include "chartthemedark_p.h"
36 36 #include "chartthemebrownsand_p.h"
37 37 #include "chartthemebluencs_p.h"
38 #include "chartthemevanilla_p.h"
39 38 #include "chartthemeicy_p.h"
40 #include "chartthemegrayscale_p.h"
41 39 #include "chartthemescientific_p.h"
42 40
43 41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
44 42
45 43 ChartTheme::ChartTheme(QChart::ChartTheme id) :
46 44 m_masterFont(QFont()),
47 45 m_titleBrush(QColor(QRgb(0x000000))),
48 46 m_axisLinePen(QPen(QRgb(0x000000))),
49 47 m_axisLabelBrush(QColor(QRgb(0x000000))),
50 48 m_backgroundShadesPen(Qt::NoPen),
51 49 m_backgroundShadesBrush(Qt::NoBrush),
52 50 m_backgroundShades(BackgroundShadesNone),
53 51 m_gridLinePen(QPen(QRgb(0x000000)))
54 52 {
55 53 m_id = id;
56 54 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
57 55 }
58 56
59 57
60 58 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
61 59 {
62 60 switch(theme) {
63 61 case QChart::ChartThemeLight:
64 62 return new ChartThemeLight();
65 63 case QChart::ChartThemeBlueCerulean:
66 64 return new ChartThemeBlueCerulean();
67 65 case QChart::ChartThemeDark:
68 66 return new ChartThemeDark();
69 67 case QChart::ChartThemeBrownSand:
70 68 return new ChartThemeBrownSand();
71 69 case QChart::ChartThemeBlueNcs:
72 70 return new ChartThemeBlueNcs();
73 case QChart::ChartThemeVanilla:
74 return new ChartThemeVanilla();
75 71 case QChart::ChartThemeIcy:
76 72 return new ChartThemeIcy();
77 case QChart::ChartThemeGrayscale:
78 return new ChartThemeGrayscale();
79 73 case QChart::ChartThemeScientific:
80 74 return new ChartThemeScientific();
81 75 default:
82 76 return new ChartThemeDefault();
83 77 }
84 78 }
85 79
86 80 void ChartTheme::decorate(QChart* chart,bool force)
87 81 {
88 82 QPen pen;
89 83 QBrush brush;
90 84
91 85 if(brush == chart->backgroundBrush() || force)
92 86 {
93 87 if (m_backgroundShades == BackgroundShadesNone) {
94 88 chart->setBackgroundBrush(m_chartBackgroundGradient);
95 89 }
96 90 else {
97 91 chart->setBackgroundBrush(Qt::NoBrush);
98 92 }
99 93 }
100 94 chart->setTitleFont(m_masterFont);
101 95 chart->setTitleBrush(m_titleBrush);
102 96 }
103 97
104 98 void ChartTheme::decorate(QLegend* legend,bool force)
105 99 {
106 100 QPen pen;
107 101 QBrush brush;
108 102
109 103 if (pen == legend->pen() || force){
110 104 //TODO:: legend->setPen();
111 105 }
112 106
113 107
114 108 if (brush == legend->brush() || force) {
115 109 legend->setBrush(m_chartBackgroundGradient);
116 110 }
117 111 }
118 112
119 113 void ChartTheme::decorate(QAreaSeries* series, int index,bool force)
120 114 {
121 115 QPen pen;
122 116 QBrush brush;
123 117
124 118 if (pen == series->pen() || force){
125 119 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
126 120 pen.setWidthF(2);
127 121 series->setPen(pen);
128 122 }
129 123
130 124 if (brush == series->brush() || force) {
131 125 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
132 126 series->setBrush(brush);
133 127 }
134 128 }
135 129
136 130
137 131 void ChartTheme::decorate(QLineSeries* series,int index,bool force)
138 132 {
139 133 QPen pen;
140 134 if(pen == series->pen() || force ){
141 135 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
142 136 pen.setWidthF(2);
143 137 series->setPen(pen);
144 138 }
145 139 }
146 140
147 141 void ChartTheme::decorate(QBarSeries* series, int index, bool force)
148 142 {
149 143 QBrush brush;
150 144 QPen pen;
151 145 QList<QBarSet*> sets = series->barSets();
152 146
153 147 qreal takeAtPos = 0.5;
154 148 qreal step = 0.2;
155 149 if (sets.count() > 1 ) {
156 150 step = 1.0 / (qreal) sets.count();
157 151 if (sets.count() % m_seriesGradients.count())
158 152 step *= m_seriesGradients.count();
159 153 else
160 154 step *= (m_seriesGradients.count() - 1);
161 155 }
162 156
163 157 for (int i(0); i < sets.count(); i++) {
164 158 int colorIndex = (index + i) % m_seriesGradients.count();
165 159 if (i > 0 && i % m_seriesGradients.count() == 0) {
166 160 // There is no dedicated base color for each sets, generate more colors
167 161 takeAtPos += step;
168 162 if (takeAtPos == 1.0)
169 163 takeAtPos += step;
170 164 takeAtPos -= (int) takeAtPos;
171 165 }
172 166 qDebug() << "pos:" << takeAtPos;
173 167 if (brush == sets.at(i)->brush() || force )
174 168 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
175 169
176 170 // Pick label color from the opposite end of the gradient.
177 171 // 0.3 as a boundary seems to work well.
178 172 if (takeAtPos < 0.3)
179 173 sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
180 174 else
181 175 sets.at(i)->setFloatingValuePen(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
182 176
183 177 if (pen == sets.at(i)->pen() || force) {
184 178 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
185 179 sets.at(i)->setPen(c);
186 180 }
187 181 }
188 182 }
189 183
190 184 void ChartTheme::decorate(QScatterSeries* series, int index,bool force)
191 185 {
192 186 QPen pen;
193 187 QBrush brush;
194 188
195 189 if (pen == series->pen() || force) {
196 190 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
197 191 pen.setWidthF(2);
198 192 series->setPen(pen);
199 193 }
200 194
201 195 if (brush == series->brush() || force) {
202 196 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
203 197 series->setBrush(brush);
204 198 }
205 199 }
206 200
207 201 void ChartTheme::decorate(QPieSeries* series, int index, bool force)
208 202 {
209 203 for (int i(0); i < series->slices().count(); i++) {
210 204
211 205 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
212 206
213 207 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
214 208 qreal pos = (qreal) (i + 1) / (qreal) series->count();
215 209 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
216 210
217 211 QPieSlice::DataPtr s = series->slices().at(i)->data_ptr();
218 212 PieSliceData data = s->m_data;
219 213
220 214 if (data.m_slicePen.isThemed() || force) {
221 215 data.m_slicePen = penColor;
222 216 data.m_slicePen.setThemed(true);
223 217 }
224 218
225 219 if (data.m_sliceBrush.isThemed() || force) {
226 220 data.m_sliceBrush = brushColor;
227 221 data.m_sliceBrush.setThemed(true);
228 222 }
229 223
230 224 if (s->m_data != data) {
231 225 s->m_data = data;
232 226 emit s->changed();
233 227 }
234 228 }
235 229 }
236 230
237 231 void ChartTheme::decorate(QSplineSeries* series, int index, bool force)
238 232 {
239 233 QPen pen;
240 234 if(pen == series->pen() || force){
241 235 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
242 236 pen.setWidthF(2);
243 237 series->setPen(pen);
244 238 }
245 239 }
246 240
247 241 void ChartTheme::decorate(QChartAxis* axis,bool axisX, bool force)
248 242 {
249 243 QPen pen;
250 244 QBrush brush;
251 245 QFont font;
252 246
253 247 if (axis->isAxisVisible()) {
254 248
255 249 if(brush == axis->labelsBrush() || force){
256 250 axis->setLabelsBrush(m_axisLabelBrush);
257 251 }
258 252 if(pen == axis->labelsPen() || force){
259 253 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
260 254 }
261 255
262 256
263 257 if (axis->shadesVisible() || force) {
264 258
265 259 if(brush == axis->shadesBrush() || force){
266 260 axis->setShadesBrush(m_backgroundShadesBrush);
267 261 }
268 262
269 263 if(pen == axis->shadesPen() || force){
270 264 axis->setShadesPen(m_backgroundShadesPen);
271 265 }
272 266
273 267 if(force && (m_backgroundShades == BackgroundShadesBoth
274 268 || (m_backgroundShades == BackgroundShadesVertical && axisX)
275 269 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
276 270 axis->setShadesVisible(true);
277 271
278 272 }
279 273 }
280 274
281 275 if(pen == axis->axisPen() || force){
282 276 axis->setAxisPen(m_axisLinePen);
283 277 }
284 278
285 279 if(pen == axis->gridLinePen() || force){
286 280 axis->setGridLinePen(m_gridLinePen);
287 281 }
288 282
289 283 if(font == axis->labelsFont() || force){
290 284 axis->setLabelsFont(m_masterFont);
291 285 }
292 286 }
293 287 }
294 288
295 289 void ChartTheme::generateSeriesGradients()
296 290 {
297 291 // Generate gradients in HSV color space
298 292 foreach (QColor color, m_seriesColors) {
299 293 QLinearGradient g;
300 294 qreal h = color.hsvHueF();
301 295 qreal s = color.hsvSaturationF();
302 296
303 297 // TODO: tune the algorithm to give nice results with most base colors defined in
304 298 // most themes. The rest of the gradients we can define manually in theme specific
305 299 // implementation.
306 300 QColor start = color;
307 301 start.setHsvF(h, 0.0, 1.0);
308 302 g.setColorAt(0.0, start);
309 303
310 304 g.setColorAt(0.5, color);
311 305
312 306 QColor end = color;
313 307 end.setHsvF(h, s, 0.25);
314 308 g.setColorAt(1.0, end);
315 309
316 310 m_seriesGradients << g;
317 311 }
318 312 }
319 313
320 314
321 315 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
322 316 {
323 317 Q_ASSERT(pos >=0.0 && pos <= 1.0);
324 318 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
325 319 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
326 320 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
327 321 QColor c;
328 322 c.setRgbF(r, g, b);
329 323 return c;
330 324 }
331 325
332 326 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
333 327 {
334 328 Q_ASSERT(pos >=0 && pos <= 1.0);
335 329
336 330 // another possibility:
337 331 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
338 332
339 333 QGradientStops stops = gradient.stops();
340 334 int count = stops.count();
341 335
342 336 // find previous stop relative to position
343 337 QGradientStop prev = stops.first();
344 338 for (int i=0; i<count; i++) {
345 339 QGradientStop stop = stops.at(i);
346 340 if (pos > stop.first)
347 341 prev = stop;
348 342
349 343 // given position is actually a stop position?
350 344 if (pos == stop.first) {
351 345 //qDebug() << "stop color" << pos;
352 346 return stop.second;
353 347 }
354 348 }
355 349
356 350 // find next stop relative to position
357 351 QGradientStop next = stops.last();
358 352 for (int i=count-1; i>=0; i--) {
359 353 QGradientStop stop = stops.at(i);
360 354 if (pos < stop.first)
361 355 next = stop;
362 356 }
363 357
364 358 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
365 359
366 360 qreal range = next.first - prev.first;
367 361 qreal posDelta = pos - prev.first;
368 362 qreal relativePos = posDelta / range;
369 363
370 364 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
371 365
372 366 return colorAt(prev.second, next.second, relativePos);
373 367 }
374 368
375 369 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,391 +1,392
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "qlegend.h"
4 4 #include "chartpresenter_p.h"
5 5 #include "chartdataset_p.h"
6 6 #include "chartbackground_p.h"
7 7 #include <QGraphicsScene>
8 8 #include <QGraphicsSceneResizeEvent>
9 9 #include <QDebug>
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 /*!
14 14 \enum QChart::ChartTheme
15 15
16 16 This enum describes the theme used by the chart.
17 17
18 18 \value ChartThemeDefault Follows the GUI style of the Operating System
19 \value ChartThemeVanilla
19 \value ChartThemeLight
20 \value ChartThemeBlueCerulean
21 \value ChartThemeDark
22 \value ChartThemeBrownSand
23 \value ChartThemeBlueNcs
20 24 \value ChartThemeIcy
21 \value ChartThemeGrayscale
22 25 \value ChartThemeScientific
23 \value ChartThemeBlueCerulean
24 \value ChartThemeLight
25 26 \value ChartThemeCount Not really a theme; the total count of themes.
26 27 */
27 28
28 29 /*!
29 30 \enum QChart::AnimationOption
30 31
31 32 For enabling/disabling animations. Defaults to NoAnimation.
32 33
33 34 \value NoAnimation
34 35 \value GridAxisAnimations
35 36 \value SeriesAnimations
36 37 \value AllAnimations
37 38 */
38 39
39 40 /*!
40 41 \class QChart
41 42 \brief QtCommercial chart API.
42 43
43 44 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
44 45 representation of different types of QChartSeries and other chart related objects like
45 46 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
46 47 convenience class QChartView instead of QChart.
47 48 \sa QChartView
48 49 */
49 50
50 51 /*!
51 52 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
52 53 */
53 54 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
54 55 m_backgroundItem(0),
55 56 m_titleItem(0),
56 57 m_legend(new QLegend(this)),
57 58 m_dataset(new ChartDataSet(this)),
58 59 m_presenter(new ChartPresenter(this,m_dataset)),
59 60 m_padding(50),
60 61 m_backgroundPadding(10)
61 62 {
62 63 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
63 64 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
64 65 }
65 66
66 67 /*!
67 68 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
68 69 */
69 70 QChart::~QChart()
70 71 {
71 72 //delete first presenter , since this is a root of all the graphical items
72 73 delete m_presenter;
73 74 m_presenter=0;
74 75 }
75 76
76 77 /*!
77 78 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
78 79 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
79 80 the y axis).
80 81 */
81 82 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
82 83 {
83 84 m_dataset->addSeries(series, axisY);
84 85 }
85 86
86 87 /*!
87 88 Removes the \a series specified in a perameter from the QChartView.
88 89 It releses its ownership of the specified QChartSeries object.
89 90 It does not delete the pointed QChartSeries data object
90 91 \sa addSeries(), removeAllSeries()
91 92 */
92 93 void QChart::removeSeries(QSeries* series)
93 94 {
94 95 m_dataset->removeSeries(series);
95 96 }
96 97
97 98 /*!
98 99 Removes all the QChartSeries that have been added to the QChartView
99 100 It also deletes the pointed QChartSeries data objects
100 101 \sa addSeries(), removeSeries()
101 102 */
102 103 void QChart::removeAllSeries()
103 104 {
104 105 m_dataset->removeAllSeries();
105 106 }
106 107
107 108 /*!
108 109 Sets the \a brush that is used for painting the background of the chart area.
109 110 */
110 111 void QChart::setBackgroundBrush(const QBrush& brush)
111 112 {
112 113 createChartBackgroundItem();
113 114 m_backgroundItem->setBrush(brush);
114 115 m_backgroundItem->update();
115 116 }
116 117
117 118 QBrush QChart::backgroundBrush() const
118 119 {
119 120 if(!m_backgroundItem) return QBrush();
120 121 return m_backgroundItem->brush();
121 122 }
122 123
123 124 /*!
124 125 Sets the \a pen that is used for painting the background of the chart area.
125 126 */
126 127 void QChart::setBackgroundPen(const QPen& pen)
127 128 {
128 129 createChartBackgroundItem();
129 130 m_backgroundItem->setPen(pen);
130 131 m_backgroundItem->update();
131 132 }
132 133
133 134 QPen QChart::backgroundPen() const
134 135 {
135 136 if(!m_backgroundItem) return QPen();
136 137 return m_backgroundItem->pen();
137 138 }
138 139
139 140 /*!
140 141 Sets the chart \a title. The description text that is drawn above the chart.
141 142 */
142 143 void QChart::setTitle(const QString& title)
143 144 {
144 145 createChartTitleItem();
145 146 m_titleItem->setText(title);
146 147 updateLayout();
147 148 }
148 149
149 150 /*!
150 151 Returns the chart title. The description text that is drawn above the chart.
151 152 */
152 153 QString QChart::title() const
153 154 {
154 155 if(m_titleItem)
155 156 return m_titleItem->text();
156 157 else
157 158 return QString();
158 159 }
159 160
160 161 /*!
161 162 Sets the \a font that is used for rendering the description text that is rendered above the chart.
162 163 */
163 164 void QChart::setTitleFont(const QFont& font)
164 165 {
165 166 createChartTitleItem();
166 167 m_titleItem->setFont(font);
167 168 updateLayout();
168 169 }
169 170
170 171 /*!
171 172 Sets the \a brush used for rendering the title text.
172 173 */
173 174 void QChart::setTitleBrush(const QBrush &brush)
174 175 {
175 176 createChartTitleItem();
176 177 m_titleItem->setBrush(brush);
177 178 updateLayout();
178 179 }
179 180
180 181 /*!
181 182 Returns the brush used for rendering the title text.
182 183 */
183 184 QBrush QChart::titleBrush() const
184 185 {
185 186 if(!m_titleItem) return QBrush();
186 187 return m_titleItem->brush();
187 188 }
188 189
189 190 void QChart::createChartBackgroundItem()
190 191 {
191 192 if(!m_backgroundItem) {
192 193 m_backgroundItem = new ChartBackground(this);
193 194 m_backgroundItem->setPen(Qt::NoPen);
194 195 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
195 196 }
196 197 }
197 198
198 199 void QChart::createChartTitleItem()
199 200 {
200 201 if(!m_titleItem) {
201 202 m_titleItem = new QGraphicsSimpleTextItem(this);
202 203 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
203 204 }
204 205 }
205 206
206 207 /*!
207 208 Sets the \a theme used by the chart for rendering the graphical representation of the data
208 209 \sa ChartTheme, chartTheme()
209 210 */
210 211 void QChart::setChartTheme(QChart::ChartTheme theme)
211 212 {
212 213 m_presenter->setChartTheme(theme);
213 214 }
214 215
215 216 /*!
216 217 Returns the theme enum used by the chart.
217 218 \sa ChartTheme, setChartTheme()
218 219 */
219 220 QChart::ChartTheme QChart::chartTheme() const
220 221 {
221 222 return m_presenter->chartTheme();
222 223 }
223 224
224 225 /*!
225 226 Zooms in the view by a factor of 2
226 227 */
227 228 void QChart::zoomIn()
228 229 {
229 230 m_presenter->zoomIn();
230 231 }
231 232
232 233 /*!
233 234 Zooms in the view to a maximum level at which \a rect is still fully visible.
234 235 */
235 236 void QChart::zoomIn(const QRectF& rect)
236 237 {
237 238
238 239 if(!rect.isValid()) return;
239 240 m_presenter->zoomIn(rect);
240 241 }
241 242
242 243 /*!
243 244 Restores the view zoom level to the previous one.
244 245 */
245 246 void QChart::zoomOut()
246 247 {
247 248 m_presenter->zoomOut();
248 249 }
249 250
250 251 /*!
251 252 Returns the pointer to the x axis object of the chart
252 253 */
253 254 QChartAxis* QChart::axisX() const
254 255 {
255 256 return m_dataset->axisX();
256 257 }
257 258
258 259 /*!
259 260 Returns the pointer to the y axis object of the chart
260 261 */
261 262 QChartAxis* QChart::axisY() const
262 263 {
263 264 return m_dataset->axisY();
264 265 }
265 266
266 267 /*!
267 268 Returns the legend object of the chart. Ownership stays in chart.
268 269 */
269 270 QLegend* QChart::legend() const
270 271 {
271 272 return m_legend;
272 273 }
273 274
274 275 /*!
275 276 Resizes and updates the chart area using the \a event data
276 277 */
277 278 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
278 279 {
279 280
280 281 m_rect = QRectF(QPoint(0,0),event->newSize());
281 282 updateLayout();
282 283 QGraphicsWidget::resizeEvent(event);
283 284 update();
284 285 }
285 286
286 287 /*!
287 288 Sets animation \a options for the chart
288 289 */
289 290 void QChart::setAnimationOptions(AnimationOptions options)
290 291 {
291 292 m_presenter->setAnimationOptions(options);
292 293 }
293 294
294 295 /*!
295 296 Returns animation options for the chart
296 297 */
297 298 QChart::AnimationOptions QChart::animationOptions() const
298 299 {
299 300 return m_presenter->animationOptions();
300 301 }
301 302
302 303 void QChart::scrollLeft()
303 304 {
304 305 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
305 306 }
306 307
307 308 void QChart::scrollRight()
308 309 {
309 310 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
310 311 }
311 312 void QChart::scrollUp()
312 313 {
313 314 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
314 315 }
315 316 void QChart::scrollDown()
316 317 {
317 318 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
318 319 }
319 320
320 321 void QChart::updateLayout()
321 322 {
322 323 if(!m_rect.isValid()) return;
323 324
324 325 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
325 326
326 327 // recalculate title position
327 328 if (m_titleItem) {
328 329 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
329 330 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
330 331 }
331 332
332 333 //recalculate background gradient
333 334 if (m_backgroundItem) {
334 335 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
335 336 }
336 337
337 338 // recalculate legend position
338 339 if (m_legend) {
339 340 if (m_legend->parentObject() == this) {
340 341 m_legend->setMaximumSize(rect.size());
341 342 m_legend->setPos(rect.topLeft());
342 343 }
343 344 }
344 345 }
345 346
346 347
347 348 int QChart::padding() const
348 349 {
349 350 return m_padding;
350 351 }
351 352
352 353 void QChart::setPadding(int padding)
353 354 {
354 355 if(m_padding==padding){
355 356 m_padding = padding;
356 357 m_presenter->handleGeometryChanged();
357 358 updateLayout();
358 359 }
359 360 }
360 361
361 362 void QChart::setBackgroundPadding(int padding)
362 363 {
363 364 if(m_backgroundPadding!=padding){
364 365 m_backgroundPadding = padding;
365 366 updateLayout();
366 367 }
367 368 }
368 369
369 370 void QChart::setBackgroundDiameter(int diameter)
370 371 {
371 372 createChartBackgroundItem();
372 373 m_backgroundItem->setDimeter(diameter);
373 374 m_backgroundItem->update();
374 375 }
375 376
376 377 void QChart::setBackgroundVisible(bool visible)
377 378 {
378 379 createChartBackgroundItem();
379 380 m_backgroundItem->setVisible(visible);
380 381 }
381 382
382 383 bool QChart::isBackgroundVisible() const
383 384 {
384 385 if(!m_backgroundItem) return false;
385 386 return m_backgroundItem->isVisible();
386 387 }
387 388
388 389
389 390 #include "moc_qchart.cpp"
390 391
391 392 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,123 +1,121
1 1 #ifndef QCHART_H
2 2 #define QCHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qseries.h>
6 6 #include <QGraphicsWidget>
7 7 #include <QLinearGradient>
8 8 #include <QFont>
9 9
10 10 class QGraphicsSceneResizeEvent;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class Axis;
15 15 class QSeries;
16 16 class PlotDomain;
17 17 class BarChartItem;
18 18 class QChartAxis;
19 19 class ChartTheme;
20 20 class ChartItem;
21 21 class ChartDataSet;
22 22 class ChartPresenter;
23 23 class QLegend;
24 24 class ChartBackground;
25 25
26 26
27 27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
28 28 {
29 29 Q_OBJECT
30 30 public:
31 31 enum ChartTheme {
32 32 ChartThemeDefault,
33 33 ChartThemeLight,
34 34 ChartThemeBlueCerulean,
35 35 ChartThemeDark,
36 36 ChartThemeBrownSand,
37 37 ChartThemeBlueNcs,
38 ChartThemeVanilla,
39 38 ChartThemeIcy,
40 ChartThemeGrayscale,
41 39 ChartThemeScientific,
42 40 ChartThemeCount
43 41 };
44 42
45 43 enum AnimationOption {
46 44 NoAnimation = 0x0,
47 45 GridAxisAnimations = 0x1,
48 46 SeriesAnimations =0x2,
49 47 AllAnimations = 0x3
50 48 };
51 49 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
52 50
53 51 public:
54 52 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
55 53 ~QChart();
56 54
57 55 void addSeries(QSeries* series, QChartAxis* axisY = 0);
58 56 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
59 57 void removeAllSeries(); // deletes series and axis
60 58
61 59 void setChartTheme(QChart::ChartTheme theme);
62 60 QChart::ChartTheme chartTheme() const;
63 61
64 62 void setTitle(const QString& title);
65 63 QString title() const;
66 64 void setTitleFont(const QFont& font);
67 65 QFont titleFont() const;
68 66 void setTitleBrush(const QBrush &brush);
69 67 QBrush titleBrush() const;
70 68 void setBackgroundBrush(const QBrush& brush);
71 69 QBrush backgroundBrush() const;
72 70 void setBackgroundPen(const QPen& pen);
73 71 QPen backgroundPen() const;
74 72
75 73 void setBackgroundVisible(bool visible);
76 74 bool isBackgroundVisible() const;
77 75
78 76 void setAnimationOptions(AnimationOptions options);
79 77 AnimationOptions animationOptions() const;
80 78
81 79 void zoomIn();
82 80 void zoomIn(const QRectF& rect);
83 81 void zoomOut();
84 82 void scrollLeft();
85 83 void scrollRight();
86 84 void scrollUp();
87 85 void scrollDown();
88 86
89 87 QChartAxis* axisX() const;
90 88 QChartAxis* axisY() const;
91 89
92 90 QLegend* legend() const;
93 91
94 92 int padding() const;
95 93
96 94 protected:
97 95 void resizeEvent(QGraphicsSceneResizeEvent *event);
98 96
99 97 private:
100 98 inline void createChartBackgroundItem();
101 99 inline void createChartTitleItem();
102 100 void setPadding(int padding);
103 101 void setBackgroundPadding(int padding);
104 102 void setBackgroundDiameter(int diameter);
105 103 void updateLayout();
106 104
107 105 private:
108 106 Q_DISABLE_COPY(QChart)
109 107 ChartBackground* m_backgroundItem;
110 108 QGraphicsSimpleTextItem* m_titleItem;
111 109 QRectF m_rect;
112 110 QLegend* m_legend;
113 111 ChartDataSet *m_dataset;
114 112 ChartPresenter *m_presenter;
115 113 int m_padding;
116 114 int m_backgroundPadding;
117 115 };
118 116
119 117 QTCOMMERCIALCHART_END_NAMESPACE
120 118
121 119 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
122 120
123 121 #endif
@@ -1,149 +1,147
1 1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
2 2 TARGET = QtCommercialChart
3 3 DESTDIR = $$CHART_BUILD_LIB_DIR
4 4 TEMPLATE = lib
5 5 QT += core \
6 6 gui
7 7 win32-msvc*: LIBS += User32.lib
8 8 CONFIG += debug_and_release
9 9 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
10 10 SOURCES += \
11 11 chartdataset.cpp \
12 12 chartpresenter.cpp \
13 13 charttheme.cpp \
14 14 domain.cpp \
15 15 qchart.cpp \
16 16 qchartview.cpp \
17 17 qseries.cpp \
18 18 qlegend.cpp \
19 19 legendmarker.cpp \
20 20 chartbackground.cpp \
21 21 chart.cpp
22 22 PRIVATE_HEADERS += \
23 23 chartdataset_p.h \
24 24 chartitem_p.h \
25 25 chartpresenter_p.h \
26 26 charttheme_p.h \
27 27 domain_p.h \
28 28 legendmarker_p.h \
29 29 chartbackground_p.h \
30 30 chart_p.h
31 31 PUBLIC_HEADERS += \
32 32 qchart.h \
33 33 qchartglobal.h \
34 34 qseries.h \
35 35 qchartview.h \
36 36 qlegend.h
37 37
38 38 include(animations/animations.pri)
39 39 include(axis/axis.pri)
40 40 include(xychart/xychart.pri)
41 41 include(linechart/linechart.pri)
42 42 include(areachart/areachart.pri)
43 43 include(barchart/barchart.pri)
44 44 include(piechart/piechart.pri)
45 45 include(scatterseries/scatter.pri)
46 46 include(splinechart/splinechart.pri)
47 47
48 48 THEMES += themes/chartthemedefault_p.h \
49 49 themes/chartthemelight_p.h \
50 50 themes/chartthemebluecerulean_p.h \
51 51 themes/chartthemedark_p.h \
52 52 themes/chartthemebrownsand_p.h \
53 53 themes/chartthemebluencs_p.h \
54 54 themes/chartthemeicy_p.h \
55 themes/chartthemegrayscale_p.h \
56 themes/chartthemescientific_p.h \
57 themes/chartthemevanilla_p.h
55 themes/chartthemescientific_p.h
58 56
59 57 HEADERS += $$PUBLIC_HEADERS
60 58 HEADERS += $$PRIVATE_HEADERS
61 59 HEADERS += $$THEMES
62 60 INCLUDEPATH += linechart \
63 61 barchart \
64 62 themes \
65 63 .
66 64 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
67 65 MOC_DIR = $$CHART_BUILD_DIR/lib
68 66 UI_DIR = $$CHART_BUILD_DIR/lib
69 67 RCC_DIR = $$CHART_BUILD_DIR/lib
70 68 DEFINES += QTCOMMERCIALCHART_LIBRARY
71 69
72 70 #qt public headers
73 71 #this is very primitive and lame parser , TODO: make perl script insted
74 72 !exists($$CHART_BUILD_PUBLIC_HEADER_DIR)
75 73 {
76 74 system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR)
77 75 }
78 76
79 77 for(file, PUBLIC_HEADERS) {
80 78 name = $$split(file,'/')
81 79 name = $$last(name)
82 80 class = "$$cat($$file)"
83 81 class = $$find(class,class)
84 82 !isEmpty(class){
85 83 class = $$split(class,QTCOMMERCIALCHART_EXPORT)
86 84 class = $$member(class,1)
87 85 class = $$split(class,' ')
88 86 class = $$replace(class,' ','')
89 87 class = $$member(class,0)
90 88 win32:{
91 89 command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
92 90 }else{
93 91 command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
94 92 }
95 93 PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class
96 94 system($$command)
97 95 }
98 96 }
99 97
100 98 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
101 99 public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS
102 100
103 101 target.path = $$[QT_INSTALL_LIBS]
104 102 INSTALLS += target public_headers
105 103
106 104 install_build_public_headers.name = build_public_headers
107 105 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
108 106 install_build_public_headers.input = PUBLIC_HEADERS
109 107 install_build_public_headers.commands = $$QMAKE_COPY \
110 108 ${QMAKE_FILE_NAME} \
111 109 $$CHART_BUILD_PUBLIC_HEADER_DIR
112 110 install_build_public_headers.CONFIG += target_predeps \
113 111 no_link
114 112
115 113 install_build_private_headers.name = buld_private_headers
116 114 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
117 115 install_build_private_headers.input = PRIVATE_HEADERS
118 116 install_build_private_headers.commands = $$QMAKE_COPY \
119 117 ${QMAKE_FILE_NAME} \
120 118 $$CHART_BUILD_PRIVATE_HEADER_DIR
121 119 install_build_private_headers.CONFIG += target_predeps \
122 120 no_link
123 121
124 122 QMAKE_EXTRA_COMPILERS += install_build_public_headers \
125 123 install_build_private_headers \
126 124
127 125 chartversion.target = qchartversion_p.h
128 126 chartversion.commands = @echo \
129 127 "build_time" \
130 128 > \
131 129 $$chartversion.target;
132 130 chartversion.depends = $$HEADERS \
133 131 $$SOURCES
134 132 PRE_TARGETDEPS += qchartversion_p.h
135 133 QMAKE_CLEAN += qchartversion_p.h
136 134 QMAKE_EXTRA_TARGETS += chartversion
137 135 unix:QMAKE_DISTCLEAN += -r \
138 136 $$CHART_BUILD_HEADER_DIR \
139 137 $$CHART_BUILD_LIB_DIR
140 138 win32:QMAKE_DISTCLEAN += /Q \
141 139 $$CHART_BUILD_HEADER_DIR \
142 140 $$CHART_BUILD_LIB_DIR
143 141
144 142 # treat warnings as errors
145 143 win32-msvc*: {
146 144 QMAKE_CXXFLAGS += /WX
147 145 } else {
148 146 QMAKE_CXXFLAGS += -Werror
149 147 }
@@ -1,36 +1,37
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeBlueCerulean: public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeBlueCerulean() : ChartTheme(QChart::ChartThemeBlueCerulean)
9 9 {
10 10 // Series colors
11 11 m_seriesColors << QRgb(0xc7e85b);
12 12 m_seriesColors << QRgb(0x1cb54f);
13 13 m_seriesColors << QRgb(0x5cbf9b);
14 14 m_seriesColors << QRgb(0x009fbf);
15 15 m_seriesColors << QRgb(0xee7392);
16 16 generateSeriesGradients();
17 17
18 18 // Background
19 19 QLinearGradient backgroundGradient(0.5, 0.0, 0.5, 1.0);
20 20 backgroundGradient.setColorAt(0.0, QRgb(0x056189));
21 21 backgroundGradient.setColorAt(1.0, QRgb(0x101a31));
22 22 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
23 23 m_chartBackgroundGradient = backgroundGradient;
24 24
25 25 // Axes and other
26 26 m_masterFont = QFont("arial");
27 27 m_titleBrush = QBrush(QRgb(0xffffff));
28 28 m_axisLinePen = QPen(QRgb(0xd6d6d6));
29 29 m_axisLinePen.setWidth(2);
30 30 m_axisLabelBrush = QBrush(QRgb(0xffffff));
31 31 m_gridLinePen = QPen(QRgb(0x84a2b0));
32 32 m_gridLinePen.setWidth(1);
33 m_backgroundShades = BackgroundShadesNone;
33 34 }
34 35 };
35 36
36 37 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,36 +1,37
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeBlueNcs: public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeBlueNcs() : ChartTheme(QChart::ChartThemeBlueNcs)
9 9 {
10 10 // Series colors
11 11 m_seriesColors << QRgb(0x1db0da);
12 12 m_seriesColors << QRgb(0x1341a6);
13 13 m_seriesColors << QRgb(0x88d41e);
14 14 m_seriesColors << QRgb(0xff8e1a);
15 15 m_seriesColors << QRgb(0x398ca3);
16 16 generateSeriesGradients();
17 17
18 18 // Background
19 19 QLinearGradient backgroundGradient;
20 20 backgroundGradient.setColorAt(0.0, QRgb(0xffffff));
21 21 backgroundGradient.setColorAt(1.0, QRgb(0xffffff));
22 22 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
23 23 m_chartBackgroundGradient = backgroundGradient;
24 24
25 25 // Axes and other
26 26 m_masterFont = QFont("arial");
27 27 m_titleBrush = QBrush(QRgb(0x404044));
28 28 m_axisLinePen = QPen(QRgb(0xd6d6d6));
29 29 m_axisLinePen.setWidth(2);
30 30 m_axisLabelBrush = QBrush(QRgb(0x404044));
31 31 m_gridLinePen = QPen(QRgb(0xe2e2e2));
32 32 m_gridLinePen.setWidth(1);
33 m_backgroundShades = BackgroundShadesNone;
33 34 }
34 35 };
35 36
36 37 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,36 +1,37
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeBrownSand: public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeBrownSand() : ChartTheme(QChart::ChartThemeBrownSand)
9 9 {
10 10 // Series colors
11 11 m_seriesColors << QRgb(0xb39b72);
12 12 m_seriesColors << QRgb(0xb3b376);
13 13 m_seriesColors << QRgb(0xc35660);
14 14 m_seriesColors << QRgb(0x536780);
15 15 m_seriesColors << QRgb(0x494345);
16 16 generateSeriesGradients();
17 17
18 18 // Background
19 19 QLinearGradient backgroundGradient;
20 20 backgroundGradient.setColorAt(0.0, QRgb(0xf3ece0));
21 21 backgroundGradient.setColorAt(1.0, QRgb(0xf3ece0));
22 22 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
23 23 m_chartBackgroundGradient = backgroundGradient;
24 24
25 25 // Axes and other
26 26 m_masterFont = QFont("arial");
27 27 m_titleBrush = QBrush(QRgb(0x404044));
28 28 m_axisLinePen = QPen(QRgb(0xb5b0a7));
29 29 m_axisLinePen.setWidth(2);
30 30 m_axisLabelBrush = QBrush(QRgb(0x404044));
31 31 m_gridLinePen = QPen(QRgb(0xd4cec3));
32 32 m_gridLinePen.setWidth(1);
33 m_backgroundShades = BackgroundShadesNone;
33 34 }
34 35 };
35 36
36 37 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,36 +1,37
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeDark : public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeDark() : ChartTheme(QChart::ChartThemeDark)
9 9 {
10 10 // Series colors
11 11 m_seriesColors << QRgb(0x38ad6b);
12 12 m_seriesColors << QRgb(0x3c84a7);
13 13 m_seriesColors << QRgb(0xeb8817);
14 14 m_seriesColors << QRgb(0x7b7f8c);
15 15 m_seriesColors << QRgb(0xbf593e);
16 16 generateSeriesGradients();
17 17
18 18 // Background
19 19 QLinearGradient backgroundGradient(0.5, 0.0, 0.5, 1.0);
20 20 backgroundGradient.setColorAt(0.0, QRgb(0x2e303a));
21 21 backgroundGradient.setColorAt(1.0, QRgb(0x121218));
22 22 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
23 23 m_chartBackgroundGradient = backgroundGradient;
24 24
25 25 // Axes and other
26 26 m_masterFont = QFont("arial");
27 27 m_titleBrush = QBrush(QRgb(0xffffff));
28 28 m_axisLinePen = QPen(QRgb(0x86878c));
29 29 m_axisLinePen.setWidth(2);
30 30 m_axisLabelBrush = QBrush(QRgb(0xffffff));
31 31 m_gridLinePen = QPen(QRgb(0x86878c));
32 32 m_gridLinePen.setWidth(1);
33 m_backgroundShades = BackgroundShadesNone;
33 34 }
34 35 };
35 36
36 37 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,126 +1,149
1 1 #include "charttheme_p.h"
2 2 #ifdef Q_OS_WIN
3 3 #include <windows.h>
4 4 #include <stdio.h>
5 5 #endif
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 class ChartThemeDefault: public ChartTheme
10 10 {
11 11 public:
12 12 ChartThemeDefault() : ChartTheme(QChart::ChartThemeDefault)
13 13 {
14 14 #ifdef Q_OS_WIN
15 15 // TODO: use theme specific window frame color as a series base color (it would give more
16 16 // variation to the base colors in addition to the blue and black used now)
17 17 // TODO: COLOR_WINDOWTEXT for text color?
18 18 // TODO: COLOR_INFOTEXT for tooltip text color?
19 19 // TODO: COLOR_INFOBK for tooltip background color?
20 20
21 21 // First series base color from COLOR_HIGHLIGHT
22 22 DWORD colorHighlight;
23 23 colorHighlight = GetSysColor(COLOR_HIGHLIGHT);
24 24 m_seriesColors.append(QColor(GetRValue(colorHighlight),
25 25 GetGValue(colorHighlight),
26 26 GetBValue(colorHighlight)));
27 27
28 28 // Second series base color from COLOR_WINDOWFRAME
29 29 DWORD colorWindowFrame;
30 30 colorWindowFrame = GetSysColor(COLOR_WINDOWFRAME);
31 31 m_seriesColors.append(QColor(GetRValue(colorWindowFrame),
32 32 GetGValue(colorWindowFrame),
33 33 GetBValue(colorWindowFrame)));
34 34
35 35 // Third series base color from the middle of the COLOR_ACTIVECAPTION /
36 36 // COLOR_GRADIENTACTIVECAPTION gradient
37 37 DWORD colorGradientActiveCaptionLeft;
38 38 colorGradientActiveCaptionLeft = GetSysColor(COLOR_ACTIVECAPTION);
39 39 DWORD colorGradientActiveCaptionRight;
40 40 colorGradientActiveCaptionRight = GetSysColor(COLOR_GRADIENTACTIVECAPTION);
41 41 QLinearGradient g;
42 42 QColor start = QColor(GetRValue(colorGradientActiveCaptionLeft),
43 43 GetGValue(colorGradientActiveCaptionLeft),
44 44 GetBValue(colorGradientActiveCaptionLeft));
45 45 g.setColorAt(0.0, start);
46 46 QColor end = QColor(GetRValue(colorGradientActiveCaptionRight),
47 47 GetGValue(colorGradientActiveCaptionRight),
48 48 GetBValue(colorGradientActiveCaptionRight));
49 49 g.setColorAt(1.0, end);
50 50 m_seriesColors.append(colorAt(g, 0.5));
51 51
52 52 // Generate gradients from the base colors
53 53 generateSeriesGradients();
54 54
55 55 // Background fill color from COLOR_WINDOW
56 56 QLinearGradient backgroundGradient;
57 57 DWORD colorWindow;
58 58 colorWindow = GetSysColor(COLOR_WINDOW);
59 59 backgroundGradient.setColorAt(0.0, QColor(GetRValue(colorWindow),
60 60 GetGValue(colorWindow),
61 61 GetBValue(colorWindow)));
62 62 backgroundGradient.setColorAt(1.0, QColor(GetRValue(colorWindow),
63 63 GetGValue(colorWindow),
64 64 GetBValue(colorWindow)));
65 65 // Axes and other
66 m_backgroundShadesBrush = QBrush(QColor(0xaf, 0xaf, 0xaf, 0x50));
67 m_backgroundShades = BackgroundShadesVertical;
66 m_masterFont = QFont("arial");
67 m_axisLinePen = QPen(0xd6d6d6);
68 m_axisLinePen.setWidth(1);
69 m_axisLabelBrush = QBrush(QRgb(0x404044));
70 m_gridLinePen = QPen(QRgb(0xe2e2e2));
71 m_gridLinePen.setWidth(1);
72 m_backgroundShades = BackgroundShadesNone;
68 73
69 74 #elif defined(Q_OS_LINUX)
70 75 // TODO: replace this dummy theme with linux specific theme
71 76 m_seriesColors << QRgb(0x60a6e6);
72 77 m_seriesColors << QRgb(0x92ca66);
73 78 m_seriesColors << QRgb(0xeba85f);
74 79 m_seriesColors << QRgb(0xfc5751);
75 80 generateSeriesGradients();
76 81
82 // Background
77 83 QLinearGradient backgroundGradient;
78 84 backgroundGradient.setColorAt(0.0, QRgb(0xffffff));
79 backgroundGradient.setColorAt(1.0, QRgb(0xe9e9e9));
85 backgroundGradient.setColorAt(1.0, QRgb(0xffffff));
80 86 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
81 87 m_chartBackgroundGradient = backgroundGradient;
82 88
83 89 // Axes and other
84 m_backgroundShadesBrush = QBrush(QColor(0xaf, 0xaf, 0xaf, 0x50));
85 m_backgroundShades = BackgroundShadesVertical;
90 m_masterFont = QFont("arial");
91 m_axisLinePen = QPen(0xd6d6d6);
92 m_axisLinePen.setWidth(1);
93 m_axisLabelBrush = QBrush(QRgb(0x404044));
94 m_gridLinePen = QPen(QRgb(0xe2e2e2));
95 m_gridLinePen.setWidth(1);
96 m_backgroundShades = BackgroundShadesNone;
86 97
87 98 #elif defined(Q_OS_MAC)
88 99 // TODO: replace this dummy theme with OSX specific theme
89 100 m_seriesColors << QRgb(0x60a6e6);
90 101 m_seriesColors << QRgb(0x92ca66);
91 102 m_seriesColors << QRgb(0xeba85f);
92 103 m_seriesColors << QRgb(0xfc5751);
93 104 generateSeriesGradients();
94 105
106 // Background
95 107 QLinearGradient backgroundGradient;
96 108 backgroundGradient.setColorAt(0.0, QRgb(0xffffff));
97 backgroundGradient.setColorAt(1.0, QRgb(0xe9e9e9));
109 backgroundGradient.setColorAt(1.0, QRgb(0xffffff));
98 110 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
99 111 m_chartBackgroundGradient = backgroundGradient;
100 112
101 113 // Axes and other
102 m_backgroundShadesBrush = QBrush(QColor(0xaf, 0xaf, 0xaf, 0x50));
103 m_backgroundShades = BackgroundShadesVertical;
114 m_masterFont = QFont("arial");
115 m_axisLinePen = QPen(0xd6d6d6);
116 m_axisLinePen.setWidth(1);
117 m_axisLabelBrush = QBrush(QRgb(0x404044));
118 m_gridLinePen = QPen(QRgb(0xe2e2e2));
119 m_gridLinePen.setWidth(1);
120 m_backgroundShades = BackgroundShadesNone;
104 121
105 122 #else
106 123 // TODO: replace this dummy theme with generic (not OS specific) theme
107 124 m_seriesColors << QRgb(0x60a6e6);
108 125 m_seriesColors << QRgb(0x92ca66);
109 126 m_seriesColors << QRgb(0xeba85f);
110 127 m_seriesColors << QRgb(0xfc5751);
111 128 generateSeriesGradients();
112 129
130 // Background
113 131 QLinearGradient backgroundGradient;
114 132 backgroundGradient.setColorAt(0.0, QRgb(0xffffff));
115 backgroundGradient.setColorAt(1.0, QRgb(0xafafaf));
133 backgroundGradient.setColorAt(1.0, QRgb(0xffffff));
116 134 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
117 m_backgroundGradient = backgroundGradient;
135 m_chartBackgroundGradient = backgroundGradient;
118 136
119 137 // Axes and other
120 m_backgroundShadesBrush = QBrush(QColor(0xaf, 0xaf, 0xaf, 0x50));
121 m_backgroundShades = BackgroundShadesVertical;
138 m_masterFont = QFont("arial");
139 m_axisLinePen = QPen(0xd6d6d6);
140 m_axisLinePen.setWidth(1);
141 m_axisLabelBrush = QBrush(QRgb(0x404044));
142 m_gridLinePen = QPen(QRgb(0xe2e2e2));
143 m_gridLinePen.setWidth(1);
144 m_backgroundShades = BackgroundShadesNone;
122 145 #endif
123 146 }
124 147 };
125 148
126 149 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,33 +1,34
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeIcy: public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeIcy() : ChartTheme(QChart::ChartThemeIcy)
9 9 {
10 10 // Series
11 11 m_seriesColors << QRgb(0x0d2673);
12 12 m_seriesColors << QRgb(0x2685bf);
13 13 m_seriesColors << QRgb(0x3dadd9);
14 14 m_seriesColors << QRgb(0x62c3d9);
15 15 generateSeriesGradients();
16 16
17 17 // Background
18 18 QLinearGradient backgroundGradient;
19 19 backgroundGradient.setColorAt(0.0, QRgb(0xebebeb));
20 20 backgroundGradient.setColorAt(1.0, QRgb(0xf8f9fb));
21 21 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
22 22 m_chartBackgroundGradient = backgroundGradient;
23 23
24 24 // Axes and other
25 25 m_axisLinePen = QPen(QRgb(0x0f0f0f));
26 26 m_axisLinePen.setWidth(2);
27 27 m_axisLabelBrush = QBrush(QRgb(0x3f3f3f));
28 28 m_gridLinePen = QPen(QRgb(0x0f0f0f));
29 29 m_gridLinePen.setWidth(2);
30 m_backgroundShades = BackgroundShadesNone;
30 31 }
31 32 };
32 33
33 34 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,35 +1,36
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeLight: public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeLight() : ChartTheme(QChart::ChartThemeLight)
9 9 {
10 10 // Series colors
11 11 m_seriesColors << QRgb(0x209fdf);
12 12 m_seriesColors << QRgb(0x99ca53);
13 13 m_seriesColors << QRgb(0xf6a625);
14 14 m_seriesColors << QRgb(0x6d5fd5);
15 15 m_seriesColors << QRgb(0xbf593e);
16 16 generateSeriesGradients();
17 17
18 18 // Background
19 19 QLinearGradient backgroundGradient;
20 20 backgroundGradient.setColorAt(0.0, QRgb(0xffffff));
21 21 backgroundGradient.setColorAt(1.0, QRgb(0xffffff));
22 22 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
23 23 m_chartBackgroundGradient = backgroundGradient;
24 24
25 25 // Axes and other
26 26 m_masterFont = QFont("arial");
27 27 m_axisLinePen = QPen(0xd6d6d6);
28 28 m_axisLinePen.setWidth(1);
29 29 m_axisLabelBrush = QBrush(QRgb(0x404044));
30 30 m_gridLinePen = QPen(QRgb(0xe2e2e2));
31 31 m_gridLinePen.setWidth(1);
32 m_backgroundShades = BackgroundShadesNone;
32 33 }
33 34 };
34 35
35 36 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,35 +1,35
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeScientific: public ChartTheme
6 6 {
7 7 public:
8 8 ChartThemeScientific() : ChartTheme(QChart::ChartThemeScientific)
9 9 {
10 10 // Series
11 11 m_seriesColors << QRgb(0xFFAD00);
12 12 m_seriesColors << QRgb(0x596A75);
13 13 m_seriesColors << QRgb(0x202020);
14 14 m_seriesColors << QRgb(0x474747);
15 15 generateSeriesGradients();
16 16
17 17 // Background
18 18 QLinearGradient backgroundGradient;
19 19 backgroundGradient.setColorAt(0.0, QRgb(0xfffefc));
20 20 backgroundGradient.setColorAt(1.0, QRgb(0xfffefc));
21 21 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
22 22 m_chartBackgroundGradient = backgroundGradient;
23 23
24 24 // Axes and other
25 25 m_axisLinePen = QPen(QRgb(0x0f0f0f));
26 26 m_axisLinePen.setWidth(2);
27 27 m_axisLabelBrush = QBrush(QRgb(0x3f3f3f));
28 m_backgroundShadesBrush = QBrush(QColor(0xff, 0xad, 0x00, 0x50));
29 m_backgroundShades = BackgroundShadesHorizontal;
30 28 m_gridLinePen = QPen(QRgb(0x0f0f0f));
31 29 m_gridLinePen.setWidth(2);
30 m_backgroundShadesBrush = QBrush(QColor(0xff, 0xad, 0x00, 0x50));
31 m_backgroundShades = BackgroundShadesHorizontal;
32 32 }
33 33 };
34 34
35 35 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,331 +1,333
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 chartTheme->addItem("Vanilla");
118 chartTheme->addItem("Light");
119 chartTheme->addItem("Blue Cerulean");
120 chartTheme->addItem("Dark");
121 chartTheme->addItem("Brown Sand");
122 chartTheme->addItem("Blue NCS");
119 123 chartTheme->addItem("Icy");
120 chartTheme->addItem("Grayscale");
121 124 chartTheme->addItem("Scientific");
122 chartTheme->addItem("Unnamed1");
123 125 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
124 126 this, SLOT(changeChartTheme(int)));
125 127 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
126 128 grid->addWidget(chartTheme, 8, 1);
127 129 }
128 130
129 131 // Different check boxes for customizing chart
130 132 void MainWidget::initCheckboxes(QGridLayout *grid)
131 133 {
132 134 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
133 135 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
134 136 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
135 137 zoomCheckBox->setChecked(true);
136 138 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
137 139
138 140 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
139 141 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
140 142 aliasCheckBox->setChecked(false);
141 143 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
142 144 }
143 145
144 146 void MainWidget::antiAliasToggled(bool enabled)
145 147 {
146 148 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
147 149 }
148 150
149 151 void MainWidget::addSeries()
150 152 {
151 153 if (!m_addSerieDialog) {
152 154 m_addSerieDialog = new DataSerieDialog(this);
153 155 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
154 156 this, SLOT(addSeries(QString, int, int, QString, bool)));
155 157 }
156 158 m_addSerieDialog->exec();
157 159 }
158 160
159 161 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
160 162 {
161 163 // TODO: dataCharacteristics
162 164 QList<RealList> testData;
163 165 for (int j(0); j < columnCount; j++) {
164 166 QList <qreal> newColumn;
165 167 for (int i(0); i < rowCount; i++) {
166 168 if (dataCharacteristics == "Sin") {
167 169 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
168 170 } else if (dataCharacteristics == "Sin + random") {
169 171 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
170 172 } else if (dataCharacteristics == "Random") {
171 173 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
172 174 } else if (dataCharacteristics == "Linear") {
173 175 //newColumn.append(i * (j + 1.0));
174 176 // TODO: temporary hack to make pie work; prevent zero values:
175 177 newColumn.append(i * (j + 1.0) + 0.1);
176 178 } else { // "constant"
177 179 newColumn.append((j + 1.0));
178 180 }
179 181 }
180 182 testData.append(newColumn);
181 183 }
182 184 return testData;
183 185 }
184 186
185 187 QStringList MainWidget::generateLabels(int count)
186 188 {
187 189 QStringList result;
188 190 for (int i(0); i < count; i++)
189 191 result.append("label" + QString::number(i));
190 192 return result;
191 193 }
192 194
193 195 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
194 196 {
195 197 qDebug() << "addSeries: " << seriesName
196 198 << " columnCount: " << columnCount
197 199 << " rowCount: " << rowCount
198 200 << " dataCharacteristics: " << dataCharacteristics
199 201 << " labels enabled: " << labelsEnabled;
200 202 m_defaultSeriesName = seriesName;
201 203
202 204 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
203 205
204 206 // Line series and scatter series use similar data
205 207 if (seriesName == "Line") {
206 208 for (int j(0); j < data.count(); j ++) {
207 209 QList<qreal> column = data.at(j);
208 210 QLineSeries *series = new QLineSeries();
209 211 for (int i(0); i < column.count(); i++) {
210 212 series->add(i, column.at(i));
211 213 }
212 214 m_chartView->addSeries(series);
213 215 }
214 216 } else if (seriesName == "Area") {
215 217 // TODO: lower series for the area?
216 218 for (int j(0); j < data.count(); j ++) {
217 219 QList<qreal> column = data.at(j);
218 220 QLineSeries *lineSeries = new QLineSeries();
219 221 for (int i(0); i < column.count(); i++) {
220 222 lineSeries->add(i, column.at(i));
221 223 }
222 224 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
223 225 m_chartView->addSeries(areaSeries);
224 226 }
225 227 } else if (seriesName == "Scatter") {
226 228 for (int j(0); j < data.count(); j++) {
227 229 QList<qreal> column = data.at(j);
228 230 QScatterSeries *series = new QScatterSeries();
229 231 for (int i(0); i < column.count(); i++) {
230 232 (*series) << QPointF(i, column.at(i));
231 233 }
232 234 m_chartView->addSeries(series);
233 235 }
234 236 } else if (seriesName == "Pie") {
235 237 QStringList labels = generateLabels(rowCount);
236 238 for (int j(0); j < data.count(); j++) {
237 239 QPieSeries *series = new QPieSeries();
238 240 QList<qreal> column = data.at(j);
239 241 for (int i(0); i < column.count(); i++) {
240 242 series->add(column.at(i), labels.at(i));
241 243 }
242 244 m_chartView->addSeries(series);
243 245 }
244 246 } else if (seriesName == "Bar"
245 247 || seriesName == "Stacked bar"
246 248 || seriesName == "Percent bar") {
247 249 QStringList category;
248 250 QStringList labels = generateLabels(rowCount);
249 251 foreach(QString label, labels)
250 252 category << label;
251 253 QBarSeries* series = 0;
252 254 if (seriesName == "Bar")
253 255 series = new QBarSeries(category, this);
254 256 else if (seriesName == "Stacked bar")
255 257 series = new QStackedBarSeries(category, this);
256 258 else
257 259 series = new QPercentBarSeries(category, this);
258 260
259 261 for (int j(0); j < data.count(); j++) {
260 262 QList<qreal> column = data.at(j);
261 263 QBarSet *set = new QBarSet("set" + QString::number(j));
262 264 for (int i(0); i < column.count(); i++) {
263 265 *set << column.at(i);
264 266 }
265 267 series->addBarSet(set);
266 268 }
267 269
268 270 // TODO: new implementation of setFloatingValuesEnabled with signals
269 271 //series->setFloatingValuesEnabled(true);
270 272 series->setToolTipEnabled(true);
271 273 m_chartView->addSeries(series);
272 274 } else if (seriesName == "Spline") {
273 275 for (int j(0); j < data.count(); j ++) {
274 276 QList<qreal> column = data.at(j);
275 277 QSplineSeries *series = new QSplineSeries();
276 278 for (int i(0); i < column.count(); i++) {
277 279 series->add(i, column.at(i));
278 280 }
279 281 m_chartView->addSeries(series);
280 282 }
281 283 }
282 284 }
283 285
284 286 void MainWidget::backgroundChanged(int itemIndex)
285 287 {
286 288 qDebug() << "backgroundChanged: " << itemIndex;
287 289 }
288 290
289 291 void MainWidget::autoScaleChanged(int value)
290 292 {
291 293 if (value) {
292 294 // TODO: enable auto scaling
293 295 } else {
294 296 // TODO: set scaling manually (and disable auto scaling)
295 297 }
296 298
297 299 m_xMinSpin->setEnabled(!value);
298 300 m_xMaxSpin->setEnabled(!value);
299 301 m_yMinSpin->setEnabled(!value);
300 302 m_yMaxSpin->setEnabled(!value);
301 303 }
302 304
303 305 void MainWidget::xMinChanged(int value)
304 306 {
305 307 qDebug() << "xMinChanged: " << value;
306 308 }
307 309
308 310 void MainWidget::xMaxChanged(int value)
309 311 {
310 312 qDebug() << "xMaxChanged: " << value;
311 313 }
312 314
313 315 void MainWidget::yMinChanged(int value)
314 316 {
315 317 qDebug() << "yMinChanged: " << value;
316 318 }
317 319
318 320 void MainWidget::yMaxChanged(int value)
319 321 {
320 322 qDebug() << "yMaxChanged: " << value;
321 323 }
322 324
323 325 void MainWidget::changeChartTheme(int themeIndex)
324 326 {
325 327 qDebug() << "changeChartTheme: " << themeIndex;
326 328 m_chartView->setChartTheme((QChart::ChartTheme) themeIndex);
327 329 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
328 330 QSize s = size();
329 331 s.setWidth(s.width()+1);
330 332 resize(s);
331 333 }
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now