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