##// 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 }
@@ -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);
@@ -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 };
@@ -76,7 +76,11 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) {
@@ -87,7 +91,11 void ChartValueAxisX::updateGeometry()
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);
95 if (m_chartAxis->alternativePlacement())
96 labelItem->setPos(layout[i] - center.x(), m_internalRect.top() - rect.height() - label_padding);
97 else
98 labelItem->setPos(layout[i] - center.x(), m_internalRect.bottom() + label_padding);
91 if(labelItem->pos().x() <= width ||
99 if(labelItem->pos().x() <= width ||
92 labelItem->pos().x() < m_rect.left() ||
100 labelItem->pos().x() < m_rect.left() ||
93 labelItem->pos().x() + rect.width() > m_rect.right()){
101 labelItem->pos().x() + rect.width() > m_rect.right()){
@@ -104,7 +112,11 void ChartValueAxisX::updateGeometry()
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
@@ -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
@@ -104,7 +108,11 void ChartValueAxisY::updateGeometry()
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()) {
@@ -121,7 +129,11 void ChartValueAxisY::updateGeometry()
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
@@ -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,7 +205,6 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
169 case Qt::AlignTop: {
208 case Qt::AlignTop: {
170 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
209 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
171 result = geometry.adjusted(0,legendRect.height(),0,0);
210 result = geometry.adjusted(0,legendRect.height(),0,0);
@@ -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