##// END OF EJS Templates
barchart: removed old model
sauimone -
r1005:a6e87336a436
parent child
Show More
@@ -1,50 +1,51
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "drilldownchart.h"
21 #include "drilldownchart.h"
22 #include <QChartAxis>
22 #include <QChartAxis>
23
23
24 QTCOMMERCIALCHART_USE_NAMESPACE
24 QTCOMMERCIALCHART_USE_NAMESPACE
25
25
26 DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
26 DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 : QChart(parent, wFlags)
27 : QChart(parent, wFlags)
28 ,m_currentSeries(0)
28 ,m_currentSeries(0)
29 {
29 {
30 }
30 }
31
31
32 void DrilldownChart::changeSeries(QAbstractSeries *series)
32 void DrilldownChart::changeSeries(QAbstractSeries *series)
33 {
33 {
34 if (m_currentSeries)
34 if (m_currentSeries) {
35 removeSeries(m_currentSeries);
35 removeSeries(m_currentSeries);
36 }
36 m_currentSeries = series;
37 m_currentSeries = series;
37 addSeries(series);
38 addSeries(series);
38 setTitle(series->name());
39 setTitle(series->name());
39 axisY()->setNiceNumbersEnabled(true);
40 axisY()->setNiceNumbersEnabled(true);
40 }
41 }
41
42
42 void DrilldownChart::handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
43 void DrilldownChart::handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
43 {
44 {
44 Q_UNUSED(barset)
45 Q_UNUSED(barset)
45 Q_UNUSED(button)
46 Q_UNUSED(button)
46 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
47 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
47 changeSeries(series->drilldownSeries(category));
48 changeSeries(series->drilldownSeries(category));
48 }
49 }
49
50
50 #include "moc_drilldownchart.cpp"
51 #include "moc_drilldownchart.cpp"
@@ -1,33 +1,31
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/bar.cpp \
5 $$PWD/bar.cpp \
6 $$PWD/barchartmodel.cpp \
7 $$PWD/barchartitem.cpp \
6 $$PWD/barchartitem.cpp \
8 $$PWD/percentbarchartitem.cpp \
7 $$PWD/percentbarchartitem.cpp \
9 $$PWD/qbarseries.cpp \
8 $$PWD/qbarseries.cpp \
10 $$PWD/qbarset.cpp \
9 $$PWD/qbarset.cpp \
11 $$PWD/qpercentbarseries.cpp \
10 $$PWD/qpercentbarseries.cpp \
12 $$PWD/qstackedbarseries.cpp \
11 $$PWD/qstackedbarseries.cpp \
13 $$PWD/stackedbarchartitem.cpp \
12 $$PWD/stackedbarchartitem.cpp \
14 $$PWD/barlabel.cpp
13 $$PWD/barlabel.cpp
15
14
16 PRIVATE_HEADERS += \
15 PRIVATE_HEADERS += \
17 $$PWD/bar_p.h \
16 $$PWD/bar_p.h \
18 $$PWD/barchartmodel_p.h \
19 $$PWD/barchartitem_p.h \
17 $$PWD/barchartitem_p.h \
20 $$PWD/percentbarchartitem_p.h \
18 $$PWD/percentbarchartitem_p.h \
21 $$PWD/stackedbarchartitem_p.h \
19 $$PWD/stackedbarchartitem_p.h \
22 $$PWD/barlabel_p.h \
20 $$PWD/barlabel_p.h \
23 $$PWD/qbarset_p.h \
21 $$PWD/qbarset_p.h \
24 $$PWD/qbarseries_p.h \
22 $$PWD/qbarseries_p.h \
25 $$PWD/qstackedbarseries_p.h\
23 $$PWD/qstackedbarseries_p.h\
26 $$PWD/qpercentbarseries_p.h
24 $$PWD/qpercentbarseries_p.h
27
25
28 PUBLIC_HEADERS += \
26 PUBLIC_HEADERS += \
29 $$PWD/qbarseries.h \
27 $$PWD/qbarseries.h \
30 $$PWD/qbarset.h \
28 $$PWD/qbarset.h \
31 $$PWD/qpercentbarseries.h \
29 $$PWD/qpercentbarseries.h \
32 $$PWD/qstackedbarseries.h
30 $$PWD/qstackedbarseries.h
33
31
@@ -1,86 +1,84
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef BARCHARTITEM_H
21 #ifndef BARCHARTITEM_H
22 #define BARCHARTITEM_H
22 #define BARCHARTITEM_H
23
23
24 #include "chartitem_p.h"
24 #include "chartitem_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Bar;
31 class Bar;
32 class BarLabel;
32 class BarLabel;
33 class QChartAxisCategories;
33 class QChartAxisCategories;
34 class QChart;
34 class QChart;
35
35
36 //typedef QVector<QRectF> BarLayout;
36 //typedef QVector<QRectF> BarLayout;
37
37
38 class BarChartItem : public ChartItem
38 class BarChartItem : public ChartItem
39 {
39 {
40 Q_OBJECT
40 Q_OBJECT
41 public:
41 public:
42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
43 virtual ~BarChartItem();
43 virtual ~BarChartItem();
44
44
45 public:
45 public:
46 // From QGraphicsItem
46 // From QGraphicsItem
47 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
47 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
48 QRectF boundingRect() const;
48 QRectF boundingRect() const;
49
49
50 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
51 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
50 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
52
51
53 virtual QVector<QRectF> calculateLayout();
52 virtual QVector<QRectF> calculateLayout();
54 void applyLayout(const QVector<QRectF> &layout);
53 void applyLayout(const QVector<QRectF> &layout);
55 void setLayout(const QVector<QRectF> &layout);
54 void setLayout(const QVector<QRectF> &layout);
56 void updateLayout(const QVector<QRectF> &layout);
55 void updateLayout(const QVector<QRectF> &layout);
57
56
58 QRectF geometry() const { return m_rect;}
57 QRectF geometry() const { return m_rect;}
59
58
60 public Q_SLOTS:
59 public Q_SLOTS:
61 void handleModelChanged();
60 void handleModelChanged();
62 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
61 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
63 void handleGeometryChanged(const QRectF &size);
62 void handleGeometryChanged(const QRectF &size);
64 void handleLayoutChanged();
63 void handleLayoutChanged();
65
64
66 protected:
65 protected:
67
66
68 // TODO: consider these.
69 qreal m_domainMinX;
67 qreal m_domainMinX;
70 qreal m_domainMaxX;
68 qreal m_domainMaxX;
71 qreal m_domainMinY;
69 qreal m_domainMinY;
72 qreal m_domainMaxY;
70 qreal m_domainMaxY;
73
71
74 QRectF m_rect;
72 QRectF m_rect;
75 bool m_layoutSet; // True, if component has been laid out.
73 bool m_layoutSet; // True, if component has been laid out.
76 QVector<QRectF> m_layout;
74 QVector<QRectF> m_layout;
77
75
78 // Not owned.
76 // Not owned.
79 QBarSeries *m_series;
77 QBarSeries *m_series;
80 QList<Bar *> m_bars;
78 QList<Bar *> m_bars;
81 QList<BarLabel *> m_labels;
79 QList<BarLabel *> m_labels;
82 };
80 };
83
81
84 QTCOMMERCIALCHART_END_NAMESPACE
82 QTCOMMERCIALCHART_END_NAMESPACE
85
83
86 #endif // BARCHARTITEM_H
84 #endif // BARCHARTITEM_H
@@ -1,456 +1,512
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "barchartmodel_p.h"
26 #include "domain_p.h"
25 #include "domain_p.h"
27 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
28 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
29 #include "charttheme_p.h"
28 #include "charttheme_p.h"
30 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
31
30
32 #include <QAbstractItemModel>
31 #include <QAbstractItemModel>
33 #include <QModelIndex>
32 #include <QModelIndex>
34
33
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
35
37 /*!
36 /*!
38 \class QBarSeries
37 \class QBarSeries
39 \brief part of QtCommercial chart API.
38 \brief part of QtCommercial chart API.
40 \mainclass
39 \mainclass
41
40
42 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
41 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
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
44 by QStringList.
43 by QStringList.
45
44
46 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 \image examples_barchart.png
46 \image examples_barchart.png
48
47
49 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 */
49 */
51
50
52 /*!
51 /*!
53 \fn void QBarSeries::clicked(QBarSet *barset, QString category, Qt::MouseButtons button)
52 \fn void QBarSeries::clicked(QBarSet *barset, QString category, Qt::MouseButtons button)
54
53
55 The signal is emitted if the user clicks with a mouse \a button on top of QBarSet \a barset of category \a category
54 The signal is emitted if the user clicks with a mouse \a button on top of QBarSet \a barset of category \a category
56 contained by the series.
55 contained by the series.
57 */
56 */
58
57
59 /*!
58 /*!
60 \fn void QBarSeries::selected()
59 \fn void QBarSeries::selected()
61
60
62 The signal is emitted if the user selects/deselects the series. The logic for storing selections should be
61 The signal is emitted if the user selects/deselects the series. The logic for storing selections should be
63 implemented by the user of QBarSeries API.
62 implemented by the user of QBarSeries API.
64 */
63 */
65
64
66 /*!
65 /*!
67 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
66 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
68
67
69 The signal is emitted if mouse is hovered on top of series.
68 The signal is emitted if mouse is hovered on top of series.
70 Parameter \a barset is the pointer of barset, where hover happened.
69 Parameter \a barset is the pointer of barset, where hover happened.
71 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
70 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
72 */
71 */
73
72
74 /*!
73 /*!
75 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
74 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
76 QBarSeries is QObject which is a child of a \a parent.
75 QBarSeries is QObject which is a child of a \a parent.
77 */
76 */
78 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
77 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
79 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
78 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
80 {
79 {
81 }
80 }
82
81
83 /*!
82 /*!
84 Destructs barseries and owned barsets.
83 Destructs barseries and owned barsets.
85 */
84 */
86 QBarSeries::~QBarSeries()
85 QBarSeries::~QBarSeries()
87 {
86 {
88 // NOTE: d_ptr destroyed by QObject
87 // NOTE: d_ptr destroyed by QObject
89 }
88 }
90
89
91 /*!
90 /*!
92 \internal
91 \internal
93 */
92 */
94 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
93 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
95 QAbstractSeries(d,parent)
94 QAbstractSeries(d,parent)
96 {
95 {
97 }
96 }
98
97
99 /*!
98 /*!
100 Returns the type of series. Derived classes override this.
99 Returns the type of series. Derived classes override this.
101 */
100 */
102 QAbstractSeries::QSeriesType QBarSeries::type() const
101 QAbstractSeries::QSeriesType QBarSeries::type() const
103 {
102 {
104 return QAbstractSeries::SeriesTypeBar;
103 return QAbstractSeries::SeriesTypeBar;
105 }
104 }
106
105
107 /*!
106 /*!
108 Adds a set of bars to series. Takes ownership of \a set.
107 Adds a set of bars to series. Takes ownership of \a set.
109 Connects the clicked(QString, Qt::MouseButtons) signal
108 Connects the clicked(QString, Qt::MouseButtons) signal
110 of \a set to this series
109 of \a set to this series
111 */
110 */
112 void QBarSeries::appendBarSet(QBarSet *set)
111 void QBarSeries::appendBarSet(QBarSet *set)
113 {
112 {
114 Q_D(QBarSeries);
113 Q_D(QBarSeries);
115 d->m_internalModel->appendBarSet(set);
114 d->m_barSets.append(set);
116 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
115 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
117 emit d->restructuredBars();
116 emit d->restructuredBars();
118 }
117 }
119
118
120 /*!
119 /*!
121 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
120 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
122 Disconnects the clicked(QString, Qt::MouseButtons) signal
121 Disconnects the clicked(QString, Qt::MouseButtons) signal
123 of \a set from this series
122 of \a set from this series
124 */
123 */
125 void QBarSeries::removeBarSet(QBarSet *set)
124 void QBarSeries::removeBarSet(QBarSet *set)
126 {
125 {
127 Q_D(QBarSeries);
126 Q_D(QBarSeries);
128 d->m_internalModel->removeBarSet(set);
127 if (d->m_barSets.contains(set)) {
129 emit d->restructuredBars();
128 d->m_barSets.removeOne(set);
129 emit d->restructuredBars();
130 }
130 }
131 }
131
132
132 /*!
133 /*!
133 Adds a list of barsets to series. Takes ownership of \a sets.
134 Adds a list of barsets to series. Takes ownership of \a sets.
134 Connects the clicked(QString, Qt::MouseButtons) signals
135 Connects the clicked(QString, Qt::MouseButtons) signals
135 of \a sets to this series
136 of \a sets to this series
136 */
137 */
137 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
138 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
138 {
139 {
139 Q_D(QBarSeries);
140 Q_D(QBarSeries);
140 foreach (QBarSet* barset, sets) {
141 foreach (QBarSet* barset, sets) {
141 d->m_internalModel->appendBarSet(barset);
142 d->m_barSets.append(barset);
142 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
143 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
143 }
144 }
144 emit d->restructuredBars();
145 emit d->restructuredBars();
145
146 }
146 }
147
147
148 /*!
148 /*!
149 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
149 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
150 Disconnects the clicked(QString, Qt::MouseButtons) signal
150 Disconnects the clicked(QString, Qt::MouseButtons) signal
151 of \a sets from this series
151 of \a sets from this series
152 */
152 */
153 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
153 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
154 {
154 {
155 Q_D(QBarSeries);
155 Q_D(QBarSeries);
156
156
157 foreach (QBarSet* barset, sets) {
157 foreach (QBarSet* barset, sets) {
158 d->m_internalModel->removeBarSet(barset);
158 if (d->m_barSets.contains(barset)) {
159 d->m_barSets.removeOne(barset);
160 }
159 }
161 }
160 emit d->restructuredBars();
162 emit d->restructuredBars();
161 }
163 }
162
164
163 /*!
165 /*!
164 Inserts new \a set on the \a i position.
166 Inserts new \a set on the \a i position.
165 The barset that is currently at this postion is moved to postion i + 1
167 The barset that is currently at this postion is moved to postion i + 1
166 */
168 */
167 void QBarSeries::insertBarSet(int i, QBarSet *set)
169 void QBarSeries::insertBarSet(int i, QBarSet *set)
168 {
170 {
169 Q_D(QBarSeries);
171 Q_D(QBarSeries);
170 d->m_internalModel->insertBarSet(i, set);
172 d->m_barSets.insert(i,set);
171 emit d->barsetChanged();
173 emit d->barsetChanged();
172 }
174 }
173
175
174 /*!
176 /*!
175 Returns number of sets in series.
177 Returns number of sets in series.
176 */
178 */
177 int QBarSeries::barsetCount() const
179 int QBarSeries::barsetCount() const
178 {
180 {
179 Q_D(const QBarSeries);
181 Q_D(const QBarSeries);
180 return d->m_internalModel->barsetCount();
182 return d->m_barSets.count();
181 }
183 }
182
184
183 /*!
185 /*!
184 Returns number of categories in series
186 Returns number of categories in series
185 */
187 */
186 int QBarSeries::categoryCount() const
188 int QBarSeries::categoryCount() const
187 {
189 {
188 Q_D(const QBarSeries);
190 Q_D(const QBarSeries);
189 return d->m_internalModel->categoryCount();
191 return d->m_categories.count();
190 }
192 }
191
193
192 /*!
194 /*!
193 Returns a list of sets in series. Keeps ownership of sets.
195 Returns a list of sets in series. Keeps ownership of sets.
194 */
196 */
195 QList<QBarSet*> QBarSeries::barSets() const
197 QList<QBarSet*> QBarSeries::barSets() const
196 {
198 {
197 Q_D(const QBarSeries);
199 Q_D(const QBarSeries);
198 return d->m_internalModel->barSets();
200 return d->m_barSets;
199 }
201 }
200
202
201 /*!
203 /*!
202 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
204 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
203 Sets the \a model to be used as a data source
205 Sets the \a model to be used as a data source
204 */
206 */
205 bool QBarSeries::setModel(QAbstractItemModel *model)
207 bool QBarSeries::setModel(QAbstractItemModel *model)
206 {
208 {
207 Q_D(QBarSeries);
209 Q_D(QBarSeries);
208 return d->setModel(model);
210 return d->setModel(model);
209 }
211 }
210
212
211 /*!
213 /*!
212 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
214 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
213 Sets column/row specified by \a categories to be used as a list of bar series categories.
215 Sets column/row specified by \a categories to be used as a list of bar series categories.
214 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
216 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
215 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
217 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
216 All the columns/rows inbetween those two values are also used as data for bar sets.
218 All the columns/rows inbetween those two values are also used as data for bar sets.
217 The \a orientation parameter specifies whether the data is in columns or in rows.
219 The \a orientation parameter specifies whether the data is in columns or in rows.
218 */
220 */
219 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
221 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
220 {
222 {
221 Q_D(QBarSeries);
223 Q_D(QBarSeries);
222 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
224 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
223 }
225 }
224
226
225 /*!
227 /*!
226 Returns the bar categories of the series.
228 Returns the bar categories of the series.
227 */
229 */
228 QBarCategories QBarSeries::categories() const
230 QBarCategories QBarSeries::categories() const
229 {
231 {
230 Q_D(const QBarSeries);
232 Q_D(const QBarSeries);
231
233 return d->m_categories;
232 QBarCategories categories;
233 int count = d->m_internalModel->categoryCount();
234 for (int i=1; i <= count; i++) {
235 categories.insert(i, d->m_internalModel->categoryName(i - 1));
236 }
237 return categories;
238
239 }
234 }
240
235
241 /*!
236 /*!
242 Sets the visibility of labels in series to \a visible
237 Sets the visibility of labels in series to \a visible
243 */
238 */
244 void QBarSeries::setLabelsVisible(bool visible)
239 void QBarSeries::setLabelsVisible(bool visible)
245 {
240 {
246 foreach (QBarSet* s, barSets()) {
241 foreach (QBarSet* s, barSets()) {
247 s->setLabelsVisible(visible);
242 s->setLabelsVisible(visible);
248 }
243 }
249 }
244 }
250
245
251 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
246 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
252
247
253 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
248 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
254 QAbstractSeriesPrivate(q),
249 QAbstractSeriesPrivate(q),
255 m_internalModel(new BarChartModel(categories,this)),
250 m_categories(categories),
256 m_model(0),
251 m_model(0),
257 m_mapCategories(-1),
252 m_mapCategories(-1),
258 m_mapBarBottom(-1),
253 m_mapBarBottom(-1),
259 m_mapBarTop(-1),
254 m_mapBarTop(-1),
260 m_mapOrientation(Qt::Vertical)
255 m_mapOrientation(Qt::Vertical)
261 {
256 {
262 }
257 }
263
258
264 QBarSet* QBarSeriesPrivate::barsetAt(int index)
259 QBarSet* QBarSeriesPrivate::barsetAt(int index)
265 {
260 {
266 return m_internalModel->barsetAt(index);
261 return m_barSets.at(index);
267 }
262 }
268
263
269 QString QBarSeriesPrivate::categoryName(int category)
264 QString QBarSeriesPrivate::categoryName(int category)
270 {
265 {
271 return m_internalModel->categoryName(category);
266 return m_categories.at(category);
272 }
267 }
273
268
274 qreal QBarSeriesPrivate::min()
269 qreal QBarSeriesPrivate::min()
275 {
270 {
276 return m_internalModel->min();
271 if (m_barSets.count() <= 0) {
272 return 0;
273 }
274 qreal min = INT_MAX;
275
276 for (int i = 0; i < m_barSets.count(); i++) {
277 int categoryCount = m_barSets.at(i)->count();
278 for (int j = 0; j < categoryCount; j++) {
279 qreal temp = m_barSets.at(i)->at(j);
280 if (temp < min)
281 min = temp;
282 }
283 }
284 return min;
277 }
285 }
278
286
279 qreal QBarSeriesPrivate::max()
287 qreal QBarSeriesPrivate::max()
280 {
288 {
281 return m_internalModel->max();
289 if (m_barSets.count() <= 0) {
290 return 0;
291 }
292 qreal max = INT_MIN;
293
294 for (int i = 0; i < m_barSets.count(); i++) {
295 int categoryCount = m_barSets.at(i)->count();
296 for (int j = 0; j < categoryCount; j++) {
297 qreal temp = m_barSets.at(i)->at(j);
298 if (temp > max)
299 max = temp;
300 }
301 }
302
303 return max;
282 }
304 }
283
305
284 qreal QBarSeriesPrivate::valueAt(int set, int category)
306 qreal QBarSeriesPrivate::valueAt(int set, int category)
285 {
307 {
286 return m_internalModel->valueAt(set, category);
308 if ((set < 0) || (set >= m_barSets.count())) {
309 // No set, no value.
310 return 0;
311 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
312 // No category, no value.
313 return 0;
314 }
315
316 return m_barSets.at(set)->at(category);
287 }
317 }
288
318
289 qreal QBarSeriesPrivate::percentageAt(int set, int category)
319 qreal QBarSeriesPrivate::percentageAt(int set, int category)
290 {
320 {
291 return m_internalModel->percentageAt(set, category);
321 if ((set < 0) || (set >= m_barSets.count())) {
322 // No set, no value.
323 return 0;
324 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
325 // No category, no value.
326 return 0;
327 }
328
329 qreal value = m_barSets.at(set)->at(category);
330 qreal sum = categorySum(category);
331 if ( qFuzzyIsNull(sum) ) {
332 return 0;
333 }
334
335 return value / sum;
292 }
336 }
293
337
294 qreal QBarSeriesPrivate::categorySum(int category)
338 qreal QBarSeriesPrivate::categorySum(int category)
295 {
339 {
296 return m_internalModel->categorySum(category);
340 qreal sum(0);
341 int count = m_barSets.count(); // Count sets
342 for (int set = 0; set < count; set++) {
343 if (category < m_barSets.at(set)->count())
344 sum += m_barSets.at(set)->at(category);
345 }
346 return sum;
297 }
347 }
298
348
299 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
349 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
300 {
350 {
301 return m_internalModel->absoluteCategorySum(category);
351 qreal sum(0);
352 int count = m_barSets.count(); // Count sets
353 for (int set = 0; set < count; set++) {
354 if (category < m_barSets.at(set)->count())
355 sum += qAbs(m_barSets.at(set)->at(category));
356 }
357 return sum;
302 }
358 }
303
359
304 qreal QBarSeriesPrivate::maxCategorySum()
360 qreal QBarSeriesPrivate::maxCategorySum()
305 {
361 {
306 return m_internalModel->maxCategorySum();
362 qreal max = INT_MIN;
307 }
363 int count = m_categories.count();
308
364 for (int i = 0; i < count; i++) {
309 BarChartModel& QBarSeriesPrivate::modelInternal()
365 qreal sum = categorySum(i);
310 {
366 if (sum > max)
311 return *m_internalModel;
367 max = sum;
368 }
369 return max;
312 }
370 }
313
371
314 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
372 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
315 {
373 {
316 // disconnect signals from old model
374 // disconnect signals from old model
317 if(m_model)
375 if(m_model)
318 {
376 {
319 disconnect(m_model, 0, this, 0);
377 disconnect(m_model, 0, this, 0);
320 m_mapCategories = -1;
378 m_mapCategories = -1;
321 m_mapBarBottom = -1;
379 m_mapBarBottom = -1;
322 m_mapBarTop = -1;
380 m_mapBarTop = -1;
323 m_mapOrientation = Qt::Vertical;
381 m_mapOrientation = Qt::Vertical;
324 }
382 }
325
383
326 // set new model
384 // set new model
327 if(model)
385 if(model)
328 {
386 {
329 m_model = model;
387 m_model = model;
330 return true;
388 return true;
331 }
389 }
332 else
390 else
333 {
391 {
334 m_model = 0;
392 m_model = 0;
335 return false;
393 return false;
336 }
394 }
337 }
395 }
338
396
339 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
397 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
340 {
398 {
341 Q_Q(QBarSeries);
399 Q_Q(QBarSeries);
342
400
343 if (m_model == 0)
401 if (m_model == 0)
344 return;
402 return;
345
403
346 m_mapCategories = categories;
404 m_mapCategories = categories;
347 m_mapBarBottom = bottomBoundry;
405 m_mapBarBottom = bottomBoundry;
348 m_mapBarTop = topBoundry;
406 m_mapBarTop = topBoundry;
349 m_mapOrientation = orientation;
407 m_mapOrientation = orientation;
350
408
351 // connect the signals
409 // connect the signals
352 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
410 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
353 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
411 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
354
412
355 // create the initial bars
413 // create the initial bars
356 delete m_internalModel;
414 m_categories.clear();
357 if (m_mapOrientation == Qt::Vertical) {
415 if (m_mapOrientation == Qt::Vertical) {
358 QStringList categories;
416 for (int k = 0; k < m_model->rowCount(); k++) {
359 for (int k = 0; k < m_model->rowCount(); k++)
417 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
360 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
418 }
361 m_internalModel = new BarChartModel(categories, this);
362
419
363 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
420 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
364 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
421 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
365 for(int m = 0; m < m_model->rowCount(); m++)
422 for(int m = 0; m < m_model->rowCount(); m++)
366 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
423 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
367 q->appendBarSet(barSet);
424 q->appendBarSet(barSet);
368 }
425 }
369 } else {
426 } else {
370 QStringList categories;
427 for (int k = 0; k < m_model->columnCount(); k++) {
371 for (int k = 0; k < m_model->columnCount(); k++)
428 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
372 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
429 }
373 m_internalModel = new BarChartModel(categories, this);
374
430
375 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
431 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
376 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
432 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
377 for(int m = 0; m < m_model->columnCount(); m++)
433 for(int m = 0; m < m_model->columnCount(); m++)
378 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
434 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
379 q->appendBarSet(barSet);
435 q->appendBarSet(barSet);
380 }
436 }
381 }
437 }
382 }
438 }
383
439
384 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
440 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
385 {
441 {
386 Q_UNUSED(bottomRight)
442 Q_UNUSED(bottomRight)
387
443
388 if (m_mapOrientation == Qt::Vertical)
444 if (m_mapOrientation == Qt::Vertical)
389 {
445 {
390 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
446 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
391 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
447 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
392 barsetAt(topLeft.column() - m_mapBarBottom)->replace(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
448 barsetAt(topLeft.column() - m_mapBarBottom)->replace(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
393 }
449 }
394 else
450 else
395 {
451 {
396 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
452 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
397 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
453 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
398 barsetAt(topLeft.row() - m_mapBarBottom)->replace(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
454 barsetAt(topLeft.row() - m_mapBarBottom)->replace(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
399 }
455 }
400 }
456 }
401
457
402 void QBarSeriesPrivate::barsetChanged()
458 void QBarSeriesPrivate::barsetChanged()
403 {
459 {
404 emit updatedBars();
460 emit updatedBars();
405 }
461 }
406
462
407 void QBarSeriesPrivate::scaleDomain(Domain& domain)
463 void QBarSeriesPrivate::scaleDomain(Domain& domain)
408 {
464 {
409 qreal minX(domain.minX());
465 qreal minX(domain.minX());
410 qreal minY(domain.minY());
466 qreal minY(domain.minY());
411 qreal maxX(domain.maxX());
467 qreal maxX(domain.maxX());
412 qreal maxY(domain.maxY());
468 qreal maxY(domain.maxY());
413 int tickXCount(domain.tickXCount());
469 int tickXCount(domain.tickXCount());
414 int tickYCount(domain.tickYCount());
470 int tickYCount(domain.tickYCount());
415
471
416 qreal x = m_internalModel->categoryCount();
472 qreal x = m_categories.count();
417 qreal y = max();
473 qreal y = max();
418 minX = qMin(minX, x);
474 minX = qMin(minX, x);
419 minY = qMin(minY, y);
475 minY = qMin(minY, y);
420 maxX = qMax(maxX, x);
476 maxX = qMax(maxX, x);
421 maxY = qMax(maxY, y);
477 maxY = qMax(maxY, y);
422 tickXCount = x+1;
478 tickXCount = x+1;
423
479
424 domain.setRangeX(minX,maxX,tickXCount);
480 domain.setRangeX(minX,maxX,tickXCount);
425 domain.setRangeY(minY,maxY,tickYCount);
481 domain.setRangeY(minY,maxY,tickYCount);
426 }
482 }
427
483
428 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
484 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
429 {
485 {
430 Q_Q(QBarSeries);
486 Q_Q(QBarSeries);
431
487
432 BarChartItem* bar = new BarChartItem(q,presenter);
488 BarChartItem* bar = new BarChartItem(q,presenter);
433 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
489 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
434 presenter->animator()->addAnimation(bar);
490 presenter->animator()->addAnimation(bar);
435 }
491 }
436 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
492 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
437 return bar;
493 return bar;
438
494
439 }
495 }
440
496
441 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
497 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
442 {
498 {
443 Q_Q(QBarSeries);
499 Q_Q(QBarSeries);
444 QList<LegendMarker*> markers;
500 QList<LegendMarker*> markers;
445 foreach(QBarSet* set, q->barSets()) {
501 foreach(QBarSet* set, q->barSets()) {
446 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
502 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
447 markers << marker;
503 markers << marker;
448 }
504 }
449
505
450 return markers;
506 return markers;
451 }
507 }
452
508
453 #include "moc_qbarseries.cpp"
509 #include "moc_qbarseries.cpp"
454 #include "moc_qbarseries_p.cpp"
510 #include "moc_qbarseries_p.cpp"
455
511
456 QTCOMMERCIALCHART_END_NAMESPACE
512 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,80 +1,79
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 typedef QStringList QBarCategories;
31 typedef QStringList QBarCategories;
32
32
33 class QBarSet;
33 class QBarSet;
34 class BarChartModel;
35 class BarCategory;
34 class BarCategory;
36 class QBarSeriesPrivate;
35 class QBarSeriesPrivate;
37
36
38 // Container for series
37 // Container for series
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
38 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
40 {
39 {
41 Q_OBJECT
40 Q_OBJECT
42 public:
41 public:
43 explicit QBarSeries(QBarCategories categories, QObject *parent = 0);
42 explicit QBarSeries(QBarCategories categories, QObject *parent = 0);
44 virtual ~QBarSeries();
43 virtual ~QBarSeries();
45
44
46 QAbstractSeries::QSeriesType type() const;
45 QAbstractSeries::QSeriesType type() const;
47
46
48 void appendBarSet(QBarSet *set); // Takes ownership of set
47 void appendBarSet(QBarSet *set); // Takes ownership of set
49 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
48 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
50 void appendBarSets(QList<QBarSet* > sets);
49 void appendBarSets(QList<QBarSet* > sets);
51 void removeBarSets(QList<QBarSet* > sets);
50 void removeBarSets(QList<QBarSet* > sets);
52 void insertBarSet(int i, QBarSet *set);
51 void insertBarSet(int i, QBarSet *set);
53 int barsetCount() const;
52 int barsetCount() const;
54 int categoryCount() const;
53 int categoryCount() const;
55 QList<QBarSet*> barSets() const;
54 QList<QBarSet*> barSets() const;
56 QBarCategories categories() const;
55 QBarCategories categories() const;
57
56
58 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
59
58
60 bool setModel(QAbstractItemModel *model);
59 bool setModel(QAbstractItemModel *model);
61 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
60 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
62
61
63 protected:
62 protected:
64 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
63 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
65
64
66 Q_SIGNALS:
65 Q_SIGNALS:
67 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
66 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
68 void selected();
67 void selected();
69 void hovered(QBarSet* barset, bool status);
68 void hovered(QBarSet* barset, bool status);
70
69
71 protected:
70 protected:
72 Q_DECLARE_PRIVATE(QBarSeries)
71 Q_DECLARE_PRIVATE(QBarSeries)
73 friend class BarChartItem;
72 friend class BarChartItem;
74 friend class PercentBarChartItem;
73 friend class PercentBarChartItem;
75 friend class StackedBarChartItem;
74 friend class StackedBarChartItem;
76 };
75 };
77
76
78 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
79
78
80 #endif // BARSERIES_H
79 #endif // BARSERIES_H
@@ -1,62 +1,63
1 #ifndef QBARSERIES_P_H
1 #ifndef QBARSERIES_P_H
2 #define QBARSERIES_P_H
2 #define QBARSERIES_P_H
3
3
4 #include "qbarseries.h"
4 #include "qbarseries.h"
5 #include "qabstractseries_p.h"
5 #include "qabstractseries_p.h"
6 #include <QStringList>
6 #include <QStringList>
7 #include <QAbstractSeries>
7 #include <QAbstractSeries>
8
8
9 class QModelIndex;
9 class QModelIndex;
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 // Container for series
13 // Container for series
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
19
19
20 void scaleDomain(Domain& domain);
20 void scaleDomain(Domain& domain);
21 Chart* createGraphics(ChartPresenter* presenter);
21 Chart* createGraphics(ChartPresenter* presenter);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
23
23
24 bool setModel(QAbstractItemModel *model);
24 bool setModel(QAbstractItemModel *model);
25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
26
26
27 QBarSet* barsetAt(int index);
27 QBarSet* barsetAt(int index);
28 QString categoryName(int category);
28 QString categoryName(int category);
29 qreal min();
29 qreal min();
30 qreal max();
30 qreal max();
31 qreal valueAt(int set, int category);
31 qreal valueAt(int set, int category);
32 qreal percentageAt(int set, int category);
32 qreal percentageAt(int set, int category);
33 qreal categorySum(int category);
33 qreal categorySum(int category);
34 qreal absoluteCategorySum(int category);
34 qreal absoluteCategorySum(int category);
35 qreal maxCategorySum();
35 qreal maxCategorySum();
36 BarChartModel& modelInternal();
37
36
38 Q_SIGNALS:
37 Q_SIGNALS:
39 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
38 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
40 void selected();
39 void selected();
41 void updatedBars();
40 void updatedBars();
42 void restructuredBars();
41 void restructuredBars();
43
42
44 private Q_SLOTS:
43 private Q_SLOTS:
45 // slots for updating bars when data in model changes
44 // slots for updating bars when data in model changes
46 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
45 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
47 void barsetChanged();
46 void barsetChanged();
48
47
49 protected:
48 protected:
50 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
49 QList<QBarSet *> m_barSets;
50 QBarCategories m_categories;
51
51 QAbstractItemModel* m_model;
52 QAbstractItemModel* m_model;
52 int m_mapCategories;
53 int m_mapCategories;
53 int m_mapBarBottom;
54 int m_mapBarBottom;
54 int m_mapBarTop;
55 int m_mapBarTop;
55 Qt::Orientation m_mapOrientation;
56 Qt::Orientation m_mapOrientation;
56 private:
57 private:
57 Q_DECLARE_PUBLIC(QBarSeries)
58 Q_DECLARE_PUBLIC(QBarSeries)
58 };
59 };
59
60
60 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
61
62
62 #endif // QBARSERIESPRIVATE_P_H
63 #endif // QBARSERIESPRIVATE_P_H
@@ -1,110 +1,109
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qstackedbarseries.h"
21 #include "qstackedbarseries.h"
22 #include "qstackedbarseries_p.h"
22 #include "qstackedbarseries_p.h"
23 #include "stackedbarchartitem_p.h"
23 #include "stackedbarchartitem_p.h"
24 #include "barchartmodel_p.h"
25 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
25 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
28
27
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
29
31 /*!
30 /*!
32 \class QStackedBarSeries
31 \class QStackedBarSeries
33 \brief part of QtCommercial chart API.
32 \brief part of QtCommercial chart API.
34 \mainclass
33 \mainclass
35
34
36 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
35 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
37 stacked on top of each other. One QStackedBarSeries can contain multiple QBarSet data sets.
36 stacked on top of each other. One QStackedBarSeries can contain multiple QBarSet data sets.
38 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
37 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
39
38
40 See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart.
39 See the \l {StackedbarChart Example} {stacked bar chart example} to learn how to create a stacked bar chart.
41 \image examples_stackedbarchart.png
40 \image examples_stackedbarchart.png
42
41
43 \sa QBarSet, QPercentBarSeries, QBarSeries
42 \sa QBarSet, QPercentBarSeries, QBarSeries
44 */
43 */
45
44
46 /*!
45 /*!
47 \fn virtual QSeriesType QStackedBarSeries::type() const
46 \fn virtual QSeriesType QStackedBarSeries::type() const
48 \brief Returns type of series.
47 \brief Returns type of series.
49 \sa QSeriesType
48 \sa QSeriesType
50 */
49 */
51
50
52 /*!
51 /*!
53 Constructs empty QStackedBarSeries. Parameter \a categories defines the categories for chart.
52 Constructs empty QStackedBarSeries. Parameter \a categories defines the categories for chart.
54 QStackedBarSeries is QObject which is a child of a \a parent.
53 QStackedBarSeries is QObject which is a child of a \a parent.
55 */
54 */
56 QStackedBarSeries::QStackedBarSeries(QBarCategories categories, QObject *parent)
55 QStackedBarSeries::QStackedBarSeries(QBarCategories categories, QObject *parent)
57 : QBarSeries(*new QStackedBarSeriesPrivate(categories,this), parent)
56 : QBarSeries(*new QStackedBarSeriesPrivate(categories,this), parent)
58 {
57 {
59 }
58 }
60
59
61 QAbstractSeries::QSeriesType QStackedBarSeries::type() const
60 QAbstractSeries::QSeriesType QStackedBarSeries::type() const
62 {
61 {
63 return QAbstractSeries::SeriesTypeStackedBar;
62 return QAbstractSeries::SeriesTypeStackedBar;
64 }
63 }
65
64
66 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
67
66
68 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QBarCategories categories, QStackedBarSeries *q) : QBarSeriesPrivate(categories,q)
67 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QBarCategories categories, QStackedBarSeries *q) : QBarSeriesPrivate(categories,q)
69 {
68 {
70
69
71 }
70 }
72
71
73 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
72 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
74 {
73 {
75 qreal minX(domain.minX());
74 qreal minX(domain.minX());
76 qreal minY(domain.minY());
75 qreal minY(domain.minY());
77 qreal maxX(domain.maxX());
76 qreal maxX(domain.maxX());
78 qreal maxY(domain.maxY());
77 qreal maxY(domain.maxY());
79 int tickXCount(domain.tickXCount());
78 int tickXCount(domain.tickXCount());
80 int tickYCount(domain.tickYCount());
79 int tickYCount(domain.tickYCount());
81
80
82 qreal x = m_internalModel->categoryCount();
81 qreal x = m_categories.count();
83 qreal y = maxCategorySum();
82 qreal y = maxCategorySum();
84 minX = qMin(minX, x);
83 minX = qMin(minX, x);
85 minY = qMin(minY, y);
84 minY = qMin(minY, y);
86 maxX = qMax(maxX, x);
85 maxX = qMax(maxX, x);
87 maxY = qMax(maxY, y);
86 maxY = qMax(maxY, y);
88 tickXCount = x+1;
87 tickXCount = x+1;
89
88
90 domain.setRangeX(minX,maxX,tickXCount);
89 domain.setRangeX(minX,maxX,tickXCount);
91 domain.setRangeY(minY,maxY,tickYCount);
90 domain.setRangeY(minY,maxY,tickYCount);
92 }
91 }
93
92
94
93
95 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
94 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
96 {
95 {
97 Q_Q(QStackedBarSeries);
96 Q_Q(QStackedBarSeries);
98
97
99 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
98 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
100 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
99 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
101 presenter->animator()->addAnimation(bar);
100 presenter->animator()->addAnimation(bar);
102 }
101 }
103 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
102 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
104 return bar;
103 return bar;
105 }
104 }
106
105
107 #include "moc_qstackedbarseries.cpp"
106 #include "moc_qstackedbarseries.cpp"
108
107
109 QTCOMMERCIALCHART_END_NAMESPACE
108 QTCOMMERCIALCHART_END_NAMESPACE
110
109
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now