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