##// END OF EJS Templates
Multiaxis support...
Marek Rosa -
r2093:0a6899da1bbd
parent child
Show More
@@ -0,0 +1,84
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 <QApplication>
22 #include <QMainWindow>
23 #include <QChartView>
24 #include <QLineSeries>
25 #include <QValueAxis>
26 #include <QTime>
27 #include <QValueAxis>
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
31 int main(int argc, char *argv[])
32 {
33 QApplication a(argc, argv);
34
35 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36
37 //![1]
38 QLineSeries* series;
39 QValueAxis *axisX;
40 QValueAxis *axisY;
41 QChart* chart = new QChart();
42 for (int i = 0; i < 5; i++) {
43 series = new QLineSeries;
44 for (int k(0); k < 8; k++)
45 series->append(i + k, qrand()%20);
46 chart->addSeries(series);
47
48 axisX = new QValueAxis;
49 axisX->setTickCount(7 + i);
50 axisX->setLinePenColor(series->pen().color());
51 if (i%2)
52 axisX->setAlternativePlacement(true);
53 axisY = new QValueAxis;
54 axisY->setTickCount(7 + i);
55 axisY->setLinePenColor(series->pen().color());
56 if (i%2)
57 axisY->setAlternativePlacement(true);
58
59 chart->setAxisX(axisX, series);
60 chart->setAxisY(axisY, series);
61 }
62 //![2]
63
64 //![3]
65 chart->legend()->hide();
66
67 chart->setTitle("Simple line chart example");
68 //![3]
69
70 //![4]
71 QChartView* chartView = new QChartView(chart);
72 chartView->setRenderHint(QPainter::Antialiasing);
73 //![4]
74
75
76 //![5]
77 QMainWindow window;
78 window.setCentralWidget(chartView);
79 window.resize(800, 600);
80 window.show();
81 //![5]
82
83 return a.exec();
84 }
@@ -0,0 +1,5
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4 TARGET = multiaxis
5 SOURCES += main.cpp
@@ -30,7 +30,8 SUBDIRS += \
30 donutbreakdown \
30 donutbreakdown \
31 scrollchart \
31 scrollchart \
32 temperaturerecords \
32 temperaturerecords \
33 donutchart
33 donutchart \
34 multiaxis
34
35
35 !linux-arm*: {
36 !linux-arm*: {
36 SUBDIRS += \
37 SUBDIRS += \
@@ -399,6 +399,11 void ChartAxis::setGeometry(const QRectF &rect)
399
399
400 }
400 }
401
401
402 void ChartAxis::setInternalRect(const QRectF &size)
403 {
404 m_internalRect = size;
405 }
406
402 void ChartAxis::axisSelected()
407 void ChartAxis::axisSelected()
403 {
408 {
404 //TODO: axis clicked;
409 //TODO: axis clicked;
@@ -441,6 +446,11 Qt::Orientation ChartAxis::orientation() const
441 return m_chartAxis->orientation();
446 return m_chartAxis->orientation();
442 }
447 }
443
448
449 bool ChartAxis::alternativePlacement() const
450 {
451 return m_chartAxis->alternativePlacement();
452 }
453
444 bool ChartAxis::isVisible()
454 bool ChartAxis::isVisible()
445 {
455 {
446 return m_chartAxis->isVisible();
456 return m_chartAxis->isVisible();
@@ -96,6 +96,7 public:
96 ChartAnimation* animation() const { return m_animation; };
96 ChartAnimation* animation() const { return m_animation; };
97
97
98 Qt::Orientation orientation() const;
98 Qt::Orientation orientation() const;
99 bool alternativePlacement() const;
99
100
100 bool isVisible();
101 bool isVisible();
101 void hide();
102 void hide();
@@ -103,6 +104,8 public:
103 void setGeometry(const QRectF &size);
104 void setGeometry(const QRectF &size);
104 QRectF geometry() const { return m_rect; }
105 QRectF geometry() const { return m_rect; }
105
106
107 void setInternalRect(const QRectF &size);
108
106 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const;
109 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const;
107
110
108 protected:
111 protected:
@@ -128,6 +131,7 protected:
128 int m_labelsAngle;
131 int m_labelsAngle;
129 //TODO: to be removed
132 //TODO: to be removed
130 QRectF m_rect;
133 QRectF m_rect;
134 QRectF m_internalRect;
131 QScopedPointer<QGraphicsItemGroup> m_grid;
135 QScopedPointer<QGraphicsItemGroup> m_grid;
132 QScopedPointer<QGraphicsItemGroup> m_shades;
136 QScopedPointer<QGraphicsItemGroup> m_shades;
133 QScopedPointer<QGraphicsItemGroup> m_labels;
137 QScopedPointer<QGraphicsItemGroup> m_labels;
@@ -693,13 +693,23 Qt::Orientation QAbstractAxis::orientation()
693 return d_ptr->m_orientation;
693 return d_ptr->m_orientation;
694 }
694 }
695
695
696 bool QAbstractAxis::alternativePlacement() const
697 {
698 return d_ptr->m_alternativePlacement;
699 }
700
701 void QAbstractAxis::setAlternativePlacement(bool placement)
702 {
703 d_ptr->m_alternativePlacement = placement;
704 }
705
696 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
706 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
697
707
698 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
708 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
699 q_ptr(q),
709 q_ptr(q),
700 m_orientation(Qt::Orientation(0)),
710 m_orientation(Qt::Orientation(0)),
701 m_dataset(0),
711 m_dataset(0),
702 m_visible(false),
712 m_visible(true),
703 m_arrowVisible(true),
713 m_arrowVisible(true),
704 m_gridLineVisible(true),
714 m_gridLineVisible(true),
705 m_labelsVisible(true),
715 m_labelsVisible(true),
@@ -707,7 +717,8 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
707 m_shadesVisible(false),
717 m_shadesVisible(false),
708 m_shadesBrush(Qt::SolidPattern),
718 m_shadesBrush(Qt::SolidPattern),
709 m_shadesOpacity(1.0),
719 m_shadesOpacity(1.0),
710 m_dirty(false)
720 m_dirty(false),
721 m_alternativePlacement(false)
711 {
722 {
712
723
713 }
724 }
@@ -32,8 +32,8 class QAbstractAxisPrivate;
32
32
33 class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject
33 class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
36 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
37 Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged)
37 Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged)
38 Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged)
38 Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged)
39 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
@@ -124,6 +124,8 public:
124 QColor shadesBorderColor() const;
124 QColor shadesBorderColor() const;
125
125
126 Qt::Orientation orientation();
126 Qt::Orientation orientation();
127 bool alternativePlacement() const;
128 void setAlternativePlacement(bool placement);
127
129
128 //range handling
130 //range handling
129 void setMin(const QVariant &min);
131 void setMin(const QVariant &min);
@@ -145,7 +147,7 Q_SIGNALS:
145 void shadesBorderColorChanged(QColor color);
147 void shadesBorderColorChanged(QColor color);
146
148
147 protected:
149 protected:
148 QScopedPointer<QAbstractAxisPrivate> d_ptr;
150 QScopedPointer<QAbstractAxisPrivate> d_ptr;
149 Q_DISABLE_COPY(QAbstractAxis)
151 Q_DISABLE_COPY(QAbstractAxis)
150 friend class ChartDataSet;
152 friend class ChartDataSet;
151 friend class ChartAxis;
153 friend class ChartAxis;
@@ -107,6 +107,8 private:
107
107
108 bool m_dirty;
108 bool m_dirty;
109
109
110 bool m_alternativePlacement;
111
110 friend class QAbstractAxis;
112 friend class QAbstractAxis;
111 friend class ChartDataSet;
113 friend class ChartDataSet;
112 };
114 };
@@ -32,7 +32,7 static int label_padding = 5;
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
35 m_tickCount(0)
35 m_tickCount(0)
36 {
36 {
37 }
37 }
38
38
@@ -76,35 +76,47 void ChartValueAxisX::updateGeometry()
76 QRectF chartRrect = presenter()->chartsGeometry();
76 QRectF chartRrect = presenter()->chartsGeometry();
77
77
78 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
78 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
79 lineItem->setLine(chartRrect.left(), chartRrect.bottom(), chartRrect.right(), chartRrect.bottom());
79 // lineItem->setLine(chartRrect.left(), chartRrect.bottom(), chartRrect.right(), chartRrect.bottom());
80 if (m_chartAxis->alternativePlacement())
81 lineItem->setLine(chartRrect.left(), m_internalRect.top(), chartRrect.right(), m_internalRect.top());
82 else
83 lineItem->setLine(chartRrect.left(), m_internalRect.bottom(), chartRrect.right(), m_internalRect.bottom());
80
84
81 qreal width = 0;
85 qreal width = 0;
82 for (int i = 0; i < layout.size(); ++i) {
86 for (int i = 0; i < layout.size(); ++i) {
83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
87 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 lineItem->setLine(layout[i], chartRrect.top(), layout[i], chartRrect.bottom());
88 lineItem->setLine(layout[i], chartRrect.top(), layout[i], chartRrect.bottom());
85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
89 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 labelItem->setText(ticksList.at(i));
90 labelItem->setText(ticksList.at(i));
87 const QRectF& rect = labelItem->boundingRect();
91 const QRectF& rect = labelItem->boundingRect();
88 QPointF center = rect.center();
92 QPointF center = rect.center();
89 labelItem->setTransformOriginPoint(center.x(), center.y());
93 labelItem->setTransformOriginPoint(center.x(), center.y());
90 labelItem->setPos(layout[i] - center.x(), chartRrect.bottom() + label_padding);
94 // labelItem->setPos(layout[i] - center.x(), chartRrect.bottom() + label_padding);
91 if(labelItem->pos().x() <= width ||
95 if (m_chartAxis->alternativePlacement())
92 labelItem->pos().x() < m_rect.left() ||
96 labelItem->setPos(layout[i] - center.x(), m_internalRect.top() - rect.height() - label_padding);
93 labelItem->pos().x() + rect.width() > m_rect.right()){
97 else
94 labelItem->setVisible(false);
98 labelItem->setPos(layout[i] - center.x(), m_internalRect.bottom() + label_padding);
95 lineItem->setVisible(false);
99 if(labelItem->pos().x() <= width ||
96 }else{
100 labelItem->pos().x() < m_rect.left() ||
97 labelItem->setVisible(true);
101 labelItem->pos().x() + rect.width() > m_rect.right()){
98 lineItem->setVisible(true);
102 labelItem->setVisible(false);
99 width=rect.width()+labelItem->pos().x();
103 lineItem->setVisible(false);
100 }
104 }else{
105 labelItem->setVisible(true);
106 lineItem->setVisible(true);
107 width=rect.width()+labelItem->pos().x();
108 }
101
109
102 if ((i+1)%2 && i>1) {
110 if ((i+1)%2 && i>1) {
103 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
111 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
104 rectItem->setRect(layout[i-1],chartRrect.top(),layout[i]-layout[i-1],chartRrect.height());
112 rectItem->setRect(layout[i-1],chartRrect.top(),layout[i]-layout[i-1],chartRrect.height());
105 }
113 }
106 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
114 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
107 lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5);
115 // lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5);
116 if (m_chartAxis->alternativePlacement())
117 lineItem->setLine(layout[i],m_internalRect.top(),layout[i],m_internalRect.top()-5);
118 else
119 lineItem->setLine(layout[i],m_internalRect.bottom(),layout[i],m_internalRect.bottom()+5);
108 }
120 }
109 }
121 }
110
122
@@ -128,33 +140,33 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c
128 qreal width=0;
140 qreal width=0;
129 qreal height=0;
141 qreal height=0;
130
142
131 switch (which) {
143 switch (which) {
132 case Qt::MinimumSize:{
144 case Qt::MinimumSize:{
133 int count = qMax(ticksList.last().count(),ticksList.first().count());
145 int count = qMax(ticksList.last().count(),ticksList.first().count());
134 width=fn.averageCharWidth()*count;
146 width=fn.averageCharWidth()*count;
135 height=fn.height()+label_padding;
147 height=fn.height()+label_padding;
136 width=qMax(width,base.width());
148 width=qMax(width,base.width());
137 height+=base.height();
149 height+=base.height();
138 sh = QSizeF(width,height);
150 sh = QSizeF(width,height);
139 break;
151 break;
140 }
152 }
141 case Qt::PreferredSize:{
153 case Qt::PreferredSize:{
142 for (int i = 0; i < ticksList.size(); ++i)
154 for (int i = 0; i < ticksList.size(); ++i)
143 {
155 {
144 width+=fn.averageCharWidth()*ticksList.at(i).count();
156 width+=fn.averageCharWidth()*ticksList.at(i).count();
145
157
146 }
147 height=fn.height()+label_padding;
148 width=qMax(width,base.width());
149 height+=base.height();
150 sh = QSizeF(width,height);
151 break;
152 }
158 }
153 default:
159 height=fn.height()+label_padding;
154 break;
160 width=qMax(width,base.width());
155 }
161 height+=base.height();
162 sh = QSizeF(width,height);
163 break;
164 }
165 default:
166 break;
167 }
156
168
157 return sh;
169 return sh;
158 }
170 }
159
171
160 QTCOMMERCIALCHART_END_NAMESPACE
172 QTCOMMERCIALCHART_END_NAMESPACE
@@ -32,7 +32,7 static int label_padding = 5;
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
35 m_tickCount(0)
35 m_tickCount(0)
36 {
36 {
37 }
37 }
38
38
@@ -79,7 +79,11 void ChartValueAxisY::updateGeometry()
79 qreal height = m_rect.bottom();
79 qreal height = m_rect.bottom();
80
80
81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
82 lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
82 // lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
83 if (m_chartAxis->alternativePlacement())
84 lineItem->setLine( m_internalRect.right() , chartRect.top(), m_internalRect.right(), chartRect.bottom());
85 else
86 lineItem->setLine( m_internalRect.left() , chartRect.top(), m_internalRect.left(), chartRect.bottom());
83
87
84 QFontMetrics fn(m_font);
88 QFontMetrics fn(m_font);
85
89
@@ -88,40 +92,48 void ChartValueAxisY::updateGeometry()
88 lineItem->setLine( chartRect.left() , layout[i], chartRect.right(), layout[i]);
92 lineItem->setLine( chartRect.left() , layout[i], chartRect.right(), layout[i]);
89 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
93 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
90
94
91 QString text = ticksList.at(i);
95 QString text = ticksList.at(i);
92
96
93 if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding )
97 if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding )
94 {
98 {
95 QString label = text + "...";
99 QString label = text + "...";
96 while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3)
100 while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3)
97 label.remove(label.length() - 4, 1);
101 label.remove(label.length() - 4, 1);
98 labelItem->setText(label);
102 labelItem->setText(label);
99 }else{
103 }else{
100 labelItem->setText(text);
104 labelItem->setText(text);
101 }
105 }
102
106
103 const QRectF& rect = labelItem->boundingRect();
107 const QRectF& rect = labelItem->boundingRect();
104
108
105 QPointF center = rect.center();
109 QPointF center = rect.center();
106 labelItem->setTransformOriginPoint(center.x(), center.y());
110 labelItem->setTransformOriginPoint(center.x(), center.y());
107 labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
111 // labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
112 if (m_chartAxis->alternativePlacement())
113 labelItem->setPos( m_internalRect.right() + label_padding , layout[i]-center.y());
114 else
115 labelItem->setPos( m_internalRect.left() - rect.width() - label_padding , layout[i]-center.y());
108
116
109 if(labelItem->pos().y() + rect.height() > height ||
117 if(labelItem->pos().y() + rect.height() > height ||
110 labelItem->pos().y() < m_rect.top()) {
118 labelItem->pos().y() < m_rect.top()) {
111 labelItem->setVisible(false);
119 labelItem->setVisible(false);
112 lineItem->setVisible(false);
120 lineItem->setVisible(false);
113 }else{
121 }else{
114 labelItem->setVisible(true);
122 labelItem->setVisible(true);
115 lineItem->setVisible(true);
123 lineItem->setVisible(true);
116 height=labelItem->pos().y();
124 height=labelItem->pos().y();
117 }
125 }
118
126
119 if ((i+1)%2 && i>1) {
127 if ((i+1)%2 && i>1) {
120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
128 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
121 rectItem->setRect( chartRect.left(),layout[i], chartRect.width(),layout[i-1]-layout[i]);
129 rectItem->setRect( chartRect.left(),layout[i], chartRect.width(),layout[i-1]-layout[i]);
122 }
130 }
123 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
131 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
124 lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]);
132 // lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]);
133 if (m_chartAxis->alternativePlacement())
134 lineItem->setLine( m_internalRect.right()+5,layout[i], m_internalRect.right(),layout[i]);
135 else
136 lineItem->setLine( m_internalRect.left()-5,layout[i], m_internalRect.left(),layout[i]);
125 }
137 }
126 }
138 }
127
139
@@ -145,28 +157,28 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) c
145 qreal height=0;
157 qreal height=0;
146
158
147 switch (which) {
159 switch (which) {
148 case Qt::MinimumSize: {
160 case Qt::MinimumSize: {
149 int count = qMax(ticksList.first().count() , ticksList.last().count());
161 int count = qMax(ticksList.first().count() , ticksList.last().count());
150 width=fn.averageCharWidth()*count+label_padding;
162 width=fn.averageCharWidth()*count+label_padding;
151 height=fn.height();
163 height=fn.height();
152 height=qMax(height,base.height());
164 height=qMax(height,base.height());
153 width+=base.width();
165 width+=base.width();
154 sh = QSizeF(width,height);
166 sh = QSizeF(width,height);
155 break;
167 break;
156 }
168 }
157 case Qt::PreferredSize:
169 case Qt::PreferredSize:
170 {
171 for (int i = 0; i < ticksList.size(); ++i)
158 {
172 {
159 for (int i = 0; i < ticksList.size(); ++i)
173 width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+label_padding,width);
160 {
174 height+=fn.height();
161 width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+label_padding,width);
162 height+=fn.height();
163 }
164 height=qMax(height,base.height());
165 width+=base.width();
166 sh = QSizeF(width,height);
167 break;
168 }
175 }
169 default:
176 height=qMax(height,base.height());
177 width+=base.width();
178 sh = QSizeF(width,height);
179 break;
180 }
181 default:
170 break;
182 break;
171 }
183 }
172 return sh;
184 return sh;
@@ -31,9 +31,9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 static const qreal golden_ratio = 0.25;
31 static const qreal golden_ratio = 0.25;
32
32
33 ChartLayout::ChartLayout(ChartPresenter* presenter):
33 ChartLayout::ChartLayout(ChartPresenter* presenter):
34 m_presenter(presenter),
34 m_presenter(presenter),
35 m_margins(20,20,20,20),
35 m_margins(20,20,20,20),
36 m_minChartRect(0,0,200,200)
36 m_minChartRect(0,0,200,200)
37 {
37 {
38
38
39 }
39 }
@@ -87,7 +87,7 void ChartLayout::setGeometry(const QRectF& rect)
87
87
88 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
88 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
89 {
89 {
90 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
90 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
91 }
91 }
92
92
93 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
93 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
@@ -118,13 +118,26 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<C
118 QSizeF vertical(0,0);
118 QSizeF vertical(0,0);
119 QSizeF horizontal(0,0);
119 QSizeF horizontal(0,0);
120
120
121 int topAxisCount = 0;
122 int bottomAxisCount = 0;
123 int leftAxisCount = 0;
124 int rightAxisCount = 0;
125
121 // check axis size
126 // check axis size
122 foreach(ChartAxis* axis , axes) {
127 foreach(ChartAxis* axis , axes) {
123 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
128 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
124 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
129 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
130 if (axis->alternativePlacement())
131 rightAxisCount++;
132 else
133 leftAxisCount++;
125 }
134 }
126 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
135 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
127 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
136 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
137 if (axis->alternativePlacement())
138 topAxisCount++;
139 else
140 bottomAxisCount++;
128 }
141 }
129
142
130 }
143 }
@@ -133,10 +146,37 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<C
133
146
134 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
147 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
135
148
149 // axis area width
150 // TODO: replase with dynamic size code
151 int axisWidth = 35;
152
153 // get the final size of the plot rect
154 rect.adjust(leftAxisCount * axisWidth, topAxisCount * axisWidth, -rightAxisCount * axisWidth, -bottomAxisCount * axisWidth);
136 m_presenter->setChartsGeometry(rect);
155 m_presenter->setChartsGeometry(rect);
137
156
157 leftAxisCount = 0;
158 rightAxisCount = 0;
159 bottomAxisCount = 0;
160 topAxisCount = 0;
161
162 // adjust the axes internal and external rects
138 foreach(ChartAxis* axis , axes) {
163 foreach(ChartAxis* axis , axes) {
139 axis->setGeometry(geometry);
164 if(axis->orientation()==Qt::Vertical) {
165 axis->setInternalRect(rect.adjusted(-leftAxisCount * axisWidth, 0, rightAxisCount * axisWidth, 0));
166 axis->setGeometry(rect.adjusted(-(leftAxisCount + 1) * axisWidth, 0, (rightAxisCount + 1) * axisWidth, 0));
167 if (axis->alternativePlacement())
168 rightAxisCount++;
169 else
170 leftAxisCount++;
171 }
172 else if(axis->orientation()==Qt::Horizontal) {
173 axis->setInternalRect(rect.adjusted(0, -topAxisCount * axisWidth, 0, bottomAxisCount * axisWidth));
174 axis->setGeometry(rect.adjusted(0, -(topAxisCount + 1) * axisWidth, 0, (bottomAxisCount + 1) * axisWidth));
175 if (axis->alternativePlacement())
176 topAxisCount++;
177 else
178 bottomAxisCount++;
179 }
140 }
180 }
141
181
142 return rect;
182 return rect;
@@ -165,34 +205,33 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* lege
165 QRectF result;
205 QRectF result;
166
206
167 switch (legend->alignment()) {
207 switch (legend->alignment()) {
168
208 case Qt::AlignTop: {
169 case Qt::AlignTop: {
209 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
170 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
210 result = geometry.adjusted(0,legendRect.height(),0,0);
171 result = geometry.adjusted(0,legendRect.height(),0,0);
211 break;
172 break;
212 }
173 }
213 case Qt::AlignBottom: {
174 case Qt::AlignBottom: {
214 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
175 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
215 result = geometry.adjusted(0,0,0,-legendRect.height());
176 result = geometry.adjusted(0,0,0,-legendRect.height());
216 break;
177 break;
217 }
178 }
218 case Qt::AlignLeft: {
179 case Qt::AlignLeft: {
219 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
180 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
220 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
181 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
221 result = geometry.adjusted(width,0,0,0);
182 result = geometry.adjusted(width,0,0,0);
222 break;
183 break;
223 }
184 }
224 case Qt::AlignRight: {
185 case Qt::AlignRight: {
225 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
186 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
226 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
187 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
227 result = geometry.adjusted(0,0,-width,0);
188 result = geometry.adjusted(0,0,-width,0);
228 break;
189 break;
229 }
190 }
230 default: {
191 default: {
231 legendRect = QRectF(0,0,0,0);
192 legendRect = QRectF(0,0,0,0);
232 result = geometry;
193 result = geometry;
233 break;
194 break;
234 }
195 }
196 }
235 }
197
236
198 legend->setGeometry(legendRect);
237 legend->setGeometry(legendRect);
@@ -202,22 +241,22 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* lege
202
241
203 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
242 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
204 {
243 {
205 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
244 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
206 return geometry.adjusted(0,0,minSize.width(),minSize.height());
245 return geometry.adjusted(0,0,minSize.width(),minSize.height());
207 }
246 }
208
247
209 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
248 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
210 {
249 {
211 title->setGeometry(geometry);
250 title->setGeometry(geometry);
212 QPointF center = geometry.center() - title->boundingRect().center();
251 QPointF center = geometry.center() - title->boundingRect().center();
213 title->setPos(center.x(),title->pos().y());
252 title->setPos(center.x(),title->pos().y());
214 return geometry.adjusted(0,title->boundingRect().height(),0,0);
253 return geometry.adjusted(0,title->boundingRect().height(),0,0);
215 }
254 }
216
255
217 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
256 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
218 {
257 {
219 QSizeF min = title->sizeHint(Qt::MinimumSize);
258 QSizeF min = title->sizeHint(Qt::MinimumSize);
220 return minimum.adjusted(0,0,min.width(),min.height());
259 return minimum.adjusted(0,0,min.width(),min.height());
221 }
260 }
222
261
223 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
262 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
@@ -135,26 +135,27 void ChartPresenter::selectVisibleAxis()
135
135
136 while (i.hasNext()) {
136 while (i.hasNext()) {
137 i.next();
137 i.next();
138 i.key()->hide();
138 // i.key()->hide();
139 i.key()->show();
139 }
140 }
140
141
141 i.toFront();
142 // i.toFront();
142
143
143 bool axisX=false;
144 // bool axisX=false;
144 bool axisY=false;
145 // bool axisY=false;
145
146
146 while (i.hasNext()) {
147 // while (i.hasNext()) {
147 i.next();
148 // i.next();
148 if(i.key()->orientation()==Qt::Vertical && !axisY) {
149 // if(i.key()->orientation()==Qt::Vertical && !axisY) {
149 axisY=true;
150 // axisY=true;
150 i.key()->show();
151 // i.key()->show();
151 }
152 // }
152 if(i.key()->orientation()==Qt::Horizontal && !axisX) {
153 // if(i.key()->orientation()==Qt::Horizontal && !axisX) {
153 axisX=true;
154 // axisX=true;
154 i.key()->show();
155 // i.key()->show();
155 }
156 // }
156
157
157 }
158 // }
158 }
159 }
159
160
160
161
General Comments 0
You need to be logged in to leave comments. Login now