##// 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 1 TEMPLATE = subdirs
2 SUBDIRS += linechart
2 SUBDIRS += linechart \
3 zoomlinechart
@@ -1,55 +1,39
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartview.h>
4 4 #include <qxychartseries.h>
5 5 #include <qchart.h>
6 6 #include <cmath>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 #define PI 3.14159265358979
11 11
12 12 int main(int argc, char *argv[])
13 13 {
14 14 QApplication a(argc, argv);
15 15
16 16 QMainWindow window;
17 17
18 18 QXYChartSeries* series0 = QXYChartSeries::create();
19 19 series0->setColor(Qt::blue);
20 20 QXYChartSeries* series1 = QXYChartSeries::create();
21 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 23 int numPoints = 100;
28 24
29 for (int x = 0; x < numPoints; ++x) {
30 series0->add(x,0);
31 series1->add(x, abs(sin(PI/50*x)*100));
32 series2->add(x, abs(cos(PI/50*x)*100));
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;
25 for (int x = 0; x <= numPoints; ++x) {
26 series0->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
28 }
45 29
46 30 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
47 32 chartView->addSeries(series1);
48 chartView->addSeries(series2);
49 33
50 34 window.setCentralWidget(chartView);
51 35 window.resize(400, 300);
52 36 window.show();
53 37
54 38 return a.exec();
55 39 }
@@ -1,33 +1,31
1 #include "xyplotdomain_p.h"
1 #include "plotdomain_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 XYPlotDomain::XYPlotDomain():
6 m_ticksX(0),
7 m_ticksY(0),
5 PlotDomain::PlotDomain():
8 6 m_minX(0),
9 7 m_maxX(0),
10 8 m_minY(0),
11 9 m_maxY(0)
12 10 {
13 11
14 12 }
15 13
16 XYPlotDomain::~XYPlotDomain()
14 PlotDomain::~PlotDomain()
17 15 {
18 16 // TODO Auto-generated destructor stub
19 17 }
20 18
21 qreal XYPlotDomain::spanX() const
19 qreal PlotDomain::spanX() const
22 20 {
23 21 Q_ASSERT(m_maxX >= m_minX);
24 22 return m_maxX - m_minX;
25 23 }
26 24
27 qreal XYPlotDomain::spanY() const
25 qreal PlotDomain::spanY() const
28 26 {
29 27 Q_ASSERT(m_maxY >= m_minY);
30 28 return m_maxY - m_minY;
31 29 }
32 30
33 31 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,30 +1,25
1 1 #ifndef PLOTDOMAIN_H_
2 2 #define PLOTDOMAIN_H_
3 3 #include "qchartglobal.h"
4 4 #include <QRect>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 class XYPlotDomain {
8 class PlotDomain {
9 9 public:
10 XYPlotDomain();
11 virtual ~XYPlotDomain();
10 PlotDomain();
11 virtual ~PlotDomain();
12 12
13 13 qreal spanX() const;
14 14 qreal spanY() const;
15 int ticksX() const { return m_ticksX; }
16 int ticksY() const { return m_ticksY; }
17 15
18 16 public:
19 int m_ticksX;
20 int m_ticksY;
21 17 qreal m_minX;
22 18 qreal m_maxX;
23 19 qreal m_minY;
24 20 qreal m_maxY;
25 QRect m_viewportRect;
26 21 };
27 22
28 23 QTCOMMERCIALCHART_END_NAMESPACE
29 24
30 25 #endif /* PLOTTER_H_ */
@@ -1,213 +1,264
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7 7
8 8 #include "barchartseries.h"
9 9 #include "bargroup.h"
10 10
11 11 #include "xylinechartitem_p.h"
12 #include "xyplotdomain_p.h"
13 #include "axis_p.h"
14 #include "xygrid_p.h"
12 #include "plotdomain_p.h"
13 #include "axisitem_p.h"
15 14 #include <QGraphicsScene>
16 15 #include <QDebug>
17 16
18 17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
19 18
20 19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
21 m_axisX(new Axis(this)),
22 m_axisY(new Axis(this)),
23 m_grid(new XYGrid(this)),
20 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
21 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
24 22 m_plotDataIndex(0),
25 23 m_marginSize(0)
26 24 {
27 25 // TODO: the default theme?
28 26 setTheme(QChart::ChartThemeVanilla);
29 27 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 // set axis
31 m_axisY->rotate(90);
28 PlotDomain domain;
29 m_plotDomainList<<domain;
30
31 m_chartItems<<m_axisX;
32 m_chartItems<<m_axisY;
32 33 }
33 34
34 35 QChart::~QChart(){}
35 36
36 37 QRectF QChart::boundingRect() const
37 38 {
38 39 return m_rect;
39 40 }
40 41
41 42 void QChart::addSeries(QChartSeries* series)
42 43 {
43 44 // TODO: we should check the series not already added
44 45
45 46 m_series<<series;
46 47
47 48 switch(series->type())
48 49 {
49 50 case QChartSeries::SeriesTypeLine: {
50 51
51 52 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
52 53 // Use color defined by theme in case the series does not define a custom color
53 54 if (!xyseries->color().isValid() && m_themeColors.count())
54 55 xyseries->setColor(m_themeColors.takeFirst());
55 56
56 XYPlotDomain domain;
57 //TODO "nice numbers algorithm"
58 domain.m_ticksX=4;
59 domain.m_ticksY=4;
57 m_plotDataIndex = 0 ;
58 m_plotDomainList.resize(1);
59
60 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
60 61
61 62 for (int i = 0 ; i < xyseries->count() ; i++)
62 63 {
63 64 qreal x = xyseries->x(i);
64 65 qreal y = xyseries->y(i);
65 66 domain.m_minX = qMin(domain.m_minX,x);
66 67 domain.m_minY = qMin(domain.m_minY,y);
67 68 domain.m_maxX = qMax(domain.m_maxX,x);
68 69 domain.m_maxY = qMax(domain.m_maxY,y);
69 70 }
70 71
71 72 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
72 item->updateXYPlotDomain(domain);
73 m_plotDomainList<<domain;
74 m_xyLineChartItems<<item;
73
74 m_chartItems<<item;
75
76 foreach(ChartItem* i ,m_chartItems)
77 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
78
75 79 break;
76 80 }
77 81 case QChartSeries::SeriesTypeBar: {
78 82
79 83 qDebug() << "barSeries added";
80 84 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
81 85
82 86 // Who owns the series?
83 87 BarGroup* group = new BarGroup(*barSeries, this);
84 88 scene()->addItem(group);
85 89 m_BarGroupItems.append(group); // If we need to access group later
86 90 break;
87 91 }
88 92 case QChartSeries::SeriesTypeScatter: {
89 93 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
90 94 connect(this, SIGNAL(sizeChanged(QRectF)),
91 95 scatterSeries, SLOT(chartSizeChanged(QRectF)));
92 96 scatterSeries->d->setParentItem(this);
93 97 QColor nextColor = m_themeColors.takeFirst();
94 98 nextColor.setAlpha(150); // TODO: default opacity?
95 99 scatterSeries->setMarkerColor(nextColor);
96 100 }
97 101 case QChartSeries::SeriesTypePie: {
98 102 // TODO: we now have also a list of y values as a parameter, it is ignored
99 103 // we should use a generic data class instead of list of x and y values
100 104 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
101 105 connect(this, SIGNAL(sizeChanged(QRectF)),
102 106 pieSeries, SLOT(chartSizeChanged(QRectF)));
103 107 // TODO: how to define the color for all the slices of a pie?
104 108 }
105 109 }
106 110 }
107 111
108 112 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
109 113 {
110 114 // TODO: support also other types; not only scatter and pie
111 115
112 116 QChartSeries *series(0);
113 117
114 118 switch (type) {
115 119 case QChartSeries::SeriesTypeLine: {
116 120 series = QXYChartSeries::create();
117 121 break;
118 122 }
119 123 case QChartSeries::SeriesTypeBar: {
120 124 series = new BarChartSeries(this);
121 125 break;
122 126 }
123 127 case QChartSeries::SeriesTypeScatter: {
124 128 series = new QScatterSeries(this);
125 129 break;
126 130 }
127 131 case QChartSeries::SeriesTypePie: {
128 132 series = new QPieSeries(this);
129 133 break;
130 134 }
131 135 default:
132 136 Q_ASSERT(false);
133 137 break;
134 138 }
135 139
136 140 addSeries(series);
137 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());
143 m_rect.adjust(margin(),margin(), -margin(), -margin());
144 m_grid->setPos(m_rect.topLeft());
145 m_grid->setSize(m_rect.size());
146 m_rect = QRect(QPoint(0,0),size);
147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
148
149 foreach (ChartItem* item ,m_chartItems) {
150 item->setPos(rect.topLeft());
151 item->setSize(rect.size());
146 152
153 }
147 154 // TODO: TTD for setting scale
148 155 //emit scaleChanged(100, 100);
149 156 // TODO: calculate the origo
150 157 // TODO: not sure if emitting a signal here is the best from performance point of view
151 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 160 update();
166 161 }
167 162
168 163 int QChart::margin() const
169 164 {
170 165 return m_marginSize;
171 166 }
172 167
173 168 void QChart::setMargin(int margin)
174 169 {
175 170 m_marginSize = margin;
176 171 }
177 172
178 173 void QChart::setTheme(QChart::ChartTheme theme)
179 174 {
180 175 // TODO: define color themes
181 176 switch (theme) {
182 177 case ChartThemeVanilla:
183 178 m_themeColors.append(QColor(255, 238, 174));
184 179 m_themeColors.append(QColor(228, 228, 160));
185 180 m_themeColors.append(QColor(228, 179, 160));
186 181 m_themeColors.append(QColor(180, 151, 18));
187 182 m_themeColors.append(QColor(252, 252, 37));
188 183 break;
189 184 case ChartThemeIcy:
190 185 m_themeColors.append(QColor(255, 238, 174));
191 186 m_themeColors.append(QColor(228, 228, 160));
192 187 m_themeColors.append(QColor(228, 179, 160));
193 188 m_themeColors.append(QColor(180, 151, 18));
194 189 m_themeColors.append(QColor(252, 252, 37));
195 190 break;
196 191 case ChartThemeGrayscale:
197 192 m_themeColors.append(QColor(255, 238, 174));
198 193 m_themeColors.append(QColor(228, 228, 160));
199 194 m_themeColors.append(QColor(228, 179, 160));
200 195 m_themeColors.append(QColor(180, 151, 18));
201 196 m_themeColors.append(QColor(252, 252, 37));
202 197 break;
203 198 default:
204 199 Q_ASSERT(false);
205 200 break;
206 201 }
207 202
208 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 262 #include "moc_qchart.cpp"
212 263
213 264 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,80
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsObject>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 class Axis;
11 class XYGrid;
10 class AxisItem;
12 11 class QChartSeries;
13 class XYPlotDomain;
14 class XYLineChartItem;
12 class PlotDomain;
13 class ChartItem;
15 14 class BarGroup;
16 15
17 16 // TODO: We don't need to have QChart tied to QGraphicsItem:
18 17 //class QTCOMMERCIALCHART_EXPORT QChart
19 18 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
20 19 // public: QChartGraphicsItem(QChart &chart);
21 20
22 21 /*!
23 22 * TODO: define the responsibilities
24 23 */
25 24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
26 25 {
27 26 Q_OBJECT
28 27 public:
29 28 enum ChartTheme {
30 29 ChartThemeVanilla = 0,
31 30 ChartThemeIcy,
32 31 ChartThemeGrayscale
33 32 };
34 33
35 34 public:
36 35 QChart(QGraphicsObject* parent = 0);
37 36 ~QChart();
38 37
39 38 //from QGraphicsItem
40 39 QRectF boundingRect() const;
41 40 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
42 41
43 42 void addSeries(QChartSeries* series);
44 43 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
45 44 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
46 45 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
47 46
48 virtual void setSize(const QSizeF& rect);
47 void setSize(const QSize& size);
49 48 void setMargin(int margin);
50 49 int margin() const;
51 50 void setTheme(QChart::ChartTheme theme);
52 51
52 void zoomInToRect(const QRect& rectangle);
53 void zoomIn();
54 void zoomOut();
55
53 56 signals:
57 //TODO chage to const QSize& size
54 58 void sizeChanged(QRectF rect);
55 59 void scaleChanged(qreal xscale, qreal yscale);
56 60
57 61 private:
58 62 Q_DISABLE_COPY(QChart)
59 Axis* m_axisX;
60 Axis* m_axisY;
61 XYGrid* m_grid;
63 AxisItem* m_axisX;
64 AxisItem* m_axisY;
62 65 QRect m_rect;
63 66 QList<const QChartSeries*> m_series;
64 QList<XYPlotDomain> m_plotDomainList;
65 QList<XYLineChartItem*> m_xyLineChartItems;
67 QVector<PlotDomain> m_plotDomainList;
68 QList<ChartItem*> m_chartItems;
69 //TODO: remove
66 70 QList<QGraphicsItem*> m_items;
67 71 int m_plotDataIndex;
68 72 int m_marginSize;
69 73 QList<QColor> m_themeColors;
70 74
71 75 QList<BarGroup*> m_BarGroupItems;
72 76 };
73 77
74 78 QTCOMMERCIALCHART_END_NAMESPACE
75 79
76 80 #endif
@@ -1,81 +1,66
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include <QGraphicsView>
4 4 #include <QGraphicsScene>
5 5 #include <QRubberBand>
6 6 #include <QResizeEvent>
7 7 #include <QDebug>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 QChartView::QChartView(QWidget *parent) :
12 12 QGraphicsView(parent),
13 13 m_scene(new QGraphicsScene()),
14 m_chart(new QChart()),
15 m_rubberBand(0),
16 m_showRubberBand(false)
14 m_chart(new QChart())
17 15 {
18 16 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
19 17 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 18 setScene(m_scene);
21 19 m_chart->setMargin(50);
22 20 m_scene->addItem(m_chart);
23 21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
24 22 }
25 23
26 24 QChartView::~QChartView()
27 25 {
28 26 }
29 27
30 28 void QChartView::resizeEvent(QResizeEvent *event)
31 29 {
32 30 m_scene->setSceneRect(0,0,size().width(),size().height());
33 31 m_chart->setSize(size());
34 32 QWidget::resizeEvent(event);
35 33 }
36 34
37 35
38 36 void QChartView::addSeries(QChartSeries* series)
39 37 {
40 38 m_chart->addSeries(series);
41 39 }
42 40
43 41 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
44 42 {
45 43
46 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();
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
49 m_chart->zoomInToRect(rectangle);
65 50 }
66 51
67 void QChartView::mouseMoveEvent(QMouseEvent *event)
52 void QChartView::zoomIn()
68 53 {
69 if(m_showRubberBand)
70 m_rubberBand->setGeometry(QRect(m_origin, event->pos()).normalized());
54 m_chart->zoomIn();
71 55 }
72 56
73 void QChartView::mouseReleaseEvent(QMouseEvent *event)
57 void QChartView::zoomOut()
74 58 {
75 if(m_showRubberBand) {
76 m_rubberBand->hide();
77 m_showRubberBand=false;
78 }
59 m_chart->zoomOut();
79 60 }
80 61
62 int QChartView::margin() const
63 {
64 return m_chart->margin();
65 }
81 66 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,43
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include <QGraphicsView>
7 7
8 8 class QGraphicsScene;
9 class QRubberBand;
10 9
11 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 11
13 12 class QChart;
14 13
15 14 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
16 15 {
17 16 public:
18 17 explicit QChartView(QWidget *parent = 0);
19 18 ~QChartView();
20 19
21 20 //implement from QWidget
22 21 void resizeEvent(QResizeEvent *event);
23 22
24 23 void addSeries(QChartSeries* series);
25 24 // Convenience function
26 25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
27 26
28 protected:
29 void mouseMoveEvent (QMouseEvent *event);
30 void mousePressEvent (QMouseEvent *event);
31 void mouseReleaseEvent (QMouseEvent *event);
27 int margin() const;
28 void zoomInToRect(const QRect& rectangle);
29 void zoomIn();
30 void zoomOut();
32 31
33 32 private:
34 33 QGraphicsScene *m_scene;
35 34 QChart* m_chart;
36 QRubberBand *m_rubberBand;
37 35 QPoint m_origin;
38 bool m_showRubberBand;
39 36 Q_DISABLE_COPY(QChartView)
40 37
41 38
42 39 };
43 40
44 41 QTCOMMERCIALCHART_END_NAMESPACE
45 42
46 43 #endif // QCHARTWIDGET_H
@@ -1,90 +1,89
1 1 !include( ../common.pri ) {
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5 5 TARGET = QtCommercialChart
6 6 DESTDIR = $$CHART_BUILD_LIB_DIR
7 7 TEMPLATE = lib
8 8 QT += core \
9 9 gui
10 10
11 11 CONFIG += debug_and_release
12 12 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
13 13
14 14 SOURCES += \
15 15 barchart/barchartseries.cpp \
16 16 barchart/bargroup.cpp \
17 17 barchart/bar.cpp \
18 18 xylinechart/qxychartseries.cpp \
19 19 xylinechart/xylinechartitem.cpp \
20 xylinechart/xygrid.cpp \
21 xylinechart/xyplotdomain.cpp \
20 plotdomain.cpp \
22 21 qscatterseries.cpp \
23 22 qpieseries.cpp \
24 23 qchart.cpp \
25 axis.cpp \
24 axisitem.cpp \
26 25 qchartwidget.cpp \
27 26 pieslice.cpp \
28 27 qchartview.cpp \
29 28 qchartseries.cpp
30 29
31 30 PRIVATE_HEADERS += \
32 31 xylinechart/xylinechartitem_p.h \
33 xylinechart/xyplotdomain_p.h \
34 xylinechart/xygrid_p.h \
32 plotdomain_p.h \
35 33 qscatterseries_p.h \
36 34 pieslice.h \
37 axis_p.h
35 axisitem_p.h \
36 chartitem_p.h
38 37
39 38 PUBLIC_HEADERS += \
40 39 qchartseries.h \
41 40 qscatterseries.h \
42 41 qpieseries.h \
43 42 qchart.h \
44 43 qchartwidget.h \
45 44 qchartglobal.h \
46 45 xylinechart/qxychartseries.h \
47 46 barchart/barchartseries.h \
48 47 barchart/bargroup.h \
49 48 qchartview.h
50 49
51 50 HEADERS += $$PUBLIC_HEADERS
52 51 HEADERS += $$PRIVATE_HEADERS
53 52
54 53 INCLUDEPATH += xylinechart \
55 54 barchart \
56 55 .
57 56
58 57 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
59 58 MOC_DIR = $$CHART_BUILD_DIR/lib
60 59 UI_DIR = $$CHART_BUILD_DIR/lib
61 60 RCC_DIR = $$CHART_BUILD_DIR/lib
62 61
63 62
64 63 DEFINES += QTCOMMERCIALCHART_LIBRARY
65 64
66 65 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
67 66 public_headers.files = $$PUBLIC_HEADERS
68 67 target.path = $$[QT_INSTALL_LIBS]
69 68 INSTALLS += target \
70 69 public_headers
71 70
72 71
73 72 install_build_headers.name = bild_headers
74 73 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
75 74 install_build_headers.input = PUBLIC_HEADERS
76 75 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
77 76 install_build_headers.CONFIG += target_predeps no_link
78 77 QMAKE_EXTRA_COMPILERS += install_build_headers
79 78
80 79 chartversion.target = qchartversion_p.h
81 80 chartversion.commands = @echo "build_time" > $$chartversion.target;
82 81 chartversion.depends = $$HEADERS $$SOURCES
83 82 PRE_TARGETDEPS += qchartversion_p.h
84 83 QMAKE_CLEAN+= qchartversion_p.h
85 84 QMAKE_EXTRA_TARGETS += chartversion
86 85
87 86 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
88 87 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
89 88
90 89
@@ -1,70 +1,70
1 1 #include "xygrid_p.h"
2 2 #include <QPainter>
3 3 #include <QDebug>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 XYGrid::XYGrid(QGraphicsItem* parent):QGraphicsItem(parent)
8 8 {
9 9 }
10 10
11 11 XYGrid::~XYGrid()
12 12 {
13 13 // TODO Auto-generated destructor stub
14 14 }
15 15
16 16 void XYGrid::setSize(const QSizeF& size)
17 17 {
18 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 23 m_xyPlotData = xyPlotData;
24 24 }
25 25
26 26 QRectF XYGrid::boundingRect() const
27 27 {
28 28 return m_rect;
29 29 }
30 30
31 31 void XYGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
32 32 {
33 33
34 34 if (!m_rect.isValid())
35 35 return;
36 36
37 37 const qreal deltaX = (m_rect.width() -1) / m_xyPlotData.ticksX();
38 38 const qreal deltaY = (m_rect.height() - 1) / m_xyPlotData.ticksY();
39 39
40 40 for (int i = 0; i <= m_xyPlotData.ticksX(); ++i) {
41 41
42 42 int x = i * deltaX + m_rect.left();
43 43 qreal label = m_xyPlotData.m_minX + (i * m_xyPlotData.spanX()
44 44 / m_xyPlotData.ticksX());
45 45 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
46 46 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
47 47
48 48 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
49 49 Qt::AlignHCenter | Qt::AlignTop,
50 50 QString::number(label));
51 51 }
52 52
53 53 for (int j = 0; j <= m_xyPlotData.ticksY(); ++j) {
54 54
55 55 int y = j * -deltaY + m_rect.bottom();
56 56 qreal label = m_xyPlotData.m_minY + (j * m_xyPlotData.spanY()
57 57 / m_xyPlotData.ticksY());
58 58
59 59 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
60 60 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
61 61 //TODO : margin = 50 ;
62 62 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
63 63 Qt::AlignRight | Qt::AlignVCenter,
64 64 QString::number(label));
65 65 }
66 66
67 67 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
68 68 }
69 69
70 70 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,31 +1,31
1 1 #ifndef XYGRID_H_
2 2 #define XYGRID_H_
3 3
4 4 #include <qchartglobal.h>
5 5 #include <xyplotdomain_p.h>
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class XYGrid : public QGraphicsItem
11 11 {
12 12 public:
13 13 XYGrid(QGraphicsItem* parent = 0);
14 14 virtual ~XYGrid();
15 15
16 16 //from QGraphicsItem
17 17 virtual QRectF boundingRect() const;
18 18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 19
20 20 //TODO: this is just temporary interface
21 void setXYPlotData(const XYPlotDomain& xyPlotData);
21 void setXYPlotData(const PlotDomain& xyPlotData);
22 22 void setSize(const QSizeF& rect);
23 23
24 24 private:
25 25 QRectF m_rect;
26 XYPlotDomain m_xyPlotData;
26 PlotDomain m_xyPlotData;
27 27 };
28 28
29 29 QTCOMMERCIALCHART_END_NAMESPACE
30 30
31 31 #endif /* XYGRID_H_ */
@@ -1,56 +1,67
1 1 #include "xylinechartitem_p.h"
2 #include "axis_p.h"
3 #include "xygrid_p.h"
2 #include "axisitem_p.h"
4 3 #include "qxychartseries.h"
5 4 #include <QPainter>
6 5 #include <QStyleOptionGraphicsItem>
7 6 #include <QDebug>
8 7
9 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 9
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
12 m_series(series)
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
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())
22 return;
23 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
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();
27 const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY();
34 void XYLineChartItem::updateGeometry()
35 {
36 if (!m_rect.isValid()) return;
28 37
29 m_polyline.clear();
30 m_polyline.resize(m_series->count());
38 const qreal deltaX = (m_rect.width()-1)/m_plotDomain.spanX();
39 const qreal deltaY = (m_rect.height()-1)/m_plotDomain.spanY();
31 40
32 for (int j = 0; j < m_series->count(); ++j) {
33 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
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 }
41 m_polyline.clear();
42 m_polyline.resize(m_series->count());
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 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 63 painter->setPen(m_series->color());
52 64 painter->drawPolyline(m_polyline);
53
54 65 }
55 66
56 67 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,33 +1,38
1 1 #ifndef XYLINECHARTITEM_H
2 2 #define XYLINECHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
6 #include "xyplotdomain_p.h"
5 #include "chartitem_p.h"
7 6
8 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 8
10 9 class QXYChartSeries;
11 10
12 class XYLineChartItem : public QGraphicsItem
11 class XYLineChartItem : public ChartItem
13 12 {
14 13
15 14 public:
16 15 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
17 virtual ~ XYLineChartItem(){};
16 ~ XYLineChartItem(){};
18 17
19 18 //from QGraphicsItem
20 virtual QRectF boundingRect() const;
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 QRectF boundingRect() const;
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 28 private:
29 QRect m_rect;
26 30 QPolygonF m_polyline;
27 31 QXYChartSeries* m_series;
28 XYPlotDomain m_xyPlotData;
32 PlotDomain m_plotDomain;
33 bool m_dirty;
29 34 };
30 35
31 36 QTCOMMERCIALCHART_END_NAMESPACE
32 37
33 38 #endif
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now