@@ -0,0 +1,123 | |||
|
1 | #include "customtablemodel.h" | |
|
2 | ||
|
3 | CustomTableModel::CustomTableModel(QObject *parent) : | |
|
4 | QAbstractTableModel(parent) | |
|
5 | { | |
|
6 | m_points.append(QPointF(10, 50)); | |
|
7 | m_labels.append("Apples"); | |
|
8 | m_points.append(QPointF(60, 70)); | |
|
9 | m_labels.append("Oranges"); | |
|
10 | m_points.append(QPointF(110, 50)); | |
|
11 | m_labels.append("Bananas"); | |
|
12 | m_points.append(QPointF(140, 40)); | |
|
13 | m_labels.append("Lemons"); | |
|
14 | m_points.append(QPointF(200, 150)); | |
|
15 | m_labels.append("Plums"); | |
|
16 | } | |
|
17 | ||
|
18 | int CustomTableModel::rowCount(const QModelIndex & parent) const | |
|
19 | { | |
|
20 | return m_points.count(); | |
|
21 | } | |
|
22 | ||
|
23 | int CustomTableModel::columnCount(const QModelIndex & parent) const | |
|
24 | { | |
|
25 | return 3; | |
|
26 | } | |
|
27 | ||
|
28 | QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const | |
|
29 | { | |
|
30 | if (role != Qt::DisplayRole) | |
|
31 | return QVariant(); | |
|
32 | ||
|
33 | if (orientation == Qt::Horizontal) | |
|
34 | { | |
|
35 | switch(section) | |
|
36 | { | |
|
37 | case 0: | |
|
38 | return "x"; | |
|
39 | case 1: | |
|
40 | return "y"; | |
|
41 | case 2: | |
|
42 | return "Fruit"; | |
|
43 | default: "What?"; | |
|
44 | } | |
|
45 | } | |
|
46 | else | |
|
47 | return QString("%1").arg(section + 1); | |
|
48 | } | |
|
49 | ||
|
50 | QVariant CustomTableModel::data(const QModelIndex & index, int role) const | |
|
51 | { | |
|
52 | if (role == Qt::DisplayRole) | |
|
53 | { | |
|
54 | switch(index.column()) | |
|
55 | { | |
|
56 | case 0: | |
|
57 | return m_points[index.row()].x(); | |
|
58 | case 1: | |
|
59 | return m_points[index.row()].y(); | |
|
60 | case 2: | |
|
61 | return m_labels[index.row()]; | |
|
62 | default: | |
|
63 | return QVariant(); | |
|
64 | } | |
|
65 | } | |
|
66 | else if (role == Qt::EditRole) | |
|
67 | { | |
|
68 | switch(index.column()) | |
|
69 | { | |
|
70 | case 0: | |
|
71 | return m_points[index.row()].x(); | |
|
72 | case 1: | |
|
73 | return m_points[index.row()].y(); | |
|
74 | case 2: | |
|
75 | return m_labels[index.row()]; | |
|
76 | default: | |
|
77 | return QVariant(); | |
|
78 | } | |
|
79 | } | |
|
80 | } | |
|
81 | ||
|
82 | bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role) | |
|
83 | { | |
|
84 | if (index.isValid() && role == Qt::EditRole) | |
|
85 | { | |
|
86 | switch(index.column()) | |
|
87 | { | |
|
88 | case 0: | |
|
89 | m_points[index.row()].setX(value.toDouble()); | |
|
90 | break; | |
|
91 | case 1: | |
|
92 | m_points[index.row()].setY(value.toDouble()); | |
|
93 | break; | |
|
94 | case 2: | |
|
95 | m_labels.replace(index.row(), value.toString()); | |
|
96 | break; | |
|
97 | default: | |
|
98 | return false; | |
|
99 | } | |
|
100 | emit dataChanged(index, index); | |
|
101 | return true; | |
|
102 | } | |
|
103 | return false; | |
|
104 | } | |
|
105 | ||
|
106 | Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const | |
|
107 | { | |
|
108 | if (!index.isValid()) | |
|
109 | return Qt::ItemIsEnabled; | |
|
110 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | |
|
111 | } | |
|
112 | ||
|
113 | bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent) | |
|
114 | { | |
|
115 | beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1); | |
|
116 | for (int i = 0; i < count; i++) | |
|
117 | { | |
|
118 | m_points.append(QPointF()); | |
|
119 | m_labels.append(""); | |
|
120 | } | |
|
121 | endInsertRows(); | |
|
122 | return true; | |
|
123 | } |
@@ -0,0 +1,32 | |||
|
1 | #ifndef XYPOINTSMODEL_H | |
|
2 | #define XYPOINTSMODEL_H | |
|
3 | ||
|
4 | #include <QAbstractTableModel> | |
|
5 | #include <QPointF> | |
|
6 | #include <QStringList> | |
|
7 | ||
|
8 | class CustomTableModel : public QAbstractTableModel | |
|
9 | { | |
|
10 | Q_OBJECT | |
|
11 | public: | |
|
12 | explicit CustomTableModel(QObject *parent = 0); | |
|
13 | ||
|
14 | int rowCount ( const QModelIndex & parent = QModelIndex() ) const; | |
|
15 | int columnCount ( const QModelIndex & parent = QModelIndex() ) const; | |
|
16 | QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; | |
|
17 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | |
|
18 | bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ); | |
|
19 | Qt::ItemFlags flags ( const QModelIndex & index ) const; | |
|
20 | bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); | |
|
21 | ||
|
22 | //signals: | |
|
23 | ||
|
24 | //public slots: | |
|
25 | private: | |
|
26 | QList<QPointF> m_points; | |
|
27 | QStringList m_labels; | |
|
28 | ||
|
29 | ||
|
30 | }; | |
|
31 | ||
|
32 | #endif // XYPOINTSMODEL_H |
@@ -0,0 +1,11 | |||
|
1 | #include <QtGui/QApplication> | |
|
2 | #include "tablewidget.h" | |
|
3 | ||
|
4 | int main(int argc, char *argv[]) | |
|
5 | { | |
|
6 | QApplication a(argc, argv); | |
|
7 | TableWidget w; | |
|
8 | w.show(); | |
|
9 | ||
|
10 | return a.exec(); | |
|
11 | } |
@@ -0,0 +1,22 | |||
|
1 | #------------------------------------------------- | |
|
2 | # | |
|
3 | # Project created by QtCreator 2012-03-08T14:30:24 | |
|
4 | # | |
|
5 | #------------------------------------------------- | |
|
6 | ||
|
7 | !include( ../examples.pri ) { | |
|
8 | error( "Couldn't find the examples.pri file!" ) | |
|
9 | } | |
|
10 | ||
|
11 | QT += core gui | |
|
12 | ||
|
13 | TARGET = tablemodelchart | |
|
14 | TEMPLATE = app | |
|
15 | ||
|
16 | ||
|
17 | SOURCES += main.cpp\ | |
|
18 | tablewidget.cpp \ | |
|
19 | customtablemodel.cpp | |
|
20 | ||
|
21 | HEADERS += tablewidget.h \ | |
|
22 | customtablemodel.h |
@@ -0,0 +1,54 | |||
|
1 | #include "tablewidget.h" | |
|
2 | #include <QGridLayout> | |
|
3 | #include <QTableView> | |
|
4 | #include <QStyledItemDelegate> | |
|
5 | #include "qlineseries.h" | |
|
6 | #include "qsplineseries.h" | |
|
7 | #include "customtablemodel.h" | |
|
8 | #include "qpieseries.h" | |
|
9 | ||
|
10 | TableWidget::TableWidget(QWidget *parent) | |
|
11 | : QWidget(parent) | |
|
12 | { | |
|
13 | ||
|
14 | ||
|
15 | // create simple model for storing data | |
|
16 | // user's table data model | |
|
17 | CustomTableModel* model = new CustomTableModel; | |
|
18 | QTableView* tableView = new QTableView; | |
|
19 | tableView->setModel(model); | |
|
20 | tableView->setMinimumSize(340, 480); | |
|
21 | // tableView->setItemDelegate(new QStyledItemDelegate); | |
|
22 | chartView = new QChartView; | |
|
23 | chartView->setMinimumSize(640, 480); | |
|
24 | ||
|
25 | // create | |
|
26 | // QLineSeries* series = new QLineSeries; | |
|
27 | QSplineSeries* series = new QSplineSeries; | |
|
28 | series->setModel(model); | |
|
29 | series->setModelMappingX(0); | |
|
30 | series->setModelMappingY(1); | |
|
31 | ||
|
32 | // series->add(QPointF(150, 100)); | |
|
33 | // series->add(QPointF(200, 130)); | |
|
34 | // series->add(QPointF(250, 120)); | |
|
35 | // series->add(QPointF(300, 140)); | |
|
36 | // series->add(QPointF(350, 160)); | |
|
37 | ||
|
38 | // QPieSeries* pieSeries = new QPieSeries; | |
|
39 | // pieSeries->setModel(model); | |
|
40 | // pieSeries | |
|
41 | ||
|
42 | chartView->addSeries(series); | |
|
43 | ||
|
44 | // create main layout | |
|
45 | QGridLayout* mainLayout = new QGridLayout; | |
|
46 | mainLayout->addWidget(tableView, 1, 1); | |
|
47 | mainLayout->addWidget(chartView, 1, 2); | |
|
48 | setLayout(mainLayout); | |
|
49 | } | |
|
50 | ||
|
51 | TableWidget::~TableWidget() | |
|
52 | { | |
|
53 | ||
|
54 | } |
@@ -0,0 +1,23 | |||
|
1 | #ifndef TABLEWIDGET_H | |
|
2 | #define TABLEWIDGET_H | |
|
3 | ||
|
4 | #include <QtGui/QWidget> | |
|
5 | #include "qchartview.h" | |
|
6 | ||
|
7 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
8 | ||
|
9 | class TableWidget : public QWidget | |
|
10 | { | |
|
11 | Q_OBJECT | |
|
12 | ||
|
13 | public: | |
|
14 | TableWidget(QWidget *parent = 0); | |
|
15 | ~TableWidget(); | |
|
16 | ||
|
17 | ||
|
18 | ||
|
19 | private: | |
|
20 | QChartView* chartView; | |
|
21 | }; | |
|
22 | ||
|
23 | #endif // TABLEWIDGET_H |
@@ -1,23 +1,22 | |||
|
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 | scatterchart \ |
|
9 | 9 | piechart \ |
|
10 | 10 | piechartdrilldown \ |
|
11 | 11 | dynamiclinechart \ |
|
12 | 12 | axischart \ |
|
13 | 13 | multichart \ |
|
14 | 14 | gdpbarchart \ |
|
15 | 15 | presenterchart \ |
|
16 | 16 | chartview \ |
|
17 | 17 | scatterinteractions \ |
|
18 | 18 | splinechart \ |
|
19 | 19 | areachart \ |
|
20 | 20 | stackedbarchartdrilldown \ |
|
21 | customcolors | |
|
22 | ||
|
23 | ||
|
21 | customcolors \ | |
|
22 | tablemodelchart |
@@ -1,142 +1,148 | |||
|
1 | 1 | #include "qsplineseries.h" |
|
2 | 2 | |
|
3 | 3 | /*! |
|
4 | 4 | \class QSplineSeries |
|
5 | 5 | \brief Series type used to store data needed to draw a spline. |
|
6 | 6 | |
|
7 | 7 | QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline |
|
8 | 8 | Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn. |
|
9 | 9 | */ |
|
10 | 10 | |
|
11 | 11 | /*! |
|
12 | 12 | \fn QSeriesType QSplineSeries::type() const |
|
13 | 13 | Returns the type of the series |
|
14 | 14 | */ |
|
15 | 15 | |
|
16 | 16 | /*! |
|
17 | 17 | \fn QSeriesType QSplineSeries::controlPoint(int index) const |
|
18 | 18 | Returns the control point specified by \a index |
|
19 | 19 | */ |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | 22 | |
|
23 | 23 | /*! |
|
24 | 24 | Constructs empty series object which is a child of \a parent. |
|
25 | 25 | When series object is added to QChartView or QChart instance then the ownerships is transfered. |
|
26 | 26 | */ |
|
27 | 27 | |
|
28 | 28 | QSplineSeries::QSplineSeries(QObject *parent) : |
|
29 | 29 | QLineSeries(parent) |
|
30 | 30 | { |
|
31 | 31 | connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints())); |
|
32 | 32 | connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints())); |
|
33 | 33 | connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints())); |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | /*! |
|
37 | 37 | \internal |
|
38 | 38 | Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points. |
|
39 | 39 | */ |
|
40 | 40 | void QSplineSeries::calculateControlPoints() |
|
41 | 41 | { |
|
42 | 42 | |
|
43 | 43 | // Based on http://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit |
|
44 | 44 | // CPOL License |
|
45 | 45 | |
|
46 |
int n = |
|
|
46 | int n = count() - 1; | |
|
47 | 47 | if (n == 1) |
|
48 | 48 | { // Special case: Bezier curve should be a straight line. |
|
49 | 49 | // firstControlPoints = new Point[1]; |
|
50 | 50 | // 3P1 = 2P0 + P3 |
|
51 |
m_controlPoints.append(QPointF((2 * |
|
|
51 | m_controlPoints.append(QPointF((2 * x(0) + x(1)) / 3, (2 * y(0) + y(1)) / 3)); | |
|
52 | 52 | |
|
53 | 53 | // P2 = 2P1 P0 |
|
54 |
m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - |
|
|
54 | m_controlPoints.append(QPointF(2 * m_controlPoints[0].x() - x(0), 2 * m_controlPoints[0].y() - y(0))); | |
|
55 | 55 | return; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | // Calculate first Bezier control points |
|
59 | 59 | // Right hand side vector |
|
60 | 60 | // Set of equations for P0 to Pn points. |
|
61 | 61 | // |
|
62 | 62 | // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 | |
|
63 | 63 | // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 | |
|
64 | 64 | // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 | |
|
65 | 65 | // | . . . . . . . . . . . . | | ... | | ... | |
|
66 | 66 | // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi | |
|
67 | 67 | // | . . . . . . . . . . . . | | ... | | ... | |
|
68 | 68 | // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) | |
|
69 | 69 | // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn | |
|
70 | 70 | // |
|
71 | 71 | QList<qreal> rhs; |
|
72 |
rhs.append( |
|
|
72 | rhs.append(x(0) + 2 * x(1)); | |
|
73 | 73 | |
|
74 | 74 | // Set right hand side X values |
|
75 | 75 | for (int i = 1; i < n - 1; ++i) |
|
76 |
rhs.append(4 * |
|
|
76 | rhs.append(4 * x(i) + 2 * x(i + 1)); | |
|
77 | 77 | |
|
78 |
rhs.append((8 * |
|
|
78 | rhs.append((8 * x(n - 1) + x(n)) / 2.0); | |
|
79 | 79 | // Get first control points X-values |
|
80 | QList<qreal> x = getFirstControlPoints(rhs); | |
|
81 |
rhs[0] = |
|
|
80 | QList<qreal> xControl = getFirstControlPoints(rhs); | |
|
81 | rhs[0] = y(0) + 2 * y(1); | |
|
82 | 82 | |
|
83 | 83 | // Set right hand side Y values |
|
84 | 84 | for (int i = 1; i < n - 1; ++i) |
|
85 |
rhs[i] = 4 * |
|
|
85 | rhs[i] = 4 * y(i) + 2 * y(i + 1); | |
|
86 | 86 | |
|
87 |
rhs[n - 1] = (8 * |
|
|
87 | rhs[n - 1] = (8 * y(n - 1) + y(n)) / 2.0; | |
|
88 | 88 | // Get first control points Y-values |
|
89 | QList<qreal> y = getFirstControlPoints(rhs); | |
|
89 | QList<qreal> yControl = getFirstControlPoints(rhs); | |
|
90 | 90 | |
|
91 | 91 | // Fill output arrays. |
|
92 | 92 | for (int i = 0; i < n; ++i) |
|
93 | 93 | { |
|
94 | 94 | // First control point |
|
95 | m_controlPoints.append(QPointF(x[i], y[i])); | |
|
95 | m_controlPoints.append(QPointF(xControl[i], yControl[i])); | |
|
96 | 96 | // Second control point |
|
97 | 97 | if (i < n - 1) |
|
98 |
m_controlPoints.append(QPointF(2 * |
|
|
98 | m_controlPoints.append(QPointF(2 * x(i + 1) - xControl[i + 1], 2 * y(i + 1) - yControl[i + 1])); | |
|
99 | 99 | else |
|
100 |
m_controlPoints.append(QPointF(( |
|
|
100 | m_controlPoints.append(QPointF((x(n) + xControl[n - 1]) / 2, (y(n) + yControl[n - 1]) / 2)); | |
|
101 | 101 | } |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | /*! |
|
105 | 105 | \internal |
|
106 | 106 | */ |
|
107 | 107 | QList<qreal> QSplineSeries::getFirstControlPoints(QList<qreal> rhs) |
|
108 | 108 | { |
|
109 | 109 | QList<qreal> x; // Solution vector. |
|
110 | 110 | QList<qreal> tmp; // Temp workspace. |
|
111 | 111 | |
|
112 | 112 | qreal b = 2.0; |
|
113 | 113 | x.append(rhs[0] / b); |
|
114 | 114 | tmp.append(0); |
|
115 | 115 | for (int i = 1; i < rhs.size(); i++) // Decomposition and forward substitution. |
|
116 | 116 | { |
|
117 | 117 | tmp.append(1 / b); |
|
118 | 118 | b = (i < rhs.size() - 1 ? 4.0 : 3.5) - tmp[i]; |
|
119 | 119 | x.append((rhs[i] - x[i - 1]) / b); |
|
120 | 120 | } |
|
121 | 121 | for (int i = 1; i < rhs.size(); i++) |
|
122 | 122 | x[rhs.size() - i - 1] -= tmp[rhs.size() - i] * x[rhs.size() - i]; // Backsubstitution. |
|
123 | 123 | |
|
124 | 124 | return x; |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | /*! |
|
128 | 128 | \internal |
|
129 | 129 | Updates the control points, besed on currently avaiable knots. |
|
130 | 130 | */ |
|
131 | 131 | void QSplineSeries::updateControlPoints() |
|
132 | 132 | { |
|
133 |
if( |
|
|
133 | if(count() > 1) | |
|
134 | 134 | { |
|
135 | 135 | m_controlPoints.clear(); |
|
136 | 136 | calculateControlPoints(); |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | bool QSplineSeries::setModel(QAbstractItemModel* model) | |
|
141 | { | |
|
142 | QXYSeries::setModel(model); | |
|
143 | calculateControlPoints(); | |
|
144 | } | |
|
145 | ||
|
140 | 146 | #include "moc_qsplineseries.cpp" |
|
141 | 147 | |
|
142 | 148 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,43 +1,44 | |||
|
1 | 1 | #ifndef QSPLINESERIES_H |
|
2 | 2 | #define QSPLINESERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include <QtGlobal> |
|
6 | 6 | #include "qlineseries.h" |
|
7 | 7 | #include <QList> |
|
8 | 8 | #include <QPointF> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | public: |
|
16 | 16 | |
|
17 | 17 | QSplineSeries(QObject *parent = 0); |
|
18 | 18 | QSeriesType type() const {return QSeries::SeriesTypeSpline;} |
|
19 | 19 | |
|
20 | 20 | // int count() const { return m_x.size(); } |
|
21 | 21 | QPointF controlPoint(int index) const {return m_controlPoints[index];} |
|
22 | bool setModel(QAbstractItemModel* model); | |
|
22 | 23 | |
|
23 | 24 | // TODO: allow the user to set custom control points |
|
24 | 25 | // void setCustomControlPoints(QList<QPointsF> controlPoints); |
|
25 | 26 | // bool calculateControlPointsAutomatically(); |
|
26 | 27 | // void setCalculateControlPointsAutomatically(); |
|
27 | 28 | |
|
28 | 29 | |
|
29 | 30 | private: |
|
30 | 31 | void calculateControlPoints(); |
|
31 | 32 | QList<qreal> getFirstControlPoints(QList<qreal> rhs); |
|
32 | 33 | |
|
33 | 34 | private slots: |
|
34 | 35 | void updateControlPoints(); |
|
35 | 36 | |
|
36 | 37 | private: |
|
37 | 38 | QList<QPointF> m_controlPoints; |
|
38 | 39 | |
|
39 | 40 | }; |
|
40 | 41 | |
|
41 | 42 | QTCOMMERCIALCHART_END_NAMESPACE |
|
42 | 43 | |
|
43 | 44 | #endif // QSPLINESERIES_H |
@@ -1,229 +1,254 | |||
|
1 | 1 | #include "qxyseries.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | /*! |
|
6 | 6 | \class QXYSeries |
|
7 | 7 | \brief The QXYSeries class is a base class for line, spline and scatter series. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | /*! |
|
11 | 11 | \fn QPen QXYSeries::pen() const |
|
12 | 12 | \brief Returns pen used to draw points for series. |
|
13 | 13 | \sa setPen() |
|
14 | 14 | */ |
|
15 | 15 | |
|
16 | 16 | /*! |
|
17 | 17 | \fn QBrush QXYSeries::brush() const |
|
18 | 18 | \brief Returns brush used to draw points for series. |
|
19 | 19 | \sa setBrush() |
|
20 | 20 | */ |
|
21 | 21 | |
|
22 | 22 | /*! |
|
23 | 23 | \fn void QXYSeries::pointReplaced(int index) |
|
24 | 24 | \brief \internal \a index |
|
25 | 25 | */ |
|
26 | 26 | |
|
27 | 27 | /*! |
|
28 | 28 | \fn void QXYSeries::pointAdded(int index) |
|
29 | 29 | \brief \internal \a index |
|
30 | 30 | */ |
|
31 | 31 | |
|
32 | 32 | /*! |
|
33 | 33 | \fn void QXYSeries::pointRemoved(int index) |
|
34 | 34 | \brief \internal \a index |
|
35 | 35 | */ |
|
36 | 36 | |
|
37 | 37 | /*! |
|
38 | 38 | \fn void QXYSeries::updated() |
|
39 | 39 | \brief \internal |
|
40 | 40 | */ |
|
41 | 41 | |
|
42 | 42 | /*! |
|
43 | 43 | Constructs empty series object which is a child of \a parent. |
|
44 | 44 | When series object is added to QChartView or QChart instance ownerships is transfered. |
|
45 | 45 | */ |
|
46 | 46 | QXYSeries::QXYSeries(QObject* parent):QSeries(parent) |
|
47 | 47 | { |
|
48 | m_model = 0; | |
|
49 | m_mapX = 0; | |
|
50 | m_mapY = 1; | |
|
48 | 51 | } |
|
49 | 52 | /*! |
|
50 | 53 | Destroys the object. Series added to QChartView or QChart instances are owned by those, |
|
51 | 54 | and are deleted when mentioned object are destroyed. |
|
52 | 55 | */ |
|
53 | 56 | QXYSeries::~QXYSeries() |
|
54 | 57 | { |
|
55 | 58 | } |
|
56 | 59 | |
|
57 | 60 | /*! |
|
58 | 61 | Adds data point \a x \a y to the series. Points are connected with lines on the chart. |
|
59 | 62 | */ |
|
60 | 63 | void QXYSeries::add(qreal x,qreal y) |
|
61 | 64 | { |
|
62 | 65 | Q_ASSERT(m_x.size() == m_y.size()); |
|
63 | 66 | m_x<<x; |
|
64 | 67 | m_y<<y; |
|
65 | 68 | emit pointAdded(m_x.size()-1); |
|
66 | 69 | } |
|
67 | 70 | |
|
68 | 71 | /*! |
|
69 | 72 | This is an overloaded function. |
|
70 | 73 | Adds data \a point to the series. Points are connected with lines on the chart. |
|
71 | 74 | */ |
|
72 | 75 | void QXYSeries::add(const QPointF& point) |
|
73 | 76 | { |
|
74 | 77 | add(point.x(),point.y()); |
|
75 | 78 | } |
|
76 | 79 | |
|
77 | 80 | /*! |
|
78 | 81 | This is an overloaded function. |
|
79 | 82 | Adds list of data \a points to the series. Points are connected with lines on the chart. |
|
80 | 83 | */ |
|
81 | 84 | void QXYSeries::add(const QList<QPointF> points) |
|
82 | 85 | { |
|
83 | 86 | foreach(const QPointF& point , points) { |
|
84 | 87 | add(point.x(),point.y()); |
|
85 | 88 | } |
|
86 | 89 | } |
|
87 | 90 | |
|
88 | 91 | /*! |
|
89 | 92 | Modifies \a y value for given \a x a value. |
|
90 | 93 | */ |
|
91 | 94 | void QXYSeries::replace(qreal x,qreal y) |
|
92 | 95 | { |
|
93 | 96 | int index = m_x.indexOf(x); |
|
94 | 97 | m_x[index]=x; |
|
95 | 98 | m_y[index]=y; |
|
96 | 99 | emit pointReplaced(index); |
|
97 | 100 | } |
|
98 | 101 | |
|
99 | 102 | /*! |
|
100 | 103 | This is an overloaded function. |
|
101 | 104 | Replaces current y value of for given \a point x value with \a point y value. |
|
102 | 105 | */ |
|
103 | 106 | void QXYSeries::replace(const QPointF& point) |
|
104 | 107 | { |
|
105 | replace(point.x(),point.y()); | |
|
108 | replace(point.x(),point.y()); | |
|
106 | 109 | } |
|
107 | 110 | |
|
108 | 111 | /*! |
|
109 | 112 | Removes current \a x and y value. |
|
110 | 113 | */ |
|
111 | 114 | void QXYSeries::remove(qreal x) |
|
112 | 115 | { |
|
113 | 116 | int index = m_x.indexOf(x); |
|
114 | 117 | emit pointRemoved(index); |
|
115 | 118 | m_x.remove(index); |
|
116 | 119 | m_y.remove(index); |
|
117 | 120 | } |
|
118 | 121 | |
|
119 | 122 | /*! |
|
120 | 123 | Removes current \a point x value. Note \a point y value is ignored. |
|
121 | 124 | */ |
|
122 | 125 | void QXYSeries::remove(const QPointF& point) |
|
123 | 126 | { |
|
124 | 127 | remove(point.x()); |
|
125 | 128 | } |
|
126 | 129 | |
|
127 | 130 | /*! |
|
128 | 131 | Removes all data points from the series. |
|
129 | 132 | */ |
|
130 | 133 | void QXYSeries::removeAll() |
|
131 | 134 | { |
|
132 | 135 | m_x.clear(); |
|
133 | 136 | m_y.clear(); |
|
134 | 137 | } |
|
135 | 138 | |
|
136 | 139 | /*! |
|
137 | 140 | \internal \a pos |
|
138 | 141 | */ |
|
139 | 142 | qreal QXYSeries::x(int pos) const |
|
140 | 143 | { |
|
141 | return m_x.at(pos); | |
|
144 | if (m_model) | |
|
145 | return m_model->data(m_model->index(pos, m_mapX), Qt::DisplayRole).toDouble(); | |
|
146 | else | |
|
147 | return m_x.at(pos); | |
|
142 | 148 | } |
|
143 | 149 | |
|
144 | 150 | /*! |
|
145 | 151 | \internal \a pos |
|
146 | 152 | */ |
|
147 | 153 | qreal QXYSeries::y(int pos) const |
|
148 | 154 | { |
|
149 | return m_y.at(pos); | |
|
155 | if (m_model) | |
|
156 | return m_model->data(m_model->index(pos, m_mapY), Qt::DisplayRole).toDouble(); | |
|
157 | else | |
|
158 | return m_y.at(pos); | |
|
150 | 159 | } |
|
151 | 160 | |
|
152 | 161 | /*! |
|
153 | 162 | Returns number of data points within series. |
|
154 | 163 | */ |
|
155 | 164 | int QXYSeries::count() const |
|
156 | 165 | { |
|
157 |
|
|
|
158 | ||
|
159 | return m_x.size(); | |
|
166 | Q_ASSERT(m_x.size() == m_y.size()); | |
|
160 | 167 | |
|
168 | // int k = m_model->rowCount(); | |
|
169 | if (m_model) | |
|
170 | return m_model->rowCount(); | |
|
171 | else | |
|
172 | return m_x.size(); | |
|
161 | 173 | } |
|
162 | 174 | |
|
163 | 175 | /*! |
|
164 | 176 | Returns the data points of the series. |
|
165 | 177 | */ |
|
166 | 178 | QList<QPointF> QXYSeries::data() |
|
167 | 179 | { |
|
168 | 180 | QList<QPointF> data; |
|
169 | 181 | for (int i(0); i < m_x.count() && i < m_y.count(); i++) |
|
170 | 182 | data.append(QPointF(m_x.at(i), m_y.at(i))); |
|
171 | 183 | return data; |
|
172 | 184 | } |
|
173 | 185 | |
|
174 | 186 | |
|
175 | 187 | /*! |
|
176 | 188 | Sets \a pen used for drawing points on the chart. If the pen is not defined, the |
|
177 | 189 | pen from chart theme is used. |
|
178 | 190 | \sa QChart::setChartTheme() |
|
179 | 191 | */ |
|
180 | 192 | void QXYSeries::setPen(const QPen& pen) |
|
181 | 193 | { |
|
182 | 194 | if(pen!=m_pen){ |
|
183 | 195 | m_pen=pen; |
|
184 | 196 | emit updated(); |
|
185 | 197 | } |
|
186 | 198 | } |
|
187 | 199 | |
|
188 | 200 | /*! |
|
189 | 201 | Sets \a brush used for drawing points on the chart. If the brush is not defined, brush |
|
190 | 202 | from chart theme setting is used. |
|
191 | 203 | \sa QChart::setChartTheme() |
|
192 | 204 | */ |
|
193 | 205 | |
|
194 | 206 | void QXYSeries::setBrush(const QBrush& brush) |
|
195 | 207 | { |
|
196 | 208 | if(brush!=m_brush){ |
|
197 | 209 | m_brush=brush; |
|
198 | 210 | emit updated(); |
|
199 | 211 | } |
|
200 | 212 | } |
|
201 | 213 | |
|
202 | 214 | |
|
203 | 215 | /*! |
|
204 | 216 | Stream operator for adding a data \a point to the series. |
|
205 | 217 | \sa add() |
|
206 | 218 | */ |
|
207 | 219 | |
|
208 | 220 | QXYSeries& QXYSeries::operator<< (const QPointF &point) |
|
209 | 221 | { |
|
210 | 222 | add(point); |
|
211 | 223 | return *this; |
|
212 | 224 | } |
|
213 | 225 | |
|
214 | 226 | |
|
215 | 227 | /*! |
|
216 | 228 | Stream operator for adding a list of \a points to the series. |
|
217 | 229 | \sa add() |
|
218 | 230 | */ |
|
219 | 231 | |
|
220 | 232 | QXYSeries& QXYSeries::operator<< (const QList<QPointF> points) |
|
221 | 233 | { |
|
222 | 234 | add(points); |
|
223 | 235 | return *this; |
|
224 | 236 | } |
|
225 | 237 | |
|
226 | 238 | |
|
239 | void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
|
240 | { | |
|
241 | emit pointReplaced(topLeft.row()); | |
|
242 | } | |
|
243 | ||
|
244 | bool QXYSeries::setModel(QAbstractItemModel* model) { | |
|
245 | m_model = model; | |
|
246 | for (int i = 0; i < m_model->rowCount(); i++) | |
|
247 | emit pointAdded(i); | |
|
248 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); | |
|
249 | // connect(m_model,SIGNAL(), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); | |
|
250 | } | |
|
251 | ||
|
227 | 252 | #include "moc_qxyseries.cpp" |
|
228 | 253 | |
|
229 | 254 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,57 +1,71 | |||
|
1 | 1 | #ifndef QXYSERIES_H_ |
|
2 | 2 | #define QXYSERIES_H_ |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qseries.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | #include <QPen> |
|
8 | 8 | #include <QBrush> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | protected: |
|
16 | 16 | QXYSeries(QObject* parent=0); |
|
17 | 17 | virtual ~QXYSeries(); |
|
18 | 18 | |
|
19 | 19 | public: |
|
20 | 20 | void add(qreal x, qreal y); |
|
21 | 21 | void add(const QPointF& point); |
|
22 | 22 | void add(const QList<QPointF> points); |
|
23 | 23 | void replace(qreal x,qreal y); |
|
24 | 24 | void replace(const QPointF& point); |
|
25 | 25 | void remove(qreal x); |
|
26 | 26 | void remove(const QPointF& point); |
|
27 | 27 | void removeAll(); |
|
28 | 28 | |
|
29 | 29 | int count() const; |
|
30 | 30 | qreal x(int pos) const; |
|
31 | 31 | qreal y(int pos) const; |
|
32 | 32 | QList<QPointF> data(); |
|
33 | 33 | |
|
34 | 34 | QXYSeries& operator << (const QPointF &point); |
|
35 | 35 | QXYSeries& operator << (const QList<QPointF> points); |
|
36 | 36 | |
|
37 | 37 | void setPen(const QPen& pen); |
|
38 | 38 | QPen pen() const {return m_pen;} |
|
39 | 39 | void setBrush(const QBrush& pen); |
|
40 | 40 | QBrush brush() const {return m_brush;} |
|
41 | 41 | |
|
42 | bool setModel(QAbstractItemModel* model); | |
|
43 | QAbstractItemModel* model() {return m_model;} | |
|
44 | ||
|
45 | void setModelMappingX(int modelColumn) {m_mapX = modelColumn;} | |
|
46 | void setModelMappingY(int modelColumn) {m_mapY = modelColumn;} | |
|
47 | ||
|
48 | private slots: | |
|
49 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
|
50 | ||
|
42 | 51 | signals: |
|
43 | 52 | void updated(); |
|
44 | 53 | void pointReplaced(int index); |
|
45 | 54 | void pointRemoved(int index); |
|
46 | 55 | void pointAdded(int index); |
|
47 | 56 | |
|
48 | 57 | protected: |
|
49 | 58 | QVector<qreal> m_x; |
|
50 | 59 | QVector<qreal> m_y; |
|
60 | ||
|
51 | 61 | QPen m_pen; |
|
52 | 62 | QBrush m_brush; |
|
63 | ||
|
64 | QAbstractItemModel* m_model; | |
|
65 | int m_mapX; | |
|
66 | int m_mapY; | |
|
53 | 67 | }; |
|
54 | 68 | |
|
55 | 69 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | 70 | |
|
57 | 71 | #endif |
@@ -1,142 +1,142 | |||
|
1 | 1 | #include "xychartitem_p.h" |
|
2 | 2 | #include "qxyseries.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include <QPainter> |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | //TODO: optimize : remove points which are not visible |
|
10 | 10 | |
|
11 | 11 | XYChartItem::XYChartItem(QXYSeries* series,QGraphicsItem *parent):ChartItem(parent), |
|
12 | 12 | m_minX(0), |
|
13 | 13 | m_maxX(0), |
|
14 | 14 | m_minY(0), |
|
15 | 15 | m_maxY(0), |
|
16 | 16 | m_series(series) |
|
17 | 17 | { |
|
18 | 18 | QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int))); |
|
19 | 19 | QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int))); |
|
20 | 20 | QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int))); |
|
21 | 21 | |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const |
|
25 | 25 | { |
|
26 | 26 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
27 | 27 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
28 | 28 | qreal x = (point.x() - m_minX)* deltaX; |
|
29 | 29 | qreal y = (point.y() - m_minY)*-deltaY + m_size.height(); |
|
30 | 30 | return QPointF(x,y); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | QPointF XYChartItem::calculateGeometryPoint(int index) const |
|
35 | 35 | { |
|
36 | 36 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
37 | 37 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
38 | 38 | qreal x = (m_series->x(index) - m_minX)* deltaX; |
|
39 | 39 | qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height(); |
|
40 | 40 | return QPointF(x,y); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | QVector<QPointF> XYChartItem::calculateGeometryPoints() const |
|
44 | 44 | { |
|
45 | 45 | const qreal deltaX = m_size.width()/(m_maxX-m_minX); |
|
46 | 46 | const qreal deltaY = m_size.height()/(m_maxY-m_minY); |
|
47 | 47 | |
|
48 | 48 | QVector<QPointF> points; |
|
49 | 49 | points.reserve(m_series->count()); |
|
50 | 50 | for (int i = 0; i < m_series->count(); ++i) { |
|
51 | 51 | qreal x = (m_series->x(i) - m_minX)* deltaX; |
|
52 | 52 | qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height(); |
|
53 | 53 | points << QPointF(x,y); |
|
54 | 54 | } |
|
55 | 55 | return points; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void XYChartItem::updatePoints(QVector<QPointF>& points) |
|
59 | 59 | { |
|
60 | 60 | setGeometry(points); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | void XYChartItem::updatePoint(QVector<QPointF>& points) |
|
64 | 64 | { |
|
65 | 65 | setGeometry(points); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void XYChartItem::setGeometry(QVector<QPointF>& points) |
|
69 | 69 | { |
|
70 | 70 | m_points = points; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | //handlers |
|
74 | 74 | |
|
75 | 75 | void XYChartItem::handlePointAdded(int index) |
|
76 | 76 | { |
|
77 | 77 | Q_ASSERT(index<m_series->count()); |
|
78 | 78 | Q_ASSERT(index>=0); |
|
79 | 79 | |
|
80 | 80 | QPointF point = calculateGeometryPoint(index); |
|
81 | 81 | QVector<QPointF> points = m_points; |
|
82 | 82 | points.insert(index,point); |
|
83 | 83 | updatePoints(points); |
|
84 | 84 | update(); |
|
85 | 85 | } |
|
86 | 86 | void XYChartItem::handlePointRemoved(int index) |
|
87 | 87 | { |
|
88 | 88 | Q_ASSERT(index<m_series->count()); |
|
89 | 89 | Q_ASSERT(index>=0); |
|
90 | 90 | QPointF point = calculateGeometryPoint(index); |
|
91 | 91 | QVector<QPointF> points = m_points; |
|
92 | 92 | points.remove(index); |
|
93 | 93 | updatePoints(points); |
|
94 | 94 | update(); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | void XYChartItem::handlePointReplaced(int index) |
|
98 | 98 | { |
|
99 | 99 | Q_ASSERT(index<m_series->count()); |
|
100 | 100 | Q_ASSERT(index>=0); |
|
101 | 101 | QPointF point = calculateGeometryPoint(index); |
|
102 | 102 | QVector<QPointF> points = m_points; |
|
103 | 103 | m_points.replace(index,point); |
|
104 | updatePoint(points); | |
|
104 | updatePoint(m_points); | |
|
105 | 105 | update(); |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
109 | 109 | { |
|
110 | 110 | m_minX=minX; |
|
111 | 111 | m_maxX=maxX; |
|
112 | 112 | m_minY=minY; |
|
113 | 113 | m_maxY=maxY; |
|
114 | 114 | |
|
115 | 115 | if(isEmpty()) return; |
|
116 | 116 | QVector<QPointF> points = calculateGeometryPoints(); |
|
117 | 117 | updatePoints(points); |
|
118 | 118 | update(); |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | void XYChartItem::handleGeometryChanged(const QRectF& rect) |
|
122 | 122 | { |
|
123 | 123 | Q_ASSERT(rect.isValid()); |
|
124 | 124 | m_size=rect.size(); |
|
125 | 125 | m_clipRect=rect.translated(-rect.topLeft()); |
|
126 | 126 | setPos(rect.topLeft()); |
|
127 | 127 | |
|
128 |
|
|
|
128 | if(isEmpty()) return; | |
|
129 | 129 | QVector<QPointF> points = calculateGeometryPoints(); |
|
130 | 130 | updatePoints(points); |
|
131 | 131 | update(); |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | bool XYChartItem::isEmpty() |
|
136 | 136 | { |
|
137 | 137 | return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ; |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | #include "moc_xychartitem_p.cpp" |
|
141 | 141 | |
|
142 | 142 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now