##// END OF EJS Templates
modeldata example documented with explanations....
Marek Rosa -
r935:f71e86066044
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -1,247 +1,243
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "widget.h"
22 22
23 23 #include <QChart>
24 24 #include <QScatterSeries>
25 25 #include <QChartAxis>
26 26 #include <QBarSet>
27 27 #include <QBarSeries>
28 #include <QLegend>
28 29
29 30 #include <QGridLayout>
30 31 #include <QPushButton>
31 32 #include <QLabel>
32 33 #include <QListWidget>
33 34 #include <QPrinter>
34 35 #include <QPrintDialog>
35 36 #include <QRadioButton>
36 37 #include <QStringList>
37 38 #include <QSqlQuery>
38 39
39 40 QTCOMMERCIALCHART_USE_NAMESPACE
40 41
41 42 Widget::Widget(QWidget *parent)
42 43 : QWidget(parent)
43 44 {
44 45 setGeometry(100, 100, 1000, 600);
45 46
46 47 // right panel layout
47 48 barChartRadioButton = new QRadioButton(tr("Bar chart"));
48 49 barChartRadioButton->setChecked(true);
49 50 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
50 51 scatterChartRadioButton->setChecked(false);
51 52 countrieslist = new QListWidget;
52 53 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
53 54
54 55 //list of years widget
55 56 yearslist = new QListWidget;
56 57 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
57 58 for (int i = 1990; i < 2011; i++)
58 59 yearslist->addItem(QString("%1").arg(i));
59 60
60 61 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
61 62 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
62 63
63 QPushButton* printButton = new QPushButton(tr("Print chart"));
64 QPushButton* printButton = new QPushButton(tr("Print to pdf"));
64 65 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
65 66
66 67 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
67 68 rightPanelLayout->addWidget(barChartRadioButton);
68 69 rightPanelLayout->addWidget(scatterChartRadioButton);
69 70 rightPanelLayout->addWidget(countrieslist);
70 71 rightPanelLayout->addWidget(yearslist);
71 72 rightPanelLayout->addWidget(refreshButton);
72 73 rightPanelLayout->addWidget(printButton);
73 74 rightPanelLayout->setStretch(0, 1);
74 75 rightPanelLayout->setStretch(1, 0);
75 76
76 77 QChart *chart = new QChart();
77 78 chart->setTitle("GDP by country");
79 chart->legend()->setVisible(true);
78 80
79 81 // main layout
80 82 chartView = new QChartView(chart);
81 83 QGridLayout* mainLayout = new QGridLayout;
82 84 mainLayout->addWidget(chartView, 0, 0);
83 85 mainLayout->addLayout(rightPanelLayout, 0, 1);
84 86 mainLayout->setColumnStretch(0,1);
85 87 setLayout(mainLayout);
86 88
87 89 // connect to the database
88 90 db = QSqlDatabase::addDatabase("QSQLITE");
89 91 db.setDatabaseName("gdpData");
90 92 if(!db.open())
91 93 {
92 94 qDebug() << "could not open database. SQLite db file missing (?)";
93 95 return;
94 96 }
95 97
96 98 // get the list of all countires and regions.
97 99 QSqlQuery query;
98 100 query.exec("SELECT DISTINCT country FROM gdp2");
99 101
100 102 // add the countries to the country filter
101 103 while (query.next()) {
102 104 countrieslist->addItem(query.value(0).toString());
103 105 }
104
105 // hide axis X labels
106 //QChartAxis* axis = chartArea->axisX();
107 // axis->
108 // axis->setLabelsVisible(false);
109 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
110
111 106 }
112 107
113 108 Widget::~Widget()
114 109 {
115 110 //
116 111 db.close();
117 112 }
118 113
119 114 /*!
120 115 refreshes the chart
121 116 */
122 117 void Widget::refreshChart()
123 118 {
124 119 chartView->chart()->removeAllSeries();
125 120
126 121 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
127 122 QStringList selectedCountriesStrings;
128 123 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
129 124 for (int i = 0; i < selectedCountriesItems.size(); i++)
130 125 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
131 126 selectedCountriesStrings.sort();
132 127
133 128 QSqlQuery query;
134 129 // selected years items list is not sorted. copy the values to QList<int> and sort them.
135 130 QList<int> selectedYearsInts;
136 131 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
137 132 for (int i = 0; i < selectedYearsItems.size(); i++)
138 133 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
139 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
134 qSort(selectedYearsInts.begin(), selectedYearsInts.end());
140 135
141 136 if (barChartRadioButton->isChecked())
142 137 {
143 138 // use the sorted selected coutries list to initialize BarCategory
144 139 QStringList category;
145 140 for (int i = 0; i < selectedCountriesStrings.size(); i++)
146 141 category << selectedCountriesStrings[i];
147 142 QBarSeries* series0 = new QBarSeries(category);
148 143 series0 = new QBarSeries(category);
149 144
150 145 // prepare the selected counries SQL query
151 146 QString countriesQuery = "country IN (";
152 147 for (int i = 0; i < selectedCountriesStrings.size(); i++)
153 148 {
154 149 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
155 150 if ( i < selectedCountriesStrings.size() - 1)
156 151 countriesQuery.append(",");
157 152 else
158 153 countriesQuery.append(")");
159 154 }
160 155
161 156 // perform a query for each selected year
162 157 for (int i = 0; i < selectedYearsInts.size(); i++)
163 158 {
164 159 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
165 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
160 QBarSet* barSet = new QBarSet(QString::number(selectedYearsInts[i]));
166 161
167 162 // while (query.next()) {
168 163 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
169 164 // }
170 165 query.first();
171 166
172 167 // the data for some of the coutries for some years might be missing.
173 168 // QBarChart needs bars to have same size
174 169 for (int k = 0; k < selectedCountriesStrings.size(); k++)
175 170 {
176 171 if (selectedCountriesStrings[k] == query.value(0).toString())
177 172 {
178 173 *barSet << query.value(1).toReal();
179 174 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
180 175 query.next();
181 176 }
182 177 else
183 178 {
184 179 // data missing, put 0
185 180 *barSet << 0.0f;
186 181 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
187 182 }
188 183 }
189 184 series0->appendBarSet(barSet);
190 185 }
191 186 // add the serie to the chart
192 187 chartView->chart()->addSeries(series0);
193 188 }
194 189 else if (scatterChartRadioButton->isChecked())
195 190 {
196 191 QString yearsQuery = "year IN (";
197 192 for (int i = 0; i < selectedYearsInts.size(); i++)
198 193 {
199 194 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
200 195 if ( i < selectedYearsInts.size() - 1)
201 196 yearsQuery.append(",");
202 197 else
203 198 yearsQuery.append(")");
204 199 }
205 200
206 201 // perform a query for each selected country
207 202 for (int i = 0; i < selectedCountriesStrings.size(); i++)
208 203 {
209 204 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
210 205 query.first();
211 206
212 207 QScatterSeries* series = new QScatterSeries;
208 series->setName(selectedCountriesStrings[i]);
213 209 // the data for some of the coutries for some years might be missing.
214 210 for (int k = 0; k < selectedYearsInts.size(); k++)
215 211 {
216 212 if (selectedYearsInts[k] == query.value(0).toInt())
217 213 {
218 214 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
219 215 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
220 216 query.next();
221 217 }
222 218 else
223 219 {
224 220 // data missing, put 0
225 221 *series << QPointF(selectedYearsInts[k] , 0.0f);
226 222 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
227 223 }
228 224 }
229 225 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
230 226 chartView->chart()->addSeries(series);
231 227 }
232 228 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
233 229 }
234 230 }
235 231
236 232 void Widget::printChart()
237 233 {
238 234 QPrinter printer;
239 235 // QPrinter printer(QPrinter::HighResolution);
240 236 printer.setOutputFormat(QPrinter::PdfFormat);
241 237 printer.setOrientation(QPrinter::Landscape);
242 238 printer.setOutputFileName("print.pdf");
243 239
244 240 QPainter painter;
245 241 painter.begin(&printer);
246 242 chartView->render(&painter);
247 243 }
@@ -1,7 +1,56
1 1 /*!
2 2 \example examples/modeldata
3 3 \title Model data example
4 4 \subtitle
5 5
6 6 This example shows how to use QAbstractItemModel derived model as the data for the series.
7
8 \image modeldata.png
9
10 Let's start by creating an instance of CustomTableModel class.
11 CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example.
12 The constructor of this class populates the internal data store of the model with the data that is good for our chart example.
13
14 \snippet ../examples/modeldata/tablewidget.cpp 1
15
16 We now have a model with data that we would like to display both on the chart and in a QTableView.
17 First, we create QTableView and tell it use the model as a data source.
18
19 \snippet ../examples/modeldata/tablewidget.cpp 2
20
21 Now we need QChart instance to display the same data on the chart.
22 We also enable animations. It makes it easier to see how modifying the model's data affect the chart.
23
24 \snippet ../examples/modeldata/tablewidget.cpp 3
25
26 Then let's create two QLineSeries and tell them to use the data from the model.
27 First line of the code below creates new line series. Line number two sets the model as the data source for the series.
28 Third line specifies that x coordinates are taken from the model's column(Qt::Vertical) with index 0 and the y coordinates are taken from the model's column with index 1.
29 Finally the series is added to the chart.
30
31 \snippet ../examples/modeldata/tablewidget.cpp 4
32
33 To show in QTableView which data coresponds with which series this example uses table coloring.
34 When series is added to the chart it is assigned a color beased on the currently selected theme.
35 Code below extracts that color from the series and uses it to create colored QTableView.
36 Coloring of the view is not a part of the QChart functionality.
37
38 \snippet ../examples/modeldata/tablewidget.cpp 5
39
40 The same operations are done with second series. Notice that for this series different columns of the same model are mapped.
41
42 \snippet ../examples/modeldata/tablewidget.cpp 6
43
44 \snippet ../examples/modeldata/tablewidget.cpp 7
45
46 To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor.
47 To make the render look nicer Antialiasing is turned on and the minimum size of the chart is set.
48
49 \snippet ../examples/modeldata/tablewidget.cpp 8
50
51 Finally we place both widgets in a layout and use the layout as the application layout.
52
53 \snippet ../examples/modeldata/tablewidget.cpp 9
54
55 Application is ready. Try modifying the data in the table view and see how it affects the chart.
7 56 */
@@ -1,82 +1,101
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include "customtablemodel.h"
23 23 #include <QGridLayout>
24 24 #include <QTableView>
25 25 #include <QChart>
26 26 #include <QChartView>
27 27 #include <QLineSeries>
28 28
29 29 QTCOMMERCIALCHART_USE_NAMESPACE
30 30
31 31 TableWidget::TableWidget(QWidget *parent)
32 32 : QWidget(parent)
33 33 {
34 34 // create simple model for storing data
35 35 // user's table data model
36 //! [1]
36 37 CustomTableModel *model = new CustomTableModel;
38 //! [1]
37 39
40 //! [2]
38 41 // create table view and add model to it
39 42 QTableView *tableView = new QTableView;
40 43 tableView->setModel(model);
44 //! [2]
41 45 tableView->setColumnWidth(0, 56);
42 46 tableView->setColumnWidth(1, 56);
43 47 tableView->setColumnWidth(2, 56);
44 48 tableView->setColumnWidth(3, 56);
45 49
46 QChart *m_chart = new QChart;
47 m_chart->setAnimationOptions(QChart::AllAnimations);
48 QChartView *m_chartView = new QChartView(m_chart);
49 m_chartView->setRenderHint(QPainter::Antialiasing);
50 m_chartView->setMinimumSize(640, 480);
50 //! [3]
51 QChart *chart = new QChart;
52 chart->setAnimationOptions(QChart::AllAnimations);
53 //! [3]
51 54
55 // series 1
56 //! [4]
57 QLineSeries *series = new QLineSeries;
58 series->setModel(model);
59 series->setModelMapping(0, 1, Qt::Vertical);
60 chart->addSeries(series);
61 //! [4]
62
63 //! [5]
52 64 // for storing color hex from the series
53 65 QString seriesColorHex = "#000000";
54 66
55 // series 1
56 QLineSeries *m_series = new QLineSeries;
57 m_series->setModel(model);
58 m_series->setModelMapping(0, 1, Qt::Vertical);
59 m_chart->addSeries(m_series);
60
61 67 // get the color of the series and use it for showing the mapped area
62 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
68 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
63 69 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
70 //! [5]
64 71
65 72 // series 2
66 m_series = new QLineSeries;
67 m_series->setModel(model);
68 m_series->setModelMapping(2,3, Qt::Vertical);
69 m_chart->addSeries(m_series);
73 //! [6]
74 series = new QLineSeries;
75 series->setModel(model);
76 series->setModelMapping(2,3, Qt::Vertical);
77 chart->addSeries(series);
78 //! [6]
70 79
80 //! [7]
71 81 // get the color of the series and use it for showing the mapped area
72 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
82 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
73 83 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
84 //! [7]
85
86 //! [8]
87 QChartView *chartView = new QChartView(chart);
88 chartView->setRenderHint(QPainter::Antialiasing);
89 chartView->setMinimumSize(640, 480);
90 //! [8]
74 91
92 //! [9]
75 93 // create main layout
76 94 QGridLayout* mainLayout = new QGridLayout;
77 95 mainLayout->addWidget(tableView, 1, 0);
78 mainLayout->addWidget(m_chartView, 1, 1);
96 mainLayout->addWidget(chartView, 1, 1);
79 97 mainLayout->setColumnStretch(1, 1);
80 98 mainLayout->setColumnStretch(0, 0);
81 99 setLayout(mainLayout);
100 //! [9]
82 101 }
@@ -1,421 +1,419
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qchartaxis.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QChartAxis
27 27 \brief The QChartAxis class is used for manipulating chart's axis
28 28 and for adding optional axes to the chart.
29 29 \mainclass
30 30
31 31 There is only one x Axis, however there can be multiple y axes.
32 32 Each chart series can be bound to exactly one Y axis and the share common X axis.
33 33 Axis can be setup to show axis line with ticks, gird lines and shades.
34 34
35 35 */
36 36
37 37 /*!
38 38 \fn bool QChartAxis::isAxisVisible() const
39 39 \brief Returns if axis is visible
40 40 \sa setAxisVisible()
41 41 */
42 42
43 43 /*!
44 44 \fn QPen QChartAxis::axisPen() const
45 45 \brief Returns pen used to draw axis and ticks.
46 46 \sa setAxisPen()
47 47 */
48 48
49 49
50 50 /*!
51 51 \fn bool QChartAxis::isGridLineVisible() const
52 52 \brief Returns if grid is visible
53 53 \sa setGridLineVisible()
54 54 */
55 55
56 56 /*!
57 57 \fn QPen QChartAxis::gridLinePen() const
58 58 \brief Returns pen used to draw grid.
59 59 \sa setGridLinePen()
60 60 */
61 61
62 62 /*!
63 63 \fn bool QChartAxis::labelsVisible() const
64 64 \brief Returns if grid is visible
65 65 \sa setLabelsVisible()
66 66 */
67 67
68 68 /*!
69 69 \fn QPen QChartAxis::labelsPen() const
70 70 \brief Returns the pen used to labels.
71 71 \sa setLabelsPen()
72 72 */
73 73
74 74 /*!
75 75 \fn QBrush QChartAxis::labelsBrush() const
76 76 \brief Returns brush used to draw labels.
77 77 \sa setLabelsBrush()
78 78 */
79 79
80 80 /*!
81 81 \fn QFont QChartAxis::labelsFont() const
82 82 \brief Returns font used to draw labels.
83 83 \sa setLabelsFont()
84 84 */
85 85
86 86 /*!
87 87 \fn QFont QChartAxis::labelsAngle() const
88 88 \brief Returns angle used to draw labels.
89 89 \sa setLabelsAngle()
90 90 */
91 91
92 92 /*!
93 93 \fn bool QChartAxis::shadesVisible() const
94 94 \brief Returns if shades are visible.
95 95 \sa setShadesVisible()
96 96 */
97 97
98 98 /*!
99 99 \fn qreal QChartAxis::shadesOpacity() const
100 100 \brief Returns opacity of shades.
101 101 */
102 102
103 103 /*!
104 104 \fn QPen QChartAxis::shadesPen() const
105 105 \brief Returns pen used to draw shades.
106 106 \sa setShadesPen()
107 107 */
108 108
109 109 /*!
110 110 \fn QBrush QChartAxis::shadesBrush() const
111 111 \brief Returns brush used to draw shades.
112 112 \sa setShadesBrush()
113 113 */
114 114
115 115 /*!
116 116 \fn qreal QChartAxis::min() const
117 117 \brief Returns minimum value on the axis.
118 118 \sa setMin()
119 119 */
120 120
121 121 /*!
122 122 \fn qreal QChartAxis::max() const
123 123 \brief Returns maximim value on the axis.
124 124 \sa setMax()
125 125 */
126 126
127 127 /*!
128 128 \fn void QChartAxis::minChanged(qreal min)
129 129 \brief Axis emits signal when \a min of axis has changed.
130 130 */
131 131
132 132 /*!
133 133 \fn void QChartAxis::maxChanged(qreal max)
134 134 \brief Axis emits signal when \a max of axis has changed.
135 135 */
136 136
137 137 /*!
138 138 \fn void QChartAxis::rangeChanged(qreal min, qreal max)
139 139 \brief Axis emits signal when \a min or \a max of axis has changed.
140 140 */
141 141
142 142 /*!
143 143 \fn int QChartAxis::ticksCount() const
144 144 \brief Return number of ticks on the axis
145 145 \sa setTicksCount()
146 146 */
147 147
148 148 /*!
149 149 \fn void QChartAxis::updated()
150 150 \brief \internal
151 151 */
152 152
153 153 /*!
154 \fn void QChartAxis::handleAxisRangeChanged(qreal min, qreal max)
155 \brief \internal \a min \a max
156 */
157
158 /*!
159 154 Constructs new axis object which is a child of \a parent. Ownership is taken by
160 155 QChatView or QChart when axis added.
161 156 */
162 157
163 158 QChartAxis::QChartAxis(QObject *parent) : QObject(parent),
164 159 m_axisVisible(true),
165 160 m_gridLineVisible(true),
166 161 m_labelsVisible(true),
167 162 m_labelsAngle(0),
168 163 m_shadesVisible(false),
169 164 m_shadesOpacity(1.0),
170 165 m_min(0),
171 166 m_max(0),
172 167 m_ticksCount(5),
173 168 m_niceNumbers(false)
174 169 {
175 170
176 171 }
177 172
178 173 /*!
179 174 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
180 175 */
181 176
182 177 QChartAxis::~QChartAxis()
183 178 {
184 179 }
185 180
186 181 /*!
187 182 Sets \a pen used to draw axis line and ticks.
188 183 */
189 184 void QChartAxis::setAxisPen(const QPen &pen)
190 185 {
191 186 if (pen != m_axisPen) {
192 187 m_axisPen = pen;
193 188 emit updated();
194 189 }
195 190 }
196 191
197 192 /*!
198 193 Sets if axis and ticks are \a visible.
199 194 */
200 195 void QChartAxis::setAxisVisible(bool visible)
201 196 {
202 197 if (m_axisVisible != visible) {
203 198 m_axisVisible = visible;
204 199 emit updated();
205 200 }
206 201 }
207 202
208 203 /*!
209 204 Sets if grid line is \a visible.
210 205 */
211 206 void QChartAxis::setGridLineVisible(bool visible)
212 207 {
213 208 if (m_gridLineVisible != visible) {
214 209 m_gridLineVisible = visible;
215 210 emit updated();
216 211 }
217 212 }
218 213
219 214 /*!
220 215 Sets \a pen used to draw grid line.
221 216 */
222 217 void QChartAxis::setGridLinePen(const QPen &pen)
223 218 {
224 219 if (m_gridLinePen != pen) {
225 220 m_gridLinePen = pen;
226 221 emit updated();
227 222 }
228 223 }
229 224
230 225 /*!
231 226 Sets if axis' labels are \a visible.
232 227 */
233 228 void QChartAxis::setLabelsVisible(bool visible)
234 229 {
235 230 if (m_labelsVisible != visible) {
236 231 m_labelsVisible = visible;
237 232 emit updated();
238 233 }
239 234 }
240 235
241 236 /*!
242 237 Sets \a pen used to draw labels.
243 238 */
244 239 void QChartAxis::setLabelsPen(const QPen &pen)
245 240 {
246 241 if (m_labelsPen != pen) {
247 242 m_labelsPen = pen;
248 243 emit updated();
249 244 }
250 245 }
251 246
252 247 /*!
253 248 Sets \a brush used to draw labels.
254 249 */
255 250 void QChartAxis::setLabelsBrush(const QBrush &brush)
256 251 {
257 252 if (m_labelsBrush != brush) {
258 253 m_labelsBrush = brush;
259 254 emit updated();
260 255 }
261 256 }
262 257
263 258 /*!
264 259 Sets \a font used to draw labels.
265 260 */
266 261 void QChartAxis::setLabelsFont(const QFont &font)
267 262 {
268 263 if (m_labelsFont != font) {
269 264 m_labelsFont = font;
270 265 emit updated();
271 266 }
272 267 }
273 268
274 269 /*!
275 270 Sets \a angle for all the labels on given axis.
276 271 */
277 272 void QChartAxis::setLabelsAngle(int angle)
278 273 {
279 274 if (m_labelsAngle != angle) {
280 275 m_labelsAngle = angle;
281 276 emit updated();
282 277 }
283 278 }
284 279
285 280 /*!
286 281 Sets if shades are \a visible.
287 282 */
288 283 void QChartAxis::setShadesVisible(bool visible)
289 284 {
290 285 if (m_shadesVisible != visible) {
291 286 m_shadesVisible = visible;
292 287 emit updated();
293 288 }
294 289 }
295 290
296 291 /*!
297 292 Sets \a pen used to draw shades.
298 293 */
299 294 void QChartAxis::setShadesPen(const QPen &pen)
300 295 {
301 296 if (m_shadesPen != pen) {
302 297 m_shadesPen = pen;
303 298 emit updated();
304 299 }
305 300 }
306 301
307 302 /*!
308 303 Sets \a brush used to draw shades.
309 304 */
310 305 void QChartAxis::setShadesBrush(const QBrush &brush)
311 306 {
312 307 if (m_shadesBrush != brush) {
313 308 m_shadesBrush = brush;
314 309 emit updated();
315 310 }
316 311 }
317 312
318 313 /*!
319 314 Sets \a opacity of the shades.
320 315 */
321 316 void QChartAxis::setShadesOpacity(qreal opacity)
322 317 {
323 318 if (m_shadesOpacity != opacity) {
324 319 m_shadesOpacity=opacity;
325 320 emit updated();
326 321 }
327 322 }
328 323
329 324 /*!
330 325 Sets \a min value on the axis.
331 326 */
332 327 void QChartAxis::setMin(qreal min)
333 328 {
334 329 setRange(min,m_max);
335 330 }
336 331
337 332 /*!
338 333 Sets \a max value on the axis.
339 334 */
340 335 void QChartAxis::setMax(qreal max)
341 336 {
342 337 setRange(m_min,max);
343 338 }
344 339
345 340 /*!
346 341 Sets range from \a min to \a max on the axis.
347 342 */
348 343 void QChartAxis::setRange(qreal min, qreal max)
349 344 {
350 345 bool changed = false;
351 346 if (!qFuzzyIsNull(m_min - min)) {
352 347 m_min = min;
353 348 changed = true;
354 349 emit minChanged(min);
355 350 }
356 351
357 352 if (!qFuzzyIsNull(m_max - max)) {
358 353 m_max = max;
359 354 changed = true;
360 355 emit maxChanged(max);
361 356 }
362 357
363 358 if (changed) {
364 359 emit rangeChanged(m_min,m_max);
365 360 emit this->changed(m_min, m_max, m_ticksCount, m_niceNumbers);
366 361 }
367 362 }
368 363
369 364 /*!
370 365 Sets \a count for ticks on the axis.
371 366 */
372 367 void QChartAxis::setTicksCount(int count)
373 368 {
374 369 if (m_ticksCount != count) {
375 370 m_ticksCount = count;
376 371 emit ticksCountChanged(count);
377 372 emit changed(m_min, m_max, m_ticksCount, m_niceNumbers);
378 373 }
379 374 }
380 375
381 376 /*!
382 377 Sets axis, shades, labels and grid lines to be visible.
383 378 */
384 379 void QChartAxis::show()
385 380 {
386 381 m_axisVisible=true;
387 382 m_gridLineVisible=true;
388 383 m_labelsVisible=true;
389 384 m_shadesVisible=true;
390 385 emit updated();
391 386 }
392 387
393 388 /*!
394 389 Sets axis, shades, labels and grid lines to not be visible.
395 390 */
396 391 void QChartAxis::hide()
397 392 {
398 393 m_axisVisible = false;
399 394 m_gridLineVisible = false;
400 395 m_labelsVisible = false;
401 396 m_shadesVisible = false;
402 397 emit updated();
403 398 }
404 399
400 /*!
401 \internal
402 */
405 403 void QChartAxis::handleAxisRangeChanged(qreal min, qreal max,int count)
406 404 {
407 405 setRange(min,max);
408 406 setTicksCount(count);
409 407 }
410 408
411 409 void QChartAxis::setNiceNumbers(bool enabled)
412 410 {
413 411 if (m_niceNumbers != enabled){
414 412 m_niceNumbers = enabled;
415 413 emit changed(m_min, m_max, m_ticksCount, m_niceNumbers);
416 414 }
417 415 }
418 416
419 417 #include "moc_qchartaxis.cpp"
420 418
421 419 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,250 +1,250
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qchartview.h"
22 22 #include "qchart_p.h"
23 23 #include "qchartview_p.h"
24 24 #include <QGraphicsScene>
25 25 #include <QRubberBand>
26 26
27 27
28 28 /*!
29 \enum QChartView::RubberBandPolicy
29 \enum QChartView::RubberBand
30 30
31 31 This enum describes the different types of rubber bands that can be used for zoom rect selection
32 32
33 33 \value NoRubberBand
34 34 \value VerticalRubberBand
35 35 \value HorizonalRubberBand
36 36 \value RectangleRubberBand
37 37 */
38 38
39 39 /*!
40 40 \class QChartView
41 41 \brief Standalone charting widget.
42 42
43 43 QChartView is a standalone widget that can display charts. It does not require separate
44 44 QGraphicsScene to work. It manages the graphical representation of different types of
45 45 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
46 46 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
47 47
48 48 \sa QChart
49 49 */
50 50
51 51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52 52
53 53 /*!
54 54 Constructs a chartView object which is a child of a\a parent.
55 55 */
56 56 QChartView::QChartView(QChart *chart,QWidget *parent) :
57 57 QGraphicsView(parent),
58 58 d_ptr(new QChartViewPrivate())
59 59 {
60 60 Q_ASSERT(chart);
61 61 d_ptr->m_scene = new QGraphicsScene(this);
62 62 d_ptr->m_chart = chart;
63 63 setFrameShape(QFrame::NoFrame);
64 64 setBackgroundRole(QPalette::Window);
65 65 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 66 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 67 setScene(d_ptr->m_scene);
68 68 d_ptr->m_scene->addItem(chart);
69 69 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
70 70 }
71 71
72 72
73 73 /*!
74 74 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
75 75 */
76 76 QChartView::~QChartView()
77 77 {
78 78 }
79 79
80 80 /*!
81 81 Returns the pointer to the associated chart
82 82 */
83 83 QChart* QChartView::chart() const
84 84 {
85 85 return d_ptr->m_chart;
86 86 }
87 87
88 88 /*!
89 89 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
90 90 */
91 91 void QChartView::setRubberBand(const RubberBands& rubberBand)
92 92 {
93 93 d_ptr->m_rubberBandFlags=rubberBand;
94 94
95 95 if (!d_ptr->m_rubberBandFlags) {
96 96 delete d_ptr->m_rubberBand;
97 97 d_ptr->m_rubberBand=0;
98 98 return;
99 99 }
100 100
101 101 if (!d_ptr->m_rubberBand) {
102 102 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
103 103 d_ptr->m_rubberBand->setEnabled(true);
104 104 }
105 105 }
106 106
107 107 /*!
108 108 Returns the RubberBandPolicy that is currently being used by the widget.
109 109 */
110 110 QChartView::RubberBands QChartView::rubberBand() const
111 111 {
112 112 return d_ptr->m_rubberBandFlags;
113 113 }
114 114
115 115 /*!
116 116 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.
117 117 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
118 118 */
119 119 void QChartView::mousePressEvent(QMouseEvent *event)
120 120 {
121 121 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
122 122
123 123 int padding = d_ptr->m_chart->margins().top();
124 124 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
125 125
126 126 if (rect.contains(event->pos())) {
127 127 d_ptr->m_rubberBandOrigin = event->pos();
128 128 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
129 129 d_ptr->m_rubberBand->show();
130 130 event->accept();
131 131 }
132 132 }
133 133 else {
134 134 QGraphicsView::mousePressEvent(event);
135 135 }
136 136 }
137 137
138 138 /*!
139 139 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
140 140 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
141 141 */
142 142 void QChartView::mouseMoveEvent(QMouseEvent *event)
143 143 {
144 144 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
145 145 QRectF margins = d_ptr->m_chart->margins();
146 146 QRectF geometry = d_ptr->m_chart->geometry();
147 147 QRectF rect =geometry.adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom());
148 148 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
149 149 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
150 150 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
151 151 d_ptr->m_rubberBandOrigin.setY(rect.top());
152 152 height = rect.height();
153 153 }
154 154 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
155 155 d_ptr->m_rubberBandOrigin.setX(rect.left());
156 156 width= rect.width();
157 157 }
158 158 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
159 159 }
160 160 else {
161 161 QGraphicsView::mouseMoveEvent(event);
162 162 }
163 163 }
164 164
165 165 /*!
166 166 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
167 167 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
168 168 */
169 169 void QChartView::mouseReleaseEvent(QMouseEvent *event)
170 170 {
171 171 if(d_ptr->m_rubberBand) {
172 172 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
173 173 d_ptr->m_rubberBand->hide();
174 174 QRect rect = d_ptr->m_rubberBand->geometry();
175 175 d_ptr->m_chart->zoomIn(rect);
176 176 event->accept();
177 177 }
178 178
179 179 if(event->button()==Qt::RightButton){
180 180 d_ptr->m_chart->zoomOut();
181 181 event->accept();
182 182 }
183 183 }
184 184 else {
185 185 QGraphicsView::mouseReleaseEvent(event);
186 186 }
187 187 }
188 188
189 189 /*!
190 190 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
191 191 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
192 192 */
193 193 void QChartView::keyPressEvent(QKeyEvent *event)
194 194 {
195 195 switch (event->key()) {
196 196 case Qt::Key_Plus:
197 197 d_ptr->m_chart->zoomIn();
198 198 break;
199 199 case Qt::Key_Minus:
200 200 d_ptr->m_chart->zoomOut();
201 201 break;
202 202 case Qt::Key_Left:
203 203 d_ptr->m_chart->scrollLeft();
204 204 break;
205 205 case Qt::Key_Right:
206 206 d_ptr->m_chart->scrollRight();
207 207 break;
208 208 case Qt::Key_Up:
209 209 d_ptr->m_chart->scrollUp();
210 210 break;
211 211 case Qt::Key_Down:
212 212 d_ptr->m_chart->scrollDown();
213 213 break;
214 214 default:
215 215 QGraphicsView::keyPressEvent(event);
216 216 break;
217 217 }
218 218 }
219 219
220 220 /*!
221 221 Resizes and updates the chart area using the \a event data
222 222 */
223 223 void QChartView::resizeEvent(QResizeEvent *event)
224 224 {
225 225 QGraphicsView::resizeEvent(event);
226 226 d_ptr->m_chart->resize(size());
227 227 setMinimumSize(d_ptr->m_chart->minimumSize().toSize());
228 228 setSceneRect(d_ptr->m_chart->geometry());
229 229 }
230 230
231 231 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232 232
233 233 QChartViewPrivate::QChartViewPrivate():
234 234 m_scene(0),
235 235 m_chart(0),
236 236 m_presenter(0),
237 237 m_rubberBand(0),
238 238 m_rubberBandFlags(QChartView::NoRubberBand)
239 239 {
240 240
241 241 }
242 242
243 243 QChartViewPrivate::~QChartViewPrivate()
244 244 {
245 245
246 246 }
247 247
248 248 #include "moc_qchartview.cpp"
249 249
250 250 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,97 +1,97
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qseries.h"
22 22
23 23 /*!
24 24 \class QSeries
25 25 \brief Base class for all QtCommercial Chart series.
26 26 \mainclass
27 27
28 28 Usually you use the series type specific inherited classes instead of the base class.
29 29 \sa QScatterSeries
30 30 */
31 31
32 32 /*!
33 33 \enum QSeries::QSeriesType
34 34
35 35 The type of the series object.
36 36
37 37 \value SeriesTypeLine
38 38 \value SeriesTypeArea
39 39 \value SeriesTypeBar
40 40 \value SeriesTypeStackedBar
41 41 \value SeriesTypePercentBar
42 42 \value SeriesTypePie
43 43 \value SeriesTypeScatter
44 44 \value SeriesTypeSpline
45 45 */
46 46
47 47 /*!
48 48 \fn QSeries::QSeries(QObject *parent)
49 49 \brief Constructs ChartSeries object with \a parent.
50 50 */
51 51
52 52 /*!
53 53 \fn QSeries::~QSeries()
54 54 \brief Virtual destructor for the chart series.
55 55 */
56 56
57 57 /*!
58 58 \fn QSeriesType QSeries::type() const
59 59 \brief The type of the series.
60 60 */
61 61
62 62 /*!
63 63 \fn bool QSeries::setModel(QAbstractItemModel *model)
64 64 \brief Use the \a model to provide data for the series. The model overrides possible user data
65 65 set with QChartSeries type specific data setters. For example if you call both
66 66 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
67 67 used by the series. Returns true if the model is valid for the series.
68 68 */
69 69
70 70 /*!
71 71 \fn QAbstractItemModel* QSeries::model() const
72 72 \brief Returns the pointer to the model that is used as the series data source
73 73 */
74 74
75 75 /*!
76 \property QString QSeries::name
76 \property QSeries::name
77 77 \brief name of the series property
78 78 */
79 79
80 80 /*!
81 81 \fn void QSeries::setName(QString name)
82 82 \brief Sets a \a name for the series.
83 83
84 84 The name of a series is shown in the legend for QXYSeries.
85 85 \sa QChart::setTitle()
86 86 \sa QPieSlice::setLabel()
87 87 \sa QBarSet::setName()
88 88 */
89 89
90 90 /*!
91 91 \fn QString QSeries::name() const
92 92 \brief Returns the name of the series.
93 93 \sa setName()
94 94 */
95 95
96 96 QTCOMMERCIALCHART_USE_NAMESPACE
97 97 #include "moc_qseries.cpp"
@@ -1,72 +1,71
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QSERIES_H
22 22 #define QSERIES_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QObject>
26 //#include <QAbstractItemModel>
27 26 #include <QPen>
28 27
29 28 class QAbstractItemModel;
30 29
31 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 31
33 32 class QTCOMMERCIALCHART_EXPORT QSeries : public QObject
34 33 {
35 34 Q_OBJECT
36 35 Q_PROPERTY(QString name READ name WRITE setName)
37 36
38 37 public:
39 38 enum QSeriesType {
40 39 SeriesTypeLine,
41 40 SeriesTypeArea,
42 41 SeriesTypeBar,
43 42 SeriesTypeStackedBar,
44 43 SeriesTypePercentBar,
45 44 SeriesTypePie,
46 45 SeriesTypeScatter,
47 46 SeriesTypeSpline
48 47 };
49 48
50 49 protected:
51 50 QSeries(QObject *parent = 0) : QObject(parent) {m_model = 0;}
52 51
53 52 public:
54 53 virtual ~QSeries() {}
55 54 virtual QSeriesType type() const = 0;
56 55 // TODO
57 56 virtual bool setModel(QAbstractItemModel* /*model*/) { return false; }
58 57 QAbstractItemModel* model() const { return m_model; }
59 58
60 59 void setName(QString name) { m_name = name; }
61 60 QString name() const { return m_name; }
62 61
63 62 protected:
64 63 QAbstractItemModel* m_model;
65 64
66 65 private:
67 66 QString m_name;
68 67 };
69 68
70 69 QTCOMMERCIALCHART_END_NAMESPACE
71 70
72 71 #endif
General Comments 0
You need to be logged in to leave comments. Login now