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