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