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