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