##// END OF EJS Templates
Updates axis drawing code...
Michal Klocek -
r2133:8c175959daec
parent child
Show More
@@ -51,8 +51,10 public:
51
51
52 int count = series->barSets().first()->count();
52 int count = series->barSets().first()->count();
53
53
54 for (int i = 0; i < count; i++)
54
55 barcategory->append(QString::number(i));
55 for (int i = 0; i < count; i++) {
56 barcategory->append("BarSet " + QString::number(i));
57 }
56
58
57 chart->setAxisY(valueaxis, series);
59 chart->setAxisY(valueaxis, series);
58 chart->setAxisX(barcategory, series);
60 chart->setAxisX(barcategory, series);
@@ -51,8 +51,9 public:
51
51
52 int count = series->barSets().first()->count();
52 int count = series->barSets().first()->count();
53
53
54 for (int i = 0; i < count; i++)
54 for (int i = 0; i < count; i++) {
55 barcategory->append(QString::number(i));
55 barcategory->append("BarSet " + QString::number(i));
56 }
56
57
57 chart->setAxisX(valueaxis, series);
58 chart->setAxisX(valueaxis, series);
58 chart->setAxisY(barcategory, series);
59 chart->setAxisY(barcategory, series);
@@ -40,30 +40,30 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
40
40
41 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
41 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
42 {
42 {
43 int count = m_categoriesAxis->d_ptr->count();
43 QVector<qreal> points;
44
44
45 Q_ASSERT(count >= 1);
45 const QRectF& gridRect = gridGeometry();
46 qreal range = max() - min();
46
47
47 QVector<qreal> points;
48 const qreal delta = gridRect.width()/range;
48 points.resize(count + 2);
49
49
50 const QRectF &gridRect = gridGeometry();
50 if(delta<2) return points;
51
52 qreal offset =-min()-0.5;
53
54 offset = int(offset * delta)%int(delta);
51
55
52 const qreal delta = gridRect.width() / (count);
56 int count = qFloor(range);
53 qreal offset = -min() - 0.5;
54
57
55 if (delta < 1)
58 if(count < 1 ) return points;
56 return points;
57
59
58 if (offset < 0)
60 points.resize(count+2);
59 offset = int(offset * gridRect.width() / (max() - min())) % int(delta) + delta;
60 else
61 offset = int(offset * gridRect.width() / (max() - min())) % int(delta);
62
61
63 for (int i = -1; i < count + 1; ++i) {
62 for (int i = 0; i < count+2; ++i) {
64 qreal x = offset + i * delta + gridRect.left();
63 qreal x = offset + i * delta + gridRect.left();
65 points[i + 1] = x;
64 points[i] = x;
66 }
65 }
66
67 return points;
67 return points;
68 }
68 }
69
69
@@ -114,34 +114,35 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constra
114 QSizeF sh;
114 QSizeF sh;
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
117 qreal width = 0;
118 qreal height = 0;
119
120 switch (which) {
121 case Qt::MinimumSize:
122 width = fn.boundingRect("...").width();
123 height = fn.height() + labelPadding();
124 width = qMax(width, base.width());
125 height += base.height();
126 sh = QSizeF(width, height);
127 break;
128 case Qt::PreferredSize: {
129
130 for (int i = 0; i < ticksList.size(); ++i) {
131 QRectF rect = fn.boundingRect(ticksList.at(i));
132 width += rect.width();
133 height = qMax(rect.height() + labelPadding(), height);
134 }
135 width = qMax(width, base.width());
136 height += base.height();
137 sh = QSizeF(width, height);
138 break;
139 }
140 default:
141 break;
142 }
143
117
144 return sh;
118 qreal width=0;
119 qreal height=0;
120
121 switch (which) {
122 case Qt::MinimumSize:
123 width = fn.boundingRect("...").width();
124 height = fn.height()+labelPadding();
125 width=qMax(width,base.width());
126 height += base.height();
127 sh = QSizeF(width,height);
128 break;
129 case Qt::PreferredSize:{
130
131 for (int i = 0; i < ticksList.size(); ++i)
132 {
133 QRectF rect = fn.boundingRect(ticksList.at(i));
134 width += rect.width();
135 }
136 height = fn.height()+labelPadding();
137 width = qMax(width,base.width());
138 height += base.height();
139 sh = QSizeF(width,height);
140 break;
141 }
142 default:
143 break;
144 }
145 return sh;
145 }
146 }
146
147
147 QTCOMMERCIALCHART_END_NAMESPACE
148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -23,6 +23,7
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>
26
27
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29
@@ -111,34 +112,35 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constra
111 QSizeF sh;
112 QSizeF sh;
112 QSizeF base = ChartAxis::sizeHint(which, constraint);
113 QSizeF base = ChartAxis::sizeHint(which, constraint);
113 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
114 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
114 qreal width = 0;
115 qreal height = 0;
116
117 switch (which) {
118 case Qt::MinimumSize:
119 width = fn.boundingRect("...").width() + labelPadding();
120 height = fn.height();
121 width += base.width();
122 height = qMax(height, base.height());
123 sh = QSizeF(width, height);
124 break;
125 case Qt::PreferredSize: {
126
127 for (int i = 0; i < ticksList.size(); ++i) {
128 QRectF rect = fn.boundingRect(ticksList.at(i));
129 height += rect.height();
130 width = qMax(rect.width() + labelPadding(), width);
131 }
132 height = qMax(height, base.height());
133 width += base.width();
134 sh = QSizeF(width, height);
135 break;
136 }
137 default:
138 break;
139 }
140
115
141 return sh;
116 qreal width=0;
117 qreal height=0;
118
119 switch (which) {
120 case Qt::MinimumSize:
121 width = fn.boundingRect("...").width() + labelPadding();
122 height = fn.height();
123 width+=base.width();
124 height=qMax(height,base.height());
125 sh = QSizeF(width,height);
126 break;
127 case Qt::PreferredSize:{
128
129 for (int i = 0; i < ticksList.size(); ++i)
130 {
131 QRectF rect = fn.boundingRect(ticksList.at(i));
132 height+=rect.height();
133 width=qMax(rect.width()+labelPadding() +1 ,width); //one pixel torelance
134 }
135 height=qMax(height,base.height());
136 width+=base.width();
137 sh = QSizeF(width,height);
138 break;
139 }
140 default:
141 break;
142 }
143 return sh;
142 }
144 }
143
145
144 QTCOMMERCIALCHART_END_NAMESPACE
146 QTCOMMERCIALCHART_END_NAMESPACE
@@ -301,9 +301,8 void ChartAxis::handleDomainUpdated()
301
301
302 QVector<qreal> layout = calculateLayout();
302 QVector<qreal> layout = calculateLayout();
303 updateLayout(layout);
303 updateLayout(layout);
304 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
304 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
305
305 QSizeF after = sizeHint(Qt::PreferredSize);
306 QSizeF after = sizeHint(Qt::MinimumSize);
307
306
308 if (before != after) {
307 if (before != after) {
309 QGraphicsLayoutItem::updateGeometry();
308 QGraphicsLayoutItem::updateGeometry();
@@ -21,6 +21,7
21 #include "horizontalaxis_p.h"
21 #include "horizontalaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include <QFontMetrics>
23 #include <QFontMetrics>
24 #include <qmath.h>
24 #include <QDebug>
25 #include <QDebug>
25
26
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -41,15 +42,15 void HorizontalAxis::updateGeometry()
41 if (layout.isEmpty())
42 if (layout.isEmpty())
42 return;
43 return;
43
44
44 QStringList ticksList = labels();
45 QStringList labelList = labels();
45
46
46 QList<QGraphicsItem *> lines = lineItems();
47 QList<QGraphicsItem *> lines = lineItems();
47 QList<QGraphicsItem *> labels = labelItems();
48 QList<QGraphicsItem *> labels = labelItems();
48 QList<QGraphicsItem *> shades = shadeItems();
49 QList<QGraphicsItem *> shades = shadeItems();
49 QList<QGraphicsItem *> axis = arrowItems();
50 QList<QGraphicsItem *> axis = arrowItems();
50
51
51 Q_ASSERT(labels.size() == ticksList.size());
52 Q_ASSERT(labels.size() == labelList.size());
52 Q_ASSERT(layout.size() == ticksList.size());
53 Q_ASSERT(layout.size() == labelList.size());
53
54
54 const QRectF &axisRect = axisGeometry();
55 const QRectF &axisRect = axisGeometry();
55 const QRectF &gridRect = gridGeometry();
56 const QRectF &gridRect = gridGeometry();
@@ -63,19 +64,39 void HorizontalAxis::updateGeometry()
63 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
64 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
64
65
65 qreal width = 0;
66 qreal width = 0;
67 QFontMetrics fn(font());
66
68
67 for (int i = 0; i < layout.size(); ++i) {
69 for (int i = 0; i < layout.size(); ++i) {
68
70
69 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
71 //items
70 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
72 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
73 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
71 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
74 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
72
75
73 //grid line
76 //grid line
74 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
77 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
75
78
76 //label text
79 //label text wrapping
77 labelItem->setText(ticksList.at(i));
80 if(intervalAxis()&& i+1!=layout.size()) {
78 const QRectF &rect = labelItem->boundingRect();
81 //wrapping in case of interval axis
82 const qreal delta = layout[i+1] - layout[i];
83 QString text = labelList.at(i);
84 if (fn.boundingRect(text).width() > delta )
85 {
86 QString label = text + "...";
87 while (fn.boundingRect(label).width() > delta && label.length() > 3)
88 label.remove(label.length() - 4, 1);
89 labelItem->setText(label);
90 }
91 else {
92 labelItem->setText(text);
93 }
94 }else{
95 labelItem->setText(labelList.at(i));
96 }
97
98 //label transformation origin point
99 const QRectF& rect = labelItem->boundingRect();
79 QPointF center = rect.center();
100 QPointF center = rect.center();
80 labelItem->setTransformOriginPoint(center.x(), center.y());
101 labelItem->setTransformOriginPoint(center.x(), center.y());
81
102
@@ -88,23 +109,21 void HorizontalAxis::updateGeometry()
88 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
109 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
89 }
110 }
90
111
91 if (intervalAxis() && i + 1 != layout.size()) {
112 //label in beetwen
92 const qreal delta = (layout[i + 1] - layout[i]) / 2;
113 if(intervalAxis()&& i+1!=layout.size()) {
114 const qreal delta = (layout[i+1] - layout[i])/2;
93 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
115 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
94 }
116 }
95
117
96 //overlap detection
118 //label overlap detection
97 if (labelItem->pos().x() <= width ||
119 if(labelItem->pos().x() <= width ||
98 labelItem->pos().x() < axisRect.left() ||
120 labelItem->pos().x() < axisRect.left() ||
99 labelItem->pos().x() + rect.width() > axisRect.right()) {
121 labelItem->pos().x() + rect.width() > axisRect.right()) {
100 labelItem->setVisible(false);
122 labelItem->setVisible(false);
101 gridItem->setVisible(false);
123 }
102 tickItem->setVisible(false);
124 else {
103 } else {
104 labelItem->setVisible(true);
125 labelItem->setVisible(true);
105 gridItem->setVisible(true);
126 width=rect.width()+labelItem->pos().x();
106 tickItem->setVisible(true);
107 width = rect.width() + labelItem->pos().x();
108 }
127 }
109
128
110 //shades
129 //shades
@@ -118,8 +137,9 void HorizontalAxis::updateGeometry()
118 if (x < gridRect.left() || x > gridRect.right()) {
137 if (x < gridRect.left() || x > gridRect.right()) {
119 gridItem->setVisible(false);
138 gridItem->setVisible(false);
120 tickItem->setVisible(false);
139 tickItem->setVisible(false);
121 if (intervalAxis() && (labelItem->pos().x() < gridRect.left() || labelItem->pos().x() + rect.width() > gridRect.right()))
140 }else{
122 labelItem->setVisible(false);
141 gridItem->setVisible(true);
142 tickItem->setVisible(true);
123 }
143 }
124
144
125 }
145 }
@@ -129,8 +149,10 void HorizontalAxis::updateGeometry()
129 QGraphicsLineItem *gridLine;
149 QGraphicsLineItem *gridLine;
130 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
150 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
131 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
151 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
132 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size() + 1));
152 gridLine->setVisible(true);
153 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
133 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
154 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
155 gridLine->setVisible(true);
134 }
156 }
135 }
157 }
136
158
@@ -88,23 +88,23 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
88 qreal height = 0;
88 qreal height = 0;
89
89
90 switch (which) {
90 switch (which) {
91 case Qt::MinimumSize: {
91 case Qt::MinimumSize:{
92 int count = qMax(ticksList.last().count(), ticksList.first().count());
92 int count = qMax(ticksList.last().count(),ticksList.first().count());
93 count = qMin(count,5);
93 width = fn.averageCharWidth() * count;
94 width = fn.averageCharWidth() * count;
94 height = fn.height() + labelPadding();
95 height = fn.height() + labelPadding();
95 width = qMax(width, base.width());
96 width = qMax(width,base.width());
96 height += base.height();
97 height += base.height();
97 sh = QSizeF(width, height);
98 sh = QSizeF(width,height);
98 break;
99 break;
99 }
100 }
100 case Qt::PreferredSize: {
101 case Qt::PreferredSize:{
101 for (int i = 0; i < ticksList.size(); ++i) {
102 int count = qMax(ticksList.last().count(),ticksList.first().count());
102 width += fn.averageCharWidth() * ticksList.at(i).count();
103 width=fn.averageCharWidth() * count;
103 }
104 height=fn.height()+labelPadding();
104 height = fn.height() + labelPadding();
105 width=qMax(width,base.width());
105 width = qMax(width, base.width());
106 height+=base.height();
106 height += base.height();
107 sh = QSizeF(width,height);
107 sh = QSizeF(width, height);
108 break;
108 break;
109 }
109 }
110 default:
110 default:
@@ -26,6 +26,7
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
27 #include <QFontMetrics>
28 #include <qmath.h>
28 #include <qmath.h>
29 #include <QDebug>
29
30
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
32
@@ -90,22 +91,21 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) c
90
91
91 switch (which) {
92 switch (which) {
92 case Qt::MinimumSize: {
93 case Qt::MinimumSize: {
93 int count = qMax(ticksList.first().count() , ticksList.last().count());
94 width = fn.boundingRect("...").width() + labelPadding();
94 width = fn.averageCharWidth() * count + labelPadding();
95 height = fn.height();
96 height = qMax(height, base.height());
97 width += base.width();
95 width += base.width();
98 sh = QSizeF(width, height);
96 height = fn.height();
97 height = qMax(height,base.height());
98 sh = QSizeF(width,height);
99 break;
99 break;
100 }
100 }
101 case Qt::PreferredSize: {
101 case Qt::PreferredSize:
102 for (int i = 0; i < ticksList.size(); ++i) {
102 {
103 width = qMax(qreal(fn.averageCharWidth() * ticksList.at(i).count()) + labelPadding(), width);
103 int count = qMax(ticksList.first().count() , ticksList.last().count());
104 height += fn.height();
104 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
105 }
106 height = qMax(height, base.height());
107 width += base.width();
105 width += base.width();
108 sh = QSizeF(width, height);
106 height = fn.height() * ticksList.count();
107 height = qMax(height,base.height());
108 sh = QSizeF(width,height);
109 break;
109 break;
110 }
110 }
111 default:
111 default:
@@ -57,17 +57,21 void VerticalAxis::updateGeometry()
57
57
58 qreal height = axisRect.bottom();
58 qreal height = axisRect.bottom();
59
59
60 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem *>(axis.at(0));
61
60
62 if (alignment() == Qt::AlignLeft)
61 //arrow
63 lineItem->setLine(axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
62 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
64 else if (alignment() == Qt::AlignRight)
63
65 lineItem->setLine(axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
64 //arrow position
65 if (alignment()==Qt::AlignLeft)
66 arrowItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
67 else if(alignment()==Qt::AlignRight)
68 arrowItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
66
69
67 QFontMetrics fn(font());
70 QFontMetrics fn(font());
68
71
69 for (int i = 0; i < layout.size(); ++i) {
72 for (int i = 0; i < layout.size(); ++i) {
70
73
74 //items
71 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
75 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
72 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
76 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
73 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
77 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
@@ -75,7 +79,7 void VerticalAxis::updateGeometry()
75 //grid line
79 //grid line
76 gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]);
80 gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]);
77
81
78 //label text
82 //label text wrapping
79 QString text = labelList.at(i);
83 QString text = labelList.at(i);
80 if (fn.boundingRect(text).width() > axisRect.right() - axisRect.left() - labelPadding()) {
84 if (fn.boundingRect(text).width() > axisRect.right() - axisRect.left() - labelPadding()) {
81 QString label = text + "...";
85 QString label = text + "...";
@@ -85,7 +89,9 void VerticalAxis::updateGeometry()
85 } else {
89 } else {
86 labelItem->setText(text);
90 labelItem->setText(text);
87 }
91 }
92 //label transformation origin point
88 const QRectF &rect = labelItem->boundingRect();
93 const QRectF &rect = labelItem->boundingRect();
94
89 QPointF center = rect.center();
95 QPointF center = rect.center();
90 labelItem->setTransformOriginPoint(center.x(), center.y());
96 labelItem->setTransformOriginPoint(center.x(), center.y());
91
97
@@ -97,22 +103,22 void VerticalAxis::updateGeometry()
97 labelItem->setPos(axisRect.left() + labelPadding() , layout[i] - center.y());
103 labelItem->setPos(axisRect.left() + labelPadding() , layout[i] - center.y());
98 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
104 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
99 }
105 }
100 if (intervalAxis() && i + 1 != layout.size()) {
106
101 const qreal delta = (layout[i + 1] - layout[i]) / 2;
107 //label in beetwen
108 if(intervalAxis()&& i+1!=layout.size()) {
109 const qreal delta = (layout[i+1] - layout[i])/2;
102 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
110 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
103 }
111 }
104
112
105 //overlap detection
113 //label overlap detection
106 if (labelItem->pos().y() + rect.height() > height ||
114 if(labelItem->pos().y() + rect.height() > height ||
107 labelItem->pos().y() + rect.height() > axisRect.bottom() ||
115 labelItem->pos().y() + rect.height()/2 > gridRect.bottom() ||
108 labelItem->pos().y() < axisRect.top()) {
116 labelItem->pos().y() + rect.height()/2 < gridRect.top()) {
109 labelItem->setVisible(false);
117 labelItem->setVisible(false);
110 gridItem->setVisible(false);
118 }
111 tickItem->setVisible(false);
119 else {
112 } else {
113 labelItem->setVisible(true);
120 labelItem->setVisible(true);
114 gridItem->setVisible(true);
121 height=labelItem->pos().y();
115 height = labelItem->pos().y();
116 }
122 }
117
123
118 //shades
124 //shades
@@ -123,11 +129,13 void VerticalAxis::updateGeometry()
123
129
124 // check if the grid line and the axis tick should be shown
130 // check if the grid line and the axis tick should be shown
125 qreal y = gridItem->line().p1().y();
131 qreal y = gridItem->line().p1().y();
126 if (y < gridRect.top() || y > gridRect.bottom()) {
132 if ((y < gridRect.top() || y > gridRect.bottom()))
133 {
127 gridItem->setVisible(false);
134 gridItem->setVisible(false);
128 tickItem->setVisible(false);
135 tickItem->setVisible(false);
129 if (intervalAxis() && (labelItem->pos().y() < gridRect.top() || labelItem->pos().y() + rect.height() > gridRect.bottom()))
136 }else{
130 labelItem->setVisible(false);
137 gridItem->setVisible(true);
138 tickItem->setVisible(true);
131 }
139 }
132
140
133 }
141 }
@@ -136,8 +144,10 void VerticalAxis::updateGeometry()
136 QGraphicsLineItem *gridLine;
144 QGraphicsLineItem *gridLine;
137 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
145 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
138 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
146 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
139 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size() + 1));
147 gridLine->setVisible(true);
148 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
140 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
149 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
150 gridLine->setVisible(true);
141 }
151 }
142 }
152 }
143
153
@@ -29,7 +29,7
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 static const qreal golden_ratio = 0.25;
32 static const qreal golden_ratio = 0.4;
33
33
34 ChartLayout::ChartLayout(ChartPresenter *presenter)
34 ChartLayout::ChartLayout(ChartPresenter *presenter)
35 : m_presenter(presenter),
35 : m_presenter(presenter),
@@ -103,10 +103,14 QRectF ChartLayout::calculateBackgroundMinimum(const QRectF &minimum) const
103
103
104 QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QList<ChartAxis *>& axes) const
104 QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QList<ChartAxis *>& axes) const
105 {
105 {
106 QSizeF left;
106 QSizeF left(0,0);
107 QSizeF right;
107 QSizeF minLeft(0,0);
108 QSizeF bottom;
108 QSizeF right(0,0);
109 QSizeF top;
109 QSizeF minRight(0,0);
110 QSizeF bottom(0,0);
111 QSizeF minBottom(0,0);
112 QSizeF top(0,0);
113 QSizeF minTop(0,0);
110 int leftCount = 0;
114 int leftCount = 0;
111 int rightCount = 0;
115 int rightCount = 0;
112 int topCount = 0;
116 int topCount = 0;
@@ -117,64 +121,80 QRectF ChartLayout::calculateAxisGeometry(const QRectF &geometry, const QList<Ch
117 if (!axis->isVisible())
121 if (!axis->isVisible())
118 continue;
122 continue;
119
123
120 QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize);
124 QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize);
125 //this is used to get single thick font size
126 QSizeF minSize = axis->effectiveSizeHint(Qt::MinimumSize);
121
127
122 switch (axis->alignment()) {
128 switch (axis->alignment()) {
123 case Qt::AlignLeft:
129 case Qt::AlignLeft:
124 left.setWidth(left.width() + size.width());
130 left.setWidth(left.width()+size.width());
125 left.setHeight(qMax(left.height(), size.height()));
131 left.setHeight(qMax(left.height(),size.height()));
126 leftCount++;
132 minLeft.setWidth(minLeft.width()+minSize.width());
127 break;
133 minLeft.setHeight(qMax(minLeft.height(),minSize.height()));
134 leftCount++;
135 break;
128 case Qt::AlignRight:
136 case Qt::AlignRight:
129 right.setWidth(right.width() + size.width());
137 right.setWidth(right.width()+size.width());
130 right.setHeight(qMax(right.height(), size.height()));
138 right.setHeight(qMax(right.height(),size.height()));
139 minRight.setWidth(minRight.width()+minSize.width());
140 minRight.setHeight(qMax(minRight.height(),minSize.height()));
131 rightCount++;
141 rightCount++;
132 break;
142 break;
133 case Qt::AlignTop:
143 case Qt::AlignTop:
134 top.setWidth(qMax(top.width(), size.width()));
144 top.setWidth(qMax(top.width(),size.width()));
135 top.setHeight(top.height() + size.height());
145 top.setHeight(top.height()+size.height());
146 minTop.setWidth(qMax(minTop.width(),minSize.width()));
147 minTop.setHeight(minTop.height()+minSize.height());
136 topCount++;
148 topCount++;
137 break;
149 break;
138 case Qt::AlignBottom:
150 case Qt::AlignBottom:
139 bottom.setWidth(qMax(bottom.width(), size.width()));
151 bottom.setWidth(qMax(bottom.width(), size.width()));
140 bottom.setHeight(bottom.height() + size.height());
152 bottom.setHeight(bottom.height() + size.height());
153 minBottom.setWidth(qMax(minBottom.width(),minSize.width()));
154 minBottom.setHeight(minBottom.height() + minSize.height());
141 bottomCount++;
155 bottomCount++;
142 break;
156 break;
143 }
157 }
144 }
158 }
145
159
146 left.setWidth(qMax(qMax(top.width() / 2, bottom.width() / 2), left.width()));
160 int horizontal = leftCount + rightCount;
147 left.setWidth(qMin(left.width(), golden_ratio * geometry.width()));
161 qreal hratio = 0 ;
148 right.setWidth(qMax(qMax(top.width() / 2, bottom.width() / 2), right.width()));
162 if(horizontal>0)
149 right.setWidth(qMin(right.width(), golden_ratio * geometry.width()));
163 hratio = (golden_ratio*geometry.width())/horizontal;
150 top.setHeight(qMax(qMax(left.height() / 2, right.height() / 2), top.height()));
164
151 bottom.setHeight(qMax(qMax(left.height() / 2, right.height() / 2), bottom.height()));
165 if(leftCount>0)
166 left.setWidth(qMin(left.width(),hratio*leftCount));
167 if(rightCount>0)
168 right.setWidth(qMin(right.width(),hratio*rightCount));
152
169
153 QRectF chartRect = geometry.adjusted(left.width(), top.height(), -right.width(), -bottom.height());
170 qreal minHeight = qMax(minLeft.height(),minRight.height());
171 qreal minWidth = qMax(minTop.width(),minBottom.width());
172
173 QRectF chartRect = geometry.adjusted(qMax(left.width(),minWidth/2), qMax(top.height(), minHeight/2),-qMax(right.width(),minWidth/2),-qMax(bottom.height(),minHeight/2));
154
174
155 qreal leftOffset = 0;
175 qreal leftOffset = 0;
156 qreal rightOffset = 0;
176 qreal rightOffset = 0;
157 qreal topOffset = 0;
177 qreal topOffset = 0;
158 qreal bottomOffset = 0;
178 qreal bottomOffset = 0;
159
179
160 foreach(ChartAxis * axis , axes) {
180 foreach(ChartAxis* axis , axes) {
161
181
162 if (!axis->isVisible())
182 if (!axis->isVisible())
163 continue;
183 continue;
164
184
165 QSizeF size = axis->effectiveSizeHint(Qt::MinimumSize);
185 QSizeF size = axis->effectiveSizeHint(Qt::PreferredSize);
166
186
167 switch (axis->alignment()) {
187 switch(axis->alignment()){
168 case Qt::AlignLeft: {
188 case Qt::AlignLeft:{
169 qreal width = qMin(size.width(), left.width() / leftCount);
189 qreal width = qMin(size.width(),(left.width()/leftCount));
170 leftOffset += width;
190 leftOffset+=width;
171 axis->setGeometry(QRect(chartRect.left() - leftOffset, chartRect.top() - (size.height() + 1) / 2, width, chartRect.height() + size.height() + 2), chartRect);
191 axis->setGeometry(QRect(chartRect.left()-leftOffset, geometry.top(),width, geometry.bottom()),chartRect);
172 break;
192 break;
173 }
193 }
174 case Qt::AlignRight: {
194 case Qt::AlignRight:{
175 qreal width = qMin(size.width(), right.width() / rightCount);
195 qreal width = qMin(size.width(),(right.width()/rightCount));
176 axis->setGeometry(QRect(chartRect.right() + rightOffset, chartRect.top() - (size.height() + 1) / 2, width, chartRect.height() + size.height() + 2), chartRect);
196 axis->setGeometry(QRect(chartRect.right()+rightOffset,geometry.top(),width,geometry.bottom()),chartRect);
177 rightOffset += width;
197 rightOffset+=width;
178 break;
198 break;
179 }
199 }
180 case Qt::AlignTop:
200 case Qt::AlignTop:
@@ -287,8 +307,8 QRectF ChartLayout::calculateTitleGeometry(const QRectF &geometry, ChartTitle *t
287 {
307 {
288 title->setGeometry(geometry);
308 title->setGeometry(geometry);
289 QPointF center = geometry.center() - title->boundingRect().center();
309 QPointF center = geometry.center() - title->boundingRect().center();
290 title->setPos(center.x(), title->pos().y());
310 title->setPos(center.x(),title->pos().y());
291 return geometry.adjusted(0, title->boundingRect().height(), 0, 0);
311 return geometry.adjusted(0,title->boundingRect().height()+1,0,0);
292 }
312 }
293
313
294 QRectF ChartLayout::calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const
314 QRectF ChartLayout::calculateTitleMinimum(const QRectF &minimum, ChartTitle *title) const
@@ -76,10 +76,10 QSizeF ChartTitle::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
76 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
76 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
77 break;
77 break;
78 case Qt::PreferredSize:
78 case Qt::PreferredSize:
79 sh = QSizeF(fn.boundingRect(m_text).width(), fn.height());
79 sh = fn.boundingRect(m_text).size();
80 break;
80 break;
81 case Qt::MaximumSize:
81 case Qt::MaximumSize:
82 sh = QSizeF(fn.boundingRect(m_text).width(), fn.height());
82 sh = fn.boundingRect(m_text).size();
83 break;
83 break;
84 case Qt::MinimumDescent:
84 case Qt::MinimumDescent:
85 sh = QSizeF(0, fn.descent());
85 sh = QSizeF(0, fn.descent());
General Comments 0
You need to be logged in to leave comments. Login now