@@ -1,225 +1,226 | |||
|
1 | 1 | /*! |
|
2 | 2 | \class Widget |
|
3 | 3 | \brief Ui for the application. |
|
4 | 4 | \internal |
|
5 | 5 | */ |
|
6 | 6 | |
|
7 | 7 | #include "widget.h" |
|
8 | 8 | #include <QGridLayout> |
|
9 | 9 | #include <QPushButton> |
|
10 | 10 | #include <QLabel> |
|
11 | 11 | |
|
12 | 12 | #include <QSqlQuery> |
|
13 | 13 | #include <qscatterseries.h> |
|
14 | 14 | #include <qchartview.h> |
|
15 | 15 | #include <qchartaxis.h> |
|
16 | 16 | #include <qbarcategory.h> |
|
17 | 17 | #include <qbarset.h> |
|
18 | 18 | #include <QListWidget> |
|
19 | 19 | #include <QPrinter> |
|
20 | 20 | #include <QPrintDialog> |
|
21 | 21 | #include <QRadioButton> |
|
22 | 22 | |
|
23 | 23 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
24 | 24 | |
|
25 | 25 | Widget::Widget(QWidget *parent) |
|
26 | 26 | : QWidget(parent) |
|
27 | 27 | { |
|
28 | 28 | setGeometry(100, 100, 1000, 600); |
|
29 | 29 | |
|
30 | 30 | // right panel layout |
|
31 | 31 | barChartRadioButton = new QRadioButton(tr("Bar chart")); |
|
32 | 32 | barChartRadioButton->setChecked(true); |
|
33 | 33 | scatterChartRadioButton = new QRadioButton(tr("Scatter chart")); |
|
34 | 34 | scatterChartRadioButton->setChecked(false); |
|
35 | 35 | countrieslist = new QListWidget; |
|
36 | 36 | countrieslist->setSelectionMode(QAbstractItemView::MultiSelection); |
|
37 | 37 | |
|
38 | 38 | yearslist = new QListWidget; |
|
39 | 39 | yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection); |
|
40 | 40 | for (int i = 1990; i < 2011; i++) |
|
41 | 41 | yearslist->addItem(QString("%1").arg(i)); |
|
42 | 42 | |
|
43 | 43 | QPushButton* refreshButton = new QPushButton(tr("Refresh")); |
|
44 | 44 | connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart())); |
|
45 | 45 | |
|
46 | 46 | QPushButton* printButton = new QPushButton(tr("Print chart")); |
|
47 | 47 | connect(printButton, SIGNAL(clicked()), this, SLOT(printChart())); |
|
48 | 48 | |
|
49 | 49 | QVBoxLayout* rightPanelLayout = new QVBoxLayout; |
|
50 | 50 | rightPanelLayout->addWidget(barChartRadioButton); |
|
51 | 51 | rightPanelLayout->addWidget(scatterChartRadioButton); |
|
52 | 52 | rightPanelLayout->addWidget(countrieslist); |
|
53 | 53 | rightPanelLayout->addWidget(yearslist); |
|
54 | 54 | rightPanelLayout->addWidget(refreshButton); |
|
55 | 55 | rightPanelLayout->addWidget(printButton); |
|
56 | 56 | rightPanelLayout->setStretch(0, 1); |
|
57 | 57 | rightPanelLayout->setStretch(1, 0); |
|
58 | 58 | |
|
59 | 59 | // main layout |
|
60 | 60 | chartArea = new QChartView(this); |
|
61 | 61 | chartArea->setChartTitle("GDP by country"); |
|
62 | 62 | QGridLayout* mainLayout = new QGridLayout; |
|
63 | 63 | mainLayout->addWidget(chartArea, 0, 0); |
|
64 | 64 | mainLayout->addLayout(rightPanelLayout, 0, 1); |
|
65 | 65 | mainLayout->setColumnStretch(0,1); |
|
66 | 66 | setLayout(mainLayout); |
|
67 | 67 | |
|
68 | 68 | // connect to the database |
|
69 | 69 | db = QSqlDatabase::addDatabase("QSQLITE"); |
|
70 | 70 | db.setDatabaseName("gdpData"); |
|
71 | 71 | if(!db.open()) |
|
72 | 72 | { |
|
73 | 73 | qDebug() << "could not open database. SQLite db file missing (?)"; |
|
74 | 74 | return; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | // get the list of all countires and regions. |
|
78 | 78 | QSqlQuery query; |
|
79 | 79 | query.exec("SELECT DISTINCT country FROM gdp2"); |
|
80 | 80 | |
|
81 | 81 | // add the countries to the country filter |
|
82 | 82 | while (query.next()) { |
|
83 | 83 | countrieslist->addItem(query.value(0).toString()); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | // hide axis X labels |
|
87 | 87 | QChartAxis* axis = chartArea->axisX(); |
|
88 | 88 | // axis->setLabelsVisible(false); |
|
89 | 89 | // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); |
|
90 | 90 | |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | Widget::~Widget() |
|
94 | 94 | { |
|
95 | 95 | // |
|
96 | 96 | db.close(); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | /*! |
|
100 | 100 | refreshes the chart |
|
101 | 101 | */ |
|
102 | 102 | void Widget::refreshChart() |
|
103 | 103 | { |
|
104 |
chartArea->removeSeries( |
|
|
104 | chartArea->removeAllSeries(); | |
|
105 | 105 | |
|
106 | 106 | // selected countries items list is not sorted. copy the values to QStringlist and sort them. |
|
107 | 107 | QStringList selectedCountriesStrings; |
|
108 | 108 | QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems(); |
|
109 | 109 | for (int i = 0; i < selectedCountriesItems.size(); i++) |
|
110 | 110 | selectedCountriesStrings.append(selectedCountriesItems[i]->text()); |
|
111 | 111 | selectedCountriesStrings.sort(); |
|
112 | 112 | |
|
113 | 113 | QSqlQuery query; |
|
114 | 114 | // selected years items list is not sorted. copy the values to QList<Integer> and sort them. |
|
115 | 115 | QList<int> selectedYearsInts; |
|
116 | 116 | QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems(); |
|
117 | 117 | for (int i = 0; i < selectedYearsItems.size(); i++) |
|
118 | 118 | selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); |
|
119 | 119 | qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>()); |
|
120 | 120 | |
|
121 | 121 | if (barChartRadioButton->isChecked()) |
|
122 | 122 | { |
|
123 | 123 | // use the sorted selected coutries list to initialize BarCategory |
|
124 | 124 | QBarCategory* category = new QBarCategory; |
|
125 | 125 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
126 | 126 | *category << selectedCountriesStrings[i]; |
|
127 | 127 | series0 = new QBarChartSeries(category); |
|
128 | 128 | |
|
129 | 129 | // prepare the selected counries SQL query |
|
130 | 130 | QString countriesQuery = "country IN ("; |
|
131 | 131 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
132 | 132 | { |
|
133 | 133 | countriesQuery.append("'" + selectedCountriesStrings[i] + "'"); |
|
134 | 134 | if ( i < selectedCountriesStrings.size() - 1) |
|
135 | 135 | countriesQuery.append(","); |
|
136 | 136 | else |
|
137 | 137 | countriesQuery.append(")"); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | // perform a query for each selected year |
|
141 | 141 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
142 | 142 | { |
|
143 | 143 | query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery); |
|
144 | QBarSet* barSet = new QBarSet; | |
|
144 | QBarSet* barSet = new QBarSet(QString("GDP_%1").arg(selectedYearsInts[i])); | |
|
145 | 145 | // while (query.next()) { |
|
146 | 146 | // qDebug() << query.value(0).toString() << " : " << query.value(1).toString(); |
|
147 | 147 | // } |
|
148 | 148 | query.first(); |
|
149 | 149 | |
|
150 | 150 | // the data for some of the coutries for some years might be missing. |
|
151 | 151 | // QBarChart needs bars to have same size |
|
152 | 152 | for (int k = 0; k < selectedCountriesStrings.size(); k++) |
|
153 | 153 | { |
|
154 | 154 | if (selectedCountriesStrings[k] == query.value(0).toString()) |
|
155 | 155 | { |
|
156 | 156 | *barSet << query.value(1).toReal(); |
|
157 | 157 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]); |
|
158 | 158 | query.next(); |
|
159 | 159 | } |
|
160 | 160 | else |
|
161 | 161 | { |
|
162 | 162 | // data missing, put 0 |
|
163 | 163 | *barSet << 0.0f; |
|
164 | 164 | qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]); |
|
165 | 165 | } |
|
166 | 166 | } |
|
167 | 167 | series0->addBarSet(barSet); |
|
168 | 168 | } |
|
169 | 169 | // add the serie to the chart |
|
170 | 170 | chartArea->addSeries(series0); |
|
171 | 171 | |
|
172 | 172 | } |
|
173 | 173 | else if (scatterChartRadioButton->isChecked()) |
|
174 | 174 | { |
|
175 | 175 | QString yearsQuery = "year IN ("; |
|
176 | 176 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
177 | 177 | { |
|
178 | 178 | yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'"); |
|
179 | 179 | if ( i < selectedYearsInts.size() - 1) |
|
180 | 180 | yearsQuery.append(","); |
|
181 | 181 | else |
|
182 | 182 | yearsQuery.append(")"); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | // perform a query for each selected year |
|
186 | 186 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
187 | 187 | { |
|
188 | 188 | query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery); |
|
189 | 189 | query.first(); |
|
190 | 190 | |
|
191 | 191 | QScatterSeries* series = new QScatterSeries; |
|
192 | 192 | // the data for some of the coutries for some years might be missing. |
|
193 | 193 | for (int k = 0; k < selectedYearsInts.size(); k++) |
|
194 | 194 | { |
|
195 | 195 | if (selectedYearsInts[k] == query.value(0).toInt()) |
|
196 | 196 | { |
|
197 | 197 | *series << QPointF(query.value(0).toInt() , query.value(1).toReal()); |
|
198 | 198 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]); |
|
199 | 199 | query.next(); |
|
200 | 200 | } |
|
201 | 201 | else |
|
202 | 202 | { |
|
203 | 203 | // data missing, put 0 |
|
204 | 204 | *series << QPointF(selectedYearsInts[k] , 0.0f); |
|
205 | 205 | qDebug() << "Putting 0 for Bosnia" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt(); |
|
206 | 206 | } |
|
207 | 207 | } |
|
208 | chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); | |
|
208 | // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); | |
|
209 | 209 | chartArea->addSeries(series); |
|
210 | 210 | } |
|
211 | chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); | |
|
211 | 212 | } |
|
212 | 213 | } |
|
213 | 214 | |
|
214 | 215 | void Widget::printChart() |
|
215 | 216 | { |
|
216 | 217 | QPrinter printer; |
|
217 | 218 | // QPrinter printer(QPrinter::HighResolution); |
|
218 | 219 | printer.setOutputFormat(QPrinter::PdfFormat); |
|
219 | 220 | printer.setOrientation(QPrinter::Landscape); |
|
220 | 221 | printer.setOutputFileName("print.pdf"); |
|
221 | 222 | |
|
222 | 223 | QPainter painter; |
|
223 | 224 | painter.begin(&printer); |
|
224 | 225 | chartArea->render(&painter); |
|
225 | 226 | } |
@@ -1,237 +1,246 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include "chartdataset_p.h" |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | #include <QGraphicsSceneResizeEvent> |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | /*! |
|
12 | 12 | \enum QChart::ChartTheme |
|
13 | 13 | |
|
14 | 14 | This enum describes the theme used by the chart. |
|
15 | 15 | |
|
16 | 16 | \value ChartThemeDefault |
|
17 | 17 | \value ChartThemeVanilla |
|
18 | 18 | \value ChartThemeIcy |
|
19 | 19 | \value ChartThemeGrayscale |
|
20 | 20 | \value ChartThemeScientific |
|
21 | 21 | */ |
|
22 | 22 | |
|
23 | 23 | /*! |
|
24 | 24 | \class QChart |
|
25 | 25 | \brief QtCommercial chart API. |
|
26 | 26 | |
|
27 | 27 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
28 | 28 | representation of different types of QChartSeries and other chart related objects like |
|
29 | 29 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
30 | 30 | convenience class QChartView instead of QChart. |
|
31 | 31 | \sa QChartView |
|
32 | 32 | */ |
|
33 | 33 | |
|
34 | 34 | /*! |
|
35 | Constructs a chart object which is a child of parent. | |
|
35 | Constructs a chart object which is a child of a\a parent. | |
|
36 | 36 | */ |
|
37 | 37 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
38 | 38 | m_backgroundItem(0), |
|
39 | 39 | m_titleItem(0), |
|
40 | 40 | m_dataset(new ChartDataSet(this)), |
|
41 | 41 | m_presenter(new ChartPresenter(this,m_dataset)) |
|
42 | 42 | { |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | /*! |
|
46 | 46 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
47 | 47 | */ |
|
48 | 48 | QChart::~QChart() |
|
49 | 49 | { |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | /*! |
|
53 |
Adds the \a series and optional |
|
|
53 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. | |
|
54 | 54 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
55 | 55 | the y axis). |
|
56 | 56 | */ |
|
57 | 57 | void QChart::addSeries(QChartSeries* series, QChartAxis* axisY) |
|
58 | 58 | { |
|
59 | 59 | m_dataset->addSeries(series, axisY); |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | /*! |
|
63 |
Removes the |
|
|
63 | Removes the \a series specified in a perameter from the QChartView. | |
|
64 | 64 | It releses its ownership of the specified QChartSeries object. |
|
65 | 65 | It does not delete the pointed QChartSeries data object |
|
66 |
\sa |
|
|
66 | \sa addSeries(), removeAllSeries() | |
|
67 | 67 | */ |
|
68 | 68 | void QChart::removeSeries(QChartSeries* series) |
|
69 | 69 | { |
|
70 | 70 | m_dataset->removeSeries(series); |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | /*! |
|
74 | 74 | Removes all the QChartSeries that have been added to the QChartView |
|
75 | 75 | It also deletes the pointed QChartSeries data objects |
|
76 | 76 | \sa addSeries(), removeSeries() |
|
77 | 77 | */ |
|
78 | 78 | void QChart::removeAllSeries() |
|
79 | 79 | { |
|
80 | 80 | m_dataset->removeAllSeries(); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
84 | 84 | { |
|
85 | 85 | createChartBackgroundItem(); |
|
86 | 86 | m_backgroundItem->setBrush(brush); |
|
87 | 87 | m_backgroundItem->update(); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
91 | 91 | { |
|
92 | 92 | createChartBackgroundItem(); |
|
93 | 93 | m_backgroundItem->setPen(pen); |
|
94 | 94 | m_backgroundItem->update(); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | /*! |
|
98 | 98 | Sets the chart \a title. The description text that is rendered above the chart. |
|
99 | 99 | */ |
|
100 | 100 | void QChart::setChartTitle(const QString& title) |
|
101 | 101 | { |
|
102 | 102 | createChartTitleItem(); |
|
103 | 103 | m_titleItem->setPlainText(title); |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | /*! |
|
107 | 107 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
108 | 108 | */ |
|
109 | 109 | void QChart::setChartTitleFont(const QFont& font) |
|
110 | 110 | { |
|
111 | 111 | createChartTitleItem(); |
|
112 | 112 | m_titleItem->setFont(font); |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | void QChart::createChartBackgroundItem() |
|
116 | 116 | { |
|
117 | 117 | if(!m_backgroundItem) { |
|
118 | 118 | m_backgroundItem = new QGraphicsRectItem(this); |
|
119 | 119 | m_backgroundItem->setPen(Qt::NoPen); |
|
120 | 120 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
121 | 121 | } |
|
122 | 122 | } |
|
123 | 123 | |
|
124 | 124 | void QChart::createChartTitleItem() |
|
125 | 125 | { |
|
126 | 126 | if(!m_titleItem) { |
|
127 | 127 | m_titleItem = new QGraphicsTextItem(this); |
|
128 | 128 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
129 | 129 | } |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | /*! |
|
133 | 133 | Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. |
|
134 | 134 | \sa setMargin() |
|
135 | 135 | */ |
|
136 | 136 | int QChart::margin() const |
|
137 | 137 | { |
|
138 | 138 | return m_presenter->margin(); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | /*! |
|
142 | 142 | Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. |
|
143 | 143 | \sa margin() |
|
144 | 144 | */ |
|
145 | 145 | void QChart::setMargin(int margin) |
|
146 | 146 | { |
|
147 | 147 | m_presenter->setMargin(margin); |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | /*! |
|
151 |
Sets the \a theme used by the chart for rendering |
|
|
151 | Sets the \a theme used by the chart for rendering the graphical representation of the data | |
|
152 | 152 | \sa ChartTheme, chartTheme() |
|
153 | 153 | */ |
|
154 | 154 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
155 | 155 | { |
|
156 | 156 | m_presenter->setChartTheme(theme); |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | /*! |
|
160 | 160 | Returns the theme enum used by the chart. |
|
161 | 161 | \sa ChartTheme, setChartTheme() |
|
162 | 162 | */ |
|
163 | 163 | QChart::ChartTheme QChart::chartTheme() const |
|
164 | 164 | { |
|
165 | 165 | return m_presenter->chartTheme(); |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | /*! | |
|
169 | Zooms in the view by a factor of 2 | |
|
170 | */ | |
|
168 | 171 | void QChart::zoomIn() |
|
169 | 172 | { |
|
170 | 173 | if (!m_dataset->nextDomain()) { |
|
171 | 174 | QRectF rect = m_presenter->geometry(); |
|
172 | 175 | rect.setWidth(rect.width()/2); |
|
173 | 176 | rect.setHeight(rect.height()/2); |
|
174 | 177 | rect.moveCenter(m_presenter->geometry().center()); |
|
175 | 178 | zoomIn(rect); |
|
176 | 179 | } |
|
177 | 180 | } |
|
178 | 181 | |
|
182 | /*! | |
|
183 | Zooms in the view to a maximum level at which \a rect is still fully visible. | |
|
184 | */ | |
|
179 | 185 | void QChart::zoomIn(const QRectF& rect) |
|
180 | 186 | { |
|
181 | 187 | if(!rect.isValid()) return; |
|
182 | 188 | QRectF r = rect.normalized(); |
|
183 | 189 | int margin = m_presenter->margin(); |
|
184 | 190 | r.translate(-margin, -margin); |
|
185 | 191 | m_dataset->addDomain(r,m_presenter->geometry()); |
|
186 | 192 | } |
|
187 | 193 | |
|
194 | /*! | |
|
195 | Restores the view zoom level to the previous one. | |
|
196 | */ | |
|
188 | 197 | void QChart::zoomOut() |
|
189 | 198 | { |
|
190 | 199 | m_dataset->previousDomain(); |
|
191 | 200 | } |
|
192 | 201 | |
|
193 | 202 | void QChart::zoomReset() |
|
194 | 203 | { |
|
195 | 204 | m_dataset->clearDomains(); |
|
196 | 205 | } |
|
197 | 206 | |
|
198 | 207 | /*! |
|
199 | 208 | Returns the pointer to the x axis object of the chart |
|
200 | 209 | */ |
|
201 | 210 | QChartAxis* QChart::axisX() const |
|
202 | 211 | { |
|
203 | 212 | return m_dataset->axisX(); |
|
204 | 213 | } |
|
205 | 214 | |
|
206 | 215 | /*! |
|
207 | 216 | Returns the pointer to the y axis object of the chart |
|
208 | 217 | */ |
|
209 | 218 | QChartAxis* QChart::axisY() const |
|
210 | 219 | { |
|
211 | 220 | return m_dataset->axisY(); |
|
212 | 221 | } |
|
213 | 222 | |
|
214 | 223 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
215 | 224 | { |
|
216 | 225 | |
|
217 | 226 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
218 | 227 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
219 | 228 | |
|
220 | 229 | // recalculate title position |
|
221 | 230 | if (m_titleItem) { |
|
222 | 231 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
223 | 232 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
224 | 233 | } |
|
225 | 234 | |
|
226 | 235 | //recalculate background gradient |
|
227 | 236 | if (m_backgroundItem) { |
|
228 | 237 | m_backgroundItem->setRect(rect); |
|
229 | 238 | } |
|
230 | 239 | |
|
231 | 240 | QGraphicsWidget::resizeEvent(event); |
|
232 | 241 | update(); |
|
233 | 242 | } |
|
234 | 243 | |
|
235 | 244 | #include "moc_qchart.cpp" |
|
236 | 245 | |
|
237 | 246 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,82 +1,82 | |||
|
1 | 1 | #ifndef QCHART_H |
|
2 | 2 | #define QCHART_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qchartseries.h> |
|
6 | 6 | #include <QGraphicsWidget> |
|
7 | 7 | #include <QLinearGradient> |
|
8 | 8 | #include <QFont> |
|
9 | 9 | |
|
10 | 10 | class QGraphicsSceneResizeEvent; |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | class AxisItem; |
|
15 | 15 | class QChartSeries; |
|
16 | 16 | class PlotDomain; |
|
17 | 17 | class BarPresenter; |
|
18 | 18 | class QChartAxis; |
|
19 | 19 | class ChartTheme; |
|
20 | 20 | class ChartItem; |
|
21 | 21 | class ChartDataSet; |
|
22 | 22 | class ChartPresenter; |
|
23 | 23 | |
|
24 | 24 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
25 | 25 | { |
|
26 | 26 | Q_OBJECT |
|
27 | 27 | public: |
|
28 | enum ChartTheme { | |
|
29 | /*! The default theme follows the GUI style of the Operating System */ | |
|
28 | enum ChartTheme { | |
|
30 | 29 |
|
|
31 | 30 | ChartThemeVanilla, |
|
32 | 31 | ChartThemeIcy, |
|
33 | 32 | ChartThemeGrayscale, |
|
34 | 33 | ChartThemeScientific |
|
35 | 34 | //ChartThemeUnnamed1 |
|
35 | /*! The default theme follows the GUI style of the Operating System */ | |
|
36 | 36 | }; |
|
37 | 37 | |
|
38 | 38 | public: |
|
39 | 39 | QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); |
|
40 | 40 | ~QChart(); |
|
41 | 41 | |
|
42 | 42 | void addSeries(QChartSeries* series, QChartAxis* axisY = 0); |
|
43 | 43 | void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached |
|
44 | 44 | void removeAllSeries(); // deletes series and axis |
|
45 | 45 | |
|
46 | 46 | void setMargin(int margin); |
|
47 | 47 | int margin() const; |
|
48 | 48 | void setChartTheme(QChart::ChartTheme theme); |
|
49 | 49 | QChart::ChartTheme chartTheme() const; |
|
50 | 50 | |
|
51 | 51 | void setChartTitle(const QString& title); |
|
52 | 52 | void setChartTitleFont(const QFont& font); |
|
53 | 53 | void setChartBackgroundBrush(const QBrush& brush); |
|
54 | 54 | void setChartBackgroundPen(const QPen& pen); |
|
55 | 55 | |
|
56 | 56 | void zoomIn(); |
|
57 | 57 | void zoomIn(const QRectF& rect); |
|
58 | 58 | void zoomOut(); |
|
59 | 59 | void zoomReset(); |
|
60 | 60 | |
|
61 | 61 | QChartAxis* axisX() const; |
|
62 | 62 | QChartAxis* axisY() const; |
|
63 | 63 | |
|
64 | 64 | protected: |
|
65 | 65 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
66 | 66 | |
|
67 | 67 | private: |
|
68 | 68 | inline void createChartBackgroundItem(); |
|
69 | 69 | inline void createChartTitleItem(); |
|
70 | 70 | |
|
71 | 71 | private: |
|
72 | 72 | Q_DISABLE_COPY(QChart) |
|
73 | 73 | QGraphicsRectItem* m_backgroundItem; |
|
74 | 74 | QGraphicsTextItem* m_titleItem; |
|
75 | 75 | QRectF m_rect; |
|
76 | 76 | ChartDataSet *m_dataset; |
|
77 | 77 | ChartPresenter *m_presenter; |
|
78 | 78 | }; |
|
79 | 79 | |
|
80 | 80 | QTCOMMERCIALCHART_END_NAMESPACE |
|
81 | 81 | |
|
82 | 82 | #endif |
@@ -1,283 +1,312 | |||
|
1 | 1 | #include "qchartview.h" |
|
2 | 2 | #include "qchart.h" |
|
3 | 3 | #include "qchartaxis.h" |
|
4 | 4 | #include <QGraphicsView> |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | #include <QRubberBand> |
|
7 | 7 | #include <QResizeEvent> |
|
8 | 8 | #include <QDebug> |
|
9 | 9 | |
|
10 | 10 | /*! |
|
11 | 11 | \class QChartView |
|
12 | 12 | \brief Chart widget |
|
13 | 13 | |
|
14 | 14 | QChartView is a standalone widget that can display charts. It does not require QGraphicsScene to work. It manages the graphical |
|
15 | 15 | representation of different types of QChartSeries and other chart related objects like |
|
16 | 16 | QChartAxis and QChartLegend. If you want to display a chart in your existing QGraphicsScene, you can use the QChart class instead. |
|
17 | 17 | |
|
18 | 18 | \sa QChart |
|
19 | 19 | */ |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | 22 | |
|
23 | /*! | |
|
24 | Constructs a chartView object which is a child of a\a parent. | |
|
25 | */ | |
|
23 | 26 | QChartView::QChartView(QWidget *parent) : |
|
24 | 27 | QGraphicsView(parent), |
|
25 | 28 | m_scene(new QGraphicsScene(this)), |
|
26 | 29 | m_chart(new QChart()), |
|
27 | 30 | m_rubberBand(0), |
|
28 | 31 | m_verticalRubberBand(false), |
|
29 | 32 | m_horizonalRubberBand(false) |
|
30 | 33 | { |
|
31 | 34 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
32 | 35 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
33 | 36 | setScene(m_scene); |
|
34 | 37 | m_chart->setMargin(50); |
|
35 | 38 | m_scene->addItem(m_chart); |
|
36 | 39 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
37 | 40 | } |
|
38 | 41 | |
|
39 | 42 | |
|
40 | 43 | /*! |
|
41 | 44 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
42 | 45 | */ |
|
43 | 46 | QChartView::~QChartView() |
|
44 | 47 | { |
|
45 | 48 | } |
|
46 | 49 | |
|
47 | 50 | void QChartView::resizeEvent(QResizeEvent *event) |
|
48 | 51 | { |
|
49 | 52 | m_scene->setSceneRect(0,0,size().width(),size().height()); |
|
50 | 53 | m_chart->resize(size()); |
|
51 | 54 | QWidget::resizeEvent(event); |
|
52 | 55 | } |
|
53 | 56 | |
|
54 | 57 | /*! |
|
55 |
Adds the series and optional |
|
|
58 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. | |
|
56 | 59 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
57 | 60 | the y axis). |
|
58 | \sa removeSeries, removeAllSeries | |
|
61 | \sa removeSeries(), removeAllSeries() | |
|
59 | 62 | */ |
|
60 | 63 | void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY) |
|
61 | 64 | { |
|
62 | 65 | m_chart->addSeries(series,axisY); |
|
63 | 66 | } |
|
64 | 67 | |
|
65 | 68 | /*! |
|
66 |
Removes the |
|
|
69 | Removes the \a series specified in a perameter from the QChartView. | |
|
67 | 70 | It releses its ownership of the specified QChartSeries object. |
|
68 | 71 | It does not delete the pointed QChartSeries data object |
|
69 |
\sa |
|
|
72 | \sa addSeries(), removeAllSeries() | |
|
70 | 73 | */ |
|
71 | 74 | void QChartView::removeSeries(QChartSeries* series) |
|
72 | 75 | { |
|
73 | 76 | m_chart->removeSeries(series); |
|
74 | 77 | } |
|
75 | 78 | |
|
76 | 79 | /*! |
|
77 | 80 | Removes all the QChartSeries that have been added to the QChartView |
|
78 | 81 | It also deletes the pointed QChartSeries data objects |
|
79 | 82 | \sa addSeries(), removeSeries() |
|
80 | 83 | */ |
|
81 | 84 | void QChartView::removeAllSeries() |
|
82 | 85 | { |
|
83 | 86 | m_chart->removeAllSeries(); |
|
84 | 87 | } |
|
85 | 88 | |
|
89 | /*! | |
|
90 | Zooms in the view by a factor of 2 | |
|
91 | */ | |
|
86 | 92 | void QChartView::zoomIn() |
|
87 | 93 | { |
|
88 | 94 | m_chart->zoomIn(); |
|
89 | 95 | } |
|
90 | 96 | |
|
97 | /*! | |
|
98 | Zooms in the view to a maximum level at which \a rect is still fully visible. | |
|
99 | */ | |
|
91 | 100 | void QChartView::zoomIn(const QRect& rect) |
|
92 | 101 | { |
|
93 | 102 | m_chart->zoomIn(rect); |
|
94 | 103 | } |
|
95 | 104 | |
|
105 | /*! | |
|
106 | Restores the view zoom level to the previous one. | |
|
107 | */ | |
|
96 | 108 | void QChartView::zoomOut() |
|
97 | 109 | { |
|
98 | 110 | m_chart->zoomOut(); |
|
99 | 111 | } |
|
100 | 112 | |
|
101 | 113 | /*! |
|
102 | 114 | Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. |
|
103 | 115 | */ |
|
104 | 116 | int QChartView::margin() const |
|
105 | 117 | { |
|
106 | 118 | return m_chart->margin(); |
|
107 | 119 | } |
|
108 | 120 | |
|
109 | 121 | /*! |
|
110 | Sets the chart \a tile. A description text that is rendered above the chart. | |
|
122 | Sets the chart \a title. A description text that is rendered above the chart. | |
|
111 | 123 | */ |
|
112 | 124 | void QChartView::setChartTitle(const QString& title) |
|
113 | 125 | { |
|
114 | 126 | m_chart->setChartTitle(title); |
|
115 | 127 | } |
|
116 | 128 | |
|
117 | 129 | /*! |
|
118 | 130 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
119 | 131 | */ |
|
120 | 132 | void QChartView::setChartTitleFont(const QFont& font) |
|
121 | 133 | { |
|
122 | 134 | m_chart->setChartTitleFont(font); |
|
123 | 135 | } |
|
124 | 136 | |
|
125 | 137 | /*! |
|
126 | 138 | Sets the \a brush that is used for painting the background of the chart area of the QChartView widget. |
|
127 | 139 | */ |
|
128 | 140 | void QChartView::setChartBackgroundBrush(const QBrush& brush) |
|
129 | 141 | { |
|
130 | 142 | m_chart->setChartBackgroundBrush(brush); |
|
131 | 143 | } |
|
132 | 144 | |
|
133 | 145 | /*! |
|
134 | 146 | Sets the \a pen that is used for painting the background of the chart area of the QChartView widget. |
|
135 | 147 | */ |
|
136 | 148 | void QChartView::setChartBackgroundPen(const QPen& pen) |
|
137 | 149 | { |
|
138 | 150 | m_chart->setChartBackgroundPen(pen); |
|
139 | 151 | } |
|
140 | 152 | |
|
153 | /*! | |
|
154 | Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed. | |
|
155 | */ | |
|
141 | 156 | void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) |
|
142 | 157 | { |
|
143 | 158 | switch(policy) { |
|
144 | 159 | case VerticalRubberBand: |
|
145 | 160 | m_verticalRubberBand = true; |
|
146 | 161 | m_horizonalRubberBand = false; |
|
147 | 162 | break; |
|
148 | 163 | case HorizonalRubberBand: |
|
149 | 164 | m_verticalRubberBand = false; |
|
150 | 165 | m_horizonalRubberBand = true; |
|
151 | 166 | break; |
|
152 | 167 | case RectangleRubberBand: |
|
153 | 168 | m_verticalRubberBand = true; |
|
154 | 169 | m_horizonalRubberBand = true; |
|
155 | 170 | break; |
|
156 | 171 | case NoRubberBand: |
|
157 | 172 | default: |
|
158 | 173 | delete m_rubberBand; |
|
159 | 174 | m_rubberBand=0; |
|
160 | 175 | m_horizonalRubberBand = false; |
|
161 | 176 | m_verticalRubberBand = false; |
|
162 | 177 | return; |
|
163 | 178 | } |
|
164 | 179 | if(!m_rubberBand) { |
|
165 | 180 | m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); |
|
166 | 181 | m_rubberBand->setEnabled(true); |
|
167 | 182 | } |
|
168 | 183 | } |
|
169 | 184 | |
|
185 | /*! | |
|
186 | Returns the RubberBandPolicy that is currently being used by the widget. | |
|
187 | */ | |
|
170 | 188 | QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const |
|
171 | 189 | { |
|
172 | 190 | if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand; |
|
173 | 191 | if(m_horizonalRubberBand) return HorizonalRubberBand; |
|
174 | 192 | if(m_verticalRubberBand) return VerticalRubberBand; |
|
175 | 193 | return NoRubberBand; |
|
176 | 194 | } |
|
177 | 195 | |
|
196 | /*! | |
|
197 | If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area. | |
|
198 | If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is not consumed. | |
|
199 | */ | |
|
178 | 200 | void QChartView::mousePressEvent(QMouseEvent *event) |
|
179 | 201 | { |
|
180 | 202 | if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { |
|
181 | 203 | |
|
182 | 204 | int margin = m_chart->margin(); |
|
183 | 205 | QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); |
|
184 | 206 | |
|
185 | 207 | if (rect.contains(event->pos())) { |
|
186 | 208 | m_rubberBandOrigin = event->pos(); |
|
187 | 209 | m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize())); |
|
188 | 210 | m_rubberBand->show(); |
|
189 | 211 | event->accept(); |
|
190 | 212 | } |
|
191 | 213 | } |
|
214 | else { | |
|
215 | QGraphicsView::mousePressEvent(event); | |
|
216 | } | |
|
192 | 217 | } |
|
193 | 218 | |
|
194 | 219 | void QChartView::mouseMoveEvent(QMouseEvent *event) |
|
195 | 220 | { |
|
196 | 221 | if(m_rubberBand && m_rubberBand->isVisible()) { |
|
197 | 222 | int margin = m_chart->margin(); |
|
198 | 223 | QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); |
|
199 | 224 | int width = event->pos().x() - m_rubberBandOrigin.x(); |
|
200 | 225 | int height = event->pos().y() - m_rubberBandOrigin.y(); |
|
201 | 226 | if(!m_verticalRubberBand) { |
|
202 | 227 | m_rubberBandOrigin.setY(rect.top()); |
|
203 | 228 | height = rect.height(); |
|
204 | 229 | } |
|
205 | 230 | if(!m_horizonalRubberBand) { |
|
206 | 231 | m_rubberBandOrigin.setX(rect.left()); |
|
207 | 232 | width= rect.width(); |
|
208 | 233 | } |
|
209 | 234 | m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); |
|
210 | 235 | } |
|
211 | 236 | else { |
|
212 | 237 | QGraphicsView::mouseMoveEvent(event); |
|
213 | 238 | } |
|
214 | 239 | } |
|
215 | 240 | |
|
241 | /*! | |
|
242 | If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand | |
|
243 | If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled. | |
|
244 | */ | |
|
216 | 245 | void QChartView::mouseReleaseEvent(QMouseEvent *event) |
|
217 | 246 | { |
|
218 | 247 | if(m_rubberBand) { |
|
219 | 248 | if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { |
|
220 | 249 | m_rubberBand->hide(); |
|
221 | 250 | QRect rect = m_rubberBand->geometry(); |
|
222 | 251 | m_chart->zoomIn(rect); |
|
223 | 252 | event->accept(); |
|
224 | 253 | } |
|
225 | 254 | |
|
226 | 255 | if(event->button()==Qt::RightButton) |
|
227 | 256 | m_chart->zoomReset(); |
|
228 | 257 | } |
|
229 | 258 | else { |
|
230 | 259 | QGraphicsView::mouseReleaseEvent(event); |
|
231 | 260 | } |
|
232 | 261 | } |
|
233 | 262 | |
|
234 | 263 | void QChartView::keyPressEvent(QKeyEvent *event) |
|
235 | 264 | { |
|
236 | 265 | switch (event->key()) { |
|
237 | 266 | case Qt::Key_Plus: |
|
238 | 267 | zoomIn(); |
|
239 | 268 | break; |
|
240 | 269 | case Qt::Key_Minus: |
|
241 | 270 | zoomOut(); |
|
242 | 271 | break; |
|
243 | 272 | default: |
|
244 | 273 | QGraphicsView::keyPressEvent(event); |
|
245 | 274 | break; |
|
246 | 275 | } |
|
247 | 276 | } |
|
248 | 277 | |
|
249 | 278 | /*! |
|
250 | 279 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
251 | 280 | \sa QChart::ChartTheme, chartTheme() |
|
252 | 281 | */ |
|
253 | 282 | void QChartView::setChartTheme(QChart::ChartTheme theme) |
|
254 | 283 | { |
|
255 | 284 | m_chart->setChartTheme(theme); |
|
256 | 285 | } |
|
257 | 286 | |
|
258 | 287 | /*! |
|
259 | 288 | Returns the theme enum used by the chart. |
|
260 | 289 | \sa setChartTheme() |
|
261 | 290 | */ |
|
262 | 291 | QChart::ChartTheme QChartView::chartTheme() const |
|
263 | 292 | { |
|
264 | 293 | return m_chart->chartTheme(); |
|
265 | 294 | } |
|
266 | 295 | |
|
267 | 296 | /*! |
|
268 | 297 | Returns the pointer to the x axis object of the chart |
|
269 | 298 | */ |
|
270 | 299 | QChartAxis* QChartView::axisX() const |
|
271 | 300 | { |
|
272 | 301 | return m_chart->axisX(); |
|
273 | 302 | } |
|
274 | 303 | |
|
275 | 304 | /*! |
|
276 | 305 | Returns the pointer to the y axis object of the chart |
|
277 | 306 | */ |
|
278 | 307 | QChartAxis* QChartView::axisY() const |
|
279 | 308 | { |
|
280 | 309 | return m_chart->axisY(); |
|
281 | 310 | } |
|
282 | 311 | |
|
283 | 312 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now