##// END OF EJS Templates
Improve theme initialization performance for bar series...
Titta Heikkala -
r2598:764a09c2b2a5
parent child
Show More
@@ -1,224 +1,226
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "abstractbarchartitem_p.h"
21 #include "abstractbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qabstractbarseries.h"
25 #include "qabstractbarseries.h"
26 #include "qabstractbarseries_p.h"
26 #include "qabstractbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "charttheme_p.h"
29 #include "charttheme_p.h"
30 #include "baranimation_p.h"
30 #include "baranimation_p.h"
31 #include "chartdataset_p.h"
31 #include "chartdataset_p.h"
32 #include <QPainter>
32 #include <QPainter>
33 #include <QTextDocument>
33 #include <QTextDocument>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
37 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
38 ChartItem(series->d_func(),item),
38 ChartItem(series->d_func(),item),
39 m_animation(0),
39 m_animation(0),
40 m_series(series)
40 m_series(series)
41 {
41 {
42
42
43 setFlag(ItemClipsChildrenToShape);
43 setFlag(ItemClipsChildrenToShape);
44 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
44 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
45 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
45 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
46 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
46 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
48 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
48 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
49 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
49 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
50 setZValue(ChartPresenter::BarSeriesZValue);
50 setZValue(ChartPresenter::BarSeriesZValue);
51 handleDataStructureChanged();
51 handleDataStructureChanged();
52 handleVisibleChanged();
52 handleVisibleChanged();
53 handleUpdatedBars();
53 handleUpdatedBars();
54 }
54 }
55
55
56 AbstractBarChartItem::~AbstractBarChartItem()
56 AbstractBarChartItem::~AbstractBarChartItem()
57 {
57 {
58 }
58 }
59
59
60 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
61 {
61 {
62 Q_UNUSED(painter);
62 Q_UNUSED(painter);
63 Q_UNUSED(option);
63 Q_UNUSED(option);
64 Q_UNUSED(widget);
64 Q_UNUSED(widget);
65 }
65 }
66
66
67 QRectF AbstractBarChartItem::boundingRect() const
67 QRectF AbstractBarChartItem::boundingRect() const
68 {
68 {
69 return m_rect;
69 return m_rect;
70 }
70 }
71
71
72 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
72 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
73 {
73 {
74 QSizeF size = geometry().size();
74 QSizeF size = geometry().size();
75 if (geometry().size().isValid()) {
75 if (geometry().size().isValid()) {
76 if (m_animation) {
76 if (m_animation) {
77 if (m_layout.count() == 0 || m_oldSize != size) {
77 if (m_layout.count() == 0 || m_oldSize != size) {
78 initializeLayout();
78 initializeLayout();
79 m_oldSize = size;
79 m_oldSize = size;
80 }
80 }
81 m_animation->setup(m_layout, layout);
81 m_animation->setup(m_layout, layout);
82 presenter()->startAnimation(m_animation);
82 presenter()->startAnimation(m_animation);
83 } else {
83 } else {
84 setLayout(layout);
84 setLayout(layout);
85 update();
85 update();
86 }
86 }
87 }
87 }
88 }
88 }
89
89
90 void AbstractBarChartItem::setAnimation(BarAnimation *animation)
90 void AbstractBarChartItem::setAnimation(BarAnimation *animation)
91 {
91 {
92 m_animation = animation;
92 m_animation = animation;
93 }
93 }
94
94
95 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
95 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
96 {
96 {
97 if (layout.count() != m_bars.count())
97 if (layout.count() != m_bars.count())
98 return;
98 return;
99
99
100 m_layout = layout;
100 m_layout = layout;
101
101
102 for (int i = 0; i < m_bars.count(); i++) {
102 for (int i = 0; i < m_bars.count(); i++) {
103 m_bars.at(i)->setRect(layout.at(i));
103 m_bars.at(i)->setRect(layout.at(i));
104 QGraphicsTextItem *label = m_labels.at(i);
104 QGraphicsTextItem *label = m_labels.at(i);
105 label->setPos(layout.at(i).center() - label->boundingRect().center());
105 label->setPos(layout.at(i).center() - label->boundingRect().center());
106
106
107 }
107 }
108 }
108 }
109 //handlers
109 //handlers
110
110
111 void AbstractBarChartItem::handleDomainUpdated()
111 void AbstractBarChartItem::handleDomainUpdated()
112 {
112 {
113 m_domainMinX = domain()->minX();
113 m_domainMinX = domain()->minX();
114 m_domainMaxX = domain()->maxX();
114 m_domainMaxX = domain()->maxX();
115 m_domainMinY = domain()->minY();
115 m_domainMinY = domain()->minY();
116 m_domainMaxY = domain()->maxY();
116 m_domainMaxY = domain()->maxY();
117
117
118 QRectF rect(QPointF(0,0),domain()->size());
118 QRectF rect(QPointF(0,0),domain()->size());
119
119
120 if(m_rect != rect){
120 if(m_rect != rect){
121 prepareGeometryChange();
121 prepareGeometryChange();
122 m_rect = rect;
122 m_rect = rect;
123 }
123 }
124
124
125 handleLayoutChanged();
125 handleLayoutChanged();
126 }
126 }
127
127
128 void AbstractBarChartItem::handleLayoutChanged()
128 void AbstractBarChartItem::handleLayoutChanged()
129 {
129 {
130 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
130 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
131 return; // rect size zero.
131 return; // rect size zero.
132 QVector<QRectF> layout = calculateLayout();
132 QVector<QRectF> layout = calculateLayout();
133 applyLayout(layout);
133 applyLayout(layout);
134 handleUpdatedBars();
134 handleUpdatedBars();
135 }
135 }
136
136
137 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
137 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
138 {
138 {
139 foreach (QGraphicsTextItem *label, m_labels)
139 foreach (QGraphicsTextItem *label, m_labels)
140 label->setVisible(visible);
140 label->setVisible(visible);
141 update();
141 update();
142 }
142 }
143
143
144 void AbstractBarChartItem::handleDataStructureChanged()
144 void AbstractBarChartItem::handleDataStructureChanged()
145 {
145 {
146 foreach (QGraphicsItem *item, childItems())
146 foreach (QGraphicsItem *item, childItems())
147 delete item;
147 delete item;
148
148
149 m_bars.clear();
149 m_bars.clear();
150 m_labels.clear();
150 m_labels.clear();
151 m_layout.clear();
151 m_layout.clear();
152
152
153 // Create new graphic items for bars
153 // Create new graphic items for bars
154 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
154 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
155 for (int s = 0; s < m_series->count(); s++) {
155 for (int s = 0; s < m_series->count(); s++) {
156 QBarSet *set = m_series->d_func()->barsetAt(s);
156 QBarSet *set = m_series->d_func()->barsetAt(s);
157
157
158 // Bars
158 // Bars
159 Bar *bar = new Bar(set, c, this);
159 Bar *bar = new Bar(set, c, this);
160 m_bars.append(bar);
160 m_bars.append(bar);
161 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
161 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
162 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
162 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
163 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
163 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
164 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
164 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
165 // m_layout.append(QRectF(0, 0, 1, 1));
165 // m_layout.append(QRectF(0, 0, 1, 1));
166
166
167 // Labels
167 // Labels
168 QGraphicsTextItem *newLabel = new QGraphicsTextItem(this);
168 QGraphicsTextItem *newLabel = new QGraphicsTextItem(this);
169 newLabel->document()->setDocumentMargin(ChartPresenter::textMargin());
169 newLabel->document()->setDocumentMargin(ChartPresenter::textMargin());
170 m_labels.append(newLabel);
170 m_labels.append(newLabel);
171 }
171 }
172 }
172 }
173
173
174 if(themeManager()) themeManager()->updateSeries(m_series);
174 if(themeManager()) themeManager()->updateSeries(m_series);
175 handleLayoutChanged();
175 handleLayoutChanged();
176 handleVisibleChanged();
176 handleVisibleChanged();
177 }
177 }
178
178
179 void AbstractBarChartItem::handleVisibleChanged()
179 void AbstractBarChartItem::handleVisibleChanged()
180 {
180 {
181 bool visible = m_series->isVisible();
181 bool visible = m_series->isVisible();
182 if (visible)
182 if (visible)
183 handleLabelsVisibleChanged(m_series->isLabelsVisible());
183 handleLabelsVisibleChanged(m_series->isLabelsVisible());
184 else
184 else
185 handleLabelsVisibleChanged(visible);
185 handleLabelsVisibleChanged(visible);
186
186
187 foreach (QGraphicsItem *bar, m_bars)
187 foreach (QGraphicsItem *bar, m_bars)
188 bar->setVisible(visible);
188 bar->setVisible(visible);
189 }
189 }
190
190
191 void AbstractBarChartItem::handleOpacityChanged()
191 void AbstractBarChartItem::handleOpacityChanged()
192 {
192 {
193 foreach (QGraphicsItem *item, childItems())
193 foreach (QGraphicsItem *item, childItems())
194 item->setOpacity(m_series->opacity());
194 item->setOpacity(m_series->opacity());
195 }
195 }
196
196
197 void AbstractBarChartItem::handleUpdatedBars()
197 void AbstractBarChartItem::handleUpdatedBars()
198 {
198 {
199 // Handle changes in pen, brush, labels etc.
199 if (!m_series->d_func()->blockBarUpdate()) {
200 int categoryCount = m_series->d_func()->categoryCount();
200 // Handle changes in pen, brush, labels etc.
201 int setCount = m_series->count();
201 int categoryCount = m_series->d_func()->categoryCount();
202 int itemIndex(0);
202 int setCount = m_series->count();
203
203 int itemIndex(0);
204 for (int category = 0; category < categoryCount; category++) {
204
205 for (int set = 0; set < setCount; set++) {
205 for (int category = 0; category < categoryCount; category++) {
206 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
206 for (int set = 0; set < setCount; set++) {
207 Bar *bar = m_bars.at(itemIndex);
207 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
208 bar->setPen(barSet->m_pen);
208 Bar *bar = m_bars.at(itemIndex);
209 bar->setBrush(barSet->m_brush);
209 bar->setPen(barSet->m_pen);
210 bar->update();
210 bar->setBrush(barSet->m_brush);
211
211 bar->update();
212 QGraphicsTextItem *label = m_labels.at(itemIndex);
212
213 label->setHtml(QString("%1").arg(barSet->value(category)));
213 QGraphicsTextItem *label = m_labels.at(itemIndex);
214 label->setFont(barSet->m_labelFont);
214 label->setHtml(QString("%1").arg(barSet->value(category)));
215 label->setDefaultTextColor(barSet->m_labelBrush.color());
215 label->setFont(barSet->m_labelFont);
216 label->update();
216 label->setDefaultTextColor(barSet->m_labelBrush.color());
217 itemIndex++;
217 label->update();
218 itemIndex++;
219 }
218 }
220 }
219 }
221 }
220 }
222 }
221
223
222 #include "moc_abstractbarchartitem_p.cpp"
224 #include "moc_abstractbarchartitem_p.cpp"
223
225
224 QTCOMMERCIALCHART_END_NAMESPACE
226 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,903 +1,911
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qabstractbarseries.h"
21 #include "qabstractbarseries.h"
22 #include "qabstractbarseries_p.h"
22 #include "qabstractbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "abstractdomain_p.h"
25 #include "abstractdomain_p.h"
26 #include "chartdataset_p.h"
26 #include "chartdataset_p.h"
27 #include "charttheme_p.h"
27 #include "charttheme_p.h"
28 #include "qvalueaxis.h"
28 #include "qvalueaxis.h"
29 #include "qbarcategoryaxis.h"
29 #include "qbarcategoryaxis.h"
30 #include "qbarlegendmarker.h"
30 #include "qbarlegendmarker.h"
31 #include "baranimation_p.h"
31 #include "baranimation_p.h"
32 #include "abstractbarchartitem_p.h"
32 #include "abstractbarchartitem_p.h"
33 #include "qchart_p.h"
33 #include "qchart_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 /*!
37 /*!
38 \class QAbstractBarSeries
38 \class QAbstractBarSeries
39 \brief Series for creating a bar chart.
39 \brief Series for creating a bar chart.
40 \mainclass
40 \mainclass
41
41
42 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
42 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
43 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
43 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
44 and y-value is the height of the bar. The category names are ignored with this series and x-axis
44 and y-value is the height of the bar. The category names are ignored with this series and x-axis
45 shows the x-values.
45 shows the x-values.
46
46
47 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
47 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
48 \image examples_barchart.png
48 \image examples_barchart.png
49
49
50 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
50 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
51 */
51 */
52 /*!
52 /*!
53 \qmlclass AbstractBarSeries QAbstractBarSeries
53 \qmlclass AbstractBarSeries QAbstractBarSeries
54 \inherits QAbstractSeries
54 \inherits QAbstractSeries
55
55
56 The following QML shows how to create a simple bar chart:
56 The following QML shows how to create a simple bar chart:
57 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
57 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
58
58
59 \beginfloatleft
59 \beginfloatleft
60 \image demos_qmlchart6.png
60 \image demos_qmlchart6.png
61 \endfloat
61 \endfloat
62 \clearfloat
62 \clearfloat
63 */
63 */
64
64
65 /*!
65 /*!
66 \qmlproperty AbstractAxis AbstractBarSeries::axisX
66 \qmlproperty AbstractAxis AbstractBarSeries::axisX
67 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
67 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
68 the series.
68 the series.
69 \sa axisXTop
69 \sa axisXTop
70 */
70 */
71
71
72 /*!
72 /*!
73 \qmlproperty AbstractAxis AbstractBarSeries::axisY
73 \qmlproperty AbstractAxis AbstractBarSeries::axisY
74 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
74 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
75 the series.
75 the series.
76 \sa axisYRight
76 \sa axisYRight
77 */
77 */
78
78
79 /*!
79 /*!
80 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
80 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
81 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
81 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
82 axisXTop, but not both.
82 axisXTop, but not both.
83 \sa axisX
83 \sa axisX
84 */
84 */
85
85
86 /*!
86 /*!
87 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
87 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
88 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
88 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
89 or axisYRight, but not both.
89 or axisYRight, but not both.
90 \sa axisY
90 \sa axisY
91 */
91 */
92
92
93 /*!
93 /*!
94 \property QAbstractBarSeries::barWidth
94 \property QAbstractBarSeries::barWidth
95 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
95 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
96 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
96 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
97 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
97 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
98 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
98 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
99 \sa QBarSeries
99 \sa QBarSeries
100 */
100 */
101 /*!
101 /*!
102 \qmlproperty real AbstractBarSeries::barWidth
102 \qmlproperty real AbstractBarSeries::barWidth
103 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
103 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
104 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
104 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
105 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
105 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
106 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
106 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
107 */
107 */
108
108
109 /*!
109 /*!
110 \property QAbstractBarSeries::count
110 \property QAbstractBarSeries::count
111 Holds the number of sets in series.
111 Holds the number of sets in series.
112 */
112 */
113 /*!
113 /*!
114 \qmlproperty int AbstractBarSeries::count
114 \qmlproperty int AbstractBarSeries::count
115 Holds the number of sets in series.
115 Holds the number of sets in series.
116 */
116 */
117
117
118 /*!
118 /*!
119 \property QAbstractBarSeries::labelsVisible
119 \property QAbstractBarSeries::labelsVisible
120 Defines the visibility of the labels in series
120 Defines the visibility of the labels in series
121 */
121 */
122 /*!
122 /*!
123 \qmlproperty bool AbstractBarSeries::labelsVisible
123 \qmlproperty bool AbstractBarSeries::labelsVisible
124 Defines the visibility of the labels in series
124 Defines the visibility of the labels in series
125 */
125 */
126
126
127 /*!
127 /*!
128 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
128 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
129 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
129 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
130 Clicked bar inside set is indexed by \a index
130 Clicked bar inside set is indexed by \a index
131 */
131 */
132 /*!
132 /*!
133 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
133 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
134 The signal is emitted if the user clicks with a mouse on top of BarSet.
134 The signal is emitted if the user clicks with a mouse on top of BarSet.
135 Clicked bar inside set is indexed by \a index
135 Clicked bar inside set is indexed by \a index
136 */
136 */
137
137
138 /*!
138 /*!
139 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
139 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
140
140
141 The signal is emitted if mouse is hovered on top of series.
141 The signal is emitted if mouse is hovered on top of series.
142 Parameter \a barset is the pointer of barset, where hover happened.
142 Parameter \a barset is the pointer of barset, where hover happened.
143 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
143 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
144 */
144 */
145 /*!
145 /*!
146 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
146 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
147
147
148 The signal is emitted if mouse is hovered on top of series.
148 The signal is emitted if mouse is hovered on top of series.
149 Parameter \a barset is the pointer of barset, where hover happened.
149 Parameter \a barset is the pointer of barset, where hover happened.
150 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
150 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
151 */
151 */
152
152
153 /*!
153 /*!
154 \fn void QAbstractBarSeries::countChanged()
154 \fn void QAbstractBarSeries::countChanged()
155 This signal is emitted when barset count has been changed, for example by append or remove.
155 This signal is emitted when barset count has been changed, for example by append or remove.
156 */
156 */
157 /*!
157 /*!
158 \qmlsignal AbstractBarSeries::onCountChanged()
158 \qmlsignal AbstractBarSeries::onCountChanged()
159 This signal is emitted when barset count has been changed, for example by append or remove.
159 This signal is emitted when barset count has been changed, for example by append or remove.
160 */
160 */
161
161
162 /*!
162 /*!
163 \fn void QAbstractBarSeries::labelsVisibleChanged()
163 \fn void QAbstractBarSeries::labelsVisibleChanged()
164 This signal is emitted when labels visibility have changed.
164 This signal is emitted when labels visibility have changed.
165 \sa isLabelsVisible(), setLabelsVisible()
165 \sa isLabelsVisible(), setLabelsVisible()
166 */
166 */
167
167
168 /*!
168 /*!
169 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
169 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
170 This signal is emitted when \a sets have been added to the series.
170 This signal is emitted when \a sets have been added to the series.
171 \sa append(), insert()
171 \sa append(), insert()
172 */
172 */
173 /*!
173 /*!
174 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
174 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
175 Emitted when \a barset has been added to the series.
175 Emitted when \a barset has been added to the series.
176 */
176 */
177
177
178 /*!
178 /*!
179 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
179 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
180 This signal is emitted when \a sets have been removed from the series.
180 This signal is emitted when \a sets have been removed from the series.
181 \sa remove()
181 \sa remove()
182 */
182 */
183 /*!
183 /*!
184 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
184 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
185 Emitted when \a barset has been removed from the series.
185 Emitted when \a barset has been removed from the series.
186 */
186 */
187
187
188 /*!
188 /*!
189 \qmlmethod BarSet AbstractBarSeries::at(int index)
189 \qmlmethod BarSet AbstractBarSeries::at(int index)
190 Returns bar set at \a index. Returns null if the index is not valid.
190 Returns bar set at \a index. Returns null if the index is not valid.
191 */
191 */
192
192
193 /*!
193 /*!
194 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
194 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
195 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
195 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
196 For example:
196 For example:
197 \code
197 \code
198 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
198 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
199 \endcode
199 \endcode
200 */
200 */
201
201
202 /*!
202 /*!
203 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
203 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
204 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
204 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
205 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
205 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
206 appended.
206 appended.
207 \sa AbstractBarSeries::append()
207 \sa AbstractBarSeries::append()
208 */
208 */
209
209
210 /*!
210 /*!
211 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
211 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
212 Removes the barset from the series. Returns true if successful, false otherwise.
212 Removes the barset from the series. Returns true if successful, false otherwise.
213 */
213 */
214
214
215 /*!
215 /*!
216 \qmlmethod AbstractBarSeries::clear()
216 \qmlmethod AbstractBarSeries::clear()
217 Removes all barsets from the series.
217 Removes all barsets from the series.
218 */
218 */
219
219
220 /*!
220 /*!
221 Destructs abstractbarseries and owned barsets.
221 Destructs abstractbarseries and owned barsets.
222 */
222 */
223 QAbstractBarSeries::~QAbstractBarSeries()
223 QAbstractBarSeries::~QAbstractBarSeries()
224 {
224 {
225
225
226 }
226 }
227
227
228 /*!
228 /*!
229 \internal
229 \internal
230 */
230 */
231 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
231 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
232 : QAbstractSeries(o, parent)
232 : QAbstractSeries(o, parent)
233 {
233 {
234 Q_D(QAbstractSeries);
234 Q_D(QAbstractSeries);
235 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
235 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
236 }
236 }
237
237
238 /*!
238 /*!
239 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
239 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
240 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
240 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
241 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
241 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
242 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
242 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
243 */
243 */
244 void QAbstractBarSeries::setBarWidth(qreal width)
244 void QAbstractBarSeries::setBarWidth(qreal width)
245 {
245 {
246 Q_D(QAbstractBarSeries);
246 Q_D(QAbstractBarSeries);
247 d->setBarWidth(width);
247 d->setBarWidth(width);
248 }
248 }
249
249
250 /*!
250 /*!
251 Returns the width of the bars of the series.
251 Returns the width of the bars of the series.
252 \sa setBarWidth()
252 \sa setBarWidth()
253 */
253 */
254 qreal QAbstractBarSeries::barWidth() const
254 qreal QAbstractBarSeries::barWidth() const
255 {
255 {
256 Q_D(const QAbstractBarSeries);
256 Q_D(const QAbstractBarSeries);
257 return d->barWidth();
257 return d->barWidth();
258 }
258 }
259
259
260 /*!
260 /*!
261 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.
261 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.
262 Returns true, if appending succeeded.
262 Returns true, if appending succeeded.
263 */
263 */
264 bool QAbstractBarSeries::append(QBarSet *set)
264 bool QAbstractBarSeries::append(QBarSet *set)
265 {
265 {
266 Q_D(QAbstractBarSeries);
266 Q_D(QAbstractBarSeries);
267 bool success = d->append(set);
267 bool success = d->append(set);
268 if (success) {
268 if (success) {
269 QList<QBarSet *> sets;
269 QList<QBarSet *> sets;
270 sets.append(set);
270 sets.append(set);
271 set->setParent(this);
271 set->setParent(this);
272 emit barsetsAdded(sets);
272 emit barsetsAdded(sets);
273 emit countChanged();
273 emit countChanged();
274 }
274 }
275 return success;
275 return success;
276 }
276 }
277
277
278 /*!
278 /*!
279 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
279 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
280 was successful.
280 was successful.
281 Returns true, if set was removed.
281 Returns true, if set was removed.
282 */
282 */
283 bool QAbstractBarSeries::remove(QBarSet *set)
283 bool QAbstractBarSeries::remove(QBarSet *set)
284 {
284 {
285 Q_D(QAbstractBarSeries);
285 Q_D(QAbstractBarSeries);
286 bool success = d->remove(set);
286 bool success = d->remove(set);
287 if (success) {
287 if (success) {
288 QList<QBarSet *> sets;
288 QList<QBarSet *> sets;
289 sets.append(set);
289 sets.append(set);
290 set->setParent(0);
290 set->setParent(0);
291 emit barsetsRemoved(sets);
291 emit barsetsRemoved(sets);
292 emit countChanged();
292 emit countChanged();
293 delete set;
293 delete set;
294 set = 0;
294 set = 0;
295 }
295 }
296 return success;
296 return success;
297 }
297 }
298
298
299 /*!
299 /*!
300 Takes a single \a set from the series. Does not delete the barset object.
300 Takes a single \a set from the series. Does not delete the barset object.
301
301
302 NOTE: The series remains as the barset's parent object. You must set the
302 NOTE: The series remains as the barset's parent object. You must set the
303 parent object to take full ownership.
303 parent object to take full ownership.
304
304
305 Returns true if take was successful.
305 Returns true if take was successful.
306 */
306 */
307 bool QAbstractBarSeries::take(QBarSet *set)
307 bool QAbstractBarSeries::take(QBarSet *set)
308 {
308 {
309 Q_D(QAbstractBarSeries);
309 Q_D(QAbstractBarSeries);
310 bool success = d->remove(set);
310 bool success = d->remove(set);
311 if (success) {
311 if (success) {
312 QList<QBarSet *> sets;
312 QList<QBarSet *> sets;
313 sets.append(set);
313 sets.append(set);
314 emit barsetsRemoved(sets);
314 emit barsetsRemoved(sets);
315 emit countChanged();
315 emit countChanged();
316 }
316 }
317 return success;
317 return success;
318 }
318 }
319
319
320 /*!
320 /*!
321 Adds a list of barsets to series. Takes ownership of \a sets.
321 Adds a list of barsets to series. Takes ownership of \a sets.
322 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
322 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
323 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
323 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
324 and function returns false.
324 and function returns false.
325 */
325 */
326 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
326 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
327 {
327 {
328 Q_D(QAbstractBarSeries);
328 Q_D(QAbstractBarSeries);
329 bool success = d->append(sets);
329 bool success = d->append(sets);
330 if (success) {
330 if (success) {
331 emit barsetsAdded(sets);
331 emit barsetsAdded(sets);
332 emit countChanged();
332 emit countChanged();
333 }
333 }
334 return success;
334 return success;
335 }
335 }
336
336
337 /*!
337 /*!
338 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.
338 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.
339 Returns true, if inserting succeeded.
339 Returns true, if inserting succeeded.
340
340
341 */
341 */
342 bool QAbstractBarSeries::insert(int index, QBarSet *set)
342 bool QAbstractBarSeries::insert(int index, QBarSet *set)
343 {
343 {
344 Q_D(QAbstractBarSeries);
344 Q_D(QAbstractBarSeries);
345 bool success = d->insert(index, set);
345 bool success = d->insert(index, set);
346 if (success) {
346 if (success) {
347 QList<QBarSet *> sets;
347 QList<QBarSet *> sets;
348 sets.append(set);
348 sets.append(set);
349 emit barsetsAdded(sets);
349 emit barsetsAdded(sets);
350 emit countChanged();
350 emit countChanged();
351 }
351 }
352 return success;
352 return success;
353 }
353 }
354
354
355 /*!
355 /*!
356 Removes all barsets from the series. Deletes removed sets.
356 Removes all barsets from the series. Deletes removed sets.
357 */
357 */
358 void QAbstractBarSeries::clear()
358 void QAbstractBarSeries::clear()
359 {
359 {
360 Q_D(QAbstractBarSeries);
360 Q_D(QAbstractBarSeries);
361 QList<QBarSet *> sets = barSets();
361 QList<QBarSet *> sets = barSets();
362 bool success = d->remove(sets);
362 bool success = d->remove(sets);
363 if (success) {
363 if (success) {
364 emit barsetsRemoved(sets);
364 emit barsetsRemoved(sets);
365 emit countChanged();
365 emit countChanged();
366 foreach (QBarSet *set, sets)
366 foreach (QBarSet *set, sets)
367 delete set;
367 delete set;
368 }
368 }
369 }
369 }
370
370
371 /*!
371 /*!
372 Returns number of sets in series.
372 Returns number of sets in series.
373 */
373 */
374 int QAbstractBarSeries::count() const
374 int QAbstractBarSeries::count() const
375 {
375 {
376 Q_D(const QAbstractBarSeries);
376 Q_D(const QAbstractBarSeries);
377 return d->m_barSets.count();
377 return d->m_barSets.count();
378 }
378 }
379
379
380 /*!
380 /*!
381 Returns a list of sets in series. Keeps ownership of sets.
381 Returns a list of sets in series. Keeps ownership of sets.
382 */
382 */
383 QList<QBarSet *> QAbstractBarSeries::barSets() const
383 QList<QBarSet *> QAbstractBarSeries::barSets() const
384 {
384 {
385 Q_D(const QAbstractBarSeries);
385 Q_D(const QAbstractBarSeries);
386 return d->m_barSets;
386 return d->m_barSets;
387 }
387 }
388
388
389 /*!
389 /*!
390 Sets the visibility of labels in series to \a visible
390 Sets the visibility of labels in series to \a visible
391 */
391 */
392 void QAbstractBarSeries::setLabelsVisible(bool visible)
392 void QAbstractBarSeries::setLabelsVisible(bool visible)
393 {
393 {
394 Q_D(QAbstractBarSeries);
394 Q_D(QAbstractBarSeries);
395 if (d->m_labelsVisible != visible) {
395 if (d->m_labelsVisible != visible) {
396 d->setLabelsVisible(visible);
396 d->setLabelsVisible(visible);
397 emit labelsVisibleChanged();
397 emit labelsVisibleChanged();
398 }
398 }
399 }
399 }
400
400
401 /*!
401 /*!
402 Returns the visibility of labels
402 Returns the visibility of labels
403 */
403 */
404 bool QAbstractBarSeries::isLabelsVisible() const
404 bool QAbstractBarSeries::isLabelsVisible() const
405 {
405 {
406 Q_D(const QAbstractBarSeries);
406 Q_D(const QAbstractBarSeries);
407 return d->m_labelsVisible;
407 return d->m_labelsVisible;
408 }
408 }
409
409
410 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
410 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
411
411
412 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
412 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
413 QAbstractSeriesPrivate(q),
413 QAbstractSeriesPrivate(q),
414 m_barWidth(0.5), // Default value is 50% of category width
414 m_barWidth(0.5), // Default value is 50% of category width
415 m_labelsVisible(false),
415 m_labelsVisible(false),
416 m_visible(true)
416 m_visible(true),
417 m_blockBarUpdate(false)
417 {
418 {
418 }
419 }
419
420
420 int QAbstractBarSeriesPrivate::categoryCount() const
421 int QAbstractBarSeriesPrivate::categoryCount() const
421 {
422 {
422 // No categories defined. return count of longest set.
423 // No categories defined. return count of longest set.
423 int count = 0;
424 int count = 0;
424 for (int i = 0; i < m_barSets.count(); i++) {
425 for (int i = 0; i < m_barSets.count(); i++) {
425 if (m_barSets.at(i)->count() > count)
426 if (m_barSets.at(i)->count() > count)
426 count = m_barSets.at(i)->count();
427 count = m_barSets.at(i)->count();
427 }
428 }
428
429
429 return count;
430 return count;
430 }
431 }
431
432
432 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
433 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
433 {
434 {
434 if (width < 0.0)
435 if (width < 0.0)
435 width = 0.0;
436 width = 0.0;
436 m_barWidth = width;
437 m_barWidth = width;
437 emit updatedLayout();
438 emit updatedLayout();
438 }
439 }
439
440
440 qreal QAbstractBarSeriesPrivate::barWidth() const
441 qreal QAbstractBarSeriesPrivate::barWidth() const
441 {
442 {
442 return m_barWidth;
443 return m_barWidth;
443 }
444 }
444
445
445 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
446 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
446 {
447 {
447 return m_barSets.at(index);
448 return m_barSets.at(index);
448 }
449 }
449
450
450 void QAbstractBarSeriesPrivate::setVisible(bool visible)
451 void QAbstractBarSeriesPrivate::setVisible(bool visible)
451 {
452 {
452 m_visible = visible;
453 m_visible = visible;
453 emit visibleChanged();
454 emit visibleChanged();
454 }
455 }
455
456
456 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
457 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
457 {
458 {
458 m_labelsVisible = visible;
459 m_labelsVisible = visible;
459 emit labelsVisibleChanged(visible);
460 emit labelsVisibleChanged(visible);
460 }
461 }
461
462
462 qreal QAbstractBarSeriesPrivate::min()
463 qreal QAbstractBarSeriesPrivate::min()
463 {
464 {
464 if (m_barSets.count() <= 0)
465 if (m_barSets.count() <= 0)
465 return 0;
466 return 0;
466
467
467 qreal min = INT_MAX;
468 qreal min = INT_MAX;
468
469
469 for (int i = 0; i < m_barSets.count(); i++) {
470 for (int i = 0; i < m_barSets.count(); i++) {
470 int categoryCount = m_barSets.at(i)->count();
471 int categoryCount = m_barSets.at(i)->count();
471 for (int j = 0; j < categoryCount; j++) {
472 for (int j = 0; j < categoryCount; j++) {
472 qreal temp = m_barSets.at(i)->at(j);
473 qreal temp = m_barSets.at(i)->at(j);
473 if (temp < min)
474 if (temp < min)
474 min = temp;
475 min = temp;
475 }
476 }
476 }
477 }
477 return min;
478 return min;
478 }
479 }
479
480
480 qreal QAbstractBarSeriesPrivate::max()
481 qreal QAbstractBarSeriesPrivate::max()
481 {
482 {
482 if (m_barSets.count() <= 0)
483 if (m_barSets.count() <= 0)
483 return 0;
484 return 0;
484
485
485 qreal max = INT_MIN;
486 qreal max = INT_MIN;
486
487
487 for (int i = 0; i < m_barSets.count(); i++) {
488 for (int i = 0; i < m_barSets.count(); i++) {
488 int categoryCount = m_barSets.at(i)->count();
489 int categoryCount = m_barSets.at(i)->count();
489 for (int j = 0; j < categoryCount; j++) {
490 for (int j = 0; j < categoryCount; j++) {
490 qreal temp = m_barSets.at(i)->at(j);
491 qreal temp = m_barSets.at(i)->at(j);
491 if (temp > max)
492 if (temp > max)
492 max = temp;
493 max = temp;
493 }
494 }
494 }
495 }
495
496
496 return max;
497 return max;
497 }
498 }
498
499
499 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
500 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
500 {
501 {
501 if ((set < 0) || (set >= m_barSets.count()))
502 if ((set < 0) || (set >= m_barSets.count()))
502 return 0; // No set, no value.
503 return 0; // No set, no value.
503 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
504 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
504 return 0; // No category, no value.
505 return 0; // No category, no value.
505
506
506 return m_barSets.at(set)->at(category);
507 return m_barSets.at(set)->at(category);
507 }
508 }
508
509
509 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
510 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
510 {
511 {
511 if ((set < 0) || (set >= m_barSets.count()))
512 if ((set < 0) || (set >= m_barSets.count()))
512 return 0; // No set, no value.
513 return 0; // No set, no value.
513 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
514 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
514 return 0; // No category, no value.
515 return 0; // No category, no value.
515
516
516 qreal value = m_barSets.at(set)->at(category);
517 qreal value = m_barSets.at(set)->at(category);
517 qreal sum = categorySum(category);
518 qreal sum = categorySum(category);
518 if (qFuzzyCompare(sum, 0))
519 if (qFuzzyCompare(sum, 0))
519 return 0;
520 return 0;
520
521
521 return value / sum;
522 return value / sum;
522 }
523 }
523
524
524 qreal QAbstractBarSeriesPrivate::categorySum(int category)
525 qreal QAbstractBarSeriesPrivate::categorySum(int category)
525 {
526 {
526 qreal sum(0);
527 qreal sum(0);
527 int count = m_barSets.count(); // Count sets
528 int count = m_barSets.count(); // Count sets
528 for (int set = 0; set < count; set++) {
529 for (int set = 0; set < count; set++) {
529 if (category < m_barSets.at(set)->count())
530 if (category < m_barSets.at(set)->count())
530 sum += m_barSets.at(set)->at(category);
531 sum += m_barSets.at(set)->at(category);
531 }
532 }
532 return sum;
533 return sum;
533 }
534 }
534
535
535 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
536 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
536 {
537 {
537 qreal sum(0);
538 qreal sum(0);
538 int count = m_barSets.count(); // Count sets
539 int count = m_barSets.count(); // Count sets
539 for (int set = 0; set < count; set++) {
540 for (int set = 0; set < count; set++) {
540 if (category < m_barSets.at(set)->count())
541 if (category < m_barSets.at(set)->count())
541 sum += qAbs(m_barSets.at(set)->at(category));
542 sum += qAbs(m_barSets.at(set)->at(category));
542 }
543 }
543 return sum;
544 return sum;
544 }
545 }
545
546
546 qreal QAbstractBarSeriesPrivate::maxCategorySum()
547 qreal QAbstractBarSeriesPrivate::maxCategorySum()
547 {
548 {
548 qreal max = INT_MIN;
549 qreal max = INT_MIN;
549 int count = categoryCount();
550 int count = categoryCount();
550 for (int i = 0; i < count; i++) {
551 for (int i = 0; i < count; i++) {
551 qreal sum = categorySum(i);
552 qreal sum = categorySum(i);
552 if (sum > max)
553 if (sum > max)
553 max = sum;
554 max = sum;
554 }
555 }
555 return max;
556 return max;
556 }
557 }
557
558
558 qreal QAbstractBarSeriesPrivate::minX()
559 qreal QAbstractBarSeriesPrivate::minX()
559 {
560 {
560 if (m_barSets.count() <= 0)
561 if (m_barSets.count() <= 0)
561 return 0;
562 return 0;
562
563
563 qreal min = INT_MAX;
564 qreal min = INT_MAX;
564
565
565 for (int i = 0; i < m_barSets.count(); i++) {
566 for (int i = 0; i < m_barSets.count(); i++) {
566 int categoryCount = m_barSets.at(i)->count();
567 int categoryCount = m_barSets.at(i)->count();
567 for (int j = 0; j < categoryCount; j++) {
568 for (int j = 0; j < categoryCount; j++) {
568 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
569 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
569 if (temp < min)
570 if (temp < min)
570 min = temp;
571 min = temp;
571 }
572 }
572 }
573 }
573 return min;
574 return min;
574 }
575 }
575
576
576 qreal QAbstractBarSeriesPrivate::maxX()
577 qreal QAbstractBarSeriesPrivate::maxX()
577 {
578 {
578 if (m_barSets.count() <= 0)
579 if (m_barSets.count() <= 0)
579 return 0;
580 return 0;
580
581
581 qreal max = INT_MIN;
582 qreal max = INT_MIN;
582
583
583 for (int i = 0; i < m_barSets.count(); i++) {
584 for (int i = 0; i < m_barSets.count(); i++) {
584 int categoryCount = m_barSets.at(i)->count();
585 int categoryCount = m_barSets.at(i)->count();
585 for (int j = 0; j < categoryCount; j++) {
586 for (int j = 0; j < categoryCount; j++) {
586 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
587 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
587 if (temp > max)
588 if (temp > max)
588 max = temp;
589 max = temp;
589 }
590 }
590 }
591 }
591
592
592 return max;
593 return max;
593 }
594 }
594
595
595 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
596 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
596 {
597 {
597 // Returns top (sum of all positive values) of category.
598 // Returns top (sum of all positive values) of category.
598 // Returns 0, if all values are negative
599 // Returns 0, if all values are negative
599 qreal top(0);
600 qreal top(0);
600 int count = m_barSets.count();
601 int count = m_barSets.count();
601 for (int set = 0; set < count; set++) {
602 for (int set = 0; set < count; set++) {
602 if (category < m_barSets.at(set)->count()) {
603 if (category < m_barSets.at(set)->count()) {
603 qreal temp = m_barSets.at(set)->at(category);
604 qreal temp = m_barSets.at(set)->at(category);
604 if (temp > 0) {
605 if (temp > 0) {
605 top += temp;
606 top += temp;
606 }
607 }
607 }
608 }
608 }
609 }
609 return top;
610 return top;
610 }
611 }
611
612
612 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
613 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
613 {
614 {
614 // Returns bottom (sum of all negative values) of category
615 // Returns bottom (sum of all negative values) of category
615 // Returns 0, if all values are positive
616 // Returns 0, if all values are positive
616 qreal bottom(0);
617 qreal bottom(0);
617 int count = m_barSets.count();
618 int count = m_barSets.count();
618 for (int set = 0; set < count; set++) {
619 for (int set = 0; set < count; set++) {
619 if (category < m_barSets.at(set)->count()) {
620 if (category < m_barSets.at(set)->count()) {
620 qreal temp = m_barSets.at(set)->at(category);
621 qreal temp = m_barSets.at(set)->at(category);
621 if (temp < 0) {
622 if (temp < 0) {
622 bottom += temp;
623 bottom += temp;
623 }
624 }
624 }
625 }
625 }
626 }
626 return bottom;
627 return bottom;
627 }
628 }
628
629
629 qreal QAbstractBarSeriesPrivate::top()
630 qreal QAbstractBarSeriesPrivate::top()
630 {
631 {
631 // Returns top of all categories
632 // Returns top of all categories
632 qreal top(0);
633 qreal top(0);
633 int count = categoryCount();
634 int count = categoryCount();
634 for (int i = 0; i < count; i++) {
635 for (int i = 0; i < count; i++) {
635 qreal temp = categoryTop(i);
636 qreal temp = categoryTop(i);
636 if (temp > top)
637 if (temp > top)
637 top = temp;
638 top = temp;
638 }
639 }
639 return top;
640 return top;
640 }
641 }
641
642
642 qreal QAbstractBarSeriesPrivate::bottom()
643 qreal QAbstractBarSeriesPrivate::bottom()
643 {
644 {
644 // Returns bottom of all categories
645 // Returns bottom of all categories
645 qreal bottom(0);
646 qreal bottom(0);
646 int count = categoryCount();
647 int count = categoryCount();
647 for (int i = 0; i < count; i++) {
648 for (int i = 0; i < count; i++) {
648 qreal temp = categoryBottom(i);
649 qreal temp = categoryBottom(i);
649 if (temp < bottom)
650 if (temp < bottom)
650 bottom = temp;
651 bottom = temp;
651 }
652 }
652 return bottom;
653 return bottom;
653 }
654 }
654
655
656 bool QAbstractBarSeriesPrivate::blockBarUpdate()
657 {
658 return m_blockBarUpdate;
659 }
655
660
656 void QAbstractBarSeriesPrivate::initializeDomain()
661 void QAbstractBarSeriesPrivate::initializeDomain()
657 {
662 {
658 qreal minX(domain()->minX());
663 qreal minX(domain()->minX());
659 qreal minY(domain()->minY());
664 qreal minY(domain()->minY());
660 qreal maxX(domain()->maxX());
665 qreal maxX(domain()->maxX());
661 qreal maxY(domain()->maxY());
666 qreal maxY(domain()->maxY());
662
667
663 qreal seriesMinX = this->minX();
668 qreal seriesMinX = this->minX();
664 qreal seriesMaxX = this->maxX();
669 qreal seriesMaxX = this->maxX();
665 qreal y = max();
670 qreal y = max();
666 minX = qMin(minX, seriesMinX - (qreal)0.5);
671 minX = qMin(minX, seriesMinX - (qreal)0.5);
667 minY = qMin(minY, y);
672 minY = qMin(minY, y);
668 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
673 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
669 maxY = qMax(maxY, y);
674 maxY = qMax(maxY, y);
670
675
671 domain()->setRange(minX, maxX, minY, maxY);
676 domain()->setRange(minX, maxX, minY, maxY);
672 }
677 }
673
678
674 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
679 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
675 {
680 {
676 Q_Q(QAbstractBarSeries);
681 Q_Q(QAbstractBarSeries);
677 QList<QLegendMarker*> markers;
682 QList<QLegendMarker*> markers;
678
683
679 foreach(QBarSet* set, q->barSets()) {
684 foreach(QBarSet* set, q->barSets()) {
680 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
685 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
681 markers << marker;
686 markers << marker;
682 }
687 }
683 return markers;
688 return markers;
684 }
689 }
685
690
686
691
687 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
692 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
688 {
693 {
689 if ((m_barSets.contains(set)) || (set == 0))
694 if ((m_barSets.contains(set)) || (set == 0))
690 return false; // Fail if set is already in list or set is null.
695 return false; // Fail if set is already in list or set is null.
691
696
692 m_barSets.append(set);
697 m_barSets.append(set);
693 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
698 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
694 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
699 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
695 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
700 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
696
701
697 emit restructuredBars(); // this notifies barchartitem
702 emit restructuredBars(); // this notifies barchartitem
698 return true;
703 return true;
699 }
704 }
700
705
701 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
706 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
702 {
707 {
703 if (!m_barSets.contains(set))
708 if (!m_barSets.contains(set))
704 return false; // Fail if set is not in list
709 return false; // Fail if set is not in list
705
710
706 m_barSets.removeOne(set);
711 m_barSets.removeOne(set);
707 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
712 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
708 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
713 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
709 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
714 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
710
715
711 emit restructuredBars(); // this notifies barchartitem
716 emit restructuredBars(); // this notifies barchartitem
712 return true;
717 return true;
713 }
718 }
714
719
715 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
720 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
716 {
721 {
717 foreach (QBarSet *set, sets) {
722 foreach (QBarSet *set, sets) {
718 if ((set == 0) || (m_barSets.contains(set)))
723 if ((set == 0) || (m_barSets.contains(set)))
719 return false; // Fail if any of the sets is null or is already appended.
724 return false; // Fail if any of the sets is null or is already appended.
720 if (sets.count(set) != 1)
725 if (sets.count(set) != 1)
721 return false; // Also fail if same set is more than once in given list.
726 return false; // Also fail if same set is more than once in given list.
722 }
727 }
723
728
724 foreach (QBarSet *set, sets) {
729 foreach (QBarSet *set, sets) {
725 m_barSets.append(set);
730 m_barSets.append(set);
726 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
731 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
727 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
732 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
728 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
733 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
729 }
734 }
730
735
731 emit restructuredBars(); // this notifies barchartitem
736 emit restructuredBars(); // this notifies barchartitem
732 return true;
737 return true;
733 }
738 }
734
739
735 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
740 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
736 {
741 {
737 if (sets.count() == 0)
742 if (sets.count() == 0)
738 return false;
743 return false;
739
744
740 foreach (QBarSet *set, sets) {
745 foreach (QBarSet *set, sets) {
741 if ((set == 0) || (!m_barSets.contains(set)))
746 if ((set == 0) || (!m_barSets.contains(set)))
742 return false; // Fail if any of the sets is null or is not in series
747 return false; // Fail if any of the sets is null or is not in series
743 if (sets.count(set) != 1)
748 if (sets.count(set) != 1)
744 return false; // Also fail if same set is more than once in given list.
749 return false; // Also fail if same set is more than once in given list.
745 }
750 }
746
751
747 foreach (QBarSet *set, sets) {
752 foreach (QBarSet *set, sets) {
748 m_barSets.removeOne(set);
753 m_barSets.removeOne(set);
749 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
754 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
750 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
755 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
751 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
756 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
752 }
757 }
753
758
754 emit restructuredBars(); // this notifies barchartitem
759 emit restructuredBars(); // this notifies barchartitem
755
760
756 return true;
761 return true;
757 }
762 }
758
763
759 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
764 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
760 {
765 {
761 if ((m_barSets.contains(set)) || (set == 0))
766 if ((m_barSets.contains(set)) || (set == 0))
762 return false; // Fail if set is already in list or set is null.
767 return false; // Fail if set is already in list or set is null.
763
768
764 m_barSets.insert(index, set);
769 m_barSets.insert(index, set);
765 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
770 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
766 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
771 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
767 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
772 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
768
773
769 emit restructuredBars(); // this notifies barchartitem
774 emit restructuredBars(); // this notifies barchartitem
770 return true;
775 return true;
771 }
776 }
772
777
773 void QAbstractBarSeriesPrivate::initializeAxes()
778 void QAbstractBarSeriesPrivate::initializeAxes()
774 {
779 {
775 Q_Q(QAbstractBarSeries);
780 Q_Q(QAbstractBarSeries);
776
781
777 foreach(QAbstractAxis* axis, m_axes) {
782 foreach(QAbstractAxis* axis, m_axes) {
778
783
779 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
784 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
780 switch (q->type()) {
785 switch (q->type()) {
781 case QAbstractSeries::SeriesTypeHorizontalBar:
786 case QAbstractSeries::SeriesTypeHorizontalBar:
782 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
787 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
783 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
788 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
784 if (axis->orientation() == Qt::Vertical)
789 if (axis->orientation() == Qt::Vertical)
785 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
790 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
786 break;
791 break;
787 case QAbstractSeries::SeriesTypeBar:
792 case QAbstractSeries::SeriesTypeBar:
788 case QAbstractSeries::SeriesTypePercentBar:
793 case QAbstractSeries::SeriesTypePercentBar:
789 case QAbstractSeries::SeriesTypeStackedBar:
794 case QAbstractSeries::SeriesTypeStackedBar:
790 case QAbstractSeries::SeriesTypeBoxPlot:
795 case QAbstractSeries::SeriesTypeBoxPlot:
791 if (axis->orientation() == Qt::Horizontal)
796 if (axis->orientation() == Qt::Horizontal)
792 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
797 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
793 break;
798 break;
794 default:
799 default:
795 qWarning() << "Unexpected series type";
800 qWarning() << "Unexpected series type";
796 break;
801 break;
797 }
802 }
798 }
803 }
799 }
804 }
800 }
805 }
801
806
802 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
807 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
803 {
808 {
804 Q_Q(const QAbstractBarSeries);
809 Q_Q(const QAbstractBarSeries);
805
810
806 switch (q->type()) {
811 switch (q->type()) {
807 case QAbstractSeries::SeriesTypeHorizontalBar:
812 case QAbstractSeries::SeriesTypeHorizontalBar:
808 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
813 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
809 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
814 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
810 if (orientation == Qt::Vertical)
815 if (orientation == Qt::Vertical)
811 return QAbstractAxis::AxisTypeBarCategory;
816 return QAbstractAxis::AxisTypeBarCategory;
812 break;
817 break;
813 case QAbstractSeries::SeriesTypeBar:
818 case QAbstractSeries::SeriesTypeBar:
814 case QAbstractSeries::SeriesTypePercentBar:
819 case QAbstractSeries::SeriesTypePercentBar:
815 case QAbstractSeries::SeriesTypeStackedBar:
820 case QAbstractSeries::SeriesTypeStackedBar:
816 case QAbstractSeries::SeriesTypeBoxPlot:
821 case QAbstractSeries::SeriesTypeBoxPlot:
817 if (orientation == Qt::Horizontal)
822 if (orientation == Qt::Horizontal)
818 return QAbstractAxis::AxisTypeBarCategory;
823 return QAbstractAxis::AxisTypeBarCategory;
819 break;
824 break;
820 default:
825 default:
821 qWarning() << "Unexpected series type";
826 qWarning() << "Unexpected series type";
822 break;
827 break;
823 }
828 }
824 return QAbstractAxis::AxisTypeValue;
829 return QAbstractAxis::AxisTypeValue;
825
830
826 }
831 }
827
832
828 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
833 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
829 {
834 {
830 QStringList categories;
835 QStringList categories;
831 if (axis->categories().isEmpty()) {
836 if (axis->categories().isEmpty()) {
832 for (int i(1); i < categoryCount() + 1; i++)
837 for (int i(1); i < categoryCount() + 1; i++)
833 categories << QString::number(i);
838 categories << QString::number(i);
834 axis->append(categories);
839 axis->append(categories);
835 }
840 }
836 }
841 }
837
842
838 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
843 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
839 {
844 {
840 Q_UNUSED(orientation);
845 Q_UNUSED(orientation);
841 return 0;
846 return 0;
842 }
847 }
843
848
844 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
849 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
845 {
850 {
851 m_blockBarUpdate = true; // Ensures that the bars are not updated before the theme is ready
852
846 const QList<QGradient> gradients = theme->seriesGradients();
853 const QList<QGradient> gradients = theme->seriesGradients();
847
854
848 qreal takeAtPos = 0.5;
855 qreal takeAtPos = 0.5;
849 qreal step = 0.2;
856 qreal step = 0.2;
850 if (m_barSets.count() > 1) {
857 if (m_barSets.count() > 1) {
851 step = 1.0 / (qreal) m_barSets.count();
858 step = 1.0 / (qreal) m_barSets.count();
852 if (m_barSets.count() % gradients.count())
859 if (m_barSets.count() % gradients.count())
853 step *= gradients.count();
860 step *= gradients.count();
854 else
861 else
855 step *= (gradients.count() - 1);
862 step *= (gradients.count() - 1);
856 }
863 }
857
864
858 for (int i(0); i < m_barSets.count(); i++) {
865 for (int i(0); i < m_barSets.count(); i++) {
859 int colorIndex = (index + i) % gradients.count();
866 int colorIndex = (index + i) % gradients.count();
860 if (i > 0 && i %gradients.count() == 0) {
867 if (i > 0 && i %gradients.count() == 0) {
861 // There is no dedicated base color for each sets, generate more colors
868 // There is no dedicated base color for each sets, generate more colors
862 takeAtPos += step;
869 takeAtPos += step;
863 if (takeAtPos == 1.0)
870 if (takeAtPos == 1.0)
864 takeAtPos += step;
871 takeAtPos += step;
865 takeAtPos -= (int) takeAtPos;
872 takeAtPos -= (int) takeAtPos;
866 }
873 }
867 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_brush)
874 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_brush)
868 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
875 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
869
876
870 // Pick label color from the opposite end of the gradient.
877 // Pick label color from the opposite end of the gradient.
871 // 0.3 as a boundary seems to work well.
878 // 0.3 as a boundary seems to work well.
872 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_labelBrush) {
879 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_labelBrush) {
873 if (takeAtPos < 0.3)
880 if (takeAtPos < 0.3)
874 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
881 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
875 else
882 else
876 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
883 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
877 }
884 }
878
879 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->d_ptr->m_pen) {
885 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->d_ptr->m_pen) {
880 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
886 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
881 m_barSets.at(i)->setPen(c);
887 m_barSets.at(i)->setPen(c);
882 }
888 }
883 }
889 }
890 m_blockBarUpdate = false;
891 emit updatedBars();
884 }
892 }
885
893
886 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
894 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
887 {
895 {
888 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
896 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
889 Q_ASSERT(bar);
897 Q_ASSERT(bar);
890 if (bar->animation())
898 if (bar->animation())
891 bar->animation()->stopAndDestroyLater();
899 bar->animation()->stopAndDestroyLater();
892
900
893 if (options.testFlag(QChart::SeriesAnimations))
901 if (options.testFlag(QChart::SeriesAnimations))
894 bar->setAnimation(new BarAnimation(bar));
902 bar->setAnimation(new BarAnimation(bar));
895 else
903 else
896 bar->setAnimation(0);
904 bar->setAnimation(0);
897 QAbstractSeriesPrivate::initializeAnimations(options);
905 QAbstractSeriesPrivate::initializeAnimations(options);
898 }
906 }
899
907
900 #include "moc_qabstractbarseries.cpp"
908 #include "moc_qabstractbarseries.cpp"
901 #include "moc_qabstractbarseries_p.cpp"
909 #include "moc_qabstractbarseries_p.cpp"
902
910
903 QTCOMMERCIALCHART_END_NAMESPACE
911 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,113 +1,116
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
24 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QABSTRACTBARSERIES_P_H
30 #ifndef QABSTRACTBARSERIES_P_H
31 #define QABSTRACTBARSERIES_P_H
31 #define QABSTRACTBARSERIES_P_H
32
32
33 #include "qabstractbarseries.h"
33 #include "qabstractbarseries.h"
34 #include "qabstractseries_p.h"
34 #include "qabstractseries_p.h"
35 #include <QStringList>
35 #include <QStringList>
36 #include <QAbstractSeries>
36 #include <QAbstractSeries>
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 class QBarModelMapper;
40 class QBarModelMapper;
41 class QBarCategoryAxis;
41 class QBarCategoryAxis;
42 class QLegendMarker;
42 class QLegendMarker;
43
43
44 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
44 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
48 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
49 int categoryCount() const;
49 int categoryCount() const;
50
50
51 void setBarWidth(qreal width);
51 void setBarWidth(qreal width);
52 qreal barWidth() const;
52 qreal barWidth() const;
53
53
54 void setVisible(bool visible);
54 void setVisible(bool visible);
55 void setLabelsVisible(bool visible);
55 void setLabelsVisible(bool visible);
56
56
57 void initializeDomain();
57 void initializeDomain();
58 void initializeAxes();
58 void initializeAxes();
59 void initializeAnimations(QChart::AnimationOptions options);
59 void initializeAnimations(QChart::AnimationOptions options);
60 void initializeTheme(int index, ChartTheme* theme, bool forced = false);
60 void initializeTheme(int index, ChartTheme* theme, bool forced = false);
61
61
62 QList<QLegendMarker*> createLegendMarkers(QLegend *legend);
62 QList<QLegendMarker*> createLegendMarkers(QLegend *legend);
63
63
64 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
64 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
65 QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const;
65 QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const;
66
66
67 bool append(QBarSet *set);
67 bool append(QBarSet *set);
68 bool remove(QBarSet *set);
68 bool remove(QBarSet *set);
69 bool append(QList<QBarSet *> sets);
69 bool append(QList<QBarSet *> sets);
70 bool remove(QList<QBarSet *> sets);
70 bool remove(QList<QBarSet *> sets);
71 bool insert(int index, QBarSet *set);
71 bool insert(int index, QBarSet *set);
72
72
73 QBarSet *barsetAt(int index);
73 QBarSet *barsetAt(int index);
74 qreal min();
74 qreal min();
75 qreal max();
75 qreal max();
76 qreal valueAt(int set, int category);
76 qreal valueAt(int set, int category);
77 qreal percentageAt(int set, int category);
77 qreal percentageAt(int set, int category);
78 qreal categorySum(int category);
78 qreal categorySum(int category);
79 qreal absoluteCategorySum(int category);
79 qreal absoluteCategorySum(int category);
80 qreal maxCategorySum();
80 qreal maxCategorySum();
81 qreal minX();
81 qreal minX();
82 qreal maxX();
82 qreal maxX();
83 qreal categoryTop(int category);
83 qreal categoryTop(int category);
84 qreal categoryBottom(int category);
84 qreal categoryBottom(int category);
85 qreal top();
85 qreal top();
86 qreal bottom();
86 qreal bottom();
87
87
88 bool blockBarUpdate();
89
88 Q_SIGNALS:
90 Q_SIGNALS:
89 void clicked(int index, QBarSet *barset);
91 void clicked(int index, QBarSet *barset);
90 void updatedBars();
92 void updatedBars();
91 void updatedLayout();
93 void updatedLayout();
92 void restructuredBars();
94 void restructuredBars();
93 void labelsVisibleChanged(bool visible);
95 void labelsVisibleChanged(bool visible);
94 void visibleChanged();
96 void visibleChanged();
95
97
96 private:
98 private:
97 void populateCategories(QBarCategoryAxis *axis);
99 void populateCategories(QBarCategoryAxis *axis);
98
100
99 protected:
101 protected:
100 QList<QBarSet *> m_barSets;
102 QList<QBarSet *> m_barSets;
101 qreal m_barWidth;
103 qreal m_barWidth;
102 bool m_labelsVisible;
104 bool m_labelsVisible;
103 bool m_visible;
105 bool m_visible;
106 bool m_blockBarUpdate;
104
107
105 private:
108 private:
106 Q_DECLARE_PUBLIC(QAbstractBarSeries)
109 Q_DECLARE_PUBLIC(QAbstractBarSeries)
107 friend class HorizontalBarChartItem;
110 friend class HorizontalBarChartItem;
108 friend class BarChartItem;
111 friend class BarChartItem;
109 };
112 };
110
113
111 QTCOMMERCIALCHART_END_NAMESPACE
114 QTCOMMERCIALCHART_END_NAMESPACE
112
115
113 #endif // QABSTRACTBARSERIES_P_H
116 #endif // QABSTRACTBARSERIES_P_H
General Comments 0
You need to be logged in to leave comments. Login now