##// END OF EJS Templates
sauimone -
r70:f6809ce9f999 merge
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 = colorlineChart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp
13
14
15
@@ -0,0 +1,40
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
6 #include <cmath>
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
10 #define PI 3.14159265358979
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 QMainWindow window;
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
22
23 int numPoints = 100;
24
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 }
29
30 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
32 chartView->addSeries(series1);
33 chartView->setBackgroundColor(Qt::yellow);
34
35 window.setCentralWidget(chartView);
36 window.resize(400, 300);
37 window.show();
38
39 return a.exec();
40 }
@@ -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,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,4
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += linechart
2 SUBDIRS += linechart \
3 zoomlinechart \
4 colorlinechart
@@ -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,70 +1,95
1 #include "xygrid_p.h"
1 #include "axisitem_p.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <QDebug>
3 #include <QDebug>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 XYGrid::XYGrid(QGraphicsItem* parent):QGraphicsItem(parent)
7 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
8 m_ticks(4),
9 m_type(type)
8 {
10 {
9 }
11 }
10
12
11 XYGrid::~XYGrid()
13 AxisItem::~AxisItem()
12 {
14 {
13 // TODO Auto-generated destructor stub
14 }
15 }
15
16
16 void XYGrid::setSize(const QSizeF& size)
17 void AxisItem::setSize(const QSize& size)
17 {
18 {
18 m_rect.setSize(size.toSize());
19 m_rect = QRectF(QPoint(0,0),size);
19 }
20 }
20
21
21 void XYGrid::setXYPlotData(const XYPlotDomain& xyPlotData)
22 void AxisItem::setLength(int length)
22 {
23 {
23 m_xyPlotData = xyPlotData;
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();
24 }
35 }
25
36
26 QRectF XYGrid::boundingRect() const
37 QRectF AxisItem::boundingRect() const
27 {
38 {
28 return m_rect;
39 return m_rect;
29 }
40 }
30
41
31 void XYGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
42 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
32 {
43 {
44 m_plotDomain = plotDomain;
45 }
33
46
47 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
48 {
34 if (!m_rect.isValid())
49 if (!m_rect.isValid())
35 return;
50 return;
36
51
37 const qreal deltaX = (m_rect.width() -1) / m_xyPlotData.ticksX();
52 if(m_type==X_AXIS) {
38 const qreal deltaY = (m_rect.height() - 1) / m_xyPlotData.ticksY();
39
53
40 for (int i = 0; i <= m_xyPlotData.ticksX(); ++i) {
54 const qreal deltaX = (m_rect.width() -1) / m_ticks;
41
55
42 int x = i * deltaX + m_rect.left();
56 for (int i = 0; i <= m_ticks; ++i) {
43 qreal label = m_xyPlotData.m_minX + (i * m_xyPlotData.spanX()
44 / m_xyPlotData.ticksX());
45 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
46 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
47
57
48 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
58 int x = i * deltaX + m_rect.left();
49 Qt::AlignHCenter | Qt::AlignTop,
59 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
50 QString::number(label));
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 }
51 }
68 }
52
69
53 for (int j = 0; j <= m_xyPlotData.ticksY(); ++j) {
70 if(m_type==Y_AXIS) {
71
72 const qreal deltaY = (m_rect.height() - 1) / m_ticks;
54
73
55 int y = j * -deltaY + m_rect.bottom();
74 for (int j = 0; j <= m_ticks; ++j) {
56 qreal label = m_xyPlotData.m_minY + (j * m_xyPlotData.spanY()
57 / m_xyPlotData.ticksY());
58
75
59 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
76 int y = j * -deltaY + m_rect.bottom();
60 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
77 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
61 //TODO : margin = 50 ;
78 / m_ticks);
62 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
79
63 Qt::AlignRight | Qt::AlignVCenter,
80 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
64 QString::number(label));
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 }
65 }
87 }
66
88
67 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
89 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
90
68 }
91 }
69
92
93 //TODO "nice numbers algorithm"
94
70 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
@@ -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,29
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_background(new QGraphicsRectItem(this)),
22 m_axisY(new Axis(this)),
21 m_title(new QGraphicsTextItem(this)),
23 m_grid(new XYGrid(this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
24 m_plotDataIndex(0),
24 m_plotDataIndex(0),
25 m_marginSize(0)
25 m_marginSize(0)
26 {
26 {
27 // TODO: the default theme?
27 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
28 setTheme(QChart::ChartThemeVanilla);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 // set axis
30 PlotDomain domain;
31 m_axisY->rotate(90);
31 m_plotDomainList<<domain;
32
33 m_chartItems<<m_axisX;
34 m_chartItems<<m_axisY;
32 }
35 }
33
36
34 QChart::~QChart(){}
37 QChart::~QChart(){}
@@ -53,10 +56,10 void QChart::addSeries(QChartSeries* series)
53 if (!xyseries->color().isValid() && m_themeColors.count())
56 if (!xyseries->color().isValid() && m_themeColors.count())
54 xyseries->setColor(m_themeColors.takeFirst());
57 xyseries->setColor(m_themeColors.takeFirst());
55
58
56 XYPlotDomain domain;
59 m_plotDataIndex = 0 ;
57 //TODO "nice numbers algorithm"
60 m_plotDomainList.resize(1);
58 domain.m_ticksX=4;
61
59 domain.m_ticksY=4;
62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
60
63
61 for (int i = 0 ; i < xyseries->count() ; i++)
64 for (int i = 0 ; i < xyseries->count() ; i++)
62 {
65 {
@@ -69,9 +72,12 void QChart::addSeries(QChartSeries* series)
69 }
72 }
70
73
71 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
72 item->updateXYPlotDomain(domain);
75
73 m_plotDomainList<<domain;
76 m_chartItems<<item;
74 m_xyLineChartItems<<item;
77
78 foreach(ChartItem* i ,m_chartItems)
79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
80
75 break;
81 break;
76 }
82 }
77 case QChartSeries::SeriesTypeBar: {
83 case QChartSeries::SeriesTypeBar: {
@@ -137,32 +143,44 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
137 return series;
143 return series;
138 }
144 }
139
145
140 void QChart::setSize(const QSizeF& size)
146 void QChart::setSize(const QSize& size)
141 {
147 {
142 m_rect = QRect(QPoint(0,0),size.toSize());
148 m_rect = QRect(QPoint(0,0),size);
143 m_rect.adjust(margin(),margin(), -margin(), -margin());
149 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
144 m_grid->setPos(m_rect.topLeft());
145 m_grid->setSize(m_rect.size());
146
150
151
152 //recalculate background gradient
153 m_background->setRect(rect);
154 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
155 m_background->setBrush(m_backgroundGradient);
156
157 //resize elements
158 foreach (ChartItem* item ,m_chartItems) {
159 item->setPos(rect.topLeft());
160 item->setSize(rect.size());
161
162 }
147 // TODO: TTD for setting scale
163 // TODO: TTD for setting scale
148 //emit scaleChanged(100, 100);
164 //emit scaleChanged(100, 100);
149 // TODO: calculate the origo
165 // TODO: calculate the origo
150 // TODO: not sure if emitting a signal here is the best from performance point of view
166 // 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()));
167 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
152
168
153 for (int i(0); i < m_plotDomainList.size(); i++)
169 update();
154 m_plotDomainList[i].m_viewportRect = m_rect;
170 }
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
171
162 if (m_plotDomainList.count())
172 void QChart::setBackgroundColor(const QColor& color)
163 m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex));
173 {
174 m_backgroundGradient.setColorAt( 0.0, Qt::white);
175 m_backgroundGradient.setColorAt( 1.0, color);
176 m_background->setBrush(m_backgroundGradient);
177 m_background->setPen(Qt::NoPen);
178 m_background->update();
179 }
164
180
165 update();
181 void QChart::setTitle(const QString& title)
182 {
183 m_title->setPlainText(title);
166 }
184 }
167
185
168 int QChart::margin() const
186 int QChart::margin() const
@@ -208,6 +226,62 void QChart::setTheme(QChart::ChartTheme theme)
208 // TODO: update coloring of different elements to match the selected theme
226 // TODO: update coloring of different elements to match the selected theme
209 }
227 }
210
228
229 void QChart::zoomInToRect(const QRect& rectangle)
230 {
231
232 if(!rectangle.isValid()) return;
233
234 qreal margin = this->margin();
235
236 QRect rect = rectangle.normalized();
237 rect.translate(-margin, -margin);
238
239 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
240 PlotDomain newDomain;
241
242 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
243 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
244
245 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
246 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
247 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
248 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
249
250 m_plotDomainList.resize(m_plotDataIndex + 1);
251 m_plotDomainList<<newDomain;
252 m_plotDataIndex++;
253
254 foreach (ChartItem* item ,m_chartItems)
255 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
256 update();
257 }
258
259 void QChart::zoomIn()
260 {
261 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
262 m_plotDataIndex++;
263 foreach (ChartItem* item ,m_chartItems)
264 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
265 update();
266 }else{
267 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
268 rect.setWidth(rect.width()/2);
269 rect.setHeight(rect.height()/2);
270 rect.moveCenter(m_rect.center());
271 zoomInToRect(rect);
272 }
273 }
274
275 void QChart::zoomOut()
276 {
277 if (m_plotDataIndex > 0) {
278 m_plotDataIndex--;
279 foreach (ChartItem* item ,m_chartItems)
280 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
281 update();
282 }
283 }
284
211 #include "moc_qchart.cpp"
285 #include "moc_qchart.cpp"
212
286
213 QTCOMMERCIALCHART_END_NAMESPACE
287 QTCOMMERCIALCHART_END_NAMESPACE
@@ -4,14 +4,14
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartseries.h>
5 #include <qchartseries.h>
6 #include <QGraphicsObject>
6 #include <QGraphicsObject>
7 #include <QLinearGradient>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
10 class Axis;
11 class AxisItem;
11 class XYGrid;
12 class QChartSeries;
12 class QChartSeries;
13 class XYPlotDomain;
13 class PlotDomain;
14 class XYLineChartItem;
14 class ChartItem;
15 class BarGroup;
15 class BarGroup;
16
16
17 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 // TODO: We don't need to have QChart tied to QGraphicsItem:
@@ -45,29 +45,39 public:
45 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
45 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
46 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
46 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
47
47
48 virtual void setSize(const QSizeF& rect);
48 void setSize(const QSize& size);
49 void setMargin(int margin);
49 void setMargin(int margin);
50 int margin() const;
50 int margin() const;
51 void setTheme(QChart::ChartTheme theme);
51 void setTheme(QChart::ChartTheme theme);
52
52
53 void setTitle(const QString& title);
54 void setBackgroundColor(const QColor& color);
55
56 void zoomInToRect(const QRect& rectangle);
57 void zoomIn();
58 void zoomOut();
59
53 signals:
60 signals:
61 //TODO chage to const QSize& size
54 void sizeChanged(QRectF rect);
62 void sizeChanged(QRectF rect);
55 void scaleChanged(qreal xscale, qreal yscale);
63 void scaleChanged(qreal xscale, qreal yscale);
56
64
57 private:
65 private:
58 Q_DISABLE_COPY(QChart)
66 Q_DISABLE_COPY(QChart)
59 Axis* m_axisX;
67 QGraphicsRectItem* m_background;
60 Axis* m_axisY;
68 QLinearGradient m_backgroundGradient;
61 XYGrid* m_grid;
69 QGraphicsTextItem* m_title;
70 AxisItem* m_axisX;
71 AxisItem* m_axisY;
62 QRect m_rect;
72 QRect m_rect;
63 QList<const QChartSeries*> m_series;
73 QList<const QChartSeries*> m_series;
64 QList<XYPlotDomain> m_plotDomainList;
74 QVector<PlotDomain> m_plotDomainList;
65 QList<XYLineChartItem*> m_xyLineChartItems;
75 QList<ChartItem*> m_chartItems;
76 //TODO: remove
66 QList<QGraphicsItem*> m_items;
77 QList<QGraphicsItem*> m_items;
67 int m_plotDataIndex;
78 int m_plotDataIndex;
68 int m_marginSize;
79 int m_marginSize;
69 QList<QColor> m_themeColors;
80 QList<QColor> m_themeColors;
70
71 QList<BarGroup*> m_BarGroupItems;
81 QList<BarGroup*> m_BarGroupItems;
72 };
82 };
73
83
@@ -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,34 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
50 }
53 QRect rect(margin,margin,width()-2*margin,height()-2*margin);
54
55 m_origin = event->pos();
56
51
57 if (!rect.contains(m_origin)) return;
52 void QChartView::zoomIn()
53 {
54 m_chart->zoomIn();
55 }
58
56
59 if (!m_rubberBand)
57 void QChartView::zoomOut()
60 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
58 {
61 m_rubberBand->setGeometry(QRect(m_origin, QSize()));
59 m_chart->zoomOut();
62 m_showRubberBand=true;
60 }
63 m_rubberBand->show();
64
61
62 int QChartView::margin() const
63 {
64 return m_chart->margin();
65 }
65 }
66
66
67 void QChartView::mouseMoveEvent(QMouseEvent *event)
67 void QChartView::setTitle(const QString& title)
68 {
68 {
69 if(m_showRubberBand)
69 m_chart->setTitle(title);
70 m_rubberBand->setGeometry(QRect(m_origin, event->pos()).normalized());
71 }
70 }
72
71
73 void QChartView::mouseReleaseEvent(QMouseEvent *event)
72 void QChartView::setBackgroundColor(const QColor& color)
74 {
73 {
75 if(m_showRubberBand) {
74 m_chart->setBackgroundColor(color);
76 m_rubberBand->hide();
77 m_showRubberBand=false;
78 }
79 }
75 }
80
76
81 QTCOMMERCIALCHART_END_NAMESPACE
77 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,17 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 setTitle(const QString& title);
30 void mousePressEvent (QMouseEvent *event);
29 void setBackgroundColor(const QColor& color);
31 void mouseReleaseEvent (QMouseEvent *event);
30 void zoomInToRect(const QRect& rectangle);
31 void zoomIn();
32 void zoomOut();
32
33
33 private:
34 private:
34 QGraphicsScene *m_scene;
35 QGraphicsScene *m_scene;
35 QChart* m_chart;
36 QChart* m_chart;
36 QRubberBand *m_rubberBand;
37 QPoint m_origin;
37 QPoint m_origin;
38 bool m_showRubberBand;
39 Q_DISABLE_COPY(QChartView)
38 Q_DISABLE_COPY(QChartView)
40
39
41
40
@@ -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 \
@@ -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
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