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