@@ -1,27 +1,39 | |||
|
1 | 1 | #include "declarativechart.h" |
|
2 | 2 | |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) |
|
6 | 6 | : QDeclarativeItem(parent), |
|
7 | 7 | m_chart(new QChart(this)) |
|
8 | 8 | { |
|
9 | 9 | setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
10 | // m_chart->setMargin(50); // TODO: should not be needed? | |
|
11 | 10 | } |
|
12 | 11 | |
|
13 | 12 | DeclarativeChart::ChartTheme DeclarativeChart::theme() |
|
14 | 13 | { |
|
15 | 14 | if (m_chart) |
|
16 | 15 | return (ChartTheme) m_chart->chartTheme(); |
|
17 | 16 | } |
|
18 | 17 | |
|
19 | 18 | void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) |
|
20 | 19 | { |
|
21 | if (newGeometry.isValid()) | |
|
22 | m_chart->resize(newGeometry.width(), newGeometry.height()); | |
|
20 | qDebug() << "geometryChanged " << this << " old geometry: " << oldGeometry; | |
|
21 | if (newGeometry.isValid()) { | |
|
22 | if (newGeometry.width() > 0 && newGeometry.height() > 0) { | |
|
23 | // TODO: setting margin should not be needed to make axis visible? | |
|
24 | const int margin = 30; | |
|
25 | if (m_chart->margin() == 0 | |
|
26 | && newGeometry.width() > (margin * 2) | |
|
27 | && newGeometry.height() > (margin * 2)) { | |
|
28 | m_chart->setMargin(margin); | |
|
29 | m_chart->resize(newGeometry.width(), newGeometry.height()); | |
|
30 | } else { | |
|
31 | m_chart->resize(newGeometry.width(), newGeometry.height()); | |
|
32 | } | |
|
33 | } | |
|
34 | } | |
|
23 | 35 | } |
|
24 | 36 | |
|
25 | 37 | #include "moc_declarativechart.cpp" |
|
26 | 38 | |
|
27 | 39 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,100 +1,91 | |||
|
1 | 1 | #include "declarativeseries.h" |
|
2 | 2 | #include "declarativechart.h" |
|
3 | 3 | #include <qscatterseries.h> |
|
4 | 4 | #include <qlinechartseries.h> |
|
5 | 5 | #include <cmath> |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | DeclarativeSeries::DeclarativeSeries(QDeclarativeItem *parent) : |
|
11 | 11 | QDeclarativeItem(parent), |
|
12 | 12 | m_seriesType(SeriesTypeInvalid), // TODO: default type? |
|
13 | 13 | m_chart(0), |
|
14 | 14 | m_series(0) |
|
15 | 15 | { |
|
16 | 16 | setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
17 | 17 | connect(this, SIGNAL(parentChanged()), |
|
18 | 18 | this, SLOT(setParentForSeries())); |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | void DeclarativeSeries::setSeriesType(SeriesType type) |
|
22 | 22 | { |
|
23 | 23 | if (!m_series || type != m_seriesType) { |
|
24 | 24 | m_seriesType = type; |
|
25 | 25 | initSeries(); |
|
26 | 26 | } else { |
|
27 | 27 | m_seriesType = type; |
|
28 | 28 | } |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | void DeclarativeSeries::setParentForSeries() |
|
32 | 32 | { |
|
33 | 33 | if (!m_series) |
|
34 | 34 | initSeries(); |
|
35 | 35 | else if (m_series->type() != m_seriesType) |
|
36 | 36 | initSeries(); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | void DeclarativeSeries::initSeries() |
|
40 | 40 | { |
|
41 | 41 | DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent()); |
|
42 | 42 | |
|
43 | 43 | if (declarativeChart && m_seriesType != SeriesTypeInvalid) { |
|
44 | 44 | delete m_series; |
|
45 | 45 | m_series = 0; |
|
46 | 46 | |
|
47 | 47 | QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart); |
|
48 | 48 | qDebug() << "creating series for chart: " << chart; |
|
49 | 49 | Q_ASSERT(chart); |
|
50 | 50 | |
|
51 | 51 | switch (m_seriesType) { |
|
52 | 52 | case SeriesTypeLine: { |
|
53 | 53 | m_series = new QLineChartSeries(this); |
|
54 | 54 | for (qreal i(0.0); i < 100.0; i += 1.0) |
|
55 | 55 | ((QLineChartSeries *)m_series)->add(i, i); |
|
56 | 56 | chart->addSeries(m_series); |
|
57 | 57 | break; |
|
58 | 58 | } |
|
59 | 59 | case SeriesTypeBar: |
|
60 | 60 | // fallthrough; bar and scatter use the same test data |
|
61 | 61 | case SeriesTypeScatter: { |
|
62 | 62 | m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); |
|
63 | 63 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(m_series); |
|
64 | 64 | Q_ASSERT(scatter); |
|
65 | 65 | for (qreal i = 0; i < 100; i += 0.1) |
|
66 | 66 | scatter->addData(QPointF(i + (rand() % 5), |
|
67 | 67 | abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5))); |
|
68 | 68 | break; |
|
69 | 69 | } |
|
70 | 70 | case SeriesTypeStackedBar: |
|
71 | 71 | break; |
|
72 | 72 | case SeriesTypePercentBar: |
|
73 | 73 | break; |
|
74 | 74 | case SeriesTypePie: { |
|
75 | 75 | m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); |
|
76 | 76 | QList<qreal> data; |
|
77 | 77 | data << 1.0; |
|
78 | 78 | data << 12.0; |
|
79 | 79 | data << 4.0; |
|
80 | 80 | Q_ASSERT(m_series->setData(data)); |
|
81 | 81 | break; |
|
82 | 82 | } |
|
83 | 83 | default: |
|
84 | 84 | break; |
|
85 | 85 | } |
|
86 | 86 | } |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | QVariant DeclarativeSeries::itemChange(GraphicsItemChange change, | |
|
90 | const QVariant &value) | |
|
91 | { | |
|
92 | // For debugging purposes only: | |
|
93 | // qDebug() << QString::number(change) << " : " << value.toString(); | |
|
94 | return QGraphicsItem::itemChange(change, value); | |
|
95 | } | |
|
96 | ||
|
97 | ||
|
98 | 89 | #include "moc_declarativeseries.cpp" |
|
99 | 90 | |
|
100 | 91 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,53 +1,50 | |||
|
1 | 1 | #ifndef DECLARATIVESERIES_H |
|
2 | 2 | #define DECLARATIVESERIES_H |
|
3 | 3 | |
|
4 | 4 | #include <QDeclarativeItem> |
|
5 | 5 | #include <qchart.h> |
|
6 | 6 | #include <qchartseries.h> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class DeclarativeSeries : public QDeclarativeItem |
|
11 | 11 | { |
|
12 | 12 | Q_OBJECT |
|
13 | 13 | Q_ENUMS(SeriesType) |
|
14 | 14 | Q_PROPERTY(SeriesType seriesType READ seriesType WRITE setSeriesType) |
|
15 | 15 | |
|
16 | 16 | public: |
|
17 | 17 | // TODO: how to re-use the existing enum from QChart? |
|
18 | 18 | enum SeriesType { |
|
19 | 19 | SeriesTypeInvalid = QChartSeries::SeriesTypeInvalid, |
|
20 | 20 | SeriesTypeLine, |
|
21 | 21 | // SeriesTypeArea, |
|
22 | 22 | SeriesTypeBar, |
|
23 | 23 | SeriesTypeStackedBar, |
|
24 | 24 | SeriesTypePercentBar, |
|
25 | 25 | SeriesTypePie, |
|
26 | 26 | SeriesTypeScatter |
|
27 | 27 | // SeriesTypeSpline |
|
28 | 28 | }; |
|
29 | 29 | |
|
30 | 30 | explicit DeclarativeSeries(QDeclarativeItem *parent = 0); |
|
31 | 31 | |
|
32 | 32 | signals: |
|
33 | 33 | |
|
34 | 34 | public slots: |
|
35 | 35 | void setParentForSeries(); |
|
36 | 36 | |
|
37 | public: // from QDeclarativeItem | |
|
38 | QVariant itemChange(GraphicsItemChange, const QVariant &); | |
|
39 | ||
|
40 | 37 | public: |
|
41 | 38 | void setSeriesType(SeriesType type); |
|
42 | 39 | SeriesType seriesType() { return m_seriesType; } |
|
43 | 40 | |
|
44 | 41 | private: |
|
45 | 42 | void initSeries(); |
|
46 | 43 | SeriesType m_seriesType; |
|
47 | 44 | QChart *m_chart; |
|
48 | 45 | QChartSeries *m_series; |
|
49 | 46 | }; |
|
50 | 47 | |
|
51 | 48 | QTCOMMERCIALCHART_END_NAMESPACE |
|
52 | 49 | |
|
53 | 50 | #endif // DECLARATIVESERIES_H |
@@ -1,77 +1,96 | |||
|
1 | 1 | import QtQuick 1.0 |
|
2 | 2 | import QtCommercial.Chart 1.0 |
|
3 | 3 | |
|
4 | 4 | Rectangle { |
|
5 | 5 | width: 360 |
|
6 | 6 | height: 360 |
|
7 | 7 | |
|
8 | 8 | // Another option for QML data api: |
|
9 | 9 | // ListModel { |
|
10 | 10 | // id: listModelForPie |
|
11 | 11 | // // PieDataElement |
|
12 | 12 | // ListElement { |
|
13 | 13 | // label: "Apple" |
|
14 | 14 | // value: 4.3 |
|
15 | 15 | // } |
|
16 | 16 | // ListElement { |
|
17 | 17 | // label: "Blackberry" |
|
18 | 18 | // value: 15.1 |
|
19 | 19 | // } |
|
20 | 20 | // } |
|
21 | 21 | |
|
22 | 22 | Component.onCompleted: { |
|
23 | 23 | // console.log("Component.onCompleted: " + scatterElement.x); |
|
24 | 24 | // console.log("Component.onCompleted: " + scatterElement.y); |
|
25 | 25 | // console.log("Component.onCompleted: " + scatterElement.dataX); |
|
26 | 26 | // console.log("Component.onCompleted: " + scatterElement.dataY); |
|
27 | 27 | //console.log("Component.onCompleted: " + chartModel.get(0).x); |
|
28 | 28 | //console.log("Component.onCompleted: " + chartModel.scatterElements); |
|
29 | 29 | // console.log("Component.onCompleted: " + elementt.dataX); |
|
30 | 30 | // console.log("Component.onCompleted: " + chartModel.get(0).dataX); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | // TODO: a bug: drawing order affects the drawing; if you draw chart1 first (by changing the | |
|
34 | // z-order), then chart2 is not shown at all. By drawing chart2 first, both are visible. | |
|
35 | Chart { | |
|
36 | id: chart2 | |
|
37 | anchors.top: chart1.bottom | |
|
38 | anchors.bottom: parent.bottom | |
|
39 | anchors.left: parent.left | |
|
40 | anchors.right: parent.right | |
|
41 | theme: Chart.ThemeScientific | |
|
42 | ||
|
43 | ScatterSeries { | |
|
44 | id: scatterSeries | |
|
45 | data: [ | |
|
46 | ScatterElement { x: 1.1; y: 2.1 }, | |
|
47 | ScatterElement { x: 1.2; y: 2.0 }, | |
|
48 | ScatterElement { x: 1.4; y: 2.3 }, | |
|
49 | ScatterElement { x: 3.1; y: 5.3 }, | |
|
50 | ScatterElement { x: 4.1; y: 3.7 } | |
|
51 | ] | |
|
52 | } | |
|
53 | } | |
|
54 | ||
|
55 | 33 | Chart { |
|
56 | 34 | id: chart1 |
|
57 | 35 | anchors.top: parent.top |
|
58 | 36 | anchors.left: parent.left |
|
59 | 37 | anchors.right: parent.right |
|
60 | 38 | height: parent.height / 2 |
|
61 | 39 | theme: Chart.ThemeIcy |
|
62 |
|
|
|
40 | // opacity: 0.3 | |
|
63 | 41 | |
|
64 | 42 | Series { |
|
65 | 43 | seriesType: Series.SeriesTypePie |
|
66 | 44 | } |
|
67 | 45 | |
|
68 | Series { | |
|
69 | seriesType: Series.SeriesTypeLine | |
|
70 | } | |
|
46 | // TODO: a bug: drawing order affects the drawing; if you draw chart1 first (by changing the | |
|
47 | // z-order), then chart2 is not shown at all. By drawing chart2 first, both are visible. | |
|
48 | // Also, if you don't draw line series on chart1 (only pie), both charts are visible. | |
|
49 | // Series { | |
|
50 | // seriesType: Series.SeriesTypeLine | |
|
51 | // } | |
|
71 | 52 | // TODO: |
|
72 | 53 |
|
|
73 | 54 | // seriesType: Series.SeriesTypeBar |
|
74 | 55 | // } |
|
75 | 56 | } |
|
76 | 57 | |
|
58 | ||
|
59 | Chart { | |
|
60 | id: chart2 | |
|
61 | anchors.top: chart1.bottom | |
|
62 | anchors.bottom: parent.bottom | |
|
63 | anchors.left: parent.left | |
|
64 | anchors.right: parent.right | |
|
65 | theme: Chart.ThemeScientific | |
|
66 | ||
|
67 | ScatterSeries { | |
|
68 | data: [ | |
|
69 | ScatterElement { x: 1.1; y: 2.1 }, | |
|
70 | ScatterElement { x: 1.2; y: 2.0 }, | |
|
71 | ScatterElement { x: 1.4; y: 2.3 } | |
|
72 | ] | |
|
73 | } | |
|
74 | ScatterSeries { | |
|
75 | data: [ | |
|
76 | ScatterElement { x: 1.2; y: 2.2 }, | |
|
77 | ScatterElement { x: 1.3; y: 2.2 }, | |
|
78 | ScatterElement { x: 1.7; y: 2.6 } | |
|
79 | ] | |
|
80 | } | |
|
81 | ScatterSeries { | |
|
82 | data: [ | |
|
83 | ScatterElement { x: 1.3; y: 2.3 }, | |
|
84 | ScatterElement { x: 1.5; y: 2.4 }, | |
|
85 | ScatterElement { x: 2.0; y: 2.9 } | |
|
86 | ] | |
|
87 | } | |
|
88 | ScatterSeries { | |
|
89 | data: [ | |
|
90 | ScatterElement { x: 1.4; y: 2.4 }, | |
|
91 | ScatterElement { x: 1.8; y: 2.7 }, | |
|
92 | ScatterElement { x: 2.5; y: 3.2 } | |
|
93 | ] | |
|
94 | } | |
|
95 | } | |
|
77 | 96 | } |
General Comments 0
You need to be logged in to leave comments.
Login now