##// END OF EJS Templates
replaced qbarcategory with qstringlist
sauimone -
r377:a61a7697be62
parent child
Show More
@@ -1,69 +1,69
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartview.h>
4 4 #include <qbarseries.h>
5 5 #include <qbarset.h>
6 #include <qbarcategory.h>
6 #include <QStringList>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 int main(int argc, char *argv[])
11 11 {
12 12 QApplication a(argc, argv);
13 13 QMainWindow window;
14 14
15 15 //! [1]
16 // Create category
17 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
16 // Define categories
17 QStringList catecories;
18 catecories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19 19 //! [1]
20 20
21 21 //! [2]
22 22 // Create some test sets for chat
23 23
24 24 QBarSet *set0 = new QBarSet("Bub");
25 25 QBarSet *set1 = new QBarSet("Bob");
26 26 QBarSet *set2 = new QBarSet("Guybrush");
27 27 QBarSet *set3 = new QBarSet("Larry");
28 28 QBarSet *set4 = new QBarSet("Zak");
29 29
30 30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
31 31 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
32 32 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
33 33 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
34 34 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35 35 //! [2]
36 36
37 37 //! [3]
38 38 // Create series and add sets to it
39 QBarSeries* series= new QBarSeries(category);
39 QBarSeries* series= new QBarSeries(catecories);
40 40
41 41 series->addBarSet(set0);
42 42 series->addBarSet(set1);
43 43 series->addBarSet(set2);
44 44 series->addBarSet(set3);
45 45 series->addBarSet(set4);
46 46 //! [3]
47 47
48 48 //! [4]
49 49 // Enable some features
50 50 series->setToolTipEnabled();
51 51 series->setFloatingValuesEnabled();
52 52 //! [4]
53 53
54 54 //! [5]
55 55 // Create view for chart and add series to it. Apply theme.
56 56
57 57 QChartView* chartView = new QChartView(&window);
58 58 chartView->addSeries(series);
59 59 chartView->setChartTitle("simple stacked barchart");
60 60 chartView->setChartTheme(QChart::ChartThemeIcy);
61 61 //! [5]
62 62
63 63 window.setCentralWidget(chartView);
64 64 window.resize(600, 300);
65 65 window.show();
66 66
67 67 return a.exec();
68 68 }
69 69
@@ -1,83 +1,83
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qlineseries.h>
6 6 #include <qscatterseries.h>
7 7 #include <qbarseries.h>
8 8 #include <qbarset.h>
9 #include <qbarcategory.h>
10 9 #include <qpieseries.h>
10 #include <QStringList>
11 11
12 12 QTCOMMERCIALCHART_USE_NAMESPACE
13 13
14 14 int main(int argc, char *argv[])
15 15 {
16 16 QApplication a(argc, argv);
17 17
18 18 //! [1]
19 19 // Create chart view
20 20 QChartView *chartView = new QChartView();
21 21 chartView->setRenderHint(QPainter::Antialiasing);
22 22 chartView->setChartTitle("Simple Line Chart");
23 23 // Add series to the chart
24 24 QLineSeries *line = new QLineSeries();
25 25 line->add(0.0, 0.8);
26 26 line->add(1.1, 1.1);
27 27 line->add(2.0, 2.5);
28 28 chartView->addSeries(line);
29 29 //! [1]
30 30
31 31 chartView->setChartTitle("\'Scietific\' theme");
32 32 //! [2]
33 33 // Change theme
34 34 chartView->setChartTheme(QChart::ChartThemeScientific);
35 35 //! [2]
36 36
37 37 chartView->setChartTitle("Simple Pie Chart");
38 38 //! [3]
39 39 // Add pie series
40 40 // ...
41 41 QPieSeries *pie = new QPieSeries();
42 42 pie->add(3.4, "slice1");
43 43 pie->add(6.7, "slice2");
44 44 chartView->addSeries(pie);
45 45 //! [3]
46 46
47 47 chartView->setChartTitle("Simple Scatter Chart");
48 48 //! [4]
49 49 // Add scatter series
50 50 // ...
51 51 QScatterSeries *scatter = new QScatterSeries();
52 52 for (qreal x(0); x < 100; x += 0.5) {
53 53 qreal y = rand() % 100;
54 54 *(scatter) << QPointF(x, y);
55 55 }
56 56 chartView->addSeries(scatter);
57 57 //! [4]
58 58
59 59 chartView->setChartTitle("Simple Bar Chart");
60 60 //! [5]
61 61 // ...
62 62 // Add bar series
63 QBarCategory *barCategory = new QBarCategory();
64 *barCategory << "Jan"
63 QStringList barCategory;
64 barCategory << "Jan"
65 65 << "Feb"
66 66 << "Mar";
67 67 QBarSeries *bar = new QBarSeries(barCategory);
68 68 QBarSet *barSet = new QBarSet("Sales");
69 69 *barSet << 123.2
70 70 << 301.3
71 71 << 285.8;
72 72 bar->addBarSet(barSet);
73 73 chartView->addSeries(bar);
74 74 //! [5]
75 75
76 76 QMainWindow w;
77 77 w.resize(400, 300);
78 78 w.setCentralWidget(chartView);
79 79 w.setWindowFlags(Qt::FramelessWindowHint);
80 80 w.show();
81 81
82 82 return a.exec();
83 83 }
@@ -1,227 +1,227
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 #include <qbarcategory.h>
17 16 #include <qbarset.h>
18 17 #include <QListWidget>
19 18 #include <QPrinter>
20 19 #include <QPrintDialog>
21 20 #include <QRadioButton>
21 #include <QStringList>
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->removeAllSeries();
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 QBarCategory* category = new QBarCategory;
124 QStringList category;
125 125 for (int i = 0; i < selectedCountriesStrings.size(); i++)
126 *category << selectedCountriesStrings[i];
126 category << selectedCountriesStrings[i];
127 127 series0 = new QBarSeries(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 144 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
145 145
146 146 // while (query.next()) {
147 147 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
148 148 // }
149 149 query.first();
150 150
151 151 // the data for some of the coutries for some years might be missing.
152 152 // QBarChart needs bars to have same size
153 153 for (int k = 0; k < selectedCountriesStrings.size(); k++)
154 154 {
155 155 if (selectedCountriesStrings[k] == query.value(0).toString())
156 156 {
157 157 *barSet << query.value(1).toReal();
158 158 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
159 159 query.next();
160 160 }
161 161 else
162 162 {
163 163 // data missing, put 0
164 164 *barSet << 0.0f;
165 165 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]);
166 166 }
167 167 }
168 168 series0->addBarSet(barSet);
169 169 }
170 170 // add the serie to the chart
171 171 chartArea->addSeries(series0);
172 172
173 173 }
174 174 else if (scatterChartRadioButton->isChecked())
175 175 {
176 176 QString yearsQuery = "year IN (";
177 177 for (int i = 0; i < selectedYearsInts.size(); i++)
178 178 {
179 179 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
180 180 if ( i < selectedYearsInts.size() - 1)
181 181 yearsQuery.append(",");
182 182 else
183 183 yearsQuery.append(")");
184 184 }
185 185
186 186 // perform a query for each selected year
187 187 for (int i = 0; i < selectedCountriesStrings.size(); i++)
188 188 {
189 189 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
190 190 query.first();
191 191
192 192 QScatterSeries* series = new QScatterSeries;
193 193 // the data for some of the coutries for some years might be missing.
194 194 for (int k = 0; k < selectedYearsInts.size(); k++)
195 195 {
196 196 if (selectedYearsInts[k] == query.value(0).toInt())
197 197 {
198 198 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
199 199 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
200 200 query.next();
201 201 }
202 202 else
203 203 {
204 204 // data missing, put 0
205 205 *series << QPointF(selectedYearsInts[k] , 0.0f);
206 206 qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
207 207 }
208 208 }
209 209 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
210 210 chartArea->addSeries(series);
211 211 }
212 212 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
213 213 }
214 214 }
215 215
216 216 void Widget::printChart()
217 217 {
218 218 QPrinter printer;
219 219 // QPrinter printer(QPrinter::HighResolution);
220 220 printer.setOutputFormat(QPrinter::PdfFormat);
221 221 printer.setOrientation(QPrinter::Landscape);
222 222 printer.setOutputFileName("print.pdf");
223 223
224 224 QPainter painter;
225 225 painter.begin(&printer);
226 226 chartArea->render(&painter);
227 227 }
@@ -1,69 +1,69
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <QStandardItemModel>
4 4 #include <qpercentbarseries.h>
5 #include <qbarcategory.h>
6 5 #include <qchartview.h>
7 6 #include <qbarset.h>
7 #include <QStringList>
8 8
9 9 QTCOMMERCIALCHART_USE_NAMESPACE
10 10
11 11 int main(int argc, char *argv[])
12 12 {
13 13 QApplication a(argc, argv);
14 14 QMainWindow window;
15 15
16 16 //! [1]
17 // Create category
18 QBarCategory *category = new QBarCategory;
19 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
17 // Define categories
18 QStringList categories;
19 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
20 20 //! [1]
21 21
22 22 //! [2]
23 23 // Create some test sets for chat
24 24 QBarSet *set0 = new QBarSet("Bub");
25 25 QBarSet *set1 = new QBarSet("Bob");
26 26 QBarSet *set2 = new QBarSet("Guybrush");
27 27 QBarSet *set3 = new QBarSet("Larry");
28 28 QBarSet *set4 = new QBarSet("Zak");
29 29
30 30 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
31 31 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
32 32 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
33 33 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
34 34 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
35 35 //! [2]
36 36
37 37 //! [3]
38 38 // Create series and add sets to it
39 QPercentBarSeries* series = new QPercentBarSeries(category);
39 QPercentBarSeries* series = new QPercentBarSeries(categories);
40 40
41 41 series->addBarSet(set0);
42 42 series->addBarSet(set1);
43 43 series->addBarSet(set2);
44 44 series->addBarSet(set3);
45 45 series->addBarSet(set4);
46 46 //! [3]
47 47
48 48 //! [4]
49 49 // Enable features
50 50 series->setToolTipEnabled();
51 51 series->setFloatingValuesEnabled();
52 52 //! [4]
53 53
54 54 //! [5]
55 55 // Create view for chart and add series to it. Apply theme.
56 56
57 57 QChartView* chartView = new QChartView(&window);
58 58 chartView->addSeries(series);
59 59 chartView->setChartTitle("simple percent barchart");
60 60 chartView->setChartTheme(QChart::ChartThemeIcy);
61 61 //! [5]
62 62
63 63 window.setCentralWidget(chartView);
64 64 window.resize(400, 300);
65 65 window.show();
66 66
67 67 return a.exec();
68 68 }
69 69
@@ -1,68 +1,68
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartview.h>
4 4 #include <qstackedbarseries.h>
5 5 #include <qbarset.h>
6 #include <qbarcategory.h>
6 #include <QStringList>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 int main(int argc, char *argv[])
11 11 {
12 12 QApplication a(argc, argv);
13 13 QMainWindow window;
14 14
15 15 //! [1]
16 // Create category
17 QBarCategory *category = new QBarCategory;
18 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
16 // Define categories
17 QStringList catecories;
18 catecories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
19 19 //! [1]
20 20
21 21 //! [2]
22 22 // Create some test sets for chat
23 23 QBarSet *set0 = new QBarSet("Bub");
24 24 QBarSet *set1 = new QBarSet("Bob");
25 25 QBarSet *set2 = new QBarSet("Guybrush");
26 26 QBarSet *set3 = new QBarSet("Larry");
27 27 QBarSet *set4 = new QBarSet("Zak");
28 28
29 29 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
30 30 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
31 31 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
32 32 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
33 33 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
34 34 //! [2]
35 35
36 36 //! [3]
37 37 // Create series and add sets to it
38 QStackedBarSeries* series = new QStackedBarSeries(category);
38 QStackedBarSeries* series = new QStackedBarSeries(catecories);
39 39
40 40 series->addBarSet(set0);
41 41 series->addBarSet(set1);
42 42 series->addBarSet(set2);
43 43 series->addBarSet(set3);
44 44 series->addBarSet(set4);
45 45 //! [3]
46 46
47 47 //! [4]
48 48 // Enable features
49 49 series->setToolTipEnabled();
50 50 series->setFloatingValuesEnabled();
51 51 //! [4]
52 52
53 53 //! [5]
54 54 // Create view for chart and add series to it. Apply theme.
55 55
56 56 QChartView* chartView = new QChartView(&window);
57 57 chartView->addSeries(series);
58 58 chartView->setChartTitle("simple stacked barchart");
59 59 chartView->setChartTheme(QChart::ChartThemeIcy);
60 60 //! [5]
61 61
62 62 window.setCentralWidget(chartView);
63 63 window.resize(400, 300);
64 64 window.show();
65 65
66 66 return a.exec();
67 67 }
68 68
@@ -1,38 +1,36
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/bar.cpp \
6 6 $$PWD/barchartmodel.cpp \
7 7 $$PWD/barlabel.cpp \
8 8 $$PWD/barpresenter.cpp \
9 9 $$PWD/barpresenterbase.cpp \
10 10 $$PWD/percentbarpresenter.cpp \
11 $$PWD/qbarcategory.cpp \
12 11 $$PWD/qbarseries.cpp \
13 12 $$PWD/qbarset.cpp \
14 13 $$PWD/qpercentbarseries.cpp \
15 14 $$PWD/qstackedbarseries.cpp \
16 15 $$PWD/separator.cpp \
17 16 $$PWD/stackedbarpresenter.cpp \
18 17 $$PWD/barvalue.cpp
19 18
20 19 PRIVATE_HEADERS += \
21 20 $$PWD/bar_p.h \
22 21 $$PWD/barchartmodel_p.h \
23 22 $$PWD/barlabel_p.h \
24 23 $$PWD/barpresenter.h \
25 24 $$PWD/barpresenterbase.h \
26 25 $$PWD/percentbarpresenter.h \
27 26 $$PWD/separator_p.h \
28 27 $$PWD/stackedbarpresenter.h \
29 28 $$PWD/barvalue_p.h
30 29
31 30 PUBLIC_HEADERS += \
32 $$PWD/qbarcategory.h \
33 31 $$PWD/qbarseries.h \
34 32 $$PWD/qbarset.h \
35 33 $$PWD/qpercentbarseries.h \
36 34 $$PWD/qstackedbarseries.h
37 35
38 36
@@ -1,190 +1,183
1 1 #include <limits.h>
2 2 #include <QVector>
3 3 #include <QDebug>
4 4 #include "barchartmodel_p.h"
5 #include "qbarcategory.h"
6 5 #include "qbarset.h"
7 6
8 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 8
10 BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) :
9 BarChartModel::BarChartModel(QStringList categories, QObject *parent) :
11 10 QObject(parent)
12 ,mCategory(category)
11 ,mCategory(categories)
13 12 {
14 13 }
15 14
16 BarChartModel::~BarChartModel()
15 QStringList BarChartModel::category()
17 16 {
18 delete mCategory;
19 }
20
21
22 QBarCategory& BarChartModel::category()
23 {
24 return *mCategory;
17 return mCategory;
25 18 }
26 19
27 20 void BarChartModel::addBarSet(QBarSet *set)
28 21 {
29 22 mDataModel.append(set);
30 23 }
31 24
32 25 void BarChartModel::removeBarSet(QBarSet *set)
33 26 {
34 27 if (mDataModel.contains(set)) {
35 28 mDataModel.removeOne(set);
36 29 }
37 30 }
38 31
39 32 QBarSet* BarChartModel::setAt(int index)
40 33 {
41 34 return mDataModel.at(index);
42 35 }
43 36
44 37 QList<QBarSet*> BarChartModel::barSets()
45 38 {
46 39 return mDataModel;
47 40 }
48 41
49 42 QList<QString> BarChartModel::legend()
50 43 {
51 44 QList<QString> legend;
52 45
53 46 for (int i=0; i<mDataModel.count(); i++) {
54 47 legend.append(mDataModel.at(i)->name());
55 48 }
56 49 return legend;
57 50 }
58 51
59 52 int BarChartModel::countSets()
60 53 {
61 54 return mDataModel.count();
62 55 }
63 56
64 57 int BarChartModel::countCategories()
65 58 {
66 59 int count(0);
67 60 for (int i=0; i<mDataModel.count(); i++){
68 61 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
69 62 int temp = mDataModel.at(i)->count();
70 63 if (temp > count) {
71 64 count = temp;
72 65 }
73 66 }
74 67 return count;
75 68 }
76 69
77 70 int BarChartModel::countTotalItems()
78 71 {
79 72 int total = mDataModel.count() * countCategories();
80 73 return total;
81 74 }
82 75
83 76 qreal BarChartModel::min()
84 77 {
85 78 Q_ASSERT(mDataModel.count() > 0);
86 79 // TODO: make min and max members and update them when data changes.
87 80 // This is slower since they are checked every time, even if data is same since previous call.
88 81 qreal min = INT_MAX;
89 82
90 83 for (int i=0; i <mDataModel.count(); i++) {
91 84 int itemCount = mDataModel.at(i)->count();
92 85 for (int j=0; j<itemCount; j++) {
93 86 qreal temp = mDataModel.at(i)->valueAt(j);
94 87 if (temp < min) {
95 88 min = temp;
96 89 }
97 90 }
98 91 }
99 92 return min;
100 93 }
101 94
102 95 qreal BarChartModel::max()
103 96 {
104 97 Q_ASSERT(mDataModel.count() > 0);
105 98
106 99 // TODO: make min and max members and update them when data changes.
107 100 // This is slower since they are checked every time, even if data is same since previous call.
108 101 qreal max = INT_MIN;
109 102
110 103 for (int i=0; i <mDataModel.count(); i++) {
111 104 int itemCount = mDataModel.at(i)->count();
112 105 for (int j=0; j<itemCount; j++) {
113 106 qreal temp = mDataModel.at(i)->valueAt(j);
114 107 if (temp > max) {
115 108 max = temp;
116 109 }
117 110 }
118 111 }
119 112
120 113 return max;
121 114 }
122 115
123 116 qreal BarChartModel::valueAt(int set, int category)
124 117 {
125 118 if ((set < 0) || (set >= mDataModel.count())) {
126 119 // No set, no value.
127 120 return 0;
128 121 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
129 122 // No category, no value.
130 123 return 0;
131 124 }
132 125
133 126 return mDataModel.at(set)->valueAt(category);
134 127 }
135 128
136 129 qreal BarChartModel::percentageAt(int set, int category)
137 130 {
138 131 if ((set < 0) || (set >= mDataModel.count())) {
139 132 // No set, no value.
140 133 return 0;
141 134 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
142 135 // No category, no value.
143 136 return 0;
144 137 }
145 138
146 139 qreal value = mDataModel.at(set)->valueAt(category);
147 140 qreal total = categorySum(category);
148 141 if (0 == total) {
149 142 return 100.0;
150 143 }
151 144
152 145 return value / total;
153 146 }
154 147
155 148
156 149 qreal BarChartModel::categorySum(int category)
157 150 {
158 151 qreal sum(0);
159 152 int count = mDataModel.count(); // Count sets
160 153
161 154 for (int set = 0; set < count; set++) {
162 155 if (category < mDataModel.at(set)->count()) {
163 156 sum += mDataModel.at(set)->valueAt(category);
164 157 }
165 158 }
166 159 return sum;
167 160 }
168 161
169 162 qreal BarChartModel::maxCategorySum()
170 163 {
171 164 qreal max = INT_MIN;
172 165 int count = countCategories();
173 166
174 167 for (int col=0; col<count; col++) {
175 168 qreal sum = categorySum(col);
176 169 if (sum > max) {
177 170 max = sum;
178 171 }
179 172 }
180 173 return max;
181 174 }
182 175
183 176 QString BarChartModel::label(int category)
184 177 {
185 return mCategory->label(category);
178 return mCategory.at(category);
186 179 }
187 180
188 181 #include "moc_barchartmodel_p.cpp"
189 182
190 183 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,59
1 1 #ifndef BARCHARTMODEL_H
2 2 #define BARCHARTMODEL_H
3 3
4 4 #include <QObject>
5 #include <QStringList>
5 6 #include "qchartglobal.h"
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9
9 10 // Model for bar chart. Internal class.
10 11 // TODO: Implement as QAbstractItemModel?
11 12
12 13 class QBarSet;
13 class QBarCategory;
14 14
15 15 class BarChartModel : public QObject //, public QAbstractItemModel
16 16 {
17 17 Q_OBJECT
18 18 public:
19 explicit BarChartModel(QBarCategory *category, QObject *parent = 0);
20 ~BarChartModel();
19 explicit BarChartModel(QStringList categories, QObject *parent = 0);
21 20
22 QBarCategory& category();
21 QStringList category();
23 22 void addBarSet(QBarSet *set);
24 23 void removeBarSet(QBarSet *set);
25 24 QBarSet *setAt(int index);
26 25 QList<QBarSet*> barSets();
27 26
28 27 QList<QString> legend();
29 28
30 29 int countSets(); // Number of sets in model
31 30 int countCategories(); // Number of categories
32 31 int countTotalItems(); // Total items in all sets. Includes empty items.
33 32
34 33 qreal max(); // Maximum value of all sets
35 34 qreal min(); // Minimum value of all sets
36 35 qreal valueAt(int set, int category);
37 36 qreal percentageAt(int set, int category);
38 37
39 38 qreal categorySum(int category);
40 39 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
41 40
42 41 QString label(int category);
43 42
44 43 signals:
45 44 void modelUpdated();
46 45
47 46 public slots:
48 47
49 48 private:
50 49
51 50 QList<QBarSet*> mDataModel;
52 QBarCategory* mCategory; // Owned
51 QStringList mCategory;
53 52
54 53 int mCurrentSet;
55 54
56 55 };
57 56
58 57 QTCOMMERCIALCHART_END_NAMESPACE
59 58
60 59 #endif // BARCHARTMODEL_H
@@ -1,232 +1,230
1 1 #include <QDebug>
2 2 #include "qbarseries.h"
3 #include "qbarcategory.h"
4 3 #include "qbarset.h"
5 4 #include "barchartmodel_p.h"
6 5
7 6
8 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 8
10 9 /*!
11 10 \class QBarSeries
12 11 \brief part of QtCommercial chart API.
13 12
14 13 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
15 14 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
16 by QBarCategory class.
15 by QStringList.
17 16
18 17 \mainclass
19 18
20 \sa QBarCategory, QBarSet, QStackedBarSeries, QPercentBarSeries
19 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
21 20 */
22 21
23 22 /*!
24 23 \fn virtual QSeriesType QBarSeries::type() const
25 24 \brief Returns type of series.
26 25 \sa QSeries, QSeriesType
27 26 */
28 27 /*!
29 28 \fn void QBarSeries::changed(int index)
30 29 \brief \internal \a index
31 30 */
32 31 /*!
33 32 \fn void QBarSeries::floatingValuesEnabled(bool enabled)
34 33 \brief \internal \a enabled
35 34 */
36 35 /*!
37 36 \fn void QBarSeries::toolTipEnabled(bool enabled)
38 37 \brief \internal \a enabled
39 38 */
40 39 /*!
41 40 \fn void QBarSeries::separatorsEnabled(bool enabled)
42 41 \brief \internal \a enabled
43 42 */
44 43 /*!
45 44 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
46 45 \brief \internal \a pos \a tip
47 46 */
48 47
49 48 /*!
50 49 Constructs empty QBarSeries. Parameter \a category defines the categories for chart.
51 Takes ownership of \a category.
52 50 QBarSeries is QObject which is a child of a \a parent.
53 51 */
54 QBarSeries::QBarSeries(QBarCategory *category, QObject *parent)
52 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
55 53 : QSeries(parent)
56 ,mModel(new BarChartModel(category, this))
54 ,mModel(new BarChartModel(categories, this))
57 55 {
58 56 }
59 57
60 58 /*!
61 59 Adds a set of bars to series. Takes ownership of \a set
62 60 */
63 61 void QBarSeries::addBarSet(QBarSet *set)
64 62 {
65 63 mModel->addBarSet(set);
66 64 }
67 65
68 66 /*!
69 67 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
70 68 */
71 69 void QBarSeries::removeBarSet(QBarSet *set)
72 70 {
73 71 mModel->removeBarSet(set);
74 72 }
75 73
76 74 /*!
77 75 Returns number of sets in series.
78 76 */
79 77 int QBarSeries::barsetCount()
80 78 {
81 79 return mModel->countSets();
82 80 }
83 81
84 82 /*!
85 83 Returns number of categories in series
86 84 */
87 85 int QBarSeries::categoryCount()
88 86 {
89 87 return mModel->countCategories();
90 88 }
91 89
92 90 /*!
93 91 Returns a list of sets in series. Keeps ownership of sets.
94 92 */
95 93 QList<QBarSet*> QBarSeries::barSets()
96 94 {
97 95 return mModel->barSets();
98 96 }
99 97
100 98 /*!
101 99 \internal \a index
102 100 */
103 101 QBarSet* QBarSeries::barsetAt(int index)
104 102 {
105 103 return mModel->setAt(index);
106 104 }
107 105
108 106 /*!
109 107 Returns legend of series. Legend is a list of set names in series.
110 108 */
111 109 QList<QString> QBarSeries::legend()
112 110 {
113 111 return mModel->legend();
114 112 }
115 113
116 114 /*!
117 115 \internal \a category
118 116 */
119 117 QString QBarSeries::label(int category)
120 118 {
121 119 return mModel->label(category);
122 120 }
123 121
124 122 /*!
125 123 Enables or disables floating values depending on parameter \a enabled.
126 124 Floating values are bar values, that are displayed on top of each bar.
127 125 Calling without parameter \a enabled, enables the floating values
128 126 */
129 127 void QBarSeries::setFloatingValuesEnabled(bool enabled)
130 128 {
131 129 if (enabled) {
132 130 for (int i=0; i<mModel->countSets(); i++) {
133 131 QBarSet *set = mModel->setAt(i);
134 132 connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
135 133 }
136 134 } else {
137 135 for (int i=0; i<mModel->countSets(); i++) {
138 136 QBarSet *set = mModel->setAt(i);
139 137 disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
140 138 }
141 139 }
142 140 }
143 141
144 142 /*!
145 143 Enables or disables tooltip depending on parameter \a enabled.
146 144 Tooltip shows the name of set, when mouse is hovering on top of bar.
147 145 Calling without parameter \a enabled, enables the tooltip
148 146 */
149 147 void QBarSeries::setToolTipEnabled(bool enabled)
150 148 {
151 149 if (enabled) {
152 150 for (int i=0; i<mModel->countSets(); i++) {
153 151 QBarSet *set = mModel->setAt(i);
154 152 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
155 153 }
156 154 } else {
157 155 for (int i=0; i<mModel->countSets(); i++) {
158 156 QBarSet *set = mModel->setAt(i);
159 157 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
160 158 }
161 159 }
162 160 }
163 161
164 162 /*!
165 163 Enables or disables separators depending on parameter \a enabled.
166 164 Separators are visual elements that are drawn between categories.
167 165 Calling without parameter \a enabled, enables the separators
168 166 */
169 167 void QBarSeries::setSeparatorsEnabled(bool enabled)
170 168 {
171 169 emit separatorsEnabled(enabled);
172 170 }
173 171
174 172 /*!
175 173 \internal
176 174 */
177 175 qreal QBarSeries::min()
178 176 {
179 177 return mModel->min();
180 178 }
181 179
182 180 /*!
183 181 \internal
184 182 */
185 183 qreal QBarSeries::max()
186 184 {
187 185 return mModel->max();
188 186 }
189 187
190 188 /*!
191 189 \internal \a set \a category
192 190 */
193 191 qreal QBarSeries::valueAt(int set, int category)
194 192 {
195 193 return mModel->valueAt(set,category);
196 194 }
197 195
198 196 /*!
199 197 \internal \a set \a category
200 198 */
201 199 qreal QBarSeries::percentageAt(int set, int category)
202 200 {
203 201 return mModel->percentageAt(set,category);
204 202 }
205 203
206 204 /*!
207 205 \internal \a category
208 206 */
209 207 qreal QBarSeries::categorySum(int category)
210 208 {
211 209 return mModel->categorySum(category);
212 210 }
213 211
214 212 /*!
215 213 \internal
216 214 */
217 215 qreal QBarSeries::maxCategorySum()
218 216 {
219 217 return mModel->maxCategorySum();
220 218 }
221 219
222 220 /*!
223 221 \internal
224 222 */
225 223 BarChartModel& QBarSeries::model()
226 224 {
227 225 return *mModel;
228 226 }
229 227
230 228 #include "moc_qbarseries.cpp"
231 229
232 230 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,66
1 1 #ifndef BARSERIES_H
2 2 #define BARSERIES_H
3 3
4 4 #include "qseries.h"
5 #include <QStringList>
5 6
6 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 8
8 class QBarCategory;
9 9 class QBarSet;
10 10 class BarChartModel;
11 11
12 12 // Container for series
13 13 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
14 14 {
15 15 Q_OBJECT
16 16 public:
17 QBarSeries(QBarCategory *category, QObject* parent=0);
17 QBarSeries(QStringList categories, QObject* parent=0);
18 18
19 19 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
20 20
21 21 void addBarSet(QBarSet *set); // Takes ownership of set
22 22 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
23 23 int barsetCount();
24 24 int categoryCount();
25 25 QList<QBarSet*> barSets();
26 26
27 27 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
28 28
29 29 public:
30 30 // TODO: Functions below this are not part of api and will be moved
31 31 // to private implementation, when we start using it
32 32 // TODO: TO PIMPL --->
33 33 QBarSet *barsetAt(int index);
34 34 QString label(int category);
35 35 qreal min();
36 36 qreal max();
37 37 qreal valueAt(int set, int category);
38 38 qreal percentageAt(int set, int category);
39 39 qreal categorySum(int category);
40 40 qreal maxCategorySum();
41 41 BarChartModel& model();
42 42 // <--- TO PIMPL
43 43
44 44 signals:
45 45 void changed(int index);
46 46
47 47 // TODO: internal signals, these to private implementation.
48 48 // TODO: TO PIMPL --->
49 49 void floatingValuesEnabled(bool enabled);
50 50 void toolTipEnabled(bool enabled);
51 51 void separatorsEnabled(bool enabled);
52 52 void showToolTip(QPoint pos, QString tip);
53 53 // <--- TO PIMPL
54 54
55 55 public Q_SLOTS:
56 56 void setFloatingValuesEnabled(bool enabled=true); // enables floating values on top of bars
57 57 void setToolTipEnabled(bool enabled=true); // enables tooltips
58 58 void setSeparatorsEnabled(bool enabled=true); // enables separators between categories
59 59
60 60 protected:
61 61 BarChartModel* mModel;
62 62 };
63 63
64 64 QTCOMMERCIALCHART_END_NAMESPACE
65 65
66 66 #endif // BARSERIES_H
@@ -1,167 +1,167
1 1 #include "qbarset.h"
2 2 #include <QDebug>
3 3 #include <QToolTip>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 /*!
8 8 \class QBarSet
9 9 \brief part of QtCommercial chart API.
10 10
11 11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 12 First value of set is assumed to belong to first category, second to second category and so on.
13 13 If set has fewer values than there are categories, then the missing values are assumed to be
14 14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15 15
16 16 \mainclass
17 17
18 \sa QBarCategory, QBarSeries, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 19 */
20 20
21 21 /*!
22 22 \fn void QBarSet::clicked()
23 23 \brief signals that set has been clicked
24 24 */
25 25 /*!
26 26 \fn void QBarSet::hoverEnter(QPoint pos)
27 27 \brief signals that mouse has entered over the set at position \a pos.
28 28 */
29 29 /*!
30 30 \fn void QBarSet::hoverLeave()
31 31 \brief signals that mouse has left from the set.
32 32 */
33 33 /*!
34 34 \fn void QBarSet::toggleFloatingValues()
35 35 \brief \internal
36 36 */
37 37 /*!
38 38 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
39 39 \brief \internal \a pos \a tip
40 40 */
41 41
42 42
43 43 /*!
44 44 Constructs QBarSet with a name of \a name and with parent of \a parent
45 45 */
46 46 QBarSet::QBarSet(QString name, QObject *parent)
47 47 : QObject(parent)
48 48 ,mName(name)
49 49 {
50 50 }
51 51
52 52 /*!
53 53 Sets new \a name for set.
54 54 */
55 55 void QBarSet::setName(QString name)
56 56 {
57 57 mName = name;
58 58 }
59 59
60 60 /*!
61 61 Returns name of the set.
62 62 */
63 63 QString QBarSet::name()
64 64 {
65 65 return mName;
66 66 }
67 67
68 68 /*!
69 69 Appends new value \a value to the end of set.
70 70 */
71 71 QBarSet& QBarSet::operator << (const qreal &value)
72 72 {
73 73 mValues.append(value);
74 74 return *this;
75 75 }
76 76
77 77 /*!
78 78 Returns count of values in set.
79 79 */
80 80 int QBarSet::count()
81 81 {
82 82 return mValues.count();
83 83 }
84 84
85 85 /*!
86 86 Returns value of set indexed by \a index
87 87 */
88 88 qreal QBarSet::valueAt(int index)
89 89 {
90 90 return mValues.at(index);
91 91 }
92 92
93 93 /*!
94 94 Sets a new value \a value to set, indexed by \a index
95 95 */
96 96 void QBarSet::setValue(int index, qreal value)
97 97 {
98 98 mValues.replace(index,value);
99 99 }
100 100
101 101 /*!
102 102 Sets pen for set. Bars of this set are drawn using \a pen
103 103 */
104 104 void QBarSet::setPen(QPen pen)
105 105 {
106 106 mPen = pen;
107 107 }
108 108
109 109 /*!
110 110 Returns pen of the set.
111 111 */
112 112 QPen QBarSet::pen()
113 113 {
114 114 return mPen;
115 115 }
116 116
117 117 /*!
118 118 Sets brush for the set. Bars of this set are drawn using \a brush
119 119 */
120 120 void QBarSet::setBrush(QBrush brush)
121 121 {
122 122 mBrush = brush;
123 123 }
124 124
125 125 /*!
126 126 Returns brush of the set.
127 127 */
128 128 QBrush QBarSet::brush()
129 129 {
130 130 return mBrush;
131 131 }
132 132
133 133 /*!
134 134 \internal
135 135 */
136 136 void QBarSet::barClicked()
137 137 {
138 138 // qDebug() << "QBarset::barClicked" << this;
139 139 // Some bar of this set has been clicked
140 140 // TODO: What happens then?
141 141 emit clicked(); // Notify that set has been clicked
142 142 }
143 143
144 144 /*!
145 145 \internal \a pos
146 146 */
147 147 void QBarSet::barHoverEntered(QPoint pos)
148 148 {
149 149 emit showToolTip(pos, mName);
150 150 emit hoverEnter(pos);
151 151 }
152 152
153 153 /*!
154 154 \internal
155 155 */
156 156 void QBarSet::barHoverLeaved()
157 157 {
158 158 // qDebug() << "QBarset::barHoverLeaved" << this;
159 159 // if (mToolTipEnabled) {
160 160 // TODO: do what?
161 161 // }
162 162 // Emit signal to user of charts
163 163 emit hoverLeave();
164 164 }
165 165
166 166 #include "moc_qbarset.cpp"
167 167 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,36 +1,36
1 1 #include "qpercentbarseries.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 /*!
6 6 \class QPercentBarSeries
7 7 \brief part of QtCommercial chart API.
8 8
9 9 QPercentBarSeries represents a series of data shown as bars. Each bar of QBarSet is shown as percentage
10 10 of all bars in category. One QPercentBarSeries can contain multible QBarSet data sets.
11 QBarSeries groups the data from sets to categories, which are defined by QBarCategory class.
11 QBarSeries groups the data from sets to categories, which are defined by QStringList.
12 12
13 13 \mainclass
14 14
15 \sa QBarCategory, QBarSet, QStackedBarSeries, QBarSeries
15 \sa QBarSet, QStackedBarSeries, QBarSeries
16 16 */
17 17
18 18 /*!
19 19 \fn virtual QSeriesType QPercentBarSeries::type() const
20 20 \brief Returns type of series.
21 21 \sa QSeries, QSeriesType
22 22 */
23 23
24 24 /*!
25 25 Constructs empty QPercentBarSeries. Parameter \a category defines the categories for chart.
26 26 QPercentBarSeries is QObject which is a child of a \a parent.
27 27 */
28 QPercentBarSeries::QPercentBarSeries(QBarCategory *category, QObject *parent)
29 : QBarSeries(category, parent)
28 QPercentBarSeries::QPercentBarSeries(QStringList categories, QObject *parent)
29 : QBarSeries(categories, parent)
30 30 {
31 31 }
32 32
33 33 #include "moc_qpercentbarseries.cpp"
34 34
35 35 QTCOMMERCIALCHART_END_NAMESPACE
36 36
@@ -1,20 +1,21
1 1 #ifndef PERCENTBARSERIES_H
2 2 #define PERCENTBARSERIES_H
3 3
4 #include <QStringList>
4 5 #include "qbarseries.h"
5 6
6 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 8
8 9 class QTCOMMERCIALCHART_EXPORT QPercentBarSeries : public QBarSeries
9 10 {
10 11 Q_OBJECT
11 12 public:
12 QPercentBarSeries(QBarCategory *category, QObject* parent=0);
13 QPercentBarSeries(QStringList categories, QObject* parent=0);
13 14
14 15 virtual QSeriesType type() const { return QSeries::SeriesTypePercentBar; }
15 16 };
16 17
17 18 QTCOMMERCIALCHART_END_NAMESPACE
18 19
19 20
20 21 #endif // PERCENTBARSERIES_H
@@ -1,36 +1,36
1 1 #include "qstackedbarseries.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 /*!
6 6 \class QStackedBarSeries
7 7 \brief part of QtCommercial chart API.
8 8
9 9 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
10 10 stacked on top of each other. One QStackedBarSeries can contain multible QBarSet data sets.
11 QStackedBarSeries groups the data from sets to categories, which are defined by QBarCategory class.
11 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
12 12
13 13 \mainclass
14 14
15 \sa QBarCategory, QBarSet, QPercentBarSeries, QBarSeries
15 \sa QBarSet, QPercentBarSeries, QBarSeries
16 16 */
17 17
18 18 /*!
19 19 \fn virtual QSeriesType QStackedBarSeries::type() const
20 20 \brief Returns type of series.
21 21 \sa QSeries, QSeriesType
22 22 */
23 23
24 24 /*!
25 25 Constructs empty QStackedBarSeries. Parameter \a category defines the categories for chart.
26 26 QStackedBarSeries is QObject which is a child of a \a parent.
27 27 */
28 QStackedBarSeries::QStackedBarSeries(QBarCategory *category, QObject *parent)
29 : QBarSeries(category, parent)
28 QStackedBarSeries::QStackedBarSeries(QStringList categories, QObject *parent)
29 : QBarSeries(categories, parent)
30 30 {
31 31 }
32 32
33 33 #include "moc_qstackedbarseries.cpp"
34 34
35 35 QTCOMMERCIALCHART_END_NAMESPACE
36 36
@@ -1,21 +1,20
1 1 #ifndef STACKEDBARSERIES_H
2 2 #define STACKEDBARSERIES_H
3 3
4 #include <QStringList>
4 5 #include "qbarseries.h"
5 6
6 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 8
8 class QBarCategory;
9
10 9 class QTCOMMERCIALCHART_EXPORT QStackedBarSeries : public QBarSeries
11 10 {
12 11 Q_OBJECT
13 12 public:
14 QStackedBarSeries(QBarCategory *category, QObject* parent=0);
13 QStackedBarSeries(QStringList categories, QObject* parent=0);
15 14
16 15 virtual QSeriesType type() const { return QSeries::SeriesTypeStackedBar; }
17 16 };
18 17
19 18 QTCOMMERCIALCHART_END_NAMESPACE
20 19
21 20 #endif // STACKEDBARSERIES_H
@@ -1,375 +1,373
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 3 #include "qpieseries.h"
4 4 #include "qscatterseries.h"
5 5 #include <qlineseries.h>
6 6 #include <qbarset.h>
7 #include <qbarcategory.h>
8 7 #include <qbarseries.h>
9 8 #include <qstackedbarseries.h>
10 9 #include <qpercentbarseries.h>
11 10 #include <QPushButton>
12 11 #include <QComboBox>
13 12 #include <QSpinBox>
14 13 #include <QCheckBox>
15 14 #include <QGridLayout>
16 15 #include <QHBoxLayout>
17 16 #include <QLabel>
18 17 #include <QSpacerItem>
19 18 #include <QMessageBox>
20 19 #include <cmath>
21 20 #include <QDebug>
22 21 #include <QStandardItemModel>
23 22
24 23
25 24 QTCOMMERCIALCHART_USE_NAMESPACE
26 25
27 26 MainWidget::MainWidget(QWidget *parent) :
28 27 QWidget(parent),
29 28 m_addSerieDialog(0),
30 29 m_chartWidget(0)
31 30 {
32 31 m_chartWidget = new QChartView(this);
33 32 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
34 33
35 34 // Grid layout for the controls for configuring the chart widget
36 35 QGridLayout *grid = new QGridLayout();
37 36 QPushButton *addSeriesButton = new QPushButton("Add series");
38 37 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
39 38 grid->addWidget(addSeriesButton, 0, 1);
40 39 initBackroundCombo(grid);
41 40 initScaleControls(grid);
42 41 initThemeCombo(grid);
43 42 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
44 43 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
45 44 zoomCheckBox->setChecked(true);
46 45 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
47 46 // add row with empty label to make all the other rows static
48 47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
49 48 grid->setRowStretch(grid->rowCount() - 1, 1);
50 49
51 50 // Another grid layout as a main layout
52 51 QGridLayout *mainLayout = new QGridLayout();
53 52 mainLayout->addLayout(grid, 0, 0);
54 53
55 54 // Init series type specific controls
56 55 initPieControls();
57 56 mainLayout->addLayout(m_pieLayout, 2, 0);
58 57 // Scatter series specific settings
59 58 // m_scatterLayout = new QGridLayout();
60 59 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
61 60 // m_scatterLayout->setEnabled(false);
62 61 // mainLayout->addLayout(m_scatterLayout, 1, 0);
63 62
64 63 // Add layouts and the chart widget to the main layout
65 64 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
66 65 setLayout(mainLayout);
67 66 }
68 67
69 68 // Combo box for selecting the chart's background
70 69 void MainWidget::initBackroundCombo(QGridLayout *grid)
71 70 {
72 71 QComboBox *backgroundCombo = new QComboBox(this);
73 72 backgroundCombo->addItem("Color");
74 73 backgroundCombo->addItem("Gradient");
75 74 backgroundCombo->addItem("Image");
76 75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
77 76 this, SLOT(backgroundChanged(int)));
78 77
79 78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
80 79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
81 80 }
82 81
83 82 // Scale related controls (auto-scale vs. manual min-max values)
84 83 void MainWidget::initScaleControls(QGridLayout *grid)
85 84 {
86 85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
87 86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
88 87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
89 88 m_xMinSpin = new QSpinBox();
90 89 m_xMinSpin->setMinimum(INT_MIN);
91 90 m_xMinSpin->setMaximum(INT_MAX);
92 91 m_xMinSpin->setValue(0);
93 92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
94 93 m_xMaxSpin = new QSpinBox();
95 94 m_xMaxSpin->setMinimum(INT_MIN);
96 95 m_xMaxSpin->setMaximum(INT_MAX);
97 96 m_xMaxSpin->setValue(10);
98 97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
99 98 m_yMinSpin = new QSpinBox();
100 99 m_yMinSpin->setMinimum(INT_MIN);
101 100 m_yMinSpin->setMaximum(INT_MAX);
102 101 m_yMinSpin->setValue(0);
103 102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
104 103 m_yMaxSpin = new QSpinBox();
105 104 m_yMaxSpin->setMinimum(INT_MIN);
106 105 m_yMaxSpin->setMaximum(INT_MAX);
107 106 m_yMaxSpin->setValue(10);
108 107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
109 108
110 109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
111 110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
112 111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
113 112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
114 113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
115 114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
116 115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
117 116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
118 117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
119 118
120 119 m_autoScaleCheck->setChecked(true);
121 120 }
122 121
123 122 // Combo box for selecting theme
124 123 void MainWidget::initThemeCombo(QGridLayout *grid)
125 124 {
126 125 QComboBox *chartTheme = new QComboBox();
127 126 chartTheme->addItem("Default");
128 127 chartTheme->addItem("Vanilla");
129 128 chartTheme->addItem("Icy");
130 129 chartTheme->addItem("Grayscale");
131 130 chartTheme->addItem("Scientific");
132 131 chartTheme->addItem("Unnamed1");
133 132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
134 133 this, SLOT(changeChartTheme(int)));
135 134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
136 135 grid->addWidget(chartTheme, 8, 1);
137 136 }
138 137
139 138 void MainWidget::initPieControls()
140 139 {
141 140 // Pie series specific settings
142 141 // Pie size factory
143 142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
144 143 pieSizeSpin->setMinimum(LONG_MIN);
145 144 pieSizeSpin->setMaximum(LONG_MAX);
146 145 pieSizeSpin->setValue(1.0);
147 146 pieSizeSpin->setSingleStep(0.1);
148 147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
149 148 // Pie position
150 149 QComboBox *piePosCombo = new QComboBox(this);
151 150 piePosCombo->addItem("Maximized");
152 151 piePosCombo->addItem("Top left");
153 152 piePosCombo->addItem("Top right");
154 153 piePosCombo->addItem("Bottom left");
155 154 piePosCombo->addItem("Bottom right");
156 155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
157 156 this, SLOT(setPiePosition(int)));
158 157 m_pieLayout = new QGridLayout();
159 158 m_pieLayout->setEnabled(false);
160 159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
161 160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
162 161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
163 162 m_pieLayout->addWidget(piePosCombo, 1, 1);
164 163 }
165 164
166 165 void MainWidget::addSeries()
167 166 {
168 167 if (!m_addSerieDialog) {
169 168 m_addSerieDialog = new DataSerieDialog(this);
170 169 connect(m_addSerieDialog, SIGNAL(accepted(QString, int, int, QString, bool)),
171 170 this, SLOT(addSeries(QString, int, int, QString, bool)));
172 171 }
173 172 m_addSerieDialog->exec();
174 173 }
175 174
176 175 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
177 176 {
178 177 // TODO: dataCharacteristics
179 178 QList<RealList> testData;
180 179 for (int j(0); j < columnCount; j++) {
181 180 QList <qreal> newColumn;
182 181 for (int i(0); i < rowCount; i++) {
183 182 if (dataCharacteristics == "Sin") {
184 183 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
185 184 } else if (dataCharacteristics == "Sin + random") {
186 185 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
187 186 } else if (dataCharacteristics == "Random") {
188 187 newColumn.append(rand() % 5);
189 188 } else if (dataCharacteristics == "Linear") {
190 189 //newColumn.append(i * (j + 1.0));
191 190 // TODO: temporary hack to make pie work; prevent zero values:
192 191 newColumn.append(i * (j + 1.0) + 0.1);
193 192 } else { // "constant"
194 193 newColumn.append((j + 1.0));
195 194 }
196 195 }
197 196 testData.append(newColumn);
198 197 }
199 198 return testData;
200 199 }
201 200
202 201 QStringList MainWidget::generateLabels(int count)
203 202 {
204 203 QStringList result;
205 204 for (int i(0); i < count; i++)
206 205 result.append("label" + QString::number(i));
207 206 return result;
208 207 }
209 208
210 209 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
211 210 {
212 211 qDebug() << "addSeries: " << seriesName
213 212 << " columnCount: " << columnCount
214 213 << " rowCount: " << rowCount
215 214 << " dataCharacteristics: " << dataCharacteristics
216 215 << " labels enabled: " << labelsEnabled;
217 216 m_defaultSeriesName = seriesName;
218 217
219 218 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
220 219
221 220 // Line series and scatter series use similar data
222 221 if (seriesName.contains("line", Qt::CaseInsensitive)) {
223 222 for (int j(0); j < data.count(); j ++) {
224 223 QList<qreal> column = data.at(j);
225 224 QLineSeries *series = new QLineSeries();
226 225 for (int i(0); i < column.count(); i++) {
227 226 series->add(i, column.at(i));
228 227 }
229 228 m_chartWidget->addSeries(series);
230 229 setCurrentSeries(series);
231 230 }
232 231 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
233 232 for (int j(0); j < data.count(); j++) {
234 233 QList<qreal> column = data.at(j);
235 234 QScatterSeries *series = new QScatterSeries();
236 235 for (int i(0); i < column.count(); i++) {
237 236 (*series) << QPointF(i, column.at(i));
238 237 }
239 238 m_chartWidget->addSeries(series);
240 239 setCurrentSeries(series);
241 240 }
242 241 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
243 242 QStringList labels = generateLabels(rowCount);
244 243 for (int j(0); j < data.count(); j++) {
245 244 QPieSeries *series = new QPieSeries();
246 245 QList<qreal> column = data.at(j);
247 246 for (int i(0); i < column.count(); i++) {
248 247 series->add(column.at(i), labels.at(i));
249 248 }
250 249 m_chartWidget->addSeries(series);
251 250 setCurrentSeries(series);
252 251 }
253 252 } else if (seriesName == "Bar"
254 253 || seriesName == "Stacked bar"
255 254 || seriesName == "Percent bar") {
256 // TODO: replace QBarCategory with QStringList?
257 QBarCategory *category = new QBarCategory;
255 QStringList category;
258 256 QStringList labels = generateLabels(rowCount);
259 257 foreach(QString label, labels)
260 *category << label;
258 category << label;
261 259 QBarSeries* series = 0;
262 260 if (seriesName == "Bar")
263 261 series = new QBarSeries(category, this);
264 262 else if (seriesName == "Stacked bar")
265 263 series = new QStackedBarSeries(category, this);
266 264 else
267 265 series = new QPercentBarSeries(category, this);
268 266
269 267 for (int j(0); j < data.count(); j++) {
270 268 QList<qreal> column = data.at(j);
271 269 QBarSet *set = new QBarSet("set" + QString::number(j));
272 270 for (int i(0); i < column.count(); i++) {
273 271 *set << column.at(i);
274 272 }
275 273 series->addBarSet(set);
276 274 }
277 275 series->setFloatingValuesEnabled(true);
278 276 series->setToolTipEnabled(true);
279 277 series->setSeparatorsEnabled(false);
280 278 m_chartWidget->addSeries(series);
281 279 setCurrentSeries(series);
282 280 }
283 281
284 282 // TODO: spline and area
285 283 }
286 284
287 285 void MainWidget::setCurrentSeries(QSeries *series)
288 286 {
289 287 if (series) {
290 288 m_currentSeries = series;
291 289 switch (m_currentSeries->type()) {
292 290 case QSeries::SeriesTypeLine:
293 291 break;
294 292 case QSeries::SeriesTypeScatter:
295 293 break;
296 294 case QSeries::SeriesTypePie:
297 295 break;
298 296 case QSeries::SeriesTypeBar:
299 297 qDebug() << "setCurrentSeries (bar)";
300 298 break;
301 299 case QSeries::SeriesTypeStackedBar:
302 300 qDebug() << "setCurrentSeries (Stackedbar)";
303 301 break;
304 302 case QSeries::SeriesTypePercentBar:
305 303 qDebug() << "setCurrentSeries (Percentbar)";
306 304 break;
307 305 default:
308 306 Q_ASSERT(false);
309 307 break;
310 308 }
311 309 }
312 310 }
313 311
314 312 void MainWidget::backgroundChanged(int itemIndex)
315 313 {
316 314 qDebug() << "backgroundChanged: " << itemIndex;
317 315 }
318 316
319 317 void MainWidget::autoScaleChanged(int value)
320 318 {
321 319 if (value) {
322 320 // TODO: enable auto scaling
323 321 } else {
324 322 // TODO: set scaling manually (and disable auto scaling)
325 323 }
326 324
327 325 m_xMinSpin->setEnabled(!value);
328 326 m_xMaxSpin->setEnabled(!value);
329 327 m_yMinSpin->setEnabled(!value);
330 328 m_yMaxSpin->setEnabled(!value);
331 329 }
332 330
333 331 void MainWidget::xMinChanged(int value)
334 332 {
335 333 qDebug() << "xMinChanged: " << value;
336 334 }
337 335
338 336 void MainWidget::xMaxChanged(int value)
339 337 {
340 338 qDebug() << "xMaxChanged: " << value;
341 339 }
342 340
343 341 void MainWidget::yMinChanged(int value)
344 342 {
345 343 qDebug() << "yMinChanged: " << value;
346 344 }
347 345
348 346 void MainWidget::yMaxChanged(int value)
349 347 {
350 348 qDebug() << "yMaxChanged: " << value;
351 349 }
352 350
353 351 void MainWidget::changeChartTheme(int themeIndex)
354 352 {
355 353 qDebug() << "changeChartTheme: " << themeIndex;
356 354 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
357 355 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
358 356 QSize s = size();
359 357 s.setWidth(s.width()+1);
360 358 resize(s);
361 359 }
362 360
363 361 void MainWidget::setPieSizeFactor(double size)
364 362 {
365 363 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
366 364 if (pie)
367 365 pie->setSizeFactor(qreal(size));
368 366 }
369 367
370 368 void MainWidget::setPiePosition(int position)
371 369 {
372 370 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
373 371 if (pie)
374 372 pie->setPosition((QPieSeries::PiePosition) position);
375 373 }
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now