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