##// END OF EJS Templates
barmargin replaced with barwidth
sauimone -
r1425:a0d2e2cf1ab3
parent child
Show More
@@ -1,80 +1,80
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 <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QBarSeries>
24 #include <QBarSeries>
25 #include <QBarSet>
25 #include <QBarSet>
26 #include <QLegend>
26 #include <QLegend>
27
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
29
30 int main(int argc, char *argv[])
30 int main(int argc, char *argv[])
31 {
31 {
32 QApplication a(argc, argv);
32 QApplication a(argc, argv);
33
33
34 //![1]
34 //![1]
35 QBarSet *set0 = new QBarSet("Jane");
35 QBarSet *set0 = new QBarSet("Jane");
36 QBarSet *set1 = new QBarSet("John");
36 QBarSet *set1 = new QBarSet("John");
37 QBarSet *set2 = new QBarSet("Axel");
37 QBarSet *set2 = new QBarSet("Axel");
38 QBarSet *set3 = new QBarSet("Mary");
38 QBarSet *set3 = new QBarSet("Mary");
39
39
40 *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.4, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6);
40 *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.4, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6);
41 *set1 << QPointF(0.1, 2) << QPointF(1.2, 3) << QPointF(2.45, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7);
41 *set1 << QPointF(0.1, 2) << QPointF(1.2, 3) << QPointF(2.45, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7);
42 *set2 << QPointF(0.2, 3) << QPointF(1.4, 4) << QPointF(2.50, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8);
42 *set2 << QPointF(0.2, 3) << QPointF(1.4, 4) << QPointF(2.50, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8);
43 *set3 << QPointF(0.3, 4) << QPointF(1.6, 5) << QPointF(2.55, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9);
43 *set3 << QPointF(0.3, 4) << QPointF(1.6, 5) << QPointF(2.55, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9);
44 //![1]
44 //![1]
45
45
46 //![2]
46 //![2]
47 QBarSeries* series = new QBarSeries();
47 QBarSeries* series = new QBarSeries();
48 series->setBarMargin(0.8);
48 series->setBarWidth(0.2);
49 series->append(set0);
49 series->append(set0);
50 series->append(set1);
50 series->append(set1);
51 series->append(set2);
51 series->append(set2);
52 series->append(set3);
52 series->append(set3);
53 //![2]
53 //![2]
54
54
55 //![3]
55 //![3]
56 QChart* chart = new QChart();
56 QChart* chart = new QChart();
57 chart->addSeries(series);
57 chart->addSeries(series);
58 chart->setTitle("Barchart example");
58 chart->setTitle("Barchart example");
59 //![3]
59 //![3]
60
60
61 //![4]
61 //![4]
62 chart->legend()->setVisible(true);
62 chart->legend()->setVisible(true);
63 chart->legend()->setAlignment(Qt::AlignBottom);
63 chart->legend()->setAlignment(Qt::AlignBottom);
64 chart->axisY()->setNiceNumbersEnabled(true);
64 chart->axisY()->setNiceNumbersEnabled(true);
65 //![4]
65 //![4]
66
66
67 //![5]
67 //![5]
68 QChartView* chartView = new QChartView(chart);
68 QChartView* chartView = new QChartView(chart);
69 chartView->setRenderHint(QPainter::Antialiasing);
69 chartView->setRenderHint(QPainter::Antialiasing);
70 //![5]
70 //![5]
71
71
72 //![6]
72 //![6]
73 QMainWindow window;
73 QMainWindow window;
74 window.setCentralWidget(chartView);
74 window.setCentralWidget(chartView);
75 window.resize(400, 300);
75 window.resize(400, 300);
76 window.show();
76 window.show();
77 //![6]
77 //![6]
78
78
79 return a.exec();
79 return a.exec();
80 }
80 }
@@ -1,213 +1,213
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(handleLabelsVisibleChanged(bool)));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
44 setZValue(ChartPresenter::BarSeriesZValue);
44 setZValue(ChartPresenter::BarSeriesZValue);
45 handleDataStructureChanged();
45 handleDataStructureChanged();
46 }
46 }
47
47
48 BarChartItem::~BarChartItem()
48 BarChartItem::~BarChartItem()
49 {
49 {
50 }
50 }
51
51
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 {
53 {
54 Q_UNUSED(painter);
54 Q_UNUSED(painter);
55 Q_UNUSED(option);
55 Q_UNUSED(option);
56 Q_UNUSED(widget);
56 Q_UNUSED(widget);
57 }
57 }
58
58
59 QRectF BarChartItem::boundingRect() const
59 QRectF BarChartItem::boundingRect() const
60 {
60 {
61 return m_rect;
61 return m_rect;
62 }
62 }
63
63
64 void BarChartItem::handleDataStructureChanged()
64 void BarChartItem::handleDataStructureChanged()
65 {
65 {
66 foreach(QGraphicsItem *item, childItems()) {
66 foreach(QGraphicsItem *item, childItems()) {
67 delete item;
67 delete item;
68 }
68 }
69
69
70 m_bars.clear();
70 m_bars.clear();
71 m_labels.clear();
71 m_labels.clear();
72 m_layout.clear();
72 m_layout.clear();
73
73
74 bool labelsVisible = m_series->isLabelsVisible();
74 bool labelsVisible = m_series->isLabelsVisible();
75
75
76 // Create new graphic items for bars
76 // Create new graphic items for bars
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
79 QBarSet *set = m_series->d_func()->barsetAt(s);
79 QBarSet *set = m_series->d_func()->barsetAt(s);
80
80
81 // Bars
81 // Bars
82 Bar *bar = new Bar(set,c,this);
82 Bar *bar = new Bar(set,c,this);
83 m_bars.append(bar);
83 m_bars.append(bar);
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 m_layout.append(QRectF(0, 0, 0, 0));
86 m_layout.append(QRectF(0, 0, 0, 0));
87
87
88 // Labels
88 // Labels
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 label->setVisible(labelsVisible);
90 label->setVisible(labelsVisible);
91 m_labels.append(label);
91 m_labels.append(label);
92 }
92 }
93 }
93 }
94
94
95 // TODO: Is this the right place to call it?
95 // TODO: Is this the right place to call it?
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 handleLayoutChanged();
97 handleLayoutChanged();
98 }
98 }
99
99
100 QVector<QRectF> BarChartItem::calculateLayout()
100 QVector<QRectF> BarChartItem::calculateLayout()
101 {
101 {
102 QVector<QRectF> layout;
102 QVector<QRectF> layout;
103
103
104 // Use temporary qreals for accuracy
104 // Use temporary qreals for accuracy
105 qreal categoryCount = m_series->d_func()->categoryCount();
105 qreal categoryCount = m_series->d_func()->categoryCount();
106 qreal setCount = m_series->barsetCount();
106 qreal setCount = m_series->barsetCount();
107 bool barsVisible = m_series->isVisible();
107 bool barsVisible = m_series->isVisible();
108
108
109 // Domain:
109 // Domain:
110 qreal width = geometry().width();
110 qreal width = geometry().width();
111 qreal height = geometry().height();
111 qreal height = geometry().height();
112 qreal rangeY = m_domainMaxY - m_domainMinY;
112 qreal rangeY = m_domainMaxY - m_domainMinY;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
114 qreal scaleY = (height / rangeY);
114 qreal scaleY = (height / rangeY);
115 qreal scaleX = (width / rangeX);
115 qreal scaleX = (width / rangeX);
116 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
116 qreal barWidth = scaleX * m_series->d_func()->barWidth();
117
117
118 int itemIndex(0);
118 int itemIndex(0);
119 for (int category = 0; category < categoryCount; category++) {
119 for (int category = 0; category < categoryCount; category++) {
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 for (int set = 0; set < setCount; set++) {
121 for (int set = 0; set < setCount; set++) {
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 qreal barHeight = barSet->at(category).y() * scaleY;
124 qreal barHeight = barSet->at(category).y() * scaleY;
125
125
126 Bar* bar = m_bars.at(itemIndex);
126 Bar* bar = m_bars.at(itemIndex);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128
128
129 layout.append(rect);
129 layout.append(rect);
130 bar->setPen(barSet->pen());
130 bar->setPen(barSet->pen());
131 bar->setBrush(barSet->brush());
131 bar->setBrush(barSet->brush());
132 bar->setVisible(barsVisible);
132 bar->setVisible(barsVisible);
133
133
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
135
135
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
137 label->setText(QString::number(barSet->at(category).y()));
137 label->setText(QString::number(barSet->at(category).y()));
138 } else {
138 } else {
139 label->setText(QString(""));
139 label->setText(QString(""));
140 }
140 }
141
141
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 label->setFont(barSet->labelFont());
144 label->setFont(barSet->labelFont());
145 label->setBrush(barSet->labelBrush());
145 label->setBrush(barSet->labelBrush());
146
146
147 itemIndex++;
147 itemIndex++;
148 }
148 }
149 }
149 }
150
150
151 return layout;
151 return layout;
152 }
152 }
153
153
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 {
155 {
156 if (animator()) {
156 if (animator()) {
157 animator()->updateLayout(this, m_layout, layout);
157 animator()->updateLayout(this, m_layout, layout);
158 } else {
158 } else {
159 setLayout(layout);
159 setLayout(layout);
160 update();
160 update();
161 }
161 }
162 }
162 }
163
163
164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
165 {
165 {
166 if (layout.count() != m_bars.count())
166 if (layout.count() != m_bars.count())
167 return;
167 return;
168
168
169 m_layout = layout;
169 m_layout = layout;
170
170
171 for (int i=0; i < m_bars.count(); i++) {
171 for (int i=0; i < m_bars.count(); i++) {
172 m_bars.at(i)->setRect(layout.at(i));
172 m_bars.at(i)->setRect(layout.at(i));
173 }
173 }
174 }
174 }
175 //handlers
175 //handlers
176
176
177 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
177 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
178 {
178 {
179 m_domainMinX = minX;
179 m_domainMinX = minX;
180 m_domainMaxX = maxX;
180 m_domainMaxX = maxX;
181 m_domainMinY = minY;
181 m_domainMinY = minY;
182 m_domainMaxY = maxY;
182 m_domainMaxY = maxY;
183 handleLayoutChanged();
183 handleLayoutChanged();
184 }
184 }
185
185
186 void BarChartItem::handleGeometryChanged(const QRectF &rect)
186 void BarChartItem::handleGeometryChanged(const QRectF &rect)
187 {
187 {
188 prepareGeometryChange();
188 prepareGeometryChange();
189 m_rect = rect;
189 m_rect = rect;
190 handleLayoutChanged();
190 handleLayoutChanged();
191 }
191 }
192
192
193 void BarChartItem::handleLayoutChanged()
193 void BarChartItem::handleLayoutChanged()
194 {
194 {
195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
196 // rect size zero.
196 // rect size zero.
197 return;
197 return;
198 }
198 }
199 QVector<QRectF> layout = calculateLayout();
199 QVector<QRectF> layout = calculateLayout();
200 applyLayout(layout);
200 applyLayout(layout);
201 }
201 }
202
202
203 void BarChartItem::handleLabelsVisibleChanged(bool visible)
203 void BarChartItem::handleLabelsVisibleChanged(bool visible)
204 {
204 {
205 foreach (QGraphicsSimpleTextItem* label, m_labels) {
205 foreach (QGraphicsSimpleTextItem* label, m_labels) {
206 label->setVisible(visible);
206 label->setVisible(visible);
207 }
207 }
208 update();
208 update();
209 }
209 }
210
210
211 #include "moc_barchartitem_p.cpp"
211 #include "moc_barchartitem_p.cpp"
212
212
213 QTCOMMERCIALCHART_END_NAMESPACE
213 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,91 +1,91
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 "groupedbarchartitem_p.h"
21 #include "groupedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal barWidth = scaleX / setCount - (scaleX / setCount) * m_series->d_func()->barMargin();
50 qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
57
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
59 xPos -= setCount*barWidth/2;
59 xPos -= setCount*barWidth/2;
60 xPos += set*barWidth;
60 xPos += set*barWidth;
61 qreal barHeight = barSet->at(category).y() * scaleY;
61 qreal barHeight = barSet->at(category).y() * scaleY;
62 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
63
63
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 layout.append(rect);
65 layout.append(rect);
66 bar->setPen(barSet->pen());
66 bar->setPen(barSet->pen());
67 bar->setBrush(barSet->brush());
67 bar->setBrush(barSet->brush());
68 bar->setVisible(barsVisible);
68 bar->setVisible(barsVisible);
69
69
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71
71
72 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 if (!qFuzzyIsNull(barSet->at(category).y())) {
73 label->setText(QString::number(barSet->at(category).y()));
73 label->setText(QString::number(barSet->at(category).y()));
74 } else {
74 } else {
75 label->setText(QString(""));
75 label->setText(QString(""));
76 }
76 }
77
77
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
80 label->setFont(barSet->labelFont());
80 label->setFont(barSet->labelFont());
81 label->setBrush(barSet->labelBrush());
81 label->setBrush(barSet->labelBrush());
82
82
83 itemIndex++;
83 itemIndex++;
84 }
84 }
85 }
85 }
86 return layout;
86 return layout;
87 }
87 }
88
88
89 #include "moc_groupedbarchartitem_p.cpp"
89 #include "moc_groupedbarchartitem_p.cpp"
90
90
91 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,97 +1,97
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 "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarseries_p.h"
23 #include "qbarseries_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
55 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
57 for (int set=0; set < setCount; set++) {
57 for (int set=0; set < setCount; set++) {
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59
59
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
61
61
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
64 bar->setPen(barSet->pen());
64 bar->setPen(barSet->pen());
65 bar->setBrush(barSet->brush());
65 bar->setBrush(barSet->brush());
66 bar->setVisible(barsVisible);
66 bar->setVisible(barsVisible);
67
67
68 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
68 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
69 layout.append(rect);
69 layout.append(rect);
70
70
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
72
72
73 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
73 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
74 int p = m_series->d_func()->percentageAt(set,category) * 100;
74 int p = m_series->d_func()->percentageAt(set,category) * 100;
75 QString vString(QString::number(p));
75 QString vString(QString::number(p));
76 vString.truncate(3);
76 vString.truncate(3);
77 vString.append("%");
77 vString.append("%");
78 label->setText(vString);
78 label->setText(vString);
79 } else {
79 } else {
80 label->setText(QString(""));
80 label->setText(QString(""));
81 }
81 }
82
82
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
85 label->setFont(barSet->labelFont());
85 label->setFont(barSet->labelFont());
86 label->setBrush(barSet->labelBrush());
86 label->setBrush(barSet->labelBrush());
87
87
88 itemIndex++;
88 itemIndex++;
89 yPos -= barHeight;
89 yPos -= barHeight;
90 }
90 }
91 }
91 }
92 return layout;
92 return layout;
93 }
93 }
94
94
95 #include "moc_percentbarchartitem_p.cpp"
95 #include "moc_percentbarchartitem_p.cpp"
96
96
97 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,679 +1,684
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 \property QBarSeries::barMargin
50 \property QBarSeries::barWidth
51 \brief Defines the margin around bars.
51 \brief Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
52 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
53 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
54 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
55 because with grouped series it is more logical to set width of whole group and let the chart calculate correct
56 width for bar.
57 \sa QGroupedBarSeries
52 */
58 */
53
59
54 /*!
60 /*!
55 \property QBarSeries::count
61 \property QBarSeries::count
56 \brief Holds the number of sets in series.
62 \brief Holds the number of sets in series.
57 */
63 */
58
64
59 /*!
65 /*!
60 \property QBarSeries::labelsVisible
66 \property QBarSeries::labelsVisible
61 \brief Defines the visibility of the labels in series
67 \brief Defines the visibility of the labels in series
62 */
68 */
63
69
64 /*!
70 /*!
65 \fn void QBarSeries::clicked(QBarSet *barset, int index)
71 \fn void QBarSeries::clicked(QBarSet *barset, int index)
66
72
67 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
73 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
68 Clicked bar inside set is indexed by \a index
74 Clicked bar inside set is indexed by \a index
69 */
75 */
70
76
71 /*!
77 /*!
72 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
78 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
73
79
74 The signal is emitted if mouse is hovered on top of series.
80 The signal is emitted if mouse is hovered on top of series.
75 Parameter \a barset is the pointer of barset, where hover happened.
81 Parameter \a barset is the pointer of barset, where hover happened.
76 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
82 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
77 */
83 */
78
84
79 /*!
85 /*!
80 \fn void QBarSeries::labelsVisibleChanged()
86 \fn void QBarSeries::labelsVisibleChanged()
81
87
82 This signal is emitted when labels visibility have changed.
88 This signal is emitted when labels visibility have changed.
83
89
84 \sa isLabelsVisible(), setLabelsVisible()
90 \sa isLabelsVisible(), setLabelsVisible()
85 */
91 */
86
92
87 /*!
93 /*!
88 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
94 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
89
95
90 This signal is emitted when \a sets have been added to the series.
96 This signal is emitted when \a sets have been added to the series.
91
97
92 \sa append(), insert()
98 \sa append(), insert()
93 */
99 */
94
100
95 /*!
101 /*!
96 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
102 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
97
103
98 This signal is emitted when \a sets have been removed from the series.
104 This signal is emitted when \a sets have been removed from the series.
99
105
100 \sa remove()
106 \sa remove()
101 */
107 */
102
108
103 /*!
109 /*!
104 Constructs empty QBarSeries.
110 Constructs empty QBarSeries.
105 QBarSeries is QObject which is a child of a \a parent.
111 QBarSeries is QObject which is a child of a \a parent.
106 */
112 */
107 QBarSeries::QBarSeries(QObject *parent) :
113 QBarSeries::QBarSeries(QObject *parent) :
108 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
114 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
109 {
115 {
110 }
116 }
111
117
112 /*!
118 /*!
113 Destructs barseries and owned barsets.
119 Destructs barseries and owned barsets.
114 */
120 */
115 QBarSeries::~QBarSeries()
121 QBarSeries::~QBarSeries()
116 {
122 {
117 Q_D(QBarSeries);
123 Q_D(QBarSeries);
118 if(d->m_dataset){
124 if(d->m_dataset){
119 d->m_dataset->removeSeries(this);
125 d->m_dataset->removeSeries(this);
120 }
126 }
121 }
127 }
122
128
123 /*!
129 /*!
124 \internal
130 \internal
125 */
131 */
126 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
132 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
127 QAbstractSeries(d,parent)
133 QAbstractSeries(d,parent)
128 {
134 {
129 }
135 }
130
136
131 /*!
137 /*!
132 Returns the type of series. Derived classes override this.
138 Returns the type of series. Derived classes override this.
133 */
139 */
134 QAbstractSeries::SeriesType QBarSeries::type() const
140 QAbstractSeries::SeriesType QBarSeries::type() const
135 {
141 {
136 return QAbstractSeries::SeriesTypeBar;
142 return QAbstractSeries::SeriesTypeBar;
137 }
143 }
138
144
139 /*!
145 /*!
140 Sets the margin of the bars of the series. The unit of \a margin is the unit of x-axis. Setting the margin to 0.0
146 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
141 means there is no margin around the bars, making a single bar or bargroup to take one x-axis unit on the screen.
147 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
142 Setting margin to maximum value of 1.0 makes the bar width to exactly 1 pixel on the screen. Bars cannot be zero
148 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
143 width, otherwise they would not be visible at all. If you want to hide bars, use visible property of the series
149 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
144 instead.
150 because with grouped series it is more logical to set widht of whole group and let the chart calculate correct
151 width for bar.
152 \sa QGroupedBarSeries
145 */
153 */
146 void QBarSeries::setBarMargin(qreal margin)
154 void QBarSeries::setBarWidth(qreal width)
147 {
155 {
148 Q_D(QBarSeries);
156 Q_D(QBarSeries);
149 d->setBarMargin(margin);
157 d->setBarWidth(width);
150 }
158 }
151
159
152 /*!
160 /*!
153 Returns the margin around bars
161 Returns the width of bars.
154 */
162 */
155 qreal QBarSeries::barMargin() const
163 qreal QBarSeries::barWidth() const
156 {
164 {
157 Q_D(const QBarSeries);
165 Q_D(const QBarSeries);
158 return d->barMargin();
166 return d->barWidth();
159 }
167 }
160
168
161 /*!
169 /*!
162 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.
170 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.
163 Returns true, if appending succeeded.
171 Returns true, if appending succeeded.
164
172
165 */
173 */
166 bool QBarSeries::append(QBarSet *set)
174 bool QBarSeries::append(QBarSet *set)
167 {
175 {
168 Q_D(QBarSeries);
176 Q_D(QBarSeries);
169 bool success = d->append(set);
177 bool success = d->append(set);
170 if (success) {
178 if (success) {
171 QList<QBarSet*> sets;
179 QList<QBarSet*> sets;
172 sets.append(set);
180 sets.append(set);
173 emit barsetsAdded(sets);
181 emit barsetsAdded(sets);
174 }
182 }
175 return success;
183 return success;
176 }
184 }
177
185
178 /*!
186 /*!
179 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
187 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
180 Returns true, if set was removed.
188 Returns true, if set was removed.
181 */
189 */
182 bool QBarSeries::remove(QBarSet *set)
190 bool QBarSeries::remove(QBarSet *set)
183 {
191 {
184 Q_D(QBarSeries);
192 Q_D(QBarSeries);
185 bool success = d->remove(set);
193 bool success = d->remove(set);
186 if (success) {
194 if (success) {
187 QList<QBarSet*> sets;
195 QList<QBarSet*> sets;
188 sets.append(set);
196 sets.append(set);
189 emit barsetsRemoved(sets);
197 emit barsetsRemoved(sets);
190 }
198 }
191 return success;
199 return success;
192 }
200 }
193
201
194 /*!
202 /*!
195 Adds a list of barsets to series. Takes ownership of \a sets.
203 Adds a list of barsets to series. Takes ownership of \a sets.
196 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
204 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
197 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
205 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
198 and function returns false.
206 and function returns false.
199 */
207 */
200 bool QBarSeries::append(QList<QBarSet* > sets)
208 bool QBarSeries::append(QList<QBarSet* > sets)
201 {
209 {
202 Q_D(QBarSeries);
210 Q_D(QBarSeries);
203 bool success = d->append(sets);
211 bool success = d->append(sets);
204 if (success) {
212 if (success) {
205 emit barsetsAdded(sets);
213 emit barsetsAdded(sets);
206 }
214 }
207 return success;
215 return success;
208 }
216 }
209
217
210 /*!
218 /*!
211 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
219 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
212 Returns true, if inserting succeeded.
220 Returns true, if inserting succeeded.
213
221
214 */
222 */
215 bool QBarSeries::insert(int index, QBarSet *set)
223 bool QBarSeries::insert(int index, QBarSet *set)
216 {
224 {
217 Q_D(QBarSeries);
225 Q_D(QBarSeries);
218 bool success = d->insert(index, set);
226 bool success = d->insert(index, set);
219 if (success) {
227 if (success) {
220 QList<QBarSet*> sets;
228 QList<QBarSet*> sets;
221 sets.append(set);
229 sets.append(set);
222 emit barsetsAdded(sets);
230 emit barsetsAdded(sets);
223 }
231 }
224 return success;
232 return success;
225 }
233 }
226
234
227 /*!
235 /*!
228 Removes all of the bar sets from the series
236 Removes all of the bar sets from the series
229 */
237 */
230 void QBarSeries::clear()
238 void QBarSeries::clear()
231 {
239 {
232 Q_D(QBarSeries);
240 Q_D(QBarSeries);
233 QList<QBarSet *> sets = barSets();
241 QList<QBarSet *> sets = barSets();
234 bool success = d->remove(sets);
242 bool success = d->remove(sets);
235 if (success) {
243 if (success) {
236 emit barsetsRemoved(sets);
244 emit barsetsRemoved(sets);
237 }
245 }
238 }
246 }
239
247
240 /*!
248 /*!
241 Returns number of sets in series.
249 Returns number of sets in series.
242 */
250 */
243 int QBarSeries::barsetCount() const
251 int QBarSeries::barsetCount() const
244 {
252 {
245 Q_D(const QBarSeries);
253 Q_D(const QBarSeries);
246 return d->m_barSets.count();
254 return d->m_barSets.count();
247 }
255 }
248
256
249 /*!
257 /*!
250 Returns a list of sets in series. Keeps ownership of sets.
258 Returns a list of sets in series. Keeps ownership of sets.
251 */
259 */
252 QList<QBarSet*> QBarSeries::barSets() const
260 QList<QBarSet*> QBarSeries::barSets() const
253 {
261 {
254 Q_D(const QBarSeries);
262 Q_D(const QBarSeries);
255 return d->m_barSets;
263 return d->m_barSets;
256 }
264 }
257
265
258 /*!
266 /*!
259 Sets the visibility of labels in series to \a visible
267 Sets the visibility of labels in series to \a visible
260 */
268 */
261 void QBarSeries::setLabelsVisible(bool visible)
269 void QBarSeries::setLabelsVisible(bool visible)
262 {
270 {
263 Q_D(QBarSeries);
271 Q_D(QBarSeries);
264 if (d->m_labelsVisible != visible) {
272 if (d->m_labelsVisible != visible) {
265 d->setLabelsVisible(visible);
273 d->setLabelsVisible(visible);
266 emit labelsVisibleChanged();
274 emit labelsVisibleChanged();
267 }
275 }
268 }
276 }
269
277
270 /*!
278 /*!
271 Returns the visibility of labels
279 Returns the visibility of labels
272 */
280 */
273 bool QBarSeries::isLabelsVisible() const
281 bool QBarSeries::isLabelsVisible() const
274 {
282 {
275 Q_D(const QBarSeries);
283 Q_D(const QBarSeries);
276 return d->m_labelsVisible;
284 return d->m_labelsVisible;
277 }
285 }
278
286
279 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
287 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
280
288
281 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
289 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
282 QAbstractSeriesPrivate(q),
290 QAbstractSeriesPrivate(q),
283 m_barMargin(0.5), // Default value is 50% of category width
291 m_barWidth(0.5), // Default value is 50% of category width
284 m_labelsVisible(false),
292 m_labelsVisible(false),
285 m_visible(true)
293 m_visible(true)
286 {
294 {
287 }
295 }
288
296
289 void QBarSeriesPrivate::setCategories(QStringList categories)
297 void QBarSeriesPrivate::setCategories(QStringList categories)
290 {
298 {
291 m_categories = categories;
299 m_categories = categories;
292 }
300 }
293
301
294 void QBarSeriesPrivate::insertCategory(int index, const QString category)
302 void QBarSeriesPrivate::insertCategory(int index, const QString category)
295 {
303 {
296 m_categories.insert(index, category);
304 m_categories.insert(index, category);
297 emit categoriesUpdated();
305 emit categoriesUpdated();
298 }
306 }
299
307
300 void QBarSeriesPrivate::removeCategory(int index)
308 void QBarSeriesPrivate::removeCategory(int index)
301 {
309 {
302 m_categories.removeAt(index);
310 m_categories.removeAt(index);
303 emit categoriesUpdated();
311 emit categoriesUpdated();
304 }
312 }
305
313
306 int QBarSeriesPrivate::categoryCount() const
314 int QBarSeriesPrivate::categoryCount() const
307 {
315 {
308 if (m_categories.count() > 0) {
316 if (m_categories.count() > 0) {
309 return m_categories.count();
317 return m_categories.count();
310 }
318 }
311
319
312 // No categories defined. return count of longest set.
320 // No categories defined. return count of longest set.
313 int count = 0;
321 int count = 0;
314 for (int i=0; i<m_barSets.count(); i++) {
322 for (int i=0; i<m_barSets.count(); i++) {
315 if (m_barSets.at(i)->count() > count) {
323 if (m_barSets.at(i)->count() > count) {
316 count = m_barSets.at(i)->count();
324 count = m_barSets.at(i)->count();
317 }
325 }
318 }
326 }
319
327
320 return count;
328 return count;
321 }
329 }
322
330
323 QStringList QBarSeriesPrivate::categories() const
331 QStringList QBarSeriesPrivate::categories() const
324 {
332 {
325 if (m_categories.count() > 0) {
333 if (m_categories.count() > 0) {
326 return m_categories;
334 return m_categories;
327 }
335 }
328
336
329 // No categories defined. retun list of indices.
337 // No categories defined. retun list of indices.
330 QStringList categories;
338 QStringList categories;
331
339
332 int count = categoryCount();
340 int count = categoryCount();
333 for (int i = 0; i < count; i++) {
341 for (int i = 0; i < count; i++) {
334 categories.append(QString::number(i));
342 categories.append(QString::number(i));
335 }
343 }
336 return categories;
344 return categories;
337 }
345 }
338
346
339 void QBarSeriesPrivate::setBarMargin(qreal margin)
347 void QBarSeriesPrivate::setBarWidth(qreal width)
340 {
348 {
341 if (margin > 1.0) {
349 if (width < 0.0) {
342 margin = 1.0;
350 width = 0.0;
343 } else if (margin < 0.0) {
344 margin = 0.0;
345 }
351 }
346
352 m_barWidth = width;
347 m_barMargin = margin;
348 emit updatedBars();
353 emit updatedBars();
349 }
354 }
350
355
351 qreal QBarSeriesPrivate::barMargin() const
356 qreal QBarSeriesPrivate::barWidth() const
352 {
357 {
353 return m_barMargin;
358 return m_barWidth;
354 }
359 }
355
360
356 QBarSet* QBarSeriesPrivate::barsetAt(int index)
361 QBarSet* QBarSeriesPrivate::barsetAt(int index)
357 {
362 {
358 return m_barSets.at(index);
363 return m_barSets.at(index);
359 }
364 }
360
365
361 void QBarSeriesPrivate::setVisible(bool visible)
366 void QBarSeriesPrivate::setVisible(bool visible)
362 {
367 {
363 m_visible = visible;
368 m_visible = visible;
364 emit updatedBars();
369 emit updatedBars();
365 }
370 }
366
371
367 void QBarSeriesPrivate::setLabelsVisible(bool visible)
372 void QBarSeriesPrivate::setLabelsVisible(bool visible)
368 {
373 {
369 m_labelsVisible = visible;
374 m_labelsVisible = visible;
370 emit labelsVisibleChanged(visible);
375 emit labelsVisibleChanged(visible);
371 }
376 }
372
377
373 QString QBarSeriesPrivate::categoryName(int category)
378 QString QBarSeriesPrivate::categoryName(int category)
374 {
379 {
375 if ((category >= 0) && (category < m_categories.count())) {
380 if ((category >= 0) && (category < m_categories.count())) {
376 return m_categories.at(category);
381 return m_categories.at(category);
377 }
382 }
378
383
379 return QString::number(category);
384 return QString::number(category);
380 }
385 }
381
386
382 qreal QBarSeriesPrivate::min()
387 qreal QBarSeriesPrivate::min()
383 {
388 {
384 if (m_barSets.count() <= 0) {
389 if (m_barSets.count() <= 0) {
385 return 0;
390 return 0;
386 }
391 }
387 qreal min = INT_MAX;
392 qreal min = INT_MAX;
388
393
389 for (int i = 0; i < m_barSets.count(); i++) {
394 for (int i = 0; i < m_barSets.count(); i++) {
390 int categoryCount = m_barSets.at(i)->count();
395 int categoryCount = m_barSets.at(i)->count();
391 for (int j = 0; j < categoryCount; j++) {
396 for (int j = 0; j < categoryCount; j++) {
392 qreal temp = m_barSets.at(i)->at(j).y();
397 qreal temp = m_barSets.at(i)->at(j).y();
393 if (temp < min)
398 if (temp < min)
394 min = temp;
399 min = temp;
395 }
400 }
396 }
401 }
397 return min;
402 return min;
398 }
403 }
399
404
400 qreal QBarSeriesPrivate::max()
405 qreal QBarSeriesPrivate::max()
401 {
406 {
402 if (m_barSets.count() <= 0) {
407 if (m_barSets.count() <= 0) {
403 return 0;
408 return 0;
404 }
409 }
405 qreal max = INT_MIN;
410 qreal max = INT_MIN;
406
411
407 for (int i = 0; i < m_barSets.count(); i++) {
412 for (int i = 0; i < m_barSets.count(); i++) {
408 int categoryCount = m_barSets.at(i)->count();
413 int categoryCount = m_barSets.at(i)->count();
409 for (int j = 0; j < categoryCount; j++) {
414 for (int j = 0; j < categoryCount; j++) {
410 qreal temp = m_barSets.at(i)->at(j).y();
415 qreal temp = m_barSets.at(i)->at(j).y();
411 if (temp > max)
416 if (temp > max)
412 max = temp;
417 max = temp;
413 }
418 }
414 }
419 }
415
420
416 return max;
421 return max;
417 }
422 }
418
423
419 qreal QBarSeriesPrivate::valueAt(int set, int category)
424 qreal QBarSeriesPrivate::valueAt(int set, int category)
420 {
425 {
421 if ((set < 0) || (set >= m_barSets.count())) {
426 if ((set < 0) || (set >= m_barSets.count())) {
422 // No set, no value.
427 // No set, no value.
423 return 0;
428 return 0;
424 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
429 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
425 // No category, no value.
430 // No category, no value.
426 return 0;
431 return 0;
427 }
432 }
428
433
429 return m_barSets.at(set)->at(category).y();
434 return m_barSets.at(set)->at(category).y();
430 }
435 }
431
436
432 qreal QBarSeriesPrivate::percentageAt(int set, int category)
437 qreal QBarSeriesPrivate::percentageAt(int set, int category)
433 {
438 {
434 if ((set < 0) || (set >= m_barSets.count())) {
439 if ((set < 0) || (set >= m_barSets.count())) {
435 // No set, no value.
440 // No set, no value.
436 return 0;
441 return 0;
437 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
442 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
438 // No category, no value.
443 // No category, no value.
439 return 0;
444 return 0;
440 }
445 }
441
446
442 qreal value = m_barSets.at(set)->at(category).y();
447 qreal value = m_barSets.at(set)->at(category).y();
443 qreal sum = categorySum(category);
448 qreal sum = categorySum(category);
444 if ( qFuzzyIsNull(sum) ) {
449 if ( qFuzzyIsNull(sum) ) {
445 return 0;
450 return 0;
446 }
451 }
447
452
448 return value / sum;
453 return value / sum;
449 }
454 }
450
455
451 qreal QBarSeriesPrivate::categorySum(int category)
456 qreal QBarSeriesPrivate::categorySum(int category)
452 {
457 {
453 qreal sum(0);
458 qreal sum(0);
454 int count = m_barSets.count(); // Count sets
459 int count = m_barSets.count(); // Count sets
455 for (int set = 0; set < count; set++) {
460 for (int set = 0; set < count; set++) {
456 if (category < m_barSets.at(set)->count())
461 if (category < m_barSets.at(set)->count())
457 sum += m_barSets.at(set)->at(category).y();
462 sum += m_barSets.at(set)->at(category).y();
458 }
463 }
459 return sum;
464 return sum;
460 }
465 }
461
466
462 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
467 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
463 {
468 {
464 qreal sum(0);
469 qreal sum(0);
465 int count = m_barSets.count(); // Count sets
470 int count = m_barSets.count(); // Count sets
466 for (int set = 0; set < count; set++) {
471 for (int set = 0; set < count; set++) {
467 if (category < m_barSets.at(set)->count())
472 if (category < m_barSets.at(set)->count())
468 sum += qAbs(m_barSets.at(set)->at(category).y());
473 sum += qAbs(m_barSets.at(set)->at(category).y());
469 }
474 }
470 return sum;
475 return sum;
471 }
476 }
472
477
473 qreal QBarSeriesPrivate::maxCategorySum()
478 qreal QBarSeriesPrivate::maxCategorySum()
474 {
479 {
475 qreal max = INT_MIN;
480 qreal max = INT_MIN;
476 int count = categoryCount();
481 int count = categoryCount();
477 for (int i = 0; i < count; i++) {
482 for (int i = 0; i < count; i++) {
478 qreal sum = categorySum(i);
483 qreal sum = categorySum(i);
479 if (sum > max)
484 if (sum > max)
480 max = sum;
485 max = sum;
481 }
486 }
482 return max;
487 return max;
483 }
488 }
484
489
485 qreal QBarSeriesPrivate::minX()
490 qreal QBarSeriesPrivate::minX()
486 {
491 {
487 if (m_barSets.count() <= 0) {
492 if (m_barSets.count() <= 0) {
488 return 0;
493 return 0;
489 }
494 }
490 qreal min = INT_MAX;
495 qreal min = INT_MAX;
491
496
492 for (int i = 0; i < m_barSets.count(); i++) {
497 for (int i = 0; i < m_barSets.count(); i++) {
493 int categoryCount = m_barSets.at(i)->count();
498 int categoryCount = m_barSets.at(i)->count();
494 for (int j = 0; j < categoryCount; j++) {
499 for (int j = 0; j < categoryCount; j++) {
495 qreal temp = m_barSets.at(i)->at(j).x();
500 qreal temp = m_barSets.at(i)->at(j).x();
496 if (temp < min)
501 if (temp < min)
497 min = temp;
502 min = temp;
498 }
503 }
499 }
504 }
500 return min;
505 return min;
501 }
506 }
502
507
503 qreal QBarSeriesPrivate::maxX()
508 qreal QBarSeriesPrivate::maxX()
504 {
509 {
505 if (m_barSets.count() <= 0) {
510 if (m_barSets.count() <= 0) {
506 return 0;
511 return 0;
507 }
512 }
508 qreal max = INT_MIN;
513 qreal max = INT_MIN;
509
514
510 for (int i = 0; i < m_barSets.count(); i++) {
515 for (int i = 0; i < m_barSets.count(); i++) {
511 int categoryCount = m_barSets.at(i)->count();
516 int categoryCount = m_barSets.at(i)->count();
512 for (int j = 0; j < categoryCount; j++) {
517 for (int j = 0; j < categoryCount; j++) {
513 qreal temp = m_barSets.at(i)->at(j).x();
518 qreal temp = m_barSets.at(i)->at(j).x();
514 if (temp > max)
519 if (temp > max)
515 max = temp;
520 max = temp;
516 }
521 }
517 }
522 }
518
523
519 return max;
524 return max;
520 }
525 }
521
526
522
527
523 void QBarSeriesPrivate::scaleDomain(Domain& domain)
528 void QBarSeriesPrivate::scaleDomain(Domain& domain)
524 {
529 {
525 qreal minX(domain.minX());
530 qreal minX(domain.minX());
526 qreal minY(domain.minY());
531 qreal minY(domain.minY());
527 qreal maxX(domain.maxX());
532 qreal maxX(domain.maxX());
528 qreal maxY(domain.maxY());
533 qreal maxY(domain.maxY());
529 int tickXCount(domain.tickXCount());
534 int tickXCount(domain.tickXCount());
530 int tickYCount(domain.tickYCount());
535 int tickYCount(domain.tickYCount());
531
536
532 qreal seriesMinX = this->minX();
537 qreal seriesMinX = this->minX();
533 qreal seriesMaxX = this->maxX();
538 qreal seriesMaxX = this->maxX();
534 qreal y = max();
539 qreal y = max();
535 minX = qMin(minX, seriesMinX - 0.5);
540 minX = qMin(minX, seriesMinX - 0.5);
536 minY = qMin(minY, y);
541 minY = qMin(minY, y);
537 maxX = qMax(maxX, seriesMaxX + 0.5);
542 maxX = qMax(maxX, seriesMaxX + 0.5);
538 maxY = qMax(maxY, y);
543 maxY = qMax(maxY, y);
539 tickXCount = categoryCount()+1;
544 tickXCount = categoryCount()+1;
540
545
541 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
546 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
542 }
547 }
543
548
544 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
549 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
545 {
550 {
546 Q_Q(QBarSeries);
551 Q_Q(QBarSeries);
547
552
548 BarChartItem* bar = new BarChartItem(q,presenter);
553 BarChartItem* bar = new BarChartItem(q,presenter);
549 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
554 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
550 presenter->animator()->addAnimation(bar);
555 presenter->animator()->addAnimation(bar);
551 }
556 }
552 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
557 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
553 return bar;
558 return bar;
554
559
555 }
560 }
556
561
557 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
562 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
558 {
563 {
559 Q_Q(QBarSeries);
564 Q_Q(QBarSeries);
560 QList<LegendMarker*> markers;
565 QList<LegendMarker*> markers;
561 foreach(QBarSet* set, q->barSets()) {
566 foreach(QBarSet* set, q->barSets()) {
562 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
567 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
563 markers << marker;
568 markers << marker;
564 }
569 }
565
570
566 return markers;
571 return markers;
567 }
572 }
568
573
569 bool QBarSeriesPrivate::append(QBarSet *set)
574 bool QBarSeriesPrivate::append(QBarSet *set)
570 {
575 {
571 Q_Q(QBarSeries);
576 Q_Q(QBarSeries);
572 if ((m_barSets.contains(set)) || (set == 0)) {
577 if ((m_barSets.contains(set)) || (set == 0)) {
573 // Fail if set is already in list or set is null.
578 // Fail if set is already in list or set is null.
574 return false;
579 return false;
575 }
580 }
576 m_barSets.append(set);
581 m_barSets.append(set);
577 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
582 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
578 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
583 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
579 if (m_dataset) {
584 if (m_dataset) {
580 m_dataset->updateSeries(q); // this notifies legend
585 m_dataset->updateSeries(q); // this notifies legend
581 }
586 }
582 emit restructuredBars(); // this notifies barchartitem
587 emit restructuredBars(); // this notifies barchartitem
583 return true;
588 return true;
584 }
589 }
585
590
586 bool QBarSeriesPrivate::remove(QBarSet *set)
591 bool QBarSeriesPrivate::remove(QBarSet *set)
587 {
592 {
588 Q_Q(QBarSeries);
593 Q_Q(QBarSeries);
589 if (!m_barSets.contains(set)) {
594 if (!m_barSets.contains(set)) {
590 // Fail if set is not in list
595 // Fail if set is not in list
591 return false;
596 return false;
592 }
597 }
593 m_barSets.removeOne(set);
598 m_barSets.removeOne(set);
594 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
599 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
595 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
600 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
596 if (m_dataset) {
601 if (m_dataset) {
597 m_dataset->updateSeries(q); // this notifies legend
602 m_dataset->updateSeries(q); // this notifies legend
598 }
603 }
599 emit restructuredBars(); // this notifies barchartitem
604 emit restructuredBars(); // this notifies barchartitem
600 return true;
605 return true;
601 }
606 }
602
607
603 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
608 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
604 {
609 {
605 Q_Q(QBarSeries);
610 Q_Q(QBarSeries);
606 foreach (QBarSet* set, sets) {
611 foreach (QBarSet* set, sets) {
607 if ((set == 0) || (m_barSets.contains(set))) {
612 if ((set == 0) || (m_barSets.contains(set))) {
608 // Fail if any of the sets is null or is already appended.
613 // Fail if any of the sets is null or is already appended.
609 return false;
614 return false;
610 }
615 }
611 if (sets.count(set) != 1) {
616 if (sets.count(set) != 1) {
612 // Also fail if same set is more than once in given list.
617 // Also fail if same set is more than once in given list.
613 return false;
618 return false;
614 }
619 }
615 }
620 }
616
621
617 foreach (QBarSet* set, sets) {
622 foreach (QBarSet* set, sets) {
618 m_barSets.append(set);
623 m_barSets.append(set);
619 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
624 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
620 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
625 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
621 }
626 }
622 if (m_dataset) {
627 if (m_dataset) {
623 m_dataset->updateSeries(q); // this notifies legend
628 m_dataset->updateSeries(q); // this notifies legend
624 }
629 }
625 emit restructuredBars(); // this notifies barchartitem
630 emit restructuredBars(); // this notifies barchartitem
626 return true;
631 return true;
627 }
632 }
628
633
629 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
634 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
630 {
635 {
631 Q_Q(QBarSeries);
636 Q_Q(QBarSeries);
632 if (sets.count() == 0) {
637 if (sets.count() == 0) {
633 return false;
638 return false;
634 }
639 }
635 foreach (QBarSet* set, sets) {
640 foreach (QBarSet* set, sets) {
636 if ((set == 0) || (!m_barSets.contains(set))) {
641 if ((set == 0) || (!m_barSets.contains(set))) {
637 // Fail if any of the sets is null or is not in series
642 // Fail if any of the sets is null or is not in series
638 return false;
643 return false;
639 }
644 }
640 if (sets.count(set) != 1) {
645 if (sets.count(set) != 1) {
641 // Also fail if same set is more than once in given list.
646 // Also fail if same set is more than once in given list.
642 return false;
647 return false;
643 }
648 }
644 }
649 }
645
650
646 foreach (QBarSet* set, sets) {
651 foreach (QBarSet* set, sets) {
647 m_barSets.removeOne(set);
652 m_barSets.removeOne(set);
648 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
653 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
649 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
654 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
650 }
655 }
651
656
652 if (m_dataset) {
657 if (m_dataset) {
653 m_dataset->updateSeries(q); // this notifies legend
658 m_dataset->updateSeries(q); // this notifies legend
654 }
659 }
655 emit restructuredBars(); // this notifies barchartitem
660 emit restructuredBars(); // this notifies barchartitem
656 return true;
661 return true;
657 }
662 }
658
663
659 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
664 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
660 {
665 {
661 Q_Q(QBarSeries);
666 Q_Q(QBarSeries);
662 if ((m_barSets.contains(set)) || (set == 0)) {
667 if ((m_barSets.contains(set)) || (set == 0)) {
663 // Fail if set is already in list or set is null.
668 // Fail if set is already in list or set is null.
664 return false;
669 return false;
665 }
670 }
666 m_barSets.insert(index, set);
671 m_barSets.insert(index, set);
667 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
672 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
668 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
673 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
669 if (m_dataset) {
674 if (m_dataset) {
670 m_dataset->updateSeries(q); // this notifies legend
675 m_dataset->updateSeries(q); // this notifies legend
671 }
676 }
672 emit restructuredBars(); // this notifies barchartitem
677 emit restructuredBars(); // this notifies barchartitem
673 return true;
678 return true;
674 }
679 }
675
680
676 #include "moc_qbarseries.cpp"
681 #include "moc_qbarseries.cpp"
677 #include "moc_qbarseries_p.cpp"
682 #include "moc_qbarseries_p.cpp"
678
683
679 QTCOMMERCIALCHART_END_NAMESPACE
684 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,81
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 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class QBarSet;
29 class QBarSet;
30 class QBarSeriesPrivate;
30 class QBarSeriesPrivate;
31
31
32 // Container for series
32 // Container for series
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(qreal barMargin READ barMargin WRITE setBarMargin)
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
37 Q_PROPERTY(int count READ barsetCount)
37 Q_PROPERTY(int count READ barsetCount)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible)
39
39
40 public:
40 public:
41 explicit QBarSeries(QObject *parent = 0);
41 explicit QBarSeries(QObject *parent = 0);
42 virtual ~QBarSeries();
42 virtual ~QBarSeries();
43
43
44 QAbstractSeries::SeriesType type() const;
44 QAbstractSeries::SeriesType type() const;
45
45
46 void setBarMargin(qreal margin);
46 void setBarWidth(qreal width);
47 qreal barMargin() const;
47 qreal barWidth() const;
48
48
49 bool append(QBarSet *set);
49 bool append(QBarSet *set);
50 bool remove(QBarSet *set);
50 bool remove(QBarSet *set);
51 bool append(QList<QBarSet* > sets);
51 bool append(QList<QBarSet* > sets);
52 bool insert(int index, QBarSet *set);
52 bool insert(int index, QBarSet *set);
53 int barsetCount() const;
53 int barsetCount() const;
54 QList<QBarSet*> barSets() const;
54 QList<QBarSet*> barSets() const;
55 void clear();
55 void clear();
56
56
57 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
58 bool isLabelsVisible() const;
58 bool isLabelsVisible() const;
59
59
60 protected:
60 protected:
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
62
62
63 Q_SIGNALS:
63 Q_SIGNALS:
64 void clicked(QBarSet *barset, int index);
64 void clicked(QBarSet *barset, int index);
65 void hovered(QBarSet* barset, bool status);
65 void hovered(QBarSet* barset, bool status);
66 void labelsVisibleChanged();
66 void labelsVisibleChanged();
67
67
68 void barsetsAdded(QList<QBarSet*> sets);
68 void barsetsAdded(QList<QBarSet*> sets);
69 void barsetsRemoved(QList<QBarSet*> sets);
69 void barsetsRemoved(QList<QBarSet*> sets);
70
70
71 protected:
71 protected:
72 Q_DECLARE_PRIVATE(QBarSeries)
72 Q_DECLARE_PRIVATE(QBarSeries)
73 friend class BarChartItem;
73 friend class BarChartItem;
74 friend class PercentBarChartItem;
74 friend class PercentBarChartItem;
75 friend class StackedBarChartItem;
75 friend class StackedBarChartItem;
76 friend class GroupedBarChartItem;
76 friend class GroupedBarChartItem;
77 };
77 };
78
78
79 QTCOMMERCIALCHART_END_NAMESPACE
79 QTCOMMERCIALCHART_END_NAMESPACE
80
80
81 #endif // BARSERIES_H
81 #endif // BARSERIES_H
@@ -1,102 +1,102
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 setBarWidth(qreal width);
55 qreal barMargin() const;
55 qreal barWidth() const;
56
56
57 void setVisible(bool visible);
57 void setVisible(bool visible);
58 void setLabelsVisible(bool visible);
58 void setLabelsVisible(bool visible);
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 bool insert(int index, QBarSet *set);
68 bool insert(int index, QBarSet *set);
69
69
70 QBarSet* barsetAt(int index);
70 QBarSet* barsetAt(int index);
71 QString categoryName(int category);
71 QString categoryName(int category);
72 qreal min();
72 qreal min();
73 qreal max();
73 qreal max();
74 qreal valueAt(int set, int category);
74 qreal valueAt(int set, int category);
75 qreal percentageAt(int set, int category);
75 qreal percentageAt(int set, int category);
76 qreal categorySum(int category);
76 qreal categorySum(int category);
77 qreal absoluteCategorySum(int category);
77 qreal absoluteCategorySum(int category);
78 qreal maxCategorySum();
78 qreal maxCategorySum();
79 qreal minX();
79 qreal minX();
80 qreal maxX();
80 qreal maxX();
81
81
82 Q_SIGNALS:
82 Q_SIGNALS:
83 void clicked(QBarSet *barset, int index);
83 void clicked(QBarSet *barset, int index);
84 void updatedBars();
84 void updatedBars();
85 void restructuredBars();
85 void restructuredBars();
86 void categoriesUpdated();
86 void categoriesUpdated();
87 void labelsVisibleChanged(bool visible);
87 void labelsVisibleChanged(bool visible);
88
88
89 protected:
89 protected:
90 QList<QBarSet *> m_barSets;
90 QList<QBarSet *> m_barSets;
91 QStringList m_categories;
91 QStringList m_categories;
92 qreal m_barMargin;
92 qreal m_barWidth;
93 bool m_labelsVisible;
93 bool m_labelsVisible;
94 bool m_visible;
94 bool m_visible;
95
95
96 private:
96 private:
97 Q_DECLARE_PUBLIC(QBarSeries)
97 Q_DECLARE_PUBLIC(QBarSeries)
98 };
98 };
99
99
100 QTCOMMERCIALCHART_END_NAMESPACE
100 QTCOMMERCIALCHART_END_NAMESPACE
101
101
102 #endif // QBARSERIESPRIVATE_P_H
102 #endif // QBARSERIESPRIVATE_P_H
@@ -1,90 +1,90
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 "stackedbarchartitem_p.h"
21 #include "stackedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> StackedBarChartItem::calculateLayout()
34 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37 // Use temporary qreals for accuracy
37 // Use temporary qreals for accuracy
38 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal setCount = m_series->barsetCount();
39 qreal setCount = m_series->barsetCount();
40 bool barsVisible = m_series->isVisible();
40 bool barsVisible = m_series->isVisible();
41
41
42 // Domain:
42 // Domain:
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal height = geometry().height();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
48 qreal scaleX = (width / rangeX);
49 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
49 qreal barWidth = scaleX * m_series->d_func()->barWidth();
50
50
51 int itemIndex(0);
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
52 for (int category = 0; category < categoryCount; category++) {
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
54 for (int set=0; set < setCount; set++) {
54 for (int set=0; set < setCount; set++) {
55 QBarSet* barSet = m_series->d_func()->barsetAt(set);
55 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56
56
57 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
57 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
58
58
59 qreal barHeight = barSet->at(category).y() * scaleY;
59 qreal barHeight = barSet->at(category).y() * scaleY;
60 Bar* bar = m_bars.at(itemIndex);
60 Bar* bar = m_bars.at(itemIndex);
61 bar->setPen(barSet->pen());
61 bar->setPen(barSet->pen());
62 bar->setBrush(barSet->brush());
62 bar->setBrush(barSet->brush());
63 bar->setVisible(barsVisible);
63 bar->setVisible(barsVisible);
64
64
65 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
65 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 layout.append(rect);
66 layout.append(rect);
67
67
68 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
68 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69
69
70 if (!qFuzzyIsNull(barSet->at(category).y())) {
70 if (!qFuzzyIsNull(barSet->at(category).y())) {
71 label->setText(QString::number(barSet->at(category).y()));
71 label->setText(QString::number(barSet->at(category).y()));
72 } else {
72 } else {
73 label->setText(QString(""));
73 label->setText(QString(""));
74 }
74 }
75
75
76 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
76 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 ,yPos - barHeight/2 - label->boundingRect().height()/2);
77 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 label->setFont(barSet->labelFont());
78 label->setFont(barSet->labelFont());
79 label->setBrush(barSet->labelBrush());
79 label->setBrush(barSet->labelBrush());
80 itemIndex++;
80 itemIndex++;
81 yPos -= barHeight;
81 yPos -= barHeight;
82 }
82 }
83 }
83 }
84
84
85 return layout;
85 return layout;
86 }
86 }
87
87
88 #include "moc_stackedbarchartitem_p.cpp"
88 #include "moc_stackedbarchartitem_p.cpp"
89
89
90 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,515 +1,508
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 <qbarseries.h>
22 #include <qbarseries.h>
23 #include <qbarset.h>
23 #include <qbarset.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25 #include <qchart.h>
25 #include <qchart.h>
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QBarSet*)
30
30
31 class tst_QBarSeries : public QObject
31 class tst_QBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qbarseries_data();
42 void qbarseries_data();
43 void qbarseries();
43 void qbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void append_data();
46 void append_data();
47 void append();
47 void append();
48 void remove_data();
48 void remove_data();
49 void remove();
49 void remove();
50 void appendList_data();
50 void appendList_data();
51 void appendList();
51 void appendList();
52 void barsetCount_data();
52 void barsetCount_data();
53 void barsetCount();
53 void barsetCount();
54 void barSets_data();
54 void barSets_data();
55 void barSets();
55 void barSets();
56 void setLabelsVisible_data();
56 void setLabelsVisible_data();
57 void setLabelsVisible();
57 void setLabelsVisible();
58 void mouseclicked_data();
58 void mouseclicked_data();
59 void mouseclicked();
59 void mouseclicked();
60 void mousehovered_data();
60 void mousehovered_data();
61 void mousehovered();
61 void mousehovered();
62 void clearWithAnimations();
62 void clearWithAnimations();
63
63
64 private:
64 private:
65 QBarSeries* m_barseries;
65 QBarSeries* m_barseries;
66 QBarSeries* m_barseries_with_sets;
66 QBarSeries* m_barseries_with_sets;
67
67
68 QList<QBarSet*> m_testSets;
68 QList<QBarSet*> m_testSets;
69
69
70 };
70 };
71
71
72 void tst_QBarSeries::initTestCase()
72 void tst_QBarSeries::initTestCase()
73 {
73 {
74 qRegisterMetaType<QBarSet*>("QBarSet*");
74 qRegisterMetaType<QBarSet*>("QBarSet*");
75 }
75 }
76
76
77 void tst_QBarSeries::cleanupTestCase()
77 void tst_QBarSeries::cleanupTestCase()
78 {
78 {
79 }
79 }
80
80
81 void tst_QBarSeries::init()
81 void tst_QBarSeries::init()
82 {
82 {
83 m_barseries = new QBarSeries();
83 m_barseries = new QBarSeries();
84 m_barseries_with_sets = new QBarSeries();
84 m_barseries_with_sets = new QBarSeries();
85
85
86 for (int i=0; i<5; i++) {
86 for (int i=0; i<5; i++) {
87 m_testSets.append(new QBarSet("testset"));
87 m_testSets.append(new QBarSet("testset"));
88 m_barseries_with_sets->append(m_testSets.at(i));
88 m_barseries_with_sets->append(m_testSets.at(i));
89 }
89 }
90 }
90 }
91
91
92 void tst_QBarSeries::cleanup()
92 void tst_QBarSeries::cleanup()
93 {
93 {
94 foreach(QBarSet* s, m_testSets) {
94 foreach(QBarSet* s, m_testSets) {
95 m_barseries_with_sets->remove(s);
95 m_barseries_with_sets->remove(s);
96 delete s;
96 delete s;
97 }
97 }
98 m_testSets.clear();
98 m_testSets.clear();
99
99
100 delete m_barseries;
100 delete m_barseries;
101 m_barseries = 0;
101 m_barseries = 0;
102 delete m_barseries_with_sets;
102 delete m_barseries_with_sets;
103 m_barseries_with_sets = 0;
103 m_barseries_with_sets = 0;
104 }
104 }
105
105
106 void tst_QBarSeries::qbarseries_data()
106 void tst_QBarSeries::qbarseries_data()
107 {
107 {
108 }
108 }
109
109
110 void tst_QBarSeries::qbarseries()
110 void tst_QBarSeries::qbarseries()
111 {
111 {
112 QBarSeries *barseries = new QBarSeries();
112 QBarSeries *barseries = new QBarSeries();
113 QVERIFY(barseries != 0);
113 QVERIFY(barseries != 0);
114 }
114 }
115
115
116 void tst_QBarSeries::type_data()
116 void tst_QBarSeries::type_data()
117 {
117 {
118
118
119 }
119 }
120
120
121 void tst_QBarSeries::type()
121 void tst_QBarSeries::type()
122 {
122 {
123 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
123 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
124 }
124 }
125
125
126 void tst_QBarSeries::append_data()
126 void tst_QBarSeries::append_data()
127 {
127 {
128 }
128 }
129
129
130 void tst_QBarSeries::append()
130 void tst_QBarSeries::append()
131 {
131 {
132 QVERIFY(m_barseries->barsetCount() == 0);
132 QVERIFY(m_barseries->barsetCount() == 0);
133
133
134 bool ret = false;
134 bool ret = false;
135
135
136 // Try adding barset
136 // Try adding barset
137 QBarSet *barset = new QBarSet("testset");
137 QBarSet *barset = new QBarSet("testset");
138 ret = m_barseries->append(barset);
138 ret = m_barseries->append(barset);
139
139
140 QVERIFY(ret == true);
140 QVERIFY(ret == true);
141 QVERIFY(m_barseries->barsetCount() == 1);
141 QVERIFY(m_barseries->barsetCount() == 1);
142
142
143 // Try adding another set
143 // Try adding another set
144 QBarSet *barset2 = new QBarSet("testset2");
144 QBarSet *barset2 = new QBarSet("testset2");
145 ret = m_barseries->append(barset2);
145 ret = m_barseries->append(barset2);
146
146
147 QVERIFY(ret == true);
147 QVERIFY(ret == true);
148 QVERIFY(m_barseries->barsetCount() == 2);
148 QVERIFY(m_barseries->barsetCount() == 2);
149
149
150 // Try adding same set again
150 // Try adding same set again
151 ret = m_barseries->append(barset2);
151 ret = m_barseries->append(barset2);
152 QVERIFY(ret == false);
152 QVERIFY(ret == false);
153 QVERIFY(m_barseries->barsetCount() == 2);
153 QVERIFY(m_barseries->barsetCount() == 2);
154
154
155 // Try adding null set
155 // Try adding null set
156 ret = m_barseries->append(0);
156 ret = m_barseries->append(0);
157 QVERIFY(ret == false);
157 QVERIFY(ret == false);
158 QVERIFY(m_barseries->barsetCount() == 2);
158 QVERIFY(m_barseries->barsetCount() == 2);
159
159
160 }
160 }
161
161
162 void tst_QBarSeries::remove_data()
162 void tst_QBarSeries::remove_data()
163 {
163 {
164 }
164 }
165
165
166 void tst_QBarSeries::remove()
166 void tst_QBarSeries::remove()
167 {
167 {
168 int count = m_testSets.count();
168 int count = m_testSets.count();
169 QVERIFY(m_barseries_with_sets->barsetCount() == count);
169 QVERIFY(m_barseries_with_sets->barsetCount() == count);
170
170
171 // Try to remove null pointer (should not remove, should not crash)
171 // Try to remove null pointer (should not remove, should not crash)
172 bool ret = false;
172 bool ret = false;
173 ret = m_barseries_with_sets->remove(0);
173 ret = m_barseries_with_sets->remove(0);
174 QVERIFY(ret == false);
174 QVERIFY(ret == false);
175 QVERIFY(m_barseries_with_sets->barsetCount() == count);
175 QVERIFY(m_barseries_with_sets->barsetCount() == count);
176
176
177 // Try to remove invalid pointer (should not remove, should not crash)
177 // Try to remove invalid pointer (should not remove, should not crash)
178 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
178 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
179 QVERIFY(ret == false);
179 QVERIFY(ret == false);
180 QVERIFY(m_barseries_with_sets->barsetCount() == count);
180 QVERIFY(m_barseries_with_sets->barsetCount() == count);
181
181
182 // remove some sets
182 // remove some sets
183 ret = m_barseries_with_sets->remove(m_testSets.at(2));
183 ret = m_barseries_with_sets->remove(m_testSets.at(2));
184 QVERIFY(ret == true);
184 QVERIFY(ret == true);
185 ret = m_barseries_with_sets->remove(m_testSets.at(3));
185 ret = m_barseries_with_sets->remove(m_testSets.at(3));
186 QVERIFY(ret == true);
186 QVERIFY(ret == true);
187 ret = m_barseries_with_sets->remove(m_testSets.at(4));
187 ret = m_barseries_with_sets->remove(m_testSets.at(4));
188 QVERIFY(ret == true);
188 QVERIFY(ret == true);
189
189
190 QVERIFY(m_barseries_with_sets->barsetCount() == 2);
190 QVERIFY(m_barseries_with_sets->barsetCount() == 2);
191
191
192 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
192 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
193
193
194 QVERIFY(verifysets.at(0) == m_testSets.at(0));
194 QVERIFY(verifysets.at(0) == m_testSets.at(0));
195 QVERIFY(verifysets.at(1) == m_testSets.at(1));
195 QVERIFY(verifysets.at(1) == m_testSets.at(1));
196
196
197 // Try removing all sets again (should be ok, even if some sets have already been removed)
197 // Try removing all sets again (should be ok, even if some sets have already been removed)
198 ret = false;
198 ret = false;
199 for (int i=0; i<count; i++) {
199 for (int i=0; i<count; i++) {
200 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
200 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
201 }
201 }
202
202
203 QVERIFY(ret == true);
203 QVERIFY(ret == true);
204 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
204 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
205 }
205 }
206
206
207 void tst_QBarSeries::appendList_data()
207 void tst_QBarSeries::appendList_data()
208 {
208 {
209
209
210 }
210 }
211
211
212 void tst_QBarSeries::appendList()
212 void tst_QBarSeries::appendList()
213 {
213 {
214 int count = 5;
214 int count = 5;
215 QVERIFY(m_barseries->barsetCount() == 0);
215 QVERIFY(m_barseries->barsetCount() == 0);
216
216
217 QList<QBarSet*> sets;
217 QList<QBarSet*> sets;
218 for (int i=0; i<count; i++) {
218 for (int i=0; i<count; i++) {
219 sets.append(new QBarSet("testset"));
219 sets.append(new QBarSet("testset"));
220 }
220 }
221
221
222 // Append new sets (should succeed, count should match the count of sets)
222 // Append new sets (should succeed, count should match the count of sets)
223 bool ret = false;
223 bool ret = false;
224 ret = m_barseries->append(sets);
224 ret = m_barseries->append(sets);
225 QVERIFY(ret == true);
225 QVERIFY(ret == true);
226 QVERIFY(m_barseries->barsetCount() == count);
226 QVERIFY(m_barseries->barsetCount() == count);
227
227
228 // Append same sets again (should fail, count should remain same)
228 // Append same sets again (should fail, count should remain same)
229 ret = m_barseries->append(sets);
229 ret = m_barseries->append(sets);
230 QVERIFY(ret == false);
230 QVERIFY(ret == false);
231 QVERIFY(m_barseries->barsetCount() == count);
231 QVERIFY(m_barseries->barsetCount() == count);
232
232
233 // Try append empty list (should succeed, but count should remain same)
233 // Try append empty list (should succeed, but count should remain same)
234 QList<QBarSet*> invalidList;
234 QList<QBarSet*> invalidList;
235 ret = m_barseries->append(invalidList);
235 ret = m_barseries->append(invalidList);
236 QVERIFY(ret == true);
236 QVERIFY(ret == true);
237 QVERIFY(m_barseries->barsetCount() == count);
237 QVERIFY(m_barseries->barsetCount() == count);
238
238
239 // Try append list with one new and one existing set (should fail, count remains same)
239 // Try append list with one new and one existing set (should fail, count remains same)
240 invalidList.append(new QBarSet("ok set"));
240 invalidList.append(new QBarSet("ok set"));
241 invalidList.append(sets.at(0));
241 invalidList.append(sets.at(0));
242 ret = m_barseries->append(invalidList);
242 ret = m_barseries->append(invalidList);
243 QVERIFY(ret == false);
243 QVERIFY(ret == false);
244 QVERIFY(m_barseries->barsetCount() == count);
244 QVERIFY(m_barseries->barsetCount() == count);
245
245
246 // Try append list with null pointers (should fail, count remains same)
246 // Try append list with null pointers (should fail, count remains same)
247 QList<QBarSet*> invalidList2;
247 QList<QBarSet*> invalidList2;
248 invalidList2.append(0);
248 invalidList2.append(0);
249 invalidList2.append(0);
249 invalidList2.append(0);
250 invalidList2.append(0);
250 invalidList2.append(0);
251 ret = m_barseries->append(invalidList2);
251 ret = m_barseries->append(invalidList2);
252 QVERIFY(ret == false);
252 QVERIFY(ret == false);
253 QVERIFY(m_barseries->barsetCount() == count);
253 QVERIFY(m_barseries->barsetCount() == count);
254 }
254 }
255
255
256 void tst_QBarSeries::barsetCount_data()
256 void tst_QBarSeries::barsetCount_data()
257 {
257 {
258
258
259 }
259 }
260
260
261 void tst_QBarSeries::barsetCount()
261 void tst_QBarSeries::barsetCount()
262 {
262 {
263 QVERIFY(m_barseries->barsetCount() == 0);
263 QVERIFY(m_barseries->barsetCount() == 0);
264 QVERIFY(m_barseries_with_sets->barsetCount() == m_testSets.count());
264 QVERIFY(m_barseries_with_sets->barsetCount() == m_testSets.count());
265 }
265 }
266
266
267 void tst_QBarSeries::barSets_data()
267 void tst_QBarSeries::barSets_data()
268 {
268 {
269
269
270 }
270 }
271
271
272 void tst_QBarSeries::barSets()
272 void tst_QBarSeries::barSets()
273 {
273 {
274 QVERIFY(m_barseries->barSets().count() == 0);
274 QVERIFY(m_barseries->barSets().count() == 0);
275
275
276 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
276 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
277 QVERIFY(sets.count() == m_testSets.count());
277 QVERIFY(sets.count() == m_testSets.count());
278
278
279 for (int i=0; i<m_testSets.count(); i++) {
279 for (int i=0; i<m_testSets.count(); i++) {
280 QVERIFY(sets.at(i) == m_testSets.at(i));
280 QVERIFY(sets.at(i) == m_testSets.at(i));
281 }
281 }
282 }
282 }
283
283
284 void tst_QBarSeries::setLabelsVisible_data()
284 void tst_QBarSeries::setLabelsVisible_data()
285 {
285 {
286
286
287 }
287 }
288
288
289 void tst_QBarSeries::setLabelsVisible()
289 void tst_QBarSeries::setLabelsVisible()
290 {
290 {
291 // labels should be invisible by default
291 // labels should be invisible by default
292 QVERIFY(m_barseries->isLabelsVisible() == false);
292 QVERIFY(m_barseries->isLabelsVisible() == false);
293 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
293 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
294
294
295 // turn labels to visible
295 // turn labels to visible
296 m_barseries_with_sets->setLabelsVisible(true);
296 m_barseries_with_sets->setLabelsVisible(true);
297 // TODO: test the signal
297 // TODO: test the signal
298 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
298 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
299
299
300 // turn labels to invisible
300 // turn labels to invisible
301 m_barseries_with_sets->setLabelsVisible(false);
301 m_barseries_with_sets->setLabelsVisible(false);
302 // TODO: test the signal
302 // TODO: test the signal
303 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
303 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
304
304
305 // without parameter, should turn labels to visible
305 // without parameter, should turn labels to visible
306 m_barseries_with_sets->setLabelsVisible();
306 m_barseries_with_sets->setLabelsVisible();
307 // TODO: test the signal
307 // TODO: test the signal
308 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
308 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
309 }
309 }
310
310
311 void tst_QBarSeries::mouseclicked_data()
311 void tst_QBarSeries::mouseclicked_data()
312 {
312 {
313
313
314 }
314 }
315
315
316 void tst_QBarSeries::mouseclicked()
316 void tst_QBarSeries::mouseclicked()
317 {
317 {
318 QBarSeries* series = new QBarSeries();
318 QBarSeries* series = new QBarSeries();
319
319
320 QBarSet* set1 = new QBarSet(QString("set 1"));
320 QBarSet* set1 = new QBarSet(QString("set 1"));
321 *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10);
321 *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10);
322 series->append(set1);
322 series->append(set1);
323
323
324 QBarSet* set2 = new QBarSet(QString("set 2"));
324 QBarSet* set2 = new QBarSet(QString("set 2"));
325 *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10);
325 *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10);
326 series->append(set2);
326 series->append(set2);
327
327
328 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,int)));
328 QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,int)));
329
329
330 QChartView view(new QChart());
330 QChartView view(new QChart());
331 view.resize(400,300);
331 view.resize(400,300);
332 view.chart()->addSeries(series);
332 view.chart()->addSeries(series);
333 view.show();
333 view.show();
334 QTest::qWaitForWindowShown(&view);
334 QTest::qWaitForWindowShown(&view);
335
335
336 //====================================================================================
336 //====================================================================================
337 // barset 1, bar 0
337 // barset 1, bar 0
338 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(95,142));
338 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(102,142));
339 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
339 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
340
340
341 QCOMPARE(seriesSpy.count(), 1);
341 QCOMPARE(seriesSpy.count(), 1);
342
342
343 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
343 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
344 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
344 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
345 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
345 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
346 QVERIFY(seriesSpyArg.at(1).toInt() == 0);
346 QVERIFY(seriesSpyArg.at(1).toInt() == 0);
347
347
348 //====================================================================================
348 //====================================================================================
349 // barset 1, bar 1
349 // barset 1, bar 1
350 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(164,142));
350 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(186,142));
351 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
351 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
352
352
353 QCOMPARE(seriesSpy.count(), 1);
353 QCOMPARE(seriesSpy.count(), 1);
354
354
355 seriesSpyArg = seriesSpy.takeFirst();
355 seriesSpyArg = seriesSpy.takeFirst();
356 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
356 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
357 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
357 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
358 QVERIFY(seriesSpyArg.at(1).toInt() == 1);
358 QVERIFY(seriesSpyArg.at(1).toInt() == 1);
359
359
360 //====================================================================================
360 //====================================================================================
361 // barset 1, bar 2
361 // barset 1, bar 2
362 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(234,142));
362 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(271,142));
363 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
363 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
364
364
365 QCOMPARE(seriesSpy.count(), 1);
365 QCOMPARE(seriesSpy.count(), 1);
366
366
367 seriesSpyArg = seriesSpy.takeFirst();
367 seriesSpyArg = seriesSpy.takeFirst();
368 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
368 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
369 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
369 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
370 QVERIFY(seriesSpyArg.at(1).toInt() == 2);
370 QVERIFY(seriesSpyArg.at(1).toInt() == 2);
371
371
372 //====================================================================================
372 //====================================================================================
373 // barset 2, bar 0
373 // barset 2, bar 0
374 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(115,142));
374 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(127,142));
375 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
375 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
376
376
377 QCOMPARE(seriesSpy.count(), 1);
377 QCOMPARE(seriesSpy.count(), 1);
378
378
379 seriesSpyArg = seriesSpy.takeFirst();
379 seriesSpyArg = seriesSpy.takeFirst();
380 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
380 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
381 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
381 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
382 QVERIFY(seriesSpyArg.at(1).toInt() == 0);
382 QVERIFY(seriesSpyArg.at(1).toInt() == 0);
383
383
384 //====================================================================================
384 //====================================================================================
385 // barset 2, bar 1
385 // barset 2, bar 1
386 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(185,142));
386 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(212,142));
387 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
387 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
388
388
389 QCOMPARE(seriesSpy.count(), 1);
389 QCOMPARE(seriesSpy.count(), 1);
390
390
391 seriesSpyArg = seriesSpy.takeFirst();
391 seriesSpyArg = seriesSpy.takeFirst();
392 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
392 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
393 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
393 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
394 QVERIFY(seriesSpyArg.at(1).toInt() == 1);
394 QVERIFY(seriesSpyArg.at(1).toInt() == 1);
395
395
396 //====================================================================================
396 //====================================================================================
397 // barset 2, bar 2
397 // barset 2, bar 2
398 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(254,142));
398 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(296,142));
399 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
399 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
400
400
401 QCOMPARE(seriesSpy.count(), 1);
401 QCOMPARE(seriesSpy.count(), 1);
402
402
403 seriesSpyArg = seriesSpy.takeFirst();
403 seriesSpyArg = seriesSpy.takeFirst();
404 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
404 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
405 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
405 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Int);
406 QVERIFY(seriesSpyArg.at(1).toInt() == 2);
406 QVERIFY(seriesSpyArg.at(1).toInt() == 2);
407 }
407 }
408
408
409 void tst_QBarSeries::mousehovered_data()
409 void tst_QBarSeries::mousehovered_data()
410 {
410 {
411
411
412 }
412 }
413
413
414 void tst_QBarSeries::mousehovered()
414 void tst_QBarSeries::mousehovered()
415 {
415 {
416 QBarSeries* series = new QBarSeries();
416 QBarSeries* series = new QBarSeries();
417 QStringList categories;
418 categories << "test1" << "test2" << "test3";
419 // series->setCategories(categories);
420
417
421 QBarSet* set1 = new QBarSet(QString("set 1"));
418 QBarSet* set1 = new QBarSet(QString("set 1"));
422 *set1 << QPointF(0.1,10) << QPointF(1.1,10) << QPointF(2.1,10);
419 *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10);
423 series->append(set1);
420 series->append(set1);
424
421
425 QBarSet* set2 = new QBarSet(QString("set 2"));
422 QBarSet* set2 = new QBarSet(QString("set 2"));
426 *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10);
423 *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10);
427 series->append(set2);
424 series->append(set2);
428
425
429 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
426 QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool)));
430
427
431 QChartView view(new QChart());
428 QChartView view(new QChart());
432 view.resize(400,300);
429 view.resize(400,300);
433 view.chart()->addSeries(series);
430 view.chart()->addSeries(series);
434 view.show();
431 view.show();
435 QTest::qWaitForWindowShown(&view);
432 QTest::qWaitForWindowShown(&view);
436
433
437 //this is hack since view does not get events otherwise
434 //this is hack since view does not get events otherwise
438 view.setMouseTracking(true);
435 view.setMouseTracking(true);
439
436
440 //=======================================================================
437 //=======================================================================
441 // move mouse to left border
438 // move mouse to left border
442 QTest::mouseMove(view.viewport(), QPoint(0, 142));
439 QTest::mouseMove(view.viewport(), QPoint(0, 142));
443
440
444 QVERIFY(seriesSpy.count() == 0);
441 QVERIFY(seriesSpy.count() == 0);
445
442
446 //=======================================================================
443 //=======================================================================
447 // move mouse on top of set1
444 // move mouse on top of set1
448 QTest::mouseMove(view.viewport(), QPoint(95,142));
445 QTest::mouseMove(view.viewport(), QPoint(102,142));
449
446
450 QVERIFY(seriesSpy.count() == 1);
447 QVERIFY(seriesSpy.count() == 1);
451
448
452 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
449 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
453 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
450 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
454 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
451 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
455 QVERIFY(seriesSpyArg.at(1).toBool() == true);
452 QVERIFY(seriesSpyArg.at(1).toBool() == true);
456
453
457 //=======================================================================
454 //=======================================================================
458 // move mouse from top of set1 to top of set2
455 // move mouse from top of set1 to top of set2
459 QTest::mouseMove(view.viewport(), QPoint(116,142));
456 QTest::mouseMove(view.viewport(), QPoint(127,142));
460
457
461 QVERIFY(seriesSpy.count() == 2);
458 QVERIFY(seriesSpy.count() == 2);
462
459
463 // should leave set1
460 // should leave set1
464 seriesSpyArg = seriesSpy.takeFirst();
461 seriesSpyArg = seriesSpy.takeFirst();
465 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
462 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1);
466 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
463 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
467 QVERIFY(seriesSpyArg.at(1).toBool() == false);
464 QVERIFY(seriesSpyArg.at(1).toBool() == false);
468
465
469 // should enter set2
466 // should enter set2
470 seriesSpyArg = seriesSpy.takeFirst();
467 seriesSpyArg = seriesSpy.takeFirst();
471 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
468 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
472 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
469 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
473 QVERIFY(seriesSpyArg.at(1).toBool() == true);
470 QVERIFY(seriesSpyArg.at(1).toBool() == true);
474
471
475 //=======================================================================
472 //=======================================================================
476 // move mouse from top of set2 to background
473 // move mouse from top of set2 to background
477 QTest::mouseMove(view.viewport(), QPoint(116,0));
474 QTest::mouseMove(view.viewport(), QPoint(127,0));
478 QVERIFY(seriesSpy.count() == 1);
475 QVERIFY(seriesSpy.count() == 1);
479
476
480 // should leave set2
477 // should leave set2
481 seriesSpyArg = seriesSpy.takeFirst();
478 seriesSpyArg = seriesSpy.takeFirst();
482 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
479 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2);
483 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
480 QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool);
484 QVERIFY(seriesSpyArg.at(1).toBool() == false);
481 QVERIFY(seriesSpyArg.at(1).toBool() == false);
485 }
482 }
486
483
487 void tst_QBarSeries::clearWithAnimations()
484 void tst_QBarSeries::clearWithAnimations()
488 {
485 {
489 QSKIP("Known issue with the animation handling", SkipAll);
490 QBarSeries* series = new QBarSeries();
486 QBarSeries* series = new QBarSeries();
491 QStringList categories;
492 categories << "test1" << "test2" << "test3";
493 // series->setCategories(categories);
494
487
495 QBarSet* set1 = new QBarSet(QString("set 1"));
488 QBarSet* set1 = new QBarSet(QString("set 1"));
496 *set1 << QPointF(0.1,10) << QPointF(1.1,10) << QPointF(2.1,10);
489 *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10);
497 series->append(set1);
490 series->append(set1);
498
491
499 QBarSet* set2 = new QBarSet(QString("set 2"));
492 QBarSet* set2 = new QBarSet(QString("set 2"));
500 *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10);
493 *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10);
501 series->append(set2);
494 series->append(set2);
502
495
503 QChartView view(new QChart());
496 QChartView view(new QChart());
504 view.resize(400,300);
497 view.resize(400,300);
505 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
498 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
506 view.chart()->addSeries(series);
499 view.chart()->addSeries(series);
507 view.show();
500 view.show();
508
501
509 series->clear();
502 series->clear();
510 }
503 }
511
504
512 QTEST_MAIN(tst_QBarSeries)
505 QTEST_MAIN(tst_QBarSeries)
513
506
514 #include "tst_qbarseries.moc"
507 #include "tst_qbarseries.moc"
515
508
General Comments 0
You need to be logged in to leave comments. Login now