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