##// END OF EJS Templates
Renamed barcategory grephics item class which has been previously ommited
Marek Rosa -
r1918:a404d0b7b27f
parent child
Show More
@@ -1,161 +1,161
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 "chartbarcategoryaxisx_p.h"
22 22 #include "chartpresenter_p.h"
23 23 #include "qbarcategoryaxis_p.h"
24 24 #include <QDebug>
25 25 #include <qmath.h>
26 26
27 27 static int label_padding = 5;
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 32 m_categoriesAxis(axis)
33 33 {
34 34
35 35 }
36 36
37 ChartCategoriesAxisX::~ChartCategoriesAxisX()
37 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
38 38 {
39 39 }
40 40
41 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
41 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
42 42 {
43 43 int count = m_categoriesAxis->d_ptr->count();
44 44
45 45 Q_ASSERT(count>=1);
46 46
47 47 QVector<qreal> points;
48 48 points.resize(count+2);
49 49
50 50 const qreal delta = m_rect.width()/(count);
51 51 qreal offset =-m_min-0.5;
52 52
53 53 if(delta<1) return points;
54 54
55 55 if(offset<=0) {
56 56 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta) + delta;
57 57 }
58 58 else {
59 59 offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta);
60 60 }
61 61
62 62 points[0] = m_rect.left();
63 63 points[count+1] = m_rect.right();
64 64
65 65 for (int i = 0; i < count; ++i) {
66 66 qreal x = offset + i * delta + m_rect.left();
67 67 points[i+1] = x;
68 68 }
69 69 return points;
70 70 }
71 71
72 QStringList ChartCategoriesAxisX::createCategoryLabels(const QVector<qreal>& layout) const
72 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
73 73 {
74 74 QStringList result;
75 75 qreal d = (m_max - m_min)/m_rect.width();
76 76 for (int i = 0;i < layout.count()-1; ++i) {
77 77 qreal x = qFloor((((layout[i+1] + layout[i])/2-m_rect.left())*d + m_min+0.5));
78 78 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
79 79 result << m_categoriesAxis->categories().at(x);
80 80 }
81 81 else {
82 82 // No label for x coordinate
83 83 result << "";
84 84 }
85 85 }
86 86 result << "";
87 87 return result;
88 88 }
89 89
90 90
91 void ChartCategoriesAxisX::updateGeometry()
91 void ChartBarCategoryAxisX::updateGeometry()
92 92 {
93 93 const QVector<qreal>& layout = ChartAxis::layout();
94 94
95 95 m_minWidth = 0;
96 96 m_minHeight = 0;
97 97
98 98 if(layout.isEmpty()) return;
99 99
100 100 QStringList ticksList = createCategoryLabels(layout);
101 101
102 102 QList<QGraphicsItem *> lines = m_grid->childItems();
103 103 QList<QGraphicsItem *> labels = m_labels->childItems();
104 104 QList<QGraphicsItem *> shades = m_shades->childItems();
105 105 QList<QGraphicsItem *> axis = m_arrow->childItems();
106 106
107 107 Q_ASSERT(labels.size() == ticksList.size());
108 108 Q_ASSERT(layout.size() == ticksList.size());
109 109
110 110 const qreal delta = m_rect.width()/(m_categoriesAxis->d_ptr->count());
111 111
112 112 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
113 113 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
114 114
115 115 qreal width = m_rect.left();
116 116 for (int i = 0; i < layout.size(); ++i) {
117 117 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
118 118 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
119 119 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
120 120 labelItem->setText(ticksList.at(i));
121 121 const QRectF& rect = labelItem->boundingRect();
122 122 QPointF center = rect.center();
123 123 labelItem->setTransformOriginPoint(center.x(), center.y());
124 124
125 125 if(i==0){
126 126 labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), m_rect.bottom() + label_padding);
127 127 }else{
128 128 labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_rect.bottom() + label_padding);
129 129 }
130 130
131 131 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) {
132 132 labelItem->setVisible(false);
133 133 }
134 134 else {
135 135 labelItem->setVisible(true);
136 136 width=rect.width()+labelItem->pos().x();
137 137 }
138 138
139 139 m_minWidth+=rect.width();
140 140 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
141 141
142 142 if ((i+1)%2 && i>1) {
143 143 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
144 144 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
145 145 }
146 146 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
147 147 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
148 148 }
149 149 }
150 150
151 void ChartCategoriesAxisX::handleAxisUpdated()
151 void ChartBarCategoryAxisX::handleAxisUpdated()
152 152 {
153 153 if(m_categoriesAxis->categories()!=m_categories)
154 154 {
155 155 m_categories=m_categoriesAxis->categories();
156 156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry();
157 157 }
158 158 ChartAxis::handleAxisUpdated();
159 159 }
160 160
161 161 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,64
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTCATEGORIESAXISX_H
31 31 #define CHARTCATEGORIESAXISX_H
32 32
33 33 #include "chartaxis_p.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QAbstractAxis;
38 38 class ChartPresenter;
39 39 class QBarCategoryAxis;
40 40
41 class ChartCategoriesAxisX : public ChartAxis
41 class ChartBarCategoryAxisX : public ChartAxis
42 42 {
43 43 public:
44 ChartCategoriesAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartCategoriesAxisX();
44 ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartBarCategoryAxisX();
46 46
47 47 AxisType axisType() const { return X_AXIS;}
48 48
49 49 protected:
50 50 QVector<qreal> calculateLayout() const;
51 51 void updateGeometry();
52 52 private:
53 53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
54 54 Q_SLOTS
55 55 void handleAxisUpdated();
56 56
57 57 private:
58 58 QStringList m_categories;
59 59 QBarCategoryAxis *m_categoriesAxis;
60 60 };
61 61
62 62 QTCOMMERCIALCHART_END_NAMESPACE
63 63
64 64 #endif /* CHARTCATEGORIESAXISX_H */
@@ -1,163 +1,163
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 "chartbarcategoryaxisy_p.h"
22 22 #include "chartpresenter_p.h"
23 23 #include "qbarcategoryaxis_p.h"
24 24 #include <qmath.h>
25 25 #include <QDebug>
26 26
27 27 static int label_padding = 5;
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
31 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 32 m_categoriesAxis(axis)
33 33 {
34 34 }
35 35
36 ChartCategoriesAxisY::~ChartCategoriesAxisY()
36 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
37 37 {
38 38 }
39 39
40 QVector<qreal> ChartCategoriesAxisY::calculateLayout() const
40 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
41 41 {
42 42 int count = m_categoriesAxis->d_ptr->count();
43 43
44 44 Q_ASSERT(count>=1);
45 45
46 46 QVector<qreal> points;
47 47 points.resize(count+2);
48 48
49 49 const qreal delta = m_rect.height()/(count);
50 50 qreal offset = - m_min - 0.5;
51 51
52 52 if(delta<1) return points;
53 53
54 54 if(offset<=0) {
55 55 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta) + delta;
56 56 }
57 57 else {
58 58 offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta);
59 59 }
60 60
61 61 points[0] = m_rect.bottom();
62 62 points[count+1] = m_rect.top();
63 63
64 64 for (int i = 0; i < count; ++i) {
65 65 int y = m_rect.bottom() - i * delta - offset;
66 66 points[i+1] = y;
67 67 }
68 68 return points;
69 69 }
70 70
71 QStringList ChartCategoriesAxisY::createCategoryLabels(const QVector<qreal>& layout) const
71 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
72 72 {
73 73 QStringList result;
74 74 qreal d = (m_max - m_min)/m_rect.height();
75 75 for (int i = 0;i < layout.count()-1; ++i) {
76 76 qreal x = qFloor(((m_rect.height()- (layout[i+1] + layout[i])/2 + m_rect.top())*d + m_min+0.5));
77 77 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
78 78 result << m_categoriesAxis->categories().at(x);
79 79 }
80 80 else {
81 81 // No label for x coordinate
82 82 result << "";
83 83 }
84 84 }
85 85 result << "";
86 86 return result;
87 87 }
88 88
89 void ChartCategoriesAxisY::updateGeometry()
89 void ChartBarCategoryAxisY::updateGeometry()
90 90 {
91 91 const QVector<qreal>& layout = ChartAxis::layout();
92 92
93 93 m_minWidth = 0;
94 94 m_minHeight = 0;
95 95
96 96 if(layout.isEmpty()) return;
97 97
98 98 QStringList ticksList = createCategoryLabels(layout);
99 99
100 100 QList<QGraphicsItem *> lines = m_grid->childItems();
101 101 QList<QGraphicsItem *> labels = m_labels->childItems();
102 102 QList<QGraphicsItem *> shades = m_shades->childItems();
103 103 QList<QGraphicsItem *> axis = m_arrow->childItems();
104 104
105 105 Q_ASSERT(labels.size() == ticksList.size());
106 106 Q_ASSERT(layout.size() == ticksList.size());
107 107
108 108 const qreal delta = m_rect.height()/(m_categoriesAxis->d_ptr->count());
109 109
110 110 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
111 111 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
112 112
113 113 qreal height = m_rect.bottom();
114 114 for (int i = 0; i < layout.size(); ++i) {
115 115 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
116 116 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
117 117 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
118 118 labelItem->setText(ticksList.at(i));
119 119 const QRectF& rect = labelItem->boundingRect();
120 120 QPointF center = rect.center();
121 121 labelItem->setTransformOriginPoint(center.x(), center.y());
122 122
123 123 if(i==0) {
124 124 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y());
125 125 }
126 126 else {
127 127 labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y());
128 128 }
129 129
130 130 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) {
131 131 labelItem->setVisible(false);
132 132 }
133 133 else {
134 134 labelItem->setVisible(true);
135 135 height=labelItem->pos().y();
136 136 }
137 137
138 138 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
139 139 m_minHeight+=rect.height();
140 140
141 141 if ((i+1)%2 && i>1) {
142 142 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
143 143 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
144 144 }
145 145 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
146 146 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
147 147 }
148 148 }
149 149
150 void ChartCategoriesAxisY::handleAxisUpdated()
150 void ChartBarCategoryAxisY::handleAxisUpdated()
151 151 {
152 152
153 153 if(m_categoriesAxis->categories()!=m_categories)
154 154 {
155 155 m_categories=m_categoriesAxis->categories();
156 156 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) {
157 157 updateGeometry();
158 158 }
159 159 }
160 160 ChartAxis::handleAxisUpdated();
161 161 }
162 162
163 163 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,63
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTCATEGORIESAXISY_H
31 31 #define CHARTCATEGORIESAXISY_H
32 32
33 33 #include "chartaxis_p.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QAbstractAxis;
38 38 class QBarCategoryAxis;
39 39 class ChartPresenter;
40 40
41 class ChartCategoriesAxisY : public ChartAxis
41 class ChartBarCategoryAxisY : public ChartAxis
42 42 {
43 43 public:
44 ChartCategoriesAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartCategoriesAxisY();
44 ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartBarCategoryAxisY();
46 46
47 47 AxisType axisType() const { return Y_AXIS;}
48 48
49 49 protected:
50 50 QVector<qreal> calculateLayout() const;
51 51 void updateGeometry();
52 52 private:
53 53 QStringList createCategoryLabels(const QVector<qreal>& layout) const;
54 54 Q_SLOTS
55 55 void handleAxisUpdated();
56 56 private:
57 57 QStringList m_categories;
58 58 QBarCategoryAxis *m_categoriesAxis;
59 59 };
60 60
61 61 QTCOMMERCIALCHART_END_NAMESPACE
62 62
63 63 #endif /* CHARTCATEGORIESAXISY_H */
@@ -1,487 +1,487
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 "qbarcategoryaxis.h"
22 22 #include "qbarcategoryaxis_p.h"
23 23 #include "chartbarcategoryaxisx_p.h"
24 24 #include "chartbarcategoryaxisy_p.h"
25 25 #include "domain_p.h"
26 26 #include "chartdataset_p.h"
27 27 #include <qmath.h>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 /*!
31 31 \class QBarCategoryAxis
32 32 \brief The QBarCategoryAxis class is used for manipulating chart's axis.
33 33 \mainclass
34 34
35 35 BarCategoryAxis can be setup to show axis line with tick marks, grid lines and shades.
36 36 Categories are drawn between ticks. Note that you can use this also with lineseries too.
37 37 See the \l {Line and BarChart Example} {Line and BarChart Example} to learn how to do that.
38 38
39 39 Example code on how to use QBarCategoryAxis.
40 40 \code
41 41 QChartView *chartView = new QChartView;
42 42 QBarSeries *series = new QBarSeries;
43 43 // ...
44 44 chartView->chart()->addSeries(series);
45 45 chartView->chart()->createDefaultAxes();
46 46
47 47 QBarCategoryAxis *axisX = new QBarCategoryAxis;
48 48 QStringList categories;
49 49 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
50 50 axisX->append(categories);
51 51 chartView->chart()->setAxisX(series, axisX);
52 52 \endcode
53 53 */
54 54
55 55 /*!
56 56 \qmlclass BarCategoryAxis QBarCategoryAxis
57 57 \inherits AbstractAxis
58 58 \brief The Axis element is used for manipulating chart's axes.
59 59
60 60 Axis can be setup to show axis line with tick marks, grid lines and shades.
61 61 Categories are drawn between ticks. Note that you can use this also with lineseries too.
62 62
63 63 To access BarCategoryAxis you can use ChartView API. For example:
64 64 \code
65 65 ChartView {
66 66 BarCategoryAxis {
67 67 id: categoryAxis
68 68 categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun" ]
69 69 }
70 70 // Add a few series...
71 71 }
72 72 \endcode
73 73 */
74 74
75 75 /*!
76 76 \property QBarCategoryAxis::categories
77 77 Defines the categories of axis
78 78 */
79 79 /*!
80 80 \qmlproperty QStringList BarCategoryAxis::categories
81 81 Defines the categories of axis
82 82 */
83 83
84 84 /*!
85 85 \property QBarCategoryAxis::min
86 86 Defines the minimum value on the axis.
87 87 */
88 88 /*!
89 89 \qmlproperty QString BarCategoryAxis::min
90 90 Defines the minimum value on the axis.
91 91 */
92 92
93 93 /*!
94 94 \property QBarCategoryAxis::max
95 95 Defines the maximum value on the axis.
96 96 */
97 97 /*!
98 98 \qmlproperty QString BarCategoryAxis::max
99 99 Defines the maximum value on the axis.
100 100 */
101 101
102 102
103 103 /*!
104 104 \fn void QBarCategoryAxis::categoriesChanged()
105 105 Axis emits signal when the categories of the axis has changed.
106 106 */
107 107 /*!
108 108 \fn void QBarCategoryAxis::minChanged(const QString &min)
109 109 Axis emits signal when \a min of axis has changed.
110 110 */
111 111 /*!
112 112 \qmlsignal BarCategoryAxis::onMinChanged(const QString &min)
113 113 Axis emits signal when \a min of axis has changed.
114 114 */
115 115
116 116 /*!
117 117 \fn void QBarCategoryAxis::maxChanged(const QString &max)
118 118 Axis emits signal when \a max of axis has changed.
119 119 */
120 120 /*!
121 121 \qmlsignal BarCategoryAxis::onMaxChanged(const QString &max)
122 122 Axis emits signal when \a max of axis has changed.
123 123 */
124 124
125 125 /*!
126 126 \fn void QBarCategoryAxis::rangeChanged(const QString &min, const QString &max)
127 127 Axis emits signal when \a min or \a max of axis has changed.
128 128 */
129 129
130 130 /*!
131 131 Constructs an axis object which is a child of \a parent.
132 132 */
133 133 QBarCategoryAxis::QBarCategoryAxis(QObject *parent):
134 134 QAbstractAxis(*new QBarCategoryAxisPrivate(this),parent)
135 135 {
136 136 }
137 137
138 138 /*!
139 139 Destroys the object
140 140 */
141 141 QBarCategoryAxis::~QBarCategoryAxis()
142 142 {
143 143 Q_D(QBarCategoryAxis);
144 144 if(d->m_dataset){
145 145 d->m_dataset->removeAxis(this);
146 146 }
147 147 }
148 148
149 149 /*!
150 150 \internal
151 151 */
152 152 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
153 153 {
154 154
155 155 }
156 156
157 157 /*!
158 158 Appends \a categories to axis
159 159 */
160 160 void QBarCategoryAxis::append(const QStringList &categories)
161 161 {
162 162 if(categories.isEmpty()) return;
163 163
164 164 Q_D(QBarCategoryAxis);
165 165 if (d->m_categories.isEmpty()) {
166 166 d->m_categories.append(categories);
167 167 setRange(categories.first(),categories.last());
168 168 }else{
169 169 d->m_categories.append(categories);
170 170 d->emitUpdated();
171 171 }
172 172 emit categoriesChanged();
173 173 }
174 174
175 175 /*!
176 176 Appends \a category to axis
177 177 */
178 178 void QBarCategoryAxis::append(const QString &category)
179 179 {
180 180 Q_D(QBarCategoryAxis);
181 181 if (d->m_categories.isEmpty()) {
182 182 d->m_categories.append(category);
183 183 setRange(category,category);
184 184 }else{
185 185 d->m_categories.append(category);
186 186 d->emitUpdated();
187 187 }
188 188 emit categoriesChanged();
189 189 }
190 190
191 191 /*!
192 192 Removes \a category from axis
193 193 */
194 194 void QBarCategoryAxis::remove(const QString &category)
195 195 {
196 196 Q_D(QBarCategoryAxis);
197 197 if (d->m_categories.contains(category)) {
198 198 d->m_categories.removeAt(d->m_categories.indexOf(category));
199 199 if(!d->m_categories.isEmpty())
200 200 setRange(d->m_categories.first(),d->m_categories.last());
201 201 else
202 202 setRange(QString::null,QString::null);
203 203 emit categoriesChanged();
204 204 }
205 205 }
206 206
207 207 /*!
208 208 Inserts \a category to axis at \a index
209 209 */
210 210 void QBarCategoryAxis::insert(int index, const QString &category)
211 211 {
212 212 Q_D(QBarCategoryAxis);
213 213 if (d->m_categories.isEmpty()) {
214 214 d->m_categories.insert(index,category);
215 215 setRange(category,category);
216 216 }else{
217 217 d->m_categories.insert(index,category);
218 218 d->emitUpdated();
219 219 }
220 220 emit categoriesChanged();
221 221 }
222 222
223 223 /*!
224 224 Replaces \a oldCategory with \a newCategory.
225 225 If \a oldCategory does not exits on the axis nothing is done.
226 226 */
227 227 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
228 228 {
229 229 Q_D(QBarCategoryAxis);
230 230 int pos = d->m_categories.indexOf(oldCategory);
231 231 if (pos != -1) {
232 232 d->m_categories.replace(pos, newCategory);
233 233 d->emitUpdated();
234 234 emit categoriesChanged();
235 235 }
236 236 }
237 237
238 238 /*!
239 239 Removes all categories.
240 240 */
241 241 void QBarCategoryAxis::clear()
242 242 {
243 243 Q_D(QBarCategoryAxis);
244 244 d->m_categories.clear();
245 245 setRange(QString::null,QString::null);
246 246 emit categoriesChanged();
247 247 }
248 248
249 249 void QBarCategoryAxis::setCategories(const QStringList &categories)
250 250 {
251 251 Q_D(QBarCategoryAxis);
252 252 if(d->m_categories!=categories) {
253 253 d->m_categories = categories;
254 254 setRange(categories.first(),categories.last());
255 255 emit categoriesChanged();
256 256 }
257 257 }
258 258
259 259 QStringList QBarCategoryAxis::categories()
260 260 {
261 261 Q_D(QBarCategoryAxis);
262 262 return d->m_categories;
263 263 }
264 264
265 265 /*!
266 266 Returns number of categories.
267 267 */
268 268 int QBarCategoryAxis::count() const
269 269 {
270 270 Q_D(const QBarCategoryAxis);
271 271 return d->m_categories.count();
272 272 }
273 273
274 274 /*!
275 275 Returns category at \a index. Index must be valid.
276 276 */
277 277 QString QBarCategoryAxis::at(int index) const
278 278 {
279 279 Q_D(const QBarCategoryAxis);
280 280 return d->m_categories.at(index);
281 281 }
282 282
283 283 /*!
284 284 Sets minimum category to \a min.
285 285 */
286 286 void QBarCategoryAxis::setMin(const QString& min)
287 287 {
288 288 Q_D(QBarCategoryAxis);
289 289 setRange(min,d->m_maxCategory);
290 290 }
291 291
292 292 /*!
293 293 Returns minimum category.
294 294 */
295 295 QString QBarCategoryAxis::min() const
296 296 {
297 297 Q_D(const QBarCategoryAxis);
298 298 return d->m_minCategory;
299 299 }
300 300
301 301 /*!
302 302 Sets maximum category to \a max.
303 303 */
304 304 void QBarCategoryAxis::setMax(const QString& max)
305 305 {
306 306 Q_D(QBarCategoryAxis);
307 307 setRange(d->m_minCategory,max);
308 308 }
309 309
310 310 /*!
311 311 Returns maximum category
312 312 */
313 313 QString QBarCategoryAxis::max() const
314 314 {
315 315 Q_D(const QBarCategoryAxis);
316 316 return d->m_maxCategory;
317 317 }
318 318
319 319 /*!
320 320 Sets range from \a minCategory to \a maxCategory
321 321 */
322 322 void QBarCategoryAxis::setRange(const QString& minCategory, const QString& maxCategory)
323 323 {
324 324 Q_D(QBarCategoryAxis);
325 325
326 326 bool changed = false;
327 327
328 328 //special case
329 329 if(minCategory.isNull() && maxCategory.isNull()){
330 330 d->m_minCategory = minCategory;
331 331 d->m_maxCategory = maxCategory;
332 332 d->m_min = 0;
333 333 d->m_max = 0;
334 334 emit minChanged(minCategory);
335 335 emit maxChanged(maxCategory);
336 336 d->m_count=0;
337 337 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
338 338 d->emitUpdated();
339 339 }
340 340
341 341 if(d->m_categories.indexOf(d->m_maxCategory)<d->m_categories.indexOf(d->m_minCategory)) return;
342 342
343 343 if (!minCategory.isEmpty() && d->m_minCategory!=minCategory && d->m_categories.contains(minCategory)) {
344 344 d->m_minCategory = minCategory;
345 345 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
346 346 changed = true;
347 347 emit minChanged(minCategory);
348 348 }
349 349
350 350 if (!maxCategory.isEmpty() && d->m_maxCategory!=maxCategory && d->m_categories.contains(maxCategory)) {
351 351 d->m_maxCategory = maxCategory;
352 352 d->m_max = d->m_categories.indexOf(d->m_maxCategory) + 0.5;
353 353 changed = true;
354 354 emit maxChanged(maxCategory);
355 355 }
356 356
357 357 if (changed) {
358 358 d->m_count=d->m_max - d->m_min;
359 359 emit rangeChanged(d->m_minCategory,d->m_maxCategory);
360 360 d->emitUpdated();
361 361 }
362 362 }
363 363
364 364 /*!
365 365 Returns the type of the axis
366 366 */
367 367 QAbstractAxis::AxisType QBarCategoryAxis::type() const
368 368 {
369 369 return AxisTypeBarCategory;
370 370 }
371 371
372 372 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
373 373
374 374 QBarCategoryAxisPrivate::QBarCategoryAxisPrivate(QBarCategoryAxis* q):
375 375 QAbstractAxisPrivate(q),
376 376 m_min(0.0),
377 377 m_max(0.0),
378 378 m_count(0)
379 379 {
380 380
381 381 }
382 382
383 383 QBarCategoryAxisPrivate::~QBarCategoryAxisPrivate()
384 384 {
385 385
386 386 }
387 387
388 388 void QBarCategoryAxisPrivate::setMin(const QVariant &min)
389 389 {
390 390 setRange(min,m_maxCategory);
391 391 }
392 392
393 393 void QBarCategoryAxisPrivate::setMax(const QVariant &max)
394 394 {
395 395 setRange(m_minCategory,max);
396 396 }
397 397
398 398 void QBarCategoryAxisPrivate::setRange(const QVariant &min, const QVariant &max)
399 399 {
400 400 Q_Q(QBarCategoryAxis);
401 401 QString value1 = min.toString();
402 402 QString value2 = max.toString();
403 403 q->setRange(value1,value2);
404 404 }
405 405
406 406 void QBarCategoryAxisPrivate::handleDomainUpdated()
407 407 {
408 408 Q_Q(QBarCategoryAxis);
409 409 Domain* domain = qobject_cast<Domain*>(sender());
410 410
411 411 if(m_orientation==Qt::Horizontal) {
412 412 m_min = domain->minX();
413 413 m_max = domain->maxX();
414 414 }
415 415 else if(m_orientation==Qt::Vertical) {
416 416 m_min = domain->minY();
417 417 m_max = domain->maxY();
418 418 }
419 419
420 420 bool changed = false;
421 421
422 422 int min = m_min + 0.5;
423 423 if(min>=0 && min<m_categories.count()) {
424 424 QString minCategory = m_categories.at(min);
425 425 if(m_minCategory!=minCategory && !minCategory.isEmpty()) {
426 426 m_minCategory=minCategory;
427 427 changed=true;
428 428 emit q->minChanged(minCategory);
429 429 }
430 430 }
431 431 int max = m_max - 0.5;
432 432 if(max>=0 && max<m_categories.count()) {
433 433 QString maxCategory = m_categories.at(max);
434 434 if(m_maxCategory!=maxCategory && !maxCategory.isEmpty()) {
435 435 m_maxCategory=maxCategory;
436 436 emit q->maxChanged(maxCategory);
437 437 }
438 438 }
439 439
440 440 if (changed) {
441 441 emit q->rangeChanged(m_minCategory,m_maxCategory);
442 442 }
443 443 }
444 444
445 445 ChartAxis* QBarCategoryAxisPrivate::createGraphics(ChartPresenter* presenter)
446 446 {
447 447 Q_Q(QBarCategoryAxis);
448 448 if(m_orientation == Qt::Vertical){
449 return new ChartCategoriesAxisY(q,presenter);
449 return new ChartBarCategoryAxisY(q,presenter);
450 450 }else{
451 return new ChartCategoriesAxisX(q,presenter);
451 return new ChartBarCategoryAxisX(q,presenter);
452 452 }
453 453 }
454 454
455 455 void QBarCategoryAxisPrivate::intializeDomain(Domain* domain)
456 456 {
457 457
458 458 Q_Q(QBarCategoryAxis);
459 459 if(m_max==m_min) {
460 460 int min;
461 461 int max;
462 462 if(m_orientation==Qt::Vertical) {
463 463 min = domain->minY() + 0.5;
464 464 max = domain->maxY() - 0.5;
465 465 }
466 466 else {
467 467 min = domain->minX() + 0.5;
468 468 max = domain->maxX() - 0.5;
469 469 }
470 470
471 471 if(min>0 && min<m_categories.count() && max>0 && max<m_categories.count())
472 472 q->setRange(m_categories.at(min),m_categories.at(max));
473 473 }
474 474 else {
475 475 if(m_orientation==Qt::Vertical) {
476 476 domain->setRangeY(m_min, m_max);
477 477 }
478 478 else {
479 479 domain->setRangeX(m_min, m_max);
480 480 }
481 481 }
482 482 }
483 483
484 484 #include "moc_qbarcategoryaxis.cpp"
485 485 #include "moc_qbarcategoryaxis_p.cpp"
486 486
487 487 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,79
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 #ifndef QBARCATEGORIESAXIS_H
22 22 #define QBARCATEGORIESAXIS_H
23 23
24 24 #include "qabstractaxis.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 class QBarCategoryAxisPrivate;
29 29
30 30 class QTCOMMERCIALCHART_EXPORT QBarCategoryAxis : public QAbstractAxis
31 31 {
32 32 Q_OBJECT
33 33 Q_PROPERTY(QStringList categories READ categories WRITE setCategories NOTIFY categoriesChanged)
34 34 Q_PROPERTY(QString min READ min WRITE setMin NOTIFY minChanged)
35 35 Q_PROPERTY(QString max READ max WRITE setMax NOTIFY maxChanged)
36 36
37 37 public:
38 38 explicit QBarCategoryAxis(QObject *parent = 0);
39 39 ~QBarCategoryAxis();
40 40
41 41 protected:
42 42 QBarCategoryAxis(QBarCategoryAxisPrivate &d,QObject *parent = 0);
43 43
44 44 public:
45 45 AxisType type() const;
46 46 void append(const QStringList &categories);
47 47 void append(const QString &category);
48 48 void remove(const QString &category);
49 49 void insert(int index, const QString &category);
50 50 void replace(const QString &oldCategory, const QString &newCategory);
51 51 void clear();
52 52 void setCategories(const QStringList &categories);
53 53 QStringList categories();
54 54 int count() const;
55 55 QString at(int index) const;
56 56
57 57 //range handling
58 58 void setMin(const QString& minCategory);
59 59 QString min() const;
60 60 void setMax(const QString& maxCategory);
61 61 QString max() const;
62 62 void setRange(const QString& minCategory, const QString& maxCategory);
63 63
64 64 Q_SIGNALS:
65 65 void categoriesChanged();
66 66 void minChanged(const QString &min);
67 67 void maxChanged(const QString &max);
68 68 void rangeChanged(const QString &min, const QString &max);
69 69
70 70 private:
71 71 Q_DECLARE_PRIVATE(QBarCategoryAxis)
72 72 Q_DISABLE_COPY(QBarCategoryAxis)
73 friend class ChartCategoriesAxisX;
74 friend class ChartCategoriesAxisY;
73 friend class ChartBarCategoryAxisX;
74 friend class ChartBarCategoryAxisY;
75 75 };
76 76
77 77 QTCOMMERCIALCHART_END_NAMESPACE
78 78
79 79 #endif // QCATEGORIESAXIS_H
General Comments 0
You need to be logged in to leave comments. Login now