@@ -1,21 +1,21 | |||
|
1 | 1 | TEMPLATE = subdirs |
|
2 | 2 | SUBDIRS += linechart \ |
|
3 | 3 | zoomlinechart \ |
|
4 | 4 | colorlinechart \ |
|
5 | 5 | barchart \ |
|
6 | 6 | stackedbarchart \ |
|
7 | 7 | percentbarchart \ |
|
8 | 8 | scatter \ |
|
9 | 9 | piechart \ |
|
10 | 10 | piechartcustomization \ |
|
11 | 11 | piechartdrilldown \ |
|
12 | 12 | dynamiclinechart \ |
|
13 | 13 | axischart \ |
|
14 | 14 | multichart \ |
|
15 | 15 | gdpbarchart \ |
|
16 | 16 | presenterchart \ |
|
17 | 17 | chartview \ |
|
18 | 18 | scatterinteractions \ |
|
19 | splinechart \ | |
|
19 | #splinechart \ | |
|
20 | 20 | areachart \ |
|
21 | 21 | stackedbarchartdrilldown |
@@ -1,108 +1,108 | |||
|
1 | 1 | #include "splinewidget.h" |
|
2 | 2 | #include "qchartview.h" |
|
3 | 3 | #include "qlineseries.h" |
|
4 | 4 | #include <QGridLayout> |
|
5 | 5 | #include <QPushButton> |
|
6 | 6 | #include "qchartaxis.h" |
|
7 | 7 | #include <qmath.h> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | SplineWidget::SplineWidget(QWidget *parent) |
|
12 | 12 | : QWidget(parent) |
|
13 | 13 | { |
|
14 | qsrand(time(NULL)); | |
|
14 | // qsrand(time(NULL)); | |
|
15 | 15 | //! [1] |
|
16 | 16 | //create QSplineSeries |
|
17 | 17 | series = new QSplineSeries; |
|
18 | 18 | //! [1] |
|
19 | 19 | |
|
20 | 20 | //! [2] |
|
21 | 21 | // customize the series presentation settings |
|
22 | 22 | QPen seriesPen(Qt::blue); |
|
23 | 23 | seriesPen.setWidth(3); |
|
24 | 24 | series->setPen(seriesPen); |
|
25 | 25 | //! [2] |
|
26 | 26 | |
|
27 | 27 | //! [add points to series] |
|
28 | 28 | //add data points to the series |
|
29 | 29 | series->add(QPointF(150, 100)); |
|
30 | 30 | series->add(QPointF(200, 130)); |
|
31 | 31 | series->add(QPointF(250, 120)); |
|
32 | 32 | series->add(QPointF(300, 140)); |
|
33 | 33 | series->add(QPointF(350, 160)); |
|
34 | 34 | //! [add points to series] |
|
35 | 35 | // series->add(QPointF(400, 120)); |
|
36 | 36 | // series->add(QPointF(450, 150)); |
|
37 | 37 | // series->add(QPointF(500, 145)); |
|
38 | 38 | // series->add(QPointF(550, 170)); |
|
39 | 39 | // series->add(QPointF(600, 190)); |
|
40 | 40 | // series->add(QPointF(650, 210)); |
|
41 | 41 | // series->add(QPointF(700, 190)); |
|
42 | 42 | // series->add(QPointF(750, 180)); |
|
43 | 43 | // series->add(QPointF(800, 170)); |
|
44 | 44 | |
|
45 | 45 | //! [3] |
|
46 | 46 | // create chart view |
|
47 | 47 | QChartView* chart = new QChartView; |
|
48 | 48 | chart->addSeries(series); |
|
49 | 49 | |
|
50 | 50 | chart->setChartTitle("Spline chart example"); |
|
51 | 51 | |
|
52 | 52 | // chart->setMinimumSize(800,600); |
|
53 | 53 | // chart->axisX()->setRange(0, 1500); |
|
54 | 54 | |
|
55 | 55 | // chart->axisX()->setMax(1500); |
|
56 | 56 | // chart->axisY()->setRange(0, 400); |
|
57 | 57 | chart->axisX()->setMax(1500); |
|
58 | 58 | // chart-> |
|
59 | 59 | |
|
60 | 60 | chart->setMinimumSize(800,600); |
|
61 | 61 | //! [3] |
|
62 | 62 | |
|
63 | 63 | //! [4] |
|
64 | 64 | //add new data point button |
|
65 | 65 | QPushButton* addButton = new QPushButton("Add new point"); |
|
66 | 66 | connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); |
|
67 | 67 | |
|
68 | 68 | // remove the last data point in the series |
|
69 | 69 | QPushButton* removeButton = new QPushButton("Remove point"); |
|
70 | 70 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); |
|
71 | 71 | //! [4] |
|
72 | 72 | |
|
73 | 73 | //! [5] |
|
74 | 74 | //butttons layout |
|
75 | 75 | QVBoxLayout* buttonsLayout = new QVBoxLayout; |
|
76 | 76 | buttonsLayout->addWidget(addButton); |
|
77 | 77 | buttonsLayout->addWidget(removeButton); |
|
78 | 78 | buttonsLayout->addStretch(); |
|
79 | 79 | |
|
80 | 80 | QGridLayout* mainLayout = new QGridLayout; |
|
81 | 81 | mainLayout->addWidget(chart, 1, 0); |
|
82 | 82 | mainLayout->addLayout(buttonsLayout, 1, 1); |
|
83 | 83 | setLayout(mainLayout); |
|
84 | 84 | //! [5] |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | //! [add point] |
|
88 | 88 | void SplineWidget::addNewPoint() |
|
89 | 89 | { |
|
90 | 90 | if (series->count() > 0) |
|
91 | 91 | series->add(QPointF(series->x(series->count() - 1) + 20 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); |
|
92 | 92 | else |
|
93 | 93 | series->add(QPointF(50, 50 + qrand()%50)); |
|
94 | 94 | } |
|
95 | 95 | //! [add point] |
|
96 | 96 | |
|
97 | 97 | //! [remove point] |
|
98 | 98 | void SplineWidget::removePoint() |
|
99 | 99 | { |
|
100 | 100 | if (series->count() > 0) |
|
101 | 101 | series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); |
|
102 | 102 | } |
|
103 | 103 | //! [remove point] |
|
104 | 104 | |
|
105 | 105 | SplineWidget::~SplineWidget() |
|
106 | 106 | { |
|
107 | 107 | |
|
108 | 108 | } |
@@ -1,78 +1,75 | |||
|
1 | 1 | #include "splinepresenter_p.h" |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | |
|
4 | 4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | 5 | |
|
6 | 6 | SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) : |
|
7 | 7 | LineChartItem(series, parent)//,m_boundingRect() |
|
8 | 8 | { |
|
9 | 9 | // |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | QPointF SplinePresenter::calculateGeometryControlPoint(int index) const |
|
15 | 15 | { |
|
16 | 16 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
17 | 17 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
18 | 18 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
19 | 19 | qreal x = (splineSeries->controlPoint(index).x() - m_minX)* deltaX; |
|
20 | 20 | qreal y = (splineSeries->controlPoint(index).y() - m_minY)*-deltaY + m_size.height(); |
|
21 | 21 | return QPointF(x,y); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | void SplinePresenter::applyGeometry(QVector<QPointF>& points) |
|
25 | 25 | { |
|
26 | 26 | if(points.size()==0) return; |
|
27 | 27 | |
|
28 | qDebug() << "Kueku"; | |
|
29 | 28 | QPainterPath splinePath; |
|
30 | 29 | const QPointF& point = points.at(0); |
|
31 | 30 | splinePath.moveTo(point); |
|
32 | 31 | |
|
33 | 32 | // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
34 | 33 | for (int i = 0; i < points.size() - 1; i++) |
|
35 | 34 | { |
|
36 | 35 | const QPointF& point = points.at(i + 1); |
|
37 | 36 | splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point); |
|
38 | 37 | } |
|
39 | 38 | |
|
40 | ||
|
41 | ||
|
42 | 39 | prepareGeometryChange(); |
|
43 | 40 | m_path = splinePath; |
|
44 | 41 | m_rect = splinePath.boundingRect(); |
|
45 | 42 | } |
|
46 | 43 | |
|
47 | 44 | void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
48 | 45 | { |
|
49 | 46 | Q_UNUSED(widget); |
|
50 | 47 | Q_UNUSED(option); |
|
51 | 48 | painter->save(); |
|
52 | 49 | painter->setPen(m_pen); |
|
53 | 50 | painter->setClipRect(m_clipRect); |
|
54 | 51 | painter->drawPath(m_path); |
|
55 | 52 | |
|
56 | 53 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series); |
|
57 | 54 | for (int i = 0; i < m_points.size() - 1; i++) |
|
58 | 55 | { |
|
59 | 56 | painter->setPen(Qt::red); |
|
60 | 57 | painter->drawEllipse(m_points[i], 2, 2); |
|
61 | 58 | |
|
62 | 59 | painter->setPen(Qt::blue); |
|
63 | 60 | // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i)); |
|
64 | 61 | // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1)); |
|
65 | 62 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4); |
|
66 | 63 | // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4); |
|
67 | 64 | } |
|
68 | 65 | if (m_points.count() > 0) |
|
69 | 66 | { |
|
70 | 67 | painter->setPen(Qt::red); |
|
71 | 68 | painter->drawEllipse(m_points[m_points.count() - 1], 2, 2); |
|
72 | 69 | } |
|
73 | 70 | painter->restore(); |
|
74 | 71 | } |
|
75 | 72 | |
|
76 | 73 | #include "moc_splinepresenter_p.cpp" |
|
77 | 74 | |
|
78 | 75 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now