@@ -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 | 1 | TEMPLATE = subdirs |
|
2 | SUBDIRS += linechart | |
|
2 | SUBDIRS += linechart \ | |
|
3 | zoomlinechart \ | |
|
4 | colorlinechart |
@@ -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(s |
|
|
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,70 +1,95 | |||
|
1 |
#include " |
|
|
1 | #include "axisitem_p.h" | |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | #include <QDebug> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 |
|
|
|
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 |
|
|
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 |
|
|
37 | QRectF AxisItem::boundingRect() const | |
|
27 | 38 | { |
|
28 |
|
|
|
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 | 49 | if (!m_rect.isValid()) |
|
35 | return; | |
|
50 | return; | |
|
36 | 51 | |
|
37 | const qreal deltaX = (m_rect.width() -1) / m_xyPlotData.ticksX(); | |
|
38 | const qreal deltaY = (m_rect.height() - 1) / m_xyPlotData.ticksY(); | |
|
52 | if(m_type==X_AXIS) { | |
|
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(); | |
|
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); | |
|
56 | for (int i = 0; i <= m_ticks; ++i) { | |
|
47 | 57 | |
|
48 | painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20, | |
|
49 | Qt::AlignHCenter | Qt::AlignTop, | |
|
50 | QString::number(label)); | |
|
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 | } | |
|
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(); | |
|
56 | qreal label = m_xyPlotData.m_minY + (j * m_xyPlotData.spanY() | |
|
57 | / m_xyPlotData.ticksY()); | |
|
74 | for (int j = 0; j <= m_ticks; ++j) { | |
|
58 | 75 | |
|
59 | painter->drawLine(m_rect.left(), y, m_rect.right()-1, y); | |
|
60 | //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y); | |
|
61 | //TODO : margin = 50 ; | |
|
62 | painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20, | |
|
63 | Qt::AlignRight | Qt::AlignVCenter, | |
|
64 | QString::number(label)); | |
|
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 | } | |
|
65 | 87 | } |
|
66 | 88 | |
|
67 | 89 | //painter->drawRect(m_rect.adjusted(0, 0, -1, -1)); |
|
90 | ||
|
68 | 91 | } |
|
69 | 92 | |
|
93 | //TODO "nice numbers algorithm" | |
|
94 | ||
|
70 | 95 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,33 +1,31 | |||
|
1 |
#include " |
|
|
1 | #include "plotdomain_p.h" | |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 |
|
|
|
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 |
|
|
|
14 | PlotDomain::~PlotDomain() | |
|
17 | 15 | { |
|
18 | 16 | // TODO Auto-generated destructor stub |
|
19 | 17 | } |
|
20 | 18 | |
|
21 |
qreal |
|
|
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 |
|
|
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 |
|
|
8 | class PlotDomain { | |
|
9 | 9 | public: |
|
10 |
|
|
|
11 |
virtual ~ |
|
|
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,287 | |||
|
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 " |
|
|
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_ |
|
|
23 | m_grid(new XYGrid(this)), | |
|
20 | m_background(new QGraphicsRectItem(this)), | |
|
21 | m_title(new QGraphicsTextItem(this)), | |
|
22 | m_axisX(new AxisItem(AxisItem::X_AXIS,this)), | |
|
23 | m_axisY(new AxisItem(AxisItem::Y_AXIS,this)), | |
|
24 | 24 | m_plotDataIndex(0), |
|
25 | 25 | m_marginSize(0) |
|
26 | 26 | { |
|
27 | 27 | // TODO: the default theme? |
|
28 | 28 | setTheme(QChart::ChartThemeVanilla); |
|
29 | 29 | // setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
30 | // set axis | |
|
31 | m_axisY->rotate(90); | |
|
30 | PlotDomain domain; | |
|
31 | m_plotDomainList<<domain; | |
|
32 | ||
|
33 | m_chartItems<<m_axisX; | |
|
34 | m_chartItems<<m_axisY; | |
|
32 | 35 | } |
|
33 | 36 | |
|
34 | 37 | QChart::~QChart(){} |
|
35 | 38 | |
|
36 | 39 | QRectF QChart::boundingRect() const |
|
37 | 40 | { |
|
38 | 41 | return m_rect; |
|
39 | 42 | } |
|
40 | 43 | |
|
41 | 44 | void QChart::addSeries(QChartSeries* series) |
|
42 | 45 | { |
|
43 | 46 | // TODO: we should check the series not already added |
|
44 | 47 | |
|
45 | 48 | m_series<<series; |
|
46 | 49 | |
|
47 | 50 | switch(series->type()) |
|
48 | 51 | { |
|
49 | 52 | case QChartSeries::SeriesTypeLine: { |
|
50 | 53 | |
|
51 | 54 | QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series); |
|
52 | 55 | // Use color defined by theme in case the series does not define a custom color |
|
53 | 56 | if (!xyseries->color().isValid() && m_themeColors.count()) |
|
54 | 57 | xyseries->setColor(m_themeColors.takeFirst()); |
|
55 | 58 | |
|
56 | XYPlotDomain domain; | |
|
57 | //TODO "nice numbers algorithm" | |
|
58 | domain.m_ticksX=4; | |
|
59 | domain.m_ticksY=4; | |
|
59 | m_plotDataIndex = 0 ; | |
|
60 | m_plotDomainList.resize(1); | |
|
61 | ||
|
62 | PlotDomain& domain = m_plotDomainList[m_plotDataIndex]; | |
|
60 | 63 | |
|
61 | 64 | for (int i = 0 ; i < xyseries->count() ; i++) |
|
62 | 65 | { |
|
63 | 66 | qreal x = xyseries->x(i); |
|
64 | 67 | qreal y = xyseries->y(i); |
|
65 | 68 | domain.m_minX = qMin(domain.m_minX,x); |
|
66 | 69 | domain.m_minY = qMin(domain.m_minY,y); |
|
67 | 70 | domain.m_maxX = qMax(domain.m_maxX,x); |
|
68 | 71 | domain.m_maxY = qMax(domain.m_maxY,y); |
|
69 | 72 | } |
|
70 | 73 | |
|
71 | 74 | XYLineChartItem* item = new XYLineChartItem(xyseries,this); |
|
72 | item->updateXYPlotDomain(domain); | |
|
73 | m_plotDomainList<<domain; | |
|
74 | m_xyLineChartItems<<item; | |
|
75 | ||
|
76 | m_chartItems<<item; | |
|
77 | ||
|
78 | foreach(ChartItem* i ,m_chartItems) | |
|
79 | i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex)); | |
|
80 | ||
|
75 | 81 | break; |
|
76 | 82 | } |
|
77 | 83 | case QChartSeries::SeriesTypeBar: { |
|
78 | 84 | |
|
79 | 85 | qDebug() << "barSeries added"; |
|
80 | 86 | BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); |
|
81 | 87 | |
|
82 | 88 | // Who owns the series? |
|
83 | 89 | BarGroup* group = new BarGroup(*barSeries, this); |
|
84 | 90 | scene()->addItem(group); |
|
85 | 91 | m_BarGroupItems.append(group); // If we need to access group later |
|
86 | 92 | break; |
|
87 | 93 | } |
|
88 | 94 | case QChartSeries::SeriesTypeScatter: { |
|
89 | 95 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
90 | 96 | connect(this, SIGNAL(sizeChanged(QRectF)), |
|
91 | 97 | scatterSeries, SLOT(chartSizeChanged(QRectF))); |
|
92 | 98 | scatterSeries->d->setParentItem(this); |
|
93 | 99 | QColor nextColor = m_themeColors.takeFirst(); |
|
94 | 100 | nextColor.setAlpha(150); // TODO: default opacity? |
|
95 | 101 | scatterSeries->setMarkerColor(nextColor); |
|
96 | 102 | } |
|
97 | 103 | case QChartSeries::SeriesTypePie: { |
|
98 | 104 | // TODO: we now have also a list of y values as a parameter, it is ignored |
|
99 | 105 | // we should use a generic data class instead of list of x and y values |
|
100 | 106 | QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); |
|
101 | 107 | connect(this, SIGNAL(sizeChanged(QRectF)), |
|
102 | 108 | pieSeries, SLOT(chartSizeChanged(QRectF))); |
|
103 | 109 | // TODO: how to define the color for all the slices of a pie? |
|
104 | 110 | } |
|
105 | 111 | } |
|
106 | 112 | } |
|
107 | 113 | |
|
108 | 114 | QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) |
|
109 | 115 | { |
|
110 | 116 | // TODO: support also other types; not only scatter and pie |
|
111 | 117 | |
|
112 | 118 | QChartSeries *series(0); |
|
113 | 119 | |
|
114 | 120 | switch (type) { |
|
115 | 121 | case QChartSeries::SeriesTypeLine: { |
|
116 | 122 | series = QXYChartSeries::create(); |
|
117 | 123 | break; |
|
118 | 124 | } |
|
119 | 125 | case QChartSeries::SeriesTypeBar: { |
|
120 | 126 | series = new BarChartSeries(this); |
|
121 | 127 | break; |
|
122 | 128 | } |
|
123 | 129 | case QChartSeries::SeriesTypeScatter: { |
|
124 | 130 | series = new QScatterSeries(this); |
|
125 | 131 | break; |
|
126 | 132 | } |
|
127 | 133 | case QChartSeries::SeriesTypePie: { |
|
128 | 134 | series = new QPieSeries(this); |
|
129 | 135 | break; |
|
130 | 136 | } |
|
131 | 137 | default: |
|
132 | 138 | Q_ASSERT(false); |
|
133 | 139 | break; |
|
134 | 140 | } |
|
135 | 141 | |
|
136 | 142 | addSeries(series); |
|
137 | 143 | return series; |
|
138 | 144 | } |
|
139 | 145 | |
|
140 |
void QChart::setSize(const QSize |
|
|
146 | void QChart::setSize(const QSize& size) | |
|
141 | 147 | { |
|
142 |
m_rect = QRect(QPoint(0,0),size |
|
|
143 | m_rect.adjust(margin(),margin(), -margin(), -margin()); | |
|
144 | m_grid->setPos(m_rect.topLeft()); | |
|
145 | m_grid->setSize(m_rect.size()); | |
|
148 | m_rect = QRect(QPoint(0,0),size); | |
|
149 | QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | |
|
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 | 163 | // TODO: TTD for setting scale |
|
148 | 164 | //emit scaleChanged(100, 100); |
|
149 | 165 | // TODO: calculate the origo |
|
150 | 166 | // TODO: not sure if emitting a signal here is the best from performance point of view |
|
151 | 167 | emit sizeChanged(QRectF(0, 0, size.width(), size.height())); |
|
152 | 168 | |
|
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 | ||
|
169 | update(); | |
|
170 | } | |
|
161 | 171 | |
|
162 | if (m_plotDomainList.count()) | |
|
163 | m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex)); | |
|
172 | void QChart::setBackgroundColor(const QColor& color) | |
|
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 | 186 | int QChart::margin() const |
|
169 | 187 | { |
|
170 | 188 | return m_marginSize; |
|
171 | 189 | } |
|
172 | 190 | |
|
173 | 191 | void QChart::setMargin(int margin) |
|
174 | 192 | { |
|
175 | 193 | m_marginSize = margin; |
|
176 | 194 | } |
|
177 | 195 | |
|
178 | 196 | void QChart::setTheme(QChart::ChartTheme theme) |
|
179 | 197 | { |
|
180 | 198 | // TODO: define color themes |
|
181 | 199 | switch (theme) { |
|
182 | 200 | case ChartThemeVanilla: |
|
183 | 201 | m_themeColors.append(QColor(255, 238, 174)); |
|
184 | 202 | m_themeColors.append(QColor(228, 228, 160)); |
|
185 | 203 | m_themeColors.append(QColor(228, 179, 160)); |
|
186 | 204 | m_themeColors.append(QColor(180, 151, 18)); |
|
187 | 205 | m_themeColors.append(QColor(252, 252, 37)); |
|
188 | 206 | break; |
|
189 | 207 | case ChartThemeIcy: |
|
190 | 208 | m_themeColors.append(QColor(255, 238, 174)); |
|
191 | 209 | m_themeColors.append(QColor(228, 228, 160)); |
|
192 | 210 | m_themeColors.append(QColor(228, 179, 160)); |
|
193 | 211 | m_themeColors.append(QColor(180, 151, 18)); |
|
194 | 212 | m_themeColors.append(QColor(252, 252, 37)); |
|
195 | 213 | break; |
|
196 | 214 | case ChartThemeGrayscale: |
|
197 | 215 | m_themeColors.append(QColor(255, 238, 174)); |
|
198 | 216 | m_themeColors.append(QColor(228, 228, 160)); |
|
199 | 217 | m_themeColors.append(QColor(228, 179, 160)); |
|
200 | 218 | m_themeColors.append(QColor(180, 151, 18)); |
|
201 | 219 | m_themeColors.append(QColor(252, 252, 37)); |
|
202 | 220 | break; |
|
203 | 221 | default: |
|
204 | 222 | Q_ASSERT(false); |
|
205 | 223 | break; |
|
206 | 224 | } |
|
207 | 225 | |
|
208 | 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 | 285 | #include "moc_qchart.cpp" |
|
212 | 286 | |
|
213 | 287 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,76 +1,86 | |||
|
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 | #include <QLinearGradient> | |
|
7 | 8 | |
|
8 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 10 | |
|
10 | class Axis; | |
|
11 | class XYGrid; | |
|
11 | class AxisItem; | |
|
12 | 12 | class QChartSeries; |
|
13 |
class |
|
|
14 |
class |
|
|
13 | class PlotDomain; | |
|
14 | class ChartItem; | |
|
15 | 15 | class BarGroup; |
|
16 | 16 | |
|
17 | 17 | // TODO: We don't need to have QChart tied to QGraphicsItem: |
|
18 | 18 | //class QTCOMMERCIALCHART_EXPORT QChart |
|
19 | 19 | //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem { |
|
20 | 20 | // public: QChartGraphicsItem(QChart &chart); |
|
21 | 21 | |
|
22 | 22 | /*! |
|
23 | 23 | * TODO: define the responsibilities |
|
24 | 24 | */ |
|
25 | 25 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject |
|
26 | 26 | { |
|
27 | 27 | Q_OBJECT |
|
28 | 28 | public: |
|
29 | 29 | enum ChartTheme { |
|
30 | 30 | ChartThemeVanilla = 0, |
|
31 | 31 | ChartThemeIcy, |
|
32 | 32 | ChartThemeGrayscale |
|
33 | 33 | }; |
|
34 | 34 | |
|
35 | 35 | public: |
|
36 | 36 | QChart(QGraphicsObject* parent = 0); |
|
37 | 37 | ~QChart(); |
|
38 | 38 | |
|
39 | 39 | //from QGraphicsItem |
|
40 | 40 | QRectF boundingRect() const; |
|
41 | 41 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){}; |
|
42 | 42 | |
|
43 | 43 | void addSeries(QChartSeries* series); |
|
44 | 44 | //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type); |
|
45 | 45 | // TODO: who owns the series now? maybe owned by chart and returned a reference instead... |
|
46 | 46 | QChartSeries* createSeries(QChartSeries::QChartSeriesType type); |
|
47 | 47 | |
|
48 |
|
|
|
48 | void setSize(const QSize& size); | |
|
49 | 49 | void setMargin(int margin); |
|
50 | 50 | int margin() const; |
|
51 | 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 | 60 | signals: |
|
61 | //TODO chage to const QSize& size | |
|
54 | 62 | void sizeChanged(QRectF rect); |
|
55 | 63 | void scaleChanged(qreal xscale, qreal yscale); |
|
56 | 64 | |
|
57 | 65 | private: |
|
58 | 66 | Q_DISABLE_COPY(QChart) |
|
59 | Axis* m_axisX; | |
|
60 | Axis* m_axisY; | |
|
61 | XYGrid* m_grid; | |
|
67 | QGraphicsRectItem* m_background; | |
|
68 | QLinearGradient m_backgroundGradient; | |
|
69 | QGraphicsTextItem* m_title; | |
|
70 | AxisItem* m_axisX; | |
|
71 | AxisItem* m_axisY; | |
|
62 | 72 | QRect m_rect; |
|
63 | 73 | QList<const QChartSeries*> m_series; |
|
64 |
Q |
|
|
65 |
QList< |
|
|
74 | QVector<PlotDomain> m_plotDomainList; | |
|
75 | QList<ChartItem*> m_chartItems; | |
|
76 | //TODO: remove | |
|
66 | 77 | QList<QGraphicsItem*> m_items; |
|
67 | 78 | int m_plotDataIndex; |
|
68 | 79 | int m_marginSize; |
|
69 | 80 | QList<QColor> m_themeColors; |
|
70 | ||
|
71 | 81 | QList<BarGroup*> m_BarGroupItems; |
|
72 | 82 | }; |
|
73 | 83 | |
|
74 | 84 | QTCOMMERCIALCHART_END_NAMESPACE |
|
75 | 85 | |
|
76 | 86 | #endif |
@@ -1,81 +1,77 | |||
|
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(); | |
|
49 | m_chart->zoomInToRect(rectangle); | |
|
50 | } | |
|
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) | |
|
60 | m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | |
|
61 | m_rubberBand->setGeometry(QRect(m_origin, QSize())); | |
|
62 | m_showRubberBand=true; | |
|
63 | m_rubberBand->show(); | |
|
57 | void QChartView::zoomOut() | |
|
58 | { | |
|
59 | m_chart->zoomOut(); | |
|
60 | } | |
|
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) | |
|
70 | m_rubberBand->setGeometry(QRect(m_origin, event->pos()).normalized()); | |
|
69 | m_chart->setTitle(title); | |
|
71 | 70 | } |
|
72 | 71 | |
|
73 | void QChartView::mouseReleaseEvent(QMouseEvent *event) | |
|
72 | void QChartView::setBackgroundColor(const QColor& color) | |
|
74 | 73 | { |
|
75 | if(m_showRubberBand) { | |
|
76 | m_rubberBand->hide(); | |
|
77 | m_showRubberBand=false; | |
|
78 | } | |
|
74 | m_chart->setBackgroundColor(color); | |
|
79 | 75 | } |
|
80 | 76 | |
|
81 | 77 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,46 +1,45 | |||
|
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 setTitle(const QString& title); | |
|
29 | void setBackgroundColor(const QColor& color); | |
|
30 | void zoomInToRect(const QRect& rectangle); | |
|
31 | void zoomIn(); | |
|
32 | void zoomOut(); | |
|
32 | 33 | |
|
33 | 34 | private: |
|
34 | 35 | QGraphicsScene *m_scene; |
|
35 | 36 | QChart* m_chart; |
|
36 | QRubberBand *m_rubberBand; | |
|
37 | 37 | QPoint m_origin; |
|
38 | bool m_showRubberBand; | |
|
39 | 38 | Q_DISABLE_COPY(QChartView) |
|
40 | 39 | |
|
41 | 40 | |
|
42 | 41 | }; |
|
43 | 42 | |
|
44 | 43 | QTCOMMERCIALCHART_END_NAMESPACE |
|
45 | 44 | |
|
46 | 45 | #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 |
|
|
|
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,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): |
|
|
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:: |
|
|
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 |
|
|
11 | class XYLineChartItem : public ChartItem | |
|
13 | 12 | { |
|
14 | 13 | |
|
15 | 14 | public: |
|
16 | 15 | XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0); |
|
17 |
|
|
|
16 | ~ XYLineChartItem(){}; | |
|
18 | 17 | |
|
19 | 18 | //from QGraphicsItem |
|
20 |
|
|
|
21 |
|
|
|
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 |
|
|
|
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 |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now