@@ -1,225 +1,225 | |||
|
1 | 1 | /*! |
|
2 | 2 | \class Widget |
|
3 | 3 | \brief Ui for the application. |
|
4 | 4 | \internal |
|
5 | 5 | */ |
|
6 | 6 | |
|
7 | 7 | #include "widget.h" |
|
8 | 8 | #include <QGridLayout> |
|
9 | 9 | #include <QPushButton> |
|
10 | 10 | #include <QLabel> |
|
11 | 11 | |
|
12 | 12 | #include <QSqlQuery> |
|
13 | 13 | #include <qscatterseries.h> |
|
14 | 14 | #include <qchartview.h> |
|
15 | 15 | #include <qchartaxis.h> |
|
16 | 16 | #include <qbarcategory.h> |
|
17 | 17 | #include <qbarset.h> |
|
18 | 18 | #include <QListWidget> |
|
19 | 19 | #include <QPrinter> |
|
20 | 20 | #include <QPrintDialog> |
|
21 | 21 | #include <QRadioButton> |
|
22 | 22 | |
|
23 | 23 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
24 | 24 | |
|
25 | 25 | Widget::Widget(QWidget *parent) |
|
26 | 26 | : QWidget(parent) |
|
27 | 27 | { |
|
28 | 28 | setGeometry(100, 100, 1000, 600); |
|
29 | 29 | |
|
30 | 30 | // right panel layout |
|
31 | 31 | barChartRadioButton = new QRadioButton(tr("Bar chart")); |
|
32 | 32 | barChartRadioButton->setChecked(true); |
|
33 | 33 | scatterChartRadioButton = new QRadioButton(tr("Scatter chart")); |
|
34 | 34 | scatterChartRadioButton->setChecked(false); |
|
35 | 35 | countrieslist = new QListWidget; |
|
36 | 36 | countrieslist->setSelectionMode(QAbstractItemView::MultiSelection); |
|
37 | 37 | |
|
38 | 38 | yearslist = new QListWidget; |
|
39 | 39 | yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection); |
|
40 | 40 | for (int i = 1990; i < 2011; i++) |
|
41 | 41 | yearslist->addItem(QString("%1").arg(i)); |
|
42 | 42 | |
|
43 | 43 | QPushButton* refreshButton = new QPushButton(tr("Refresh")); |
|
44 | 44 | connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart())); |
|
45 | 45 | |
|
46 | 46 | QPushButton* printButton = new QPushButton(tr("Print chart")); |
|
47 | 47 | connect(printButton, SIGNAL(clicked()), this, SLOT(printChart())); |
|
48 | 48 | |
|
49 | 49 | QVBoxLayout* rightPanelLayout = new QVBoxLayout; |
|
50 | 50 | rightPanelLayout->addWidget(barChartRadioButton); |
|
51 | 51 | rightPanelLayout->addWidget(scatterChartRadioButton); |
|
52 | 52 | rightPanelLayout->addWidget(countrieslist); |
|
53 | 53 | rightPanelLayout->addWidget(yearslist); |
|
54 | 54 | rightPanelLayout->addWidget(refreshButton); |
|
55 | 55 | rightPanelLayout->addWidget(printButton); |
|
56 | 56 | rightPanelLayout->setStretch(0, 1); |
|
57 | 57 | rightPanelLayout->setStretch(1, 0); |
|
58 | 58 | |
|
59 | 59 | // main layout |
|
60 | 60 | chartArea = new QChartView(this); |
|
61 | 61 | chartArea->setChartTitle("GDP by country"); |
|
62 | 62 | QGridLayout* mainLayout = new QGridLayout; |
|
63 | 63 | mainLayout->addWidget(chartArea, 0, 0); |
|
64 | 64 | mainLayout->addLayout(rightPanelLayout, 0, 1); |
|
65 | 65 | mainLayout->setColumnStretch(0,1); |
|
66 | 66 | setLayout(mainLayout); |
|
67 | 67 | |
|
68 | 68 | // connect to the database |
|
69 | 69 | db = QSqlDatabase::addDatabase("QSQLITE"); |
|
70 | 70 | db.setDatabaseName("gdpData"); |
|
71 | 71 | if(!db.open()) |
|
72 | 72 | { |
|
73 | 73 | qDebug() << "could not open database. SQLite db file missing (?)"; |
|
74 | 74 | return; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | // get the list of all countires and regions. |
|
78 | 78 | QSqlQuery query; |
|
79 | 79 | query.exec("SELECT DISTINCT country FROM gdp2"); |
|
80 | 80 | |
|
81 | 81 | // add the countries to the country filter |
|
82 | 82 | while (query.next()) { |
|
83 | 83 | countrieslist->addItem(query.value(0).toString()); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | // hide axis X labels |
|
87 | 87 | QChartAxis* axis = chartArea->axisX(); |
|
88 | 88 | // axis->setLabelsVisible(false); |
|
89 | 89 | // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); |
|
90 | 90 | |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | Widget::~Widget() |
|
94 | 94 | { |
|
95 | 95 | // |
|
96 | 96 | db.close(); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | /*! |
|
100 | 100 | refreshes the chart |
|
101 | 101 | */ |
|
102 | 102 | void Widget::refreshChart() |
|
103 | 103 | { |
|
104 | 104 | chartArea->removeSeries(series0); |
|
105 | 105 | |
|
106 | 106 | // selected countries items list is not sorted. copy the values to QStringlist and sort them. |
|
107 | 107 | QStringList selectedCountriesStrings; |
|
108 | 108 | QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems(); |
|
109 | 109 | for (int i = 0; i < selectedCountriesItems.size(); i++) |
|
110 | 110 | selectedCountriesStrings.append(selectedCountriesItems[i]->text()); |
|
111 | 111 | selectedCountriesStrings.sort(); |
|
112 | 112 | |
|
113 | 113 | QSqlQuery query; |
|
114 | 114 | // selected years items list is not sorted. copy the values to QList<Integer> and sort them. |
|
115 | 115 | QList<int> selectedYearsInts; |
|
116 | 116 | QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems(); |
|
117 | 117 | for (int i = 0; i < selectedYearsItems.size(); i++) |
|
118 | 118 | selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); |
|
119 | 119 | qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>()); |
|
120 | 120 | |
|
121 | 121 | if (barChartRadioButton->isChecked()) |
|
122 | 122 | { |
|
123 | 123 | // use the sorted selected coutries list to initialize BarCategory |
|
124 | 124 | QBarCategory* category = new QBarCategory; |
|
125 | 125 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
126 | 126 | *category << selectedCountriesStrings[i]; |
|
127 | 127 | series0 = new QBarChartSeries(category); |
|
128 | 128 | |
|
129 | 129 | // prepare the selected counries SQL query |
|
130 | 130 | QString countriesQuery = "country IN ("; |
|
131 | 131 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
132 | 132 | { |
|
133 | 133 | countriesQuery.append("'" + selectedCountriesStrings[i] + "'"); |
|
134 | 134 | if ( i < selectedCountriesStrings.size() - 1) |
|
135 | 135 | countriesQuery.append(","); |
|
136 | 136 | else |
|
137 | 137 | countriesQuery.append(")"); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | // perform a query for each selected year |
|
141 | 141 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
142 | 142 | { |
|
143 | 143 | query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery); |
|
144 | QBarSet* barSet = new QBarSet; | |
|
144 | QBarSet* barSet = new QBarSet("Barset" + QString::number(i)); | |
|
145 | 145 | // while (query.next()) { |
|
146 | 146 | // qDebug() << query.value(0).toString() << " : " << query.value(1).toString(); |
|
147 | 147 | // } |
|
148 | 148 | query.first(); |
|
149 | 149 | |
|
150 | 150 | // the data for some of the coutries for some years might be missing. |
|
151 | 151 | // QBarChart needs bars to have same size |
|
152 | 152 | for (int k = 0; k < selectedCountriesStrings.size(); k++) |
|
153 | 153 | { |
|
154 | 154 | if (selectedCountriesStrings[k] == query.value(0).toString()) |
|
155 | 155 | { |
|
156 | 156 | *barSet << query.value(1).toReal(); |
|
157 | 157 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]); |
|
158 | 158 | query.next(); |
|
159 | 159 | } |
|
160 | 160 | else |
|
161 | 161 | { |
|
162 | 162 | // data missing, put 0 |
|
163 | 163 | *barSet << 0.0f; |
|
164 | 164 | qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]); |
|
165 | 165 | } |
|
166 | 166 | } |
|
167 | 167 | series0->addBarSet(barSet); |
|
168 | 168 | } |
|
169 | 169 | // add the serie to the chart |
|
170 | 170 | chartArea->addSeries(series0); |
|
171 | 171 | |
|
172 | 172 | } |
|
173 | 173 | else if (scatterChartRadioButton->isChecked()) |
|
174 | 174 | { |
|
175 | 175 | QString yearsQuery = "year IN ("; |
|
176 | 176 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
177 | 177 | { |
|
178 | 178 | yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'"); |
|
179 | 179 | if ( i < selectedYearsInts.size() - 1) |
|
180 | 180 | yearsQuery.append(","); |
|
181 | 181 | else |
|
182 | 182 | yearsQuery.append(")"); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | // perform a query for each selected year |
|
186 | 186 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
187 | 187 | { |
|
188 | 188 | query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery); |
|
189 | 189 | query.first(); |
|
190 | 190 | |
|
191 | 191 | QScatterSeries* series = new QScatterSeries; |
|
192 | 192 | // the data for some of the coutries for some years might be missing. |
|
193 | 193 | for (int k = 0; k < selectedYearsInts.size(); k++) |
|
194 | 194 | { |
|
195 | 195 | if (selectedYearsInts[k] == query.value(0).toInt()) |
|
196 | 196 | { |
|
197 | 197 | *series << QPointF(query.value(0).toInt() , query.value(1).toReal()); |
|
198 | 198 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]); |
|
199 | 199 | query.next(); |
|
200 | 200 | } |
|
201 | 201 | else |
|
202 | 202 | { |
|
203 | 203 | // data missing, put 0 |
|
204 | 204 | *series << QPointF(selectedYearsInts[k] , 0.0f); |
|
205 | 205 | qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt(); |
|
206 | 206 | } |
|
207 | 207 | } |
|
208 | 208 | chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); |
|
209 | 209 | chartArea->addSeries(series); |
|
210 | 210 | } |
|
211 | 211 | } |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | void Widget::printChart() |
|
215 | 215 | { |
|
216 | 216 | QPrinter printer; |
|
217 | 217 | // QPrinter printer(QPrinter::HighResolution); |
|
218 | 218 | printer.setOutputFormat(QPrinter::PdfFormat); |
|
219 | 219 | printer.setOrientation(QPrinter::Landscape); |
|
220 | 220 | printer.setOutputFileName("print.pdf"); |
|
221 | 221 | |
|
222 | 222 | QPainter painter; |
|
223 | 223 | painter.begin(&printer); |
|
224 | 224 | chartArea->render(&painter); |
|
225 | 225 | } |
@@ -1,395 +1,369 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include "dataseriedialog.h" |
|
3 | 3 | #include "qchartseries.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "qscatterseries.h" |
|
6 | 6 | #include <qlinechartseries.h> |
|
7 | 7 | #include <qbarset.h> |
|
8 | 8 | #include <qbarcategory.h> |
|
9 | 9 | #include <qbarchartseries.h> |
|
10 | 10 | #include <qstackedbarchartseries.h> |
|
11 | 11 | #include <qpercentbarchartseries.h> |
|
12 | 12 | #include <QPushButton> |
|
13 | 13 | #include <QComboBox> |
|
14 | 14 | #include <QSpinBox> |
|
15 | 15 | #include <QCheckBox> |
|
16 | 16 | #include <QGridLayout> |
|
17 | 17 | #include <QHBoxLayout> |
|
18 | 18 | #include <QLabel> |
|
19 | 19 | #include <QSpacerItem> |
|
20 | 20 | #include <QMessageBox> |
|
21 | 21 | #include <cmath> |
|
22 | 22 | #include <QDebug> |
|
23 | 23 | #include <QStandardItemModel> |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | MainWidget::MainWidget(QWidget *parent) : |
|
29 | 29 | QWidget(parent) |
|
30 | 30 | { |
|
31 | 31 | m_chartWidget = new QChartView(this); |
|
32 | 32 | m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand); |
|
33 | 33 | |
|
34 | 34 | // Grid layout for the controls for configuring the chart widget |
|
35 | 35 | QGridLayout *grid = new QGridLayout(); |
|
36 | 36 | QPushButton *addSeriesButton = new QPushButton("Add series"); |
|
37 | 37 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
38 | 38 | grid->addWidget(addSeriesButton, 0, 1); |
|
39 | 39 | initBackroundCombo(grid); |
|
40 | 40 | initScaleControls(grid); |
|
41 | 41 | initThemeCombo(grid); |
|
42 | 42 | QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); |
|
43 | 43 | connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); |
|
44 | 44 | zoomCheckBox->setChecked(true); |
|
45 | 45 | grid->addWidget(zoomCheckBox, grid->rowCount(), 0); |
|
46 | 46 | // add row with empty label to make all the other rows static |
|
47 | 47 | grid->addWidget(new QLabel(""), grid->rowCount(), 0); |
|
48 | 48 | grid->setRowStretch(grid->rowCount() - 1, 1); |
|
49 | 49 | |
|
50 | 50 | // Another grid layout as a main layout |
|
51 | 51 | QGridLayout *mainLayout = new QGridLayout(); |
|
52 | 52 | mainLayout->addLayout(grid, 0, 0); |
|
53 | 53 | |
|
54 | 54 | // Init series type specific controls |
|
55 | 55 | initPieControls(); |
|
56 | 56 | mainLayout->addLayout(m_pieLayout, 2, 0); |
|
57 | 57 | // Scatter series specific settings |
|
58 | 58 | // m_scatterLayout = new QGridLayout(); |
|
59 | 59 | // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); |
|
60 | 60 | // m_scatterLayout->setEnabled(false); |
|
61 | 61 | // mainLayout->addLayout(m_scatterLayout, 1, 0); |
|
62 | 62 | |
|
63 | 63 | // Add layouts and the chart widget to the main layout |
|
64 | 64 | mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); |
|
65 | 65 | setLayout(mainLayout); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | // Combo box for selecting the chart's background |
|
69 | 69 | void MainWidget::initBackroundCombo(QGridLayout *grid) |
|
70 | 70 | { |
|
71 | 71 | QComboBox *backgroundCombo = new QComboBox(this); |
|
72 | 72 | backgroundCombo->addItem("Color"); |
|
73 | 73 | backgroundCombo->addItem("Gradient"); |
|
74 | 74 | backgroundCombo->addItem("Image"); |
|
75 | 75 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
76 | 76 | this, SLOT(backgroundChanged(int))); |
|
77 | 77 | |
|
78 | 78 | grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0); |
|
79 | 79 | grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1); |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | // Scale related controls (auto-scale vs. manual min-max values) |
|
83 | 83 | void MainWidget::initScaleControls(QGridLayout *grid) |
|
84 | 84 | { |
|
85 | 85 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
86 | 86 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
87 | 87 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
88 | 88 | m_xMinSpin = new QSpinBox(); |
|
89 | 89 | m_xMinSpin->setMinimum(INT_MIN); |
|
90 | 90 | m_xMinSpin->setMaximum(INT_MAX); |
|
91 | 91 | m_xMinSpin->setValue(0); |
|
92 | 92 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
93 | 93 | m_xMaxSpin = new QSpinBox(); |
|
94 | 94 | m_xMaxSpin->setMinimum(INT_MIN); |
|
95 | 95 | m_xMaxSpin->setMaximum(INT_MAX); |
|
96 | 96 | m_xMaxSpin->setValue(10); |
|
97 | 97 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
98 | 98 | m_yMinSpin = new QSpinBox(); |
|
99 | 99 | m_yMinSpin->setMinimum(INT_MIN); |
|
100 | 100 | m_yMinSpin->setMaximum(INT_MAX); |
|
101 | 101 | m_yMinSpin->setValue(0); |
|
102 | 102 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
103 | 103 | m_yMaxSpin = new QSpinBox(); |
|
104 | 104 | m_yMaxSpin->setMinimum(INT_MIN); |
|
105 | 105 | m_yMaxSpin->setMaximum(INT_MAX); |
|
106 | 106 | m_yMaxSpin->setValue(10); |
|
107 | 107 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
108 | 108 | |
|
109 | 109 | grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0); |
|
110 | 110 | grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0); |
|
111 | 111 | grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1); |
|
112 | 112 | grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0); |
|
113 | 113 | grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1); |
|
114 | 114 | grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0); |
|
115 | 115 | grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1); |
|
116 | 116 | grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0); |
|
117 | 117 | grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1); |
|
118 | 118 | |
|
119 | 119 | m_autoScaleCheck->setChecked(true); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | // Combo box for selecting theme |
|
123 | 123 | void MainWidget::initThemeCombo(QGridLayout *grid) |
|
124 | 124 | { |
|
125 | 125 | QComboBox *chartTheme = new QComboBox(); |
|
126 | 126 | chartTheme->addItem("Default"); |
|
127 | 127 | chartTheme->addItem("Vanilla"); |
|
128 | 128 | chartTheme->addItem("Icy"); |
|
129 | 129 | chartTheme->addItem("Grayscale"); |
|
130 | 130 | chartTheme->addItem("Scientific"); |
|
131 | 131 | chartTheme->addItem("Unnamed1"); |
|
132 | 132 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
133 | 133 | this, SLOT(changeChartTheme(int))); |
|
134 | 134 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); |
|
135 | 135 | grid->addWidget(chartTheme, 8, 1); |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | void MainWidget::initPieControls() |
|
139 | 139 | { |
|
140 | 140 | // Pie series specific settings |
|
141 | 141 | // Pie size factory |
|
142 | 142 | QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox(); |
|
143 | 143 | pieSizeSpin->setMinimum(LONG_MIN); |
|
144 | 144 | pieSizeSpin->setMaximum(LONG_MAX); |
|
145 | 145 | pieSizeSpin->setValue(1.0); |
|
146 | 146 | pieSizeSpin->setSingleStep(0.1); |
|
147 | 147 | connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double))); |
|
148 | 148 | // Pie position |
|
149 | 149 | QComboBox *piePosCombo = new QComboBox(this); |
|
150 | 150 | piePosCombo->addItem("Maximized"); |
|
151 | 151 | piePosCombo->addItem("Top left"); |
|
152 | 152 | piePosCombo->addItem("Top right"); |
|
153 | 153 | piePosCombo->addItem("Bottom left"); |
|
154 | 154 | piePosCombo->addItem("Bottom right"); |
|
155 | 155 | connect(piePosCombo, SIGNAL(currentIndexChanged(int)), |
|
156 | 156 | this, SLOT(setPiePosition(int))); |
|
157 | 157 | m_pieLayout = new QGridLayout(); |
|
158 | 158 | m_pieLayout->setEnabled(false); |
|
159 | 159 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); |
|
160 | 160 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); |
|
161 | 161 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); |
|
162 | 162 | m_pieLayout->addWidget(piePosCombo, 1, 1); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | void MainWidget::addSeries() |
|
166 | 166 | { |
|
167 | 167 | DataSerieDialog dialog(m_defaultSeriesName, this); |
|
168 | 168 | connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)), |
|
169 | 169 | this, SLOT(addSeries(QString, int, int, QString, bool))); |
|
170 | 170 | dialog.exec(); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics) |
|
174 | 174 | { |
|
175 | 175 | // TODO: dataCharacteristics |
|
176 | 176 | QList<RealList> testData; |
|
177 | 177 | for (int j(0); j < columnCount; j++) { |
|
178 | 178 | QList <qreal> newColumn; |
|
179 | 179 | for (int i(0); i < rowCount; i++) { |
|
180 | 180 | if (dataCharacteristics == "Sin") { |
|
181 | 181 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100)); |
|
182 | 182 | } else if (dataCharacteristics == "Sin + random") { |
|
183 | 183 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); |
|
184 | 184 | } else if (dataCharacteristics == "Random") { |
|
185 | 185 | newColumn.append(rand() % 5); |
|
186 | 186 | } else if (dataCharacteristics == "Linear") { |
|
187 | 187 | //newColumn.append(i * (j + 1.0)); |
|
188 | 188 | // TODO: temporary hack to make pie work; prevent zero values: |
|
189 | 189 | newColumn.append(i * (j + 1.0) + 0.1); |
|
190 | 190 | } else { // "constant" |
|
191 | 191 | newColumn.append((j + 1.0)); |
|
192 | 192 | } |
|
193 | 193 | } |
|
194 | 194 | testData.append(newColumn); |
|
195 | 195 | } |
|
196 | 196 | return testData; |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | QStringList MainWidget::generateLabels(int count) |
|
200 | 200 | { |
|
201 | 201 | QStringList result; |
|
202 | 202 | for (int i(0); i < count; i++) |
|
203 | 203 | result.append("label" + QString::number(i)); |
|
204 | 204 | return result; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled) |
|
208 | 208 | { |
|
209 | 209 | qDebug() << "addSeries: " << seriesName |
|
210 | 210 | << " columnCount: " << columnCount |
|
211 | 211 | << " rowCount: " << rowCount |
|
212 | 212 | << " dataCharacteristics: " << dataCharacteristics |
|
213 | 213 | << " labels enabled: " << labelsEnabled; |
|
214 | 214 | m_defaultSeriesName = seriesName; |
|
215 | 215 | |
|
216 | 216 | QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics); |
|
217 | 217 | |
|
218 | 218 | // Line series and scatter series use similar data |
|
219 | 219 | if (seriesName.contains("line", Qt::CaseInsensitive)) { |
|
220 | 220 | for (int j(0); j < data.count(); j ++) { |
|
221 | 221 | QList<qreal> column = data.at(j); |
|
222 | 222 | QLineChartSeries *series = new QLineChartSeries(); |
|
223 | 223 | for (int i(0); i < column.count(); i++) { |
|
224 | 224 | series->add(i, column.at(i)); |
|
225 | 225 | } |
|
226 | 226 | m_chartWidget->addSeries(series); |
|
227 | 227 | setCurrentSeries(series); |
|
228 | 228 | } |
|
229 | 229 | } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) { |
|
230 | 230 | for (int j(0); j < data.count(); j++) { |
|
231 | 231 | QList<qreal> column = data.at(j); |
|
232 | 232 | QScatterSeries *series = new QScatterSeries(); |
|
233 | 233 | for (int i(0); i < column.count(); i++) { |
|
234 | 234 | (*series) << QPointF(i, column.at(i)); |
|
235 | 235 | } |
|
236 | 236 | m_chartWidget->addSeries(series); |
|
237 | 237 | setCurrentSeries(series); |
|
238 | 238 | } |
|
239 | 239 | } else if (seriesName.contains("pie", Qt::CaseInsensitive)) { |
|
240 | 240 | QStringList labels = generateLabels(rowCount); |
|
241 | 241 | for (int j(0); j < data.count(); j++) { |
|
242 | 242 | QPieSeries *series = new QPieSeries(); |
|
243 | 243 | QList<qreal> column = data.at(j); |
|
244 | 244 | for (int i(0); i < column.count(); i++) { |
|
245 | 245 | series->add(column.at(i), labels.at(i)); |
|
246 | 246 | } |
|
247 | 247 | m_chartWidget->addSeries(series); |
|
248 | 248 | setCurrentSeries(series); |
|
249 | 249 | } |
|
250 |
} else if (seriesName == "Bar" |
|
|
250 | } else if (seriesName == "Bar" | |
|
251 | || seriesName == "Stacked bar" | |
|
252 | || seriesName == "Percent bar") { | |
|
251 | 253 | // TODO: replace QBarCategory with QStringList? |
|
252 | 254 | QBarCategory *category = new QBarCategory; |
|
253 | 255 | QStringList labels = generateLabels(rowCount); |
|
254 | 256 | foreach(QString label, labels) |
|
255 | 257 | *category << label; |
|
256 |
QBarChartSeries* series = |
|
|
258 | QBarChartSeries* series = 0; | |
|
259 | if (seriesName == "Bar") | |
|
260 | series = new QBarChartSeries(category, this); | |
|
261 | else if (seriesName == "Stacked bar") | |
|
262 | series = new QStackedBarChartSeries(category, this); | |
|
263 | else | |
|
264 | series = new QPercentBarChartSeries(category, this); | |
|
257 | 265 | |
|
258 | 266 | for (int j(0); j < data.count(); j++) { |
|
259 | 267 | QList<qreal> column = data.at(j); |
|
260 | QBarSet *set = new QBarSet; | |
|
261 | for (int i(0); i < column.count(); i++) { | |
|
262 | *set << column.at(i); | |
|
263 | } | |
|
264 | series->addBarSet(set); | |
|
265 | } | |
|
266 | m_chartWidget->addSeries(series); | |
|
267 | setCurrentSeries(series); | |
|
268 | } else if (seriesName == "Stacked bar") { | |
|
269 | QBarCategory *category = new QBarCategory; | |
|
270 | QStringList labels = generateLabels(rowCount); | |
|
271 | foreach(QString label, labels) | |
|
272 | *category << label; | |
|
273 | QStackedBarChartSeries* series = new QStackedBarChartSeries(category, this); | |
|
274 | ||
|
275 | for (int j(0); j < data.count(); j++) { | |
|
276 | QList<qreal> column = data.at(j); | |
|
277 | QBarSet *set = new QBarSet; | |
|
278 | for (int i(0); i < column.count(); i++) { | |
|
279 | *set << column.at(i); | |
|
280 | } | |
|
281 | series->addBarSet(set); | |
|
282 | } | |
|
283 | m_chartWidget->addSeries(series); | |
|
284 | setCurrentSeries(series); | |
|
285 | } else if (seriesName == "Percent bar") { | |
|
286 | QBarCategory *category = new QBarCategory; | |
|
287 | QStringList labels = generateLabels(rowCount); | |
|
288 | foreach(QString label, labels) | |
|
289 | *category << label; | |
|
290 | QPercentBarChartSeries* series = new QPercentBarChartSeries(category, this); | |
|
291 | ||
|
292 | for (int j(0); j < data.count(); j++) { | |
|
293 | QList<qreal> column = data.at(j); | |
|
294 | QBarSet *set = new QBarSet; | |
|
268 | QBarSet *set = new QBarSet("set" + QString::number(j)); | |
|
295 | 269 | for (int i(0); i < column.count(); i++) { |
|
296 | 270 | *set << column.at(i); |
|
297 | 271 | } |
|
298 | 272 | series->addBarSet(set); |
|
299 | 273 | } |
|
300 | 274 | m_chartWidget->addSeries(series); |
|
301 | 275 | setCurrentSeries(series); |
|
302 | 276 | } |
|
303 | 277 | |
|
304 | 278 | // TODO: spline and area |
|
305 | 279 | } |
|
306 | 280 | |
|
307 | 281 | void MainWidget::setCurrentSeries(QChartSeries *series) |
|
308 | 282 | { |
|
309 | 283 | if (series) { |
|
310 | 284 | m_currentSeries = series; |
|
311 | 285 | switch (m_currentSeries->type()) { |
|
312 | 286 | case QChartSeries::SeriesTypeLine: |
|
313 | 287 | break; |
|
314 | 288 | case QChartSeries::SeriesTypeScatter: |
|
315 | 289 | break; |
|
316 | 290 | case QChartSeries::SeriesTypePie: |
|
317 | 291 | break; |
|
318 | 292 | case QChartSeries::SeriesTypeBar: |
|
319 | 293 | qDebug() << "setCurrentSeries (bar)"; |
|
320 | 294 | break; |
|
321 | 295 | case QChartSeries::SeriesTypeStackedBar: |
|
322 | 296 | qDebug() << "setCurrentSeries (Stackedbar)"; |
|
323 | 297 | break; |
|
324 | 298 | case QChartSeries::SeriesTypePercentBar: |
|
325 | 299 | qDebug() << "setCurrentSeries (Percentbar)"; |
|
326 | 300 | break; |
|
327 | 301 | default: |
|
328 | 302 | Q_ASSERT(false); |
|
329 | 303 | break; |
|
330 | 304 | } |
|
331 | 305 | } |
|
332 | 306 | } |
|
333 | 307 | |
|
334 | 308 | void MainWidget::backgroundChanged(int itemIndex) |
|
335 | 309 | { |
|
336 | 310 | qDebug() << "backgroundChanged: " << itemIndex; |
|
337 | 311 | } |
|
338 | 312 | |
|
339 | 313 | void MainWidget::autoScaleChanged(int value) |
|
340 | 314 | { |
|
341 | 315 | if (value) { |
|
342 | 316 | // TODO: enable auto scaling |
|
343 | 317 | } else { |
|
344 | 318 | // TODO: set scaling manually (and disable auto scaling) |
|
345 | 319 | } |
|
346 | 320 | |
|
347 | 321 | m_xMinSpin->setEnabled(!value); |
|
348 | 322 | m_xMaxSpin->setEnabled(!value); |
|
349 | 323 | m_yMinSpin->setEnabled(!value); |
|
350 | 324 | m_yMaxSpin->setEnabled(!value); |
|
351 | 325 | } |
|
352 | 326 | |
|
353 | 327 | void MainWidget::xMinChanged(int value) |
|
354 | 328 | { |
|
355 | 329 | qDebug() << "xMinChanged: " << value; |
|
356 | 330 | } |
|
357 | 331 | |
|
358 | 332 | void MainWidget::xMaxChanged(int value) |
|
359 | 333 | { |
|
360 | 334 | qDebug() << "xMaxChanged: " << value; |
|
361 | 335 | } |
|
362 | 336 | |
|
363 | 337 | void MainWidget::yMinChanged(int value) |
|
364 | 338 | { |
|
365 | 339 | qDebug() << "yMinChanged: " << value; |
|
366 | 340 | } |
|
367 | 341 | |
|
368 | 342 | void MainWidget::yMaxChanged(int value) |
|
369 | 343 | { |
|
370 | 344 | qDebug() << "yMaxChanged: " << value; |
|
371 | 345 | } |
|
372 | 346 | |
|
373 | 347 | void MainWidget::changeChartTheme(int themeIndex) |
|
374 | 348 | { |
|
375 | 349 | qDebug() << "changeChartTheme: " << themeIndex; |
|
376 | 350 | m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex); |
|
377 | 351 | //TODO: remove this hack. This is just to make it so that theme change is seen immediately. |
|
378 | 352 | QSize s = size(); |
|
379 | 353 | s.setWidth(s.width()+1); |
|
380 | 354 | resize(s); |
|
381 | 355 | } |
|
382 | 356 | |
|
383 | 357 | void MainWidget::setPieSizeFactor(double size) |
|
384 | 358 | { |
|
385 | 359 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
386 | 360 | if (pie) |
|
387 | 361 | pie->setSizeFactor(qreal(size)); |
|
388 | 362 | } |
|
389 | 363 | |
|
390 | 364 | void MainWidget::setPiePosition(int position) |
|
391 | 365 | { |
|
392 | 366 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
393 | 367 | if (pie) |
|
394 | 368 | pie->setPosition((QPieSeries::PiePosition) position); |
|
395 | 369 | } |
General Comments 0
You need to be logged in to leave comments.
Login now