##// END OF EJS Templates
Add zoom support...
Michal Klocek -
r67:8474a34cb818
parent child
Show More
@@ -0,0 +1,63
1 #include "chartwidget.h"
2 #include <QMouseEvent>
3
4 ChartWidget::ChartWidget(QWidget *parent)
5 : QChartView(parent),
6 m_rubberBand(QRubberBand::Rectangle,this)
7 {
8 }
9
10 void ChartWidget::mousePressEvent(QMouseEvent *event)
11 {
12 if(event->button()!=Qt::LeftButton) return;
13
14 int margin = this->margin();
15
16 QRect rect(margin,margin,width()-2*margin,height()-2*margin);
17
18 m_origin = event->pos();
19
20 if (!rect.contains(m_origin)) return;
21
22 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
23 m_rubberBand.show();
24
25 event->accept();
26 }
27
28 void ChartWidget::mouseMoveEvent(QMouseEvent *event)
29 {
30 if(m_rubberBand.isVisible())
31 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
32 }
33
34 void ChartWidget::mouseReleaseEvent(QMouseEvent *event)
35 {
36 if( event->button()==Qt::LeftButton && m_rubberBand.isVisible()) {
37 m_rubberBand.hide();
38
39 QRect rect = m_rubberBand.geometry();
40 zoomInToRect(rect);
41 event->accept();
42 }
43
44 if(event->button()==Qt::RightButton) {
45 zoomOut();
46 }
47 }
48
49
50 void ChartWidget::keyPressEvent(QKeyEvent *event)
51 {
52 switch (event->key()) {
53 case Qt::Key_Plus:
54 zoomIn();
55 break;
56 case Qt::Key_Minus:
57 zoomOut();
58 break;
59 default:
60 QGraphicsView::keyPressEvent(event);
61 break;
62 }
63 }
@@ -0,0 +1,28
1 #ifndef CHARTWIDGET_H
2 #define CHARTWIDGET_H
3 #include <qchartview.h>
4 #include <QRubberBand>
5
6 QTCOMMERCIALCHART_USE_NAMESPACE
7
8 class ChartWidget : public QChartView
9 {
10 Q_OBJECT
11
12 public:
13 ChartWidget(QWidget *parent = 0);
14
15 protected:
16 void mousePressEvent(QMouseEvent *event);
17 void mouseMoveEvent(QMouseEvent *event);
18 void mouseReleaseEvent(QMouseEvent *event);
19 void keyPressEvent(QKeyEvent *event);
20
21 private:
22 bool rubberBandIsShown;
23 QRubberBand m_rubberBand;
24 QPoint m_origin;
25
26 };
27
28 #endif
@@ -0,0 +1,38
1 #include "chartwidget.h"
2 #include <QApplication>
3 #include <QMainWindow>
4 #include <qxychartseries.h>
5 #include <cmath>
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 #define PI 3.14159265358979
10
11 int main(int argc, char *argv[])
12 {
13 QApplication a(argc, argv);
14
15 QMainWindow window;
16
17 QXYChartSeries* series0 = QXYChartSeries::create();
18 series0->setColor(Qt::blue);
19 QXYChartSeries* series1 = QXYChartSeries::create();
20 series1->setColor(Qt::red);
21
22 int numPoints = 100;
23
24 for (int x = 0; x <= numPoints; ++x) {
25 series0->add(x, abs(sin(PI/50*x)*100));
26 series1->add(x, abs(cos(PI/50*x)*100));
27 }
28
29 ChartWidget* chartWidget = new ChartWidget(&window);
30 chartWidget->addSeries(series0);
31 chartWidget->addSeries(series1);
32
33 window.setCentralWidget(chartWidget);
34 window.resize(400, 300);
35 window.show();
36
37 return a.exec();
38 }
@@ -0,0 +1,17
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 = zoomLineChart
10 TEMPLATE = app
11 QT += core gui
12 HEADERS += chartwidget.h
13 SOURCES += main.cpp chartwidget.cpp
14
15
16
17
@@ -0,0 +1,95
1 #include "axisitem_p.h"
2 #include <QPainter>
3 #include <QDebug>
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
7 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
8 m_ticks(4),
9 m_type(type)
10 {
11 }
12
13 AxisItem::~AxisItem()
14 {
15 }
16
17 void AxisItem::setSize(const QSize& size)
18 {
19 m_rect = QRectF(QPoint(0,0),size);
20 }
21
22 void AxisItem::setLength(int length)
23 {
24 QPainterPath path;
25 path.moveTo(QPointF(0,0));
26 path.lineTo(length,0);
27 // path.lineTo(length-4,0);
28 // path.lineTo(length,3);
29 // path.lineTo(length-4,6);
30 // path.lineTo(length-4,4);
31 // path.lineTo(0,4);
32 // path.lineTo(0,2);
33 m_path=path;
34 update();
35 }
36
37 QRectF AxisItem::boundingRect() const
38 {
39 return m_rect;
40 }
41
42 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
43 {
44 m_plotDomain = plotDomain;
45 }
46
47 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
48 {
49 if (!m_rect.isValid())
50 return;
51
52 if(m_type==X_AXIS) {
53
54 const qreal deltaX = (m_rect.width() -1) / m_ticks;
55
56 for (int i = 0; i <= m_ticks; ++i) {
57
58 int x = i * deltaX + m_rect.left();
59 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
60 / m_ticks);
61 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
62 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
63
64 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
65 Qt::AlignHCenter | Qt::AlignTop,
66 QString::number(label));
67 }
68 }
69
70 if(m_type==Y_AXIS) {
71
72 const qreal deltaY = (m_rect.height() - 1) / m_ticks;
73
74 for (int j = 0; j <= m_ticks; ++j) {
75
76 int y = j * -deltaY + m_rect.bottom();
77 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
78 / m_ticks);
79
80 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
81 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
82 //TODO : margin = 50 ;
83 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
84 Qt::AlignRight | Qt::AlignVCenter,
85 QString::number(label));
86 }
87 }
88
89 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
90
91 }
92
93 //TODO "nice numbers algorithm"
94
95 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,40
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
3
4 #include "chartitem_p.h"
5 #include "plotdomain_p.h"
6 #include <QGraphicsItem>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class AxisItem: public ChartItem
11 {
12 public:
13 enum AxisType{X_AXIS,Y_AXIS};
14
15 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
16 ~AxisItem();
17
18 //from QGraphicsItem
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
22 //from ChartItem
23 void setSize(const QSize& size);
24 void setPlotDomain(const PlotDomain& data);
25
26 void setLength(int length);
27 void setWidth(int width);
28 AxisType axisType() const {return m_type;};
29
30 private:
31 QRectF m_rect;
32 int m_ticks;
33 PlotDomain m_plotDomain;
34 QPainterPath m_path;
35 AxisType m_type;
36 };
37
38 QTCOMMERCIALCHART_END_NAMESPACE
39
40 #endif /* AXISITEM_H_ */
@@ -0,0 +1,22
1 #ifndef CHARTITEM_H_
2 #define CHARTITEM_H_
3
4 #include "plotdomain_p.h"
5 #include <QGraphicsItem>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 class ChartItem : public QGraphicsItem
10 {
11 enum ChartItemTypes{ AXIS_ITEM = UserType+1, XYLINE_ITEM};
12 public:
13 ChartItem(QGraphicsItem* parent = 0):QGraphicsItem(parent){};
14 virtual ~ChartItem(){};
15
16 virtual void setSize(const QSize& size) = 0;
17 virtual void setPlotDomain(const PlotDomain& data) = 0;
18 };
19
20 QTCOMMERCIALCHART_END_NAMESPACE
21
22 #endif /* CHARTITEM_H_ */
@@ -1,2 +1,3
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += linechart
2 SUBDIRS += linechart \
3 zoomlinechart
@@ -19,33 +19,17 int main(int argc, char *argv[])
19 series0->setColor(Qt::blue);
19 series0->setColor(Qt::blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
20 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
21 series1->setColor(Qt::red);
22 QXYChartSeries* series2 = QXYChartSeries::create();
23 series2->setColor(Qt::gray);
24 QXYChartSeries* series3 = QXYChartSeries::create();
25 series3->setColor(Qt::green);
26
22
27 int numPoints = 100;
23 int numPoints = 100;
28
24
29 for (int x = 0; x < numPoints; ++x) {
25 for (int x = 0; x <= numPoints; ++x) {
30 series0->add(x,0);
26 series0->add(x, abs(sin(PI/50*x)*100));
31 series1->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
32 series2->add(x, abs(cos(PI/50*x)*100));
28 }
33 series3->add(x,100);
34 }
35
36 QList<QXYChartSeries*> dataset;
37
38 //qDebug()<<"Series 1:" << *series1;
39 //qDebug()<<"Series 2:" << *series2;
40
41 dataset << series0;
42 dataset << series1;
43 dataset << series2;
44 dataset << series3;
45
29
46 QChartView* chartView = new QChartView(&window);
30 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
47 chartView->addSeries(series1);
32 chartView->addSeries(series1);
48 chartView->addSeries(series2);
49
33
50 window.setCentralWidget(chartView);
34 window.setCentralWidget(chartView);
51 window.resize(400, 300);
35 window.resize(400, 300);
@@ -1,10 +1,8
1 #include "xyplotdomain_p.h"
1 #include "plotdomain_p.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 XYPlotDomain::XYPlotDomain():
5 PlotDomain::PlotDomain():
6 m_ticksX(0),
7 m_ticksY(0),
8 m_minX(0),
6 m_minX(0),
9 m_maxX(0),
7 m_maxX(0),
10 m_minY(0),
8 m_minY(0),
@@ -13,18 +11,18 m_maxY(0)
13
11
14 }
12 }
15
13
16 XYPlotDomain::~XYPlotDomain()
14 PlotDomain::~PlotDomain()
17 {
15 {
18 // TODO Auto-generated destructor stub
16 // TODO Auto-generated destructor stub
19 }
17 }
20
18
21 qreal XYPlotDomain::spanX() const
19 qreal PlotDomain::spanX() const
22 {
20 {
23 Q_ASSERT(m_maxX >= m_minX);
21 Q_ASSERT(m_maxX >= m_minX);
24 return m_maxX - m_minX;
22 return m_maxX - m_minX;
25 }
23 }
26
24
27 qreal XYPlotDomain::spanY() const
25 qreal PlotDomain::spanY() const
28 {
26 {
29 Q_ASSERT(m_maxY >= m_minY);
27 Q_ASSERT(m_maxY >= m_minY);
30 return m_maxY - m_minY;
28 return m_maxY - m_minY;
@@ -5,24 +5,19
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 class XYPlotDomain {
8 class PlotDomain {
9 public:
9 public:
10 XYPlotDomain();
10 PlotDomain();
11 virtual ~XYPlotDomain();
11 virtual ~PlotDomain();
12
12
13 qreal spanX() const;
13 qreal spanX() const;
14 qreal spanY() const;
14 qreal spanY() const;
15 int ticksX() const { return m_ticksX; }
16 int ticksY() const { return m_ticksY; }
17
15
18 public:
16 public:
19 int m_ticksX;
20 int m_ticksY;
21 qreal m_minX;
17 qreal m_minX;
22 qreal m_maxX;
18 qreal m_maxX;
23 qreal m_minY;
19 qreal m_minY;
24 qreal m_maxY;
20 qreal m_maxY;
25 QRect m_viewportRect;
26 };
21 };
27
22
28 QTCOMMERCIALCHART_END_NAMESPACE
23 QTCOMMERCIALCHART_END_NAMESPACE
@@ -9,26 +9,27
9 #include "bargroup.h"
9 #include "bargroup.h"
10
10
11 #include "xylinechartitem_p.h"
11 #include "xylinechartitem_p.h"
12 #include "xyplotdomain_p.h"
12 #include "plotdomain_p.h"
13 #include "axis_p.h"
13 #include "axisitem_p.h"
14 #include "xygrid_p.h"
15 #include <QGraphicsScene>
14 #include <QGraphicsScene>
16 #include <QDebug>
15 #include <QDebug>
17
16
18 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19
18
20 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
21 m_axisX(new Axis(this)),
20 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisY(new Axis(this)),
21 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
23 m_grid(new XYGrid(this)),
24 m_plotDataIndex(0),
22 m_plotDataIndex(0),
25 m_marginSize(0)
23 m_marginSize(0)
26 {
24 {
27 // TODO: the default theme?
25 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
26 setTheme(QChart::ChartThemeVanilla);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
27 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 // set axis
28 PlotDomain domain;
31 m_axisY->rotate(90);
29 m_plotDomainList<<domain;
30
31 m_chartItems<<m_axisX;
32 m_chartItems<<m_axisY;
32 }
33 }
33
34
34 QChart::~QChart(){}
35 QChart::~QChart(){}
@@ -53,10 +54,10 void QChart::addSeries(QChartSeries* series)
53 if (!xyseries->color().isValid() && m_themeColors.count())
54 if (!xyseries->color().isValid() && m_themeColors.count())
54 xyseries->setColor(m_themeColors.takeFirst());
55 xyseries->setColor(m_themeColors.takeFirst());
55
56
56 XYPlotDomain domain;
57 m_plotDataIndex = 0 ;
57 //TODO "nice numbers algorithm"
58 m_plotDomainList.resize(1);
58 domain.m_ticksX=4;
59
59 domain.m_ticksY=4;
60 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
60
61
61 for (int i = 0 ; i < xyseries->count() ; i++)
62 for (int i = 0 ; i < xyseries->count() ; i++)
62 {
63 {
@@ -69,9 +70,12 void QChart::addSeries(QChartSeries* series)
69 }
70 }
70
71
71 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
72 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
72 item->updateXYPlotDomain(domain);
73
73 m_plotDomainList<<domain;
74 m_chartItems<<item;
74 m_xyLineChartItems<<item;
75
76 foreach(ChartItem* i ,m_chartItems)
77 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
78
75 break;
79 break;
76 }
80 }
77 case QChartSeries::SeriesTypeBar: {
81 case QChartSeries::SeriesTypeBar: {
@@ -137,31 +141,22 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
137 return series;
141 return series;
138 }
142 }
139
143
140 void QChart::setSize(const QSizeF& size)
144 void QChart::setSize(const QSize& size)
141 {
145 {
142 m_rect = QRect(QPoint(0,0),size.toSize());
146 m_rect = QRect(QPoint(0,0),size);
143 m_rect.adjust(margin(),margin(), -margin(), -margin());
147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
144 m_grid->setPos(m_rect.topLeft());
148
145 m_grid->setSize(m_rect.size());
149 foreach (ChartItem* item ,m_chartItems) {
150 item->setPos(rect.topLeft());
151 item->setSize(rect.size());
146
152
153 }
147 // TODO: TTD for setting scale
154 // TODO: TTD for setting scale
148 //emit scaleChanged(100, 100);
155 //emit scaleChanged(100, 100);
149 // TODO: calculate the origo
156 // TODO: calculate the origo
150 // TODO: not sure if emitting a signal here is the best from performance point of view
157 // TODO: not sure if emitting a signal here is the best from performance point of view
151 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
158 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
152
159
153 for (int i(0); i < m_plotDomainList.size(); i++)
154 m_plotDomainList[i].m_viewportRect = m_rect;
155
156 // TODO: line chart items are updated separately as they don't support update
157 // via sizeChanged signal
158 foreach(XYLineChartItem* item ,m_xyLineChartItems)
159 item->updateXYPlotDomain(m_plotDomainList.at(m_plotDataIndex));
160
161
162 if (m_plotDomainList.count())
163 m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex));
164
165 update();
160 update();
166 }
161 }
167
162
@@ -208,6 +203,62 void QChart::setTheme(QChart::ChartTheme theme)
208 // TODO: update coloring of different elements to match the selected theme
203 // TODO: update coloring of different elements to match the selected theme
209 }
204 }
210
205
206 void QChart::zoomInToRect(const QRect& rectangle)
207 {
208
209 if(!rectangle.isValid()) return;
210
211 qreal margin = this->margin();
212
213 QRect rect = rectangle.normalized();
214 rect.translate(-margin, -margin);
215
216 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
217 PlotDomain newDomain;
218
219 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
220 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
221
222 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
223 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
224 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
225 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
226
227 m_plotDomainList.resize(m_plotDataIndex + 1);
228 m_plotDomainList<<newDomain;
229 m_plotDataIndex++;
230
231 foreach (ChartItem* item ,m_chartItems)
232 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
233 update();
234 }
235
236 void QChart::zoomIn()
237 {
238 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
239 m_plotDataIndex++;
240 foreach (ChartItem* item ,m_chartItems)
241 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
242 update();
243 }else{
244 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
245 rect.setWidth(rect.width()/2);
246 rect.setHeight(rect.height()/2);
247 rect.moveCenter(m_rect.center());
248 zoomInToRect(rect);
249 }
250 }
251
252 void QChart::zoomOut()
253 {
254 if (m_plotDataIndex > 0) {
255 m_plotDataIndex--;
256 foreach (ChartItem* item ,m_chartItems)
257 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
258 update();
259 }
260 }
261
211 #include "moc_qchart.cpp"
262 #include "moc_qchart.cpp"
212
263
213 QTCOMMERCIALCHART_END_NAMESPACE
264 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,11 +7,10
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class Axis;
10 class AxisItem;
11 class XYGrid;
12 class QChartSeries;
11 class QChartSeries;
13 class XYPlotDomain;
12 class PlotDomain;
14 class XYLineChartItem;
13 class ChartItem;
15 class BarGroup;
14 class BarGroup;
16
15
17 // TODO: We don't need to have QChart tied to QGraphicsItem:
16 // TODO: We don't need to have QChart tied to QGraphicsItem:
@@ -45,24 +44,29 public:
45 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
44 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
46 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
45 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
47
46
48 virtual void setSize(const QSizeF& rect);
47 void setSize(const QSize& size);
49 void setMargin(int margin);
48 void setMargin(int margin);
50 int margin() const;
49 int margin() const;
51 void setTheme(QChart::ChartTheme theme);
50 void setTheme(QChart::ChartTheme theme);
52
51
52 void zoomInToRect(const QRect& rectangle);
53 void zoomIn();
54 void zoomOut();
55
53 signals:
56 signals:
57 //TODO chage to const QSize& size
54 void sizeChanged(QRectF rect);
58 void sizeChanged(QRectF rect);
55 void scaleChanged(qreal xscale, qreal yscale);
59 void scaleChanged(qreal xscale, qreal yscale);
56
60
57 private:
61 private:
58 Q_DISABLE_COPY(QChart)
62 Q_DISABLE_COPY(QChart)
59 Axis* m_axisX;
63 AxisItem* m_axisX;
60 Axis* m_axisY;
64 AxisItem* m_axisY;
61 XYGrid* m_grid;
62 QRect m_rect;
65 QRect m_rect;
63 QList<const QChartSeries*> m_series;
66 QList<const QChartSeries*> m_series;
64 QList<XYPlotDomain> m_plotDomainList;
67 QVector<PlotDomain> m_plotDomainList;
65 QList<XYLineChartItem*> m_xyLineChartItems;
68 QList<ChartItem*> m_chartItems;
69 //TODO: remove
66 QList<QGraphicsItem*> m_items;
70 QList<QGraphicsItem*> m_items;
67 int m_plotDataIndex;
71 int m_plotDataIndex;
68 int m_marginSize;
72 int m_marginSize;
@@ -11,9 +11,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QChartView::QChartView(QWidget *parent) :
11 QChartView::QChartView(QWidget *parent) :
12 QGraphicsView(parent),
12 QGraphicsView(parent),
13 m_scene(new QGraphicsScene()),
13 m_scene(new QGraphicsScene()),
14 m_chart(new QChart()),
14 m_chart(new QChart())
15 m_rubberBand(0),
16 m_showRubberBand(false)
17 {
15 {
18 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
19 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@@ -46,36 +44,23 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
46 return m_chart->createSeries(type);
44 return m_chart->createSeries(type);
47 }
45 }
48
46
49 void QChartView::mousePressEvent(QMouseEvent *event)
47 void QChartView::zoomInToRect(const QRect& rectangle)
50 {
48 {
51 int margin = m_chart->margin();
49 m_chart->zoomInToRect(rectangle);
52
53 QRect rect(margin,margin,width()-2*margin,height()-2*margin);
54
55 m_origin = event->pos();
56
57 if (!rect.contains(m_origin)) return;
58
59 if (!m_rubberBand)
60 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
61 m_rubberBand->setGeometry(QRect(m_origin, QSize()));
62 m_showRubberBand=true;
63 m_rubberBand->show();
64
65 }
50 }
66
51
67 void QChartView::mouseMoveEvent(QMouseEvent *event)
52 void QChartView::zoomIn()
68 {
53 {
69 if(m_showRubberBand)
54 m_chart->zoomIn();
70 m_rubberBand->setGeometry(QRect(m_origin, event->pos()).normalized());
71 }
55 }
72
56
73 void QChartView::mouseReleaseEvent(QMouseEvent *event)
57 void QChartView::zoomOut()
74 {
58 {
75 if(m_showRubberBand) {
59 m_chart->zoomOut();
76 m_rubberBand->hide();
77 m_showRubberBand=false;
78 }
79 }
60 }
80
61
62 int QChartView::margin() const
63 {
64 return m_chart->margin();
65 }
81 QTCOMMERCIALCHART_END_NAMESPACE
66 QTCOMMERCIALCHART_END_NAMESPACE
@@ -6,7 +6,6
6 #include <QGraphicsView>
6 #include <QGraphicsView>
7
7
8 class QGraphicsScene;
8 class QGraphicsScene;
9 class QRubberBand;
10
9
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
11
@@ -25,17 +24,15 public:
25 // Convenience function
24 // Convenience function
26 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
27
26
28 protected:
27 int margin() const;
29 void mouseMoveEvent (QMouseEvent *event);
28 void zoomInToRect(const QRect& rectangle);
30 void mousePressEvent (QMouseEvent *event);
29 void zoomIn();
31 void mouseReleaseEvent (QMouseEvent *event);
30 void zoomOut();
32
31
33 private:
32 private:
34 QGraphicsScene *m_scene;
33 QGraphicsScene *m_scene;
35 QChart* m_chart;
34 QChart* m_chart;
36 QRubberBand *m_rubberBand;
37 QPoint m_origin;
35 QPoint m_origin;
38 bool m_showRubberBand;
39 Q_DISABLE_COPY(QChartView)
36 Q_DISABLE_COPY(QChartView)
40
37
41
38
@@ -17,12 +17,11 SOURCES += \
17 barchart/bar.cpp \
17 barchart/bar.cpp \
18 xylinechart/qxychartseries.cpp \
18 xylinechart/qxychartseries.cpp \
19 xylinechart/xylinechartitem.cpp \
19 xylinechart/xylinechartitem.cpp \
20 xylinechart/xygrid.cpp \
20 plotdomain.cpp \
21 xylinechart/xyplotdomain.cpp \
22 qscatterseries.cpp \
21 qscatterseries.cpp \
23 qpieseries.cpp \
22 qpieseries.cpp \
24 qchart.cpp \
23 qchart.cpp \
25 axis.cpp \
24 axisitem.cpp \
26 qchartwidget.cpp \
25 qchartwidget.cpp \
27 pieslice.cpp \
26 pieslice.cpp \
28 qchartview.cpp \
27 qchartview.cpp \
@@ -30,11 +29,11 SOURCES += \
30
29
31 PRIVATE_HEADERS += \
30 PRIVATE_HEADERS += \
32 xylinechart/xylinechartitem_p.h \
31 xylinechart/xylinechartitem_p.h \
33 xylinechart/xyplotdomain_p.h \
32 plotdomain_p.h \
34 xylinechart/xygrid_p.h \
35 qscatterseries_p.h \
33 qscatterseries_p.h \
36 pieslice.h \
34 pieslice.h \
37 axis_p.h
35 axisitem_p.h \
36 chartitem_p.h
38
37
39 PUBLIC_HEADERS += \
38 PUBLIC_HEADERS += \
40 qchartseries.h \
39 qchartseries.h \
@@ -18,7 +18,7 void XYGrid::setSize(const QSizeF& size)
18 m_rect.setSize(size.toSize());
18 m_rect.setSize(size.toSize());
19 }
19 }
20
20
21 void XYGrid::setXYPlotData(const XYPlotDomain& xyPlotData)
21 void XYGrid::setXYPlotData(const PlotDomain& xyPlotData)
22 {
22 {
23 m_xyPlotData = xyPlotData;
23 m_xyPlotData = xyPlotData;
24 }
24 }
@@ -18,12 +18,12 public:
18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19
19
20 //TODO: this is just temporary interface
20 //TODO: this is just temporary interface
21 void setXYPlotData(const XYPlotDomain& xyPlotData);
21 void setXYPlotData(const PlotDomain& xyPlotData);
22 void setSize(const QSizeF& rect);
22 void setSize(const QSizeF& rect);
23
23
24 private:
24 private:
25 QRectF m_rect;
25 QRectF m_rect;
26 XYPlotDomain m_xyPlotData;
26 PlotDomain m_xyPlotData;
27 };
27 };
28
28
29 QTCOMMERCIALCHART_END_NAMESPACE
29 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,6 +1,5
1 #include "xylinechartitem_p.h"
1 #include "xylinechartitem_p.h"
2 #include "axis_p.h"
2 #include "axisitem_p.h"
3 #include "xygrid_p.h"
4 #include "qxychartseries.h"
3 #include "qxychartseries.h"
5 #include <QPainter>
4 #include <QPainter>
6 #include <QStyleOptionGraphicsItem>
5 #include <QStyleOptionGraphicsItem>
@@ -8,49 +7,61
8
7
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
9
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
12 m_series(series)
11 m_series(series),
12 m_dirty(false)
13 {
13 {
14
14
15 }
15 }
16
16
17 void XYLineChartItem::updateXYPlotDomain(const XYPlotDomain& data)
17 void XYLineChartItem::setSize(const QSize& size)
18 {
18 {
19 m_xyPlotData=data;
19 m_rect=QRect(0,0,size.width(),size.height());
20 m_dirty=true;
21 }
20
22
21 if (!m_xyPlotData.m_viewportRect.isValid())
23 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
22 return;
24 {
25 m_plotDomain=data;
26 m_dirty=true;
27 }
23
28
24 const QRect& rect = m_xyPlotData.m_viewportRect;
29 QRectF XYLineChartItem::boundingRect() const
30 {
31 return m_polyline.boundingRect();
32 }
25
33
26 const qreal deltaX = (rect.width()-1)/m_xyPlotData.spanX();
34 void XYLineChartItem::updateGeometry()
27 const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY();
35 {
36 if (!m_rect.isValid()) return;
28
37
29 m_polyline.clear();
38 const qreal deltaX = (m_rect.width()-1)/m_plotDomain.spanX();
30 m_polyline.resize(m_series->count());
39 const qreal deltaY = (m_rect.height()-1)/m_plotDomain.spanY();
31
40
32 for (int j = 0; j < m_series->count(); ++j) {
41 m_polyline.clear();
33 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
42 m_polyline.resize(m_series->count());
34 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
35 qreal x = (dx * deltaX) + rect.left();
36 qreal y = - (dy * deltaY) + rect.bottom();
37 m_polyline[j] = QPointF(x, y);
38 }
39
43
40 }
44 for (int j = 0; j < m_series->count(); ++j) {
45 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
46 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
47 qreal x = (dx * deltaX) + m_rect.left();
48 qreal y = - (dy * deltaY) + m_rect.bottom();
49 m_polyline[j] = QPointF(x, y);
50 }
41
51
42 QRectF XYLineChartItem::boundingRect() const
43 {
44 return m_polyline.boundingRect();
45 }
52 }
46
53
47
54
48 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
55 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
49 {
56 {
50 painter->setClipRect(m_xyPlotData.m_viewportRect.adjusted(+1, +1, -1, -1));
57 //TODO: remove it
58 if (m_dirty) {
59 updateGeometry();
60 m_dirty=false;
61 }
62 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
51 painter->setPen(m_series->color());
63 painter->setPen(m_series->color());
52 painter->drawPolyline(m_polyline);
64 painter->drawPolyline(m_polyline);
53
54 }
65 }
55
66
56 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,30 +2,35
2 #define XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
5 #include "chartitem_p.h"
6 #include "xyplotdomain_p.h"
7
6
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
8
10 class QXYChartSeries;
9 class QXYChartSeries;
11
10
12 class XYLineChartItem : public QGraphicsItem
11 class XYLineChartItem : public ChartItem
13 {
12 {
14
13
15 public:
14 public:
16 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
15 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 virtual ~ XYLineChartItem(){};
16 ~ XYLineChartItem(){};
18
17
19 //from QGraphicsItem
18 //from QGraphicsItem
20 virtual QRectF boundingRect() const;
19 QRectF boundingRect() const;
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 //from ChartItem
22 void setSize(const QSize& size);
23 void setPlotDomain(const PlotDomain& data);
22
24
23 void updateXYPlotDomain(const XYPlotDomain& data);
25 private:
26 void updateGeometry();
24
27
25 private:
28 private:
29 QRect m_rect;
26 QPolygonF m_polyline;
30 QPolygonF m_polyline;
27 QXYChartSeries* m_series;
31 QXYChartSeries* m_series;
28 XYPlotDomain m_xyPlotData;
32 PlotDomain m_plotDomain;
33 bool m_dirty;
29 };
34 };
30
35
31 QTCOMMERCIALCHART_END_NAMESPACE
36 QTCOMMERCIALCHART_END_NAMESPACE
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now