@@ -1,196 +1,198 | |||||
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 = 21; |
|
43 | int valueCount = 21; | |
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 | // theme combo | |
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 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme())); |
|
64 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme())); | |
65 | baseLayout->addWidget(new QLabel("Theme:"), 0, 0); |
|
65 | baseLayout->addWidget(new QLabel("Theme:"), 0, 0); | |
66 | baseLayout->addWidget(m_themeComboBox, 0, 1); |
|
66 | baseLayout->addWidget(m_themeComboBox, 0, 1); | |
67 |
|
67 | |||
68 | // area chart |
|
68 | // area chart | |
69 | QChartView *chart = new QChartView(); |
|
69 | QChartView *chart = new QChartView(); | |
70 | chart->setChartTitle("Area chart"); |
|
70 | chart->setChartTitle("Area chart"); | |
71 | chart->setRenderHint(QPainter::Antialiasing); |
|
71 | chart->setRenderHint(QPainter::Antialiasing); | |
72 | baseLayout->addWidget(chart, 1, 0); |
|
72 | baseLayout->addWidget(chart, 1, 0); | |
73 | { |
|
73 | { | |
74 | QLineSeries *series1 = new QLineSeries(chart); |
|
74 | for (int i(0); i < m_dataTable.count(); i++) { | |
75 | foreach (Data data, m_dataTable.first()) |
|
75 | QLineSeries *series1 = new QLineSeries(chart); | |
76 | series1->add(data.first); |
|
76 | QLineSeries *series2 = new QLineSeries(chart); | |
77 | QLineSeries *series2 = new QLineSeries(chart); |
|
77 | foreach (Data data, m_dataTable[i]) { | |
78 | foreach (Data data, m_dataTable.last()) |
|
78 | series1->add(data.first); | |
79 | series2->add(data.first); |
|
79 | series2->add(QPointF(data.first.x(), 0.0)); | |
80 | QAreaSeries *series = new QAreaSeries(series1, series2); |
|
80 | } | |
81 | chart->addSeries(series); |
|
81 | QAreaSeries *area = new QAreaSeries(series1, series2); | |
|
82 | chart->addSeries(area); | |||
|
83 | } | |||
82 | } |
|
84 | } | |
83 | m_charts << chart; |
|
85 | m_charts << chart; | |
84 |
|
86 | |||
85 | // bar chart |
|
87 | // bar chart | |
86 | chart = new QChartView(); |
|
88 | chart = new QChartView(); | |
87 | chart->setChartTitle("bar chart"); |
|
89 | chart->setChartTitle("bar chart"); | |
88 | chart->setRenderHint(QPainter::Antialiasing); |
|
90 | chart->setRenderHint(QPainter::Antialiasing); | |
89 | baseLayout->addWidget(chart, 1, 1); |
|
91 | baseLayout->addWidget(chart, 1, 1); | |
90 | { |
|
92 | { | |
91 | QStringList categories; |
|
93 | QStringList categories; | |
92 | // TODO: categories |
|
94 | // TODO: categories | |
93 | for (int i(0); i < valueCount; i++) |
|
95 | for (int i(0); i < valueCount; i++) | |
94 | categories << QString::number(i); |
|
96 | categories << QString::number(i); | |
95 | QPercentBarSeries* series = new QPercentBarSeries(categories, chart); |
|
97 | QPercentBarSeries* series = new QPercentBarSeries(categories, chart); | |
96 | for (int i(0); i < m_dataTable.count(); i++) { |
|
98 | for (int i(0); i < m_dataTable.count(); i++) { | |
97 | QBarSet *set = new QBarSet("Set" + QString::number(i)); |
|
99 | QBarSet *set = new QBarSet("Set" + QString::number(i)); | |
98 | foreach (Data data, m_dataTable[i]) |
|
100 | foreach (Data data, m_dataTable[i]) | |
99 | *set << data.first.y(); |
|
101 | *set << data.first.y(); | |
100 | series->addBarSet(set); |
|
102 | series->addBarSet(set); | |
101 | } |
|
103 | } | |
102 | chart->addSeries(series); |
|
104 | chart->addSeries(series); | |
103 | } |
|
105 | } | |
104 | m_charts << chart; |
|
106 | m_charts << chart; | |
105 |
|
107 | |||
106 | // line chart |
|
108 | // line chart | |
107 | chart = new QChartView(); |
|
109 | chart = new QChartView(); | |
108 | chart->setChartTitle("line chart"); |
|
110 | chart->setChartTitle("line chart"); | |
109 | chart->setRenderHint(QPainter::Antialiasing); |
|
111 | chart->setRenderHint(QPainter::Antialiasing); | |
110 | baseLayout->addWidget(chart, 1, 2); |
|
112 | baseLayout->addWidget(chart, 1, 2); | |
111 | foreach (DataList list, m_dataTable) { |
|
113 | foreach (DataList list, m_dataTable) { | |
112 | QLineSeries *series = new QLineSeries(chart); |
|
114 | QLineSeries *series = new QLineSeries(chart); | |
113 | foreach (Data data, list) |
|
115 | foreach (Data data, list) | |
114 | series->add(data.first); |
|
116 | series->add(data.first); | |
115 | chart->addSeries(series); |
|
117 | chart->addSeries(series); | |
116 | } |
|
118 | } | |
117 | m_charts << chart; |
|
119 | m_charts << chart; | |
118 |
|
120 | |||
119 | // pie chart |
|
121 | // pie chart | |
120 | chart = new QChartView(); |
|
122 | chart = new QChartView(); | |
121 | chart->setChartTitle("pie chart"); |
|
123 | chart->setChartTitle("pie chart"); | |
122 | chart->setRenderHint(QPainter::Antialiasing); |
|
124 | chart->setRenderHint(QPainter::Antialiasing); | |
123 | baseLayout->addWidget(chart, 2, 0); |
|
125 | baseLayout->addWidget(chart, 2, 0); | |
124 |
|
126 | |||
125 | qreal pieSize = 1.0 / m_dataTable.count(); |
|
127 | qreal pieSize = 1.0 / m_dataTable.count(); | |
126 | for (int i=0; i<m_dataTable.count(); i++) { |
|
128 | for (int i=0; i<m_dataTable.count(); i++) { | |
127 | QPieSeries *series = new QPieSeries(chart); |
|
129 | QPieSeries *series = new QPieSeries(chart); | |
128 | foreach (Data data, m_dataTable[i]) |
|
130 | foreach (Data data, m_dataTable[i]) | |
129 | series->add(data.first.y(), data.second); |
|
131 | series->add(data.first.y(), data.second); | |
130 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); |
|
132 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); | |
131 | series->setPieSize(pieSize); |
|
133 | series->setPieSize(pieSize); | |
132 | series->setPiePosition(hPos, 0.5); |
|
134 | series->setPiePosition(hPos, 0.5); | |
133 | chart->addSeries(series); |
|
135 | chart->addSeries(series); | |
134 | } |
|
136 | } | |
135 | m_charts << chart; |
|
137 | m_charts << chart; | |
136 |
|
138 | |||
137 | // spine chart |
|
139 | // spine chart | |
138 | chart = new QChartView(); |
|
140 | chart = new QChartView(); | |
139 | chart->setChartTitle("spline chart"); |
|
141 | chart->setChartTitle("spline chart"); | |
140 | chart->setRenderHint(QPainter::Antialiasing); |
|
142 | chart->setRenderHint(QPainter::Antialiasing); | |
141 | baseLayout->addWidget(chart, 2, 1); |
|
143 | baseLayout->addWidget(chart, 2, 1); | |
142 | foreach (DataList list, m_dataTable) { |
|
144 | foreach (DataList list, m_dataTable) { | |
143 | QSplineSeries *series = new QSplineSeries(chart); |
|
145 | QSplineSeries *series = new QSplineSeries(chart); | |
144 | foreach (Data data, list) |
|
146 | foreach (Data data, list) | |
145 | series->add(data.first); |
|
147 | series->add(data.first); | |
146 | chart->addSeries(series); |
|
148 | chart->addSeries(series); | |
147 | } |
|
149 | } | |
148 | m_charts << chart; |
|
150 | m_charts << chart; | |
149 |
|
151 | |||
150 | // scatter chart |
|
152 | // scatter chart | |
151 | chart = new QChartView(); |
|
153 | chart = new QChartView(); | |
152 | chart->setChartTitle("scatter chart"); |
|
154 | chart->setChartTitle("scatter chart"); | |
153 | chart->setRenderHint(QPainter::Antialiasing); |
|
155 | chart->setRenderHint(QPainter::Antialiasing); | |
154 | baseLayout->addWidget(chart, 2, 2); |
|
156 | baseLayout->addWidget(chart, 2, 2); | |
155 | foreach (DataList list, m_dataTable) { |
|
157 | foreach (DataList list, m_dataTable) { | |
156 | QScatterSeries *series = new QScatterSeries(chart); |
|
158 | QScatterSeries *series = new QScatterSeries(chart); | |
157 | foreach (Data data, list) |
|
159 | foreach (Data data, list) | |
158 | series->add(data.first); |
|
160 | series->add(data.first); | |
159 | chart->addSeries(series); |
|
161 | chart->addSeries(series); | |
160 | } |
|
162 | } | |
161 | m_charts << chart; |
|
163 | m_charts << chart; | |
162 |
|
164 | |||
163 | setLayout(baseLayout); |
|
165 | setLayout(baseLayout); | |
164 | } |
|
166 | } | |
165 |
|
167 | |||
166 | public Q_SLOTS: |
|
168 | public Q_SLOTS: | |
167 |
|
169 | |||
168 | void updateTheme() |
|
170 | void updateTheme() | |
169 | { |
|
171 | { | |
170 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); |
|
172 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); | |
171 | foreach (QChartView *chart, m_charts) |
|
173 | foreach (QChartView *chart, m_charts) | |
172 | chart->setChartTheme(theme); |
|
174 | chart->setChartTheme(theme); | |
173 | } |
|
175 | } | |
174 |
|
176 | |||
175 | private: |
|
177 | private: | |
176 | QList<QChartView*> m_charts; |
|
178 | QList<QChartView*> m_charts; | |
177 | QComboBox *m_themeComboBox; |
|
179 | QComboBox *m_themeComboBox; | |
178 | DataTable m_dataTable; |
|
180 | DataTable m_dataTable; | |
179 | }; |
|
181 | }; | |
180 |
|
182 | |||
181 | int main(int argc, char *argv[]) |
|
183 | int main(int argc, char *argv[]) | |
182 | { |
|
184 | { | |
183 | QApplication a(argc, argv); |
|
185 | QApplication a(argc, argv); | |
184 |
|
186 | |||
185 | QMainWindow window; |
|
187 | QMainWindow window; | |
186 |
|
188 | |||
187 | MainWidget* widget = new MainWidget(); |
|
189 | MainWidget* widget = new MainWidget(); | |
188 |
|
190 | |||
189 | window.setCentralWidget(widget); |
|
191 | window.setCentralWidget(widget); | |
190 | window.resize(900, 600); |
|
192 | window.resize(900, 600); | |
191 | window.show(); |
|
193 | window.show(); | |
192 |
|
194 | |||
193 | return a.exec(); |
|
195 | return a.exec(); | |
194 | } |
|
196 | } | |
195 |
|
197 | |||
196 | #include "main.moc" |
|
198 | #include "main.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now