##// END OF EJS Templates
fixed crash in barseries with empty sets
sauimone -
r1339:495016ed1794
parent child
Show More
@@ -1,207 +1,211
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 "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qbarseries_p.h"
26 #include "qbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include "chartdataset_p.h"
30 #include "chartdataset_p.h"
31 #include <QPainter>
31 #include <QPainter>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 ChartItem(presenter),
36 ChartItem(presenter),
37 m_series(series)
37 m_series(series)
38 {
38 {
39 setFlag(ItemClipsChildrenToShape);
39 setFlag(ItemClipsChildrenToShape);
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
42 setZValue(ChartPresenter::BarSeriesZValue);
43 setZValue(ChartPresenter::BarSeriesZValue);
43 dataChanged();
44 handleDataStructureChanged();
44 }
45 }
45
46
46 BarChartItem::~BarChartItem()
47 BarChartItem::~BarChartItem()
47 {
48 {
48 }
49 }
49
50
50 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 {
52 {
52 Q_UNUSED(painter);
53 Q_UNUSED(painter);
53 Q_UNUSED(option);
54 Q_UNUSED(option);
54 Q_UNUSED(widget);
55 Q_UNUSED(widget);
55 }
56 }
56
57
57 QRectF BarChartItem::boundingRect() const
58 QRectF BarChartItem::boundingRect() const
58 {
59 {
59 return m_rect;
60 return m_rect;
60 }
61 }
61
62
62 void BarChartItem::dataChanged()
63 void BarChartItem::handleDataStructureChanged()
63 {
64 {
64 foreach(QGraphicsItem *item, childItems()) {
65 foreach(QGraphicsItem *item, childItems()) {
65 delete item;
66 delete item;
66 }
67 }
67
68
68 m_bars.clear();
69 m_bars.clear();
69 m_labels.clear();
70 m_labels.clear();
70 m_layout.clear();
71 m_layout.clear();
71
72
72 bool labelsVisible = m_series->isLabelsVisible();
73 bool labelsVisible = m_series->isLabelsVisible();
73
74
74 // Create new graphic items for bars
75 // Create new graphic items for bars
75 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
76 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
76 for (int s = 0; s < m_series->barsetCount(); s++) {
77 for (int s = 0; s < m_series->barsetCount(); s++) {
77 QBarSet *set = m_series->d_func()->barsetAt(s);
78 QBarSet *set = m_series->d_func()->barsetAt(s);
78
79
79 // Bars
80 // Bars
80 Bar *bar = new Bar(set,c,this);
81 Bar *bar = new Bar(set,c,this);
81 m_bars.append(bar);
82 m_bars.append(bar);
82 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
83 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
83 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
84 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
84 m_layout.append(QRectF(0, 0, 0, 0));
85 m_layout.append(QRectF(0, 0, 0, 0));
85
86
86 // Labels
87 // Labels
87 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
88 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
88 label->setVisible(labelsVisible);
89 label->setVisible(labelsVisible);
89 m_labels.append(label);
90 m_labels.append(label);
90 }
91 }
91 }
92 }
92
93
93 // TODO: Is this the right place to call it?
94 // TODO: Is this the right place to call it?
94 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
95 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 handleLayoutChanged();
95 }
97 }
96
98
97 QVector<QRectF> BarChartItem::calculateLayout()
99 QVector<QRectF> BarChartItem::calculateLayout()
98 {
100 {
99 QVector<QRectF> layout;
101 QVector<QRectF> layout;
100
102
101 // Use temporary qreals for accuracy
103 // Use temporary qreals for accuracy
102 qreal categoryCount = m_series->d_func()->categoryCount();
104 qreal categoryCount = m_series->d_func()->categoryCount();
103 qreal setCount = m_series->barsetCount();
105 qreal setCount = m_series->barsetCount();
104 bool barsVisible = m_series->isVisible();
106 bool barsVisible = m_series->isVisible();
105
107
106 // Domain:
108 // Domain:
107 qreal width = geometry().width();
109 qreal width = geometry().width();
108 qreal height = geometry().height();
110 qreal height = geometry().height();
109 qreal rangeY = m_domainMaxY - m_domainMinY;
111 qreal rangeY = m_domainMaxY - m_domainMinY;
110 qreal rangeX = m_domainMaxX - m_domainMinX;
112 qreal rangeX = m_domainMaxX - m_domainMinX;
111 qreal scaleY = (height / rangeY);
113 qreal scaleY = (height / rangeY);
112 qreal scaleX = (width / rangeX);
114 qreal scaleX = (width / rangeX);
113 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
115 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
114
116
115 int itemIndex(0);
117 int itemIndex(0);
116 for (int category = 0; category < categoryCount; category++) {
118 for (int category = 0; category < categoryCount; category++) {
117 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
119 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
118 for (int set = 0; set < setCount; set++) {
120 for (int set = 0; set < setCount; set++) {
119 QBarSet* barSet = m_series->d_func()->barsetAt(set);
121 QBarSet* barSet = m_series->d_func()->barsetAt(set);
120 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
122 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
121 qreal barHeight = barSet->at(category).y() * scaleY;
123 qreal barHeight = barSet->at(category).y() * scaleY;
122
124
123 Bar* bar = m_bars.at(itemIndex);
125 Bar* bar = m_bars.at(itemIndex);
124 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
126 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
125
127
126 layout.append(rect);
128 layout.append(rect);
127 bar->setPen(barSet->pen());
129 bar->setPen(barSet->pen());
128 bar->setBrush(barSet->brush());
130 bar->setBrush(barSet->brush());
129 bar->setVisible(barsVisible);
131 bar->setVisible(barsVisible);
130
132
131 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
133 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
132
134
133 if (!qFuzzyIsNull(barSet->at(category).y())) {
135 if (!qFuzzyIsNull(barSet->at(category).y())) {
134 label->setText(QString::number(barSet->at(category).y()));
136 label->setText(QString::number(barSet->at(category).y()));
135 } else {
137 } else {
136 label->setText(QString(""));
138 label->setText(QString(""));
137 }
139 }
138
140
139 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
141 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
140 ,yPos - barHeight/2 - label->boundingRect().height()/2);
142 ,yPos - barHeight/2 - label->boundingRect().height()/2);
141 label->setFont(barSet->labelFont());
143 label->setFont(barSet->labelFont());
142 label->setBrush(barSet->labelBrush());
144 label->setBrush(barSet->labelBrush());
143
145
144 itemIndex++;
146 itemIndex++;
145 }
147 }
146 }
148 }
147
149
148 return layout;
150 return layout;
149 }
151 }
150
152
151 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
152 {
154 {
153 if (animator())
155 if (animator()) {
154 animator()->updateLayout(this, m_layout, layout);
156 animator()->updateLayout(this, m_layout, layout);
155 else
157 } else {
156 setLayout(layout);
158 setLayout(layout);
159 }
157 }
160 }
158
161
159 void BarChartItem::setLayout(const QVector<QRectF> &layout)
162 void BarChartItem::setLayout(const QVector<QRectF> &layout)
160 {
163 {
161 m_layout = layout;
164 m_layout = layout;
162
165
163 for (int i=0; i < m_bars.count(); i++)
166 for (int i=0; i < m_bars.count(); i++) {
164 m_bars.at(i)->setRect(layout.at(i));
167 m_bars.at(i)->setRect(layout.at(i));
168 }
165
169
166 update();
170 update();
167 }
171 }
168 //handlers
172 //handlers
169
173
170 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
174 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
171 {
175 {
172 m_domainMinX = minX;
176 m_domainMinX = minX;
173 m_domainMaxX = maxX;
177 m_domainMaxX = maxX;
174 m_domainMinY = minY;
178 m_domainMinY = minY;
175 m_domainMaxY = maxY;
179 m_domainMaxY = maxY;
176 handleLayoutChanged();
180 handleLayoutChanged();
177 }
181 }
178
182
179 void BarChartItem::handleGeometryChanged(const QRectF &rect)
183 void BarChartItem::handleGeometryChanged(const QRectF &rect)
180 {
184 {
181 prepareGeometryChange();
185 prepareGeometryChange();
182 m_rect = rect;
186 m_rect = rect;
183 handleLayoutChanged();
187 handleLayoutChanged();
184 }
188 }
185
189
186 void BarChartItem::handleLayoutChanged()
190 void BarChartItem::handleLayoutChanged()
187 {
191 {
188 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
192 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
189 // rect size zero.
193 // rect size zero.
190 return;
194 return;
191 }
195 }
192 QVector<QRectF> layout = calculateLayout();
196 QVector<QRectF> layout = calculateLayout();
193 applyLayout(layout);
197 applyLayout(layout);
194 update();
198 update();
195 }
199 }
196
200
197 void BarChartItem::labelsVisibleChanged(bool visible)
201 void BarChartItem::handleLabelsVisibleChanged(bool visible)
198 {
202 {
199 foreach (QGraphicsSimpleTextItem* label, m_labels) {
203 foreach (QGraphicsSimpleTextItem* label, m_labels) {
200 label->setVisible(visible);
204 label->setVisible(visible);
201 }
205 }
202 update();
206 update();
203 }
207 }
204
208
205 #include "moc_barchartitem_p.cpp"
209 #include "moc_barchartitem_p.cpp"
206
210
207 QTCOMMERCIALCHART_END_NAMESPACE
211 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,88
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30
30
31 #ifndef BARCHARTITEM_H
31 #ifndef BARCHARTITEM_H
32 #define BARCHARTITEM_H
32 #define BARCHARTITEM_H
33
33
34 #include "chartitem_p.h"
34 #include "chartitem_p.h"
35 #include "qbarseries.h"
35 #include "qbarseries.h"
36 #include <QPen>
36 #include <QPen>
37 #include <QBrush>
37 #include <QBrush>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class Bar;
41 class Bar;
42 class QAxisCategories;
42 class QAxisCategories;
43 class QChart;
43 class QChart;
44
44
45 class BarChartItem : public ChartItem
45 class BarChartItem : public ChartItem
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 public:
48 public:
49 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
49 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
50 virtual ~BarChartItem();
50 virtual ~BarChartItem();
51
51
52 public:
52 public:
53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
54 QRectF boundingRect() const;
54 QRectF boundingRect() const;
55
55
56 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
57
58 virtual QVector<QRectF> calculateLayout();
56 virtual QVector<QRectF> calculateLayout();
59 void applyLayout(const QVector<QRectF> &layout);
57 void applyLayout(const QVector<QRectF> &layout);
60 void setLayout(const QVector<QRectF> &layout);
58 void setLayout(const QVector<QRectF> &layout);
61 void updateLayout(const QVector<QRectF> &layout);
59 void updateLayout(const QVector<QRectF> &layout);
62
60
63 QRectF geometry() const { return m_rect;}
61 QRectF geometry() const { return m_rect;}
64
62
65 public Q_SLOTS:
63 public Q_SLOTS:
66 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
64 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
67 void handleGeometryChanged(const QRectF &size);
65 void handleGeometryChanged(const QRectF &size);
68 void handleLayoutChanged();
66 void handleLayoutChanged();
69 void labelsVisibleChanged(bool visible);
67 void handleLabelsVisibleChanged(bool visible);
68 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
70
69
71 protected:
70 protected:
72
71
73 qreal m_domainMinX;
72 qreal m_domainMinX;
74 qreal m_domainMaxX;
73 qreal m_domainMaxX;
75 qreal m_domainMinY;
74 qreal m_domainMinY;
76 qreal m_domainMaxY;
75 qreal m_domainMaxY;
77
76
78 QRectF m_rect;
77 QRectF m_rect;
79 QVector<QRectF> m_layout;
78 QVector<QRectF> m_layout;
80
79
81 // Not owned.
80 // Not owned.
82 QBarSeries *m_series;
81 QBarSeries *m_series;
83 QList<Bar *> m_bars;
82 QList<Bar *> m_bars;
84 QList<QGraphicsSimpleTextItem *> m_labels;
83 QList<QGraphicsSimpleTextItem *> m_labels;
85 };
84 };
86
85
87 QTCOMMERCIALCHART_END_NAMESPACE
86 QTCOMMERCIALCHART_END_NAMESPACE
88
87
89 #endif // BARCHARTITEM_H
88 #endif // BARCHARTITEM_H
@@ -1,562 +1,561
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 "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QBarSeries
34 \class QBarSeries
35 \brief part of QtCommercial chart API.
35 \brief part of QtCommercial chart API.
36 \mainclass
36 \mainclass
37
37
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 shows the x-values.
41 shows the x-values.
42
42
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 \image examples_barchart.png
44 \image examples_barchart.png
45
45
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \fn void QBarSeries::clicked(QBarSet *barset, int index)
50 \fn void QBarSeries::clicked(QBarSet *barset, int index)
51
51
52 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
52 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
53 Clicked bar inside set is indexed by \a index
53 Clicked bar inside set is indexed by \a index
54 */
54 */
55
55
56 /*!
56 /*!
57 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
57 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
58
58
59 The signal is emitted if mouse is hovered on top of series.
59 The signal is emitted if mouse is hovered on top of series.
60 Parameter \a barset is the pointer of barset, where hover happened.
60 Parameter \a barset is the pointer of barset, where hover happened.
61 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
61 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
62 */
62 */
63
63
64 /*!
64 /*!
65 Constructs empty QBarSeries.
65 Constructs empty QBarSeries.
66 QBarSeries is QObject which is a child of a \a parent.
66 QBarSeries is QObject which is a child of a \a parent.
67 */
67 */
68 QBarSeries::QBarSeries(QObject *parent) :
68 QBarSeries::QBarSeries(QObject *parent) :
69 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
69 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
70 {
70 {
71 }
71 }
72
72
73 /*!
73 /*!
74 Destructs barseries and owned barsets.
74 Destructs barseries and owned barsets.
75 */
75 */
76 QBarSeries::~QBarSeries()
76 QBarSeries::~QBarSeries()
77 {
77 {
78 Q_D(QBarSeries);
78 Q_D(QBarSeries);
79 if(d->m_dataset){
79 if(d->m_dataset){
80 d->m_dataset->removeSeries(this);
80 d->m_dataset->removeSeries(this);
81 }
81 }
82 }
82 }
83
83
84 /*!
84 /*!
85 \internal
85 \internal
86 */
86 */
87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
88 QAbstractSeries(d,parent)
88 QAbstractSeries(d,parent)
89 {
89 {
90 }
90 }
91
91
92 /*!
92 /*!
93 Returns the type of series. Derived classes override this.
93 Returns the type of series. Derived classes override this.
94 */
94 */
95 QAbstractSeries::SeriesType QBarSeries::type() const
95 QAbstractSeries::SeriesType QBarSeries::type() const
96 {
96 {
97 return QAbstractSeries::SeriesTypeBar;
97 return QAbstractSeries::SeriesTypeBar;
98 }
98 }
99
99
100 /*!
100 /*!
101 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
101 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
102 percentage of margin compared to bars
102 percentage of margin compared to bars
103 */
103 */
104 void QBarSeries::setBarMargin(qreal margin)
104 void QBarSeries::setBarMargin(qreal margin)
105 {
105 {
106 Q_D(QBarSeries);
106 Q_D(QBarSeries);
107 d->setBarMargin(margin);
107 d->setBarMargin(margin);
108 }
108 }
109
109
110 /*!
110 /*!
111 Returns the margin around bars
111 Returns the margin around bars
112 */
112 */
113 qreal QBarSeries::barMargin() const
113 qreal QBarSeries::barMargin() const
114 {
114 {
115 Q_D(const QBarSeries);
115 Q_D(const QBarSeries);
116 return d->barMargin();
116 return d->barMargin();
117 }
117 }
118
118
119 /*!
119 /*!
120 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.
120 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.
121 Returns true, if appending succeeded.
121 Returns true, if appending succeeded.
122
122
123 */
123 */
124 bool QBarSeries::append(QBarSet *set)
124 bool QBarSeries::append(QBarSet *set)
125 {
125 {
126 Q_D(QBarSeries);
126 Q_D(QBarSeries);
127 return d->append(set);
127 return d->append(set);
128 }
128 }
129
129
130 /*!
130 /*!
131 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
131 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
132 Returns true, if set was removed.
132 Returns true, if set was removed.
133 */
133 */
134 bool QBarSeries::remove(QBarSet *set)
134 bool QBarSeries::remove(QBarSet *set)
135 {
135 {
136 Q_D(QBarSeries);
136 Q_D(QBarSeries);
137 return d->remove(set);
137 return d->remove(set);
138 }
138 }
139
139
140 /*!
140 /*!
141 Adds a list of barsets to series. Takes ownership of \a sets.
141 Adds a list of barsets to series. Takes ownership of \a sets.
142 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
142 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
143 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
143 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
144 and function returns false.
144 and function returns false.
145 */
145 */
146 bool QBarSeries::append(QList<QBarSet* > sets)
146 bool QBarSeries::append(QList<QBarSet* > sets)
147 {
147 {
148 Q_D(QBarSeries);
148 Q_D(QBarSeries);
149 return d->append(sets);
149 return d->append(sets);
150 }
150 }
151
151
152 /*!
152 /*!
153 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
153 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
154 */
154 */
155 bool QBarSeries::remove(QList<QBarSet* > sets)
155 bool QBarSeries::remove(QList<QBarSet* > sets)
156 {
156 {
157 Q_D(QBarSeries);
157 Q_D(QBarSeries);
158 return d->remove(sets);
158 return d->remove(sets);
159 }
159 }
160
160
161 void QBarSeries::clear()
161 void QBarSeries::clear()
162 {
162 {
163 Q_D(QBarSeries);
163 Q_D(QBarSeries);
164 d->m_barSets.clear();
164 d->m_barSets.clear();
165 }
165 }
166
166
167 /*!
167 /*!
168 Returns number of sets in series.
168 Returns number of sets in series.
169 */
169 */
170 int QBarSeries::barsetCount() const
170 int QBarSeries::barsetCount() const
171 {
171 {
172 Q_D(const QBarSeries);
172 Q_D(const QBarSeries);
173 return d->m_barSets.count();
173 return d->m_barSets.count();
174 }
174 }
175
175
176 /*!
176 /*!
177 Returns a list of sets in series. Keeps ownership of sets.
177 Returns a list of sets in series. Keeps ownership of sets.
178 */
178 */
179 QList<QBarSet*> QBarSeries::barSets() const
179 QList<QBarSet*> QBarSeries::barSets() const
180 {
180 {
181 Q_D(const QBarSeries);
181 Q_D(const QBarSeries);
182 return d->m_barSets;
182 return d->m_barSets;
183 }
183 }
184
184
185 /*!
185 /*!
186 Sets the visibility of series to \a visible
186 Sets the visibility of series to \a visible
187 */
187 */
188 void QBarSeries::setVisible(bool visible)
188 void QBarSeries::setVisible(bool visible)
189 {
189 {
190 Q_D(QBarSeries);
190 Q_D(QBarSeries);
191 d->setVisible(visible);
191 d->setVisible(visible);
192 }
192 }
193
193
194 /*!
194 /*!
195 Returns the visibility of series
195 Returns the visibility of series
196 */
196 */
197 bool QBarSeries::isVisible() const
197 bool QBarSeries::isVisible() const
198 {
198 {
199 Q_D(const QBarSeries);
199 Q_D(const QBarSeries);
200 return d->isVisible();
200 return d->isVisible();
201 }
201 }
202
202
203 /*!
203 /*!
204 Sets the visibility of labels in series to \a visible
204 Sets the visibility of labels in series to \a visible
205 */
205 */
206 void QBarSeries::setLabelsVisible(bool visible)
206 void QBarSeries::setLabelsVisible(bool visible)
207 {
207 {
208 Q_D(QBarSeries);
208 Q_D(QBarSeries);
209 if (d->m_labelsVisible != visible) {
209 if (d->m_labelsVisible != visible) {
210 d->m_labelsVisible = visible;
210 d->m_labelsVisible = visible;
211 emit d->labelsVisibleChanged(visible);
211 emit d->labelsVisibleChanged(visible);
212 }
212 }
213 }
213 }
214
214
215 /*!
215 /*!
216 Returns the visibility of labels
216 Returns the visibility of labels
217 */
217 */
218 bool QBarSeries::isLabelsVisible() const
218 bool QBarSeries::isLabelsVisible() const
219 {
219 {
220 Q_D(const QBarSeries);
220 Q_D(const QBarSeries);
221 return d->m_labelsVisible;
221 return d->m_labelsVisible;
222 }
222 }
223
223
224 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
224 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
225
225
226 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
226 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
227 QAbstractSeriesPrivate(q),
227 QAbstractSeriesPrivate(q),
228 m_barMargin(0.5), // Default value is 50% of category width
228 m_barMargin(0.5), // Default value is 50% of category width
229 m_labelsVisible(false),
229 m_labelsVisible(false),
230 m_visible(true)
230 m_visible(true)
231 {
231 {
232 }
232 }
233
233
234 void QBarSeriesPrivate::setCategories(QStringList categories)
234 void QBarSeriesPrivate::setCategories(QStringList categories)
235 {
235 {
236 m_categories = categories;
236 m_categories = categories;
237 }
237 }
238
238
239 void QBarSeriesPrivate::insertCategory(int index, const QString category)
239 void QBarSeriesPrivate::insertCategory(int index, const QString category)
240 {
240 {
241 m_categories.insert(index, category);
241 m_categories.insert(index, category);
242 emit categoriesUpdated();
242 emit categoriesUpdated();
243 }
243 }
244
244
245 void QBarSeriesPrivate::removeCategory(int index)
245 void QBarSeriesPrivate::removeCategory(int index)
246 {
246 {
247 m_categories.removeAt(index);
247 m_categories.removeAt(index);
248 emit categoriesUpdated();
248 emit categoriesUpdated();
249 }
249 }
250
250
251 int QBarSeriesPrivate::categoryCount() const
251 int QBarSeriesPrivate::categoryCount() const
252 {
252 {
253 if (m_categories.count() > 0) {
253 if (m_categories.count() > 0) {
254 return m_categories.count();
254 return m_categories.count();
255 }
255 }
256
256
257 // No categories defined. return count of longest set.
257 // No categories defined. return count of longest set.
258 int count = 0;
258 int count = 0;
259 for (int i=0; i<m_barSets.count(); i++) {
259 for (int i=0; i<m_barSets.count(); i++) {
260 if (m_barSets.at(i)->count() > count) {
260 if (m_barSets.at(i)->count() > count) {
261 count = m_barSets.at(i)->count();
261 count = m_barSets.at(i)->count();
262 }
262 }
263 }
263 }
264
264
265 return count;
265 return count;
266 }
266 }
267
267
268 QStringList QBarSeriesPrivate::categories() const
268 QStringList QBarSeriesPrivate::categories() const
269 {
269 {
270 if (m_categories.count() > 0) {
270 if (m_categories.count() > 0) {
271 return m_categories;
271 return m_categories;
272 }
272 }
273
273
274 // No categories defined. retun list of indices.
274 // No categories defined. retun list of indices.
275 QStringList categories;
275 QStringList categories;
276
276
277 int count = categoryCount();
277 int count = categoryCount();
278 for (int i = 0; i < count; i++) {
278 for (int i = 0; i < count; i++) {
279 categories.append(QString::number(i));
279 categories.append(QString::number(i));
280 }
280 }
281 return categories;
281 return categories;
282 }
282 }
283
283
284 void QBarSeriesPrivate::setBarMargin(qreal margin)
284 void QBarSeriesPrivate::setBarMargin(qreal margin)
285 {
285 {
286 if (margin > 1.0) {
286 if (margin > 1.0) {
287 margin = 1.0;
287 margin = 1.0;
288 } else if (margin < 0.0) {
288 } else if (margin < 0.0) {
289 margin = 0.0;
289 margin = 0.0;
290 }
290 }
291
291
292 m_barMargin = margin;
292 m_barMargin = margin;
293 emit updatedBars();
293 emit updatedBars();
294 }
294 }
295
295
296 qreal QBarSeriesPrivate::barMargin() const
296 qreal QBarSeriesPrivate::barMargin() const
297 {
297 {
298 return m_barMargin;
298 return m_barMargin;
299 }
299 }
300
300
301 QBarSet* QBarSeriesPrivate::barsetAt(int index)
301 QBarSet* QBarSeriesPrivate::barsetAt(int index)
302 {
302 {
303 return m_barSets.at(index);
303 return m_barSets.at(index);
304 }
304 }
305
305
306 void QBarSeriesPrivate::setVisible(bool visible)
306 void QBarSeriesPrivate::setVisible(bool visible)
307 {
307 {
308 if (m_visible != visible) {
308 if (m_visible != visible) {
309 m_visible = visible;
309 m_visible = visible;
310 emit updatedBars();
310 emit updatedBars();
311 }
311 }
312 }
312 }
313
313
314 bool QBarSeriesPrivate::isVisible() const
314 bool QBarSeriesPrivate::isVisible() const
315 {
315 {
316 return m_visible;
316 return m_visible;
317 }
317 }
318
318
319 QString QBarSeriesPrivate::categoryName(int category)
319 QString QBarSeriesPrivate::categoryName(int category)
320 {
320 {
321 if ((category >= 0) && (category < m_categories.count())) {
321 if ((category >= 0) && (category < m_categories.count())) {
322 return m_categories.at(category);
322 return m_categories.at(category);
323 }
323 }
324
324
325 return QString::number(category);
325 return QString::number(category);
326 }
326 }
327
327
328 qreal QBarSeriesPrivate::min()
328 qreal QBarSeriesPrivate::min()
329 {
329 {
330 if (m_barSets.count() <= 0) {
330 if (m_barSets.count() <= 0) {
331 return 0;
331 return 0;
332 }
332 }
333 qreal min = INT_MAX;
333 qreal min = INT_MAX;
334
334
335 for (int i = 0; i < m_barSets.count(); i++) {
335 for (int i = 0; i < m_barSets.count(); i++) {
336 int categoryCount = m_barSets.at(i)->count();
336 int categoryCount = m_barSets.at(i)->count();
337 for (int j = 0; j < categoryCount; j++) {
337 for (int j = 0; j < categoryCount; j++) {
338 qreal temp = m_barSets.at(i)->at(j).y();
338 qreal temp = m_barSets.at(i)->at(j).y();
339 if (temp < min)
339 if (temp < min)
340 min = temp;
340 min = temp;
341 }
341 }
342 }
342 }
343 return min;
343 return min;
344 }
344 }
345
345
346 qreal QBarSeriesPrivate::max()
346 qreal QBarSeriesPrivate::max()
347 {
347 {
348 if (m_barSets.count() <= 0) {
348 if (m_barSets.count() <= 0) {
349 return 0;
349 return 0;
350 }
350 }
351 qreal max = INT_MIN;
351 qreal max = INT_MIN;
352
352
353 for (int i = 0; i < m_barSets.count(); i++) {
353 for (int i = 0; i < m_barSets.count(); i++) {
354 int categoryCount = m_barSets.at(i)->count();
354 int categoryCount = m_barSets.at(i)->count();
355 for (int j = 0; j < categoryCount; j++) {
355 for (int j = 0; j < categoryCount; j++) {
356 qreal temp = m_barSets.at(i)->at(j).y();
356 qreal temp = m_barSets.at(i)->at(j).y();
357 if (temp > max)
357 if (temp > max)
358 max = temp;
358 max = temp;
359 }
359 }
360 }
360 }
361
361
362 return max;
362 return max;
363 }
363 }
364
364
365 qreal QBarSeriesPrivate::valueAt(int set, int category)
365 qreal QBarSeriesPrivate::valueAt(int set, int category)
366 {
366 {
367 if ((set < 0) || (set >= m_barSets.count())) {
367 if ((set < 0) || (set >= m_barSets.count())) {
368 // No set, no value.
368 // No set, no value.
369 return 0;
369 return 0;
370 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
370 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
371 // No category, no value.
371 // No category, no value.
372 return 0;
372 return 0;
373 }
373 }
374
374
375 return m_barSets.at(set)->at(category).y();
375 return m_barSets.at(set)->at(category).y();
376 }
376 }
377
377
378 qreal QBarSeriesPrivate::percentageAt(int set, int category)
378 qreal QBarSeriesPrivate::percentageAt(int set, int category)
379 {
379 {
380 if ((set < 0) || (set >= m_barSets.count())) {
380 if ((set < 0) || (set >= m_barSets.count())) {
381 // No set, no value.
381 // No set, no value.
382 return 0;
382 return 0;
383 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
383 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
384 // No category, no value.
384 // No category, no value.
385 return 0;
385 return 0;
386 }
386 }
387
387
388 qreal value = m_barSets.at(set)->at(category).y();
388 qreal value = m_barSets.at(set)->at(category).y();
389 qreal sum = categorySum(category);
389 qreal sum = categorySum(category);
390 if ( qFuzzyIsNull(sum) ) {
390 if ( qFuzzyIsNull(sum) ) {
391 return 0;
391 return 0;
392 }
392 }
393
393
394 return value / sum;
394 return value / sum;
395 }
395 }
396
396
397 qreal QBarSeriesPrivate::categorySum(int category)
397 qreal QBarSeriesPrivate::categorySum(int category)
398 {
398 {
399 qreal sum(0);
399 qreal sum(0);
400 int count = m_barSets.count(); // Count sets
400 int count = m_barSets.count(); // Count sets
401 for (int set = 0; set < count; set++) {
401 for (int set = 0; set < count; set++) {
402 if (category < m_barSets.at(set)->count())
402 if (category < m_barSets.at(set)->count())
403 sum += m_barSets.at(set)->at(category).y();
403 sum += m_barSets.at(set)->at(category).y();
404 }
404 }
405 return sum;
405 return sum;
406 }
406 }
407
407
408 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
408 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
409 {
409 {
410 qreal sum(0);
410 qreal sum(0);
411 int count = m_barSets.count(); // Count sets
411 int count = m_barSets.count(); // Count sets
412 for (int set = 0; set < count; set++) {
412 for (int set = 0; set < count; set++) {
413 if (category < m_barSets.at(set)->count())
413 if (category < m_barSets.at(set)->count())
414 sum += qAbs(m_barSets.at(set)->at(category).y());
414 sum += qAbs(m_barSets.at(set)->at(category).y());
415 }
415 }
416 return sum;
416 return sum;
417 }
417 }
418
418
419 qreal QBarSeriesPrivate::maxCategorySum()
419 qreal QBarSeriesPrivate::maxCategorySum()
420 {
420 {
421 qreal max = INT_MIN;
421 qreal max = INT_MIN;
422 int count = categoryCount();
422 int count = categoryCount();
423 for (int i = 0; i < count; i++) {
423 for (int i = 0; i < count; i++) {
424 qreal sum = categorySum(i);
424 qreal sum = categorySum(i);
425 if (sum > max)
425 if (sum > max)
426 max = sum;
426 max = sum;
427 }
427 }
428 return max;
428 return max;
429 }
429 }
430
430
431 void QBarSeriesPrivate::barsetChanged()
432 {
433 emit updatedBars();
434 }
435
436 void QBarSeriesPrivate::scaleDomain(Domain& domain)
431 void QBarSeriesPrivate::scaleDomain(Domain& domain)
437 {
432 {
438 qreal minX(domain.minX());
433 qreal minX(domain.minX());
439 qreal minY(domain.minY());
434 qreal minY(domain.minY());
440 qreal maxX(domain.maxX());
435 qreal maxX(domain.maxX());
441 qreal maxY(domain.maxY());
436 qreal maxY(domain.maxY());
442 int tickXCount(domain.tickXCount());
437 int tickXCount(domain.tickXCount());
443 int tickYCount(domain.tickYCount());
438 int tickYCount(domain.tickYCount());
444
439
445 qreal x = categoryCount();
440 qreal x = categoryCount();
446 qreal y = max();
441 qreal y = max();
447 minX = qMin(minX, x) - 0.5;
442 minX = qMin(minX, x) - 0.5;
448 minY = qMin(minY, y);
443 minY = qMin(minY, y);
449 maxX = qMax(maxX, x) - 0.5;
444 maxX = qMax(maxX, x) - 0.5;
450 maxY = qMax(maxY, y);
445 maxY = qMax(maxY, y);
451 tickXCount = x+1;
446 tickXCount = x+1;
452
447
453 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
448 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
454 }
449 }
455
450
456 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
451 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
457 {
452 {
458 Q_Q(QBarSeries);
453 Q_Q(QBarSeries);
459
454
460 BarChartItem* bar = new BarChartItem(q,presenter);
455 BarChartItem* bar = new BarChartItem(q,presenter);
461 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
456 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
462 presenter->animator()->addAnimation(bar);
457 presenter->animator()->addAnimation(bar);
463 }
458 }
464 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
459 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
465 return bar;
460 return bar;
466
461
467 }
462 }
468
463
469 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
464 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
470 {
465 {
471 Q_Q(QBarSeries);
466 Q_Q(QBarSeries);
472 QList<LegendMarker*> markers;
467 QList<LegendMarker*> markers;
473 foreach(QBarSet* set, q->barSets()) {
468 foreach(QBarSet* set, q->barSets()) {
474 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
469 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
475 markers << marker;
470 markers << marker;
476 }
471 }
477
472
478 return markers;
473 return markers;
479 }
474 }
480
475
481 bool QBarSeriesPrivate::append(QBarSet *set)
476 bool QBarSeriesPrivate::append(QBarSet *set)
482 {
477 {
483 Q_Q(QBarSeries);
478 Q_Q(QBarSeries);
484 if ((m_barSets.contains(set)) || (set == 0)) {
479 if ((m_barSets.contains(set)) || (set == 0)) {
485 // Fail if set is already in list or set is null.
480 // Fail if set is already in list or set is null.
486 return false;
481 return false;
487 }
482 }
488 m_barSets.append(set);
483 m_barSets.append(set);
489 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
484 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
485 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
490 if (m_dataset) {
486 if (m_dataset) {
491 m_dataset->updateSeries(q); // this notifies legend
487 m_dataset->updateSeries(q); // this notifies legend
492 }
488 }
493 emit restructuredBars(); // this notifies barchartitem
489 emit restructuredBars(); // this notifies barchartitem
494 return true;
490 return true;
495 }
491 }
496
492
497 bool QBarSeriesPrivate::remove(QBarSet *set)
493 bool QBarSeriesPrivate::remove(QBarSet *set)
498 {
494 {
499 Q_Q(QBarSeries);
495 Q_Q(QBarSeries);
500 if (!m_barSets.contains(set)) {
496 if (!m_barSets.contains(set)) {
501 // Fail if set is not in list
497 // Fail if set is not in list
502 return false;
498 return false;
503 }
499 }
504 m_barSets.removeOne(set);
500 m_barSets.removeOne(set);
505 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
501 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
502 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
506 if (m_dataset) {
503 if (m_dataset) {
507 m_dataset->updateSeries(q); // this notifies legend
504 m_dataset->updateSeries(q); // this notifies legend
508 }
505 }
509 emit restructuredBars(); // this notifies barchartitem
506 emit restructuredBars(); // this notifies barchartitem
510 return true;
507 return true;
511 }
508 }
512
509
513 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
510 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
514 {
511 {
515 Q_Q(QBarSeries);
512 Q_Q(QBarSeries);
516 foreach (QBarSet* set, sets) {
513 foreach (QBarSet* set, sets) {
517 if ((set == 0) || (m_barSets.contains(set))) {
514 if ((set == 0) || (m_barSets.contains(set))) {
518 // Fail if any of the sets is null or is already appended.
515 // Fail if any of the sets is null or is already appended.
519 return false;
516 return false;
520 }
517 }
521 if (sets.count(set) != 1) {
518 if (sets.count(set) != 1) {
522 // Also fail if same set is more than once in given list.
519 // Also fail if same set is more than once in given list.
523 return false;
520 return false;
524 }
521 }
525 }
522 }
526
523
527 foreach (QBarSet* set, sets) {
524 foreach (QBarSet* set, sets) {
528 m_barSets.append(set);
525 m_barSets.append(set);
529 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
526 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
527 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
530 }
528 }
531 if (m_dataset) {
529 if (m_dataset) {
532 m_dataset->updateSeries(q); // this notifies legend
530 m_dataset->updateSeries(q); // this notifies legend
533 }
531 }
534 emit restructuredBars(); // this notifies barchartitem
532 emit restructuredBars(); // this notifies barchartitem
535 return true;
533 return true;
536 }
534 }
537
535
538 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
536 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
539 {
537 {
540 Q_Q(QBarSeries);
538 Q_Q(QBarSeries);
541 bool setsRemoved = false;
539 bool setsRemoved = false;
542 foreach (QBarSet* set, sets) {
540 foreach (QBarSet* set, sets) {
543 if (m_barSets.contains(set)) {
541 if (m_barSets.contains(set)) {
544 m_barSets.removeOne(set);
542 m_barSets.removeOne(set);
545 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
543 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
544 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
546 setsRemoved = true;
545 setsRemoved = true;
547 }
546 }
548 }
547 }
549
548
550 if (setsRemoved) {
549 if (setsRemoved) {
551 if (m_dataset) {
550 if (m_dataset) {
552 m_dataset->updateSeries(q); // this notifies legend
551 m_dataset->updateSeries(q); // this notifies legend
553 }
552 }
554 emit restructuredBars(); // this notifies barchartitem
553 emit restructuredBars(); // this notifies barchartitem
555 }
554 }
556 return setsRemoved;
555 return setsRemoved;
557 }
556 }
558
557
559 #include "moc_qbarseries.cpp"
558 #include "moc_qbarseries.cpp"
560 #include "moc_qbarseries_p.cpp"
559 #include "moc_qbarseries_p.cpp"
561
560
562 QTCOMMERCIALCHART_END_NAMESPACE
561 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,99
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 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QBARSERIES_P_H
30 #ifndef QBARSERIES_P_H
31 #define QBARSERIES_P_H
31 #define QBARSERIES_P_H
32
32
33 #include "qbarseries.h"
33 #include "qbarseries.h"
34 #include "qabstractseries_p.h"
34 #include "qabstractseries_p.h"
35 #include <QStringList>
35 #include <QStringList>
36 #include <QAbstractSeries>
36 #include <QAbstractSeries>
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 class QBarModelMapper;
40 class QBarModelMapper;
41
41
42 class QBarSeriesPrivate : public QAbstractSeriesPrivate
42 class QBarSeriesPrivate : public QAbstractSeriesPrivate
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 public:
45 public:
46 QBarSeriesPrivate(QBarSeries *parent);
46 QBarSeriesPrivate(QBarSeries *parent);
47 // TODO: refactor/remove private category stuff
47 // TODO: refactor/remove private category stuff
48 void setCategories(QStringList categories);
48 void setCategories(QStringList categories);
49 void insertCategory(int index, const QString category);
49 void insertCategory(int index, const QString category);
50 void removeCategory(int index);
50 void removeCategory(int index);
51 int categoryCount() const;
51 int categoryCount() const;
52 QStringList categories() const;
52 QStringList categories() const;
53
53
54 void setBarMargin(qreal margin);
54 void setBarMargin(qreal margin);
55 qreal barMargin() const;
55 qreal barMargin() const;
56
56
57 void setVisible(bool visible);
57 void setVisible(bool visible);
58 bool isVisible() const;
58 bool isVisible() const;
59
59
60 void scaleDomain(Domain& domain);
60 void scaleDomain(Domain& domain);
61 Chart* createGraphics(ChartPresenter* presenter);
61 Chart* createGraphics(ChartPresenter* presenter);
62 QList<LegendMarker*> createLegendMarker(QLegend* legend);
62 QList<LegendMarker*> createLegendMarker(QLegend* legend);
63
63
64 bool append(QBarSet *set);
64 bool append(QBarSet *set);
65 bool remove(QBarSet *set);
65 bool remove(QBarSet *set);
66 bool append(QList<QBarSet* > sets);
66 bool append(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
68
68
69 QBarSet* barsetAt(int index);
69 QBarSet* barsetAt(int index);
70 QString categoryName(int category);
70 QString categoryName(int category);
71 qreal min();
71 qreal min();
72 qreal max();
72 qreal max();
73 qreal valueAt(int set, int category);
73 qreal valueAt(int set, int category);
74 qreal percentageAt(int set, int category);
74 qreal percentageAt(int set, int category);
75 qreal categorySum(int category);
75 qreal categorySum(int category);
76 qreal absoluteCategorySum(int category);
76 qreal absoluteCategorySum(int category);
77 qreal maxCategorySum();
77 qreal maxCategorySum();
78
78
79 Q_SIGNALS:
79 Q_SIGNALS:
80 void clicked(QBarSet *barset, int index);
80 void clicked(QBarSet *barset, int index);
81 void updatedBars();
81 void updatedBars();
82 void restructuredBars();
82 void restructuredBars();
83 void categoriesUpdated();
83 void categoriesUpdated();
84 void labelsVisibleChanged(bool visible);
84 void labelsVisibleChanged(bool visible);
85
85
86 private Q_SLOTS:
87 void barsetChanged();
88
89 protected:
86 protected:
90 QList<QBarSet *> m_barSets;
87 QList<QBarSet *> m_barSets;
91 QStringList m_categories;
88 QStringList m_categories;
92 qreal m_barMargin;
89 qreal m_barMargin;
93 bool m_labelsVisible;
90 bool m_labelsVisible;
94 bool m_visible;
91 bool m_visible;
95
92
96 private:
93 private:
97 Q_DECLARE_PUBLIC(QBarSeries)
94 Q_DECLARE_PUBLIC(QBarSeries)
98 };
95 };
99
96
100 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
101
98
102 #endif // QBARSERIESPRIVATE_P_H
99 #endif // QBARSERIESPRIVATE_P_H
@@ -1,409 +1,410
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 <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qbarset.h>
22 #include <qbarset.h>
23 #include <qgroupedbarseries.h>
23 #include <qgroupedbarseries.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 class tst_QBarSet : public QObject
28 class tst_QBarSet : public QObject
29 {
29 {
30 Q_OBJECT
30 Q_OBJECT
31
31
32 public slots:
32 public slots:
33 void initTestCase();
33 void initTestCase();
34 void cleanupTestCase();
34 void cleanupTestCase();
35 void init();
35 void init();
36 void cleanup();
36 void cleanup();
37
37
38 private slots:
38 private slots:
39 void qbarset_data();
39 void qbarset_data();
40 void qbarset();
40 void qbarset();
41 void name_data();
41 void name_data();
42 void name();
42 void name();
43 void append_data();
43 void append_data();
44 void append();
44 void append();
45 void appendOperator_data();
45 void appendOperator_data();
46 void appendOperator();
46 void appendOperator();
47 void insert_data();
47 void insert_data();
48 void insert();
48 void insert();
49 void remove_data();
49 void remove_data();
50 void remove();
50 void remove();
51 void replace_data();
51 void replace_data();
52 void replace();
52 void replace();
53 void at_data();
53 void at_data();
54 void at();
54 void at();
55 void atOperator_data();
55 void atOperator_data();
56 void atOperator();
56 void atOperator();
57 void count_data();
57 void count_data();
58 void count();
58 void count();
59 void sum_data();
59 void sum_data();
60 void sum();
60 void sum();
61 void customize();
61 void customize();
62
62
63 private:
63 private:
64 QBarSet* m_barset;
64 QBarSet* m_barset;
65 };
65 };
66
66
67 void tst_QBarSet::initTestCase()
67 void tst_QBarSet::initTestCase()
68 {
68 {
69 }
69 }
70
70
71 void tst_QBarSet::cleanupTestCase()
71 void tst_QBarSet::cleanupTestCase()
72 {
72 {
73 }
73 }
74
74
75 void tst_QBarSet::init()
75 void tst_QBarSet::init()
76 {
76 {
77 m_barset = new QBarSet(QString("Name"));
77 m_barset = new QBarSet(QString("Name"));
78 }
78 }
79
79
80 void tst_QBarSet::cleanup()
80 void tst_QBarSet::cleanup()
81 {
81 {
82 delete m_barset;
82 delete m_barset;
83 m_barset = 0;
83 m_barset = 0;
84 }
84 }
85
85
86 void tst_QBarSet::qbarset_data()
86 void tst_QBarSet::qbarset_data()
87 {
87 {
88 }
88 }
89
89
90 void tst_QBarSet::qbarset()
90 void tst_QBarSet::qbarset()
91 {
91 {
92 QBarSet barset(QString("Name"));
92 QBarSet barset(QString("Name"));
93 QCOMPARE(barset.name(), QString("Name"));
93 QCOMPARE(barset.name(), QString("Name"));
94 QCOMPARE(barset.count(), 0);
94 QCOMPARE(barset.count(), 0);
95 QVERIFY(qFuzzyIsNull(barset.sum()));
95 QVERIFY(qFuzzyIsNull(barset.sum()));
96 }
96 }
97
97
98 void tst_QBarSet::name_data()
98 void tst_QBarSet::name_data()
99 {
99 {
100 QTest::addColumn<QString> ("name");
100 QTest::addColumn<QString> ("name");
101 QTest::addColumn<QString> ("result");
101 QTest::addColumn<QString> ("result");
102 QTest::newRow("name0") << QString("name0") << QString("name0");
102 QTest::newRow("name0") << QString("name0") << QString("name0");
103 QTest::newRow("name1") << QString("name1") << QString("name1");
103 QTest::newRow("name1") << QString("name1") << QString("name1");
104 }
104 }
105
105
106 void tst_QBarSet::name()
106 void tst_QBarSet::name()
107 {
107 {
108 QFETCH(QString, name);
108 QFETCH(QString, name);
109 QFETCH(QString, result);
109 QFETCH(QString, result);
110
110
111 m_barset->setName(name);
111 m_barset->setName(name);
112 QCOMPARE(m_barset->name(), result);
112 QCOMPARE(m_barset->name(), result);
113 }
113 }
114
114
115 void tst_QBarSet::append_data()
115 void tst_QBarSet::append_data()
116 {
116 {
117 QTest::addColumn<int> ("count");
117 QTest::addColumn<int> ("count");
118 QTest::newRow("0") << 0;
118 QTest::newRow("0") << 0;
119 QTest::newRow("5") << 5;
119 QTest::newRow("5") << 5;
120 QTest::newRow("100") << 100;
120 QTest::newRow("100") << 100;
121 QTest::newRow("1000") << 1000;
121 QTest::newRow("1000") << 1000;
122 }
122 }
123
123
124 void tst_QBarSet::append()
124 void tst_QBarSet::append()
125 {
125 {
126 QFETCH(int, count);
126 QFETCH(int, count);
127
127
128 QCOMPARE(m_barset->count(), 0);
128 QCOMPARE(m_barset->count(), 0);
129 QVERIFY(qFuzzyIsNull(m_barset->sum()));
129 QVERIFY(qFuzzyIsNull(m_barset->sum()));
130
130
131 qreal sum(0.0);
131 qreal sum(0.0);
132 qreal value(0.0);
132 qreal value(0.0);
133
133
134 for (int i=0; i<count; i++) {
134 for (int i=0; i<count; i++) {
135 m_barset->append(value);
135 m_barset->append(value);
136 QCOMPARE(m_barset->at(i).y(), value);
136 QCOMPARE(m_barset->at(i).y(), value);
137 sum += value;
137 sum += value;
138 value += 1.0;
138 value += 1.0;
139 }
139 }
140
140
141 QCOMPARE(m_barset->count(), count);
141 QCOMPARE(m_barset->count(), count);
142 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
142 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
143 }
143 }
144
144
145 void tst_QBarSet::appendOperator_data()
145 void tst_QBarSet::appendOperator_data()
146 {
146 {
147 append_data();
147 append_data();
148 }
148 }
149
149
150 void tst_QBarSet::appendOperator()
150 void tst_QBarSet::appendOperator()
151 {
151 {
152 QFETCH(int, count);
152 QFETCH(int, count);
153
153
154 QCOMPARE(m_barset->count(), 0);
154 QCOMPARE(m_barset->count(), 0);
155 QVERIFY(qFuzzyIsNull(m_barset->sum()));
155 QVERIFY(qFuzzyIsNull(m_barset->sum()));
156
156
157 qreal sum(0.0);
157 qreal sum(0.0);
158 qreal value(0.0);
158 qreal value(0.0);
159
159
160 for (int i=0; i<count; i++) {
160 for (int i=0; i<count; i++) {
161 *m_barset << value;
161 *m_barset << value;
162 QCOMPARE(m_barset->at(i).y(), value);
162 QCOMPARE(m_barset->at(i).y(), value);
163 sum += value;
163 sum += value;
164 value += 1.0;
164 value += 1.0;
165 }
165 }
166
166
167 QCOMPARE(m_barset->count(), count);
167 QCOMPARE(m_barset->count(), count);
168 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
168 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
169 }
169 }
170
170
171 void tst_QBarSet::insert_data()
171 void tst_QBarSet::insert_data()
172 {
172 {
173 }
173 }
174
174
175 void tst_QBarSet::insert()
175 void tst_QBarSet::insert()
176 {
176 {
177 QCOMPARE(m_barset->count(), 0);
177 QCOMPARE(m_barset->count(), 0);
178 QVERIFY(qFuzzyIsNull(m_barset->sum()));
178 QVERIFY(qFuzzyIsNull(m_barset->sum()));
179
179
180 m_barset->insert(0, 1.0); // 1.0
180 m_barset->insert(0, 1.0); // 1.0
181 QCOMPARE(m_barset->at(0).y(), 1.0);
181 QCOMPARE(m_barset->at(0).y(), 1.0);
182 QCOMPARE(m_barset->count(), 1);
182 QCOMPARE(m_barset->count(), 1);
183 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
183 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
184
184
185 m_barset->insert(0, 2.0); // 2.0 1.0
185 m_barset->insert(0, 2.0); // 2.0 1.0
186 QCOMPARE(m_barset->at(0).y(), 2.0);
186 QCOMPARE(m_barset->at(0).y(), 2.0);
187 QCOMPARE(m_barset->at(1).y(), 1.0);
187 QCOMPARE(m_barset->at(1).y(), 1.0);
188 QCOMPARE(m_barset->count(), 2);
188 QCOMPARE(m_barset->count(), 2);
189 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
189 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
190
190
191 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
191 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
192 QCOMPARE(m_barset->at(1).y(), 3.0);
192 QCOMPARE(m_barset->at(1).y(), 3.0);
193 QCOMPARE(m_barset->at(0).y(), 2.0);
193 QCOMPARE(m_barset->at(0).y(), 2.0);
194 QCOMPARE(m_barset->at(2).y(), 1.0);
194 QCOMPARE(m_barset->at(2).y(), 1.0);
195 QCOMPARE(m_barset->count(), 3);
195 QCOMPARE(m_barset->count(), 3);
196 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
196 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
197 }
197 }
198
198
199 void tst_QBarSet::remove_data()
199 void tst_QBarSet::remove_data()
200 {
200 {
201 }
201 }
202
202
203 void tst_QBarSet::remove()
203 void tst_QBarSet::remove()
204 {
204 {
205 QCOMPARE(m_barset->count(), 0);
205 QCOMPARE(m_barset->count(), 0);
206 QVERIFY(qFuzzyIsNull(m_barset->sum()));
206 QVERIFY(qFuzzyIsNull(m_barset->sum()));
207
207
208 m_barset->append(1.0);
208 m_barset->append(1.0);
209 m_barset->append(2.0);
209 m_barset->append(2.0);
210 m_barset->append(3.0);
210 m_barset->append(3.0);
211 m_barset->append(4.0);
211 m_barset->append(4.0);
212
212
213 QCOMPARE(m_barset->count(), 4);
213 QCOMPARE(m_barset->count(), 4);
214 QCOMPARE(m_barset->sum(), 10.0);
214 QCOMPARE(m_barset->sum(), 10.0);
215
215
216 m_barset->remove(2); // 1.0 2.0 4.0
216 m_barset->remove(2); // 1.0 2.0 4.0
217 QCOMPARE(m_barset->at(0).y(), 1.0);
217 QCOMPARE(m_barset->at(0).y(), 1.0);
218 QCOMPARE(m_barset->at(1).y(), 2.0);
218 QCOMPARE(m_barset->at(1).y(), 2.0);
219 QCOMPARE(m_barset->at(2).y(), 4.0);
219 QCOMPARE(m_barset->at(2).y(), 4.0);
220 QCOMPARE(m_barset->count(), 3);
220 QCOMPARE(m_barset->count(), 3);
221 QCOMPARE(m_barset->sum(), 7.0);
221 QCOMPARE(m_barset->sum(), 7.0);
222
222
223 m_barset->remove(0); // 2.0 4.0
223 m_barset->remove(0); // 2.0 4.0
224 QCOMPARE(m_barset->at(0).y(), 2.0);
224 QCOMPARE(m_barset->at(0).y(), 2.0);
225 QCOMPARE(m_barset->at(1).y(), 4.0);
225 QCOMPARE(m_barset->at(1).y(), 4.0);
226 QCOMPARE(m_barset->count(), 2);
226 QCOMPARE(m_barset->count(), 2);
227 QCOMPARE(m_barset->sum(), 6.0);
227 QCOMPARE(m_barset->sum(), 6.0);
228 }
228 }
229
229
230 void tst_QBarSet::replace_data()
230 void tst_QBarSet::replace_data()
231 {
231 {
232
232
233 }
233 }
234
234
235 void tst_QBarSet::replace()
235 void tst_QBarSet::replace()
236 {
236 {
237 QCOMPARE(m_barset->count(), 0);
237 QCOMPARE(m_barset->count(), 0);
238 QVERIFY(qFuzzyIsNull(m_barset->sum()));
238 QVERIFY(qFuzzyIsNull(m_barset->sum()));
239
239
240 m_barset->append(1.0);
240 m_barset->append(1.0);
241 m_barset->append(2.0);
241 m_barset->append(2.0);
242 m_barset->append(3.0);
242 m_barset->append(3.0);
243 m_barset->append(4.0);
243 m_barset->append(4.0);
244
244
245 QCOMPARE(m_barset->count(), 4);
245 QCOMPARE(m_barset->count(), 4);
246 QCOMPARE(m_barset->sum(), 10.0);
246 QCOMPARE(m_barset->sum(), 10.0);
247
247
248 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
248 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
249 QCOMPARE(m_barset->count(), 4);
249 QCOMPARE(m_barset->count(), 4);
250 QCOMPARE(m_barset->sum(), 14.0);
250 QCOMPARE(m_barset->sum(), 14.0);
251 QCOMPARE(m_barset->at(0).y(), 5.0);
251 QCOMPARE(m_barset->at(0).y(), 5.0);
252
252
253 m_barset->replace(3, 6.0);
253 m_barset->replace(3, 6.0);
254 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
254 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
255 QCOMPARE(m_barset->sum(), 16.0);
255 QCOMPARE(m_barset->sum(), 16.0);
256 QCOMPARE(m_barset->at(0).y(), 5.0);
256 QCOMPARE(m_barset->at(0).y(), 5.0);
257 QCOMPARE(m_barset->at(1).y(), 2.0);
257 QCOMPARE(m_barset->at(1).y(), 2.0);
258 QCOMPARE(m_barset->at(2).y(), 3.0);
258 QCOMPARE(m_barset->at(2).y(), 3.0);
259 QCOMPARE(m_barset->at(3).y(), 6.0);
259 QCOMPARE(m_barset->at(3).y(), 6.0);
260 }
260 }
261
261
262 void tst_QBarSet::at_data()
262 void tst_QBarSet::at_data()
263 {
263 {
264
264
265 }
265 }
266
266
267 void tst_QBarSet::at()
267 void tst_QBarSet::at()
268 {
268 {
269 QCOMPARE(m_barset->count(), 0);
269 QCOMPARE(m_barset->count(), 0);
270 QVERIFY(qFuzzyIsNull(m_barset->sum()));
270 QVERIFY(qFuzzyIsNull(m_barset->sum()));
271
271
272 m_barset->append(1.0);
272 m_barset->append(1.0);
273 m_barset->append(2.0);
273 m_barset->append(2.0);
274 m_barset->append(3.0);
274 m_barset->append(3.0);
275 m_barset->append(4.0);
275 m_barset->append(4.0);
276
276
277 QCOMPARE(m_barset->at(0).y(), 1.0);
277 QCOMPARE(m_barset->at(0).y(), 1.0);
278 QCOMPARE(m_barset->at(1).y(), 2.0);
278 QCOMPARE(m_barset->at(1).y(), 2.0);
279 QCOMPARE(m_barset->at(2).y(), 3.0);
279 QCOMPARE(m_barset->at(2).y(), 3.0);
280 QCOMPARE(m_barset->at(3).y(), 4.0);
280 QCOMPARE(m_barset->at(3).y(), 4.0);
281 }
281 }
282
282
283 void tst_QBarSet::atOperator_data()
283 void tst_QBarSet::atOperator_data()
284 {
284 {
285
285
286 }
286 }
287
287
288 void tst_QBarSet::atOperator()
288 void tst_QBarSet::atOperator()
289 {
289 {
290 QCOMPARE(m_barset->count(), 0);
290 QCOMPARE(m_barset->count(), 0);
291 QVERIFY(qFuzzyIsNull(m_barset->sum()));
291 QVERIFY(qFuzzyIsNull(m_barset->sum()));
292
292
293 m_barset->append(1.0);
293 m_barset->append(1.0);
294 m_barset->append(2.0);
294 m_barset->append(2.0);
295 m_barset->append(3.0);
295 m_barset->append(3.0);
296 m_barset->append(4.0);
296 m_barset->append(4.0);
297
297
298 QCOMPARE(m_barset->operator [](0).y(), 1.0);
298 QCOMPARE(m_barset->operator [](0).y(), 1.0);
299 QCOMPARE(m_barset->operator [](1).y(), 2.0);
299 QCOMPARE(m_barset->operator [](1).y(), 2.0);
300 QCOMPARE(m_barset->operator [](2).y(), 3.0);
300 QCOMPARE(m_barset->operator [](2).y(), 3.0);
301 QCOMPARE(m_barset->operator [](3).y(), 4.0);
301 QCOMPARE(m_barset->operator [](3).y(), 4.0);
302 }
302 }
303
303
304 void tst_QBarSet::count_data()
304 void tst_QBarSet::count_data()
305 {
305 {
306
306
307 }
307 }
308
308
309 void tst_QBarSet::count()
309 void tst_QBarSet::count()
310 {
310 {
311 QCOMPARE(m_barset->count(), 0);
311 QCOMPARE(m_barset->count(), 0);
312 QVERIFY(qFuzzyIsNull(m_barset->sum()));
312 QVERIFY(qFuzzyIsNull(m_barset->sum()));
313
313
314 m_barset->append(1.0);
314 m_barset->append(1.0);
315 QCOMPARE(m_barset->count(),1);
315 QCOMPARE(m_barset->count(),1);
316 m_barset->append(2.0);
316 m_barset->append(2.0);
317 QCOMPARE(m_barset->count(),2);
317 QCOMPARE(m_barset->count(),2);
318 m_barset->append(3.0);
318 m_barset->append(3.0);
319 QCOMPARE(m_barset->count(),3);
319 QCOMPARE(m_barset->count(),3);
320 m_barset->append(4.0);
320 m_barset->append(4.0);
321 QCOMPARE(m_barset->count(),4);
321 QCOMPARE(m_barset->count(),4);
322 }
322 }
323
323
324 void tst_QBarSet::sum_data()
324 void tst_QBarSet::sum_data()
325 {
325 {
326
326
327 }
327 }
328
328
329 void tst_QBarSet::sum()
329 void tst_QBarSet::sum()
330 {
330 {
331 QCOMPARE(m_barset->count(), 0);
331 QCOMPARE(m_barset->count(), 0);
332 QVERIFY(qFuzzyIsNull(m_barset->sum()));
332 QVERIFY(qFuzzyIsNull(m_barset->sum()));
333
333
334 m_barset->append(1.0);
334 m_barset->append(1.0);
335 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
335 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
336 m_barset->append(2.0);
336 m_barset->append(2.0);
337 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
337 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
338 m_barset->append(3.0);
338 m_barset->append(3.0);
339 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
339 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
340 m_barset->append(4.0);
340 m_barset->append(4.0);
341 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
341 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
342 }
342 }
343
343
344 void tst_QBarSet::customize()
344 void tst_QBarSet::customize()
345 {
345 {
346 // Create sets
346 // Create sets
347 QBarSet *set1 = new QBarSet("set1");
347 QBarSet *set1 = new QBarSet("set1");
348 QBarSet *set2 = new QBarSet("set2");
348 QBarSet *set2 = new QBarSet("set2");
349
349
350 // Append set1 to series
350 // Append set1 to series
351 QGroupedBarSeries *series = new QGroupedBarSeries();
351 QGroupedBarSeries *series = new QGroupedBarSeries();
352 series->append(set1);
352 bool success = series->append(set1);
353 QVERIFY(success);
353
354
354 // Add series to the chart
355 // Add series to the chart
355 QChartView view(new QChart());
356 QChartView view(new QChart());
356 view.resize(200, 200);
357 view.resize(200, 200);
357 view.chart()->addSeries(series);
358 view.chart()->addSeries(series);
358 view.show();
359 view.show();
359 QTest::qWaitForWindowShown(&view);
360 QTest::qWaitForWindowShown(&view);
360
361
361 // Test adding data to the sets
362 // Test adding data to the sets
362 *set1 << 1 << 2 << 1 << 3;
363 *set1 << 1 << 2 << 1 << 3;
363 *set2 << 2 << 1 << 3 << 1;
364 *set2 << 2 << 1 << 3 << 1;
364
365
365 // Test pen
366 // Test pen
366 QVERIFY(set1->pen() != QPen());
367 QVERIFY(set1->pen() != QPen());
367 QVERIFY(set2->pen() == QPen());
368 QVERIFY(set2->pen() == QPen());
368 QPen pen(QColor(128,128,128,128));
369 QPen pen(QColor(128,128,128,128));
369 set1->setPen(pen);
370 set1->setPen(pen);
370 QVERIFY(set1->pen() == pen);
371 QVERIFY(set1->pen() == pen);
371 QVERIFY(set2->pen() == QPen());
372 QVERIFY(set2->pen() == QPen());
372
373
373 // Test brush
374 // Test brush
374 QVERIFY(set1->brush() != QBrush());
375 QVERIFY(set1->brush() != QBrush());
375 QVERIFY(set2->brush() == QBrush());
376 QVERIFY(set2->brush() == QBrush());
376 QBrush brush(QColor(128,128,128,128));
377 QBrush brush(QColor(128,128,128,128));
377 set1->setBrush(brush);
378 set1->setBrush(brush);
378 QVERIFY(set1->brush() == brush);
379 QVERIFY(set1->brush() == brush);
379 QVERIFY(set2->brush() == QBrush());
380 QVERIFY(set2->brush() == QBrush());
380
381
381 // Test label brush
382 // Test label brush
382 QVERIFY(set1->labelBrush() != QBrush());
383 QVERIFY(set1->labelBrush() != QBrush());
383 QVERIFY(set2->labelBrush() == QBrush());
384 QVERIFY(set2->labelBrush() == QBrush());
384 set1->setLabelBrush(brush);
385 set1->setLabelBrush(brush);
385 QVERIFY(set1->labelBrush() == brush);
386 QVERIFY(set1->labelBrush() == brush);
386 QVERIFY(set2->labelBrush() == QBrush());
387 QVERIFY(set2->labelBrush() == QBrush());
387
388
388 // Test label font
389 // Test label font
389 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
390 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
390 // same for the set added to the series (depending on the QChart's theme configuration)
391 // same for the set added to the series (depending on the QChart's theme configuration)
391 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
392 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
392 QVERIFY(set2->labelFont() == QFont());
393 QVERIFY(set2->labelFont() == QFont());
393 QFont font;
394 QFont font;
394 font.setBold(true);
395 font.setBold(true);
395 font.setItalic(true);
396 font.setItalic(true);
396 set1->setLabelFont(font);
397 set1->setLabelFont(font);
397 QVERIFY(set1->labelFont() == font);
398 QVERIFY(set1->labelFont() == font);
398 QVERIFY(set2->labelFont() == QFont());
399 QVERIFY(set2->labelFont() == QFont());
399
400
400 // Test adding data to the sets
401 // Test adding data to the sets
401 *set1 << 1 << 2 << 1 << 3;
402 *set1 << 1 << 2 << 1 << 3;
402 *set2 << 2 << 1 << 3 << 1;
403 *set2 << 2 << 1 << 3 << 1;
403 QTest::qWait(3000);
404 QTest::qWait(3000);
404 }
405 }
405
406
406 QTEST_MAIN(tst_QBarSet)
407 QTEST_MAIN(tst_QBarSet)
407
408
408 #include "tst_qbarset.moc"
409 #include "tst_qbarset.moc"
409
410
General Comments 0
You need to be logged in to leave comments. Login now