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