##// END OF EJS Templates
Refactora axis and line chart to use graphics items insted of painter.
Michal Klocek -
r85:f77f13ee265a
parent child
Show More
@@ -1,40 +1,44
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <cmath>
6 #include <cmath>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 #define PI 3.14159265358979
10 #define PI 3.14159265358979
11
11
12 int main(int argc, char *argv[])
12 int main(int argc, char *argv[])
13 {
13 {
14 QApplication a(argc, argv);
14 QApplication a(argc, argv);
15
15
16 QMainWindow window;
16 QMainWindow window;
17
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
21 series0->setPen(blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
22 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
23 QPen red(Qt::red);
24 red.setWidth(3);
25 series1->setPen(red);
22
26
23 int numPoints = 100;
27 int numPoints = 100;
24
28
25 for (int x = 0; x <= numPoints; ++x) {
29 for (int x = 0; x <= numPoints; ++x) {
26 series0->add(x, abs(sin(PI/50*x)*100));
30 series0->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
31 series1->add(x, abs(cos(PI/50*x)*100));
28 }
32 }
29
33
30 QChartView* chartView = new QChartView(&window);
34 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
35 chartView->addSeries(series0);
32 chartView->addSeries(series1);
36 chartView->addSeries(series1);
33 chartView->setBackgroundColor(Qt::yellow);
37 //chartView->setBackgroundColor(Qt::yellow);
34
38
35 window.setCentralWidget(chartView);
39 window.setCentralWidget(chartView);
36 window.resize(400, 300);
40 window.resize(400, 300);
37 window.show();
41 window.show();
38
42
39 return a.exec();
43 return a.exec();
40 }
44 }
@@ -1,39 +1,44
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <cmath>
6 #include <cmath>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 #define PI 3.14159265358979
10 #define PI 3.14159265358979
11
11
12 int main(int argc, char *argv[])
12 int main(int argc, char *argv[])
13 {
13 {
14 QApplication a(argc, argv);
14 QApplication a(argc, argv);
15
15
16 QMainWindow window;
16 QMainWindow window;
17
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
21 series0->setPen(blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
22 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
23 QPen red(Qt::red);
24 red.setWidth(3);
25 series1->setPen(red);
22
26
23 int numPoints = 100;
27 int numPoints = 100;
24
28
25 for (int x = 0; x <= numPoints; ++x) {
29 for (int x = 0; x <= numPoints; ++x) {
26 series0->add(x, abs(sin(PI/50*x)*100));
30 series0->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
31 series1->add(x, abs(cos(PI/50*x)*100));
28 }
32 }
29
33
30 QChartView* chartView = new QChartView(&window);
34 QChartView* chartView = new QChartView(&window);
35 chartView->setRenderHint(QPainter::Antialiasing);
31 chartView->addSeries(series0);
36 chartView->addSeries(series0);
32 chartView->addSeries(series1);
37 chartView->addSeries(series1);
33
38
34 window.setCentralWidget(chartView);
39 window.setCentralWidget(chartView);
35 window.resize(400, 300);
40 window.resize(400, 300);
36 window.show();
41 window.show();
37
42
38 return a.exec();
43 return a.exec();
39 }
44 }
@@ -1,38 +1,42
1 #include "chartwidget.h"
1 #include "chartwidget.h"
2 #include <QApplication>
2 #include <QApplication>
3 #include <QMainWindow>
3 #include <QMainWindow>
4 #include <qxychartseries.h>
4 #include <qxychartseries.h>
5 #include <cmath>
5 #include <cmath>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 #define PI 3.14159265358979
9 #define PI 3.14159265358979
10
10
11 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
12 {
12 {
13 QApplication a(argc, argv);
13 QApplication a(argc, argv);
14
14
15 QMainWindow window;
15 QMainWindow window;
16
16
17 QXYChartSeries* series0 = QXYChartSeries::create();
17 QXYChartSeries* series0 = QXYChartSeries::create();
18 series0->setColor(Qt::blue);
18 QPen blue(Qt::blue);
19 blue.setWidth(3);
20 series0->setPen(blue);
19 QXYChartSeries* series1 = QXYChartSeries::create();
21 QXYChartSeries* series1 = QXYChartSeries::create();
20 series1->setColor(Qt::red);
22 QPen red(Qt::red);
23 red.setWidth(3);
24 series1->setPen(red);
21
25
22 int numPoints = 100;
26 int numPoints = 100;
23
27
24 for (int x = 0; x <= numPoints; ++x) {
28 for (int x = 0; x <= numPoints; ++x) {
25 series0->add(x, abs(sin(PI/50*x)*100));
29 series0->add(x, abs(sin(PI/50*x)*100));
26 series1->add(x, abs(cos(PI/50*x)*100));
30 series1->add(x, abs(cos(PI/50*x)*100));
27 }
31 }
28
32
29 ChartWidget* chartWidget = new ChartWidget(&window);
33 ChartWidget* chartWidget = new ChartWidget(&window);
30 chartWidget->addSeries(series0);
34 chartWidget->addSeries(series0);
31 chartWidget->addSeries(series1);
35 chartWidget->addSeries(series1);
32
36
33 window.setCentralWidget(chartWidget);
37 window.setCentralWidget(chartWidget);
34 window.resize(400, 300);
38 window.resize(400, 300);
35 window.show();
39 window.show();
36
40
37 return a.exec();
41 return a.exec();
38 }
42 }
@@ -1,95 +1,170
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <QDebug>
3 #include <QDebug>
4
4
5 #define LABEL_PADDING 5
6
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
8
7 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent): ChartItem(parent),
8 m_ticks(4),
10 m_ticks(4),
9 m_type(type)
11 m_type(type)
10 {
12 {
11 }
13 }
12
14
13 AxisItem::~AxisItem()
15 AxisItem::~AxisItem()
14 {
16 {
15 }
17 }
16
18
17 void AxisItem::setSize(const QSize& size)
19 void AxisItem::setSize(const QSize& size)
18 {
20 {
19 m_rect = QRectF(QPoint(0,0),size);
21 m_rect = QRectF(QPoint(0,0),size);
22 createItems();
20 }
23 }
21
24
22 void AxisItem::setLength(int length)
25 void AxisItem::setLength(int length)
23 {
26 {
24 QPainterPath path;
27 QPainterPath path;
25 path.moveTo(QPointF(0,0));
28 path.moveTo(QPointF(0,0));
26 path.lineTo(length,0);
29 path.lineTo(length,0);
27 // path.lineTo(length-4,0);
30 // path.lineTo(length-4,0);
28 // path.lineTo(length,3);
31 // path.lineTo(length,3);
29 // path.lineTo(length-4,6);
32 // path.lineTo(length-4,6);
30 // path.lineTo(length-4,4);
33 // path.lineTo(length-4,4);
31 // path.lineTo(0,4);
34 // path.lineTo(0,4);
32 // path.lineTo(0,2);
35 // path.lineTo(0,2);
33 m_path=path;
36 m_path=path;
34 update();
37 update();
35 }
38 }
36
39
37 QRectF AxisItem::boundingRect() const
40 QRectF AxisItem::boundingRect() const
38 {
41 {
39 return m_rect;
42 return m_rect;
40 }
43 }
41
44
42 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
45 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
43 {
46 {
44 m_plotDomain = plotDomain;
47 m_plotDomain = plotDomain;
48 createItems();
45 }
49 }
46
50 /*
47 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
51 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
48 {
52 {
49 if (!m_rect.isValid())
53 if (!m_rect.isValid())
50 return;
54 return;
51
55
52 if(m_type==X_AXIS) {
56 if(m_type==X_AXIS) {
53
57
54 const qreal deltaX = (m_rect.width() -1) / m_ticks;
58 const qreal deltaX = m_rect.width() / m_ticks;
59
60 for (int i = 0; i <= m_ticks; ++i) {
61
62 int x = i * deltaX + m_rect.left();
55
63
56 for (int i = 0; i <= m_ticks; ++i) {
64 if(i==0) x--;
65 if(i==m_ticks) x++;
57
66
58 int x = i * deltaX + m_rect.left();
67 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
59 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
68 / m_ticks);
60 / m_ticks);
69 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
61 painter->drawLine(x, m_rect.top()+1, x, m_rect.bottom());
70 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
62 //painter->drawLine(x, m_rect.bottom(), x, m_rect.bottom() + 5);
63
71
64 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,
72 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
65 Qt::AlignHCenter | Qt::AlignTop,
73 }
66 QString::number(label));
67 }
68 }
74 }
69
75
70 if(m_type==Y_AXIS) {
76 if(m_type==Y_AXIS) {
71
77
72 const qreal deltaY = (m_rect.height() - 1) / m_ticks;
78 const qreal deltaY = (m_rect.height()) / m_ticks;
73
79
74 for (int j = 0; j <= m_ticks; ++j) {
80 for (int j = 0; j <= m_ticks; ++j) {
75
81
76 int y = j * -deltaY + m_rect.bottom();
82 int y = j * -deltaY + m_rect.bottom();
77 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
78 / m_ticks);
79
83
80 painter->drawLine(m_rect.left(), y, m_rect.right()-1, y);
84 if(j==0) y++;
81 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
85 if(j==m_ticks) y--;
82 //TODO : margin = 50 ;
86
83 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
87 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
84 Qt::AlignRight | Qt::AlignVCenter,
88 / m_ticks);
85 QString::number(label));
89
86 }
90 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
91 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
92 //TODO : margin = 50 ;
93 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
94 Qt::AlignRight | Qt::AlignVCenter,
95 QString::number(label));
96 }
87 }
97 }
88
98
89 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
99 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
100 }
101 */
102 void AxisItem::createItems()
103 {
104
105 //TODO: this is very inefficient handling
106
107 qDeleteAll(m_shades);
108 m_shades.clear();
109 qDeleteAll(m_grid);
110 m_grid.clear();
111 qDeleteAll(m_labels);
112 m_labels.clear();
113
114
115 if(m_type==X_AXIS) {
116
117 const qreal deltaX = m_rect.width() / m_ticks;
118
119 for (int i = 0; i <= m_ticks; ++i) {
120
121 int x = i * deltaX + m_rect.left();
122
123 //last grid outside chart rect
124 if(i==0) x--;
125 if(i==m_ticks) x++;
126
127 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
128
129 m_grid<<new QGraphicsLineItem(x, m_rect.top()-1, x, m_rect.bottom()+1+LABEL_PADDING,this);
130
131 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
132 QPointF center = text->boundingRect().center();
133 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
134 //text->rotate(-45);
135 m_labels<<text;
136 }
137 }
138
139 if(m_type==Y_AXIS) {
140
141 const qreal deltaY = m_rect.height()/ m_ticks;
142
143 for (int j = 0; j <= m_ticks; ++j) {
144
145 int y = j * -deltaY + m_rect.bottom();
146
147 //last grid outside chart rect
148 if(j==0) y++;
149 if(j==m_ticks) y--;
150
151 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
152 / m_ticks);
153
154 m_grid<<new QGraphicsLineItem(m_rect.left()- 1 - LABEL_PADDING , y, m_rect.right()+1, y,this);
155 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
156 QPointF center = text->boundingRect().center();
157
158 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
159 //text->rotate(-45);
160 m_labels<<text;
161
162 }
163 }
90
164
165 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
91 }
166 }
92
167
93 //TODO "nice numbers algorithm"
168 //TODO "nice numbers algorithm"
94
169
95 QTCOMMERCIALCHART_END_NAMESPACE
170 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,40 +1,46
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "plotdomain_p.h"
5 #include "plotdomain_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class AxisItem: public ChartItem
10 class AxisItem: public ChartItem
11 {
11 {
12 public:
12 public:
13 enum AxisType{X_AXIS,Y_AXIS};
13 enum AxisType{X_AXIS,Y_AXIS};
14
14
15 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
15 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
16 ~AxisItem();
16 ~AxisItem();
17
17
18 //from QGraphicsItem
18 //from QGraphicsItem
19 QRectF boundingRect() const;
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21
21
22 //from ChartItem
22 //from ChartItem
23 void setSize(const QSize& size);
23 void setSize(const QSize& size);
24 void setPlotDomain(const PlotDomain& data);
24 void setPlotDomain(const PlotDomain& data);
25
25
26 void setLength(int length);
26 void setLength(int length);
27 void setWidth(int width);
27 void setWidth(int width);
28 AxisType axisType() const {return m_type;};
28 AxisType axisType() const {return m_type;};
29
29
30 private:
30 private:
31 void createItems();
32 private:
31 QRectF m_rect;
33 QRectF m_rect;
32 int m_ticks;
34 int m_ticks;
33 PlotDomain m_plotDomain;
35 PlotDomain m_plotDomain;
34 QPainterPath m_path;
36 QPainterPath m_path;
37
38 QList<QGraphicsLineItem*> m_grid;
39 QList<QGraphicsRectItem*> m_shades;
40 QList<QGraphicsSimpleTextItem*> m_labels;
35 AxisType m_type;
41 AxisType m_type;
36 };
42 };
37
43
38 QTCOMMERCIALCHART_END_NAMESPACE
44 QTCOMMERCIALCHART_END_NAMESPACE
39
45
40 #endif /* AXISITEM_H_ */
46 #endif /* AXISITEM_H_ */
@@ -1,345 +1,366
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6 #include "qxychartseries.h"
6 #include "qxychartseries.h"
7
7 #include "qchartaxis.h"
8 #include "barchartseries.h"
8 #include "barchartseries.h"
9 #include "bargroup.h"
9 #include "bargroup.h"
10
10
11 #include "xylinechartitem_p.h"
11 #include "xylinechartitem_p.h"
12 #include "plotdomain_p.h"
12 #include "plotdomain_p.h"
13 #include "axisitem_p.h"
13 #include "axisitem_p.h"
14 #include <QGraphicsScene>
14 #include <QGraphicsScene>
15 #include <QDebug>
15 #include <QDebug>
16
16
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18
18
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem(this)),
20 m_background(new QGraphicsRectItem()),
21 m_title(new QGraphicsTextItem(this)),
21 m_title(new QGraphicsTextItem(this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
24 m_plotDataIndex(0),
23 m_plotDataIndex(0),
25 m_marginSize(0)
24 m_marginSize(0)
26 {
25 {
27 // TODO: the default theme?
26 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
27 //setTheme(QChart::ChartThemeVanilla);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
28 m_backgroundGradient.setColorAt(0.0, Qt::white);
29
30 PlotDomain domain;
30 PlotDomain domain;
31 m_plotDomainList<<domain;
31 m_plotDomainList<<domain;
32
32 m_axisY << new AxisItem(AxisItem::Y_AXIS,this);
33 m_chartItems<<m_axisX;
33 m_chartItems<<m_axisX;
34 m_chartItems<<m_axisY;
34 m_chartItems<<m_axisY.at(0);
35 }
35 }
36
36
37 QChart::~QChart(){}
37 QChart::~QChart(){}
38
38
39 QRectF QChart::boundingRect() const
39 QRectF QChart::boundingRect() const
40 {
40 {
41 return m_rect;
41 return m_rect;
42 }
42 }
43
43
44 void QChart::addSeries(QChartSeries* series)
44 void QChart::addSeries(QChartSeries* series)
45 {
45 {
46 // TODO: we should check the series not already added
46 // TODO: we should check the series not already added
47
47
48 m_chartSeries << series;
48 m_chartSeries << series;
49
49
50 switch(series->type())
50 switch(series->type())
51 {
51 {
52 case QChartSeries::SeriesTypeLine: {
52 case QChartSeries::SeriesTypeLine: {
53
53
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
55 // Use color defined by theme in case the series does not define a custom color
55 // Use color defined by theme in case the series does not define a custom color
56 if (!xyseries->color().isValid() && m_themeColors.count())
56
57 xyseries->setColor(nextColor());
57 if (!xyseries->pen().color().isValid() && m_themeColors.count()) //TODO: wtf
58 xyseries->setPen(nextColor());
58
59
59 m_plotDataIndex = 0 ;
60 m_plotDataIndex = 0 ;
60 m_plotDomainList.resize(1);
61 m_plotDomainList.resize(1);
61
62
62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
63 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
63
64
64 for (int i = 0 ; i < xyseries->count() ; i++)
65 for (int i = 0 ; i < xyseries->count() ; i++)
65 {
66 {
66 qreal x = xyseries->x(i);
67 qreal x = xyseries->x(i);
67 qreal y = xyseries->y(i);
68 qreal y = xyseries->y(i);
68 domain.m_minX = qMin(domain.m_minX,x);
69 domain.m_minX = qMin(domain.m_minX,x);
69 domain.m_minY = qMin(domain.m_minY,y);
70 domain.m_minY = qMin(domain.m_minY,y);
70 domain.m_maxX = qMax(domain.m_maxX,x);
71 domain.m_maxX = qMax(domain.m_maxX,x);
71 domain.m_maxY = qMax(domain.m_maxY,y);
72 domain.m_maxY = qMax(domain.m_maxY,y);
72 }
73 }
73
74
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75 m_chartItems<<item;
76 m_chartItems<<item;
76
77
77 foreach(ChartItem* i ,m_chartItems)
78 foreach(ChartItem* i ,m_chartItems)
78 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
79
80
80 break;
81 break;
81 }
82 }
82 case QChartSeries::SeriesTypeBar: {
83 case QChartSeries::SeriesTypeBar: {
83
84
84 qDebug() << "barSeries added";
85 qDebug() << "barSeries added";
85 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 BarGroup* barGroup = new BarGroup(*barSeries,this);
87 BarGroup* barGroup = new BarGroup(*barSeries,this);
87
88
88 // Add some fugly colors for 5 fist series...
89 // Add some fugly colors for 5 fist series...
89 barGroup->addColor(QColor(255,0,0,128));
90 barGroup->addColor(QColor(255,0,0,128));
90 barGroup->addColor(QColor(255,255,0,128));
91 barGroup->addColor(QColor(255,255,0,128));
91 barGroup->addColor(QColor(0,255,0,128));
92 barGroup->addColor(QColor(0,255,0,128));
92 barGroup->addColor(QColor(0,0,255,128));
93 barGroup->addColor(QColor(0,0,255,128));
93 barGroup->addColor(QColor(255,128,0,128));
94 barGroup->addColor(QColor(255,128,0,128));
94
95
95 m_chartItems<<barGroup;
96 m_chartItems<<barGroup;
96 childItems().append(barGroup);
97 childItems().append(barGroup);
97 break;
98 break;
98 }
99 }
99 case QChartSeries::SeriesTypeScatter: {
100 case QChartSeries::SeriesTypeScatter: {
100 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
101 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
101 scatterSeries->d->setParentItem(this);
102 scatterSeries->d->setParentItem(this);
102 // Set pre-defined colors in case the series has no colors defined
103 // Set pre-defined colors in case the series has no colors defined
103 if (!scatterSeries->markerColor().isValid())
104 if (!scatterSeries->markerColor().isValid())
104 scatterSeries->setMarkerColor(nextColor());
105 scatterSeries->setMarkerColor(nextColor());
105 connect(this, SIGNAL(sizeChanged(QRectF)),
106 connect(this, SIGNAL(sizeChanged(QRectF)),
106 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 scatterSeries, SLOT(chartSizeChanged(QRectF)));
107 // QColor nextColor = m_themeColors.takeFirst();
108 // QColor nextColor = m_themeColors.takeFirst();
108 // nextColor.setAlpha(150); // TODO: default opacity?
109 // nextColor.setAlpha(150); // TODO: default opacity?
109 // scatterSeries->setMarkerColor(nextColor);
110 // scatterSeries->setMarkerColor(nextColor);
110 break;
111 break;
111 }
112 }
112 case QChartSeries::SeriesTypePie: {
113 case QChartSeries::SeriesTypePie: {
113 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
114 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
114 for (int i(0); i < pieSeries->sliceCount(); i++) {
115 for (int i(0); i < pieSeries->sliceCount(); i++) {
115 if (!pieSeries->sliceColor(i).isValid())
116 if (!pieSeries->sliceColor(i).isValid())
116 pieSeries->setSliceColor(i, nextColor());
117 pieSeries->setSliceColor(i, nextColor());
117 }
118 }
118 connect(this, SIGNAL(sizeChanged(QRectF)),
119 connect(this, SIGNAL(sizeChanged(QRectF)),
119 pieSeries, SLOT(chartSizeChanged(QRectF)));
120 pieSeries, SLOT(chartSizeChanged(QRectF)));
120
121
121 // Set pre-defined colors in case the series has no colors defined
122 // Set pre-defined colors in case the series has no colors defined
122 // TODO: how to define the color for all the slices of a pie?
123 // TODO: how to define the color for all the slices of a pie?
123 // for (int (i); i < pieSeries.sliceCount(); i++)
124 // for (int (i); i < pieSeries.sliceCount(); i++)
124 break;
125 break;
125 }
126 }
126 }
127 }
127 }
128 }
128
129
129 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
130 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
130 {
131 {
131 // TODO: support also other types; not only scatter and pie
132 // TODO: support also other types; not only scatter and pie
132
133
133 QChartSeries *series(0);
134 QChartSeries *series(0);
134
135
135 switch (type) {
136 switch (type) {
136 case QChartSeries::SeriesTypeLine: {
137 case QChartSeries::SeriesTypeLine: {
137 series = QXYChartSeries::create();
138 series = QXYChartSeries::create();
138 break;
139 break;
139 }
140 }
140 case QChartSeries::SeriesTypeBar: {
141 case QChartSeries::SeriesTypeBar: {
141 series = new BarChartSeries(this);
142 series = new BarChartSeries(this);
142 break;
143 break;
143 }
144 }
144 case QChartSeries::SeriesTypeScatter: {
145 case QChartSeries::SeriesTypeScatter: {
145 series = new QScatterSeries(this);
146 series = new QScatterSeries(this);
146 break;
147 break;
147 }
148 }
148 case QChartSeries::SeriesTypePie: {
149 case QChartSeries::SeriesTypePie: {
149 series = new QPieSeries(this);
150 series = new QPieSeries(this);
150 break;
151 break;
151 }
152 }
152 default:
153 default:
153 Q_ASSERT(false);
154 Q_ASSERT(false);
154 break;
155 break;
155 }
156 }
156
157
157 addSeries(series);
158 addSeries(series);
158 return series;
159 return series;
159 }
160 }
160
161
161 void QChart::setSize(const QSize& size)
162 void QChart::setSize(const QSize& size)
162 {
163 {
163 m_rect = QRect(QPoint(0,0),size);
164 m_rect = QRect(QPoint(0,0),size);
164 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
165 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
165
166
166
167
167 //recalculate background gradient
168 //recalculate background gradient
168 m_background->setRect(rect);
169 m_background->setRect(rect);
169 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
170 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
170 m_background->setBrush(m_backgroundGradient);
171 m_background->setBrush(m_backgroundGradient);
172 m_background->setPen(Qt::NoPen);
171
173
172 //resize elements
174 //resize elements
173 foreach (ChartItem* item ,m_chartItems) {
175 foreach (ChartItem* item ,m_chartItems) {
174 item->setPos(rect.topLeft());
176 item->setPos(rect.topLeft());
175 item->setSize(rect.size());
177 item->setSize(rect.size());
176
178
177 }
179 }
178 // TODO: TTD for setting scale
180 // TODO: TTD for setting scale
179 //emit scaleChanged(100, 100);
181 //emit scaleChanged(100, 100);
180 // TODO: calculate the origo
182 // TODO: calculate the origo
181 // TODO: not sure if emitting a signal here is the best from performance point of view
183 // TODO: not sure if emitting a signal here is the best from performance point of view
182 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
184 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
183
185
184 update();
186 update();
185 }
187 }
186
188
187 void QChart::setBackgroundColor(const QColor& color)
189 void QChart::setBackgroundColor(const QColor& color)
188 {
190 {
189 m_backgroundGradient.setColorAt( 0.0, Qt::white);
191 m_backgroundGradient.setColorAt( 0.0, Qt::white);
190 m_backgroundGradient.setColorAt( 1.0, color);
192 m_backgroundGradient.setColorAt( 1.0, color);
191 m_background->setBrush(m_backgroundGradient);
193 m_background->setBrush(m_backgroundGradient);
192 m_background->setPen(Qt::NoPen);
194 m_background->setPen(Qt::NoPen);
193 m_background->update();
195 m_background->update();
194 }
196 }
195
197
196 void QChart::setTitle(const QString& title)
198 void QChart::setTitle(const QString& title)
197 {
199 {
198 m_title->setPlainText(title);
200 m_title->setPlainText(title);
199 }
201 }
200
202
201 int QChart::margin() const
203 int QChart::margin() const
202 {
204 {
203 return m_marginSize;
205 return m_marginSize;
204 }
206 }
205
207
206 void QChart::setMargin(int margin)
208 void QChart::setMargin(int margin)
207 {
209 {
208 m_marginSize = margin;
210 m_marginSize = margin;
209 }
211 }
210
212
211 void QChart::setTheme(QChart::ChartThemeId theme)
213 void QChart::setTheme(QChart::ChartThemeId theme)
212 {
214 {
213 // if (theme != m_currentTheme) {
215 // if (theme != m_currentTheme) {
214 m_themeColors.clear();
216 m_themeColors.clear();
215
217
216 // TODO: define color themes
218 // TODO: define color themes
217 switch (theme) {
219 switch (theme) {
218 case QChart::ChartThemeDefault:
220 case QChart::ChartThemeDefault:
219 // TODO: define the default theme based on the OS
221 // TODO: define the default theme based on the OS
220 // For now we just fallthrough to "vanilla"
222 // For now we just fallthrough to "vanilla"
221 case QChart::ChartThemeVanilla:
223 case QChart::ChartThemeVanilla:
222 m_themeColors.append(QColor(217, 197, 116));
224 m_themeColors.append(QColor(217, 197, 116));
223 m_themeColors.append(QColor(214, 168, 150));
225 m_themeColors.append(QColor(214, 168, 150));
224 m_themeColors.append(QColor(160, 160, 113));
226 m_themeColors.append(QColor(160, 160, 113));
225 m_themeColors.append(QColor(210, 210, 52));
227 m_themeColors.append(QColor(210, 210, 52));
226 m_themeColors.append(QColor(136, 114, 58));
228 m_themeColors.append(QColor(136, 114, 58));
227
229
228 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
230 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xff9d844d)));
229 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
231 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
230 break;
232 break;
231 case QChart::ChartThemeIcy:
233 case QChart::ChartThemeIcy:
232 m_themeColors.append(QColor(0, 3, 165));
234 m_themeColors.append(QColor(0, 3, 165));
233 m_themeColors.append(QColor(49, 52, 123));
235 m_themeColors.append(QColor(49, 52, 123));
234 m_themeColors.append(QColor(71, 114, 187));
236 m_themeColors.append(QColor(71, 114, 187));
235 m_themeColors.append(QColor(48, 97, 87));
237 m_themeColors.append(QColor(48, 97, 87));
236 m_themeColors.append(QColor(19, 71, 90));
238 m_themeColors.append(QColor(19, 71, 90));
237 m_themeColors.append(QColor(110, 70, 228));
239 m_themeColors.append(QColor(110, 70, 228));
238
240
239 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
241 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffe4ffff)));
240 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff)));
242 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffe4ffff)));
241 break;
243 break;
242 case QChart::ChartThemeGrayscale:
244 case QChart::ChartThemeGrayscale:
243 m_themeColors.append(QColor(0, 0, 0));
245 m_themeColors.append(QColor(0, 0, 0));
244 m_themeColors.append(QColor(50, 50, 50));
246 m_themeColors.append(QColor(50, 50, 50));
245 m_themeColors.append(QColor(100, 100, 100));
247 m_themeColors.append(QColor(100, 100, 100));
246 m_themeColors.append(QColor(140, 140, 140));
248 m_themeColors.append(QColor(140, 140, 140));
247 m_themeColors.append(QColor(180, 180, 180));
249 m_themeColors.append(QColor(180, 180, 180));
248
250
249 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
251 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xffffffff)));
250 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
252 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
251 break;
253 break;
252 case QChart::ChartThemeUnnamed1:
254 case QChart::ChartThemeUnnamed1:
253 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
255 m_themeColors.append(QColor(QRgb(0xff3fa9f5)));
254 m_themeColors.append(QColor(QRgb(0xff7AC943)));
256 m_themeColors.append(QColor(QRgb(0xff7AC943)));
255 m_themeColors.append(QColor(QRgb(0xffFF931E)));
257 m_themeColors.append(QColor(QRgb(0xffFF931E)));
256 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
258 m_themeColors.append(QColor(QRgb(0xffFF1D25)));
257 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
259 m_themeColors.append(QColor(QRgb(0xffFF7BAC)));
258
260
259 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
261 m_backgroundGradient.setColorAt(0.0, QColor(QRgb(0xfff3dc9e)));
260 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
262 m_backgroundGradient.setColorAt(1.0, QColor(QRgb(0xffafafaf)));
261 break;
263 break;
262 default:
264 default:
263 Q_ASSERT(false);
265 Q_ASSERT(false);
264 break;
266 break;
265 }
267 }
266
268
267 m_background->setBrush(m_backgroundGradient);
269 m_background->setBrush(m_backgroundGradient);
268 m_background->setPen(Qt::NoPen);
270 m_background->setPen(Qt::NoPen);
269
271
270 foreach(QChartSeries* series, m_chartSeries) {
272 foreach(QChartSeries* series, m_chartSeries) {
271 // TODO: other series interested on themes?
273 // TODO: other series interested on themes?
272 if (series->type() == QChartSeries::SeriesTypeLine) {
274 if (series->type() == QChartSeries::SeriesTypeLine) {
273 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
275 QXYChartSeries *lineseries = reinterpret_cast<QXYChartSeries *>(series);
274 lineseries->setColor(nextColor());
276 lineseries->setPen(nextColor());
275 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
277 } else if (series->type() == QChartSeries::SeriesTypeScatter) {
276 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
278 QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
277 scatter->setMarkerColor(nextColor());
279 scatter->setMarkerColor(nextColor());
278 } else if (series->type() == QChartSeries::SeriesTypePie) {
280 } else if (series->type() == QChartSeries::SeriesTypePie) {
279 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
281 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
280 for (int i(0); i < pieSeries->sliceCount(); i++)
282 for (int i(0); i < pieSeries->sliceCount(); i++)
281 pieSeries->setSliceColor(i, nextColor());
283 pieSeries->setSliceColor(i, nextColor());
282 }
284 }
283 }
285 }
284 update();
286 update();
285 }
287 }
286
288
287 QColor QChart::nextColor()
289 QColor QChart::nextColor()
288 {
290 {
289 QColor nextColor = m_themeColors.first();
291 QColor nextColor = m_themeColors.first();
290 m_themeColors.move(0, m_themeColors.size() - 1);
292 m_themeColors.move(0, m_themeColors.size() - 1);
291 return nextColor;
293 return nextColor;
292 }
294 }
293
295
294 void QChart::zoomInToRect(const QRect& rectangle)
296 void QChart::zoomInToRect(const QRect& rectangle)
295 {
297 {
296
298
297 if(!rectangle.isValid()) return;
299 if(!rectangle.isValid()) return;
298
300
299 qreal margin = this->margin();
301 qreal margin = this->margin();
300
302
301 QRect rect = rectangle.normalized();
303 QRect rect = rectangle.normalized();
302 rect.translate(-margin, -margin);
304 rect.translate(-margin, -margin);
303
305
304 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
306 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
305
307
306 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
308 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
307
309
308 m_plotDomainList.resize(m_plotDataIndex + 1);
310 m_plotDomainList.resize(m_plotDataIndex + 1);
309 m_plotDomainList<<domain;
311 m_plotDomainList<<domain;
310 m_plotDataIndex++;
312 m_plotDataIndex++;
311
313
312 foreach (ChartItem* item ,m_chartItems)
314 foreach (ChartItem* item ,m_chartItems)
313 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
315 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
314 update();
316 update();
315 }
317 }
316
318
317 void QChart::zoomIn()
319 void QChart::zoomIn()
318 {
320 {
319 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
321 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
320 m_plotDataIndex++;
322 m_plotDataIndex++;
321 foreach (ChartItem* item ,m_chartItems)
323 foreach (ChartItem* item ,m_chartItems)
322 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
324 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
323 update();
325 update();
324 }else{
326 }else{
325 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
327 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
326 rect.setWidth(rect.width()/2);
328 rect.setWidth(rect.width()/2);
327 rect.setHeight(rect.height()/2);
329 rect.setHeight(rect.height()/2);
328 rect.moveCenter(m_rect.center());
330 rect.moveCenter(m_rect.center());
329 zoomInToRect(rect);
331 zoomInToRect(rect);
330 }
332 }
331 }
333 }
332
334
333 void QChart::zoomOut()
335 void QChart::zoomOut()
334 {
336 {
335 if (m_plotDataIndex > 0) {
337 if (m_plotDataIndex > 0) {
336 m_plotDataIndex--;
338 m_plotDataIndex--;
337 foreach (ChartItem* item ,m_chartItems)
339 foreach (ChartItem* item ,m_chartItems)
338 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
340 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
339 update();
341 update();
340 }
342 }
341 }
343 }
342
344
345 void QChart::setAxisX(const QChartAxis& axis)
346 {
347 setAxis(m_axisX,axis);
348 }
349 void QChart::setAxisY(const QChartAxis& axis)
350 {
351 setAxis(m_axisY.at(0),axis);
352 }
353
354 void QChart::setAxisY(const QList<QChartAxis>& axis)
355 {
356 //TODO not implemented
357 }
358
359 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
360 {
361 item->setVisible(axis.isAxisVisible());
362 }
363
343 #include "moc_qchart.cpp"
364 #include "moc_qchart.cpp"
344
365
345 QTCOMMERCIALCHART_END_NAMESPACE
366 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,95 +1,97
1 #ifndef CHART_H
1 #ifndef CHART_H
2 #define CHART_H
2 #define CHART_H
3
3
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 #include <QLinearGradient>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class AxisItem;
11 class AxisItem;
12 class QChartSeries;
12 class QChartSeries;
13 class PlotDomain;
13 class PlotDomain;
14 class ChartItem;
14 class ChartItem;
15 class BarGroup;
15 class BarGroup;
16 class QChartAxis;
16 class QChartAxis;
17
17
18 // TODO: We don't need to have QChart tied to QGraphicsItem:
18 // TODO: We don't need to have QChart tied to QGraphicsItem:
19 //class QTCOMMERCIALCHART_EXPORT QChart
19 //class QTCOMMERCIALCHART_EXPORT QChart
20 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
20 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
21 // public: QChartGraphicsItem(QChart &chart);
21 // public: QChartGraphicsItem(QChart &chart);
22
22
23 /*!
23 /*!
24 * TODO: define the responsibilities
24 * TODO: define the responsibilities
25 */
25 */
26 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
26 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
27 {
27 {
28 Q_OBJECT
28 Q_OBJECT
29 public:
29 public:
30 enum ChartThemeId {
30 enum ChartThemeId {
31 /*! The default theme follows the GUI style of the Operating System */
31 /*! The default theme follows the GUI style of the Operating System */
32 ChartThemeDefault = 0,
32 ChartThemeDefault = 0,
33 ChartThemeVanilla,
33 ChartThemeVanilla,
34 ChartThemeIcy,
34 ChartThemeIcy,
35 ChartThemeGrayscale,
35 ChartThemeGrayscale,
36 //ChartThemeScientific,
36 //ChartThemeScientific,
37 ChartThemeUnnamed1
37 ChartThemeUnnamed1
38 };
38 };
39
39
40 public:
40 public:
41 QChart(QGraphicsObject* parent = 0);
41 QChart(QGraphicsObject* parent = 0);
42 ~QChart();
42 ~QChart();
43
43
44 //from QGraphicsItem
44 //from QGraphicsItem
45 QRectF boundingRect() const;
45 QRectF boundingRect() const;
46 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
46 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
47
47
48 void addSeries(QChartSeries* series);
48 void addSeries(QChartSeries* series);
49 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
49 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
50 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
50 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
51 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
51 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
52
52
53 void setSize(const QSize& size);
53 void setSize(const QSize& size);
54 void setMargin(int margin);
54 void setMargin(int margin);
55 int margin() const;
55 int margin() const;
56 void setTheme(QChart::ChartThemeId theme);
56 void setTheme(QChart::ChartThemeId theme);
57
57
58 void setTitle(const QString& title);
58 void setTitle(const QString& title);
59 void setBackgroundColor(const QColor& color);
59 void setBackgroundColor(const QColor& color);
60
60
61 void zoomInToRect(const QRect& rectangle);
61 void zoomInToRect(const QRect& rectangle);
62 void zoomIn();
62 void zoomIn();
63 void zoomOut();
63 void zoomOut();
64
64
65 void setAxisX(QChartAxis* axis){};
65 void setAxisX(const QChartAxis& axis);
66 void setAxisY(QChartAxis* axis){};
66 void setAxisY(const QChartAxis& axis);
67 void setAxisY(QList<QChartAxis*> axis){};
67 void setAxisY(const QList<QChartAxis>& axis);
68
68
69 private:
70 void setAxis(AxisItem *item, const QChartAxis& axis);
69
71
70 signals:
72 signals:
71 //TODO chage to const QSize& size
73 //TODO chage to const QSize& size
72 void sizeChanged(QRectF rect);
74 void sizeChanged(QRectF rect);
73 void scaleChanged(qreal xscale, qreal yscale);
75 void scaleChanged(qreal xscale, qreal yscale);
74
76
75 private:
77 private:
76 QColor nextColor();
78 QColor nextColor();
77
79
78 Q_DISABLE_COPY(QChart)
80 Q_DISABLE_COPY(QChart)
79 QGraphicsRectItem* m_background;
81 QGraphicsRectItem* m_background;
80 QLinearGradient m_backgroundGradient;
82 QLinearGradient m_backgroundGradient;
81 QGraphicsTextItem* m_title;
83 QGraphicsTextItem* m_title;
82 AxisItem* m_axisX;
84 AxisItem* m_axisX;
83 AxisItem* m_axisY;
85 QList<AxisItem*> m_axisY;
84 QRect m_rect;
86 QRect m_rect;
85 QList<QChartSeries*> m_chartSeries;
87 QList<QChartSeries*> m_chartSeries;
86 QVector<PlotDomain> m_plotDomainList;
88 QVector<PlotDomain> m_plotDomainList;
87 QList<ChartItem*> m_chartItems;
89 QList<ChartItem*> m_chartItems;
88 int m_plotDataIndex;
90 int m_plotDataIndex;
89 int m_marginSize;
91 int m_marginSize;
90 QList<QColor> m_themeColors;
92 QList<QColor> m_themeColors;
91 };
93 };
92
94
93 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
94
96
95 #endif
97 #endif
@@ -1,14 +1,43
1
1
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3
3
4 QChartAxis::QChartAxis()
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 QChartAxis::QChartAxis():
7 m_axisVisible(true),
8 m_girdVisible(true),
9 m_labelsVisible(true),
10 m_rowShadesVisible(true)
5 {
11 {
6 // TODO Auto-generated constructor stub
12 // TODO Auto-generated constructor stub
7
13
8 }
14 }
9
15
10 QChartAxis::~QChartAxis()
16 QChartAxis::~QChartAxis()
11 {
17 {
12 // TODO Auto-generated destructor stub
18 // TODO Auto-generated destructor stub
13 }
19 }
14
20
21 void QChartAxis::setAxisVisible(bool visible)
22 {
23 m_axisVisible=visible;
24 }
25
26 void QChartAxis::setGridVisible(bool visible)
27 {
28 m_girdVisible=visible;
29 }
30
31 void QChartAxis::setLabelsVisible(bool visible)
32 {
33 m_labelsVisible=visible;
34 }
35
36 void QChartAxis::setRowShadesVisible(bool visible)
37 {
38 m_rowShadesVisible=visible;
39 }
40
41
42
43 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,11 +1,31
1 #ifndef QCHARTAXIS_H_
1 #ifndef QCHARTAXIS_H_
2 #define QCHARTAXIS_H_
2 #define QCHARTAXIS_H_
3
3
4 #include <qchartglobal.h>
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
4 class QChartAxis
8 class QChartAxis
5 {
9 {
6 public:
10 public:
7 QChartAxis();
11 QChartAxis();
8 virtual ~QChartAxis();
12 virtual ~QChartAxis();
13
14 bool isAxisVisible() const { return m_axisVisible;};
15 void setAxisVisible(bool visible);
16 bool isGridVisible() const { return m_girdVisible;};
17 void setGridVisible(bool visible);
18 bool isLabelsVisible() const { return m_labelsVisible;};
19 void setLabelsVisible(bool visible);
20 bool isRowShadesVisible() const { return m_rowShadesVisible;};
21 void setRowShadesVisible(bool visible);
22
23 private:
24 bool m_axisVisible;
25 bool m_girdVisible;
26 bool m_labelsVisible;
27 bool m_rowShadesVisible;
9 };
28 };
10
29
30 QTCOMMERCIALCHART_END_NAMESPACE
11 #endif /* QCHARTAXIS_H_ */
31 #endif /* QCHARTAXIS_H_ */
@@ -1,68 +1,72
1 #include "qxychartseries.h"
1 #include "qxychartseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent),
5 QXYChartSeries::QXYChartSeries(QObject* parent):QChartSeries(parent)
6 m_color()
7 {
6 {
8 }
7 }
9
8
10 QXYChartSeries::~QXYChartSeries()
9 QXYChartSeries::~QXYChartSeries()
11 {
10 {
12 }
11 }
13
12
14 QXYChartSeries* QXYChartSeries::create(QObject* parent)
13 QXYChartSeries* QXYChartSeries::create(QObject* parent)
15 {
14 {
16 //TODO: here we take QChartData when it is ready
15 //TODO: here we take QChartData when it is ready
17 // return null if malformed;
16 // return null if malformed;
18 return new QXYChartSeries(parent);
17 return new QXYChartSeries(parent);
19 }
18 }
20
19
21 void QXYChartSeries::setColor(const QColor& color)
22 {
23 m_color = color;
24 }
25
26 void QXYChartSeries::add(qreal x,qreal y)
20 void QXYChartSeries::add(qreal x,qreal y)
27 {
21 {
28 m_x<<x;
22 m_x<<x;
29 m_y<<y;
23 m_y<<y;
30 }
24 }
31
25
32 void QXYChartSeries::clear()
26 void QXYChartSeries::clear()
33 {
27 {
34 m_x.clear();
28 m_x.clear();
35 m_y.clear();
29 m_y.clear();
36 }
30 }
37
31
38 qreal QXYChartSeries::x(int pos) const
32 qreal QXYChartSeries::x(int pos) const
39 {
33 {
40 return m_x.at(pos);
34 return m_x.at(pos);
41 }
35 }
42
36
43 qreal QXYChartSeries::y(int pos) const
37 qreal QXYChartSeries::y(int pos) const
44 {
38 {
45 return m_y.at(pos);
39 return m_y.at(pos);
46 }
40 }
47
41
48 int QXYChartSeries::count() const
42 int QXYChartSeries::count() const
49 {
43 {
50 Q_ASSERT(m_x.size() == m_y.size());
44 Q_ASSERT(m_x.size() == m_y.size());
51
45
52 return m_x.size();
46 return m_x.size();
53
47
54 }
48 }
55
49
50 void QXYChartSeries::setPen(const QPen& pen)
51 {
52 m_pen=pen;
53 }
54
55 void QXYChartSeries::setBrush(const QBrush& brush)
56 {
57 m_brush=brush;
58 }
59
56 QDebug operator<< (QDebug debug, const QXYChartSeries series)
60 QDebug operator<< (QDebug debug, const QXYChartSeries series)
57 {
61 {
58 Q_ASSERT(series.m_x.size() == series.m_y.size());
62 Q_ASSERT(series.m_x.size() == series.m_y.size());
59
63
60 int size = series.m_x.size();
64 int size = series.m_x.size();
61
65
62 for (int i=0;i<size;i++) {
66 for (int i=0;i<size;i++) {
63 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
67 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
64 }
68 }
65 return debug.space();
69 return debug.space();
66 }
70 }
67
71
68 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,42 +1,48
1 #ifndef QXYSERIES_H_
1 #ifndef QXYSERIES_H_
2 #define QXYSERIES_H_
2 #define QXYSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchartseries.h"
5 #include "qchartseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QColor>
7 #include <QPen>
8 #include <QBrush>
8
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11
11 class QTCOMMERCIALCHART_EXPORT QXYChartSeries : public QChartSeries
12 class QTCOMMERCIALCHART_EXPORT QXYChartSeries : public QChartSeries
12 {
13 {
13 //TODO:
14 //TODO:
14 // Q_OBJECT
15 // Q_OBJECT
15 private:
16 private:
16 QXYChartSeries(QObject* parent=0);
17 QXYChartSeries(QObject* parent=0);
17 public:
18 public:
18 virtual ~QXYChartSeries();
19 virtual ~QXYChartSeries();
19
20
20 //implemented from QChartSeries
21 //implemented from QChartSeries
21 static QXYChartSeries* create(QObject* parent=0);
22 static QXYChartSeries* create(QObject* parent=0);
22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
23 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
23
24
24 void add(qreal x, qreal y);
25 void add(qreal x, qreal y);
25 void clear();
26 void clear();
26 void setColor(const QColor& color);
27
27 const QColor& color() const { return m_color;}
28 void setPen(const QPen& pen);
29 const QPen& pen() const { return m_pen;}
30 void setBrush(const QBrush& brush);
31 const QBrush& brush() const { return m_brush;}
32
28 int count() const;
33 int count() const;
29 qreal x(int pos) const;
34 qreal x(int pos) const;
30 qreal y(int pos) const;
35 qreal y(int pos) const;
31 friend QDebug operator<< (QDebug d, const QXYChartSeries series);
36 friend QDebug operator<< (QDebug d, const QXYChartSeries series);
32
37
33 private:
38 private:
34 QColor m_color;
35 QList<qreal> m_x;
39 QList<qreal> m_x;
36 QList<qreal> m_y;
40 QList<qreal> m_y;
41 QBrush m_brush;
42 QPen m_pen;
37
43
38 };
44 };
39
45
40 QTCOMMERCIALCHART_END_NAMESPACE
46 QTCOMMERCIALCHART_END_NAMESPACE
41
47
42 #endif
48 #endif
@@ -1,67 +1,67
1 #include "xylinechartitem_p.h"
1 #include "xylinechartitem_p.h"
2 #include "axisitem_p.h"
2 #include "axisitem_p.h"
3 #include "qxychartseries.h"
3 #include "qxychartseries.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QStyleOptionGraphicsItem>
5 #include <QStyleOptionGraphicsItem>
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
10 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 m_series(series),
11 m_series(series),
12 m_dirty(false)
12 m_pathItem(new QGraphicsPathItem(this))
13 {
13 {
14
14 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
15 }
15 }
16
16
17 void XYLineChartItem::setSize(const QSize& size)
17 void XYLineChartItem::setSize(const QSize& size)
18 {
18 {
19 m_rect=QRect(0,0,size.width(),size.height());
19 m_rect=QRect(0,0,size.width(),size.height());
20 m_dirty=true;
20 prepareGeometryChange();
21 updateGeometry();
22
21 }
23 }
22
24
23 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
25 void XYLineChartItem::setPlotDomain(const PlotDomain& data)
24 {
26 {
25 m_plotDomain=data;
27 m_plotDomain=data;
26 m_dirty=true;
28 prepareGeometryChange();
29 updateGeometry();
30
27 }
31 }
28
32
29 QRectF XYLineChartItem::boundingRect() const
33 QRectF XYLineChartItem::boundingRect() const
30 {
34 {
31 return m_polyline.boundingRect();
35 return m_rect;
32 }
36 }
33
37 /*
38 QPainterPath XYLineChartItem::shape() const
39 {
40 return m_pathItem->shape();
41 }
42 */
34 void XYLineChartItem::updateGeometry()
43 void XYLineChartItem::updateGeometry()
35 {
44 {
36 if (!m_rect.isValid()) return;
45 if (!m_rect.isValid()) return;
37
46
38 const qreal deltaX = (m_rect.width()-1)/m_plotDomain.spanX();
47 const qreal deltaX = m_rect.width()/m_plotDomain.spanX();
39 const qreal deltaY = (m_rect.height()-1)/m_plotDomain.spanY();
48 const qreal deltaY = m_rect.height()/m_plotDomain.spanY();
40
49
41 m_polyline.clear();
50 QPainterPath path;
42 m_polyline.resize(m_series->count());
43
51
44 for (int j = 0; j < m_series->count(); ++j) {
52 for (int j = 0; j < m_series->count(); ++j) {
45 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
53 qreal dx = m_series->x(j) - m_plotDomain.m_minX;
46 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
54 qreal dy = m_series->y(j) - m_plotDomain.m_minY;
47 qreal x = (dx * deltaX) + m_rect.left();
55 qreal x = (dx * deltaX) + m_rect.left();
48 qreal y = - (dy * deltaY) + m_rect.bottom();
56 qreal y = - (dy * deltaY) + m_rect.bottom();
49 m_polyline[j] = QPointF(x, y);
57 if(j==0) path.moveTo(x,y);
58 else path.lineTo(x,y);
50 }
59 }
51
60
61 m_pathItem->setPath(path);
62 m_pathItem->setPen(m_series->pen());
63 m_pathItem->setBrush(m_series->brush());
52 }
64 }
53
65
54
66
55 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
56 {
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));
63 painter->setPen(m_series->color());
64 painter->drawPolyline(m_polyline);
65 }
66
67 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,38 +1,39
1 #ifndef XYLINECHARTITEM_H
1 #ifndef XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
2 #define XYLINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QXYChartSeries;
9 class QXYChartSeries;
10
10
11 class XYLineChartItem : public ChartItem
11 class XYLineChartItem : public ChartItem
12 {
12 {
13
13
14 public:
14 public:
15 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
15 XYLineChartItem(QXYChartSeries* m_series,QGraphicsItem *parent = 0);
16 ~ XYLineChartItem(){};
16 ~ XYLineChartItem(){};
17
17
18 //from QGraphicsItem
18 //from QGraphicsItem
19 QRectF boundingRect() const;
19 QRectF boundingRect() const;
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21 // virtual QPainterPath shape() const;
21 //from ChartItem
22 //from ChartItem
22 void setSize(const QSize& size);
23 void setSize(const QSize& size);
23 void setPlotDomain(const PlotDomain& data);
24 void setPlotDomain(const PlotDomain& data);
24
25
25 private:
26 private:
26 void updateGeometry();
27 void updateGeometry();
27
28
28 private:
29 private:
29 QRect m_rect;
30 QRect m_rect;
30 QPolygonF m_polyline;
31 QPolygonF m_polyline;
31 QXYChartSeries* m_series;
32 QXYChartSeries* m_series;
32 PlotDomain m_plotDomain;
33 PlotDomain m_plotDomain;
33 bool m_dirty;
34 QGraphicsPathItem *m_pathItem;
34 };
35 };
35
36
36 QTCOMMERCIALCHART_END_NAMESPACE
37 QTCOMMERCIALCHART_END_NAMESPACE
37
38
38 #endif
39 #endif
General Comments 0
You need to be logged in to leave comments. Login now