@@ -1,32 +1,33 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <cmath> |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <qchartview.h> |
|
6 | 6 | #include <qscatterseries.h> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | int main(int argc, char *argv[]) |
|
11 | 11 | { |
|
12 | 12 | QApplication a(argc, argv); |
|
13 | 13 | |
|
14 | 14 | // Create widget and scatter series |
|
15 | 15 | QChartView *chartWidget = new QChartView(); |
|
16 | QScatterSeries *scatter = | |
|
17 | qobject_cast<QScatterSeries *>(chartWidget->createSeries(QChartSeries::SeriesTypeScatter)); | |
|
18 | Q_ASSERT(scatter); | |
|
16 | QScatterSeries *scatter = new QScatterSeries(); | |
|
19 | 17 | |
|
20 | 18 | // Add test data to the series |
|
21 | 19 | for (qreal i(0.0); i < 20; i += 0.5) |
|
22 |
scatter |
|
|
23 |
|
|
|
20 | (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0, | |
|
21 | i + (qreal)(rand() % 100) / 100.0); | |
|
22 | ||
|
23 | // Add series to the chart widget | |
|
24 | chartWidget->addSeries(scatter); | |
|
24 | 25 | |
|
25 | 26 | // Use the chart widget as the central widget |
|
26 | 27 | QMainWindow w; |
|
27 | 28 | w.resize(640, 480); |
|
28 | 29 | w.setCentralWidget(chartWidget); |
|
29 | 30 | w.show(); |
|
30 | 31 | |
|
31 | 32 | return a.exec(); |
|
32 | 33 | } |
@@ -1,95 +1,100 | |||
|
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 | } else { | |
|
27 | m_seriesType = type; | |
|
26 | 28 | } |
|
27 | 29 | } |
|
28 | 30 | |
|
29 | 31 | void DeclarativeSeries::setParentForSeries() |
|
30 | 32 | { |
|
31 |
i |
|
|
33 | if (!m_series) | |
|
34 | initSeries(); | |
|
35 | else if (m_series->type() != m_seriesType) | |
|
36 | initSeries(); | |
|
32 | 37 | } |
|
33 | 38 | |
|
34 | 39 | void DeclarativeSeries::initSeries() |
|
35 | 40 | { |
|
36 | 41 | DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent()); |
|
37 | 42 | |
|
38 | 43 | if (declarativeChart && m_seriesType != SeriesTypeInvalid) { |
|
39 | 44 | delete m_series; |
|
40 | 45 | m_series = 0; |
|
41 | 46 | |
|
42 | 47 | QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart); |
|
43 | 48 | qDebug() << "creating series for chart: " << chart; |
|
44 | 49 | Q_ASSERT(chart); |
|
45 | 50 | |
|
46 | 51 | switch (m_seriesType) { |
|
47 | 52 | case SeriesTypeLine: { |
|
48 | 53 | m_series = new QLineChartSeries(this); |
|
49 | 54 | for (qreal i(0.0); i < 100.0; i += 1.0) |
|
50 | 55 | ((QLineChartSeries *)m_series)->add(i, i); |
|
51 | 56 | chart->addSeries(m_series); |
|
52 | 57 | break; |
|
53 | 58 | } |
|
54 | 59 | case SeriesTypeBar: |
|
55 | 60 | // fallthrough; bar and scatter use the same test data |
|
56 | 61 | case SeriesTypeScatter: { |
|
57 | 62 | m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); |
|
58 | 63 | QScatterSeries *scatter = qobject_cast<QScatterSeries *>(m_series); |
|
59 | 64 | Q_ASSERT(scatter); |
|
60 | 65 | for (qreal i = 0; i < 100; i += 0.1) |
|
61 | 66 | scatter->addData(QPointF(i + (rand() % 5), |
|
62 | 67 | abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5))); |
|
63 | 68 | break; |
|
64 | 69 | } |
|
65 | 70 | case SeriesTypeStackedBar: |
|
66 | 71 | break; |
|
67 | 72 | case SeriesTypePercentBar: |
|
68 | 73 | break; |
|
69 | 74 | case SeriesTypePie: { |
|
70 | 75 | m_series = chart->createSeries((QChartSeries::QChartSeriesType) m_seriesType); |
|
71 | 76 | QList<qreal> data; |
|
72 | 77 | data << 1.0; |
|
73 | 78 | data << 12.0; |
|
74 | 79 | data << 4.0; |
|
75 | 80 | Q_ASSERT(m_series->setData(data)); |
|
76 | 81 | break; |
|
77 | 82 | } |
|
78 | 83 | default: |
|
79 | 84 | break; |
|
80 | 85 | } |
|
81 | 86 | } |
|
82 | 87 | } |
|
83 | 88 | |
|
84 | 89 | QVariant DeclarativeSeries::itemChange(GraphicsItemChange change, |
|
85 | 90 | const QVariant &value) |
|
86 | 91 | { |
|
87 | 92 | // For debugging purposes only: |
|
88 | 93 | // qDebug() << QString::number(change) << " : " << value.toString(); |
|
89 | 94 | return QGraphicsItem::itemChange(change, value); |
|
90 | 95 | } |
|
91 | 96 | |
|
92 | 97 | |
|
93 | 98 | #include "moc_declarativeseries.cpp" |
|
94 | 99 | |
|
95 | 100 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,54 +1,59 | |||
|
1 | 1 | #include "qscatterseries.h" |
|
2 | 2 | #include "qscatterseries_p.h" |
|
3 | 3 | #include "qchart.h" |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | QScatterSeriesPrivate::QScatterSeriesPrivate() : |
|
8 | 8 | m_data(QList<QPointF>()) |
|
9 | 9 | { |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | QScatterSeries::QScatterSeries(QObject *parent) : |
|
13 | 13 | QChartSeries(parent), |
|
14 | 14 | d(new QScatterSeriesPrivate()) |
|
15 | 15 | { |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | QScatterSeries::~QScatterSeries() |
|
19 | 19 | { |
|
20 | 20 | delete d; |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | // TODO: change to list of QPointFs? | |
|
24 | //bool QScatterSeries::setData(QList<qreal> xlist, QList<qreal> ylist) | |
|
25 | void QScatterSeries::setData(QList<QPointF> data) | |
|
23 | void QScatterSeries::addData(QPointF value) | |
|
26 | 24 | { |
|
27 |
d->m_data |
|
|
25 | d->m_data.append(value); | |
|
26 | emit changed(); | |
|
27 | } | |
|
28 | ||
|
29 | QScatterSeries& QScatterSeries::operator << (const QPointF &value) | |
|
30 | { | |
|
31 | d->m_data.append(value); | |
|
28 | 32 | emit changed(); |
|
33 | return *this; | |
|
29 | 34 | } |
|
30 | 35 | |
|
31 |
void QScatterSeries:: |
|
|
36 | void QScatterSeries::setData(QList<QPointF> data) | |
|
32 | 37 | { |
|
33 |
d->m_data |
|
|
38 | d->m_data = data; | |
|
34 | 39 | emit changed(); |
|
35 | 40 | } |
|
36 | 41 | |
|
37 | 42 | QList<QPointF> QScatterSeries::data() |
|
38 | 43 | { |
|
39 | 44 | return d->m_data; |
|
40 | 45 | } |
|
41 | 46 | |
|
42 | 47 | void QScatterSeries::setMarkerPen(QPen pen) |
|
43 | 48 | { |
|
44 | 49 | d->m_markerPen = pen; |
|
45 | 50 | } |
|
46 | 51 | |
|
47 | 52 | QPen QScatterSeries::markerPen() |
|
48 | 53 | { |
|
49 | 54 | return d->m_markerPen; |
|
50 | 55 | } |
|
51 | 56 | |
|
52 | 57 | #include "moc_qscatterseries.cpp" |
|
53 | 58 | |
|
54 | 59 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,51 +1,52 | |||
|
1 | 1 | #ifndef QSCATTERSERIES_H |
|
2 | 2 | #define QSCATTERSERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartseries.h" |
|
5 | 5 | #include <QRectF> |
|
6 | 6 | #include <QColor> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | class QScatterSeriesPrivate; |
|
10 | 10 | |
|
11 | 11 | class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries |
|
12 | 12 | { |
|
13 | 13 | Q_OBJECT |
|
14 | 14 | public: |
|
15 | 15 | //QScatterSeries(QSeriesData *data, QObject *chart); |
|
16 | 16 | QScatterSeries(QObject *parent = 0); |
|
17 | 17 | ~QScatterSeries(); |
|
18 | 18 | |
|
19 | 19 | public: // from QChartSeries |
|
20 | 20 | QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; } |
|
21 | 21 | |
|
22 | 22 | public: |
|
23 |
// TODO: the name of the function? addPoint? addData? add |
|
|
24 |
void addData(QPointF |
|
|
23 | // TODO: the name of the function? addPoint? addData? addValue? | |
|
24 | void addData(QPointF value); | |
|
25 | QScatterSeries& operator << (const QPointF &value); | |
|
25 | 26 | |
|
26 | 27 | void setData(QList<QPointF> data); |
|
27 | 28 | |
|
28 | 29 | QList<QPointF> data(); |
|
29 | 30 | |
|
30 | 31 | //TODO? void insertData(int index, QPointF data); |
|
31 | 32 | |
|
32 | 33 | void setMarkerPen(QPen pen); |
|
33 | 34 | QPen markerPen(); |
|
34 | 35 | // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot |
|
35 | 36 | //void setMarkerShape(MarkerShape shape); |
|
36 | 37 | |
|
37 | 38 | Q_SIGNALS: |
|
38 | 39 | // TODO: move to PIMPL? |
|
39 | 40 | // TODO: more finegrained signaling for performance reasons |
|
40 | 41 | void changed(); |
|
41 | 42 | |
|
42 | 43 | //public Q_SLOTS: |
|
43 | 44 | private: |
|
44 | 45 | Q_DECLARE_PRIVATE(QScatterSeries) |
|
45 | 46 | Q_DISABLE_COPY(QScatterSeries) |
|
46 | 47 | QScatterSeriesPrivate *const d; |
|
47 | 48 | }; |
|
48 | 49 | |
|
49 | 50 | QTCOMMERCIALCHART_END_NAMESPACE |
|
50 | 51 | |
|
51 | 52 | #endif // QSCATTERSERIES_H |
@@ -1,429 +1,430 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include "dataseriedialog.h" |
|
3 | 3 | #include "qchartseries.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "qscatterseries.h" |
|
6 | 6 | #include <qlinechartseries.h> |
|
7 | 7 | #include <qbarset.h> |
|
8 | 8 | #include <qbarcategory.h> |
|
9 | 9 | #include <barchartseries.h> |
|
10 | 10 | #include <stackedbarchartseries.h> |
|
11 | 11 | #include <percentbarchartseries.h> |
|
12 | 12 | #include <QPushButton> |
|
13 | 13 | #include <QComboBox> |
|
14 | 14 | #include <QSpinBox> |
|
15 | 15 | #include <QCheckBox> |
|
16 | 16 | #include <QGridLayout> |
|
17 | 17 | #include <QHBoxLayout> |
|
18 | 18 | #include <QLabel> |
|
19 | 19 | #include <QSpacerItem> |
|
20 | 20 | #include <QMessageBox> |
|
21 | 21 | #include <cmath> |
|
22 | 22 | #include <QDebug> |
|
23 | 23 | #include <QStandardItemModel> |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | MainWidget::MainWidget(QWidget *parent) : |
|
29 | 29 | QWidget(parent) |
|
30 | 30 | { |
|
31 | 31 | m_chartWidget = new QChartView(this); |
|
32 | 32 | m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand); |
|
33 | 33 | |
|
34 | 34 | // Grid layout for the controls for configuring the chart widget |
|
35 | 35 | QGridLayout *grid = new QGridLayout(); |
|
36 | 36 | QPushButton *addSeriesButton = new QPushButton("Add series"); |
|
37 | 37 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
38 | 38 | grid->addWidget(addSeriesButton, 0, 1); |
|
39 | 39 | initBackroundCombo(grid); |
|
40 | 40 | initScaleControls(grid); |
|
41 | 41 | initThemeCombo(grid); |
|
42 | 42 | QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); |
|
43 | 43 | connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); |
|
44 | 44 | zoomCheckBox->setChecked(true); |
|
45 | 45 | grid->addWidget(zoomCheckBox, grid->rowCount(), 0); |
|
46 | 46 | // add row with empty label to make all the other rows static |
|
47 | 47 | grid->addWidget(new QLabel(""), grid->rowCount(), 0); |
|
48 | 48 | grid->setRowStretch(grid->rowCount() - 1, 1); |
|
49 | 49 | |
|
50 | 50 | // Another grid layout as a main layout |
|
51 | 51 | QGridLayout *mainLayout = new QGridLayout(); |
|
52 | 52 | mainLayout->addLayout(grid, 0, 0); |
|
53 | 53 | |
|
54 | 54 | // Init series type specific controls |
|
55 | 55 | initPieControls(); |
|
56 | 56 | mainLayout->addLayout(m_pieLayout, 2, 0); |
|
57 | 57 | // Scatter series specific settings |
|
58 | 58 | // m_scatterLayout = new QGridLayout(); |
|
59 | 59 | // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); |
|
60 | 60 | // m_scatterLayout->setEnabled(false); |
|
61 | 61 | // mainLayout->addLayout(m_scatterLayout, 1, 0); |
|
62 | 62 | |
|
63 | 63 | // Add layouts and the chart widget to the main layout |
|
64 | 64 | mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); |
|
65 | 65 | setLayout(mainLayout); |
|
66 | 66 | |
|
67 | 67 | // force an update to test data |
|
68 | 68 | testDataChanged(0); |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | // Combo box for selecting the chart's background |
|
72 | 72 | void MainWidget::initBackroundCombo(QGridLayout *grid) |
|
73 | 73 | { |
|
74 | 74 | QComboBox *backgroundCombo = new QComboBox(this); |
|
75 | 75 | backgroundCombo->addItem("Color"); |
|
76 | 76 | backgroundCombo->addItem("Gradient"); |
|
77 | 77 | backgroundCombo->addItem("Image"); |
|
78 | 78 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
79 | 79 | this, SLOT(backgroundChanged(int))); |
|
80 | 80 | |
|
81 | 81 | grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0); |
|
82 | 82 | grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1); |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | // Scale related controls (auto-scale vs. manual min-max values) |
|
86 | 86 | void MainWidget::initScaleControls(QGridLayout *grid) |
|
87 | 87 | { |
|
88 | 88 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
89 | 89 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
90 | 90 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
91 | 91 | m_xMinSpin = new QSpinBox(); |
|
92 | 92 | m_xMinSpin->setMinimum(INT_MIN); |
|
93 | 93 | m_xMinSpin->setMaximum(INT_MAX); |
|
94 | 94 | m_xMinSpin->setValue(0); |
|
95 | 95 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
96 | 96 | m_xMaxSpin = new QSpinBox(); |
|
97 | 97 | m_xMaxSpin->setMinimum(INT_MIN); |
|
98 | 98 | m_xMaxSpin->setMaximum(INT_MAX); |
|
99 | 99 | m_xMaxSpin->setValue(10); |
|
100 | 100 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
101 | 101 | m_yMinSpin = new QSpinBox(); |
|
102 | 102 | m_yMinSpin->setMinimum(INT_MIN); |
|
103 | 103 | m_yMinSpin->setMaximum(INT_MAX); |
|
104 | 104 | m_yMinSpin->setValue(0); |
|
105 | 105 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
106 | 106 | m_yMaxSpin = new QSpinBox(); |
|
107 | 107 | m_yMaxSpin->setMinimum(INT_MIN); |
|
108 | 108 | m_yMaxSpin->setMaximum(INT_MAX); |
|
109 | 109 | m_yMaxSpin->setValue(10); |
|
110 | 110 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
111 | 111 | |
|
112 | 112 | grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0); |
|
113 | 113 | grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0); |
|
114 | 114 | grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1); |
|
115 | 115 | grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0); |
|
116 | 116 | grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1); |
|
117 | 117 | grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0); |
|
118 | 118 | grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1); |
|
119 | 119 | grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0); |
|
120 | 120 | grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1); |
|
121 | 121 | |
|
122 | 122 | m_autoScaleCheck->setChecked(true); |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | // Combo box for selecting theme |
|
126 | 126 | void MainWidget::initThemeCombo(QGridLayout *grid) |
|
127 | 127 | { |
|
128 | 128 | QComboBox *chartTheme = new QComboBox(); |
|
129 | 129 | chartTheme->addItem("Default"); |
|
130 | 130 | chartTheme->addItem("Vanilla"); |
|
131 | 131 | chartTheme->addItem("Icy"); |
|
132 | 132 | chartTheme->addItem("Grayscale"); |
|
133 | 133 | chartTheme->addItem("Scientific"); |
|
134 | 134 | chartTheme->addItem("Unnamed1"); |
|
135 | 135 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
136 | 136 | this, SLOT(changeChartTheme(int))); |
|
137 | 137 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); |
|
138 | 138 | grid->addWidget(chartTheme, 8, 1); |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | void MainWidget::initPieControls() |
|
142 | 142 | { |
|
143 | 143 | // Pie series specific settings |
|
144 | 144 | // Pie size factory |
|
145 | 145 | QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox(); |
|
146 | 146 | pieSizeSpin->setMinimum(LONG_MIN); |
|
147 | 147 | pieSizeSpin->setMaximum(LONG_MAX); |
|
148 | 148 | pieSizeSpin->setValue(1.0); |
|
149 | 149 | pieSizeSpin->setSingleStep(0.1); |
|
150 | 150 | connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double))); |
|
151 | 151 | // Pie position |
|
152 | 152 | QComboBox *piePosCombo = new QComboBox(this); |
|
153 | 153 | piePosCombo->addItem("Maximized"); |
|
154 | 154 | piePosCombo->addItem("Top left"); |
|
155 | 155 | piePosCombo->addItem("Top right"); |
|
156 | 156 | piePosCombo->addItem("Bottom left"); |
|
157 | 157 | piePosCombo->addItem("Bottom right"); |
|
158 | 158 | connect(piePosCombo, SIGNAL(currentIndexChanged(int)), |
|
159 | 159 | this, SLOT(setPiePosition(int))); |
|
160 | 160 | m_pieLayout = new QGridLayout(); |
|
161 | 161 | m_pieLayout->setEnabled(false); |
|
162 | 162 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); |
|
163 | 163 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); |
|
164 | 164 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); |
|
165 | 165 | m_pieLayout->addWidget(piePosCombo, 1, 1); |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | void MainWidget::addSeries() |
|
169 | 169 | { |
|
170 | 170 | DataSerieDialog dialog(m_defaultSeriesName, this); |
|
171 | 171 | connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString))); |
|
172 | 172 | dialog.exec(); |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | void MainWidget::addSeries(QString series, QString data) |
|
176 | 176 | { |
|
177 | 177 | qDebug() << "addSeries: " << series << " data: " << data; |
|
178 | 178 | m_defaultSeriesName = series; |
|
179 | 179 | |
|
180 | 180 | // TODO: a dedicated data class for storing x and y values |
|
181 | 181 | QList<qreal> x; |
|
182 | 182 | QList<qreal> y; |
|
183 | 183 | |
|
184 | 184 | QBarSet *set0 = new QBarSet; |
|
185 | 185 | QBarSet *set1 = new QBarSet; |
|
186 | 186 | QBarSet *set2 = new QBarSet; |
|
187 | 187 | QBarSet *set3 = new QBarSet; |
|
188 | 188 | QBarSet *set4 = new QBarSet; |
|
189 | 189 | |
|
190 | 190 | if (data == "linear") { |
|
191 | 191 | for (int i = 0; i < 20; i++) { |
|
192 | 192 | x.append(i); |
|
193 | 193 | y.append(i); |
|
194 | 194 | } |
|
195 | 195 | } else if (data == "linear, 1M") { |
|
196 | 196 | // 1 million data points from 0.0001 to 100 |
|
197 | 197 | // TODO: What is the requirement? Should we be able to show this kind of data with |
|
198 | 198 | // reasonable performance, or can we expect the application developer to do "data mining" |
|
199 | 199 | // for us, so that the count of data points given to QtCommercial Chart is always |
|
200 | 200 | // reasonable? |
|
201 | 201 | for (qreal i = 0; i < 100; i += 0.0001) { |
|
202 | 202 | x.append(i); |
|
203 | 203 | y.append(20); |
|
204 | 204 | } |
|
205 | 205 | } else if (data == "SIN") { |
|
206 | 206 | for (int i = 0; i < 100; i++) { |
|
207 | 207 | x.append(i); |
|
208 | 208 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100)); |
|
209 | 209 | } |
|
210 | 210 | } else if (data == "SIN + random") { |
|
211 | 211 | for (qreal i = 0; i < 100; i += 0.1) { |
|
212 | 212 | x.append(i + (rand() % 5)); |
|
213 | 213 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); |
|
214 | 214 | } |
|
215 | 215 | } else if (data == "Table, 5 series"){ |
|
216 | 216 | // Create some test data to chart |
|
217 | 217 | *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; |
|
218 | 218 | *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; |
|
219 | 219 | *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; |
|
220 | 220 | *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; |
|
221 | 221 | *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; |
|
222 | 222 | } else { |
|
223 | 223 | // TODO: check if data has a valid file name |
|
224 | 224 | Q_ASSERT(false); |
|
225 | 225 | } |
|
226 | 226 | |
|
227 | 227 | // TODO: color of the series |
|
228 | 228 | QChartSeries *newSeries = 0; |
|
229 | 229 | if (series == "Scatter") { |
|
230 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter); | |
|
231 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(newSeries); | |
|
232 | Q_ASSERT(scatterSeries); | |
|
230 | QScatterSeries *scatter = new QScatterSeries(); | |
|
233 | 231 | for (int i(0); i < x.count() && i < y.count(); i++) |
|
234 |
|
|
|
232 | (*scatter) << QPointF(x.at(i), y.at(i)); | |
|
233 | m_chartWidget->addSeries(scatter); | |
|
235 | 234 | } else if (series == "Pie") { |
|
236 | 235 | newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie); |
|
237 | 236 | Q_ASSERT(newSeries->setData(y)); |
|
238 | 237 | } else if (series == "Line") { |
|
239 | 238 | // TODO: adding data to an existing line series does not give any visuals for some reason |
|
240 | 239 | // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine); |
|
241 | 240 | // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries); |
|
242 | 241 | // lineSeries->setColor(Qt::blue); |
|
243 | 242 | // for (int i(0); i < x.count() && i < y.count(); i++) { |
|
244 | 243 | // lineSeries->add(x.at(i), y.at(i)); |
|
245 | 244 | // } |
|
246 | 245 | //Q_ASSERT(newSeries->setData(x, y)); |
|
247 | 246 | QLineChartSeries* series0 = new QLineChartSeries(); |
|
248 | 247 | for (int i(0); i < x.count() && i < y.count(); i++) |
|
249 | 248 | series0->add(x.at(i), y.at(i)); |
|
250 | 249 | m_chartWidget->addSeries(series0); |
|
251 | 250 | newSeries = series0; |
|
252 | 251 | } else if (series == "Bar") { |
|
253 | 252 | qDebug() << "Bar chart series"; |
|
254 | 253 | |
|
255 | 254 | QBarCategory *category = new QBarCategory; |
|
256 | 255 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
257 | 256 | |
|
258 | 257 | BarChartSeries* series0 = new BarChartSeries(category, this); |
|
259 | 258 | |
|
260 | 259 | series0->addBarSet(set0); |
|
261 | 260 | series0->addBarSet(set1); |
|
262 | 261 | series0->addBarSet(set2); |
|
263 | 262 | series0->addBarSet(set3); |
|
264 | 263 | series0->addBarSet(set4); |
|
265 | 264 | |
|
266 | 265 | m_chartWidget->addSeries(series0); |
|
267 | 266 | newSeries = series0; |
|
268 | 267 | } else if (series == "StackedBar") { |
|
269 | 268 | qDebug() << "Stacked bar chart series"; |
|
270 | 269 | |
|
271 | 270 | QBarCategory *category = new QBarCategory; |
|
272 | 271 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
273 | 272 | |
|
274 | 273 | StackedBarChartSeries* series0 = new StackedBarChartSeries(category, this); |
|
275 | 274 | |
|
276 | 275 | series0->addBarSet(set0); |
|
277 | 276 | series0->addBarSet(set1); |
|
278 | 277 | series0->addBarSet(set2); |
|
279 | 278 | series0->addBarSet(set3); |
|
280 | 279 | series0->addBarSet(set4); |
|
281 | 280 | |
|
282 | 281 | m_chartWidget->addSeries(series0); |
|
283 | 282 | newSeries = series0; |
|
284 | 283 | } else if (series == "PercentBar") { |
|
285 | 284 | qDebug() << "Percent bar chart series"; |
|
286 | 285 | |
|
287 | 286 | QBarCategory *category = new QBarCategory; |
|
288 | 287 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
289 | 288 | |
|
290 | 289 | PercentBarChartSeries* series0 = new PercentBarChartSeries(category, this); |
|
291 | 290 | |
|
292 | 291 | series0->addBarSet(set0); |
|
293 | 292 | series0->addBarSet(set1); |
|
294 | 293 | series0->addBarSet(set2); |
|
295 | 294 | series0->addBarSet(set3); |
|
296 | 295 | series0->addBarSet(set4); |
|
297 | 296 | |
|
298 | 297 | m_chartWidget->addSeries(series0); |
|
299 | 298 | newSeries = series0; |
|
300 | 299 | } else { |
|
301 | 300 | qDebug() << "Something weird going on in MainWidget::addSeries"; |
|
302 | 301 | } |
|
303 | 302 | |
|
304 | 303 | setCurrentSeries(newSeries); |
|
305 | 304 | } |
|
306 | 305 | |
|
307 | 306 | void MainWidget::setCurrentSeries(QChartSeries *series) |
|
308 | 307 | { |
|
309 | m_currentSeries = series; | |
|
310 | switch (m_currentSeries->type()) { | |
|
311 | case QChartSeries::SeriesTypeLine: | |
|
312 | break; | |
|
313 | case QChartSeries::SeriesTypeScatter: | |
|
314 | break; | |
|
315 | case QChartSeries::SeriesTypePie: | |
|
316 | break; | |
|
317 | case QChartSeries::SeriesTypeBar: | |
|
318 | qDebug() << "setCurrentSeries (bar)"; | |
|
319 | break; | |
|
320 | case QChartSeries::SeriesTypeStackedBar: | |
|
321 | qDebug() << "setCurrentSeries (Stackedbar)"; | |
|
322 | break; | |
|
323 | case QChartSeries::SeriesTypePercentBar: | |
|
324 | qDebug() << "setCurrentSeries (Percentbar)"; | |
|
325 | break; | |
|
326 | default: | |
|
327 | Q_ASSERT(false); | |
|
328 | break; | |
|
308 | if (series) { | |
|
309 | m_currentSeries = series; | |
|
310 | switch (m_currentSeries->type()) { | |
|
311 | case QChartSeries::SeriesTypeLine: | |
|
312 | break; | |
|
313 | case QChartSeries::SeriesTypeScatter: | |
|
314 | break; | |
|
315 | case QChartSeries::SeriesTypePie: | |
|
316 | break; | |
|
317 | case QChartSeries::SeriesTypeBar: | |
|
318 | qDebug() << "setCurrentSeries (bar)"; | |
|
319 | break; | |
|
320 | case QChartSeries::SeriesTypeStackedBar: | |
|
321 | qDebug() << "setCurrentSeries (Stackedbar)"; | |
|
322 | break; | |
|
323 | case QChartSeries::SeriesTypePercentBar: | |
|
324 | qDebug() << "setCurrentSeries (Percentbar)"; | |
|
325 | break; | |
|
326 | default: | |
|
327 | Q_ASSERT(false); | |
|
328 | break; | |
|
329 | } | |
|
329 | 330 | } |
|
330 | 331 | } |
|
331 | 332 | |
|
332 | 333 | void MainWidget::testDataChanged(int itemIndex) |
|
333 | 334 | { |
|
334 | 335 | qDebug() << "testDataChanged: " << itemIndex; |
|
335 | 336 | |
|
336 | 337 | // switch (itemIndex) { |
|
337 | 338 | // case 0: { |
|
338 | 339 | // QList<QChartDataPoint> data; |
|
339 | 340 | // for (int x = 0; x < 20; x++) { |
|
340 | 341 | // data.append(QChartDataPoint() << x << x / 2); |
|
341 | 342 | // } |
|
342 | 343 | // m_chartWidget->setData(data); |
|
343 | 344 | // break; |
|
344 | 345 | // } |
|
345 | 346 | // case 1: { |
|
346 | 347 | // QList<QChartDataPoint> data; |
|
347 | 348 | // for (int x = 0; x < 100; x++) { |
|
348 | 349 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); |
|
349 | 350 | // } |
|
350 | 351 | // m_chartWidget->setData(data); |
|
351 | 352 | // break; |
|
352 | 353 | // } |
|
353 | 354 | // case 2: { |
|
354 | 355 | // QList<QChartDataPoint> data; |
|
355 | 356 | // for (int x = 0; x < 1000; x++) { |
|
356 | 357 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
357 | 358 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
358 | 359 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); |
|
359 | 360 | // } |
|
360 | 361 | // m_chartWidget->setData(data); |
|
361 | 362 | // break; |
|
362 | 363 | // } |
|
363 | 364 | // default: |
|
364 | 365 | // break; |
|
365 | 366 | // } |
|
366 | 367 | } |
|
367 | 368 | |
|
368 | 369 | void MainWidget::backgroundChanged(int itemIndex) |
|
369 | 370 | { |
|
370 | 371 | qDebug() << "backgroundChanged: " << itemIndex; |
|
371 | 372 | } |
|
372 | 373 | |
|
373 | 374 | void MainWidget::autoScaleChanged(int value) |
|
374 | 375 | { |
|
375 | 376 | if (value) { |
|
376 | 377 | // TODO: enable auto scaling |
|
377 | 378 | } else { |
|
378 | 379 | // TODO: set scaling manually (and disable auto scaling) |
|
379 | 380 | } |
|
380 | 381 | |
|
381 | 382 | m_xMinSpin->setEnabled(!value); |
|
382 | 383 | m_xMaxSpin->setEnabled(!value); |
|
383 | 384 | m_yMinSpin->setEnabled(!value); |
|
384 | 385 | m_yMaxSpin->setEnabled(!value); |
|
385 | 386 | } |
|
386 | 387 | |
|
387 | 388 | void MainWidget::xMinChanged(int value) |
|
388 | 389 | { |
|
389 | 390 | qDebug() << "xMinChanged: " << value; |
|
390 | 391 | } |
|
391 | 392 | |
|
392 | 393 | void MainWidget::xMaxChanged(int value) |
|
393 | 394 | { |
|
394 | 395 | qDebug() << "xMaxChanged: " << value; |
|
395 | 396 | } |
|
396 | 397 | |
|
397 | 398 | void MainWidget::yMinChanged(int value) |
|
398 | 399 | { |
|
399 | 400 | qDebug() << "yMinChanged: " << value; |
|
400 | 401 | } |
|
401 | 402 | |
|
402 | 403 | void MainWidget::yMaxChanged(int value) |
|
403 | 404 | { |
|
404 | 405 | qDebug() << "yMaxChanged: " << value; |
|
405 | 406 | } |
|
406 | 407 | |
|
407 | 408 | void MainWidget::changeChartTheme(int themeIndex) |
|
408 | 409 | { |
|
409 | 410 | qDebug() << "changeChartTheme: " << themeIndex; |
|
410 | 411 | m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex); |
|
411 | 412 | //TODO: remove this hack. This is just to make it so that theme change is seen immediately. |
|
412 | 413 | QSize s = size(); |
|
413 | 414 | s.setWidth(s.width()+1); |
|
414 | 415 | resize(s); |
|
415 | 416 | } |
|
416 | 417 | |
|
417 | 418 | void MainWidget::setPieSizeFactor(double size) |
|
418 | 419 | { |
|
419 | 420 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
420 | 421 | if (pie) |
|
421 | 422 | pie->setSizeFactor(qreal(size)); |
|
422 | 423 | } |
|
423 | 424 | |
|
424 | 425 | void MainWidget::setPiePosition(int position) |
|
425 | 426 | { |
|
426 | 427 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
427 | 428 | if (pie) |
|
428 | 429 | pie->setPosition((QPieSeries::PiePosition) position); |
|
429 | 430 | } |
General Comments 0
You need to be logged in to leave comments.
Login now