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