##// END OF EJS Templates
Fixed category handling of barcharts. Now the categories can be undefined. Updated documentation.
sauimone -
r1208:2943560d5819
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,40
1 /*!
2 \example examples/groupedbarchart
3 \title GroupedBarChart Example
4 \subtitle
5
6 The example shows how to create simple bar chart. GroupedBarChart shows the data in sets as separate bars, which are grouped in categories.
7
8 \image examples_groupedbarchart.png
9
10 First we define categories.
11
12 \snippet ../examples/groupedbarchart/main.cpp 1
13
14 Data that groupedbarchart visualizes, is defined by QBarSet instances. Here we create some sets and append data
15 we want to visualize to them.
16
17 \snippet ../examples/groupedbarchart/main.cpp 2
18
19 To combine the sets and categories to a chart, we need to create QBarSeries instance. When creating
20 the QBarSeries, the categories must be known. Sets can be added later. For example purposes the sets
21 are added to series here.
22
23 \snippet ../examples/groupedbarchart/main.cpp 3
24
25 Then we create a chart and add the series to it.
26
27 \snippet ../examples/groupedbarchart/main.cpp 4
28
29 And we also want to show the legend, so that the data is easier to read.
30
31 \snippet ../examples/groupedbarchart/main.cpp 5
32
33 Finally we add the chart onto a view.
34
35 \snippet ../examples/groupedbarchart/main.cpp 6
36
37 And it is ready to be shown in a window.
38
39 \snippet ../examples/groupedbarchart/main.cpp 7
40 */
1 NO CONTENT: modified file, binary diff hidden
@@ -1,40 +1,34
1 1 /*!
2 2 \example examples/barchart
3 3 \title BarChart Example
4 4 \subtitle
5 5
6 The example shows how to create simple bar chart. Barchart shows the data in sets as separate bars, which are grouped in categories.
6 The example shows how to create simple bar chart. Barchart shows the data in sets as separate bars, which are drawn at the x-axis to the position defined by data.
7 7
8 8 \image examples_barchart.png
9 9
10 First we define categories.
10 Data that barchart visualizes, is defined by QBarSet instances. Here we create some sets and append data
11 we want to visualize to them. Note that first and third QPointF in each set have smaller intervals at their x coordinates. This will be reflected in the chart.
11 12
12 13 \snippet ../examples/barchart/main.cpp 1
13 14
14 Data that barchart visualizes, is defined by QBarSet instances. Here we create some sets and append data
15 we want to visualize to them.
15 To combine the sets to a chart, we need to create QBarSeries instance. Then we append our barsets to the series.
16 16
17 17 \snippet ../examples/barchart/main.cpp 2
18 18
19 To combine the sets and categories to a chart, we need to create QBarSeries instance. When creating
20 the QBarSeries, the categories must be known. Sets can be added later. For example purposes the sets
21 are added to series here.
19 Next we create a chart and add the series to it.
22 20
23 21 \snippet ../examples/barchart/main.cpp 3
24 22
25 Then we create a chart and add the series to it.
23 We want to have the legend displayed at the bottom of the chart. Also we turn on the nice numbers algorithm on for y-axis.
26 24
27 25 \snippet ../examples/barchart/main.cpp 4
28 26
29 And we also want to show the legend, so that the data is easier to read.
30
31 \snippet ../examples/barchart/main.cpp 5
32
33 27 Finally we add the chart onto a view.
34 28
35 \snippet ../examples/barchart/main.cpp 6
29 \snippet ../examples/barchart/main.cpp 5
36 30
37 31 And it is ready to be shown in a window.
38 32
39 \snippet ../examples/percentbarchart/main.cpp 7
33 \snippet ../examples/percentbarchart/main.cpp 6
40 34 */
@@ -1,36 +1,37
1 1 /*!
2 2 \page examples.html
3 3 \title Examples
4 4 \keyword Examples
5 5
6 6 \raw HTML
7 7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 8 <tr>
9 9 <th class="titleheader" width="33%">
10 10 List of examples
11 11 </th>
12 12 </tr>
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 16 <li><a href="examples-areachart.html">Area chart</a></li>
17 17 <li><a href="examples-barchart.html">Bar chart</a></li>
18 18 <li><a href="examples-customchart.html">Custom chart</a></li>
19 <li><a href="examples-groupedbarchart.html">Grouped bar chart</a></li>
19 20 <li><a href="examples-linechart.html">Line chart</a></li>
20 21 <li><a href="examples-modeldata.html">Model data</a></li>
21 22 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
22 23 <li><a href="examples-piechart.html">Pie chart</a></li>
23 24 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
24 25 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
25 26 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
26 27 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
27 28 <li><a href="examples-splinechart.html">Spline chart</a></li>
28 29 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
29 30 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
30 31 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
31 32 </ul>
32 33 </td>
33 34 </tr>
34 35 </table>
35 36 \endraw
36 37 */
@@ -1,44 +1,45
1 1 /*!
2 2 \page index.html
3 3 \keyword About
4 4
5 5 \raw HTML
6 6 <div class="qchart">
7 7 <img src="images/qt_commercial_logo.png" alt="qtcommercial"/>
8 8
9 9 <p>
10 10 QCharts is a part of Qt Commercial addons package. It provides a set of easy to use chart
11 11 components which are available for Qt Commercial customers. It uses Qt Graphics View
12 12 Framework, therefore charts can be easily integrated to modern 2D user interfaces. QCharts can
13 13 be used as QWidgets, QGraphicsWidget or QML elements. Users can easily create impressive
14 14 graphs by selecting one of the charts themes.
15 15 </p>
16 16
17 17 <table>
18 18 <tr>
19 19 <td><a href="examples-linechart.html"><img src="images/examples_linechart.png" width="440" alt="linechart" /></a></td>
20 20 <td><a href="examples-areachart.html"><img src="images/examples_areachart.png" width="440" alt="areachart" /></a></td>
21 21 </tr>
22 22 <tr>
23 23 <td><a href="examples-scatterchart.html"><img src="images/examples_scatterchart.png" width="440" alt="scatterchart" /></a></td>
24 24 <td><a href="examples-barchart.html"><img src="images/examples_barchart.png" width="440" alt="barchart" /></a></td>
25 25 </tr>
26 26 <tr>
27 <td><a href="examples-groupedbarchart.html"><img src="images/examples_groupedbarchart.png" width="440" alt="groupedbarcchart" /></a></td>
27 28 <td><a href="examples-stackedbarchart.html"><img src="images/examples_stackedbarchart.png" width="440" alt="stackedbarchart" /></a></td>
28 <td><a href="examples-percentbarchart.html"><img src="images/examples_percentbarchart.png" width="440" alt="percentbarcchart" /></a></td>
29 29 </tr>
30 30 <tr>
31 <td><a href="examples-percentbarchart.html"><img src="images/examples_percentbarchart.png" width="440" alt="percentbarcchart" /></a></td>
31 32 <td><a href="examples-splinechart.html"><img src="images/examples_splinechart.png" width="440" alt="linechart" /></a></td>
32 <td><a href="examples-piechart.html"><img src="images/examples_piechart.png" width="440" alt="piechart" /></a></td>
33 33 </tr>
34 34 <tr>
35 35 <td><a href="examples-customchart.html"><img src="images/examples_customchart.png" width="440" alt="customerchart" /></a></td>
36 <td><a href="examples-zoomlinechart.html"><img src="images/examples_zoomlinechart2.png" width="440" alt="zoomlinechart" /></a></td>
36 <td><a href="examples-piechart.html"><img src="images/examples_piechart.png" width="440" alt="piechart" /></a></td>
37 37 </tr>
38 38 <tr>
39 39 <td><a href="demos-chartthemes.html"><img src="images/demo_chartthemes_blue_cerulean.png" width=440 alt="charttheme" /></a></td>
40 <td><a href="examples-zoomlinechart.html"><img src="images/examples_zoomlinechart2.png" width="440" alt="zoomlinechart" /></a></td>
40 41 </tr>
41 42 </table>
42 43 </div>
43 44 \endraw
44 45 */
@@ -1,89 +1,82
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QBarSeries>
25 25 #include <QBarSet>
26 26 #include <QLegend>
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 int main(int argc, char *argv[])
31 31 {
32 32 QApplication a(argc, argv);
33 33
34 34 //![1]
35 QBarCategories categories;
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
37 //![1]
38
39 //![2]
40 35 QBarSet *set0 = new QBarSet("Jane");
41 36 QBarSet *set1 = new QBarSet("John");
42 37 QBarSet *set2 = new QBarSet("Axel");
43 38 QBarSet *set3 = new QBarSet("Mary");
44 39 QBarSet *set4 = new QBarSet("Samantha");
45 40
46 41 *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.4, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6);
47 42 *set1 << QPointF(0.1, 2) << QPointF(1.2, 3) << QPointF(2.45, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7);
48 43 *set2 << QPointF(0.2, 3) << QPointF(1.4, 4) << QPointF(2.50, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8);
49 44 *set3 << QPointF(0.3, 4) << QPointF(1.6, 5) << QPointF(2.55, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9);
50 45 *set4 << QPointF(0.4, 5) << QPointF(1.8, 6) << QPointF(2.6, 7) << QPointF(3.8, 8) << QPointF(4.8, 9) << QPointF(5.8, 10);
51 //![2]
46 //![1]
52 47
53 //![3]
48 //![2]
54 49 QBarSeries* series = new QBarSeries();
55 series->setCategories(categories);
56 50 series->append(set0);
57 51 series->append(set1);
58 52 series->append(set2);
59 53 series->append(set3);
60 54 series->append(set4);
55 //![2]
61 56
62 57 //![3]
63
64 //![4]
65 58 QChart* chart = new QChart();
66 59 chart->addSeries(series);
67 60 chart->setTitle("Simple barchart example");
68 //![4]
61 //![3]
69 62
70 //![5]
63 //![4]
71 64 chart->legend()->setVisible(true);
72 65 chart->legend()->setAlignment(QLegend::AlignmentBottom);
73 66 chart->axisY()->setNiceNumbersEnabled(true);
74 //![5]
67 //![4]
75 68
76 //![6]
69 //![5]
77 70 QChartView* chartView = new QChartView(chart);
78 71 chartView->setRenderHint(QPainter::Antialiasing);
79 //![6]
72 //![5]
80 73
81 //![7]
74 //![6]
82 75 QMainWindow window;
83 76 window.setCentralWidget(chartView);
84 77 window.resize(400, 300);
85 78 window.show();
86 //![7]
79 //![6]
87 80
88 81 return a.exec();
89 82 }
@@ -1,608 +1,654
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 "qbarseries.h"
22 22 #include "qbarseries_p.h"
23 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "domain_p.h"
26 26 #include "legendmarker_p.h"
27 27 #include "chartdataset_p.h"
28 28 #include "charttheme_p.h"
29 29 #include "chartanimator_p.h"
30 30
31 31 #include <QAbstractItemModel>
32 32 #include <QModelIndex>
33 33 #include <QBarModelMapper>
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 /*!
38 38 \class QBarSeries
39 39 \brief part of QtCommercial chart API.
40 40 \mainclass
41 41
42 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
43 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
44 by QStringList.
42 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
43 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
44 and y-value is the height of the bar. The category names are ignored with this series and x-axis
45 shows the x-values.
45 46
46 47 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 48 \image examples_barchart.png
48 49
49 50 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 51 */
51 52
52 53 /*!
53 54 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
54 55
55 56 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
56 57 contained by the series.
57 58 */
58 59
59 60 /*!
60 61 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
61 62
62 63 The signal is emitted if mouse is hovered on top of series.
63 64 Parameter \a barset is the pointer of barset, where hover happened.
64 65 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
65 66 */
66 67
67 68 /*!
68 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
69 Constructs empty QBarSeries.
69 70 QBarSeries is QObject which is a child of a \a parent.
70 71 */
71 QBarSeries::QBarSeries(/*QBarCategories categories,*/ QObject *parent) :
72 QAbstractSeries(*new QBarSeriesPrivate(/*categories,*/ this),parent)
72 QBarSeries::QBarSeries(QObject *parent) :
73 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
73 74 {
74 75 }
75 76
76 77 /*!
77 78 Destructs barseries and owned barsets.
78 79 */
79 80 QBarSeries::~QBarSeries()
80 81 {
81 82 // NOTE: d_ptr destroyed by QObject
82 83 }
83 84
84 85 /*!
85 86 \internal
86 87 */
87 88 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
88 89 QAbstractSeries(d,parent)
89 90 {
90 91 }
91 92
92 93 /*!
93 94 Returns the type of series. Derived classes override this.
94 95 */
95 96 QAbstractSeries::SeriesType QBarSeries::type() const
96 97 {
97 98 return QAbstractSeries::SeriesTypeBar;
98 99 }
99 100
101 /*!
102 Sets the \a categories, which are used to to group the data.
103 */
100 104 void QBarSeries::setCategories(QBarCategories categories)
101 105 {
102 106 Q_D(QBarSeries);
103 107 d->setCategories(categories);
104 108 emit d->categoriesUpdated();
105 109 }
106 110
107 111 /*!
108 Adds a set of bars to series. Takes ownership of \a set.
112 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
113 Returns true, if appending succeeded.
114
109 115 */
110 116 bool QBarSeries::append(QBarSet *set)
111 117 {
112 118 Q_D(QBarSeries);
113 119 if ((d->m_barSets.contains(set)) || (set == 0)) {
114 120 // Fail if set is already in list or set is null.
115 121 return false;
116 122 }
117 123 d->m_barSets.append(set);
118 124 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
119 125 emit d->restructuredBars();
120 126 return true;
121 127 }
122 128
123 129 /*!
124 130 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
131 Returns true, if set was removed.
125 132 */
126 133 bool QBarSeries::remove(QBarSet *set)
127 134 {
128 135 Q_D(QBarSeries);
129 136 if (!d->m_barSets.contains(set)) {
130 137 // Fail if set is not in list
131 138 return false;
132 139 }
133 140 d->m_barSets.removeOne(set);
134 141 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
135 142 emit d->restructuredBars();
136 143 return true;
137 144 }
138 145
139 146 /*!
140 147 Adds a list of barsets to series. Takes ownership of \a sets.
148 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
149 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
150 and function returns false.
141 151 */
142 152 bool QBarSeries::append(QList<QBarSet* > sets)
143 153 {
144 154 Q_D(QBarSeries);
145 155 foreach (QBarSet* set, sets) {
146 156 if ((set == 0) || (d->m_barSets.contains(set))) {
147 157 // Fail if any of the sets is null or is already appended.
148 158 return false;
149 159 }
150 160 if (sets.count(set) != 1) {
151 161 // Also fail if same set is more than once in given list.
152 162 return false;
153 163 }
154 164 }
155 165
156 166 foreach (QBarSet* set, sets) {
157 167 d->m_barSets.append(set);
158 168 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
159 169 }
160 170 emit d->restructuredBars();
161 171 return true;
162 172 }
163 173
164 174 /*!
165 175 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
166 176 */
167 177 bool QBarSeries::remove(QList<QBarSet* > sets)
168 178 {
169 179 Q_D(QBarSeries);
170 180
171 181 bool setsRemoved = false;
172 182 foreach (QBarSet* set, sets) {
173 183 if (d->m_barSets.contains(set)) {
174 184 d->m_barSets.removeOne(set);
175 185 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
176 186 setsRemoved = true;
177 187 }
178 188 }
179 189
180 190 if (setsRemoved) {
181 191 emit d->restructuredBars();
182 192 }
183 193 return setsRemoved;
184 194 }
185 195
186 196 /*!
187 197 Returns number of sets in series.
188 198 */
189 199 int QBarSeries::barsetCount() const
190 200 {
191 201 Q_D(const QBarSeries);
192 202 return d->m_barSets.count();
193 203 }
194 204
195 205 /*!
196 206 Returns number of categories in series
197 207 */
198 208 int QBarSeries::categoryCount() const
199 209 {
200 210 Q_D(const QBarSeries);
201 return d->m_categories.count();
211 return d->categoryCount();
202 212 }
203 213
204 214 /*!
205 215 Returns a list of sets in series. Keeps ownership of sets.
206 216 */
207 217 QList<QBarSet*> QBarSeries::barSets() const
208 218 {
209 219 Q_D(const QBarSeries);
210 220 return d->m_barSets;
211 221 }
212 222
213 223 /*!
214 224 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
215 225 Sets the \a model to be used as a data source
216 226 */
217 227 void QBarSeries::setModel(QAbstractItemModel *model)
218 228 {
219 229 Q_D(QBarSeries);
220 230 // disconnect signals from old model
221 231 if(d->m_model)
222 232 {
223 233 disconnect(d->m_model, 0, this, 0);
224 234 }
225 235
226 236 // set new model
227 237 if(model)
228 238 {
229 239 d->m_model = model;
230 240
231 241 // connect the signals
232 242 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
233 243 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
234 244 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
235 245 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
236 246 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
237 247
238 248 if (d->m_mapper)
239 249 d->initializeDataFromModel();
240 250 }
241 251 else
242 252 {
243 253 d->m_model = 0;
244 254 }
245 255 }
246 256
247 257 void QBarSeries::setModelMapper(QBarModelMapper *mapper)
248 258 {
249 259 Q_D(QBarSeries);
250 260 // disconnect signals from old mapper
251 261 if (d->m_mapper) {
252 262 QObject::disconnect(d->m_mapper, 0, this, 0);
253 263 }
254 264
255 265 if (mapper) {
256 266 d->m_mapper = mapper;
257 267 // connect the signal from the mapper
258 268 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializeDataFromModel()));
259 269
260 270 if (d->m_model)
261 271 d->initializeDataFromModel();
262 272 } else {
263 273 d->m_mapper = 0;
264 274 }
265 275 }
266 276
267 277 QBarModelMapper* QBarSeries::modelMapper() const
268 278 {
269 279 Q_D(const QBarSeries);
270 280 return d->m_mapper;
271 281 }
272 282
273 283 /*!
274 284 Returns the bar categories of the series.
275 285 */
276 286 QBarCategories QBarSeries::categories() const
277 287 {
278 288 Q_D(const QBarSeries);
279 return d->m_categories;
289 return d->categories();
280 290 }
281 291
282 292 /*!
283 293 Sets the visibility of labels in series to \a visible
284 294 */
285 295 void QBarSeries::setLabelsVisible(bool visible)
286 296 {
287 297 foreach (QBarSet* s, barSets()) {
288 298 s->setLabelsVisible(visible);
289 299 }
290 300 }
291 301
292 302 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293 303
294 304 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
295 305 QAbstractSeriesPrivate(q),
296 306 m_barMargin(0.05), // Default value is 5% of category width
297 307 m_mapper(0)
298 // m_categories(categories),
299 308 {
300 309 }
301 310
302 311 void QBarSeriesPrivate::setCategories(QBarCategories categories)
303 312 {
304 313 m_categories = categories;
305 314 }
306 315
316 void QBarSeriesPrivate::insertCategory(int index, const QString category)
317 {
318 m_categories.insert(index, category);
319 emit categoriesUpdated();
320 }
321
322 void QBarSeriesPrivate::removeCategory(int index)
323 {
324 m_categories.removeAt(index);
325 emit categoriesUpdated();
326 }
327
328 int QBarSeriesPrivate::categoryCount() const
329 {
330 if (m_categories.count() > 0) {
331 return m_categories.count();
332 }
333
334 // No categories defined. return count of longest set.
335 int count = 0;
336 for (int i=0; i<m_barSets.count(); i++) {
337 if (m_barSets.at(i)->count() > count) {
338 count = m_barSets.at(i)->count();
339 }
340 }
341
342 return count;
343 }
344
345 QBarCategories QBarSeriesPrivate::categories() const
346 {
347 if (m_categories.count() > 0) {
348 return m_categories;
349 }
350
351 // No categories defined. retun list of indices.
352 QBarCategories categories;
353
354 int count = categoryCount();
355 for (int i = 0; i < count; i++) {
356 categories.append(QString::number(i));
357 }
358 return categories;
359 }
360
307 361 void QBarSeriesPrivate::setBarMargin(qreal margin)
308 362 {
309 363 if (margin > 1.0) {
310 364 margin = 1.0;
311 365 } else if (margin < 0.0) {
312 366 margin = 0.0;
313 367 }
314 368
315 369 m_barMargin = margin;
316 370 emit updatedBars();
317 371 }
318 372
319 373 qreal QBarSeriesPrivate::barMargin()
320 374 {
321 375 return m_barMargin;
322 376 }
323 377
324 378 QBarSet* QBarSeriesPrivate::barsetAt(int index)
325 379 {
326 380 return m_barSets.at(index);
327 381 }
328 382
329 383 QString QBarSeriesPrivate::categoryName(int category)
330 384 {
385 if ((category > 0) && (category < m_categories.count())) {
331 386 return m_categories.at(category);
332 387 }
333 388
389 return QString::number(category);
390 }
391
334 392 qreal QBarSeriesPrivate::min()
335 393 {
336 394 if (m_barSets.count() <= 0) {
337 395 return 0;
338 396 }
339 397 qreal min = INT_MAX;
340 398
341 399 for (int i = 0; i < m_barSets.count(); i++) {
342 400 int categoryCount = m_barSets.at(i)->count();
343 401 for (int j = 0; j < categoryCount; j++) {
344 402 qreal temp = m_barSets.at(i)->at(j).y();
345 403 if (temp < min)
346 404 min = temp;
347 405 }
348 406 }
349 407 return min;
350 408 }
351 409
352 410 qreal QBarSeriesPrivate::max()
353 411 {
354 412 if (m_barSets.count() <= 0) {
355 413 return 0;
356 414 }
357 415 qreal max = INT_MIN;
358 416
359 417 for (int i = 0; i < m_barSets.count(); i++) {
360 418 int categoryCount = m_barSets.at(i)->count();
361 419 for (int j = 0; j < categoryCount; j++) {
362 420 qreal temp = m_barSets.at(i)->at(j).y();
363 421 if (temp > max)
364 422 max = temp;
365 423 }
366 424 }
367 425
368 426 return max;
369 427 }
370 428
371 429 qreal QBarSeriesPrivate::valueAt(int set, int category)
372 430 {
373 431 if ((set < 0) || (set >= m_barSets.count())) {
374 432 // No set, no value.
375 433 return 0;
376 434 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
377 435 // No category, no value.
378 436 return 0;
379 437 }
380 438
381 439 return m_barSets.at(set)->at(category).y();
382 440 }
383 441
384 442 qreal QBarSeriesPrivate::percentageAt(int set, int category)
385 443 {
386 444 if ((set < 0) || (set >= m_barSets.count())) {
387 445 // No set, no value.
388 446 return 0;
389 447 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
390 448 // No category, no value.
391 449 return 0;
392 450 }
393 451
394 452 qreal value = m_barSets.at(set)->at(category).y();
395 453 qreal sum = categorySum(category);
396 454 if ( qFuzzyIsNull(sum) ) {
397 455 return 0;
398 456 }
399 457
400 458 return value / sum;
401 459 }
402 460
403 461 qreal QBarSeriesPrivate::categorySum(int category)
404 462 {
405 463 qreal sum(0);
406 464 int count = m_barSets.count(); // Count sets
407 465 for (int set = 0; set < count; set++) {
408 466 if (category < m_barSets.at(set)->count())
409 467 sum += m_barSets.at(set)->at(category).y();
410 468 }
411 469 return sum;
412 470 }
413 471
414 472 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
415 473 {
416 474 qreal sum(0);
417 475 int count = m_barSets.count(); // Count sets
418 476 for (int set = 0; set < count; set++) {
419 477 if (category < m_barSets.at(set)->count())
420 478 sum += qAbs(m_barSets.at(set)->at(category).y());
421 479 }
422 480 return sum;
423 481 }
424 482
425 483 qreal QBarSeriesPrivate::maxCategorySum()
426 484 {
427 485 qreal max = INT_MIN;
428 int count = m_categories.count();
486 int count = categoryCount();
429 487 for (int i = 0; i < count; i++) {
430 488 qreal sum = categorySum(i);
431 489 if (sum > max)
432 490 max = sum;
433 491 }
434 492 return max;
435 493 }
436 494
437 495 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
438 496 {
439 497 if (m_model == 0 || m_mapper == 0)
440 498 return;
441 499
442 500 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
443 501 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
444 502 if (m_mapper->orientation() == Qt::Vertical)
445 503 {
446 504 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
447 505 if ( row >= m_mapper->first() && (m_mapper->count() == - 1 || row < m_mapper->first() + m_mapper->count())) {
448 506 if (column >= m_mapper->mapBarBottom() && column <= m_mapper->mapBarTop())
449 507 barsetAt(column - m_mapper->mapBarBottom())->replace(row - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
450 508 // if (column == m_mapper->mapCategories());// TODO:
451 509 }
452 510 }
453 511 else
454 512 {
455 513 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
456 514 if (column >= m_mapper->first() && (m_mapper->count() == - 1 || column < m_mapper->first() + m_mapper->count())) {
457 515 if (row >= m_mapper->mapBarBottom() && row <= m_mapper->mapBarTop())
458 516 barsetAt(row - m_mapper->mapBarBottom())->replace(column - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
459 517 // if (row == m_mapper->mapCategories());// TODO:
460 518 }
461 519 }
462 520 }
463 521 }
464 522 }
465 523
466 524 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
467 525 {
468 526 Q_UNUSED(parent);
469 527 Q_UNUSED(start);
470 528 Q_UNUSED(end);
471 529 initializeDataFromModel();
472 530 }
473 531
474 532 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
475 533 {
476 534 Q_UNUSED(parent);
477 535 Q_UNUSED(start);
478 536 Q_UNUSED(end);
479 537 initializeDataFromModel();
480 538 }
481 539
482 540 void QBarSeriesPrivate::initializeDataFromModel()
483 541 {
484 542 Q_Q(QBarSeries);
485 543
486 544 // create the initial bars
487 545 m_categories.clear();
488 546 m_barSets.clear();
489 547
490 548 if (m_model == 0 || m_mapper == 0)
491 549 return;
492 550
493 551 // check if mappings are set
494 552 if (m_mapper->mapBarBottom() == -1 || m_mapper->mapBarTop() == -1 || m_mapper->mapCategories() == -1)
495 553 return;
496 554
497 555 // emit restructuredBars();
498 556 if (m_mapper->orientation() == Qt::Vertical) {
499 557 if (m_mapCategories >= m_model->columnCount())
500 558 return;
501 559 int rowCount = 0;
502 560 if(m_mapper->count() == -1)
503 561 rowCount = m_model->rowCount() - m_mapper->first();
504 562 else
505 563 rowCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
506 564 for (int k = m_mapper->first(); k < m_mapper->first() + rowCount; k++) {
507 565 m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString();
508 566 }
509 567
510 568 int lastAvailableBarSet = qMin(m_model->columnCount() - 1, m_mapper->mapBarTop());
511 569 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
512 570 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
513 571 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
514 572 for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++)
515 573 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
516 574 q->append(barSet);
517 575 }
518 576 } else {
519 577 if (m_mapCategories >= m_model->rowCount())
520 578 return;
521 579 int columnCount = 0;
522 580 if(m_mapper->count() == -1)
523 581 columnCount = m_model->columnCount() - m_mapper->first();
524 582 else
525 583 columnCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
526 584 for (int k = m_mapper->first(); k < m_mapper->first() + columnCount; k++) {
527 585 m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString();
528 586 }
529 587
530 588 int lastAvailableBarSet = qMin(m_model->rowCount() - 1, m_mapper->mapBarTop());
531 589 for (int i = m_mapper->mapBarBottom(); i <= lastAvailableBarSet; i++) {
532 590 // for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
533 591 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
534 592 for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++)
535 593 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
536 594 q->append(barSet);
537 595 }
538 596 }
539 597 emit restructuredBars();
540 598 // emit updatedBars();
541 599 }
542 600
543 void QBarSeriesPrivate::insertCategory(int index, const QString category)
544 {
545 m_categories.insert(index, category);
546 emit categoriesUpdated();
547 }
548
549 void QBarSeriesPrivate::removeCategory(int index)
550 {
551 m_categories.removeAt(index);
552 emit categoriesUpdated();
553 }
554
555 601 void QBarSeriesPrivate::barsetChanged()
556 602 {
557 603 emit updatedBars();
558 604 }
559 605
560 606 void QBarSeriesPrivate::scaleDomain(Domain& domain)
561 607 {
562 608 qreal minX(domain.minX());
563 609 qreal minY(domain.minY());
564 610 qreal maxX(domain.maxX());
565 611 qreal maxY(domain.maxY());
566 612 int tickXCount(domain.tickXCount());
567 613 int tickYCount(domain.tickYCount());
568 614
569 qreal x = m_categories.count();
615 qreal x = categoryCount();
570 616 qreal y = max();
571 617 minX = qMin(minX, x);
572 618 minY = qMin(minY, y);
573 619 maxX = qMax(maxX, x);
574 620 maxY = qMax(maxY, y);
575 621 tickXCount = x+1;
576 622
577 623 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
578 624 }
579 625
580 626 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
581 627 {
582 628 Q_Q(QBarSeries);
583 629
584 630 BarChartItem* bar = new BarChartItem(q,presenter);
585 631 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
586 632 presenter->animator()->addAnimation(bar);
587 633 }
588 634 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
589 635 return bar;
590 636
591 637 }
592 638
593 639 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
594 640 {
595 641 Q_Q(QBarSeries);
596 642 QList<LegendMarker*> markers;
597 643 foreach(QBarSet* set, q->barSets()) {
598 644 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
599 645 markers << marker;
600 646 }
601 647
602 648 return markers;
603 649 }
604 650
605 651 #include "moc_qbarseries.cpp"
606 652 #include "moc_qbarseries_p.cpp"
607 653
608 654 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,81
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 BARSERIES_H
22 22 #define BARSERIES_H
23 23
24 24 #include <qabstractseries.h>
25 25 #include <QStringList>
26 26
27 27 class QModelIndex;
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 typedef QStringList QBarCategories;
32 32
33 33 class QBarSet;
34 34 class BarCategory;
35 35 class QBarSeriesPrivate;
36 36 class QBarModelMapper;
37 37
38 38 // Container for series
39 39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
40 40 {
41 41 Q_OBJECT
42 42 public:
43 43 explicit QBarSeries(QObject *parent = 0);
44 44 virtual ~QBarSeries();
45 45
46 46 QAbstractSeries::SeriesType type() const;
47 47 void setCategories(QBarCategories categories);
48 48
49 bool append(QBarSet *set); // Takes ownership of set
50 bool remove(QBarSet *set); // Releases ownership, doesn't delete set
49 bool append(QBarSet *set);
50 bool remove(QBarSet *set);
51 51 bool append(QList<QBarSet* > sets);
52 52 bool remove(QList<QBarSet* > sets);
53 53 int barsetCount() const;
54 54 int categoryCount() const;
55 55 QList<QBarSet*> barSets() const;
56 56 QBarCategories categories() const;
57 57
58 58 void setLabelsVisible(bool visible = true);
59 59
60 60 void setModel(QAbstractItemModel *model);
61 61 void setModelMapper(QBarModelMapper *mapper);
62 62 QBarModelMapper* modelMapper() const;
63 63
64 64 protected:
65 65 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
66 66
67 67 Q_SIGNALS:
68 68 void clicked(QBarSet *barset, QString category);
69 69 void hovered(QBarSet* barset, bool status);
70 70
71 71 protected:
72 72 Q_DECLARE_PRIVATE(QBarSeries)
73 73 friend class BarChartItem;
74 74 friend class PercentBarChartItem;
75 75 friend class StackedBarChartItem;
76 76 friend class GroupedBarChartItem;
77 77 };
78 78
79 79 QTCOMMERCIALCHART_END_NAMESPACE
80 80
81 81 #endif // BARSERIES_H
@@ -1,70 +1,72
1 1 #ifndef QBARSERIES_P_H
2 2 #define QBARSERIES_P_H
3 3
4 4 #include "qbarseries.h"
5 5 #include "qabstractseries_p.h"
6 6 #include <QStringList>
7 7 #include <QAbstractSeries>
8 8
9 9 class QModelIndex;
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 class QBarModelMapper;
14 14
15 15 // Container for series
16 16 class QBarSeriesPrivate : public QAbstractSeriesPrivate
17 17 {
18 18 Q_OBJECT
19 19 public:
20 20 QBarSeriesPrivate(QBarSeries *parent);
21 21 void setCategories(QBarCategories categories);
22 void insertCategory(int index, const QString category);
23 void removeCategory(int index);
24 int categoryCount() const;
25 QBarCategories categories() const;
26
22 27 void setBarMargin(qreal margin);
23 28 qreal barMargin();
24 29
25 30 void scaleDomain(Domain& domain);
26 31 Chart* createGraphics(ChartPresenter* presenter);
27 32 QList<LegendMarker*> createLegendMarker(QLegend* legend);
28 33
29 void insertCategory(int index, const QString category);
30 void removeCategory(int index);
31
32 34 QBarSet* barsetAt(int index);
33 35 QString categoryName(int category);
34 36 qreal min();
35 37 qreal max();
36 38 qreal valueAt(int set, int category);
37 39 qreal percentageAt(int set, int category);
38 40 qreal categorySum(int category);
39 41 qreal absoluteCategorySum(int category);
40 42 qreal maxCategorySum();
41 43
42 44 Q_SIGNALS:
43 45 void clicked(QBarSet *barset, QString category);
44 46 void updatedBars();
45 47 void restructuredBars();
46 48 void categoriesUpdated();
47 49
48 50 private Q_SLOTS:
49 51 // slots for updating bars when data in model changes
50 52 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 53 void modelDataAdded(QModelIndex parent, int start, int end);
52 54 void modelDataRemoved(QModelIndex parent, int start, int end);
53 55 void initializeDataFromModel();
54 56 void barsetChanged();
55 57
56 58 protected:
57 59 QList<QBarSet *> m_barSets;
58 60 QBarCategories m_categories;
59 61 qreal m_barMargin;
60 62 int m_mapCategories;
61 63 int m_mapBarBottom;
62 64 int m_mapBarTop;
63 65 QBarModelMapper *m_mapper;
64 66 private:
65 67 Q_DECLARE_PUBLIC(QBarSeries)
66 68 };
67 69
68 70 QTCOMMERCIALCHART_END_NAMESPACE
69 71
70 72 #endif // QBARSERIESPRIVATE_P_H
@@ -1,340 +1,363
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 "qbarset.h"
22 22 #include "qbarset_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QBarSet
28 28 \brief part of QtCommercial chart API.
29 29
30 30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 31 First value of set is assumed to belong to first category, second to second category and so on.
32 32 If set has fewer values than there are categories, then the missing values are assumed to be
33 33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34 34
35 35 \mainclass
36 36
37 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
37 \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries
38 38 */
39 39
40 40 /*!
41 41 \fn void QBarSet::clicked(QString category)
42 42 \brief signals that set has been clicked
43 43 Parameter \a category describes on which category was clicked
44 44 */
45 45
46 46 /*!
47 47 \fn void QBarSet::hovered(bool status)
48 48 \brief signals that mouse has hovered over the set. If \a status is true, then mouse was entered. If \a status is false, then mouse was left.
49 49
50 50 The signal is emitted if mouse is hovered on top of set
51 51 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
52 52 */
53 53
54 54 /*!
55 55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 56 */
57 57 QBarSet::QBarSet(const QString name, QObject *parent)
58 58 : QObject(parent)
59 59 ,d_ptr(new QBarSetPrivate(name,this))
60 60 {
61 61 }
62 62
63 63 /*!
64 64 Destroys the barset
65 65 */
66 66 QBarSet::~QBarSet()
67 67 {
68 68 // NOTE: d_ptr destroyed by QObject
69 69 }
70 70
71 71 /*!
72 72 Sets new \a name for set.
73 73 */
74 74 void QBarSet::setName(const QString name)
75 75 {
76 76 d_ptr->m_name = name;
77 77 }
78 78
79 79 /*!
80 80 Returns name of the set.
81 81 */
82 82 QString QBarSet::name() const
83 83 {
84 84 return d_ptr->m_name;
85 85 }
86 86
87 /*!
88 Appends a point to set. Parameter \a value x coordinate defines the
89 position in x-axis and y coordinate defines the height of bar.
90 Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries)
91 the x values are used or ignored.
92 */
87 93 void QBarSet::append(const QPointF value)
88 94 {
89 95 d_ptr->m_values.append(value);
90 96 emit d_ptr->restructuredBars();
91 97 }
92 98
93
99 /*!
100 Appends a list of \a points to set. Works like append with single point.
101 \sa append()
102 */
94 103 void QBarSet::append(const QList<QPointF> values)
95 104 {
96 105 for (int i=0; i<values.count(); i++) {
97 106 d_ptr->m_values.append(values.at(i));
98 107 }
99 108 emit d_ptr->restructuredBars();
100 109 }
101 110
102 111 /*!
103 Appends new value \a value to the end of set.
112 Appends new value \a value to the end of set. Internally the value is converted to QPointF,
113 with x coordinate being the index of appended value and y coordinate is the value.
104 114 */
105 115 void QBarSet::append(const qreal value)
106 116 {
107 117 append(QPointF(d_ptr->m_values.count(), value));
108 // d_ptr->m_values.append(value);
109 118 }
110 119
111
120 /*!
121 Appends a list of reals to set. Works like append with single real value. The values in list
122 are converted to QPointF, where x coordinate is the index of point and y coordinate is the value.
123 \sa append()
124 */
112 125 void QBarSet::append(const QList<qreal> values)
113 126 {
114 127 int index = d_ptr->m_values.count();
115 128 for (int i=0; i<values.count(); i++) {
116 129 d_ptr->m_values.append(QPointF(index,values.at(i)));
117 130 index++;
118 131 }
119 132 emit d_ptr->restructuredBars();
120 133 }
121 134
122 135 /*!
123 Appends new value \a value to the end of set.
136 Convinience operator. Same as append, with real \a value.
137 \sa append()
124 138 */
125 139 QBarSet& QBarSet::operator << (const qreal &value)
126 140 {
127 141 append(value);
128 142 return *this;
129 143 }
130 144
145 /*!
146 Convinience operator. Same as append, with QPointF \a value.
147 \sa append()
148 */
131 149 QBarSet& QBarSet::operator << (const QPointF &value)
132 150 {
133 151 append(value);
134 152 return *this;
135 153 }
136 154
137 155 /*!
138 156 Inserts new \a value on the \a index position.
139 157 The value that is currently at this postion is moved to postion index + 1
140 158 \sa remove()
141 159 */
142 160 void QBarSet::insert(const int index, const qreal value)
143 161 {
144 162 d_ptr->m_values.insert(index, QPointF(index, value));
145 163 // emit d_ptr->updatedBars();
146 164 }
147 165
148 166 /*!
149 167 Removes the value specified by \a index
150 168 \sa insert()
151 169 */
152 170 void QBarSet::remove(const int index)
153 171 {
154 172 d_ptr->m_values.removeAt(index);
155 173 // emit d_ptr->updatedBars();
156 174 }
157 175
158 176 /*!
159 177 Sets a new value \a value to set, indexed by \a index
160 178 */
161 179 void QBarSet::replace(const int index, const qreal value)
162 180 {
163 181 d_ptr->m_values.replace(index,QPointF(index,value));
164 182 emit d_ptr->updatedBars();
165 183 }
166 184
167 185 /*!
168 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF
186 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF.
187 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
188 of the QPointF (if appended with QPointF append).
189 If the index is out of bounds QPointF(0, 0.0) is returned.
169 190 */
170 191 QPointF QBarSet::at(const int index) const
171 192 {
172 193 if (index < 0 || index >= d_ptr->m_values.count()) {
173 194 return QPointF(index, 0.0);
174 195 }
175 196
176 197 return d_ptr->m_values.at(index);
177 198 }
178 199
179 200 /*!
180 Returns value of set indexed by \a index
201 Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF.
202 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
203 of the QPointF (if appended with QPointF append).
181 204 */
182 205 QPointF QBarSet::operator [](const int index) const
183 206 {
184 207 return d_ptr->m_values.at(index);
185 208 }
186 209
187 210 /*!
188 211 Returns count of values in set.
189 212 */
190 213 int QBarSet::count() const
191 214 {
192 215 return d_ptr->m_values.count();
193 216 }
194 217
195 218 /*!
196 Returns sum of all values in barset.
219 Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation.
197 220 */
198 221 qreal QBarSet::sum() const
199 222 {
200 223 qreal total(0);
201 224 for (int i=0; i < d_ptr->m_values.count(); i++) {
202 225 //total += d_ptr->m_values.at(i);
203 226 total += d_ptr->m_values.at(i).y();
204 227 }
205 228 return total;
206 229 }
207 230
208 231 /*!
209 232 Sets pen for set. Bars of this set are drawn using \a pen
210 233 */
211 234 void QBarSet::setPen(const QPen &pen)
212 235 {
213 236 if(d_ptr->m_pen!=pen){
214 237 d_ptr->m_pen = pen;
215 238 emit d_ptr->updatedBars();
216 239 }
217 240 }
218 241
219 242 /*!
220 243 Returns pen of the set.
221 244 */
222 245 QPen QBarSet::pen() const
223 246 {
224 247 return d_ptr->m_pen;
225 248 }
226 249
227 250 /*!
228 251 Sets brush for the set. Bars of this set are drawn using \a brush
229 252 */
230 253 void QBarSet::setBrush(const QBrush &brush)
231 254 {
232 255 if(d_ptr->m_brush!=brush){
233 256 d_ptr->m_brush = brush;
234 257 emit d_ptr->updatedBars();
235 258 }
236 259 }
237 260
238 261 /*!
239 262 Returns brush of the set.
240 263 */
241 264 QBrush QBarSet::brush() const
242 265 {
243 266 return d_ptr->m_brush;
244 267 }
245 268
246 269 /*!
247 270 Sets \a pen of the values that are drawn on top of this barset
248 271 */
249 272 void QBarSet::setLabelPen(const QPen &pen)
250 273 {
251 274 if(d_ptr->m_labelPen!=pen){
252 275 d_ptr->m_labelPen = pen;
253 276 emit d_ptr->updatedBars();
254 277 }
255 278 }
256 279
257 280 /*!
258 281 Returns pen of the values that are drawn on top of this barset
259 282 */
260 283 QPen QBarSet::labelPen() const
261 284 {
262 285 return d_ptr->m_labelPen;
263 286 }
264 287
265 288 /*!
266 289 Sets \a brush of the values that are drawn on top of this barset
267 290 */
268 291 void QBarSet::setLabelBrush(const QBrush &brush)
269 292 {
270 293 if(d_ptr->m_labelBrush!=brush){
271 294 d_ptr->m_labelBrush = brush;
272 295 emit d_ptr->updatedBars();
273 296 }
274 297 }
275 298
276 299 /*!
277 300 Returns brush of the values that are drawn on top of this barset
278 301 */
279 302 QBrush QBarSet::labelBrush() const
280 303 {
281 304 return d_ptr->m_labelBrush;
282 305 }
283 306
284 307 /*!
285 308 Sets the \a font for values that are drawn on top of this barset
286 309 */
287 310 void QBarSet::setLabelFont(const QFont &font)
288 311 {
289 312 if(d_ptr->m_labelFont!=font) {
290 313 d_ptr->m_labelFont = font;
291 314 emit d_ptr->updatedBars();
292 315 }
293 316
294 317 }
295 318
296 319 /*!
297 320 Returns the pen for values that are drawn on top of this set
298 321 */
299 322 QFont QBarSet::labelFont() const
300 323 {
301 324 return d_ptr->m_labelFont;
302 325 }
303 326
304 327 /*!
305 328 Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
306 329 */
307 330
308 331 void QBarSet::setLabelsVisible(bool visible)
309 332 {
310 333 if(d_ptr->m_labelsVisible!=visible) {
311 334 d_ptr->m_labelsVisible = visible;
312 335 emit d_ptr->labelsVisibleChanged(visible);
313 336 }
314 337 }
315 338
316 339 /*!
317 340 Returns the visibility of values
318 341 */
319 342 bool QBarSet::labelsVisible() const
320 343 {
321 344 return d_ptr->m_labelsVisible;
322 345 }
323 346
324 347 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
325 348
326 349 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
327 350 q_ptr(parent),
328 351 m_name(name),
329 352 m_labelsVisible(false)
330 353 {
331 354 }
332 355
333 356 QBarSetPrivate::~QBarSetPrivate()
334 357 {
335 358 }
336 359
337 360 #include "moc_qbarset.cpp"
338 361 #include "moc_qbarset_p.cpp"
339 362
340 363 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,108 +1,108
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 "qgroupedbarseries.h"
22 22 #include "qgroupedbarseries_p.h"
23 23 #include "groupedbarchartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 /*!
31 31 \class QGroupedBarSeries
32 32 \brief part of QtCommercial chart API.
33 33 \mainclass
34 34
35 QGroupedBarSeries represents a series of data shown as bars. All bars in same category are
36 grouped next to each other. One QGroupedBarSeries can contain multiple QBarSet data sets.
37 QGroupedBarSeries groups the data from sets to categories, which are defined by QStringList.
35 QGroupedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as groups, where bars in same category are grouped next to each other. QGroupedBarSeries groups the data
37 from sets to categories, which are defined by a QStringList.
38 38
39 39 See the \l {GroupedbarChart Example} {grouped bar chart example} to learn how to create a grouped bar chart.
40 40 \image examples_groupedbarchart.png
41 41
42 42 \sa QBarSet, QPercentBarSeries, QBarSeries, QStackedBarSeries
43 43 */
44 44
45 45 /*!
46 46 \fn virtual QSeriesType QGroupedBarSeries::type() const
47 47 \brief Returns type of series.
48 \sa QSeriesType
48 \sa QAbstractSeries, QSeriesType
49 49 */
50 50
51 51 /*!
52 Constructs empty QGroupedBarSeries. Parameter \a categories defines the categories for chart.
52 Constructs empty QGroupedBarSeries.
53 53 QGroupedBarSeries is QObject which is a child of a \a parent.
54 54 */
55 55 QGroupedBarSeries::QGroupedBarSeries(QObject *parent)
56 56 : QBarSeries(*new QGroupedBarSeriesPrivate(this), parent)
57 57 {
58 58 }
59 59
60 60 QAbstractSeries::SeriesType QGroupedBarSeries::type() const
61 61 {
62 62 return QAbstractSeries::SeriesTypeGroupedBar;
63 63 }
64 64
65 65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 66
67 67 QGroupedBarSeriesPrivate::QGroupedBarSeriesPrivate(/*QBarCategories categories,*/ QGroupedBarSeries *q) : QBarSeriesPrivate(/*categories,*/q)
68 68 {
69 69
70 70 }
71 71
72 72 void QGroupedBarSeriesPrivate::scaleDomain(Domain& domain)
73 73 {
74 74 qreal minX(domain.minX());
75 75 qreal minY(domain.minY());
76 76 qreal maxX(domain.maxX());
77 77 qreal maxY(domain.maxY());
78 78 int tickXCount(domain.tickXCount());
79 79 int tickYCount(domain.tickYCount());
80 80
81 81 qreal x = m_categories.count();
82 82 qreal y = maxCategorySum();
83 83 minX = qMin(minX, x);
84 84 minY = qMin(minY, y);
85 85 maxX = qMax(maxX, x);
86 86 maxY = qMax(maxY, y);
87 87 tickXCount = x+1;
88 88
89 89 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
90 90 }
91 91
92 92
93 93 Chart* QGroupedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
94 94 {
95 95 Q_Q(QGroupedBarSeries);
96 96
97 97 GroupedBarChartItem* bar = new GroupedBarChartItem(q,presenter);
98 98 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
99 99 presenter->animator()->addAnimation(bar);
100 100 }
101 101 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
102 102 return bar;
103 103 }
104 104
105 105 #include "moc_qgroupedbarseries.cpp"
106 106
107 107 QTCOMMERCIALCHART_END_NAMESPACE
108 108
@@ -1,109 +1,108
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 "qpercentbarseries.h"
22 22 #include "qpercentbarseries_p.h"
23 23 #include "percentbarchartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27
28
29 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 29
31 30 /*!
32 31 \class QPercentBarSeries
33 32 \brief part of QtCommercial chart API.
34 33 \mainclass
35 34
36 QPercentBarSeries represents a series of data shown as bars. Each bar of QBarSet is shown as percentage
37 of all bars in category. One QPercentBarSeries can contain multiple QBarSet data sets.
38 QBarSeries groups the data from sets to categories, which are defined by QStringList.
35 QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as stacks, where each bar is shown as percentage of all bars in that category.
37 QPercentBarSeries groups the data from sets to categories, which are defined by a QStringList.
39 38
40 39 See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a percent bar chart.
41 40 \image examples_percentbarchart.png
42 41
43 42 \sa QBarSet, QStackedBarSeries, QBarSeries
44 43 */
45 44
46 45 /*!
47 46 \fn virtual QSeriesType QPercentBarSeries::type() const
48 47 \brief Returns type of series.
49 48 \sa QAbstractSeries, QSeriesType
50 49 */
51 50
52 51 /*!
53 Constructs empty QPercentBarSeries. Parameter \a categories defines the categories for chart.
52 Constructs empty QPercentBarSeries.
54 53 QPercentBarSeries is QObject which is a child of a \a parent.
55 54 */
56 QPercentBarSeries::QPercentBarSeries(/*QBarCategories categories,*/ QObject *parent)
57 : QBarSeries(*new QPercentBarSeriesPrivate(/*categories,*/this), parent)
55 QPercentBarSeries::QPercentBarSeries(QObject *parent)
56 : QBarSeries(*new QPercentBarSeriesPrivate(this), parent)
58 57 {
59 58 }
60 59
61 60 QAbstractSeries::SeriesType QPercentBarSeries::type() const
62 61 {
63 62 return QAbstractSeries::SeriesTypePercentBar;
64 63 }
65 64
66 65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67 66
68 QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(/*QBarCategories categories,*/ QPercentBarSeries *q) : QBarSeriesPrivate(/*categories,*/q)
67 QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QBarSeriesPrivate(q)
69 68 {
70 69
71 70 }
72 71
73 72 void QPercentBarSeriesPrivate::scaleDomain(Domain& domain)
74 73 {
75 74 Q_Q(QPercentBarSeries);
76 75 qreal minX(domain.minX());
77 76 qreal minY(domain.minY());
78 77 qreal maxX(domain.maxX());
79 78 qreal maxY(domain.maxY());
80 79 int tickXCount(domain.tickXCount());
81 80 int tickYCount(domain.tickYCount());
82 81
83 82 qreal x = q->categoryCount();
84 83 minX = qMin(minX, x);
85 84 maxX = qMax(maxX, x);
86 85 minY = 0;
87 86 maxY = 100;
88 87 tickXCount = x+1;
89 88
90 89 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
91 90 }
92 91
93 92
94 93 Chart* QPercentBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
95 94 {
96 95 Q_Q(QPercentBarSeries);
97 96
98 97 PercentBarChartItem* bar = new PercentBarChartItem(q,presenter);
99 98 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
100 99 presenter->animator()->addAnimation(bar);
101 100 }
102 101 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
103 102 return bar;
104 103 }
105 104
106 105 #include "moc_qpercentbarseries.cpp"
107 106
108 107 QTCOMMERCIALCHART_END_NAMESPACE
109 108
@@ -1,45 +1,45
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 PERCENTBARSERIES_H
22 22 #define PERCENTBARSERIES_H
23 23
24 24 #include <QStringList>
25 25 #include <qbarseries.h>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 class QPercentBarSeriesPrivate;
30 30
31 31 class QTCOMMERCIALCHART_EXPORT QPercentBarSeries : public QBarSeries
32 32 {
33 33 Q_OBJECT
34 34 public:
35 explicit QPercentBarSeries(/*QBarCategories categories,*/ QObject *parent = 0);
35 explicit QPercentBarSeries(QObject *parent = 0);
36 36 QAbstractSeries::SeriesType type() const;
37 37
38 38 private:
39 39 Q_DECLARE_PRIVATE(QPercentBarSeries)
40 40 Q_DISABLE_COPY(QPercentBarSeries)
41 41 };
42 42
43 43 QTCOMMERCIALCHART_END_NAMESPACE
44 44
45 45 #endif // PERCENTBARSERIES_H
@@ -1,108 +1,108
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 "qstackedbarseries.h"
22 22 #include "qstackedbarseries_p.h"
23 23 #include "stackedbarchartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "chartanimator_p.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 /*!
31 31 \class QStackedBarSeries
32 32 \brief part of QtCommercial chart API.
33 33 \mainclass
34 34
35 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
36 stacked on top of each other. One QStackedBarSeries can contain multiple QBarSet data sets.
35 QStackedBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as stacks, where bars in same category are stacked on top of each other.
37 37 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
38 38
39 39 See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart.
40 40 \image examples_stackedbarchart.png
41 41
42 42 \sa QBarSet, QPercentBarSeries, QBarSeries
43 43 */
44 44
45 45 /*!
46 46 \fn virtual QSeriesType QStackedBarSeries::type() const
47 47 \brief Returns type of series.
48 \sa QSeriesType
48 \sa QAbstractSeries, QSeriesType
49 49 */
50 50
51 51 /*!
52 Constructs empty QStackedBarSeries. Parameter \a categories defines the categories for chart.
52 Constructs empty QStackedBarSeries.
53 53 QStackedBarSeries is QObject which is a child of a \a parent.
54 54 */
55 QStackedBarSeries::QStackedBarSeries(/*QBarCategories categories,*/ QObject *parent)
56 : QBarSeries(*new QStackedBarSeriesPrivate(/*categories,*/this), parent)
55 QStackedBarSeries::QStackedBarSeries(QObject *parent)
56 : QBarSeries(*new QStackedBarSeriesPrivate(this), parent)
57 57 {
58 58 }
59 59
60 60 QAbstractSeries::SeriesType QStackedBarSeries::type() const
61 61 {
62 62 return QAbstractSeries::SeriesTypeStackedBar;
63 63 }
64 64
65 65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66 66
67 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(/*QBarCategories categories,*/ QStackedBarSeries *q) : QBarSeriesPrivate(/*categories,*/q)
67 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QStackedBarSeries *q) : QBarSeriesPrivate(q)
68 68 {
69 69
70 70 }
71 71
72 72 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
73 73 {
74 74 qreal minX(domain.minX());
75 75 qreal minY(domain.minY());
76 76 qreal maxX(domain.maxX());
77 77 qreal maxY(domain.maxY());
78 78 int tickXCount(domain.tickXCount());
79 79 int tickYCount(domain.tickYCount());
80 80
81 81 qreal x = m_categories.count();
82 82 qreal y = maxCategorySum();
83 83 minX = qMin(minX, x);
84 84 minY = qMin(minY, y);
85 85 maxX = qMax(maxX, x);
86 86 maxY = qMax(maxY, y);
87 87 tickXCount = x+1;
88 88
89 89 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
90 90 }
91 91
92 92
93 93 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
94 94 {
95 95 Q_Q(QStackedBarSeries);
96 96
97 97 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
98 98 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
99 99 presenter->animator()->addAnimation(bar);
100 100 }
101 101 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
102 102 return bar;
103 103 }
104 104
105 105 #include "moc_qstackedbarseries.cpp"
106 106
107 107 QTCOMMERCIALCHART_END_NAMESPACE
108 108
@@ -1,45 +1,45
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 STACKEDBARSERIES_H
22 22 #define STACKEDBARSERIES_H
23 23
24 24 #include <QStringList>
25 25 #include <qbarseries.h>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 class QStackedBarSeriesPrivate;
30 30
31 31 class QTCOMMERCIALCHART_EXPORT QStackedBarSeries : public QBarSeries
32 32 {
33 33 Q_OBJECT
34 34 public:
35 explicit QStackedBarSeries(/*QBarCategories categories,*/ QObject *parent = 0);
35 explicit QStackedBarSeries(QObject *parent = 0);
36 36 QAbstractSeries::SeriesType type() const;
37 37
38 38 private:
39 39 Q_DECLARE_PRIVATE(QStackedBarSeries)
40 40 Q_DISABLE_COPY(QStackedBarSeries)
41 41 };
42 42
43 43 QTCOMMERCIALCHART_END_NAMESPACE
44 44
45 45 #endif // STACKEDBARSERIES_H
@@ -1,290 +1,291
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 "chartdataset_p.h"
22 22 #include "qaxis.h"
23 23 #include "qaxis_p.h"
24 24 #include "qabstractseries_p.h"
25 25 #include "qbarseries.h"
26 26 #include "qstackedbarseries.h"
27 27 #include "qpercentbarseries.h"
28 28 #include "qpieseries.h"
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
33 33 m_axisX(new QAxis(this)),
34 34 m_axisY(new QAxis(this)),
35 35 m_domainIndex(0),
36 36 m_axisXInitialized(false),
37 37 m_axisYInitialized(false)
38 38 {
39 39 //create main domain
40 40 Domain* domain = new Domain(m_axisY);
41 41 m_axisDomainMap.insert(m_axisY,domain);
42 42 QObject::connect(m_axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
43 43 QObject::connect(m_axisX->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
44 44 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),m_axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
45 45 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),m_axisX->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
46 46 }
47 47
48 48 ChartDataSet::~ChartDataSet()
49 49 {
50 50 removeAllSeries();
51 51 }
52 52
53 53 void ChartDataSet::addSeries(QAbstractSeries* series, QAxis *axisY)
54 54 {
55 55 if(axisY==0) axisY = m_axisY;
56 56
57 57 QAxis* axis = m_seriesAxisMap.value(series);
58 58
59 59 if(axis) {
60 60 qWarning() << "Can not add series. Series already on the chart";
61 61 return;
62 62 }
63 63
64 64 series->setParent(this); // take ownership
65 65 axisY->setParent(this); // take ownership
66 66
67 67 Domain* domain = m_axisDomainMap.value(axisY);
68 68
69 69 if(!domain) {
70 70 domain = new Domain(axisY);
71 71 QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
72 72 QObject::connect(axisX()->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int)));
73 73 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
74 74 //initialize
75 75 m_axisDomainMap.insert(axisY,domain);
76 76 emit axisAdded(axisY,domain);
77 77 }
78 78
79 79 if(!m_axisXInitialized){
80 80 m_axisXInitialized=true;
81 81 emit axisAdded(m_axisX,domain);
82 82 }
83 83
84 84 if(!m_axisYInitialized && axisY==m_axisY){
85 85 m_axisYInitialized=true;
86 86 emit axisAdded(m_axisY,domain);
87 87 }
88 88
89 89 series->d_ptr->scaleDomain(*domain);
90 90
91 // Note that with SeriesTypeBar we don't set up categories, but use real values on x-asis
91 92 if(series->type() == QAbstractSeries::SeriesTypeGroupedBar
92 93 || series->type() == QAbstractSeries::SeriesTypeStackedBar
93 94 || series->type() == QAbstractSeries::SeriesTypePercentBar) {
94 95 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
95 96 setupCategories(barSeries);
96 97 }
97 98
98 99 if (series->type()== QAbstractSeries::SeriesTypePie && m_seriesAxisMap.count() == 0) {
99 100 axisX()->hide();
100 101 this->axisY()->hide();
101 102 }
102 103
103 104 m_seriesAxisMap.insert(series,axisY);
104 105
105 106 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
106 107
107 108 int key=0;
108 109 while (i.hasNext()) {
109 110 i.next();
110 111 if(i.key()!=key) {
111 112 break;
112 113 }
113 114 key++;
114 115 }
115 116
116 117 m_indexSeriesMap.insert(key,series);
117 118
118 119 series->d_ptr->m_dataset=this;
119 120
120 121 emit seriesAdded(series,domain);
121 122
122 123 }
123 124
124 125 QAxis* ChartDataSet::removeSeries(QAbstractSeries* series)
125 126 {
126 127 QAxis* axis = m_seriesAxisMap.value(series);
127 128
128 129 if(!axis){
129 130 qWarning()<<"Can not remove series. Series not found on the chart.";
130 131 return 0;
131 132 }
132 133
133 134 emit seriesRemoved(series);
134 135
135 136 m_seriesAxisMap.remove(series);
136 137 int key = seriesIndex(series);
137 138 Q_ASSERT(key!=-1);
138 139
139 140 m_indexSeriesMap.remove(key);
140 141 series->setParent(0);
141 142 series->d_ptr->m_dataset=0;
142 143
143 144 QList<QAxis*> axes = m_seriesAxisMap.values();
144 145
145 146 int i = axes.indexOf(axis);
146 147
147 148 if(i==-1){
148 149 Domain* domain = m_axisDomainMap.take(axis);
149 150 emit axisRemoved(axis);
150 151 if(axis!=m_axisY){
151 152 axis->setParent(0);
152 153 delete domain;
153 154 }else{
154 155 m_axisYInitialized=false;
155 156 m_axisDomainMap.insert(m_axisY,domain);
156 157 }
157 158 }
158 159
159 160 if(m_seriesAxisMap.values().size()==0)
160 161 {
161 162 m_axisXInitialized=false;
162 163 emit axisRemoved(axisX());
163 164 }
164 165
165 166 return axis;
166 167 }
167 168
168 169 void ChartDataSet::removeAllSeries()
169 170 {
170 171 QList<QAbstractSeries*> series = m_seriesAxisMap.keys();
171 172 QList<QAxis*> axes;
172 173 foreach(QAbstractSeries *s , series) {
173 174 QAxis* axis = removeSeries(s);
174 175 if(axis==axisY()) continue;
175 176 int i = axes.indexOf(axis);
176 177 if(i==-1){
177 178 axes<<axis;
178 179 }
179 180 }
180 181
181 182 Q_ASSERT(m_seriesAxisMap.count()==0);
182 183 Q_ASSERT(m_axisDomainMap.count()==1);
183 184
184 185 qDeleteAll(series);
185 186 qDeleteAll(axes);
186 187 }
187 188
188 189 void ChartDataSet::setupCategories(QBarSeries* series)
189 190 {
190 191 QAxisCategories* categories = axisX()->categories();
191 192 categories->clear();
192 193 categories->insert(series->categories());
193 194 }
194 195
195 196 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
196 197 {
197 198 QMapIterator<QAxis*, Domain*> i(m_axisDomainMap);
198 199 //main domain has to be the last one;
199 200 Domain *domain = m_axisDomainMap.value(axisY());
200 201 Q_ASSERT(domain);
201 202 while (i.hasNext()) {
202 203 i.next();
203 204 if(i.value()==domain) continue;
204 205 i.value()->zoomIn(rect,size);
205 206 }
206 207 domain->zoomIn(rect,size);
207 208 }
208 209
209 210 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
210 211 {
211 212 QMapIterator<QAxis*, Domain*> i(m_axisDomainMap);
212 213 //main domain has to be the last one;
213 214 Domain *domain = m_axisDomainMap.value(axisY());
214 215 Q_ASSERT(domain);
215 216 while (i.hasNext()) {
216 217 i.next();
217 218 if(i.value()==domain) continue;
218 219 i.value()->zoomOut(rect,size);
219 220 }
220 221 domain->zoomOut(rect,size);
221 222 }
222 223
223 224 int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type)
224 225 {
225 226 int count=0;
226 227 QMapIterator<QAbstractSeries*, QAxis*> i(m_seriesAxisMap);
227 228 while (i.hasNext()) {
228 229 i.next();
229 230 if(i.key()->type()==type) count++;
230 231 }
231 232 return count;
232 233 }
233 234
234 235 int ChartDataSet::seriesIndex(QAbstractSeries *series)
235 236 {
236 237 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
237 238 while (i.hasNext()) {
238 239 i.next();
239 240 if (i.value() == series)
240 241 return i.key();
241 242 }
242 243 return -1;
243 244 }
244 245
245 246 QAxis* ChartDataSet::axisY(QAbstractSeries *series) const
246 247 {
247 248 if(series == 0) return m_axisY;
248 249 return m_seriesAxisMap.value(series);
249 250 }
250 251
251 252 Domain* ChartDataSet::domain(QAbstractSeries *series) const
252 253 {
253 254 QAxis* axis = m_seriesAxisMap.value(series);
254 255 if(axis){
255 256 return m_axisDomainMap.value(axis);
256 257 }else
257 258 return 0;
258 259 }
259 260
260 261 Domain* ChartDataSet::domain(QAxis* axis) const
261 262 {
262 263 if(!axis || axis==axisX()) {
263 264 return m_axisDomainMap.value(axisY());
264 265 }
265 266 else {
266 267 return m_axisDomainMap.value(axis);
267 268 }
268 269 }
269 270
270 271 void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size)
271 272 {
272 273 QMapIterator<QAxis*, Domain*> i( m_axisDomainMap);
273 274 //main domain has to be the last one;
274 275 Domain *domain = m_axisDomainMap.value(axisY());
275 276 while (i.hasNext()) {
276 277 i.next();
277 278 if(i.value()==domain) continue;
278 279 i.value()->move(dx,dy,size);
279 280 }
280 281 domain->move(dx,dy,size);
281 282 }
282 283
283 284 QList<QAbstractSeries*> ChartDataSet::series() const
284 285 {
285 286 return m_seriesAxisMap.keys();
286 287 }
287 288
288 289 #include "moc_chartdataset_p.cpp"
289 290
290 291 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,138 +1,139
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 "qabstractseries.h"
22 22 #include "qabstractseries_p.h"
23 23 #include "chartdataset_p.h"
24 24
25 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 26
27 27 /*!
28 28 \class QAbstractSeries
29 29 \brief Base class for all QtCommercial Chart series.
30 30 \mainclass
31 31
32 32 Usually you use the series type specific inherited classes instead of the base class.
33 33 \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries,
34 34 QPercentBarSeries, QPieSeries
35 35 */
36 36
37 37 /*!
38 38 \enum QAbstractSeries::SeriesType
39 39
40 40 The type of the series object.
41 41
42 42 \value SeriesTypeLine
43 43 \value SeriesTypeArea
44 44 \value SeriesTypeBar
45 45 \value SeriesTypeStackedBar
46 46 \value SeriesTypePercentBar
47 \value SeriesTypeGroupedBar
47 48 \value SeriesTypePie
48 49 \value SeriesTypeScatter
49 50 \value SeriesTypeSpline
50 51 */
51 52
52 53 /*!
53 54 \fn QSeriesType QAbstractSeries::type() const
54 55 \brief The type of the series.
55 56 */
56 57
57 58 /*!
58 59 \fn bool QAbstractSeries::setModel(QAbstractItemModel *model)
59 60 \brief Use the \a model to provide data for the series. The model overrides possible user data
60 61 set with QChartSeries type specific data setters. For example if you call both
61 62 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
62 63 used by the series. Returns true if the model is valid for the series.
63 64 */
64 65
65 66 /*!
66 67 \property QAbstractSeries::name
67 68 \brief name of the series property
68 69 */
69 70
70 71 /*!
71 72 \fn void QAbstractSeries::setName(const QString& name)
72 73 \brief Sets a \a name for the series.
73 74
74 75 The name of a series is shown in the legend for QXYSeries.
75 76 \sa QChart::setTitle()
76 77 \sa QPieSlice::setLabel()
77 78 \sa QBarSet::setName()
78 79 */
79 80
80 81 /*!
81 82 \internal
82 83 \brief Constructs ChartSeries object with \a parent.
83 84 */
84 85 QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) :
85 86 QObject(parent),
86 87 d_ptr(&d)
87 88 {
88 89 }
89 90
90 91 /*!
91 92 \brief Virtual destructor for the chart series.
92 93 */
93 94 QAbstractSeries::~QAbstractSeries()
94 95 {
95 96 if(d_ptr->m_dataset) qFatal("Still binded series detected !");
96 97 }
97 98
98 99 /*!
99 100 \brief Returns the pointer to the model that is used as the series data source
100 101 */
101 102 QAbstractItemModel* QAbstractSeries::model() const
102 103 {
103 104 return d_ptr->m_model;
104 105 }
105 106
106 107 void QAbstractSeries::setName(const QString& name)
107 108 {
108 109 d_ptr->m_name = name;
109 110 }
110 111
111 112 /*!
112 113 \brief Returns the name of the series.
113 114 \sa setName()
114 115 */
115 116 QString QAbstractSeries::name() const
116 117 {
117 118 return d_ptr->m_name;
118 119 }
119 120
120 121 ///////////////////////////////////////////////////////////////////////////////////////////////////
121 122
122 123 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q):
123 124 q_ptr(q),
124 125 m_model(0),
125 126 m_dataset(0)
126 127 {
127 128 }
128 129
129 130 QAbstractSeriesPrivate::~QAbstractSeriesPrivate()
130 131 {
131 132 }
132 133
133 134 #include "moc_qabstractseries.cpp"
134 135 #include "moc_qabstractseries_p.cpp"
135 136
136 137 QTCOMMERCIALCHART_END_NAMESPACE
137 138
138 139
General Comments 0
You need to be logged in to leave comments. Login now