##// END OF EJS Templates
Fix chartthemes demo. Pie chart is reserving too much space because labels are going offscreen.
Jani Honkonen -
r704:f36c47a5e137
parent child
Show More
@@ -1,250 +1,251
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qpieseries.h>
5 #include <qpieseries.h>
6 #include <qpieslice.h>
6 #include <qpieslice.h>
7 #include <qbarseries.h>
7 #include <qbarseries.h>
8 #include <qpercentbarseries.h>
8 #include <qpercentbarseries.h>
9 #include <qstackedbarseries.h>
9 #include <qstackedbarseries.h>
10 #include <qbarset.h>
10 #include <qbarset.h>
11 #include <QGridLayout>
11 #include <QGridLayout>
12 #include <QFormLayout>
12 #include <QFormLayout>
13 #include <QComboBox>
13 #include <QComboBox>
14 #include <QSpinBox>
14 #include <QSpinBox>
15 #include <QCheckBox>
15 #include <QCheckBox>
16 #include <QGroupBox>
16 #include <QGroupBox>
17 #include <QLabel>
17 #include <QLabel>
18 #include <QTime>
18 #include <QTime>
19 #include <qlineseries.h>
19 #include <qlineseries.h>
20 #include <qsplineseries.h>
20 #include <qsplineseries.h>
21 #include <qscatterseries.h>
21 #include <qscatterseries.h>
22 #include <qareaseries.h>
22 #include <qareaseries.h>
23
23
24 QTCOMMERCIALCHART_USE_NAMESPACE
24 QTCOMMERCIALCHART_USE_NAMESPACE
25
25
26 typedef QPair<QPointF, QString> Data;
26 typedef QPair<QPointF, QString> Data;
27 typedef QList<Data> DataList;
27 typedef QList<Data> DataList;
28 typedef QList<DataList> DataTable;
28 typedef QList<DataList> DataTable;
29
29
30
30
31 class MainWidget : public QWidget
31 class MainWidget : public QWidget
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public:
35 public:
36 explicit MainWidget(QWidget* parent = 0)
36 explicit MainWidget(QWidget* parent = 0)
37 :QWidget(parent)
37 :QWidget(parent)
38 {
38 {
39 // set seed for random stuff
39 // set seed for random stuff
40 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
40 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
41
41
42 // generate random data
42 // generate random data
43 int listCount = 3;
43 int listCount = 3;
44 int valueMax = 100;
44 int valueMax = 100;
45 int valueCount = 11;
45 int valueCount = 11;
46 for (int i(0); i < listCount; i++) {
46 for (int i(0); i < listCount; i++) {
47 DataList dataList;
47 DataList dataList;
48 for (int j(0); j < valueCount; j++) {
48 for (int j(0); j < valueCount; j++) {
49 QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax);
49 QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax);
50 QString label = "Item " + QString::number(i) + ":" + QString::number(j);
50 QString label = "Item " + QString::number(i) + ":" + QString::number(j);
51 dataList << Data(value, label);
51 dataList << Data(value, label);
52 }
52 }
53 m_dataTable << dataList;
53 m_dataTable << dataList;
54 }
54 }
55
55
56 // create layout
56 // create layout
57 QGridLayout* baseLayout = new QGridLayout();
57 QGridLayout* baseLayout = new QGridLayout();
58
58
59 // settings layout
59 // settings layout
60 m_themeComboBox = new QComboBox();
60 m_themeComboBox = new QComboBox();
61 m_themeComboBox->addItem("Default", QChart::ChartThemeDefault);
61 m_themeComboBox->addItem("Default", QChart::ChartThemeDefault);
62 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
62 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
63 m_themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
63 m_themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
64 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
64 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
65 m_themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
65 m_themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
66 m_themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
66 m_themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
67 m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy);
67 m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy);
68 m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific);
68 m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific);
69 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme()));
69 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme()));
70 QCheckBox *antialiasCheckBox = new QCheckBox("Anti aliasing");
70 QCheckBox *antialiasCheckBox = new QCheckBox("Anti aliasing");
71 connect(antialiasCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAntialiasing(bool)));
71 connect(antialiasCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAntialiasing(bool)));
72 QCheckBox *animatedCheckBox = new QCheckBox("Animated");
72 QCheckBox *animatedCheckBox = new QCheckBox("Animated");
73 connect(animatedCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAnimations(bool)));
73 connect(animatedCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateAnimations(bool)));
74 QHBoxLayout *settingsLayout = new QHBoxLayout();
74 QHBoxLayout *settingsLayout = new QHBoxLayout();
75 settingsLayout->addWidget(new QLabel("Theme:"));
75 settingsLayout->addWidget(new QLabel("Theme:"));
76 settingsLayout->addWidget(m_themeComboBox);
76 settingsLayout->addWidget(m_themeComboBox);
77 settingsLayout->addWidget(antialiasCheckBox);
77 settingsLayout->addWidget(antialiasCheckBox);
78 settingsLayout->addWidget(animatedCheckBox);
78 settingsLayout->addWidget(animatedCheckBox);
79 settingsLayout->addStretch();
79 settingsLayout->addStretch();
80 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
80 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
81
81
82 // area chart
82 // area chart
83 QChartView *chart = new QChartView();
83 QChartView *chart = new QChartView();
84 chart->setChartTitle("Area chart");
84 chart->setChartTitle("Area chart");
85 baseLayout->addWidget(chart, 1, 0);
85 baseLayout->addWidget(chart, 1, 0);
86 {
86 {
87 for (int i(0); i < m_dataTable.count(); i++) {
87 for (int i(0); i < m_dataTable.count(); i++) {
88 QLineSeries *series1 = new QLineSeries(chart);
88 QLineSeries *series1 = new QLineSeries(chart);
89 QLineSeries *series2 = new QLineSeries(chart);
89 QLineSeries *series2 = new QLineSeries(chart);
90 foreach (Data data, m_dataTable[i]) {
90 foreach (Data data, m_dataTable[i]) {
91 series1->add(data.first);
91 series1->add(data.first);
92 series2->add(QPointF(data.first.x(), 0.0));
92 series2->add(QPointF(data.first.x(), 0.0));
93 }
93 }
94 QAreaSeries *area = new QAreaSeries(series1, series2);
94 QAreaSeries *area = new QAreaSeries(series1, series2);
95 chart->addSeries(area);
95 chart->addSeries(area);
96 }
96 }
97 }
97 }
98 m_charts << chart;
98 m_charts << chart;
99
99
100 // bar chart
100 // bar chart
101 chart = new QChartView();
101 chart = new QChartView();
102 chart->setChartTitle("bar chart");
102 chart->setChartTitle("Bar chart");
103 baseLayout->addWidget(chart, 1, 1);
103 baseLayout->addWidget(chart, 1, 1);
104 {
104 {
105 QStringList categories;
105 QStringList categories;
106 // TODO: categories
106 // TODO: categories
107 for (int i(0); i < valueCount; i++)
107 for (int i(0); i < valueCount; i++)
108 categories << QString::number(i);
108 categories << QString::number(i);
109 // QBarSeries* series = new QBarSeries(categories, chart);
109 // QBarSeries* series = new QBarSeries(categories, chart);
110 // QPercentBarSeries* series = new QPercentBarSeries(categories, chart);
110 // QPercentBarSeries* series = new QPercentBarSeries(categories, chart);
111 QStackedBarSeries* series = new QStackedBarSeries(categories, chart);
111 QStackedBarSeries* series = new QStackedBarSeries(categories, chart);
112 for (int i(0); i < m_dataTable.count(); i++) {
112 for (int i(0); i < m_dataTable.count(); i++) {
113 QBarSet *set = new QBarSet("Set" + QString::number(i));
113 QBarSet *set = new QBarSet("Set" + QString::number(i));
114 foreach (Data data, m_dataTable[i])
114 foreach (Data data, m_dataTable[i])
115 *set << data.first.y();
115 *set << data.first.y();
116 series->addBarSet(set);
116 series->addBarSet(set);
117 }
117 }
118 chart->addSeries(series);
118 chart->addSeries(series);
119 }
119 }
120 m_charts << chart;
120 m_charts << chart;
121
121
122 // line chart
122 // line chart
123 chart = new QChartView();
123 chart = new QChartView();
124 chart->setChartTitle("line chart");
124 chart->setChartTitle("Line chart");
125 baseLayout->addWidget(chart, 1, 2);
125 baseLayout->addWidget(chart, 1, 2);
126 foreach (DataList list, m_dataTable) {
126 foreach (DataList list, m_dataTable) {
127 QLineSeries *series = new QLineSeries(chart);
127 QLineSeries *series = new QLineSeries(chart);
128 foreach (Data data, list)
128 foreach (Data data, list)
129 series->add(data.first);
129 series->add(data.first);
130 chart->addSeries(series);
130 chart->addSeries(series);
131 }
131 }
132 m_charts << chart;
132 m_charts << chart;
133
133
134 // pie chart
134 // pie chart
135 chart = new QChartView();
135 chart = new QChartView();
136 chart->setChartTitle("pie chart");
136 chart->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
137 chart->setChartTitle("Pie chart");
137 baseLayout->addWidget(chart, 2, 0);
138 baseLayout->addWidget(chart, 2, 0);
138 qreal pieSize = 1.0 / m_dataTable.count();
139 qreal pieSize = 1.0 / m_dataTable.count();
139 for (int i=0; i<m_dataTable.count(); i++) {
140 for (int i=0; i<m_dataTable.count(); i++) {
140 QPieSeries *series = new QPieSeries(chart);
141 QPieSeries *series = new QPieSeries(chart);
141 foreach (Data data, m_dataTable[i]) {
142 foreach (Data data, m_dataTable[i]) {
142 QPieSlice *slice = series->add(data.first.y(), data.second);
143 QPieSlice *slice = series->add(data.first.y(), data.second);
143 if (data == m_dataTable[i].first()) {
144 if (data == m_dataTable[i].first()) {
144 slice->setLabelVisible();
145 slice->setLabelVisible();
145 slice->setExploded();
146 slice->setExploded();
146 }
147 }
147 }
148 }
148 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
149 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
149 series->setPieSize(pieSize);
150 series->setPieSize(pieSize);
150 series->setPiePosition(hPos, 0.5);
151 series->setPiePosition(hPos, 0.5);
151 chart->addSeries(series);
152 chart->addSeries(series);
152 }
153 }
153 m_charts << chart;
154 m_charts << chart;
154
155
155 // spine chart
156 // spine chart
156 chart = new QChartView();
157 chart = new QChartView();
157 chart->setChartTitle("spline chart");
158 chart->setChartTitle("Spline chart");
158 baseLayout->addWidget(chart, 2, 1);
159 baseLayout->addWidget(chart, 2, 1);
159 foreach (DataList list, m_dataTable) {
160 foreach (DataList list, m_dataTable) {
160 QSplineSeries *series = new QSplineSeries(chart);
161 QSplineSeries *series = new QSplineSeries(chart);
161 foreach (Data data, list)
162 foreach (Data data, list)
162 series->add(data.first);
163 series->add(data.first);
163 chart->addSeries(series);
164 chart->addSeries(series);
164 }
165 }
165 m_charts << chart;
166 m_charts << chart;
166
167
167 // scatter chart
168 // scatter chart
168 chart = new QChartView();
169 chart = new QChartView();
169 chart->setChartTitle("scatter chart");
170 chart->setChartTitle("Scatter chart");
170 baseLayout->addWidget(chart, 2, 2);
171 baseLayout->addWidget(chart, 2, 2);
171 foreach (DataList list, m_dataTable) {
172 foreach (DataList list, m_dataTable) {
172 QScatterSeries *series = new QScatterSeries(chart);
173 QScatterSeries *series = new QScatterSeries(chart);
173 foreach (Data data, list)
174 foreach (Data data, list)
174 series->add(data.first);
175 series->add(data.first);
175 chart->addSeries(series);
176 chart->addSeries(series);
176 }
177 }
177 m_charts << chart;
178 m_charts << chart;
178
179
179 setLayout(baseLayout);
180 setLayout(baseLayout);
180 }
181 }
181
182
182 public Q_SLOTS:
183 public Q_SLOTS:
183
184
184 void updateTheme()
185 void updateTheme()
185 {
186 {
186 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
187 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
187 foreach (QChartView *chart, m_charts)
188 foreach (QChartView *chart, m_charts)
188 chart->setChartTheme(theme);
189 chart->setChartTheme(theme);
189
190
190 QPalette pal = window()->palette();
191 QPalette pal = window()->palette();
191 if (theme == QChart::ChartThemeLight) {
192 if (theme == QChart::ChartThemeLight) {
192 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
193 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
193 pal.setColor(QPalette::WindowText, QRgb(0x404044));
194 pal.setColor(QPalette::WindowText, QRgb(0x404044));
194 } else if (theme == QChart::ChartThemeDark) {
195 } else if (theme == QChart::ChartThemeDark) {
195 pal.setColor(QPalette::Window, QRgb(0x121218));
196 pal.setColor(QPalette::Window, QRgb(0x121218));
196 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
197 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
197 } else if (theme == QChart::ChartThemeBlueCerulean) {
198 } else if (theme == QChart::ChartThemeBlueCerulean) {
198 pal.setColor(QPalette::Window, QRgb(0x40434a));
199 pal.setColor(QPalette::Window, QRgb(0x40434a));
199 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
200 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
200 } else if (theme == QChart::ChartThemeBrownSand) {
201 } else if (theme == QChart::ChartThemeBrownSand) {
201 pal.setColor(QPalette::Window, QRgb(0x9e8965));
202 pal.setColor(QPalette::Window, QRgb(0x9e8965));
202 pal.setColor(QPalette::WindowText, QRgb(0x404044));
203 pal.setColor(QPalette::WindowText, QRgb(0x404044));
203 } else if (theme == QChart::ChartThemeBlueNcs) {
204 } else if (theme == QChart::ChartThemeBlueNcs) {
204 pal.setColor(QPalette::Window, QRgb(0x018bba));
205 pal.setColor(QPalette::Window, QRgb(0x018bba));
205 pal.setColor(QPalette::WindowText, QRgb(0x404044));
206 pal.setColor(QPalette::WindowText, QRgb(0x404044));
206 } else {
207 } else {
207 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
208 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
208 pal.setColor(QPalette::WindowText, QRgb(0x404044));
209 pal.setColor(QPalette::WindowText, QRgb(0x404044));
209 }
210 }
210 window()->setPalette(pal);
211 window()->setPalette(pal);
211 }
212 }
212
213
213 void updateAntialiasing(bool enabled)
214 void updateAntialiasing(bool enabled)
214 {
215 {
215 foreach (QChartView *chart, m_charts)
216 foreach (QChartView *chart, m_charts)
216 chart->setRenderHint(QPainter::Antialiasing, enabled);
217 chart->setRenderHint(QPainter::Antialiasing, enabled);
217 }
218 }
218
219
219 void updateAnimations(bool animated)
220 void updateAnimations(bool animated)
220 {
221 {
221 QChart::AnimationOptions options = QChart::NoAnimation;
222 QChart::AnimationOptions options = QChart::NoAnimation;
222 if (animated)
223 if (animated)
223 options = QChart::AllAnimations;
224 options = QChart::AllAnimations;
224
225
225 foreach (QChartView *chart, m_charts)
226 foreach (QChartView *chart, m_charts)
226 chart->setAnimationOptions(options);
227 chart->setAnimationOptions(options);
227 }
228 }
228
229
229 private:
230 private:
230 QList<QChartView*> m_charts;
231 QList<QChartView*> m_charts;
231 QComboBox *m_themeComboBox;
232 QComboBox *m_themeComboBox;
232 DataTable m_dataTable;
233 DataTable m_dataTable;
233 };
234 };
234
235
235 int main(int argc, char *argv[])
236 int main(int argc, char *argv[])
236 {
237 {
237 QApplication a(argc, argv);
238 QApplication a(argc, argv);
238
239
239 QMainWindow window;
240 QMainWindow window;
240
241
241 MainWidget* widget = new MainWidget();
242 MainWidget* widget = new MainWidget();
242
243
243 window.setCentralWidget(widget);
244 window.setCentralWidget(widget);
244 window.resize(900, 600);
245 window.resize(900, 600);
245 window.show();
246 window.show();
246
247
247 return a.exec();
248 return a.exec();
248 }
249 }
249
250
250 #include "main.moc"
251 #include "main.moc"
General Comments 0
You need to be logged in to leave comments. Login now