##// END OF EJS Templates
QCategoryAxis: some commented out code removed
Marek Rosa -
r1975:34236310382e
parent child
Show More
@@ -1,161 +1,151
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartcategoryaxisy_p.h"
22 22 #include "qcategoryaxis.h"
23 23 #include "qabstractaxis.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QFontMetrics>
27 27 #include <qmath.h>
28 28
29 29 static int label_padding = 5;
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
34 34 {
35 35 }
36 36
37 37 ChartCategoryAxisY::~ChartCategoryAxisY()
38 38 {
39 39 }
40 40
41 41 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
42 42 {
43 43 QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis);
44 44 int tickCount = axis->categoriesLabels().count() + 1;
45 45 QVector<qreal> points;
46 46
47 47 if (tickCount < 2)
48 48 return points;
49 49
50 50 qreal range = axis->max() - axis->min();
51 51 if (range > 0) {
52 52 points.resize(tickCount);
53 53 qreal scale = m_rect.height() / range;
54 54 for (int i = 0; i < tickCount; ++i)
55 55 if (i < tickCount - 1) {
56 56 int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.bottom();
57 57 points[i] = y;
58 58 } else {
59 59 int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.bottom();
60 60 points[i] = y;
61 61 }
62 62 }
63 63
64 64 return points;
65 65 }
66 66
67 67 void ChartCategoryAxisY::updateGeometry()
68 68 {
69 69 const QVector<qreal> &layout = ChartAxis::layout();
70 70 m_minWidth = 0;
71 71 m_minHeight = 0;
72 72
73 73 if(layout.isEmpty()) {
74 74 return;
75 75 }
76 76
77 77 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
78 78 QStringList ticksList = categoryAxis->categoriesLabels();
79 79
80 80 QList<QGraphicsItem *> lines = m_grid->childItems();
81 81 QList<QGraphicsItem *> labels = m_labels->childItems();
82 82 QList<QGraphicsItem *> shades = m_shades->childItems();
83 83 QList<QGraphicsItem *> axis = m_arrow->childItems();
84 84
85 // qreal height = 2*m_rect.bottom();
86
87 85 for (int i = 0; i < labels.count(); i++) {
88 86 labels.at(i)->setVisible(false);
89 87 }
90 88
91 89 // axis base line
92 90 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
93 91 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
94 92
95 93 for (int i = 0; i < layout.size(); ++i) {
96 94
97 95 // label items
98 96 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
99 97 if (i < ticksList.count()) {
100 98 labelItem->setText(ticksList.at(i));
101 99 }
102 100 const QRectF& rect = labelItem->boundingRect();
103 101
104 102 QPointF center = rect.center();
105 103 labelItem->setTransformOriginPoint(center.x(), center.y());
106 104
107 105 if (i < layout.size() - 1)
108 106 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
109 107 else
110 108 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
111 109
112 110 // check if the label should be shown
113 111 if (labelItem->pos().y() + center.y() < m_rect.top() || labelItem->pos().y() + center.y() > m_rect.bottom())
114 112 labelItem->setVisible(false);
115 113 else
116 114 labelItem->setVisible(true);
117 115
118 // if(labelItem->pos().y()+rect.height()>height) {
119 // labelItem->setVisible(false);
120 // }
121 // else {
122 // labelItem->setVisible(true);
123 // height=labelItem->pos().y();
124 // }
125
126 116 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
127 117 m_minHeight+=rect.height();
128 118
129 119 if ((i+1)%2 && i>1) {
130 120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
131 121 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
132 122 }
133 123
134 124 // grid lines and axis line ticks
135 125 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
136 126 lineItem->setPos(m_rect.left(), layout[i]);
137 127 lineItem->setLine(0, 0, m_rect.width(), 0);
138 128
139 129 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
140 130 tickLineItem->setPos(m_rect.left(), layout[i]);
141 131 tickLineItem->setLine(-5, 0, 0, 0);
142 132
143 133 // check if the grid line and the axis tick should be shown
144 134 if (lineItem->pos().y() < m_rect.top() || lineItem->pos().y() > m_rect.bottom()) {
145 135 lineItem->setVisible(false);
146 136 tickLineItem->setVisible(false);
147 137 } else {
148 138 lineItem->setVisible(true);
149 139 tickLineItem->setVisible(true);
150 140 }
151 141 }
152 142
153 143 }
154 144
155 145 void ChartCategoryAxisY::handleAxisUpdated()
156 146 {
157 147 updateGeometry();
158 148 ChartAxis::handleAxisUpdated();
159 149 }
160 150
161 151 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,301 +1,295
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qcategoryaxis.h"
22 22 #include "qcategoryaxis_p.h"
23 23 #include "chartcategoryaxisx_p.h"
24 24 #include "chartcategoryaxisy_p.h"
25 25 #include <qmath.h>
26 26 #include <QDebug>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29 /*!
30 30 \class QCategoryAxis
31 31 \brief The QCategoryAxis class allows putting a named ranges on the axis.
32 32 \mainclass
33 33
34 34 This class can be used when the underlying data needs to be given extra meaning.
35 35 Unlike with the QBarCategoryAxis the QCategoryAxis allows the categories ranges widths to be specified freely.
36 36
37 37 Example code on how to use QCategoryAxis.
38 38 \code
39 39 QChartView *chartView = new QChartView;
40 40 QLineSeries *series = new QLineSeries;
41 41 // ...
42 42 chartView->chart()->addSeries(series);
43 43
44 44 QCategoryAxis *axisX = new QCategoryAxis;
45 45 axisX->setStartValue(15);
46 46 axisX->append("First", 20);
47 47 axisX->append("Second", 37);
48 48 axisX->append("Third", 52);
49 49 chartView->chart()->setAxisX(series, axisX);
50 50 \endcode
51 51 */
52 52 /*!
53 53 \qmlclass CategoryAxis QCategoryAxis
54 54 \inherits AbstractAxis
55 55 \brief CategoryAxis allows putting a named ranges on the axis.
56 56
57 57 For example:
58 58 \code
59 59 CategoryAxis {
60 60 startValue: 0.0
61 61 CategoryRange { endValue: 1.0; label: "min (0-1)" }
62 62 CategoryRange { endValue: 3.0; label: "standard (1-3)" }
63 63 CategoryRange { endValue: 4.0; label: "high (3-4)" }
64 64 }
65 65 \endcode
66 66 */
67 67
68 68 /*!
69 69 \property QCategoryAxis::startValue
70 70 Defines the low end of the first category on the axis.
71 71 */
72 72 /*!
73 73 \qmlproperty int CategoryAxis::startValue
74 74 Defines the low end of the first category on the axis.
75 75 */
76 76
77 77 /*!
78 78 Constructs an axis object which is a child of \a parent.
79 79 */
80 80 QCategoryAxis::QCategoryAxis(QObject *parent):
81 81 QValueAxis(*new QCategoryAxisPrivate(this),parent)
82 82 {
83 83 }
84 84
85 85 /*!
86 86 Destroys the object
87 87 */
88 88 QCategoryAxis::~QCategoryAxis()
89 89 {
90 // Q_D(QValueAxis);
91 // if(d->m_dataset) {
92 // d->m_dataset->removeAxis(this);
93 // }
94 90 }
95 91
96 92 /*!
97 93 \internal
98 94 */
99 95 QCategoryAxis::QCategoryAxis(QCategoryAxisPrivate &d,QObject *parent):QValueAxis(d,parent)
100 96 {
101 97
102 98 }
103 99
104 100 /*!
105 101 \qmlmethod CategoryAxis::append(string label, real endValue)
106 102 Appends new category to the axis with an \a label. Category label has to be unique.
107 103 Parameter \a endValue specifies the high end limit of the category.
108 104 It has to be greater than the high end limit of the previous category.
109 105 Otherwise the method returns without adding a new category.
110 106 */
111 107 /*!
112 108 Appends new category to the axis with an \a categoryLabel.
113 109 Category label has to be unique.
114 110 Parameter \a categoryEndValue specifies the high end limit of the category.
115 111 It has to be greater than the high end limit of the previous category.
116 112 Otherwise the method returns without adding a new category.
117 113 */
118 114 void QCategoryAxis::append(const QString& categoryLabel, qreal categoryEndValue)
119 115 {
120 116 Q_D(QCategoryAxis);
121 117
122 118 if (!d->m_categories.contains(categoryLabel))
123 119 {
124 120 if(d->m_categories.isEmpty()){
125 121 Range range(d->m_categoryMinimum, categoryEndValue);
126 122 d->m_categoriesMap.insert(categoryLabel, range);
127 123 d->m_categories.append(categoryLabel);
128 124 }else if (categoryEndValue > endValue(d->m_categories.last())){
129 125 Range previousRange = d->m_categoriesMap.value(d->m_categories.last());
130 126 d->m_categoriesMap.insert(categoryLabel, Range(previousRange.second, categoryEndValue));
131 127 d->m_categories.append(categoryLabel);
132 128 }
133 129 }
134 130 }
135 131
136 132 /*!
137 133 Sets \a min to be the low end limit of the first category on the axis.
138 134 If there is already some categories added to the axis then passed value must be lower than the high end value of the already defined first category range.
139 135 Otherwise nothing is done.
140 136 */
141 137 void QCategoryAxis::setStartValue(qreal min)
142 138 {
143 139 Q_D(QCategoryAxis);
144 140 if(d->m_categories.isEmpty()){
145 141 d->m_categoryMinimum = min;
146 142 }else{
147 143 Range range = d->m_categoriesMap.value(d->m_categories.first());
148 144 if (min < range.second)
149 145 d->m_categoriesMap.insert(d->m_categories.first(), Range(min, range.second));
150 146 }
151 147 }
152 148
153 149 /*!
154 150 Returns the low end limit of the category specified by an \a categoryLabel
155 151 */
156 152 qreal QCategoryAxis::startValue(const QString& categoryLabel) const
157 153 {
158 154 Q_D(const QCategoryAxis);
159 155 if (categoryLabel.isEmpty())
160 156 return d->m_categoryMinimum;
161 157 else
162 158 return d->m_categoriesMap.value(categoryLabel).first;
163 159 }
164 160
165 161 /*!
166 162 Returns the high end limit of the interval specified by an \a categoryLabel
167 163 */
168 164 qreal QCategoryAxis::endValue(const QString& categoryLabel) const
169 165 {
170 166 Q_D(const QCategoryAxis);
171 167 return d->m_categoriesMap.value(categoryLabel).second;
172 168 }
173 169
174 170 /*!
175 171 \qmlmethod CategoryAxis::remove(string label)
176 172 Removes a category specified by the \a label from the axis
177 173 */
178 174 /*!
179 175 Removes an interval specified by the \a categoryLabel from the axis
180 176 */
181 177 void QCategoryAxis::remove(const QString &categoryLabel)
182 178 {
183 179 Q_D(QCategoryAxis);
184 180 int labelIndex = d->m_categories.indexOf(categoryLabel);
185 181
186 182 // check if such label exists
187 183 if (labelIndex != -1) {
188 184 d->m_categories.removeAt(labelIndex);
189 185 d->m_categoriesMap.remove(categoryLabel);
190 186
191 187 // the range of the interval that follows (if exists) needs to be updated
192 188 if (labelIndex < d->m_categories.count()) {
193 189 QString label = d->m_categories.at(labelIndex);
194 190 Range range = d->m_categoriesMap.value(label);
195 191
196 192 // set the range
197 193 if (labelIndex == 0) {
198 194 range.first = d->m_categoryMinimum;
199 195 d->m_categoriesMap.insert(label, range);
200 196 } else {
201 197 range.first = d->m_categoriesMap.value(d->m_categories.at(labelIndex - 1)).second;
202 198 d->m_categoriesMap.insert(label, range);
203 199 }
204 200 }
205 201 d->emitUpdated();
206 202 }
207 203 }
208 204
209 205 /*!
210 206 \qmlmethod CategoryAxis::replace(string oldLabel, string newLabel)
211 207 Replaces \a oldLabel of an existing category with a \a newLabel.
212 208 If the old label does not exist the method returns without making any changes.
213 209 */
214 210 /*!
215 211 Replaces \a oldLabel of an existing category with a \a newLabel
216 212 If the old label does not exist the method returns without making any changes.
217 213 */
218 214 void QCategoryAxis::replaceLabel(const QString& oldLabel, const QString& newLabel)
219 215 {
220 216 Q_D(QCategoryAxis);
221 217 int labelIndex = d->m_categories.indexOf(oldLabel);
222 218
223 219 // check if such label exists
224 220 if (labelIndex != -1) {
225 221 d->m_categories.replace(labelIndex, newLabel);
226 222 Range range = d->m_categoriesMap.value(oldLabel);
227 223 d->m_categoriesMap.remove(oldLabel);
228 224 d->m_categoriesMap.insert(newLabel, range);
229 225 d->emitUpdated();
230 226 }
231 227
232 228 }
233 229
234 230 /*!
235 231 Returns the list of the intervals labels
236 232 */
237 233 QStringList QCategoryAxis::categoriesLabels()
238 234 {
239 235 Q_D(QCategoryAxis);
240 236 return d->m_categories;
241 237 }
242 238
243 239 /*!
244 240 Returns number of intervals.
245 241 */
246 242 int QCategoryAxis::count() const
247 243 {
248 244 Q_D(const QCategoryAxis);
249 245 return d->m_categories.count();
250 246 }
251 247
252 248 /*!
253 249 Returns the type of the axis
254 250 */
255 251 QAbstractAxis::AxisType QCategoryAxis::type() const
256 252 {
257 253 return QAbstractAxis::AxisTypeCategory;
258 254 }
259 255
260 256 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
261 257
262 258 QCategoryAxisPrivate::QCategoryAxisPrivate(QCategoryAxis* q):
263 259 QValueAxisPrivate(q),
264 260 m_categoryMinimum(0)
265 261 {
266 262
267 263 }
268 264
269 265 QCategoryAxisPrivate::~QCategoryAxisPrivate()
270 266 {
271 267
272 268 }
273 269
274 270 int QCategoryAxisPrivate::ticksCount() const
275 271 {
276 272 return m_categories.count() + 1;
277 273 }
278 274
279 275 void QCategoryAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
280 276 {
281 277 Q_UNUSED(count);
282 278 Q_UNUSED(min);
283 279 Q_UNUSED(max);
284 //m_min = min;
285 //m_max = max;
286 280 }
287 281
288 282 ChartAxis* QCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
289 283 {
290 284 Q_Q(QCategoryAxis);
291 285 if(m_orientation == Qt::Vertical){
292 286 return new ChartCategoryAxisY(q,presenter);
293 287 }else{
294 288 return new ChartCategoryAxisX(q,presenter);
295 289 }
296 290 }
297 291
298 292 #include "moc_qcategoryaxis.cpp"
299 293 #include "moc_qcategoryaxis_p.cpp"
300 294
301 295 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now