##// END OF EJS Templates
Adds more axis handling...
Michal Klocek -
r176:6a1519e364a3
parent child
Show More
@@ -0,0 +1,15
1 !include( ../../common.pri ) {
2 error( "Couldn't find the common.pri file!" )
3 }
4
5 !include( ../../integrated.pri ) {
6 error( "Couldn't find the integrated.pri file !")
7 }
8
9 TARGET = axisChart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp
13
14
15
@@ -0,0 +1,56
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartview.h>
4 #include <qlinechartseries.h>
5 #include <qchart.h>
6 #include <qchartaxis.h>
7 #include <cmath>
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
11 #define PI 3.14159265358979
12
13 int main(int argc, char *argv[])
14 {
15 QApplication a(argc, argv);
16
17 QMainWindow window;
18
19 QLineChartSeries* series0 = new QLineChartSeries();
20 QPen blue(Qt::blue);
21 blue.setWidth(3);
22 series0->setPen(blue);
23 QLineChartSeries* series1 = new QLineChartSeries();
24 QPen red(Qt::red);
25 red.setWidth(3);
26 series1->setPen(red);
27
28 int numPoints = 100;
29
30 for (int x = 0; x <= numPoints; ++x) {
31 series0->add(x, fabs(sin(PI/50*x)*100));
32 series1->add(x, fabs(cos(PI/50*x)*100));
33 }
34
35 QChartView* chartView = new QChartView(&window);
36
37 chartView->setRenderHint(QPainter::Antialiasing);
38 chartView->setTitle("Basic line chart example");
39 chartView->addSeries(series0);
40 chartView->addSeries(series1);
41 chartView->setChartBackgroundBrush(Qt::white);
42
43 QChartAxis axis = chartView->defaultAxisX();
44 axis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
45 axis.setGridPen(Qt::DashLine);
46
47 chartView->setDefaultAxisX(axis);
48 //axis.setShadesBrush(Qt::gray);
49 chartView->setDefaultAxisY(axis);
50
51 window.setCentralWidget(chartView);
52 window.resize(400, 300);
53 window.show();
54
55 return a.exec();
56 }
@@ -7,4 +7,5 SUBDIRS += linechart \
7 7 percentbarchart \
8 8 scatter \
9 9 piechart \
10 dynamiclinechart
10 dynamiclinechart \
11 axischart
@@ -3,18 +3,22
3 3 #include <QPainter>
4 4 #include <QDebug>
5 5
6 #define LABEL_PADDING 5
6 static int label_padding = 5;
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
11 ChartItem(parent),
12 m_axis(axis),
13 m_ticks(4),
14 m_type(type)
10 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 ChartItem(parent),
12 m_ticks(4),
13 m_type(type),
14 m_labelsAngle(0),
15 m_shadesEnabled(true),
16 m_grid(this),
17 m_shades(this),
18 m_labels(this)
15 19 {
16 20 //initial initialization
17 handleAxisChanged();
21 createItems();
18 22 }
19 23
20 24 AxisItem::~AxisItem()
@@ -26,148 +30,257 QRectF AxisItem::boundingRect() const
26 30 return m_rect;
27 31 }
28 32
33 void AxisItem::createItems()
34 {
35 for (int i = 0; i <= m_ticks; ++i) {
36 m_grid.addToGroup(new QGraphicsLineItem(this));
37 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
38 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
39 }
40 }
41
42 void AxisItem::clear()
43 {
44 foreach(QGraphicsItem* item , m_shades.childItems()) {
45 delete item;
46 }
47
48 foreach(QGraphicsItem* item , m_grid.childItems()) {
49 delete item;
50 }
51
52 foreach(QGraphicsItem* item , m_labels.childItems()) {
53 delete item;
54 }
55
56 }
57
58 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
59 {
29 60
30 /*
31 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
61 }
62
63 void AxisItem::updateDomain()
32 64 {
33 if (!m_rect.isValid())
34 return;
35 65
36 if(m_type==X_AXIS) {
66 QList<QGraphicsItem *> lines = m_grid.childItems();
67 QList<QGraphicsItem *> labels = m_labels.childItems();
68 QList<QGraphicsItem *> shades = m_shades.childItems();
37 69
38 const qreal deltaX = m_rect.width() / m_ticks;
70 switch (m_type)
71 {
72 case X_AXIS:
73 {
74 const qreal deltaX = m_rect.width() / m_ticks;
39 75
40 for (int i = 0; i <= m_ticks; ++i) {
76 for (int i = 0; i <= m_ticks; ++i) {
41 77
42 int x = i * deltaX + m_rect.left();
78 int x = i * deltaX + m_rect.left();
43 79
44 if(i==0) x--;
45 if(i==m_ticks) x++;
80 qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks);
46 81
47 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
48 / m_ticks);
49 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
50 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
82 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
83 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
51 84
52 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
53 }
54 }
85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 labelItem->setText(QString::number(label));
87 QPointF center = labelItem->boundingRect().center();
88 labelItem->setTransformOriginPoint(center.x(), center.y());
89 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
55 90
56 if(m_type==Y_AXIS) {
91 if(i%2){
92 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
93 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
94 }
95 }
96 }
97 break;
57 98
58 const qreal deltaY = (m_rect.height()) / m_ticks;
99 case Y_AXIS:
100 {
101 const qreal deltaY = m_rect.height()/ m_ticks;
59 102
60 for (int j = 0; j <= m_ticks; ++j) {
103 for (int i = 0; i <= m_ticks; ++i) {
61 104
62 int y = j * -deltaY + m_rect.bottom();
105 int y = i * -deltaY + m_rect.bottom();
63 106
64 if(j==0) y++;
65 if(j==m_ticks) y--;
107 qreal label = m_domain.m_minY + (i * m_domain.spanY()/ m_ticks);
66 108
67 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
68 / m_ticks);
109 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
110 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
69 111
70 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
71 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
72 //TODO : margin = 50 ;
73 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
74 Qt::AlignRight | Qt::AlignVCenter,
75 QString::number(label));
76 }
77 }
112 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
113 labelItem->setText(QString::number(label));
114 QPointF center = labelItem->boundingRect().center();
115 labelItem->setTransformOriginPoint(center.x(), center.y());
116 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
78 117
79 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
118
119 if(i%2){
120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
121 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
122 }
123 }
124 }
125 break;
126 default:
127 qDebug()<<"Unknown axis type";
128 break;
129 }
80 130 }
81 */
82 void AxisItem::createItems()
131
132 void AxisItem::handleAxisChanged(const QChartAxis& axis)
83 133 {
134 if(axis.isGridVisible()) {
135 setGridOpacity(100);
136 }
137 else {
138 setGridOpacity(0);
139 }
84 140
85 if(!m_rect.isValid()) return;
141 if(axis.isLabelsVisible())
142 {
143 setLabelsOpacity(100);
144 }
145 else {
146 setLabelsOpacity(0);
147 }
86 148
87 switch (m_type)
149 if(axis.isShadesVisible()) {
150 setShadesOpacity(100);
151 }
152 else {
153 setShadesOpacity(0);
154 }
155
156 switch(axis.labelsOrientation())
88 157 {
89 case X_AXIS:
90 {
91 const qreal deltaX = m_rect.width() / m_ticks;
158 case QChartAxis::LabelsOrientationHorizontal:
159 setLabelsAngle(0);
160 break;
161 case QChartAxis::LabelsOrientationVertical:
162 setLabelsAngle(90);
163 break;
164 case QChartAxis::LabelsOrientationSlide:
165 setLabelsAngle(-45);
166 break;
167 default:
168 break;
169 }
92 170
93 for (int i = 0; i <= m_ticks; ++i) {
171 setLabelsPen(axis.labelsPen());
172 setLabelsBrush(axis.labelsBrush());
173 setLabelsFont(axis.labelFont());
174 setGridPen(axis.gridPen());
175 setShadesPen(axis.shadesPen());
176 setShadesBrush(axis.shadesBrush());
94 177
95 int x = i * deltaX + m_rect.left();
178 }
96 179
97 qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks);
180 void AxisItem::handleDomainChanged(const Domain& domain)
181 {
182 m_domain = domain;
183 updateDomain();
184 update();
185 }
98 186
99 m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this);
187 void AxisItem::handleGeometryChanged(const QRectF& rect)
188 {
189 m_rect = rect;
190 updateDomain();
191 update();
192 }
100 193
101 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
102 QPointF center = text->boundingRect().center();
103 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
104 //text->rotate(-45);
105 m_labels<<text;
106 }
107 }
108 break;
194 void AxisItem::setGridOpacity(qreal opacity)
195 {
196 m_grid.setOpacity(opacity);
197 }
109 198
110 case Y_AXIS:
111 {
112 const qreal deltaY = m_rect.height()/ m_ticks;
113 199
114 for (int j = 0; j <= m_ticks; ++j) {
200 qreal AxisItem::gridOpacity() const
201 {
202 return m_grid.opacity();
203 }
115 204
116 int y = j * -deltaY + m_rect.bottom();
205 void AxisItem::setLabelsOpacity(qreal opacity)
206 {
207 m_labels.setOpacity(opacity);
208 }
117 209
118 qreal label = m_domain.m_minY + (j * m_domain.spanY()
119 / m_ticks);
210 qreal AxisItem::labelsOpacity() const
211 {
212 return m_labels.opacity();
213 }
120 214
121 m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this);
122 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
123 QPointF center = text->boundingRect().center();
124 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
215 void AxisItem::setShadesOpacity(qreal opacity)
216 {
217 m_shades.setOpacity(opacity);
218 }
125 219
126 m_labels<<text;
220 qreal AxisItem::shadesOpacity() const
221 {
222 return m_shades.opacity();
223 }
127 224
128 }
129 }
130 break;
131 default:
132 qDebug()<<"Unknown axis type";
133 break;
225 void AxisItem::setLabelsAngle(int angle)
226 {
227 foreach(QGraphicsItem* item , m_labels.childItems()) {
228 QPointF center = item->boundingRect().center();
229 item->setRotation(angle);
134 230 }
231
232 m_labelsAngle=angle;
135 233 }
136 234
137 void AxisItem::clear()
235 void AxisItem::setLabelsPen(const QPen& pen)
138 236 {
139 qDeleteAll(m_shades);
140 m_shades.clear();
141 qDeleteAll(m_grid);
142 m_grid.clear();
143 qDeleteAll(m_labels);
144 m_labels.clear();
237 foreach(QGraphicsItem* item , m_labels.childItems()) {
238 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
239 }
145 240 }
146 241
147 void AxisItem::updateDomain()
242 void AxisItem::setLabelsBrush(const QBrush& brush)
148 243 {
149 clear();
150 createItems();
244 foreach(QGraphicsItem* item , m_labels.childItems()) {
245 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
246 }
151 247 }
152 248
153 void AxisItem::handleAxisChanged()
249 void AxisItem::setLabelsFont(const QFont& font)
154 250 {
155 //m_axis->
251 foreach(QGraphicsItem* item , m_labels.childItems()) {
252 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
253 }
156 254 }
157 255
158 void AxisItem::handleDomainChanged(const Domain& domain)
256 void AxisItem::setShadesBrush(const QBrush& brush)
159 257 {
160 m_domain = domain;
161 clear();
162 createItems();
258 foreach(QGraphicsItem* item , m_shades.childItems()) {
259 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
260 }
163 261 }
164 262
165 void AxisItem::handleGeometryChanged(const QRectF& rect)
263 void AxisItem::setShadesPen(const QPen& pen)
166 264 {
167 Q_ASSERT(rect.isValid());
168 m_rect = rect;
265 foreach(QGraphicsItem* item , m_shades.childItems()) {
266 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
267 }
268 }
269
270 void AxisItem::setGridPen(const QPen& pen)
271 {
272 foreach(QGraphicsItem* item , m_grid.childItems()) {
273 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
274 }
275 }
276
277 void AxisItem::setTicks(int count)
278 {
279 if(count!=m_ticks){
169 280 clear();
281 m_ticks=count;
170 282 createItems();
283 }
171 284 }
172 285
173 286 //TODO "nice numbers algorithm"
@@ -15,46 +15,58 class AxisItem : public QObject, public ChartItem
15 15 public:
16 16 enum AxisType{X_AXIS,Y_AXIS};
17 17
18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 19 ~AxisItem();
20 20
21 21 //from QGraphicsItem
22 22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 24
25 protected slots:
26 void handleAxisChanged();
27 void handleDomainChanged(const Domain& domain);
28 void handleGeometryChanged(const QRectF& size);
25 AxisType axisType() const {return m_type;};
29 26
30 protected:
31 void updateDomain();
27 void setGridOpacity(qreal opacity);
28 qreal gridOpacity() const;
32 29
33 private:
34 void clear();
30 void setLabelsOpacity(qreal opacity);
31 qreal labelsOpacity() const;
35 32
36 public:
37 AxisType axisType() const {return m_type;};
33 void setShadesOpacity(qreal opacity);
34 qreal shadesOpacity() const;
38 35
39 protected:
36 void setLabelsAngle(int angle);
37 int labelsAngle()const { return m_labelsAngle; }
38
39 void setTicks(int count);
40 int ticks() const { return m_ticks;}
40 41
42 void setShadesBrush(const QBrush& brush);
43 void setShadesPen(const QPen& pen);
41 44
45 void setGridPen(const QPen& pen);
46
47 void setLabelsPen(const QPen& pen);
48 void setLabelsBrush(const QBrush& brush);
49 void setLabelsFont(const QFont& font);
50
51 public slots:
52 void handleAxisChanged(const QChartAxis& axis);
53 void handleDomainChanged(const Domain& domain);
54 void handleGeometryChanged(const QRectF& size);
55 protected:
56 void updateDomain();
42 57 private:
58 void clear();
43 59 void createItems();
44 60 private:
45
46 QChartAxis* m_axis;
47 61 AxisType m_type;
48 62 int m_ticks;
49 63 Domain m_domain;
50 64 QRectF m_rect;
51
52 QPainterPath m_path;
53
54
55 QList<QGraphicsLineItem*> m_grid;
56 QList<QGraphicsRectItem*> m_shades;
57 QList<QGraphicsSimpleTextItem*> m_labels;
65 int m_labelsAngle;
66 bool m_shadesEnabled;
67 QGraphicsItemGroup m_grid;
68 QGraphicsItemGroup m_shades;
69 QGraphicsItemGroup m_labels;
58 70
59 71 };
60 72
@@ -26,15 +26,13 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char
26 26 m_chart(chart),
27 27 m_dataset(dataset),
28 28 m_chartTheme(0),
29 m_axisXItem(new AxisItem(AxisItem::X_AXIS,m_chart)),
30 m_axisYItem(new AxisItem(AxisItem::Y_AXIS,m_chart)),
29 31 m_domainIndex(0),
30 32 m_marginSize(0),
31 m_axisX(new QChartAxis(this)),
32 m_axisY(new QChartAxis(this)),
33 33 m_rect(QRectF(QPoint(0,0),m_chart->size()))
34 34 {
35 35 setChartTheme(QChart::ChartThemeDefault);
36 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
37 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
38 36 createConnections();
39 37 }
40 38
@@ -46,14 +44,10 void ChartPresenter::createConnections()
46 44 {
47 45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
48 46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
49
50 QMapIterator<QChartAxis*,AxisItem*> i(m_axisItems);
51
52 while (i.hasNext()) {
53 i.next();
54 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),i.value(),SLOT(handleGeometryChanged(const QRectF&)));
55 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),i.value(),SLOT(handleDomainChanged(const Domain&)));
56 }
47 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisXItem,SLOT(handleGeometryChanged(const QRectF&)));
48 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisXItem,SLOT(handleDomainChanged(const Domain&)));
49 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),m_axisYItem,SLOT(handleGeometryChanged(const QRectF&)));
50 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),m_axisYItem,SLOT(handleDomainChanged(const Domain&)));
57 51 }
58 52
59 53 void ChartPresenter::handleGeometryChanged()
@@ -206,58 +200,70 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
206 200 i.next();
207 201 index++;
208 202 m_chartTheme->decorate(i.value(),i.key(),index);
209 }
210 203 }
211 204
205 m_chartTheme->decorate(m_axisX, m_axisXItem);
206 m_chartTheme->decorate(m_axisY, m_axisYItem);
207
208 }
209
212 210
213 211 QChart::ChartTheme ChartPresenter::chartTheme()
214 212 {
215 213 return m_chartTheme->id();
216 214 }
217 215
218 QChartAxis* ChartPresenter::axisX()
216 void ChartPresenter::setDefaultAxisX(const QChartAxis& axis)
219 217 {
220 return m_axisX;
218 //if(m_axisX != axis) {
219 m_axisX = axis;
220 m_axisXItem->handleAxisChanged(m_axisX);
221 //}
221 222 }
222 223
223 QChartAxis* ChartPresenter::axisY()
224 void ChartPresenter::setDefaultAxisY(const QChartAxis& axis)
224 225 {
225 return m_axisY;
226 // if(m_axisY != axis) {
227 m_axisY = axis;
228 m_axisYItem->handleAxisChanged(m_axisY);
229 //}
226 230 }
227 231
228 QChartAxis* ChartPresenter::addAxisX()
232 QChartAxis ChartPresenter::defaultAxisX() const
229 233 {
230 //only one axis
231 if(m_axisX==0){
232 m_axisX = new QChartAxis(this);
233 m_axisItems[m_axisX] = new AxisItem(m_axisX,AxisItem::X_AXIS,m_chart);
234 }
235 return m_axisX;
234 return m_axisX;
236 235 }
237 236
238 QChartAxis* ChartPresenter::addAxisY()
237 QChartAxis ChartPresenter::defaultAxisY() const
239 238 {
240 if(m_axisY==0){
241 m_axisY = new QChartAxis(this);
242 m_axisItems[m_axisY] = new AxisItem(m_axisY,AxisItem::Y_AXIS,m_chart);
243 return m_axisY;
244 }
239 return m_axisY;
240 }
245 241
246 QChartAxis* axis = new QChartAxis(this);
247 m_axisItems[axis] = new AxisItem(axis,AxisItem::Y_AXIS,m_chart);
248 return axis;
242 QChartAxis ChartPresenter::axisY(int id) const
243 {
244 return m_axis.value(id);
249 245 }
250 246
251 void ChartPresenter::removeAxis(QChartAxis* axis)
247 int ChartPresenter::addAxisY(const QChartAxis& axis)
252 248 {
253 AxisItem* item = m_axisItems.take(axis);
254 if(item){
255 delete item;
256 delete axis;
249 int key =0 ;
250
251 while(m_axis.contains(key)){
252 key++;
253 //TODO overflow
257 254 }
258 //reset pointers to default ones
259 if(axis == m_axisX) m_axisX=0;
260 else if(axis == m_axisY) m_axisY=0;
255
256 m_axis.insert(key,axis);
257 m_axisItems.insert(key,new AxisItem(AxisItem::Y_AXIS,m_chart));
258
259 return key;
260 }
261
262
263 void ChartPresenter::removeAxisY(int id)
264 {
265 m_axis.remove(id);
266 delete m_axisItems.take(id);
261 267 }
262 268
263 269 #include "moc_chartpresenter_p.cpp"
@@ -3,6 +3,7
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 #include "qchartaxis.h"
6 7 #include <QRectF>
7 8
8 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -33,11 +34,13 public:
33 34 void setChartTheme(QChart::ChartTheme theme);
34 35 QChart::ChartTheme chartTheme();
35 36
36 QChartAxis* axisX();
37 QChartAxis* axisY();
38 QChartAxis* addAxisX();
39 QChartAxis* addAxisY();
40 void removeAxis(QChartAxis* axis);
37 void setDefaultAxisX(const QChartAxis& axis);
38 void setDefaultAxisY(const QChartAxis& axis);
39 QChartAxis defaultAxisX() const;
40 QChartAxis defaultAxisY() const;
41 QChartAxis axisY(int id) const;
42 int addAxisY(const QChartAxis& axis);
43 void removeAxisY(int id);
41 44
42 45 private:
43 46 void createConnections();
@@ -48,19 +51,20 public slots:
48 51 void handleSeriesChanged(QChartSeries* series);
49 52 //void handleDomainChanged(Domain oldDomain,Domain newDomain);
50 53 void handleGeometryChanged();
51
52 54 signals:
53 55 void geometryChanged(const QRectF& rect);
54
55 56 private:
56 57 QMap<QChartSeries*,ChartItem*> m_chartItems;
57 QMap<QChartAxis*,AxisItem*> m_axisItems;
58 QMap<int,AxisItem*> m_axisItems;
59 QMap<int,QChartAxis> m_axis;
58 60 QChart* m_chart;
59 61 ChartDataSet* m_dataset;
60 62 QVector<Domain> m_domains;
61 63 ChartTheme *m_chartTheme;
62 QChartAxis* m_axisX;
63 QChartAxis* m_axisY;
64 QChartAxis m_axisX;
65 AxisItem* m_axisXItem;
66 QChartAxis m_axisY;
67 AxisItem* m_axisYItem;
64 68 int m_domainIndex;
65 69 int m_marginSize;
66 70 QRectF m_rect;
@@ -1,5 +1,6
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 #include "qchartaxis.h"
3 4
4 5
5 6 //series
@@ -207,4 +208,13 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
207 208 }
208 209
209 210
211 void ChartTheme::decorate(QChartAxis& axis,AxisItem* item)
212 {
213 //TODO: dummy defults for now
214
215 axis.setLabelsBrush(Qt::black);
216 axis.setLabelsPen(Qt::NoPen);
217 item->handleAxisChanged(axis);
218 }
219
210 220 QTCOMMERCIALCHART_END_NAMESPACE
@@ -34,6 +34,7 public:
34 34 void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count);
35 35 void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count);
36 36 void decorate(PiePresenter* item, QPieSeries* series, int count);
37 void decorate(QChartAxis& axis,AxisItem* item);
37 38
38 39 protected:
39 40 QChart::ChartTheme m_id;
@@ -143,29 +143,39 void QChart::zoomReset()
143 143 m_presenter->zoomReset();
144 144 }
145 145
146 QChartAxis* QChart::axisX()
146 void QChart::setDefaultAxisX(const QChartAxis& axis)
147 147 {
148 return m_presenter->axisX();
148 m_presenter->setDefaultAxisX(axis);
149 149 }
150 150
151 QChartAxis* QChart::axisY()
151 void QChart::setDefaultAxisY(const QChartAxis& axis)
152 152 {
153 return m_presenter->axisY();
153 m_presenter->setDefaultAxisY(axis);
154 154 }
155 155
156 QChartAxis* QChart::addAxisX()
156 QChartAxis QChart::defaultAxisX() const
157 157 {
158 return m_presenter->addAxisX();
158 return m_presenter->defaultAxisX();
159 159 }
160 160
161 QChartAxis* QChart::addAxisY()
161 QChartAxis QChart::defaultAxisY() const
162 162 {
163 return m_presenter->addAxisY();
163 return m_presenter->defaultAxisY();
164 164 }
165 165
166 void QChart::removeAxis(QChartAxis* axis)
166 int QChart::addAxisY(const QChartAxis& axis)
167 167 {
168 m_presenter->removeAxis(axis);
168 return m_presenter->addAxisY(axis);
169 }
170
171 QChartAxis QChart::axisY(int id) const
172 {
173 return m_presenter->axisY(id);
174 }
175
176 void QChart::removeAxisY(int id)
177 {
178 m_presenter->removeAxisY(id);
169 179 }
170 180
171 181 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
@@ -67,11 +67,13 public:
67 67 void zoomOut();
68 68 void zoomReset();
69 69
70 QChartAxis* axisX();
71 QChartAxis* axisY();
72 QChartAxis* addAxisX();
73 QChartAxis* addAxisY();
74 void removeAxis(QChartAxis* axis);
70 void setDefaultAxisX(const QChartAxis& axis);
71 void setDefaultAxisY(const QChartAxis& axis);
72 QChartAxis defaultAxisX() const;
73 QChartAxis defaultAxisY() const;
74 QChartAxis axisY(int id) const;
75 int addAxisY(const QChartAxis& axis);
76 void removeAxisY(int id);
75 77
76 78 protected:
77 79 void resizeEvent(QGraphicsSceneResizeEvent *event);
@@ -1,13 +1,12
1
2 1 #include "qchartaxis.h"
3 2
4 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 4
6 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
5 QChartAxis::QChartAxis():
7 6 m_axisVisible(true),
8 m_girdVisible(true),
7 m_gridVisible(true),
9 8 m_labelsVisible(true),
10 m_rowShadesVisible(true)
9 m_shadesVisible(true)
11 10 {
12 11 // TODO Auto-generated constructor stub
13 12
@@ -20,12 +19,12 QChartAxis::~QChartAxis()
20 19
21 20 void QChartAxis::setAxisPen(const QPen& pen)
22 21 {
23 m_axisPen = pen;
22 m_axisPen=pen;
24 23 }
25 24
26 25 void QChartAxis::setAxisBrush(const QBrush& brush)
27 26 {
28 m_axisBrush = brush;
27 m_axisBrush=brush;
29 28 }
30 29
31 30 void QChartAxis::setAxisVisible(bool visible)
@@ -35,7 +34,12 void QChartAxis::setAxisVisible(bool visible)
35 34
36 35 void QChartAxis::setGridVisible(bool visible)
37 36 {
38 m_girdVisible=visible;
37 m_gridVisible=visible;
38 }
39
40 void QChartAxis::setGridPen(const QPen& pen)
41 {
42 m_gridPen=pen;
39 43 }
40 44
41 45 void QChartAxis::setLabelsVisible(bool visible)
@@ -43,11 +47,40 void QChartAxis::setLabelsVisible(bool visible)
43 47 m_labelsVisible=visible;
44 48 }
45 49
46 void QChartAxis::setRowShadesVisible(bool visible)
50 void QChartAxis::setLabelsPen(const QPen& pen)
51 {
52 m_labelsPen=pen;
53 }
54
55 void QChartAxis::setLabelsBrush(const QBrush& brush)
56 {
57 m_labelsBrush=brush;
58 }
59
60 void QChartAxis::setLabelsFont(const QFont& font)
61 {
62 m_labelsFont=font;
63 }
64
65 void QChartAxis::setLabelsOrientation(LabelsOrientation orientation)
66 {
67 m_labelsOrientation=orientation;
68 }
69
70 void QChartAxis::setShadesVisible(bool visible)
71 {
72 m_shadesVisible=visible;
73 }
74
75 void QChartAxis::setShadesPen(const QPen& pen)
76 {
77 m_shadesPen=pen;
78 }
79
80 void QChartAxis::setShadesBrush(const QBrush& brush)
47 81 {
48 m_rowShadesVisible=visible;
82 m_shadesBrush=brush;
49 83 }
50 84
51 #include "moc_qchartaxis.cpp"
52 85
53 86 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,62 +3,52
3 3
4 4 #include <qchartglobal.h>
5 5 #include <QPen>
6 #include <QFont>
6 7
7 8
8 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 10
10 class QChartAxis : public QObject
11 class QChartAxis
11 12 {
12 Q_OBJECT
13
14 Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged);
15 Q_PROPERTY(QPen axisPen READ axisPen WRITE setAxisPen NOTIFY axisPenChanged);
16 Q_PROPERTY(QBrush axisBrush READ axisBrush WRITE setAxisBrush NOTIFY axisBurshChanged);
17
18 // Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged);
19 13 public:
20 enum LabelOrientation{ HORIZONTAL, VERTICAL , SLIDE };
14 enum LabelsOrientation{ LabelsOrientationHorizontal, LabelsOrientationVertical , LabelsOrientationSlide };
21 15
22 QChartAxis(QObject* parent = 0);
16 QChartAxis();
23 17 virtual ~QChartAxis();
24 18
25 19 //axis
26 20 bool isAxisVisible() const { return m_axisVisible;};
27 21 void setAxisVisible(bool visible);
28 22 void setAxisPen(const QPen& pen);
29 const QPen& axisPen() const { return m_axisPen;};
23 QPen axisPen() const { return m_axisPen;};
30 24 void setAxisBrush(const QBrush& brush);
31 const QBrush& axisBrush() const { return m_axisBrush;};
25 QBrush axisBrush() const { return m_axisBrush;};
32 26
33 27 //grid
34 bool isGridVisible() const { return m_girdVisible;};
28 bool isGridVisible() const { return m_gridVisible;};
35 29 void setGridVisible(bool visible);
30 void setGridPen(const QPen& pen);
31 QPen gridPen() const {return m_gridPen;}
36 32
33 //labels
37 34 bool isLabelsVisible() const { return m_labelsVisible;};
38 35 void setLabelsVisible(bool visible);
39
40 bool isRowShadesVisible() const { return m_rowShadesVisible;};
41 void setRowShadesVisible(bool visible);
42
43 /*
44 void setLabelFont(const QFont& font);
45 const QFont& labelFont();
46
47 void setLabelPen(const QPen& pen);
48 const QPen& labelPen();
49
50 void setGridPen(const QPen& pen);
51 const QPen& gridPen();
52
53 void setGridBrush(const QBrush& brush);
54 const QBrush& gridBrush();
55 */
56
57
58 signals:
59 void axisVisibilityChanged();
60 void axisPenChanged();
61 void axisBurshChanged();
36 void setLabelsPen(const QPen& pen);
37 QPen labelsPen() const { return m_labelsPen;}
38 void setLabelsBrush(const QBrush& brush);
39 QBrush labelsBrush() const { return m_labelsBrush;}
40 void setLabelsFont(const QFont& font);
41 QFont labelFont() const { return m_labelsFont;}
42 void setLabelsOrientation(LabelsOrientation orientation);
43 LabelsOrientation labelsOrientation() const { return m_labelsOrientation;};
44
45 //shades
46 bool isShadesVisible() const { return m_shadesVisible;};
47 void setShadesVisible(bool visible);
48 void setShadesPen(const QPen& pen);
49 QPen shadesPen() const { return m_shadesPen;}
50 void setShadesBrush(const QBrush& brush);
51 QBrush shadesBrush() const { return m_shadesBrush;}
62 52
63 53
64 54 private:
@@ -67,9 +57,19 private:
67 57 QPen m_axisPen;
68 58 QBrush m_axisBrush;
69 59
70 bool m_girdVisible;
60 bool m_gridVisible;
61 QPen m_gridPen;
62
71 63 bool m_labelsVisible;
72 bool m_rowShadesVisible;
64 QPen m_labelsPen;
65 QBrush m_labelsBrush;
66 QFont m_labelsFont;
67
68 bool m_shadesVisible;
69 QPen m_shadesPen;
70 QBrush m_shadesBrush;
71
72 LabelsOrientation m_labelsOrientation;
73 73 };
74 74
75 75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,5 +1,6
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 #include "qchartaxis.h"
3 4 #include <QGraphicsView>
4 5 #include <QGraphicsScene>
5 6 #include <QRubberBand>
@@ -9,14 +10,14
9 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 11
11 12 QChartView::QChartView(QWidget *parent) :
12 QGraphicsView(parent),
13 m_scene(new QGraphicsScene()),
14 m_chart(new QChart()),
15 m_rubberBand(0),
16 m_verticalRubberBand(false),
17 m_horizonalRubberBand(false)
18 {
19 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
13 QGraphicsView(parent),
14 m_scene(new QGraphicsScene()),
15 m_chart(new QChart()),
16 m_rubberBand(0),
17 m_verticalRubberBand(false),
18 m_horizonalRubberBand(false)
19 {
20 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 21 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
21 22 setScene(m_scene);
22 23 m_chart->setMargin(50);
@@ -35,7 +36,6 void QChartView::resizeEvent(QResizeEvent *event)
35 36 QWidget::resizeEvent(event);
36 37 }
37 38
38
39 39 void QChartView::addSeries(QChartSeries* series)
40 40 {
41 41 m_chart->addSeries(series);
@@ -74,40 +74,39 void QChartView::setTitle(const QString& title)
74 74
75 75 void QChartView::setChartBackgroundBrush(const QBrush& brush)
76 76 {
77 m_chart->setChartBackgroundBrush(brush);
77 m_chart->setChartBackgroundBrush(brush);
78 78 }
79 79 void QChartView::setChartBackgroundPen(const QPen& pen)
80 80 {
81 m_chart->setChartBackgroundPen(pen);
81 m_chart->setChartBackgroundPen(pen);
82 82 }
83 83
84
85 84 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
86 85 {
87 switch(policy){
86 switch(policy) {
88 87 case VerticalRubberBand:
89 m_verticalRubberBand = true;
90 m_horizonalRubberBand = false;
91 break;
88 m_verticalRubberBand = true;
89 m_horizonalRubberBand = false;
90 break;
92 91 case HorizonalRubberBand:
93 m_verticalRubberBand = false;
94 m_horizonalRubberBand = true;
95 break;
92 m_verticalRubberBand = false;
93 m_horizonalRubberBand = true;
94 break;
96 95 case RectangleRubberBand:
97 m_verticalRubberBand = true;
98 m_horizonalRubberBand = true;
99 break;
96 m_verticalRubberBand = true;
97 m_horizonalRubberBand = true;
98 break;
100 99 case NoRubberBand:
101 100 default:
102 delete m_rubberBand;
103 m_rubberBand=0;
104 m_horizonalRubberBand = false;
105 m_verticalRubberBand = false;
106 return;
101 delete m_rubberBand;
102 m_rubberBand=0;
103 m_horizonalRubberBand = false;
104 m_verticalRubberBand = false;
105 return;
107 106 }
108 if(!m_rubberBand){
109 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
110 m_rubberBand->setEnabled(true);
107 if(!m_rubberBand) {
108 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
109 m_rubberBand->setEnabled(true);
111 110 }
112 111 }
113 112
@@ -119,7 +118,6 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
119 118 return NoRubberBand;
120 119 }
121 120
122
123 121 void QChartView::mousePressEvent(QMouseEvent *event)
124 122 {
125 123 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
@@ -138,7 +136,7 void QChartView::mousePressEvent(QMouseEvent *event)
138 136
139 137 void QChartView::mouseMoveEvent(QMouseEvent *event)
140 138 {
141 if(m_rubberBand && m_rubberBand->isVisible()){
139 if(m_rubberBand && m_rubberBand->isVisible()) {
142 140 int margin = m_chart->margin();
143 141 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
144 142 int width = event->pos().x() - m_rubberBandOrigin.x();
@@ -152,24 +150,26 void QChartView::mouseMoveEvent(QMouseEvent *event)
152 150 width= rect.width();
153 151 }
154 152 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
155 } else {
153 }
154 else {
156 155 QGraphicsView::mouseMoveEvent(event);
157 156 }
158 157 }
159 158
160 159 void QChartView::mouseReleaseEvent(QMouseEvent *event)
161 160 {
162 if(m_rubberBand){
163 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
164 m_rubberBand->hide();
165 QRect rect = m_rubberBand->geometry();
166 m_chart->zoomInToRect(rect);
167 event->accept();
168 }
161 if(m_rubberBand) {
162 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
163 m_rubberBand->hide();
164 QRect rect = m_rubberBand->geometry();
165 m_chart->zoomInToRect(rect);
166 event->accept();
167 }
169 168
170 if(event->button()==Qt::RightButton)
169 if(event->button()==Qt::RightButton)
171 170 m_chart->zoomReset();
172 }else{
171 }
172 else {
173 173 QGraphicsView::mouseReleaseEvent(event);
174 174 }
175 175 }
@@ -177,13 +177,13 void QChartView::mouseReleaseEvent(QMouseEvent *event)
177 177 void QChartView::keyPressEvent(QKeyEvent *event)
178 178 {
179 179 switch (event->key()) {
180 case Qt::Key_Plus:
180 case Qt::Key_Plus:
181 181 zoomIn();
182 182 break;
183 case Qt::Key_Minus:
183 case Qt::Key_Minus:
184 184 zoomOut();
185 185 break;
186 default:
186 default:
187 187 QGraphicsView::keyPressEvent(event);
188 188 break;
189 189 }
@@ -199,29 +199,38 QChart::ChartTheme QChartView::chartTheme() const
199 199 return m_chart->chartTheme();
200 200 }
201 201
202 QChartAxis* QChartView::axisX()
202 void QChartView::setDefaultAxisX(const QChartAxis& axis)
203 {
204 m_chart->setDefaultAxisX(axis);
205 }
206
207 void QChartView::setDefaultAxisY(const QChartAxis& axis)
203 208 {
204 return m_chart->axisX();
209 m_chart->setDefaultAxisY(axis);
205 210 }
206 211
207 QChartAxis* QChartView::axisY()
212 QChartAxis QChartView::defaultAxisX() const
208 213 {
209 return m_chart->axisY();
214 return m_chart->defaultAxisX();
210 215 }
211 216
212 QChartAxis* QChartView::addAxisX()
217 QChartAxis QChartView::defaultAxisY() const
213 218 {
214 return m_chart->addAxisX();
219 return m_chart->defaultAxisY();
215 220 }
216 221
217 QChartAxis* QChartView::addAxisY()
222 int QChartView::addAxisY(const QChartAxis& axis)
218 223 {
219 return m_chart->addAxisY();
224 return m_chart->addAxisY(axis);
220 225 }
221 226
222 void QChartView::removeAxis(QChartAxis* axis)
227 QChartAxis QChartView::axisY(int id) const
223 228 {
224 m_chart->removeAxis(axis);
229 return m_chart->axisY(id);
225 230 }
226 231
232 void QChartView::removeAxisY(int id)
233 {
234 m_chart->removeAxisY(id);
235 }
227 236 QTCOMMERCIALCHART_END_NAMESPACE
@@ -45,11 +45,13 public:
45 45 void setChartTheme(QChart::ChartTheme theme);
46 46 QChart::ChartTheme chartTheme() const;
47 47
48 QChartAxis* axisX();
49 QChartAxis* axisY();
50 QChartAxis* addAxisX();
51 QChartAxis* addAxisY();
52 void removeAxis(QChartAxis* axis);
48 void setDefaultAxisX(const QChartAxis& axis);
49 void setDefaultAxisY(const QChartAxis& axis);
50 QChartAxis defaultAxisX() const;
51 QChartAxis defaultAxisY() const;
52 QChartAxis axisY(int id) const;
53 int addAxisY(const QChartAxis& axis);
54 void removeAxisY(int id);
53 55
54 56 protected:
55 57 void mousePressEvent(QMouseEvent *event);
General Comments 0
You need to be logged in to leave comments. Login now