##// END OF EJS Templates
Spline chart example added
Marek Rosa -
r434:52aa7c3abf86
parent child
Show More
@@ -20,6 +20,7
20 <li><a href="example-linechart.html">Line Chart example</a></li>
20 <li><a href="example-linechart.html">Line Chart example</a></li>
21 <li><a href="example-piechart.html">Pie Chart example</a></li>
21 <li><a href="example-piechart.html">Pie Chart example</a></li>
22 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
22 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
23 <li><a href="example-splinechart.html">Spline Chart example</a></li>
23 </ul>
24 </ul>
24 </td>
25 </td>
25 </tr>
26 </tr>
@@ -87,6 +87,7 Widget::Widget(QWidget *parent)
87
87
88 // hide axis X labels
88 // hide axis X labels
89 QChartAxis* axis = chartArea->axisX();
89 QChartAxis* axis = chartArea->axisX();
90 // axis->
90 // axis->setLabelsVisible(false);
91 // axis->setLabelsVisible(false);
91 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
92 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
92
93
@@ -211,7 +212,7 void Widget::refreshChart()
211 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
212 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
212 chartArea->addSeries(series);
213 chartArea->addSeries(series);
213 }
214 }
214 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
215 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
215 }
216 }
216 }
217 }
217
218
@@ -3,80 +3,67
3 #include "qlineseries.h"
3 #include "qlineseries.h"
4 #include <QGridLayout>
4 #include <QGridLayout>
5 #include <QPushButton>
5 #include <QPushButton>
6 #include "qchartaxis.h"
7 #include <qmath.h>
6
8
7 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
8
10
9 SplineWidget::SplineWidget(QWidget *parent)
11 SplineWidget::SplineWidget(QWidget *parent)
10 : QWidget(parent)
12 : QWidget(parent)
11 {
13 {
14 qsrand(time(NULL));
15 //! [1]
12 //create QSplineSeries
16 //create QSplineSeries
13 series = new QSplineSeries(this);
17 series = new QSplineSeries(this);
18 //! [1]
19
20 //! [2]
21 // customize the series presentation settings
22 QPen seriesPen(Qt::blue);
23 seriesPen.setWidth(3);
24 series->setPen(seriesPen);
25 //! [2]
26
27 //! [add points to series]
28 //add data points to the series
14 series->add(QPointF(150, 100));
29 series->add(QPointF(150, 100));
15 series->add(QPointF(200, 130));
30 series->add(QPointF(200, 130));
16 series->add(QPointF(250, 120));
31 series->add(QPointF(250, 120));
17 series->add(QPointF(300, 140));
32 series->add(QPointF(300, 140));
18 series->add(QPointF(350, 100));
33 series->add(QPointF(350, 160));
19 series->add(QPointF(400, 120));
34 //! [add points to series]
20 series->add(QPointF(450, 150));
35 // series->add(QPointF(400, 120));
21 // series->add(QPointF(600, 150));
36 // series->add(QPointF(450, 150));
22 series->add(QPointF(500, 145));
37 // series->add(QPointF(500, 145));
23 series->add(QPointF(550, 170));
38 // series->add(QPointF(550, 170));
24 series->add(QPointF(600, 190));
39 // series->add(QPointF(600, 190));
25 series->add(QPointF(650, 210));
40 // series->add(QPointF(650, 210));
26 series->add(QPointF(700, 190));
41 // series->add(QPointF(700, 190));
27 series->add(QPointF(750, 180));
42 // series->add(QPointF(750, 180));
28 series->add(QPointF(800, 170));
43 // series->add(QPointF(800, 170));
29
30 // series->calculateControlPoints();
31
32 QSplineSeries* series2 = new QSplineSeries(this);
33 qsrand(time(NULL));
34 // for (int i = 0; i < 100; i++)
35 // {
36 // series2->add(QPointF(i*7, qrand()%600));
37 // }
38 int k = 10;
39 for (int i = 0; i < 25; i++)
40 {
41 if (k > 60)
42 {
43 k = 10;
44 }
45 series2->add(QPointF(i*50, k));
46 k +=10;
47 }
48
44
49 // series2->calculateControlPoints();
45 //! [3]
50
46 // create chart view
51 // QLineSeries* lineSeries = new QLineSeries;
47 QChartView* chart = new QChartView;
52 // for (int i = 0; i < series->count() - 1; i++)
48 chart->addSeries(series);
53 // {
54 // lineSeries->add(series->at(i).x(), series->at(i).y());
55 // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y());
56 // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y());
57 // }
58
49
59 // QLineChartSeries* lineSeries2 = new QLineChartSeries;
50 chart->axisX()->setRange(0, 1500);
60 // lineSeries2->add(10, 50);
51 chart->axisY()->setRange(0, 400);
61 // lineSeries2->add(30, 15);
62 // lineSeries2->add(60, 40);
63 // lineSeries2->add(90, 70);
64 // lineSeries2->add(100, 20);
65
52
66 //create chart view
67 QChartView* chart = new QChartView;
68 chart->setMinimumSize(800,600);
53 chart->setMinimumSize(800,600);
69 // chart->setGeometry(50, 50, 400, 300);
54 //! [3]
70 chart->addSeries(series);
71 chart->addSeries(series2);
72
55
73 //add new item
56 //! [4]
57 //add new data point button
74 QPushButton* addButton = new QPushButton("Add new point");
58 QPushButton* addButton = new QPushButton("Add new point");
75 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
59 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
76
60
77 QPushButton* removeButton = new QPushButton("Remove point from the end");
61 // remove the last data point in the series
62 QPushButton* removeButton = new QPushButton("Remove point");
78 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
63 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
64 //! [4]
79
65
66 //! [5]
80 //butttons layout
67 //butttons layout
81 QVBoxLayout* buttonsLayout = new QVBoxLayout;
68 QVBoxLayout* buttonsLayout = new QVBoxLayout;
82 buttonsLayout->addWidget(addButton);
69 buttonsLayout->addWidget(addButton);
@@ -87,21 +74,26 SplineWidget::SplineWidget(QWidget *parent)
87 mainLayout->addWidget(chart, 1, 0);
74 mainLayout->addWidget(chart, 1, 0);
88 mainLayout->addLayout(buttonsLayout, 1, 1);
75 mainLayout->addLayout(buttonsLayout, 1, 1);
89 setLayout(mainLayout);
76 setLayout(mainLayout);
77 //! [5]
90 }
78 }
91
79
80 //! [add point]
92 void SplineWidget::addNewPoint()
81 void SplineWidget::addNewPoint()
93 {
82 {
94 if (series->count() > 0)
83 if (series->count() > 0)
95 series->add(QPointF(series->x(series->count() - 1) + 50, series->y(series->count() - 1) - 50 + qrand()%100));
84 series->add(QPointF(series->x(series->count() - 1) + 20 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100)));
96 else
85 else
97 series->add(QPointF(50, 50 + qrand()%50));
86 series->add(QPointF(50, 50 + qrand()%50));
98 }
87 }
88 //! [add point]
99
89
90 //! [remove point]
100 void SplineWidget::removePoint()
91 void SplineWidget::removePoint()
101 {
92 {
102 if (series->count() > 0)
93 if (series->count() > 0)
103 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
94 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
104 }
95 }
96 //! [remove point]
105
97
106 SplineWidget::~SplineWidget()
98 SplineWidget::~SplineWidget()
107 {
99 {
@@ -304,6 +304,15 void ChartTheme::decorate(SplinePresenter* presenter, QSplineSeries* series, int
304 Q_ASSERT(presenter);
304 Q_ASSERT(presenter);
305 Q_ASSERT(series);
305 Q_ASSERT(series);
306
306
307 QPen pen;
308 if(pen != series->pen()){
309 presenter->setPen(series->pen());
310 return;
311 }
312 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
313 pen.setWidthF(series->pen().widthF());
314 presenter->setPen(pen);
315
307 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
316 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
308 // TODO: define alpha in the theme? or in the series?
317 // TODO: define alpha in the theme? or in the series?
309 //color.setAlpha(120);
318 //color.setAlpha(120);
@@ -124,9 +124,9 void QLineSeries::replace(const QPointF& point)
124 void QLineSeries::remove(qreal x)
124 void QLineSeries::remove(qreal x)
125 {
125 {
126 int index = m_x.indexOf(x);
126 int index = m_x.indexOf(x);
127 m_x.remove(index);
128 m_y.remove(index);
129 emit pointRemoved(index);
127 emit pointRemoved(index);
128 m_x.remove(index);
129 m_y.remove(index);
130 }
130 }
131
131
132 /*!
132 /*!
@@ -20,6 +20,11 class QSplineSeries : public QLineSeries
20 // int count() const { return m_x.size(); }
20 // int count() const { return m_x.size(); }
21 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
21 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
22
22
23 // TODO: allow the user to set custom control points
24 // void setCustomControlPoints(QList<QPointsF> controlPoints);
25 // bool calculateControlPointsAutomatically();
26 // void setCalculateControlPointsAutomatically();
27
23 private:
28 private:
24 void calculateControlPoints();
29 void calculateControlPoints();
25 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
30 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
@@ -29,8 +29,8 void SplinePresenter::applyGeometry(QVector<QPointF>& points)
29 const QPointF& point = points.at(0);
29 const QPointF& point = points.at(0);
30 splinePath.moveTo(point);
30 splinePath.moveTo(point);
31
31
32 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
32 // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
33 for (int i = 0; i < splineSeries->count() - 1; i++)
33 for (int i = 0; i < points.size() - 1; i++)
34 {
34 {
35 const QPointF& point = points.at(i + 1);
35 const QPointF& point = points.at(i + 1);
36 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
36 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
@@ -56,7 +56,7 void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
56 for (int i = 0; i < splineSeries->count() - 1; i++)
56 for (int i = 0; i < splineSeries->count() - 1; i++)
57 {
57 {
58 painter->setPen(Qt::red);
58 painter->setPen(Qt::red);
59 painter->drawEllipse(m_points[i], 4, 4);
59 painter->drawEllipse(m_points[i], 2, 2);
60
60
61 painter->setPen(Qt::blue);
61 painter->setPen(Qt::blue);
62 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
62 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
@@ -67,7 +67,7 void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
67 if (m_points.count() > 0)
67 if (m_points.count() > 0)
68 {
68 {
69 painter->setPen(Qt::red);
69 painter->setPen(Qt::red);
70 painter->drawEllipse(m_points[m_points.count() - 1], 4, 4);
70 painter->drawEllipse(m_points[m_points.count() - 1], 2, 2);
71 }
71 }
72 painter->restore();
72 painter->restore();
73 }
73 }
General Comments 0
You need to be logged in to leave comments. Login now