##// END OF EJS Templates
Refactors axis updateGeometry handling...
Michal Klocek -
r2111:80de54ed93f0
parent child
Show More
@@ -0,0 +1,136
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "horizontalaxis_p.h"
22 #include "qabstractaxis.h"
23 #include <QFontMetrics>
24 #include <QDebug>
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
28 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter):ChartAxis(axis,presenter)
29 {
30 }
31
32 HorizontalAxis::~HorizontalAxis()
33 {
34 }
35
36 void HorizontalAxis::updateGeometry()
37 {
38 const QVector<qreal>& layout = ChartAxis::layout();
39
40 if(layout.isEmpty()) return;
41
42 QStringList ticksList = labels();
43
44 QList<QGraphicsItem *> lines = lineItems();
45 QList<QGraphicsItem *> labels = labelItems();
46 QList<QGraphicsItem *> shades = shadeItems();
47 QList<QGraphicsItem *> axis = arrowItems();
48
49 Q_ASSERT(labels.size() == ticksList.size());
50 Q_ASSERT(layout.size() == ticksList.size());
51
52 const QRectF& axisRect = axisGeometry();
53 const QRectF& gridRect = gridGeometry();
54
55 //arrow
56 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
57
58 if (alignment()==Qt::AlignTop)
59 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
60 else if(alignment()==Qt::AlignBottom)
61 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
62
63 qreal width = 0;
64
65 for (int i = 0; i < layout.size(); ++i) {
66
67 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
68 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
69 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
70
71 //grid line
72 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
73
74 //label text
75 labelItem->setText(ticksList.at(i));
76 const QRectF& rect = labelItem->boundingRect();
77 QPointF center = rect.center();
78 labelItem->setTransformOriginPoint(center.x(), center.y());
79
80 //ticks and label position
81 if (alignment()==Qt::AlignTop){
82 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding());
83 tickItem->setLine(layout[i],axisRect.bottom(),layout[i],axisRect.bottom() - labelPadding());
84 }else if(alignment()==Qt::AlignBottom){
85 labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding());
86 tickItem->setLine(layout[i],axisRect.top(),layout[i],axisRect.top() + labelPadding());
87 }
88
89 if(labelBetweenTicks()&& i+1!=layout.size()) {
90 const qreal delta = (layout[i+1] - layout[i])/2;
91 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
92 }
93
94 //overlap detection
95 if(labelItem->pos().x() <= width ||
96 labelItem->pos().x() < axisRect.left() ||
97 labelItem->pos().x() + rect.width() > axisRect.right()) {
98 labelItem->setVisible(false);
99 gridItem->setVisible(false);
100 tickItem->setVisible(false);
101 }
102 else {
103 labelItem->setVisible(true);
104 gridItem->setVisible(true);
105 tickItem->setVisible(true);
106 width=rect.width()+labelItem->pos().x();
107 }
108
109 //shades
110 if ((i+1)%2 && i>1) {
111 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
112 rectItem->setRect(layout[i-1],gridRect.top(),layout[i]-layout[i-1],gridRect.height());
113 }
114
115 // check if the grid line and the axis tick should be shown
116 qreal x = gridItem->line().p1().x();
117 if (x < gridRect.left() || x > gridRect.right()) {
118 gridItem->setVisible(false);
119 tickItem->setVisible(false);
120 if( labelBetweenTicks() && ( labelItem->pos().x() < gridRect.left() || labelItem->pos().x() + rect.width() > gridRect.right()))
121 labelItem->setVisible(false);
122 }
123
124 }
125
126 //begin/end grid line in case labels between
127 if(labelBetweenTicks()) {
128 QGraphicsLineItem *gridLine;
129 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()));
130 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
131 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
132 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
133 }
134 }
135
136 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,49
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30 #ifndef HORIZONTALAXIS_P_H_
31 #define HORIZONTALAXIS_P_H_
32
33 #include "chartaxis_p.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class HorizontalAxis : public ChartAxis
38 {
39 public:
40 HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter);
41 ~HorizontalAxis();
42 protected:
43 void updateGeometry();
44
45 };
46
47 QTCOMMERCIALCHART_END_NAMESPACE
48
49 #endif
@@ -0,0 +1,146
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "verticalaxis_p.h"
22 #include "qabstractaxis.h"
23 #include <QFontMetrics>
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
27 VerticalAxis::VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter):ChartAxis(axis,presenter)
28 {
29
30 }
31
32 VerticalAxis::~VerticalAxis()
33 {
34
35 }
36
37 void VerticalAxis::updateGeometry()
38 {
39 const QVector<qreal> &layout = ChartAxis::layout();
40
41 if(layout.isEmpty()) return;
42
43 QStringList labelList = labels();
44
45 QList<QGraphicsItem *> lines = lineItems();
46 QList<QGraphicsItem *> labels = labelItems();
47 QList<QGraphicsItem *> shades = shadeItems();
48 QList<QGraphicsItem *> axis = arrowItems();
49
50 Q_ASSERT(labels.size() == labelList.size());
51 Q_ASSERT(layout.size() == labelList.size());
52
53 const QRectF& axisRect = axisGeometry();
54 const QRectF& gridRect = gridGeometry();
55
56 qreal height = axisRect.bottom();
57
58 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
59
60 if (alignment()==Qt::AlignLeft)
61 lineItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
62 else if(alignment()==Qt::AlignRight)
63 lineItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
64
65 QFontMetrics fn(font());
66
67 for (int i = 0; i < layout.size(); ++i) {
68
69 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
70 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
71 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
72
73 //grid line
74 gridItem->setLine( gridRect.left() , layout[i], gridRect.right(), layout[i]);
75
76 //label text
77 QString text = labelList.at(i);
78 if (fn.boundingRect(text).width() > axisRect.right() - axisRect.left() - labelPadding() )
79 {
80 QString label = text + "...";
81 while (fn.boundingRect(label).width() > axisRect.right() - axisRect.left() - labelPadding() && label.length() > 3)
82 label.remove(label.length() - 4, 1);
83 labelItem->setText(label);
84 }
85 else {
86 labelItem->setText(text);
87 }
88 const QRectF& rect = labelItem->boundingRect();
89 QPointF center = rect.center();
90 labelItem->setTransformOriginPoint(center.x(), center.y());
91
92 //ticks and label position
93 if (alignment()==Qt::AlignLeft) {
94 labelItem->setPos( axisRect.right() - rect.width() - labelPadding() , layout[i]-center.y());
95 tickItem->setLine( axisRect.right()- labelPadding(),layout[i], axisRect.right(),layout[i]);
96 }
97 else if(alignment()==Qt::AlignRight) {
98 labelItem->setPos( axisRect.left() + labelPadding() , layout[i]-center.y());
99 tickItem->setLine( axisRect.left(),layout[i], axisRect.left()+ labelPadding(),layout[i]);
100 }
101 if(labelBetweenTicks()&& i+1!=layout.size()) {
102 const qreal delta = (layout[i+1] - layout[i])/2;
103 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
104 }
105
106 //overlap detection
107 if(labelItem->pos().y() + rect.height() > height ||
108 labelItem->pos().y() + rect.height() > axisRect.bottom() ||
109 labelItem->pos().y() < axisRect.top()) {
110 labelItem->setVisible(false);
111 gridItem->setVisible(false);
112 tickItem->setVisible(false);
113 }
114 else {
115 labelItem->setVisible(true);
116 gridItem->setVisible(true);
117 height=labelItem->pos().y();
118 }
119
120 //shades
121 if ((i+1)%2 && i>1) {
122 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
123 rectItem->setRect( gridRect.left(),layout[i], gridRect.width(),layout[i-1]-layout[i]);
124 }
125
126 // check if the grid line and the axis tick should be shown
127 qreal y = gridItem->line().p1().y();
128 if (y < gridRect.top() || y > gridRect.bottom()) {
129 gridItem->setVisible(false);
130 tickItem->setVisible(false);
131 if( labelBetweenTicks() && ( labelItem->pos().y() < gridRect.top() || labelItem->pos().y() + rect.height() > gridRect.bottom()))
132 labelItem->setVisible(false);
133 }
134
135 }
136 //begin/end grid line in case labels between
137 if(labelBetweenTicks()) {
138 QGraphicsLineItem *gridLine;
139 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()));
140 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
141 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
142 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
143 }
144 }
145
146 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,49
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30 #ifndef VERTICALAXIS_P_H_
31 #define VERTICALAXIS_P_H_
32
33 #include "chartaxis_p.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class VerticalAxis : public ChartAxis
38 {
39 public:
40 VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter);
41 ~VerticalAxis();
42 protected:
43 void updateGeometry();
44
45 };
46
47 QTCOMMERCIALCHART_END_NAMESPACE
48
49 #endif /* VERTICALAXIS_P_H_ */
@@ -69,13 +69,13 void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayo
69 oldLayout.resize(newLayout.count());
69 oldLayout.resize(newLayout.count());
70
70
71 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
71 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
72 oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
72 oldLayout[i] = m_axis->orientation() == Qt::Horizontal ? rect.left() : rect.bottom();
73 oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
73 oldLayout[j] = m_axis->orientation() == Qt::Horizontal ? rect.right() : rect.top();
74 }
74 }
75 }
75 }
76 break;
76 break;
77 case ZoomInAnimation: {
77 case ZoomInAnimation: {
78 int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0);
78 int index = qMin(oldLayout.count() * (m_axis->orientation() == Qt::Horizontal ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0);
79 oldLayout.resize(newLayout.count());
79 oldLayout.resize(newLayout.count());
80
80
81 for(int i = 0; i < oldLayout.count(); i++)
81 for(int i = 0; i < oldLayout.count(); i++)
@@ -100,7 +100,7 void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayo
100 oldLayout.resize(newLayout.count());
100 oldLayout.resize(newLayout.count());
101 QRectF rect = m_axis->gridGeometry();
101 QRectF rect = m_axis->gridGeometry();
102 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
102 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
103 oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
103 oldLayout[i] = m_axis->orientation() == Qt::Horizontal ? rect.left() : rect.top();
104 }
104 }
105 break;
105 break;
106 }
106 }
@@ -13,6 +13,8 DEPENDPATH += $$PWD \
13 SOURCES += \
13 SOURCES += \
14 $$PWD/chartaxis.cpp \
14 $$PWD/chartaxis.cpp \
15 $$PWD/qabstractaxis.cpp \
15 $$PWD/qabstractaxis.cpp \
16 $$PWD/verticalaxis.cpp \
17 $$PWD/horizontalaxis.cpp \
16 $$PWD/valueaxis/chartvalueaxisx.cpp \
18 $$PWD/valueaxis/chartvalueaxisx.cpp \
17 $$PWD/valueaxis/chartvalueaxisy.cpp \
19 $$PWD/valueaxis/chartvalueaxisy.cpp \
18 $$PWD/valueaxis/qvalueaxis.cpp \
20 $$PWD/valueaxis/qvalueaxis.cpp \
@@ -21,11 +23,13 SOURCES += \
21 $$PWD/barcategoryaxis/qbarcategoryaxis.cpp \
23 $$PWD/barcategoryaxis/qbarcategoryaxis.cpp \
22 $$PWD/categoryaxis/chartcategoryaxisx.cpp \
24 $$PWD/categoryaxis/chartcategoryaxisx.cpp \
23 $$PWD/categoryaxis/chartcategoryaxisy.cpp \
25 $$PWD/categoryaxis/chartcategoryaxisy.cpp \
24 $$PWD/categoryaxis/qcategoryaxis.cpp
26 $$PWD/categoryaxis/qcategoryaxis.cpp
25
27
26 PRIVATE_HEADERS += \
28 PRIVATE_HEADERS += \
27 $$PWD/chartaxis_p.h \
29 $$PWD/chartaxis_p.h \
28 $$PWD/qabstractaxis_p.h \
30 $$PWD/qabstractaxis_p.h \
31 $$PWD/verticalaxis_p.h \
32 $$PWD/horizontalaxis_p.h \
29 $$PWD/valueaxis/chartvalueaxisx_p.h \
33 $$PWD/valueaxis/chartvalueaxisx_p.h \
30 $$PWD/valueaxis/chartvalueaxisy_p.h \
34 $$PWD/valueaxis/chartvalueaxisy_p.h \
31 $$PWD/valueaxis/qvalueaxis_p.h \
35 $$PWD/valueaxis/qvalueaxis_p.h \
@@ -25,14 +25,12
25 #include <QDebug>
25 #include <QDebug>
26 #include <qmath.h>
26 #include <qmath.h>
27
27
28 static int label_padding = 5;
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
29
32 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
30 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter),
33 m_categoriesAxis(axis)
31 m_categoriesAxis(axis)
34 {
32 {
35
33 setLabelBetweenTicks(true);
36 }
34 }
37
35
38 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
36 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
@@ -48,23 +46,22 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
48 QVector<qreal> points;
46 QVector<qreal> points;
49 points.resize(count+2);
47 points.resize(count+2);
50
48
51 const qreal delta = m_gridRect.width()/(count);
49 const QRectF& gridRect = gridGeometry();
52 qreal offset =-m_min-0.5;
50
51 const qreal delta = gridRect.width()/(count);
52 qreal offset =-min()-0.5;
53
53
54 if(delta<1) return points;
54 if(delta<1) return points;
55
55
56 if(offset<=0) {
56 if(offset<0) {
57 offset = int(offset * m_gridRect.width()/(m_max - m_min))%int(delta) + delta;
57 offset = int(offset * gridRect.width()/(max() - min()))%int(delta) + delta;
58 }
58 }
59 else {
59 else {
60 offset = int(offset * m_gridRect.width()/(m_max - m_min))%int(delta);
60 offset = int(offset * gridRect.width()/(max() - min()))%int(delta);
61 }
61 }
62
62
63 points[0] = m_gridRect.left();
63 for (int i = -1; i < count+1; ++i) {
64 points[count+1] = m_gridRect.right();
64 qreal x = offset + i * delta + gridRect.left();
65
66 for (int i = 0; i < count; ++i) {
67 qreal x = offset + i * delta + m_gridRect.left();
68 points[i+1] = x;
65 points[i+1] = x;
69 }
66 }
70 return points;
67 return points;
@@ -72,11 +69,12 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
72
69
73 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
70 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
74 {
71 {
75 QStringList result;
72 QStringList result ;
73 const QRectF& gridRect = gridGeometry();
76
74
77 qreal d = (m_max - m_min)/m_gridRect.width();
75 qreal d = (max() - min())/gridRect.width();
78 for (int i = 0;i < layout.count()-1; ++i) {
76 for (int i = 0;i < layout.count()-1; ++i) {
79 qreal x = qFloor((((layout[i+1] + layout[i])/2-m_gridRect.left())*d + m_min+0.5));
77 qreal x = qFloor((((layout[i] + layout[i+1])/2-gridRect.left())*d + min()+0.5));
80 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
78 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
81 result << m_categoriesAxis->categories().at(x);
79 result << m_categoriesAxis->categories().at(x);
82 }
80 }
@@ -93,55 +91,9 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& la
93 void ChartBarCategoryAxisX::updateGeometry()
91 void ChartBarCategoryAxisX::updateGeometry()
94 {
92 {
95 const QVector<qreal>& layout = ChartAxis::layout();
93 const QVector<qreal>& layout = ChartAxis::layout();
96
97 if(layout.isEmpty()) return;
94 if(layout.isEmpty()) return;
98
95 setLabels(createCategoryLabels(layout));
99 QStringList ticksList = createCategoryLabels(layout);
96 HorizontalAxis::updateGeometry();
100
101 QList<QGraphicsItem *> lines = m_grid->childItems();
102 QList<QGraphicsItem *> labels = m_labels->childItems();
103 QList<QGraphicsItem *> shades = m_shades->childItems();
104 QList<QGraphicsItem *> axis = m_arrow->childItems();
105
106 Q_ASSERT(labels.size() == ticksList.size());
107 Q_ASSERT(layout.size() == ticksList.size());
108
109 const qreal delta = m_gridRect.width()/(m_categoriesAxis->d_ptr->count());
110
111 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
112 lineItem->setLine( m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom());
113
114 qreal width = m_gridRect.left();
115 for (int i = 0; i < layout.size(); ++i) {
116 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
117 lineItem->setLine(layout[i], m_gridRect.top(), layout[i], m_gridRect.bottom());
118 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
119 labelItem->setText(ticksList.at(i));
120 const QRectF& rect = labelItem->boundingRect();
121 QPointF center = rect.center();
122 labelItem->setTransformOriginPoint(center.x(), center.y());
123
124 if(i==0){
125 labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), m_gridRect.bottom() + label_padding);
126 }else{
127 labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_gridRect.bottom() + label_padding);
128 }
129
130 if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()> m_gridRect.right()) {
131 labelItem->setVisible(false);
132 }
133 else {
134 labelItem->setVisible(true);
135 width=rect.width()+labelItem->pos().x();
136 }
137
138 if ((i+1)%2 && i>1) {
139 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
140 rectItem->setRect(layout[i-1], m_gridRect.top(),layout[i]-layout[i-1], m_gridRect.height());
141 }
142 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
143 lineItem->setLine(layout[i], m_gridRect.bottom(),layout[i], m_gridRect.bottom()+5);
144 }
145 }
97 }
146
98
147 void ChartBarCategoryAxisX::handleAxisUpdated()
99 void ChartBarCategoryAxisX::handleAxisUpdated()
@@ -158,7 +110,7 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constra
158 {
110 {
159 Q_UNUSED(constraint)
111 Q_UNUSED(constraint)
160
112
161 QFontMetrics fn(m_font);
113 QFontMetrics fn(font());
162 QSizeF sh;
114 QSizeF sh;
163 QSizeF base = ChartAxis::sizeHint(which, constraint);
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
164 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
@@ -168,7 +120,7 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constra
168 switch (which) {
120 switch (which) {
169 case Qt::MinimumSize:
121 case Qt::MinimumSize:
170 width = fn.boundingRect("...").width();
122 width = fn.boundingRect("...").width();
171 height = fn.height()+label_padding;
123 height = fn.height()+labelPadding();
172 width=qMax(width,base.width());
124 width=qMax(width,base.width());
173 height+=base.height();
125 height+=base.height();
174 sh = QSizeF(width,height);
126 sh = QSizeF(width,height);
@@ -179,7 +131,7 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constra
179 {
131 {
180 QRectF rect = fn.boundingRect(ticksList.at(i));
132 QRectF rect = fn.boundingRect(ticksList.at(i));
181 width+=rect.width();
133 width+=rect.width();
182 height=qMax(rect.height()+label_padding,height);
134 height=qMax(rect.height()+labelPadding(),height);
183 }
135 }
184 width=qMax(width,base.width());
136 width=qMax(width,base.width());
185 height+=base.height();
137 height+=base.height();
@@ -30,21 +30,19
30 #ifndef CHARTBARCATEGORYAXISX_H
30 #ifndef CHARTBARCATEGORYAXISX_H
31 #define CHARTBARCATEGORYAXISX_H
31 #define CHARTBARCATEGORYAXISX_H
32
32
33 #include "chartaxis_p.h"
33 #include "horizontalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
38 class ChartPresenter;
37 class ChartPresenter;
39 class QBarCategoryAxis;
38 class QBarCategoryAxis;
40
39
41 class ChartBarCategoryAxisX : public ChartAxis
40 class ChartBarCategoryAxisX : public HorizontalAxis
42 {
41 {
43 public:
42 public:
44 ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter);
43 ChartBarCategoryAxisX(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartBarCategoryAxisX();
44 ~ChartBarCategoryAxisX();
46
45
47 AxisType axisType() const { return X_AXIS;}
48 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
49 protected:
47 protected:
50 QVector<qreal> calculateLayout() const;
48 QVector<qreal> calculateLayout() const;
@@ -23,15 +23,13
23 #include "qbarcategoryaxis_p.h"
23 #include "qbarcategoryaxis_p.h"
24 #include <qmath.h>
24 #include <qmath.h>
25 #include <QFontMetrics>
25 #include <QFontMetrics>
26 #include <QDebug>
27
28 static int label_padding = 5;
29
26
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
28
32 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
29 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter),
33 m_categoriesAxis(axis)
30 m_categoriesAxis(axis)
34 {
31 {
32 setLabelBetweenTicks(true);
35 }
33 }
36
34
37 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
35 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
@@ -47,23 +45,22 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
47 QVector<qreal> points;
45 QVector<qreal> points;
48 points.resize(count+2);
46 points.resize(count+2);
49
47
50 const qreal delta = m_gridRect.height()/(count);
48 const QRectF& gridRect = gridGeometry();
51 qreal offset = - m_min - 0.5;
49
50 const qreal delta = gridRect.height()/(count);
51 qreal offset = - min() - 0.5;
52
52
53 if(delta<1) return points;
53 if(delta<1) return points;
54
54
55 if(offset<=0) {
55 if(offset<0) {
56 offset = int(offset * m_gridRect.height()/(m_max - m_min))%int(delta) + delta;
56 offset = int(offset * gridRect.height()/(max() - min()))%int(delta) + delta;
57 }
57 }
58 else {
58 else {
59 offset = int(offset * m_gridRect.height()/(m_max - m_min))%int(delta);
59 offset = int(offset * gridRect.height()/(max() - min()))%int(delta);
60 }
60 }
61
61
62 points[0] = m_gridRect.bottom();
62 for (int i = -1; i < count +1; ++i) {
63 points[count+1] = m_gridRect.top();
63 int y = gridRect.bottom() - i * delta - offset;
64
65 for (int i = 0; i < count; ++i) {
66 int y = m_gridRect.bottom() - i * delta - offset;
67 points[i+1] = y;
64 points[i+1] = y;
68 }
65 }
69 return points;
66 return points;
@@ -72,10 +69,10 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
72 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
69 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
73 {
70 {
74 QStringList result;
71 QStringList result;
75
72 const QRectF& gridRect = gridGeometry();
76 qreal d = (m_max - m_min)/m_gridRect.height();
73 qreal d = (max() - min())/gridRect.height();
77 for (int i = 0;i < layout.count()-1; ++i) {
74 for (int i = 0;i < layout.count()-1; ++i) {
78 qreal x = qFloor(((m_gridRect.height()- (layout[i+1] + layout[i])/2 + m_gridRect.top())*d + m_min+0.5));
75 qreal x = qFloor(((gridRect.height()- (layout[i+1] + layout[i])/2 + gridRect.top())*d + min()+0.5));
79 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
80 result << m_categoriesAxis->categories().at(x);
77 result << m_categoriesAxis->categories().at(x);
81 }
78 }
@@ -91,56 +88,9 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& la
91 void ChartBarCategoryAxisY::updateGeometry()
88 void ChartBarCategoryAxisY::updateGeometry()
92 {
89 {
93 const QVector<qreal>& layout = ChartAxis::layout();
90 const QVector<qreal>& layout = ChartAxis::layout();
94
95 if(layout.isEmpty()) return;
91 if(layout.isEmpty()) return;
96
92 setLabels(createCategoryLabels(layout));
97 QStringList ticksList = createCategoryLabels(layout);
93 VerticalAxis::updateGeometry();
98
99 QList<QGraphicsItem *> lines = m_grid->childItems();
100 QList<QGraphicsItem *> labels = m_labels->childItems();
101 QList<QGraphicsItem *> shades = m_shades->childItems();
102 QList<QGraphicsItem *> axis = m_arrow->childItems();
103
104 Q_ASSERT(labels.size() == ticksList.size());
105 Q_ASSERT(layout.size() == ticksList.size());
106
107 const qreal delta = m_gridRect.height()/(m_categoriesAxis->d_ptr->count());
108
109 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
110 lineItem->setLine(m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom());
111
112 qreal height = m_gridRect.bottom();
113 for (int i = 0; i < layout.size(); ++i) {
114 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
115 lineItem->setLine(m_gridRect.left() , layout[i], m_gridRect.right(), layout[i]);
116 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
117 labelItem->setText(ticksList.at(i));
118 const QRectF& rect = labelItem->boundingRect();
119 QPointF center = rect.center();
120 labelItem->setTransformOriginPoint(center.x(), center.y());
121
122 if(i==0) {
123 labelItem->setPos(m_gridRect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y());
124 }
125 else {
126 labelItem->setPos(m_gridRect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y());
127 }
128
129 if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_gridRect.top()) {
130 labelItem->setVisible(false);
131 }
132 else {
133 labelItem->setVisible(true);
134 height=labelItem->pos().y();
135 }
136
137 if ((i+1)%2 && i>1) {
138 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
139 rectItem->setRect(m_gridRect.left(),layout[i],m_gridRect.width(),layout[i-1]-layout[i]);
140 }
141 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
142 lineItem->setLine(m_gridRect.left()-5,layout[i],m_gridRect.left(),layout[i]);
143 }
144 }
94 }
145
95
146 void ChartBarCategoryAxisY::handleAxisUpdated()
96 void ChartBarCategoryAxisY::handleAxisUpdated()
@@ -160,7 +110,7 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constra
160 {
110 {
161 Q_UNUSED(constraint)
111 Q_UNUSED(constraint)
162
112
163 QFontMetrics fn(m_font);
113 QFontMetrics fn(font());
164 QSizeF sh;
114 QSizeF sh;
165 QSizeF base = ChartAxis::sizeHint(which, constraint);
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
166 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
@@ -169,7 +119,7 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constra
169
119
170 switch (which) {
120 switch (which) {
171 case Qt::MinimumSize:
121 case Qt::MinimumSize:
172 width = fn.boundingRect("...").width() + label_padding;
122 width = fn.boundingRect("...").width() + labelPadding();
173 height = fn.height();
123 height = fn.height();
174 width+=base.width();
124 width+=base.width();
175 height=qMax(height,base.height());
125 height=qMax(height,base.height());
@@ -181,7 +131,7 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constra
181 {
131 {
182 QRectF rect = fn.boundingRect(ticksList.at(i));
132 QRectF rect = fn.boundingRect(ticksList.at(i));
183 height+=rect.height();
133 height+=rect.height();
184 width=qMax(rect.width()+label_padding,width);
134 width=qMax(rect.width()+labelPadding(),width);
185 }
135 }
186 height=qMax(height,base.height());
136 height=qMax(height,base.height());
187 width+=base.width();
137 width+=base.width();
@@ -30,21 +30,19
30 #ifndef CHARTBARCATEGORYAXISY_H
30 #ifndef CHARTBARCATEGORYAXISY_H
31 #define CHARTBARCATEGORYAXISY_H
31 #define CHARTBARCATEGORYAXISY_H
32
32
33 #include "chartaxis_p.h"
33 #include "verticalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
38 class QBarCategoryAxis;
37 class QBarCategoryAxis;
39 class ChartPresenter;
38 class ChartPresenter;
40
39
41 class ChartBarCategoryAxisY : public ChartAxis
40 class ChartBarCategoryAxisY : public VerticalAxis
42 {
41 {
43 public:
42 public:
44 ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter);
43 ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter);
45 ~ChartBarCategoryAxisY();
44 ~ChartBarCategoryAxisY();
46
45
47 AxisType axisType() const { return Y_AXIS;}
48 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
49 protected:
47 protected:
50 QVector<qreal> calculateLayout() const;
48 QVector<qreal> calculateLayout() const;
@@ -26,12 +26,12
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 static int label_padding = 5;
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
30
33 ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
31 ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter),
32 m_axis(axis)
34 {
33 {
34 setLabelBetweenTicks(true);
35 }
35 }
36
36
37 ChartCategoryAxisX::~ChartCategoryAxisX()
37 ChartCategoryAxisX::~ChartCategoryAxisX()
@@ -40,99 +40,34 ChartCategoryAxisX::~ChartCategoryAxisX()
40
40
41 QVector<qreal> ChartCategoryAxisX::calculateLayout() const
41 QVector<qreal> ChartCategoryAxisX::calculateLayout() const
42 {
42 {
43 QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis);
43 int tickCount = m_axis->categoriesLabels().count() + 1;
44 int tickCount = axis->categoriesLabels().count() + 1;
45 QVector<qreal> points;
44 QVector<qreal> points;
46
45
47 if (tickCount < 2)
46 if (tickCount < 2)
48 return points;
47 return points;
49
48
50 qreal range = axis->max() - axis->min();
49 const QRectF& gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
51 if (range > 0) {
51 if (range > 0) {
52 points.resize(tickCount);
52 points.resize(tickCount);
53 qreal scale = m_gridRect.width() / range;
53 qreal scale = gridRect.width() / range;
54 for (int i = 0; i < tickCount; ++i)
54 for (int i = 0; i < tickCount; ++i)
55 if (i < tickCount - 1) {
55 if (i < tickCount - 1) {
56 int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_gridRect.left();
56 int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.left();
57 points[i] = x;
57 points[i] = x;
58 } else {
58 } else {
59 int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_gridRect.left();
59 int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.left();
60 points[i] = x;
60 points[i] = x;
61 }
61 }
62 }
62 }
63
63 return points;
64 return points;
64 }
65 }
65
66
66 void ChartCategoryAxisX::updateGeometry()
67 void ChartCategoryAxisX::updateGeometry()
67 {
68 {
68 const QVector<qreal>& layout = ChartAxis::layout();
69 setLabels(m_axis->categoriesLabels()<<"");
69
70 HorizontalAxis::updateGeometry();
70 if(layout.isEmpty()) return;
71
72 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
73 QStringList ticksList = categoryAxis->categoriesLabels();
74
75 QList<QGraphicsItem *> lines = m_grid->childItems();
76 QList<QGraphicsItem *> labels = m_labels->childItems();
77 QList<QGraphicsItem *> shades = m_shades->childItems();
78 QList<QGraphicsItem *> axis = m_arrow->childItems();
79
80
81 for (int i = 0; i < labels.count(); i++) {
82 labels.at(i)->setVisible(false);
83 }
84 // axis base line
85
86 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
87 lineItem->setLine(m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom());
88
89 for (int i = 0; i < layout.size(); ++i) {
90
91 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
92 if (i < ticksList.count()) {
93 labelItem->setText(ticksList.at(i));
94 }
95 const QRectF& rect = labelItem->boundingRect();
96 QPointF center = rect.center();
97 labelItem->setTransformOriginPoint(center.x(), center.y());
98
99 if (i < layout.size() - 1) {
100 labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_gridRect.bottom() + label_padding);
101 } else {
102 labelItem->setPos(layout[i] - center.x(), m_gridRect.bottom() + label_padding);
103 }
104
105 // check if the label should be shown
106 if (labelItem->pos().x() + center.x() < m_gridRect.left() || labelItem->pos().x() + center.x() > m_gridRect.right())
107 labelItem->setVisible(false);
108 else
109 labelItem->setVisible(true);
110
111 if ((i + 1) % 2 && i > 1) {
112 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1));
113 rectItem->setRect(layout[i - 1],m_gridRect.top(),layout[i]-layout[i - 1],m_gridRect.height());
114 }
115
116 // grid lines and axis line ticks
117 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
118 lineItem->setPos(layout[i], m_gridRect.top());
119 lineItem->setLine(0, 0, 0, m_gridRect.height());
120
121 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
122 tickLineItem->setPos(layout[i], m_gridRect.bottom());
123 tickLineItem->setLine(0, 0, 0, 5);
124
125 // check if the grid line and the axis tick should be shown
126 if (lineItem->pos().x() < m_gridRect.left() || lineItem->pos().x() > m_gridRect.right()) {
127 lineItem->setVisible(false);
128 tickLineItem->setVisible(false);
129 } else {
130 lineItem->setVisible(true);
131 tickLineItem->setVisible(true);
132 }
133
134 }
135
136 }
71 }
137
72
138 void ChartCategoryAxisX::handleAxisUpdated()
73 void ChartCategoryAxisX::handleAxisUpdated()
@@ -145,7 +80,7 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint
145 {
80 {
146 Q_UNUSED(constraint)
81 Q_UNUSED(constraint)
147
82
148 QFontMetrics fn(m_font);
83 QFontMetrics fn(font());
149 QSizeF sh;
84 QSizeF sh;
150 QSizeF base = ChartAxis::sizeHint(which, constraint);
85 QSizeF base = ChartAxis::sizeHint(which, constraint);
151 QStringList ticksList ; //TODO:
86 QStringList ticksList ; //TODO:
@@ -155,7 +90,7 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint
155 switch (which) {
90 switch (which) {
156 case Qt::MinimumSize:
91 case Qt::MinimumSize:
157 width = fn.boundingRect("...").width();
92 width = fn.boundingRect("...").width();
158 height = fn.height() + label_padding;
93 height = fn.height() + labelPadding();
159 width=qMax(width,base.width());
94 width=qMax(width,base.width());
160 height+=base.height();
95 height+=base.height();
161 sh = QSizeF(width,height);
96 sh = QSizeF(width,height);
@@ -166,7 +101,7 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint
166 {
101 {
167 QRectF rect = fn.boundingRect(ticksList.at(i));
102 QRectF rect = fn.boundingRect(ticksList.at(i));
168 width+=rect.width();
103 width+=rect.width();
169 height=qMax(rect.height()+label_padding,height);
104 height=qMax(rect.height()+labelPadding(),height);
170 }
105 }
171 width=qMax(width,base.width());
106 width=qMax(width,base.width());
172 height+=base.height();
107 height+=base.height();
@@ -30,27 +30,27
30 #ifndef CHARTCATEGORYAXISX_H
30 #ifndef CHARTCATEGORYAXISX_H
31 #define CHARTCATEGORYAXISX_H
31 #define CHARTCATEGORYAXISX_H
32
32
33 #include "chartaxis_p.h"
33 #include "horizontalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QCategoryAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartCategoryAxisX : public ChartAxis
40 class ChartCategoryAxisX : public HorizontalAxis
41 {
41 {
42 public:
42 public:
43 ChartCategoryAxisX(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter);
44 ~ChartCategoryAxisX();
44 ~ChartCategoryAxisX();
45
45
46 AxisType axisType() const { return X_AXIS; }
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 protected:
47 protected:
49 QVector<qreal> calculateLayout() const;
48 QVector<qreal> calculateLayout() const;
50 void updateGeometry();
49 void updateGeometry();
51
50 Q_SLOTS
52 Q_SLOTS
53 void handleAxisUpdated();
51 void handleAxisUpdated();
52 private:
53 QCategoryAxis *m_axis;
54 };
54 };
55
55
56 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
@@ -26,12 +26,12
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 static int label_padding = 5;
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
30
33 ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
31 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter),
32 m_axis(axis)
34 {
33 {
34 setLabelBetweenTicks(true);
35 }
35 }
36
36
37 ChartCategoryAxisY::~ChartCategoryAxisY()
37 ChartCategoryAxisY::~ChartCategoryAxisY()
@@ -40,23 +40,23 ChartCategoryAxisY::~ChartCategoryAxisY()
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 int tickCount = m_axis->categoriesLabels().count() + 1;
44 int tickCount = axis->categoriesLabels().count() + 1;
45 QVector<qreal> points;
44 QVector<qreal> points;
46
45
47 if (tickCount < 2)
46 if (tickCount < 2)
48 return points;
47 return points;
49
48
50 qreal range = axis->max() - axis->min();
49 const QRectF& gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
51 if (range > 0) {
51 if (range > 0) {
52 points.resize(tickCount);
52 points.resize(tickCount);
53 qreal scale = m_gridRect.height() / range;
53 qreal scale = gridRect.height() / range;
54 for (int i = 0; i < tickCount; ++i)
54 for (int i = 0; i < tickCount; ++i)
55 if (i < tickCount - 1) {
55 if (i < tickCount - 1) {
56 int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_gridRect.bottom();
56 int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.bottom();
57 points[i] = y;
57 points[i] = y;
58 } else {
58 } else {
59 int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_gridRect.bottom();
59 int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.bottom();
60 points[i] = y;
60 points[i] = y;
61 }
61 }
62 }
62 }
@@ -66,77 +66,8 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
66
66
67 void ChartCategoryAxisY::updateGeometry()
67 void ChartCategoryAxisY::updateGeometry()
68 {
68 {
69 const QVector<qreal> &layout = ChartAxis::layout();
69 setLabels(m_axis->categoriesLabels()<<"");
70
70 VerticalAxis::updateGeometry();
71 if(layout.isEmpty()) {
72 return;
73 }
74
75 QCategoryAxis *categoryAxis = qobject_cast<QCategoryAxis *>(m_chartAxis);
76 QStringList ticksList = categoryAxis->categoriesLabels();
77
78 QList<QGraphicsItem *> lines = m_grid->childItems();
79 QList<QGraphicsItem *> labels = m_labels->childItems();
80 QList<QGraphicsItem *> shades = m_shades->childItems();
81 QList<QGraphicsItem *> axis = m_arrow->childItems();
82
83 for (int i = 0; i < labels.count(); i++) {
84 labels.at(i)->setVisible(false);
85 }
86
87 // axis base line
88 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
89 lineItem->setLine(m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom());
90
91 for (int i = 0; i < layout.size(); ++i) {
92
93 // label items
94 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
95 if (i < ticksList.count()) {
96 labelItem->setText(ticksList.at(i));
97 }
98 const QRectF& rect = labelItem->boundingRect();
99
100 QPointF center = rect.center();
101 labelItem->setTransformOriginPoint(center.x(), center.y());
102
103
104 if (i < layout.size() - 1)
105 labelItem->setPos(m_gridRect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
106 else
107 labelItem->setPos(m_gridRect.left() - rect.width() - label_padding , layout[i]-center.y());
108
109 // check if the label should be shown
110 if (labelItem->pos().y() + center.y() < m_gridRect.top() || labelItem->pos().y() + center.y() > m_gridRect.bottom())
111 labelItem->setVisible(false);
112 else
113 labelItem->setVisible(true);
114
115 if ((i+1)%2 && i>1) {
116 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
117 rectItem->setRect(m_gridRect.left(),layout[i],m_gridRect.width(),layout[i-1]-layout[i]);
118 }
119
120 // grid lines and axis line ticks
121 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
122 lineItem->setPos(m_gridRect.left(), layout[i]);
123 lineItem->setLine(0, 0, m_gridRect.width(), 0);
124
125 QGraphicsLineItem *tickLineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
126 tickLineItem->setPos(m_gridRect.left(), layout[i]);
127 tickLineItem->setLine(-5, 0, 0, 0);
128
129 // check if the grid line and the axis tick should be shown
130 if (lineItem->pos().y() < m_gridRect.top() || lineItem->pos().y() > m_gridRect.bottom()) {
131 lineItem->setVisible(false);
132 tickLineItem->setVisible(false);
133 } else {
134 lineItem->setVisible(true);
135 tickLineItem->setVisible(true);
136 }
137
138 }
139
140 }
71 }
141
72
142 void ChartCategoryAxisY::handleAxisUpdated()
73 void ChartCategoryAxisY::handleAxisUpdated()
@@ -149,7 +80,7 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint
149 {
80 {
150 Q_UNUSED(constraint)
81 Q_UNUSED(constraint)
151
82
152 QFontMetrics fn(m_font);
83 QFontMetrics fn(font());
153 QSizeF sh;
84 QSizeF sh;
154 QSizeF base = ChartAxis::sizeHint(which, constraint);
85 QSizeF base = ChartAxis::sizeHint(which, constraint);
155 QStringList ticksList; //TODO::
86 QStringList ticksList; //TODO::
@@ -158,7 +89,7 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint
158
89
159 switch (which) {
90 switch (which) {
160 case Qt::MinimumSize:
91 case Qt::MinimumSize:
161 width = fn.boundingRect("...").width()+label_padding;
92 width = fn.boundingRect("...").width()+labelPadding();
162 height = fn.height();
93 height = fn.height();
163 width=qMax(width,base.width());
94 width=qMax(width,base.width());
164 height+=base.height();
95 height+=base.height();
@@ -170,7 +101,7 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint
170 {
101 {
171 QRectF rect = fn.boundingRect(ticksList.at(i));
102 QRectF rect = fn.boundingRect(ticksList.at(i));
172 height+=rect.height();
103 height+=rect.height();
173 width=qMax(rect.width()+label_padding,width);
104 width=qMax(rect.width()+labelPadding(),width);
174 }
105 }
175 height=qMax(height,base.height());
106 height=qMax(height,base.height());
176 width+=base.width();
107 width+=base.width();
@@ -30,27 +30,27
30 #ifndef CHARTCATEGORYAXISY_H
30 #ifndef CHARTCATEGORYAXISY_H
31 #define CHARTCATEGORYAXISY_H
31 #define CHARTCATEGORYAXISY_H
32
32
33 #include "chartaxis_p.h"
33 #include "verticalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QCategoryAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartCategoryAxisY : public ChartAxis
40 class ChartCategoryAxisY : public VerticalAxis
41 {
41 {
42 public:
42 public:
43 ChartCategoryAxisY(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter);
44 ~ChartCategoryAxisY();
44 ~ChartCategoryAxisY();
45
45
46 AxisType axisType() const { return Y_AXIS; }
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 protected:
47 protected:
49 QVector<qreal> calculateLayout() const;
48 QVector<qreal> calculateLayout() const;
50 void updateGeometry();
49 void updateGeometry();
51
50 Q_SLOTS
52 public Q_SLOTS:
53 void handleAxisUpdated();
51 void handleAxisUpdated();
52 private:
53 QCategoryAxis *m_axis;
54 };
54 };
55
55
56 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
@@ -43,7 +43,8 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartEleme
43 m_min(0),
43 m_min(0),
44 m_max(0),
44 m_max(0),
45 m_animation(0),
45 m_animation(0),
46 m_labelPadding(5)
46 m_labelPadding(5),
47 m_labelBetween(false)
47 {
48 {
48 //initial initialization
49 //initial initialization
49 m_arrow->setZValue(ChartPresenter::AxisZValue);
50 m_arrow->setZValue(ChartPresenter::AxisZValue);
@@ -76,12 +77,17 void ChartAxis::setLayout(QVector<qreal> &layout)
76 void ChartAxis::createItems(int count)
77 void ChartAxis::createItems(int count)
77 {
78 {
78 if (m_arrow->children().size() == 0)
79 if (m_arrow->children().size() == 0)
79 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
80 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
81 if (m_grid->children().size() == 0){
82 for(int i = 0 ; i < 2 ;i ++)
83 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
84 }
80 for (int i = 0; i < count; ++i) {
85 for (int i = 0; i < count; ++i) {
81 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
86 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
82 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
87 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
83 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
88 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
84 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
89 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2)
90 m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
85 }
91 }
86 }
92 }
87
93
@@ -409,36 +415,9 void ChartAxis::axisSelected()
409 //TODO: axis clicked;
415 //TODO: axis clicked;
410 }
416 }
411
417
412
418 void ChartAxis::setLabelBetweenTicks(bool enabled)
413 QStringList ChartAxis::createNumberLabels(qreal min, qreal max, int ticks) const
414 {
419 {
415 Q_ASSERT(max>min);
420 m_labelBetween=enabled;
416 Q_ASSERT(ticks>1);
417
418 QStringList labels;
419
420 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
421 n++;
422
423 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
424
425 QString format = axis->labelFormat();
426
427 if(format.isNull()) {
428 for (int i=0; i< ticks; i++) {
429 qreal value = min + (i * (max - min)/ (ticks-1));
430 labels << QString::number(value,'f',n);
431 }
432 }
433 else {
434 QByteArray array = format.toAscii();
435 for (int i=0; i< ticks; i++) {
436 qreal value = min + (i * (max - min)/ (ticks-1));
437 labels << QString().sprintf(array, value);
438 }
439 }
440
441 return labels;
442 }
421 }
443
422
444 Qt::Orientation ChartAxis::orientation() const
423 Qt::Orientation ChartAxis::orientation() const
@@ -456,6 +435,11 bool ChartAxis::isVisible()
456 return m_chartAxis->isVisible();
435 return m_chartAxis->isVisible();
457 }
436 }
458
437
438 void ChartAxis::setLabels(const QStringList& labels)
439 {
440 m_labelsList=labels;
441 }
442
459 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
443 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
460 {
444 {
461
445
@@ -492,6 +476,52 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
492 return sh;
476 return sh;
493 }
477 }
494
478
479 QStringList ChartAxis::createValueLabels(int ticks) const
480 {
481 Q_ASSERT(m_max>m_min);
482 Q_ASSERT(ticks>1);
483
484 QStringList labels;
485
486 int n = qMax(int(-qFloor(log10((m_max-m_min)/(ticks-1)))),0);
487 n++;
488
489 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
490
491 QString format = axis->labelFormat();
492
493 if(format.isNull()) {
494 for (int i=0; i< ticks; i++) {
495 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
496 labels << QString::number(value,'f',n);
497 }
498 }
499 else {
500 QByteArray array = format.toAscii();
501 for (int i=0; i< ticks; i++) {
502 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
503 labels << QString().sprintf(array, value);
504 }
505 }
506
507 return labels;
508 }
509
510 QStringList ChartAxis::createDateTimeLabels(const QString& format,int ticks) const
511 {
512 Q_ASSERT(m_max>m_min);
513 Q_ASSERT(ticks>1);
514 QStringList labels;
515 int n = qMax(int(-floor(log10((m_max-m_min)/(ticks-1)))),0);
516 n++;
517 for (int i=0; i< ticks; i++) {
518 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
519 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
520 }
521
522 return labels;
523 }
524
495 #include "moc_chartaxis_p.cpp"
525 #include "moc_chartaxis_p.cpp"
496
526
497 QTCOMMERCIALCHART_END_NAMESPACE
527 QTCOMMERCIALCHART_END_NAMESPACE
@@ -47,13 +47,10 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
47 Q_OBJECT
47 Q_OBJECT
48 Q_INTERFACES(QGraphicsLayoutItem)
48 Q_INTERFACES(QGraphicsLayoutItem)
49 public:
49 public:
50 enum AxisType { X_AXIS, Y_AXIS };
51
50
52 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
51 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
53 ~ChartAxis();
52 ~ChartAxis();
54
53
55 virtual AxisType axisType() const = 0;
56
57 void setArrowOpacity(qreal opacity);
54 void setArrowOpacity(qreal opacity);
58 qreal arrowOpacity() const;
55 qreal arrowOpacity() const;
59 void setArrowVisibility(bool visible);
56 void setArrowVisibility(bool visible);
@@ -106,20 +103,34 public:
106 QRectF axisGeometry() const { return m_axisRect; }
103 QRectF axisGeometry() const { return m_axisRect; }
107 QRectF gridGeometry() const { return m_gridRect; }
104 QRectF gridGeometry() const { return m_gridRect; }
108
105
106 void setLabels(const QStringList& labels);
107 QStringList labels() const { return m_labelsList; }
108
109 void setLabelBetweenTicks(bool enabled);
110 bool labelBetweenTicks() const { return m_labelBetween; }
111
109 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
112 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
110
113
111 protected:
114 protected:
112 void setGeometry(const QRectF &size){ Q_UNUSED(size);};
115 void setGeometry(const QRectF &size){ Q_UNUSED(size);};
113 virtual void updateGeometry() = 0;
116 virtual void updateGeometry() = 0;
114 virtual QVector<qreal> calculateLayout() const = 0;
117 virtual QVector<qreal> calculateLayout() const = 0;
115 QStringList createNumberLabels(qreal min, qreal max, int ticks) const;
116
118
119 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
120 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
121 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
122 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
123
124 QFont font() const { return m_font; }
125 qreal min() const {return m_min; }
126 qreal max() const {return m_max; }
127 QStringList createValueLabels(int ticks) const;
128 QStringList createDateTimeLabels(const QString& format,int ticks) const;
117
129
118 public Q_SLOTS:
130 public Q_SLOTS:
119 virtual void handleAxisUpdated();
131 virtual void handleAxisUpdated();
120 virtual void handleDomainUpdated();
132 virtual void handleDomainUpdated();
121
133
122
123 private:
134 private:
124 inline bool isEmpty();
135 inline bool isEmpty();
125 void createItems(int count);
136 void createItems(int count);
@@ -127,10 +138,9 private:
127 void updateLayout(QVector<qreal> &layout);
138 void updateLayout(QVector<qreal> &layout);
128 void axisSelected();
139 void axisSelected();
129
140
130 protected:
141 private:
131 QAbstractAxis *m_chartAxis;
142 QAbstractAxis *m_chartAxis;
132 int m_labelsAngle;
143 int m_labelsAngle;
133 //TODO: to be removed
134 QRectF m_axisRect;
144 QRectF m_axisRect;
135 QRectF m_gridRect;
145 QRectF m_gridRect;
136 QScopedPointer<QGraphicsItemGroup> m_grid;
146 QScopedPointer<QGraphicsItemGroup> m_grid;
@@ -145,9 +155,11 protected:
145 QFont m_font;
155 QFont m_font;
146 QString m_titleText;
156 QString m_titleText;
147 int m_labelPadding;
157 int m_labelPadding;
158 QStringList m_labelsList;
159 bool m_labelBetween;
148
160
149 friend class AxisAnimation;
161 friend class AxisAnimation;
150 friend class AxisItem;
162 friend class AxisItem;
151
163
152 };
164 };
153
165
@@ -170,7 +182,7 protected:
170 QPainterPath shape() const {
182 QPainterPath shape() const {
171 QPainterPath path = QGraphicsLineItem::shape();
183 QPainterPath path = QGraphicsLineItem::shape();
172 QRectF rect = path.boundingRect();
184 QRectF rect = path.boundingRect();
173 path.addRect(rect.adjusted(0, 0, m_axis->axisType() != ChartAxis::X_AXIS ? 8 : 0, m_axis->axisType() != ChartAxis::Y_AXIS ? 8 : 0));
185 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
174 return path;
186 return path;
175 }
187 }
176
188
@@ -31,8 +31,8 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartDateTimeAxisX::ChartDateTimeAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter),
35 m_tickCount(0)
35 m_tickCount(0),m_axis(axis)
36 {
36 {
37 }
37 }
38
38
@@ -40,31 +40,16 ChartDateTimeAxisX::~ChartDateTimeAxisX()
40 {
40 {
41 }
41 }
42
42
43 void ChartDateTimeAxisX::createLabels(QStringList &labels,qreal min, qreal max, int ticks)
44 {
45 Q_ASSERT(max>min);
46 Q_ASSERT(ticks>1);
47
48 QDateTimeAxis *axis = qobject_cast<QDateTimeAxis *>(m_chartAxis);
49
50 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
51 n++;
52 for (int i=0; i< ticks; i++) {
53 qreal value = min + (i * (max - min)/ (ticks-1));
54 labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format());
55 }
56 }
57
58 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
43 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
59 {
44 {
60 Q_ASSERT(m_tickCount>=2);
45 Q_ASSERT(m_tickCount>=2);
61
46
62 QVector<qreal> points;
47 QVector<qreal> points;
63 points.resize(m_tickCount);
48 points.resize(m_tickCount);
64
49 const QRectF& gridRect = gridGeometry();
65 const qreal deltaX = m_gridRect.width()/(m_tickCount-1);
50 const qreal deltaX = gridRect.width()/(m_tickCount-1);
66 for (int i = 0; i < m_tickCount; ++i) {
51 for (int i = 0; i < m_tickCount; ++i) {
67 int x = i * deltaX + m_gridRect.left();
52 int x = i * deltaX + gridRect.left();
68 points[i] = x;
53 points[i] = x;
69 }
54 }
70 return points;
55 return points;
@@ -73,58 +58,14 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
73 void ChartDateTimeAxisX::updateGeometry()
58 void ChartDateTimeAxisX::updateGeometry()
74 {
59 {
75 const QVector<qreal>& layout = ChartAxis::layout();
60 const QVector<qreal>& layout = ChartAxis::layout();
76
77 if(layout.isEmpty()) return;
61 if(layout.isEmpty()) return;
78
62 setLabels(createDateTimeLabels(m_axis->format(),layout.size()));
79 QStringList ticksList;
63 HorizontalAxis::updateGeometry();
80
81 createLabels(ticksList,m_min,m_max,layout.size());
82
83 QList<QGraphicsItem *> lines = m_grid->childItems();
84 QList<QGraphicsItem *> labels = m_labels->childItems();
85 QList<QGraphicsItem *> shades = m_shades->childItems();
86 QList<QGraphicsItem *> axis = m_arrow->childItems();
87
88 Q_ASSERT(labels.size() == ticksList.size());
89 Q_ASSERT(layout.size() == ticksList.size());
90
91 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
92 lineItem->setLine(m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom());
93
94 qreal width = 0;
95 for (int i = 0; i < layout.size(); ++i) {
96 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
97 lineItem->setLine(layout[i], m_gridRect.top(), layout[i], m_gridRect.bottom());
98 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
99 labelItem->setText(ticksList.at(i));
100 const QRectF& rect = labelItem->boundingRect();
101 QPointF center = rect.center();
102 labelItem->setTransformOriginPoint(center.x(), center.y());
103 labelItem->setPos(layout[i] - center.x(), m_gridRect.bottom() + label_padding);
104
105 if(labelItem->pos().x()<=width){
106 labelItem->setVisible(false);
107 lineItem->setVisible(false);
108 }else{
109 labelItem->setVisible(true);
110 lineItem->setVisible(true);
111 width=rect.width()+labelItem->pos().x();
112 }
113
114 if ((i+1)%2 && i>1) {
115 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
116 rectItem->setRect(layout[i-1],m_gridRect.top(),layout[i]-layout[i-1],m_gridRect.height());
117 }
118 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
119 lineItem->setLine(layout[i],m_gridRect.bottom(),layout[i],m_gridRect.bottom()+5);
120 }
121 }
64 }
122
65
123 void ChartDateTimeAxisX::handleAxisUpdated()
66 void ChartDateTimeAxisX::handleAxisUpdated()
124 {
67 {
125 //TODO:: fix this
68 m_tickCount = m_axis->tickCount();
126 QDateTimeAxis* axis = qobject_cast<QDateTimeAxis*>(m_chartAxis);
127 m_tickCount = axis->tickCount();
128 ChartAxis::handleAxisUpdated();
69 ChartAxis::handleAxisUpdated();
129 }
70 }
130
71
@@ -132,7 +73,7 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint
132 {
73 {
133 Q_UNUSED(constraint)
74 Q_UNUSED(constraint)
134
75
135 QFontMetrics fn(m_font);
76 QFontMetrics fn(font());
136 QSizeF sh;
77 QSizeF sh;
137
78
138 switch (which) {
79 switch (which) {
@@ -30,29 +30,28
30 #ifndef CHARTDATETIMEAXISX_H
30 #ifndef CHARTDATETIMEAXISX_H
31 #define CHARTDATETIMEAXISX_H
31 #define CHARTDATETIMEAXISX_H
32
32
33 #include "chartaxis_p.h"
33 #include "horizontalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QDateTimeAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartDateTimeAxisX : public ChartAxis
40 class ChartDateTimeAxisX : public HorizontalAxis
41 {
41 {
42 public:
42 public:
43 ChartDateTimeAxisX(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartDateTimeAxisX(QDateTimeAxis *axis, ChartPresenter *presenter);
44 ~ChartDateTimeAxisX();
44 ~ChartDateTimeAxisX();
45
45
46 AxisType axisType() const { return X_AXIS;}
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 protected:
47 protected:
49 void createLabels(QStringList &labels, qreal min, qreal max, int ticks);
50 void handleAxisUpdated();
48 void handleAxisUpdated();
51 QVector<qreal> calculateLayout() const;
49 QVector<qreal> calculateLayout() const;
52 void updateGeometry();
50 void updateGeometry();
53
51
54 private:
52 private:
55 int m_tickCount;
53 int m_tickCount;
54 QDateTimeAxis *m_axis;
56 };
55 };
57
56
58 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
@@ -31,8 +31,8 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartDateTimeAxisY::ChartDateTimeAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter),
35 m_tickCount(0)
35 m_tickCount(0),m_axis(axis)
36 {
36 {
37 }
37 }
38
38
@@ -40,31 +40,16 ChartDateTimeAxisY::~ChartDateTimeAxisY()
40 {
40 {
41 }
41 }
42
42
43 void ChartDateTimeAxisY::createLabels(QStringList &labels,qreal min, qreal max, int ticks)
44 {
45 Q_ASSERT(max>min);
46 Q_ASSERT(ticks>1);
47
48 QDateTimeAxis *axis = qobject_cast<QDateTimeAxis *>(m_chartAxis);
49
50 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
51 n++;
52 for (int i=0; i< ticks; i++) {
53 qreal value = min + (i * (max - min)/ (ticks-1));
54 labels << QDateTime::fromMSecsSinceEpoch(value).toString(axis->format());
55 }
56 }
57
58 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
43 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
59 {
44 {
60 Q_ASSERT(m_tickCount>=2);
45 Q_ASSERT(m_tickCount>=2);
61
46
62 QVector<qreal> points;
47 QVector<qreal> points;
63 points.resize(m_tickCount);
48 points.resize(m_tickCount);
64
49 const QRectF& gridRect = gridGeometry();
65 const qreal deltaY = m_gridRect.height()/(m_tickCount-1);
50 const qreal deltaY = gridRect.height()/(m_tickCount-1);
66 for (int i = 0; i < m_tickCount; ++i) {
51 for (int i = 0; i < m_tickCount; ++i) {
67 int y = i * -deltaY + m_gridRect.bottom();
52 int y = i * -deltaY + gridRect.bottom();
68 points[i] = y;
53 points[i] = y;
69 }
54 }
70
55
@@ -74,62 +59,14 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
74 void ChartDateTimeAxisY::updateGeometry()
59 void ChartDateTimeAxisY::updateGeometry()
75 {
60 {
76 const QVector<qreal> &layout = ChartAxis::layout();
61 const QVector<qreal> &layout = ChartAxis::layout();
77
78 if(layout.isEmpty()) return;
62 if(layout.isEmpty()) return;
79
63 setLabels(createDateTimeLabels(m_axis->format(),layout.size()));
80 QStringList ticksList;
64 VerticalAxis::updateGeometry();
81
82 createLabels(ticksList,m_min,m_max,layout.size());
83
84 QList<QGraphicsItem *> lines = m_grid->childItems();
85 QList<QGraphicsItem *> labels = m_labels->childItems();
86 QList<QGraphicsItem *> shades = m_shades->childItems();
87 QList<QGraphicsItem *> axis = m_arrow->childItems();
88
89 Q_ASSERT(labels.size() == ticksList.size());
90 Q_ASSERT(layout.size() == ticksList.size());
91
92 qreal height = m_gridRect.bottom();
93
94 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
95 lineItem->setLine(m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom());
96
97 for (int i = 0; i < layout.size(); ++i) {
98 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
99 lineItem->setLine(m_gridRect.left() , layout[i], m_gridRect.right(), layout[i]);
100 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
101
102 labelItem->setText(ticksList.at(i));
103 const QRectF& rect = labelItem->boundingRect();
104
105 QPointF center = rect.center();
106 labelItem->setTransformOriginPoint(center.x(), center.y());
107 labelItem->setPos(m_gridRect.left() - rect.width() - label_padding , layout[i]-center.y());
108
109 if(labelItem->pos().y()+rect.height()>height) {
110 labelItem->setVisible(false);
111 lineItem->setVisible(false);
112 }
113 else {
114 labelItem->setVisible(true);
115 lineItem->setVisible(true);
116 height=labelItem->pos().y();
117 }
118
119 if ((i+1)%2 && i>1) {
120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
121 rectItem->setRect(m_gridRect.left(),layout[i],m_gridRect.width(),layout[i-1]-layout[i]);
122 }
123 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
124 lineItem->setLine(m_gridRect.left()-5,layout[i],m_gridRect.left(),layout[i]);
125 }
126 }
65 }
127
66
128 void ChartDateTimeAxisY::handleAxisUpdated()
67 void ChartDateTimeAxisY::handleAxisUpdated()
129 {
68 {
130 //TODO:: fix this
69 m_tickCount = m_axis->tickCount();
131 QDateTimeAxis* axis = qobject_cast<QDateTimeAxis*>(m_chartAxis);
132 m_tickCount = axis->tickCount();
133 ChartAxis::handleAxisUpdated();
70 ChartAxis::handleAxisUpdated();
134 }
71 }
135
72
@@ -137,7 +74,7 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint
137 {
74 {
138 Q_UNUSED(constraint)
75 Q_UNUSED(constraint)
139
76
140 QFontMetrics fn(m_font);
77 QFontMetrics fn(font());
141 QSizeF sh;
78 QSizeF sh;
142
79
143 switch (which) {
80 switch (which) {
@@ -30,28 +30,27
30 #ifndef CHARTDATETIMEAXISY_H
30 #ifndef CHARTDATETIMEAXISY_H
31 #define CHARTDATETIMEAXISY_H
31 #define CHARTDATETIMEAXISY_H
32
32
33 #include "chartaxis_p.h"
33 #include "verticalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QDateTimeAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartDateTimeAxisY : public ChartAxis
40 class ChartDateTimeAxisY : public VerticalAxis
41 {
41 {
42 public:
42 public:
43 ChartDateTimeAxisY(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartDateTimeAxisY(QDateTimeAxis *axis, ChartPresenter *presenter);
44 ~ChartDateTimeAxisY();
44 ~ChartDateTimeAxisY();
45
45
46 AxisType axisType() const { return Y_AXIS;}
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 protected:
47 protected:
49 void createLabels(QStringList &labels, qreal min, qreal max, int ticks);
50 QVector<qreal> calculateLayout() const;
48 QVector<qreal> calculateLayout() const;
51 void updateGeometry();
49 void updateGeometry();
52 void handleAxisUpdated();
50 void handleAxisUpdated();
53 private:
51 private:
54 int m_tickCount;
52 int m_tickCount;
53 QDateTimeAxis *m_axis;
55 };
54 };
56
55
57 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
@@ -30,8 +30,8
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
33 ChartValueAxisX::ChartValueAxisX(QValueAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter),
34 m_tickCount(0)
34 m_tickCount(0),m_axis(axis)
35 {
35 {
36 }
36 }
37
37
@@ -46,9 +46,10 QVector<qreal> ChartValueAxisX::calculateLayout() const
46 QVector<qreal> points;
46 QVector<qreal> points;
47 points.resize(m_tickCount);
47 points.resize(m_tickCount);
48
48
49 const qreal deltaX = m_gridRect.width()/(m_tickCount-1);
49 const QRectF& gridRect = gridGeometry();
50 const qreal deltaX = gridRect.width()/(m_tickCount-1);
50 for (int i = 0; i < m_tickCount; ++i) {
51 for (int i = 0; i < m_tickCount; ++i) {
51 int x = i * deltaX + m_gridRect.left();
52 int x = i * deltaX + gridRect.left();
52 points[i] = x;
53 points[i] = x;
53 }
54 }
54 return points;
55 return points;
@@ -57,74 +58,15 QVector<qreal> ChartValueAxisX::calculateLayout() const
57 void ChartValueAxisX::updateGeometry()
58 void ChartValueAxisX::updateGeometry()
58 {
59 {
59 const QVector<qreal>& layout = ChartAxis::layout();
60 const QVector<qreal>& layout = ChartAxis::layout();
60
61 if(layout.isEmpty()) return;
61 if(layout.isEmpty()) return;
62
62 setLabels(createValueLabels(layout.size()));
63 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
63 HorizontalAxis::updateGeometry();
64
65 QList<QGraphicsItem *> lines = m_grid->childItems();
66 QList<QGraphicsItem *> labels = m_labels->childItems();
67 QList<QGraphicsItem *> shades = m_shades->childItems();
68 QList<QGraphicsItem *> axis = m_arrow->childItems();
69
70 Q_ASSERT(labels.size() == ticksList.size());
71 Q_ASSERT(layout.size() == ticksList.size());
72
73 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
74 //lineItem->setLine(m_gridRect.left(), m_gridRect.bottom(), m_gridRect.right(), m_gridRect.bottom());
75
76 if (m_chartAxis->alignment()==Qt::AlignTop)
77 lineItem->setLine(m_gridRect.left(), m_axisRect.bottom(), m_gridRect.right(), m_axisRect.bottom());
78 else if(m_chartAxis->alignment()==Qt::AlignBottom)
79 lineItem->setLine(m_gridRect.left(), m_axisRect.top(), m_gridRect.right(), m_axisRect.top());
80
81 qreal width = 0;
82 for (int i = 0; i < layout.size(); ++i) {
83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 lineItem->setLine(layout[i], m_gridRect.top(), layout[i], m_gridRect.bottom());
85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 labelItem->setText(ticksList.at(i));
87 const QRectF& rect = labelItem->boundingRect();
88 QPointF center = rect.center();
89 labelItem->setTransformOriginPoint(center.x(), center.y());
90 //labelItem->setPos(layout[i] - center.x(), m_gridRect.bottom() + label_padding);
91
92 if (m_chartAxis->alignment()==Qt::AlignTop)
93 labelItem->setPos(layout[i] - center.x(), m_axisRect.bottom() - rect.height() - labelPadding());
94 else if(m_chartAxis->alignment()==Qt::AlignBottom)
95 labelItem->setPos(layout[i] - center.x(), m_axisRect.top() + labelPadding());
96
97 if(labelItem->pos().x() <= width ||
98 labelItem->pos().x() < m_axisRect.left() ||
99 labelItem->pos().x() + rect.width() > m_axisRect.right()){
100 labelItem->setVisible(false);
101 lineItem->setVisible(false);
102 }else{
103 labelItem->setVisible(true);
104 lineItem->setVisible(true);
105 width=rect.width()+labelItem->pos().x();
106 }
107
108 if ((i+1)%2 && i>1) {
109 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
110 rectItem->setRect(layout[i-1],m_gridRect.top(),layout[i]-layout[i-1],m_gridRect.height());
111 }
112 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
113 //lineItem->setLine(layout[i],m_gridRect.bottom(),layout[i],m_gridRect.bottom()+5);
114
115 if (m_chartAxis->alignment()==Qt::AlignTop)
116 lineItem->setLine(layout[i],m_axisRect.bottom(),layout[i],m_axisRect.bottom() - labelPadding());
117 else if(m_chartAxis->alignment()==Qt::AlignBottom)
118 lineItem->setLine(layout[i],m_axisRect.top(),layout[i],m_axisRect.top() + labelPadding());
119
120 }
121 }
64 }
122
65
123 void ChartValueAxisX::handleAxisUpdated()
66 void ChartValueAxisX::handleAxisUpdated()
124 {
67 {
125 QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis);
68 if (m_tickCount != m_axis->tickCount()) {
126 if (m_tickCount != axis->tickCount()) {
69 m_tickCount = m_axis->tickCount();
127 m_tickCount = axis->tickCount();
128 presenter()->layout()->invalidate();
70 presenter()->layout()->invalidate();
129 }
71 }
130
72
@@ -135,11 +77,11 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c
135 {
77 {
136 Q_UNUSED(constraint)
78 Q_UNUSED(constraint)
137
79
138 QFontMetrics fn(m_font);
80 QFontMetrics fn(font());
139 QSizeF sh;
81 QSizeF sh;
140
82
141 QSizeF base = ChartAxis::sizeHint(which, constraint);
83 QSizeF base = ChartAxis::sizeHint(which, constraint);
142 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
84 QStringList ticksList = createValueLabels(m_tickCount);
143 qreal width=0;
85 qreal width=0;
144 qreal height=0;
86 qreal height=0;
145
87
@@ -30,20 +30,19
30 #ifndef CHARTVALUEAXISX_H
30 #ifndef CHARTVALUEAXISX_H
31 #define CHARTVALUEAXISX_H
31 #define CHARTVALUEAXISX_H
32
32
33 #include "chartaxis_p.h"
33 #include "horizontalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
38 class ChartPresenter;
37 class ChartPresenter;
38 class QValueAxis;
39
39
40 class ChartValueAxisX : public ChartAxis
40 class ChartValueAxisX : public HorizontalAxis
41 {
41 {
42 public:
42 public:
43 ChartValueAxisX(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartValueAxisX(QValueAxis *axis, ChartPresenter *presenter);
44 ~ChartValueAxisX();
44 ~ChartValueAxisX();
45
45
46 AxisType axisType() const { return X_AXIS;}
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48
47
49 protected:
48 protected:
@@ -53,7 +52,7 protected:
53
52
54 private:
53 private:
55 int m_tickCount;
54 int m_tickCount;
56
55 QValueAxis *m_axis;
57 };
56 };
58
57
59 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
@@ -29,8 +29,8
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
32 ChartValueAxisY::ChartValueAxisY(QValueAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter),
33 m_tickCount(0)
33 m_tickCount(0),m_axis(axis)
34 {
34 {
35 }
35 }
36
36
@@ -45,9 +45,11 QVector<qreal> ChartValueAxisY::calculateLayout() const
45 QVector<qreal> points;
45 QVector<qreal> points;
46 points.resize(m_tickCount);
46 points.resize(m_tickCount);
47
47
48 const qreal deltaY = m_gridRect.height()/(m_tickCount-1);
48 const QRectF& gridRect = gridGeometry();
49
50 const qreal deltaY = gridRect.height()/(m_tickCount-1);
49 for (int i = 0; i < m_tickCount; ++i) {
51 for (int i = 0; i < m_tickCount; ++i) {
50 int y = i * -deltaY + m_gridRect.bottom();
52 int y = i * -deltaY + gridRect.bottom();
51 points[i] = y;
53 points[i] = y;
52 }
54 }
53
55
@@ -57,89 +59,15 QVector<qreal> ChartValueAxisY::calculateLayout() const
57 void ChartValueAxisY::updateGeometry()
59 void ChartValueAxisY::updateGeometry()
58 {
60 {
59 const QVector<qreal> &layout = ChartAxis::layout();
61 const QVector<qreal> &layout = ChartAxis::layout();
60
61 if(layout.isEmpty()) return;
62 if(layout.isEmpty()) return;
62
63 setLabels(createValueLabels(layout.size()));
63 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
64 VerticalAxis::updateGeometry();
64
65 QList<QGraphicsItem *> lines = m_grid->childItems();
66 QList<QGraphicsItem *> labels = m_labels->childItems();
67 QList<QGraphicsItem *> shades = m_shades->childItems();
68 QList<QGraphicsItem *> axis = m_arrow->childItems();
69
70 Q_ASSERT(labels.size() == ticksList.size());
71 Q_ASSERT(layout.size() == ticksList.size());
72
73 qreal height = m_axisRect.bottom();
74
75 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
76 //lineItem->setLine( m_gridRect.left() , m_gridRect.top(), m_gridRect.left(), m_gridRect.bottom());
77
78 if (m_chartAxis->alignment()==Qt::AlignLeft)
79 lineItem->setLine( m_axisRect.right() , m_gridRect.top(), m_axisRect.right(), m_gridRect.bottom());
80 else if(m_chartAxis->alignment()==Qt::AlignRight)
81 lineItem->setLine( m_axisRect.left() , m_gridRect.top(), m_axisRect.left(), m_gridRect.bottom());
82
83 QFontMetrics fn(m_font);
84
85 for (int i = 0; i < layout.size(); ++i) {
86 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
87 lineItem->setLine( m_gridRect.left() , layout[i], m_gridRect.right(), layout[i]);
88 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
89
90 QString text = ticksList.at(i);
91
92 if (fn.boundingRect(text).width() > m_axisRect.right() - m_axisRect.left() - labelPadding() )
93 {
94 QString label = text + "...";
95 while (fn.boundingRect(label).width() > m_axisRect.right() - m_axisRect.left() - labelPadding() && label.length() > 3)
96 label.remove(label.length() - 4, 1);
97 labelItem->setText(label);
98 }else{
99 labelItem->setText(text);
100 }
101
102 const QRectF& rect = labelItem->boundingRect();
103
104 QPointF center = rect.center();
105 labelItem->setTransformOriginPoint(center.x(), center.y());
106 //labelItem->setPos( m_gridRect.left() - rect.width() - labelPadding() , layout[i]-center.y());
107
108 if (m_chartAxis->alignment()==Qt::AlignLeft)
109 labelItem->setPos( m_axisRect.right() - rect.width() - labelPadding() , layout[i]-center.y());
110 else if(m_chartAxis->alignment()==Qt::AlignRight)
111 labelItem->setPos( m_axisRect.left() + labelPadding() , layout[i]-center.y());
112
113 if(labelItem->pos().y() + rect.height() > height ||
114 labelItem->pos().y() < m_axisRect.top()) {
115 labelItem->setVisible(false);
116 lineItem->setVisible(false);
117 }else{
118 labelItem->setVisible(true);
119 lineItem->setVisible(true);
120 height=labelItem->pos().y();
121 }
122
123 if ((i+1)%2 && i>1) {
124 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
125 rectItem->setRect( m_gridRect.left(),layout[i], m_gridRect.width(),layout[i-1]-layout[i]);
126 }
127 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
128 // lineItem->setLine( m_gridRect.left()-5,layout[i], m_gridRect.left(),layout[i]);
129
130 if (m_chartAxis->alignment()==Qt::AlignLeft)
131 lineItem->setLine( m_axisRect.right()- labelPadding(),layout[i], m_axisRect.right(),layout[i]);
132 else if(m_chartAxis->alignment()==Qt::AlignRight)
133 lineItem->setLine( m_axisRect.left(),layout[i], m_axisRect.left()+ labelPadding(),layout[i]);
134
135 }
136 }
65 }
137
66
138 void ChartValueAxisY::handleAxisUpdated()
67 void ChartValueAxisY::handleAxisUpdated()
139 {
68 {
140 QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis);
69 if (m_tickCount != m_axis->tickCount()) {
141 if (m_tickCount != axis->tickCount()) {
70 m_tickCount = m_axis->tickCount();
142 m_tickCount = axis->tickCount();
143 presenter()->layout()->invalidate();
71 presenter()->layout()->invalidate();
144 }
72 }
145
73
@@ -150,10 +78,10 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c
150 {
78 {
151 Q_UNUSED(constraint)
79 Q_UNUSED(constraint)
152
80
153 QFontMetrics fn(m_font);
81 QFontMetrics fn(font());
154 QSizeF sh;
82 QSizeF sh;
155 QSizeF base = ChartAxis::sizeHint(which, constraint);
83 QSizeF base = ChartAxis::sizeHint(which, constraint);
156 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
84 QStringList ticksList = createValueLabels(m_tickCount);
157 qreal width=0;
85 qreal width=0;
158 qreal height=0;
86 qreal height=0;
159
87
@@ -30,20 +30,19
30 #ifndef CHARTVALUEAXISY_H
30 #ifndef CHARTVALUEAXISY_H
31 #define CHARTVALUEAXISY_H
31 #define CHARTVALUEAXISY_H
32
32
33 #include "chartaxis_p.h"
33 #include "verticalaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAbstractAxis;
37 class QValueAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartValueAxisY : public ChartAxis
40 class ChartValueAxisY : public VerticalAxis
41 {
41 {
42 public:
42 public:
43 ChartValueAxisY(QAbstractAxis *axis, ChartPresenter *presenter);
43 ChartValueAxisY(QValueAxis *axis, ChartPresenter *presenter);
44 ~ChartValueAxisY();
44 ~ChartValueAxisY();
45
45
46 AxisType axisType() const { return Y_AXIS;}
47 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
46 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
48 protected:
47 protected:
49 QVector<qreal> calculateLayout() const;
48 QVector<qreal> calculateLayout() const;
@@ -51,6 +50,7 protected:
51 void handleAxisUpdated();
50 void handleAxisUpdated();
52 private:
51 private:
53 int m_tickCount;
52 int m_tickCount;
53 QValueAxis* m_axis;
54 };
54 };
55
55
56 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now