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