##// END OF EJS Templates
Marge spline
Marek Rosa -
r418:4e82d53075c4 merge
parent child
Show More
@@ -0,0 +1,11
1 #include <QtGui/QApplication>
2 #include "splinewidget.h"
3
4 int main(int argc, char *argv[])
5 {
6 QApplication a(argc, argv);
7 SplineWidget w;
8 w.show();
9
10 return a.exec();
11 }
@@ -0,0 +1,20
1 #-------------------------------------------------
2 #
3 # Project created by QtCreator 2012-02-29T12:37:46
4 #
5 #-------------------------------------------------
6
7 !include( ../example.pri ) {
8 error( "Couldn't find the example.pri file!" )
9 }
10
11 QT += core gui
12
13 TARGET = splinechart
14 TEMPLATE = app
15
16
17 SOURCES += main.cpp\
18 splinewidget.cpp
19
20 HEADERS += splinewidget.h
@@ -0,0 +1,53
1 #include "splinewidget.h"
2 #include "qchartview.h"
3 #include "qsplineseries.h"
4 #include "qlinechartseries.h"
5 #include <QGridLayout>
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 SplineWidget::SplineWidget(QWidget *parent)
10 : QWidget(parent)
11 {
12 //create QSplineSeries
13 QSplineSeries* series = new QSplineSeries(this);
14 series->addData(QPointF(150, 100));
15 series->addData(QPointF(200, 180));
16 series->addData(QPointF(240, 130));
17 series->addData(QPointF(270, 120));
18 series->addData(QPointF(310, 120));
19 series->addData(QPointF(420, 160));
20 series->addData(QPointF(535, 250));
21
22 series->calculateControlPoints();
23
24 QLineChartSeries* lineSeries = new QLineChartSeries;
25 for (int i = 0; i < series->count() - 1; i++)
26 {
27 lineSeries->add(series->at(i).x(), series->at(i).y());
28 lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y());
29 lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y());
30 }
31
32 // QLineChartSeries* lineSeries2 = new QLineChartSeries;
33 // lineSeries2->add(10, 50);
34 // lineSeries2->add(30, 15);
35 // lineSeries2->add(60, 40);
36 // lineSeries2->add(90, 70);
37 // lineSeries2->add(100, 20);
38
39 //create chart view
40 QChartView* chart = new QChartView;
41 chart->setMinimumSize(800,600);
42 // chart->setGeometry(50, 50, 400, 300);
43 chart->addSeries(series);
44
45 QGridLayout* mainLayout = new QGridLayout;
46 mainLayout->addWidget(chart);
47 setLayout(mainLayout);
48 }
49
50 SplineWidget::~SplineWidget()
51 {
52
53 }
@@ -0,0 +1,15
1 #ifndef SPLINEWIDGET_H
2 #define SPLINEWIDGET_H
3
4 #include <QtGui/QWidget>
5
6 class SplineWidget : public QWidget
7 {
8 Q_OBJECT
9
10 public:
11 SplineWidget(QWidget *parent = 0);
12 ~SplineWidget();
13 };
14
15 #endif // SPLINEWIDGET_H
@@ -0,0 +1,110
1 #include "qsplineseries.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QSplineSeries::QSplineSeries(QObject *parent) :
6 QChartSeries(parent)
7 {
8 }
9
10 QSplineSeries& QSplineSeries::operator << (const QPointF &value)
11 {
12 // d->m_data.append(value);
13 m_data.append(value);
14 // emit changed();
15 return *this;
16 }
17
18 void QSplineSeries::addData(QPointF value)
19 {
20 m_data.append(value);
21 }
22
23 void QSplineSeries::calculateControlPoints()
24 {
25
26 // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit
27 // CPOL Licence
28
29 int n = m_data.size() - 1;
30 if (n == 1)
31 { // Special case: Bezier curve should be a straight line.
32 // firstControlPoints = new Point[1];
33 // 3P1 = 2P0 + P3
34 m_controlPoints.append(QPointF((2 * m_data[0].x() + m_data[1].x()) / 3, (2 * m_data[0].y() + m_data[1].y()) / 3));
35
36 // P2 = 2P1 P0
37 m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - m_data[0].x(), 2 * m_controlPoints[0].y() - m_data[0].y()));
38 return;
39 }
40
41 // Calculate first Bezier control points
42 // Right hand side vector
43 // Set of equations for P0 to Pn points.
44 //
45 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
46 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
47 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
48 // | . . . . . . . . . . . . | | ... | | ... |
49 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
50 // | . . . . . . . . . . . . | | ... | | ... |
51 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
52 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
53 //
54 QList<qreal> rhs;
55 rhs.append(m_data[0].x() + 2 * m_data[1].x());
56
57 // Set right hand side X values
58 for (int i = 1; i < m_data.size() - 1; ++i)
59 rhs.append(4 * m_data[i].x() + 2 * m_data[i + 1].x());
60
61 rhs.append((8 * m_data[n - 1].x() + m_data[n].x()) / 2.0);
62 // Get first control points X-values
63 QList<qreal> x = getFirstControlPoints(rhs);
64 rhs[0] = m_data[0].y() + 2 * m_data[1].y();
65
66 // Set right hand side Y values
67 for (int i = 1; i < m_data.size() - 1; ++i)
68 rhs[i] = 4 * m_data[i].y() + 2 * m_data[i + 1].y();
69
70 rhs[n - 1] = (8 * m_data[n - 1].y() + m_data[n].y()) / 2.0;
71 // Get first control points Y-values
72 QList<qreal> y = getFirstControlPoints(rhs);
73
74 // Fill output arrays.
75 // firstControlPoints = new Point[n];
76 // secondControlPoints = new Point[n];
77 for (int i = 0; i < m_data.size(); ++i)
78 {
79 // First control point
80 m_controlPoints.append(QPointF(x[i], y[i]));
81 // Second control point
82 if (i < n - 1)
83 m_controlPoints.append(QPointF(2 * m_data[i + 1].x() - x[i + 1], 2 * m_data[i + 1].y() - y[i + 1]));
84 else
85 m_controlPoints.append(QPointF((m_data[n].x() + x[n - 1]) / 2, (m_data[n].y() + y[n - 1]) / 2));
86 }
87 }
88
89 QList<qreal> QSplineSeries::getFirstControlPoints(QList<qreal> rhs)
90 {
91 QList<qreal> x; // Solution vector.
92 QList<qreal> tmp; // Temp workspace.
93
94 qreal b = 2.0;
95 x.append(rhs[0] / b);
96 tmp.append(0);
97 for (int i = 1; i < rhs.size(); i++) // Decomposition and forward substitution.
98 {
99 tmp.append(1 / b);
100 b = (i < rhs.size() - 1 ? 4.0 : 3.5) - tmp[i];
101 x.append((rhs[i] - x[i - 1]) / b);
102 }
103 for (int i = 1; i < rhs.size(); i++)
104 x[rhs.size() - i - 1] -= tmp[rhs.size() - i] * x[rhs.size() - i]; // Backsubstitution.
105
106 return x;
107 }
108 #include "moc_qsplineseries.cpp"
109
110 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,41
1 #ifndef QSPLINESERIES_H
2 #define QSPLINESERIES_H
3
4 #include "qchartglobal.h"
5 #include <QtGlobal>
6 #include "qchartseries.h"
7 #include <QList>
8 #include <QPointF>
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
12 class QSplineSeries : public QChartSeries
13 {
14 Q_OBJECT
15 public:
16
17 QSplineSeries(QObject *parent = 0);
18 QChartSeriesType type() const { return QChartSeries::SeriesTypeSpline; }
19 void addData(QPointF value);
20 QSplineSeries& operator << (const QPointF &value);
21 void calculateControlPoints();
22 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
23
24 int count() const { return m_data.size(); }
25
26 QPointF at(int index) const { return m_data[index]; }
27 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
28
29 signals:
30
31 public slots:
32
33 private:
34 QList<QPointF> m_data;
35 QList<QPointF> m_controlPoints;
36
37 };
38
39 QTCOMMERCIALCHART_END_NAMESPACE
40
41 #endif // QSPLINESERIES_H
@@ -0,0 +1,12
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
3
4 SOURCES += \
5 $$PWD/qsplineseries.cpp \
6 $$PWD/splinepresenter.cpp
7
8 PRIVATE_HEADERS += \
9 $$PWD/splinepresenter_p.h
10
11 PUBLIC_HEADERS += \
12 $$PWD/qsplineseries.h
@@ -0,0 +1,72
1 #include "splinepresenter_p.h"
2 #include <QPainter>
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) :
7 LineChartItem(this, parent),m_series(series)//,m_boundingRect()
8 {
9 // if (parent)
10 // m_boundingRect = parent->boundingRect();
11 // else
12 // m_boundingRect = QRectF(10,50, 250, 250);
13 }
14
15 //void SplinePresenter::handleGeometryChanged(const QRectF&)
16 //{
17 // update();
18 //}
19
20 //void SplinePresenter::handleDomainChanged(const Domain& domain)
21 //{
22 // //
23 //}
24
25 void SplinePresenter::updateGeometry()
26 {
27
28 if(m_data.size()==0) return;
29
30 prepareGeometryChange();
31 QPainterPath path;
32 const QPointF& point = m_data.at(0);
33 path.moveTo(point);
34
35 foreach( const QPointF& point , m_data) {
36 path.lineTo(point);
37 }
38
39 m_path = path;
40 m_rect = path.boundingRect();
41 }
42
43 //void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
44 //{
45 // Q_UNUSED(widget);
46 // Q_UNUSED(option);
47 // painter->save();
48
49 // painter->setPen(Qt::SolidLine);
50
51 // QPainterPath splinePath;
52 // splinePath.moveTo(m_series->at(0));
53 // for (int i = 0; i < m_series->count() - 1; i++)
54 // {
55 // painter->setPen(Qt::red);
56 // splinePath.cubicTo(m_series->controlPoint(2 * i), m_series->controlPoint(2 * i + 1), m_series->at(i + 1));
57 // painter->drawEllipse(m_series->at(i), 4, 4);
58
59 // painter->setPen(Qt::blue);
60 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
61 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
62 // painter->drawEllipse(m_series->controlPoint(2 * i), 4, 4);
63 // painter->drawEllipse(m_series->controlPoint(2 * i + 1), 4, 4);
64 // }
65 // painter->setPen(Qt::red);
66 // painter->drawPath(splinePath);
67 // painter->restore();
68 //}
69
70 #include "moc_splinepresenter_p.cpp"
71
72 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,36
1 #ifndef SPLINEPRESENTER_P_H
2 #define SPLINEPRESENTER_P_H
3
4 #include "chartitem_p.h"
5 #include <QObject>
6 #include "qsplineseries.h"
7 #include "linechartitem_p.h"
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 class SplinePresenter : public LineChartItem
12 {
13 Q_OBJECT
14 public:
15 SplinePresenter(QSplineSeries* series, QGraphicsObject *parent = 0);
16
17 void updateGeometry();
18
19 // QRectF boundingRect() const { return m_boundingRect; }
20 // void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21
22 signals:
23
24 public slots:
25 // void handleDomainChanged(const Domain& domain);
26 // void handleGeometryChanged(const QRectF& rect);
27
28 private:
29 QSplineSeries* m_series;
30 // QRectF m_boundingRect;
31
32 };
33
34 QTCOMMERCIALCHART_END_NAMESPACE
35
36 #endif // SPLINEPRESENTER_P_H
@@ -14,4 +14,5 SUBDIRS += linechart \
14 gdpbarchart \
14 gdpbarchart \
15 presenterchart \
15 presenterchart \
16 chartview \
16 chartview \
17 scatterinteractions
17 scatterinteractions \
18 splinechart
@@ -7,14 +7,15
7 #include "qpercentbarseries.h"
7 #include "qpercentbarseries.h"
8 #include "qpieseries.h"
8 #include "qpieseries.h"
9 #include "qscatterseries.h"
9 #include "qscatterseries.h"
10 #include "qsplineseries.h"
10
11
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 m_axisX(new QChartAxis(this)),
15 m_axisX(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
16 m_axisY(new QChartAxis(this)),
16 m_domainIndex(0),
17 m_domainIndex(0),
17 m_axisXInitialized(false)
18 m_axisXInitialized(false)
18 {
19 {
19 }
20 }
20
21
@@ -25,11 +26,11 ChartDataSet::~ChartDataSet()
25
26
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 {
28 {
28 int i = m_domainMap.count(axisY);
29 int i = m_domainMap.count(axisY);
29 if(i == 0){
30 if(i == 0){
30 return Domain();
31 return Domain();
31 }
32 }
32 i = i - m_domainIndex -1;
33 i = i - m_domainIndex -1;
33 return m_domainMap.values(axisY).at(i);
34 return m_domainMap.values(axisY).at(i);
34 }
35 }
35
36
@@ -55,79 +56,94 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
55
56
56 switch(series->type())
57 switch(series->type())
57 {
58 {
58 case QSeries::SeriesTypeLine: {
59 case QSeries::SeriesTypeLine: {
59
60
60 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
61 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
61
62
62 for (int i = 0; i < xyseries->count(); i++)
63 for (int i = 0; i < xyseries->count(); i++)
63 {
64 {
64 qreal x = xyseries->x(i);
65 qreal x = xyseries->x(i);
65 qreal y = xyseries->y(i);
66 qreal y = xyseries->y(i);
66 domain.m_minX = qMin(domain.m_minX,x);
67 domain.m_minY = qMin(domain.m_minY,y);
68 domain.m_maxX = qMax(domain.m_maxX,x);
69 domain.m_maxY = qMax(domain.m_maxY,y);
70 }
71 break;
72 }
73 case QSeries::SeriesTypeBar: {
74 qDebug() << "QChartSeries::SeriesTypeBar";
75 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
76 qreal x = barSeries->categoryCount();
77 qreal y = barSeries->max();
78 domain.m_minX = qMin(domain.m_minX,x);
67 domain.m_minX = qMin(domain.m_minX,x);
79 domain.m_minY = qMin(domain.m_minY,y);
68 domain.m_minY = qMin(domain.m_minY,y);
80 domain.m_maxX = qMax(domain.m_maxX,x);
69 domain.m_maxX = qMax(domain.m_maxX,x);
81 domain.m_maxY = qMax(domain.m_maxY,y);
70 domain.m_maxY = qMax(domain.m_maxY,y);
82 break;
83 }
71 }
84 case QSeries::SeriesTypeStackedBar: {
72 break;
73 }
74 case QSeries::SeriesTypeBar: {
75 qDebug() << "QChartSeries::SeriesTypeBar";
76 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
77 qreal x = barSeries->categoryCount();
78 qreal y = barSeries->max();
79 domain.m_minX = qMin(domain.m_minX,x);
80 domain.m_minY = qMin(domain.m_minY,y);
81 domain.m_maxX = qMax(domain.m_maxX,x);
82 domain.m_maxY = qMax(domain.m_maxY,y);
83 break;
84 }
85 case QSeries::SeriesTypeStackedBar: {
85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86
87
87 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
88 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
88 qreal x = stackedBarSeries->categoryCount();
89 qreal x = stackedBarSeries->categoryCount();
89 qreal y = stackedBarSeries->maxCategorySum();
90 qreal y = stackedBarSeries->maxCategorySum();
90 domain.m_minX = qMin(domain.m_minX,x);
91 domain.m_minX = qMin(domain.m_minX,x);
91 domain.m_minY = qMin(domain.m_minY,y);
92 domain.m_minY = qMin(domain.m_minY,y);
92 domain.m_maxX = qMax(domain.m_maxX,x);
93 domain.m_maxX = qMax(domain.m_maxX,x);
93 domain.m_maxY = qMax(domain.m_maxY,y);
94 domain.m_maxY = qMax(domain.m_maxY,y);
94 break;
95 break;
95 }
96 }
96 case QSeries::SeriesTypePercentBar: {
97 case QSeries::SeriesTypePercentBar: {
97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98 qDebug() << "QChartSeries::SeriesTypePercentBar";
98
99
99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
100 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
100 qreal x = percentBarSeries->categoryCount();
101 qreal x = percentBarSeries->categoryCount();
101 domain.m_minX = qMin(domain.m_minX,x);
102 domain.m_minX = qMin(domain.m_minX,x);
102 domain.m_minY = 0;
103 domain.m_minY = 0;
103 domain.m_maxX = qMax(domain.m_maxX,x);
104 domain.m_maxX = qMax(domain.m_maxX,x);
104 domain.m_maxY = 100;
105 domain.m_maxY = 100;
105 break;
106 break;
106 }
107 }
107
108
108 case QSeries::SeriesTypePie: {
109 case QSeries::SeriesTypePie: {
109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 // TODO: domain stuff
111 // TODO: domain stuff
111 break;
112 break;
112 }
113 }
113
114
114 case QSeries::SeriesTypeScatter: {
115 case QSeries::SeriesTypeScatter: {
115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 Q_ASSERT(scatterSeries);
117 Q_ASSERT(scatterSeries);
117 foreach (QPointF point, scatterSeries->data()) {
118 foreach (QPointF point, scatterSeries->data()) {
118 domain.m_minX = qMin(domain.m_minX, point.x());
119 domain.m_minX = qMin(domain.m_minX, point.x());
119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 domain.m_minY = qMin(domain.m_minY, point.y());
121 domain.m_minY = qMin(domain.m_minY, point.y());
121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 }
123 break;
124 }
123 }
124 break;
125 }
125
126
126 default: {
127 case QChartSeries::SeriesTypeSpline: {
127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
128 return;
129
129 break;
130 for (int i = 0; i < splineSeries->count(); i++)
131 {
132 qreal x = splineSeries->at(i).x();
133 qreal y = splineSeries->at(i).y();
134 domain.m_minX = qMin(domain.m_minX,x);
135 domain.m_minY = qMin(domain.m_minY,y);
136 domain.m_maxX = qMax(domain.m_maxX,x);
137 domain.m_maxY = qMax(domain.m_maxY,y);
130 }
138 }
139 break;
140 }
141
142 default: {
143 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
144 return;
145 break;
146 }
131
147
132 }
148 }
133
149
@@ -165,7 +181,7 void ChartDataSet::removeSeries(QSeries* series)
165 emit axisRemoved(axis);
181 emit axisRemoved(axis);
166 m_domainMap.remove(axis);
182 m_domainMap.remove(axis);
167 if(axis != m_axisY)
183 if(axis != m_axisY)
168 delete axis;
184 delete axis;
169 }
185 }
170 series->setParent(0);
186 series->setParent(0);
171 break;
187 break;
@@ -187,7 +203,7 void ChartDataSet::removeAllSeries()
187 m_domainMap.remove(axis);
203 m_domainMap.remove(axis);
188 emit axisRemoved(axis);
204 emit axisRemoved(axis);
189 if(axis != m_axisY) delete axis;
205 if(axis != m_axisY) delete axis;
190 }
206 }
191 m_domainIndex=0;
207 m_domainIndex=0;
192 }
208 }
193
209
@@ -250,13 +266,13 void ChartDataSet::setDomain(int index,QChartAxis* axis)
250 Q_ASSERT(i>=0);
266 Q_ASSERT(i>=0);
251 Domain domain = m_domainMap.values(axis).at(i);
267 Domain domain = m_domainMap.values(axis).at(i);
252 {
268 {
253 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
269 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
254 QList<QSeries*> seriesList = m_seriesMap.values(axis);
270 QList<QSeries*> seriesList = m_seriesMap.values(axis);
255 foreach(QSeries* series, seriesList) {
271 foreach(QSeries* series, seriesList) {
256 emit seriesDomainChanged(series,domain);
272 emit seriesDomainChanged(series,domain);
257 }
273 }
258 axis->updateRange(domain.m_minY,domain.m_maxY);
274 axis->updateRange(domain.m_minY,domain.m_maxY);
259 emit axisRangeChanged(axis,labels);
275 emit axisRangeChanged(axis,labels);
260 }
276 }
261
277
262 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
278 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
@@ -378,7 +394,7 void ChartDataSet::handleRangeChanged(QChartAxis* axis)
378 }
394 }
379
395
380 for(int j=domains.size()-1; j>=0;j--)
396 for(int j=domains.size()-1; j>=0;j--)
381 m_domainMap.insert(axis,domains.at(j));
397 m_domainMap.insert(axis,domains.at(j));
382
398
383 setDomain(m_domainIndex,axis);
399 setDomain(m_domainIndex,axis);
384 }
400 }
@@ -10,6 +10,7
10 #include "qlineseries.h"
10 #include "qlineseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 #include "qsplineseries.h"
13 //items
14 //items
14 #include "axisitem_p.h"
15 #include "axisitem_p.h"
15 #include "axisanimationitem_p.h"
16 #include "axisanimationitem_p.h"
@@ -20,18 +21,19
20 #include "linechartanimationitem_p.h"
21 #include "linechartanimationitem_p.h"
21 #include "piepresenter_p.h"
22 #include "piepresenter_p.h"
22 #include "scatterpresenter_p.h"
23 #include "scatterpresenter_p.h"
24 #include "splinepresenter_p.h"
23
25
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
27
26 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
28 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
27 m_chart(chart),
29 m_chart(chart),
28 m_dataset(dataset),
30 m_dataset(dataset),
29 m_chartTheme(0),
31 m_chartTheme(0),
30 m_marginSize(0),
32 m_marginSize(0),
31 m_rect(QRectF(QPoint(0,0),m_chart->size())),
33 m_rect(QRectF(QPoint(0,0),m_chart->size())),
32 m_options(0)
34 m_options(0)
33 {
35 {
34 createConnections();
36 createConnections();
35 setChartTheme(QChart::ChartThemeDefault);
37 setChartTheme(QChart::ChartThemeDefault);
36
38
37 }
39 }
@@ -60,7 +62,7 QRectF ChartPresenter::geometry() const
60 void ChartPresenter::handleGeometryChanged()
62 void ChartPresenter::handleGeometryChanged()
61 {
63 {
62 m_rect = QRectF(QPoint(0,0),m_chart->size());
64 m_rect = QRectF(QPoint(0,0),m_chart->size());
63 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
65 // m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
64 Q_ASSERT(m_rect.isValid());
66 Q_ASSERT(m_rect.isValid());
65 emit geometryChanged(m_rect);
67 emit geometryChanged(m_rect);
66 }
68 }
@@ -108,108 +110,117 void ChartPresenter::handleSeriesAdded(QSeries* series)
108 {
110 {
109 switch(series->type())
111 switch(series->type())
110 {
112 {
111 case QSeries::SeriesTypeLine: {
113 case QSeries::SeriesTypeLine: {
112 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
114 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
113 LineChartItem* item;
115 LineChartItem* item;
114 if(m_options.testFlag(QChart::SeriesAnimations)){
116 if(m_options.testFlag(QChart::SeriesAnimations)){
115 item = new LineChartAnimationItem(this,lineSeries,m_chart);
117 item = new LineChartAnimationItem(this,lineSeries,m_chart);
116 }else{
118 }else{
117 item = new LineChartItem(this,lineSeries,m_chart);
119 item = new LineChartItem(this,lineSeries,m_chart);
118 }
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int)));
122 QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int)));
123 QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int)));
124 QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated()));
125 m_chartItems.insert(series,item);
126 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
127 item->handleUpdated();
128 break;
129 }
120 }
121 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
123 QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int)));
124 QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int)));
125 QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int)));
126 QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated()));
127 m_chartItems.insert(series,item);
128 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
129 item->handleUpdated();
130 break;
131 }
130
132
131 case QSeries::SeriesTypeBar: {
133 case QSeries::SeriesTypeBar: {
132 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
134 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
133 BarPresenter* item = new BarPresenter(barSeries,m_chart);
135 BarPresenter* item = new BarPresenter(barSeries,m_chart);
134 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
136 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
135 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
137 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
136 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
138 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
137 m_chartItems.insert(series,item);
139 m_chartItems.insert(series,item);
138 // m_axisXItem->setVisible(false);
140 // m_axisXItem->setVisible(false);
139 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
141 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
140 break;
142 break;
141 }
143 }
142
144
143 case QSeries::SeriesTypeStackedBar: {
145 case QSeries::SeriesTypeStackedBar: {
144
146
145 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
147 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
146 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
148 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
147 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
149 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
148 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
150 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
149 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
151 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
150 m_chartItems.insert(series,item);
152 m_chartItems.insert(series,item);
151 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
153 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
152 break;
154 break;
153 }
155 }
154
156
155 case QSeries::SeriesTypePercentBar: {
157 case QSeries::SeriesTypePercentBar: {
156
158
157 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
159 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
158 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
160 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
159 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
161 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
160 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
162 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
161 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
163 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
162 m_chartItems.insert(series,item);
164 m_chartItems.insert(series,item);
163 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
165 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
164 break;
166 break;
165 }
167 }
166 case QSeries::SeriesTypeScatter: {
168 case QSeries::SeriesTypeScatter: {
167 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
169 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
168 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
170 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
169 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
171 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
170 scatterSeries, SIGNAL(clicked(QPointF)));
172 scatterSeries, SIGNAL(clicked(QPointF)));
171 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
173 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
172 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
174 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
173 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
175 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
174 m_chartItems.insert(scatterSeries, scatterPresenter);
176 m_chartItems.insert(scatterSeries, scatterPresenter);
175 if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect);
177 if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect);
176 break;
178 break;
177 }
179 }
178 case QSeries::SeriesTypePie: {
180 case QSeries::SeriesTypePie: {
179 QPieSeries *s = qobject_cast<QPieSeries *>(series);
181 QPieSeries *s = qobject_cast<QPieSeries *>(series);
180 PiePresenter* pie = new PiePresenter(m_chart, s);
182 PiePresenter* pie = new PiePresenter(m_chart, s);
181 m_chartTheme->decorate(pie, s, m_chartItems.count());
183 m_chartTheme->decorate(pie, s, m_chartItems.count());
182 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
184 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
183
185
184 // Hide all from background when there is only piechart
186 // Hide all from background when there is only piechart
185 // TODO: refactor this ugly code... should be one setting for this
187 // TODO: refactor this ugly code... should be one setting for this
186 if (m_chartItems.count() == 0) {
188 if (m_chartItems.count() == 0) {
187 m_chart->axisX()->setAxisVisible(false);
189 m_chart->axisX()->setAxisVisible(false);
188 m_chart->axisY()->setAxisVisible(false);
190 m_chart->axisY()->setAxisVisible(false);
189 m_chart->axisX()->setGridVisible(false);
191 m_chart->axisX()->setGridVisible(false);
190 m_chart->axisY()->setGridVisible(false);
192 m_chart->axisY()->setGridVisible(false);
191 m_chart->axisX()->setLabelsVisible(false);
193 m_chart->axisX()->setLabelsVisible(false);
192 m_chart->axisY()->setLabelsVisible(false);
194 m_chart->axisY()->setLabelsVisible(false);
193 m_chart->axisX()->setShadesVisible(false);
195 m_chart->axisX()->setShadesVisible(false);
194 m_chart->axisY()->setShadesVisible(false);
196 m_chart->axisY()->setShadesVisible(false);
195 m_chart->setChartBackgroundBrush(Qt::transparent);
197 m_chart->setChartBackgroundBrush(Qt::transparent);
196 }
197
198 m_chartItems.insert(series, pie);
199 pie->handleGeometryChanged(m_rect);
200 break;
201 }
202 default: {
203 qDebug()<< "Series type" << series->type() << "not implemented.";
204 break;
205 }
198 }
199
200 m_chartItems.insert(series, pie);
201 pie->handleGeometryChanged(m_rect);
202 break;
203 }
204 case QChartSeries::SeriesTypeSpline: {
205 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series);
206 SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart);
207 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&)));
208 m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count());
209 m_chartItems.insert(splineSeries, splinePresenter);
210 break;
211 }
212 default: {
213 qDebug()<< "Series type" << series->type() << "not implemented.";
214 break;
215 }
216
206 }
217 }
207 }
218 }
208
219
209 void ChartPresenter::handleSeriesRemoved(QSeries* series)
220 void ChartPresenter::handleSeriesRemoved(QSeries* series)
210 {
221 {
211 ChartItem* item = m_chartItems.take(series);
222 ChartItem* item = m_chartItems.take(series);
212 delete item;
223 delete item;
213 }
224 }
214
225
215 void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain)
226 void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain)
@@ -240,8 +251,8 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
240
251
241 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
252 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
242 while (j.hasNext()) {
253 while (j.hasNext()) {
243 j.next();
254 j.next();
244 m_chartTheme->decorate(j.key(),j.value());
255 m_chartTheme->decorate(j.key(),j.value());
245 }
256 }
246 }
257 }
247
258
@@ -12,6 +12,7
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 #include "qpieseries.h"
13 #include "qpieseries.h"
14 #include "qpieslice.h"
14 #include "qpieslice.h"
15 #include "qsplineseries.h"
15
16
16 //items
17 //items
17 #include "axisitem_p.h"
18 #include "axisitem_p.h"
@@ -20,7 +21,8
20 #include "percentbarpresenter_p.h"
21 #include "percentbarpresenter_p.h"
21 #include "linechartitem_p.h"
22 #include "linechartitem_p.h"
22 #include "scatterpresenter_p.h"
23 #include "scatterpresenter_p.h"
23 #include "piepresenter_p.h"
24 #include "piepresenter.h"
25 #include "splinepresenter_p.h"
24
26
25 //themes
27 //themes
26 #include "chartthemevanilla_p.h"
28 #include "chartthemevanilla_p.h"
@@ -267,4 +269,21 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
267 axis->setShadesOpacity(0.5);
269 axis->setShadesOpacity(0.5);
268 }
270 }
269
271
272 void ChartTheme::decorate(SplinePresenter* presenter, QSplineSeries* series, int count)
273 {
274 Q_ASSERT(presenter);
275 Q_ASSERT(series);
276
277 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
278 // TODO: define alpha in the theme? or in the series?
279 //color.setAlpha(120);
280
281 // QBrush brush(color, Qt::SolidPattern);
282 // presenter->m_markerBrush = brush;
283
284 // QPen pen(brush, 3);
285 // pen.setColor(color);
286 // presenter->m_markerPen = pen;
287 }
288
270 QTCOMMERCIALCHART_END_NAMESPACE
289 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,6 +21,8 class QScatterSeries;
21 class ScatterPresenter;
21 class ScatterPresenter;
22 class PiePresenter;
22 class PiePresenter;
23 class QPieSeries;
23 class QPieSeries;
24 class SplinePresenter;
25 class QSplineSeries;
24
26
25 class ChartTheme
27 class ChartTheme
26 {
28 {
@@ -38,6 +40,7 public:
38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
40 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
39 void decorate(PiePresenter* item, QPieSeries* series, int count);
41 void decorate(PiePresenter* item, QPieSeries* series, int count);
40 void decorate(QChartAxis* axis,AxisItem* item);
42 void decorate(QChartAxis* axis,AxisItem* item);
43 void decorate(SplinePresenter* presenter, QSplineSeries* series, int count);
41
44
42 protected:
45 protected:
43 QChart::ChartTheme m_id;
46 QChart::ChartTheme m_id;
@@ -32,6 +32,8 include(linechart/linechart.pri)
32 include(barchart/barchart.pri)
32 include(barchart/barchart.pri)
33 include(piechart/piechart.pri)
33 include(piechart/piechart.pri)
34 include(scatterseries/scatter.pri)
34 include(scatterseries/scatter.pri)
35 include(splinechart/splinechart.pri)
36
35 THEMES += themes/chartthemeicy_p.h \
37 THEMES += themes/chartthemeicy_p.h \
36 themes/chartthemegrayscale_p.h \
38 themes/chartthemegrayscale_p.h \
37 themes/chartthemescientific_p.h \
39 themes/chartthemescientific_p.h \
@@ -86,4 +88,9 unix:QMAKE_DISTCLEAN += -r \
86 $$CHART_BUILD_LIB_DIR
88 $$CHART_BUILD_LIB_DIR
87 win32:QMAKE_DISTCLEAN += /Q \
89 win32:QMAKE_DISTCLEAN += /Q \
88 $$CHART_BUILD_HEADER_DIR \
90 $$CHART_BUILD_HEADER_DIR \
91 <<<<<<< HEAD
89 $$CHART_BUILD_LIB_DIR
92 $$CHART_BUILD_LIB_DIR
93 =======
94 $$CHART_BUILD_LIB_DIR
95
96 >>>>>>> spline
General Comments 0
You need to be logged in to leave comments. Login now