@@ -13,6 +13,7 int main(int argc, char *argv[]) | |||
|
13 | 13 | // Create chart widget |
|
14 | 14 | QChartView *chartView = new QChartView(); |
|
15 | 15 | |
|
16 | //! [1] | |
|
16 | 17 | // Add scatter series with simple test data |
|
17 | 18 | QScatterSeries *scatter = new QScatterSeries(); |
|
18 | 19 | *scatter << QPointF(0.5, 5.0) |
@@ -24,6 +25,7 int main(int argc, char *argv[]) | |||
|
24 | 25 | << QPointF(2.5, 5.0); |
|
25 | 26 | // Chart takes ownership |
|
26 | 27 | chartView->addSeries(scatter); |
|
28 | //! [1] | |
|
27 | 29 | |
|
28 | 30 | // Add another scatter series |
|
29 | 31 | // - more data with random component |
@@ -167,12 +167,12 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, i | |||
|
167 | 167 | |
|
168 | 168 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
169 | 169 | // TODO: define alpha in the theme? or in the series? |
|
170 | color.setAlpha(120); | |
|
170 | //color.setAlpha(120); | |
|
171 | 171 | |
|
172 | 172 | QBrush brush(color, Qt::SolidPattern); |
|
173 | 173 | presenter->m_markerBrush = brush; |
|
174 | 174 | |
|
175 |
QPen pen(brush, |
|
|
175 | QPen pen(brush, 3); | |
|
176 | 176 | pen.setColor(color); |
|
177 | 177 | presenter->m_markerPen = pen; |
|
178 | 178 | } |
@@ -66,7 +66,7 bool QPieSeries::setData(QList<qreal> data) | |||
|
66 | 66 | { |
|
67 | 67 | // TODO: remove this function |
|
68 | 68 | QList<QPieSlice*> slices; |
|
69 |
foreach ( |
|
|
69 | foreach (qreal value, data) | |
|
70 | 70 | slices << new QPieSlice(value, QString::number(value)); |
|
71 | 71 | set(slices); |
|
72 | 72 | return true; |
@@ -33,6 +33,7 QScatterSeriesPrivate::QScatterSeriesPrivate() : | |||
|
33 | 33 | \class QScatterSeries |
|
34 | 34 | \brief QtCommercial Chart series API for showing scatter series. |
|
35 | 35 | |
|
36 | \snippet ../../example/scatter/main.cpp 1 | |
|
36 | 37 | Example on how to create a chart with scatter series: |
|
37 | 38 | \code |
|
38 | 39 | #include <qchartglobal.h> |
@@ -4,6 +4,7 | |||
|
4 | 4 | #include <QPainter> |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | #include <QDebug> |
|
7 | #include <QTime> | |
|
7 | 8 | |
|
8 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 10 | |
@@ -45,21 +46,61 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem * | |||
|
45 | 46 | { |
|
46 | 47 | // TODO: Optimization: avoid setting on every paint method call? |
|
47 | 48 | // The custom settings in series override those defined by the theme |
|
48 | if (m_series->markerPen().color().isValid()) | |
|
49 | painter->setPen(m_series->markerPen()); | |
|
50 | else | |
|
51 | painter->setPen(m_markerPen); | |
|
49 | int shape = m_series->markerShape(); | |
|
50 | ||
|
51 | painter->save(); | |
|
52 | painter->setClipRect(m_boundingRect); | |
|
53 | ||
|
54 | // Paint dropshadow | |
|
55 | QPen dropShadowPen(QColor(0, 0, 0, 70)); | |
|
56 | dropShadowPen.setWidth(3); | |
|
57 | painter->setPen(dropShadowPen); | |
|
58 | painter->setBrush(Qt::NoBrush); | |
|
59 | painter->setRenderHint(QPainter::Antialiasing); | |
|
60 | for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { | |
|
61 | if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i)) | |
|
62 | switch (shape) { | |
|
63 | case QScatterSeries::MarkerShapeDefault: | |
|
64 | // Fallthrough, defaults to circle | |
|
65 | case QScatterSeries::MarkerShapeCircle: | |
|
66 | painter->drawChord(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 9, 9, 0, 5760); | |
|
67 | break; | |
|
68 | case QScatterSeries::MarkerShapePoint: | |
|
69 | //painter->drawPoint(m_scenex.at(i), m_sceney.at(i)); | |
|
70 | break; | |
|
71 | case QScatterSeries::MarkerShapeRectangle: | |
|
72 | painter->drawRect(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 8, 8); | |
|
73 | break; | |
|
74 | case QScatterSeries::MarkerShapeTiltedRectangle: { | |
|
75 | // TODO: | |
|
76 | static const QPointF points[4] = { | |
|
77 | QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)), | |
|
78 | QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)), | |
|
79 | QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)), | |
|
80 | QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i)) | |
|
81 | }; | |
|
82 | painter->drawPolygon(points, 4); | |
|
83 | break; | |
|
84 | } | |
|
85 | default: | |
|
86 | // TODO: implement the rest of the shapes | |
|
87 | Q_ASSERT(false); | |
|
88 | break; | |
|
89 | } | |
|
90 | } | |
|
52 | 91 | |
|
92 | // Paint the shape | |
|
93 | QPen pen = m_markerPen; | |
|
94 | if (m_series->markerPen().color().isValid()) | |
|
95 | pen = m_series->markerPen(); | |
|
53 | 96 | if (m_series->markerBrush().color().isValid()) |
|
54 | 97 | painter->setBrush(m_series->markerBrush()); |
|
55 | 98 | else |
|
56 | 99 | painter->setBrush(m_markerBrush); |
|
57 | ||
|
58 | int shape = m_series->markerShape(); | |
|
59 | ||
|
100 | painter->setPen(pen); | |
|
101 | painter->setRenderHint(QPainter::Antialiasing, false); | |
|
60 | 102 | for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) { |
|
61 | 103 | if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i)) |
|
62 | // Paint a shape | |
|
63 | 104 | switch (shape) { |
|
64 | 105 | case QScatterSeries::MarkerShapeDefault: |
|
65 | 106 | // Fallthrough, defaults to circle |
@@ -72,7 +113,7 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem * | |||
|
72 | 113 | case QScatterSeries::MarkerShapeRectangle: |
|
73 | 114 | painter->drawRect(m_scenex.at(i), m_sceney.at(i), 9, 9); |
|
74 | 115 | break; |
|
75 | case QScatterSeries::MarkerShapeTiltedRectangle: | |
|
116 | case QScatterSeries::MarkerShapeTiltedRectangle: { | |
|
76 | 117 | // TODO: |
|
77 | 118 | static const QPointF points[4] = { |
|
78 | 119 | QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)), |
@@ -82,12 +123,15 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem * | |||
|
82 | 123 | }; |
|
83 | 124 | painter->drawPolygon(points, 4); |
|
84 | 125 | break; |
|
126 | } | |
|
85 | 127 | default: |
|
86 | 128 | // TODO: implement the rest of the shapes |
|
87 | 129 | Q_ASSERT(false); |
|
88 | 130 | break; |
|
89 | 131 | } |
|
90 | 132 | } |
|
133 | ||
|
134 | painter->restore(); | |
|
91 | 135 | } |
|
92 | 136 | |
|
93 | 137 | void ScatterPresenter::changeGeometry() |
@@ -1,42 +1,16 | |||
|
1 | 1 | #include "dataseriedialog.h" |
|
2 | 2 | #include <QDialogButtonBox> |
|
3 | 3 | #include <QGridLayout> |
|
4 |
#include <QC |
|
|
4 | #include <QCheckbox> | |
|
5 | 5 | #include <QPushButton> |
|
6 | #include <QGroupBox> | |
|
7 | #include <QRadioButton> | |
|
6 | 8 | #include <QLabel> |
|
7 | 9 | #include <QDebug> |
|
8 | 10 | |
|
9 | 11 | DataSerieDialog::DataSerieDialog(QString defaultType, QWidget *parent) : |
|
10 | 12 | QDialog(parent) |
|
11 | 13 | { |
|
12 | // Combo box for selecting the series type | |
|
13 | m_seriesTypeCombo = new QComboBox(this); | |
|
14 | m_seriesTypeCombo->addItem("Line"); | |
|
15 | m_seriesTypeCombo->addItem("Area"); | |
|
16 | m_seriesTypeCombo->addItem("Bar"); | |
|
17 | m_seriesTypeCombo->addItem("StackedBar"); | |
|
18 | m_seriesTypeCombo->addItem("PercentBar"); | |
|
19 | m_seriesTypeCombo->addItem("Pie"); | |
|
20 | m_seriesTypeCombo->addItem("Scatter"); | |
|
21 | m_seriesTypeCombo->addItem("Spline"); | |
|
22 | ||
|
23 | // Allow pre-selection of a series type | |
|
24 | if (defaultType != "") { | |
|
25 | int index = m_seriesTypeCombo->findText(defaultType); | |
|
26 | if (index > 0) | |
|
27 | m_seriesTypeCombo->setCurrentIndex(index); | |
|
28 | } | |
|
29 | ||
|
30 | // Combo box for selecting data for the new series | |
|
31 | m_testDataCombo = new QComboBox(this); | |
|
32 | m_testDataCombo->addItem("linear"); | |
|
33 | m_testDataCombo->addItem("linear, 1M"); | |
|
34 | m_testDataCombo->addItem("SIN"); | |
|
35 | m_testDataCombo->addItem("SIN + random"); | |
|
36 | m_testDataCombo->addItem("Table, 5 series"); | |
|
37 | m_testDataCombo->addItem("TODO From file..."); | |
|
38 | m_testDataCombo->addItem("TODO From URL..."); | |
|
39 | ||
|
40 | 14 | QDialogButtonBox *addSeriesBox = new QDialogButtonBox(Qt::Horizontal); |
|
41 | 15 | QPushButton *b = addSeriesBox->addButton(QDialogButtonBox::Ok); |
|
42 | 16 | connect(b, SIGNAL(clicked()), this, SLOT(accept())); |
@@ -44,22 +18,132 DataSerieDialog::DataSerieDialog(QString defaultType, QWidget *parent) : | |||
|
44 | 18 | connect(b, SIGNAL(clicked()), this, SLOT(reject())); |
|
45 | 19 | |
|
46 | 20 | QGridLayout *grid = new QGridLayout(); |
|
47 | grid->addWidget(new QLabel("Series type:"), 0, 0); | |
|
48 | grid->addWidget(m_seriesTypeCombo, 0, 1); | |
|
49 | grid->addWidget(new QLabel("Data:"), 1, 0); | |
|
50 | grid->addWidget(m_testDataCombo, 1, 1); | |
|
51 | grid->addWidget(addSeriesBox, 2, 1); | |
|
21 | ||
|
22 | m_seriesTypeSelector = seriesTypeSelector(); | |
|
23 | m_columnCountSelector = columnCountSelector(); | |
|
24 | m_rowCountSelector = rowCountSelector(); | |
|
25 | m_dataCharacteristicsSelector = dataCharacteristicsSelector(); | |
|
26 | ||
|
27 | grid->addWidget(m_seriesTypeSelector, 0, 0); | |
|
28 | grid->addWidget(m_columnCountSelector, 0, 1); | |
|
29 | grid->addWidget(m_rowCountSelector, 1, 1); | |
|
30 | grid->addWidget(m_dataCharacteristicsSelector, 1, 0); | |
|
31 | m_labelsSelector = new QCheckBox("Labels defined"); | |
|
32 | m_labelsSelector->setChecked(true); | |
|
33 | grid->addWidget(m_labelsSelector, 2, 0); | |
|
34 | grid->addWidget(addSeriesBox, 3, 1); | |
|
52 | 35 | |
|
53 | 36 | setLayout(grid); |
|
54 | 37 | } |
|
55 | 38 | |
|
39 | QGroupBox *DataSerieDialog::seriesTypeSelector() | |
|
40 | { | |
|
41 | QVBoxLayout *layout = new QVBoxLayout(); | |
|
42 | ||
|
43 | QRadioButton *line = new QRadioButton("&Line"); | |
|
44 | QRadioButton *area = new QRadioButton("&Area"); | |
|
45 | QRadioButton *pie = new QRadioButton("&Pie"); | |
|
46 | QRadioButton *bar = new QRadioButton("&Bar"); | |
|
47 | QRadioButton *scatter = new QRadioButton("&Scatter"); | |
|
48 | QRadioButton *spline = new QRadioButton("Spl&ine"); | |
|
49 | line->setChecked(true); | |
|
50 | ||
|
51 | layout->addWidget(line); | |
|
52 | layout->addWidget(area); | |
|
53 | layout->addWidget(pie); | |
|
54 | layout->addWidget(bar); | |
|
55 | layout->addWidget(scatter); | |
|
56 | layout->addWidget(spline); | |
|
57 | ||
|
58 | QGroupBox *groupBox = new QGroupBox("Series type"); | |
|
59 | groupBox->setLayout(layout); | |
|
60 | ||
|
61 | return groupBox; | |
|
62 | } | |
|
63 | ||
|
64 | QGroupBox *DataSerieDialog::columnCountSelector() | |
|
65 | { | |
|
66 | QVBoxLayout *layout = new QVBoxLayout(); | |
|
67 | ||
|
68 | QRadioButton *radio = new QRadioButton("1"); | |
|
69 | radio->setChecked(true); | |
|
70 | layout->addWidget(radio); | |
|
71 | layout->addWidget(new QRadioButton("2")); | |
|
72 | layout->addWidget(new QRadioButton("3")); | |
|
73 | layout->addWidget(new QRadioButton("5")); | |
|
74 | layout->addWidget(new QRadioButton("10")); | |
|
75 | layout->addWidget(new QRadioButton("100")); | |
|
76 | ||
|
77 | QGroupBox *groupBox = new QGroupBox("Column count"); | |
|
78 | groupBox->setLayout(layout); | |
|
79 | ||
|
80 | return groupBox; | |
|
81 | } | |
|
82 | ||
|
83 | QGroupBox *DataSerieDialog::rowCountSelector() | |
|
84 | { | |
|
85 | QVBoxLayout *layout = new QVBoxLayout(); | |
|
86 | ||
|
87 | layout->addWidget(new QRadioButton("1")); | |
|
88 | QRadioButton *radio = new QRadioButton("10"); | |
|
89 | radio->setChecked(true); | |
|
90 | layout->addWidget(radio); | |
|
91 | layout->addWidget(new QRadioButton("50")); | |
|
92 | layout->addWidget(new QRadioButton("100")); | |
|
93 | layout->addWidget(new QRadioButton("10000")); | |
|
94 | layout->addWidget(new QRadioButton("1000000")); | |
|
95 | ||
|
96 | QGroupBox *groupBox = new QGroupBox("Row count"); | |
|
97 | groupBox->setLayout(layout); | |
|
98 | ||
|
99 | return groupBox; | |
|
100 | } | |
|
101 | ||
|
102 | QGroupBox *DataSerieDialog::dataCharacteristicsSelector() | |
|
103 | { | |
|
104 | QVBoxLayout *layout = new QVBoxLayout(); | |
|
105 | ||
|
106 | QRadioButton *radio1 = new QRadioButton("Linear"); | |
|
107 | radio1->setChecked(true); | |
|
108 | layout->addWidget(radio1); | |
|
109 | layout->addWidget(new QRadioButton("Constant")); | |
|
110 | layout->addWidget(new QRadioButton("Random")); | |
|
111 | layout->addWidget(new QRadioButton("Sin")); | |
|
112 | layout->addWidget(new QRadioButton("Sin + random")); | |
|
113 | ||
|
114 | QGroupBox *groupBox = new QGroupBox("Data Characteristics"); | |
|
115 | groupBox->setLayout(layout); | |
|
116 | ||
|
117 | return groupBox; | |
|
118 | } | |
|
119 | ||
|
56 | 120 | void DataSerieDialog::accept() |
|
57 | 121 | { |
|
58 | accepted(m_seriesTypeCombo->currentText(), m_testDataCombo->currentText()); | |
|
122 | accepted(radioSelection(m_seriesTypeSelector), | |
|
123 | radioSelection(m_columnCountSelector).toInt(), | |
|
124 | radioSelection(m_rowCountSelector).toInt(), | |
|
125 | radioSelection(m_dataCharacteristicsSelector), | |
|
126 | m_labelsSelector->isChecked()); | |
|
59 | 127 | QDialog::accept(); |
|
60 | 128 | } |
|
61 | //void DataSerieDialog::addSeries(QAbstractButton *button) | |
|
62 | //{ | |
|
63 | // addSeries(button->text().toAscii()); | |
|
64 | // accept(); | |
|
65 | //} | |
|
129 | ||
|
130 | QString DataSerieDialog::radioSelection(QGroupBox *groupBox) | |
|
131 | { | |
|
132 | QString selection; | |
|
133 | QVBoxLayout *layout = qobject_cast<QVBoxLayout *>(groupBox->layout()); | |
|
134 | Q_ASSERT(layout); | |
|
135 | ||
|
136 | for (int i(0); i < layout->count(); i++) { | |
|
137 | QLayoutItem *item = layout->itemAt(i); | |
|
138 | Q_ASSERT(item); | |
|
139 | QRadioButton *radio = qobject_cast<QRadioButton *>(item->widget()); | |
|
140 | Q_ASSERT(radio); | |
|
141 | if (radio->isChecked()) { | |
|
142 | selection = radio->text(); | |
|
143 | break; | |
|
144 | } | |
|
145 | } | |
|
146 | ||
|
147 | qDebug() << "radioSelection: " << selection; | |
|
148 | return selection; | |
|
149 | } |
@@ -3,7 +3,8 | |||
|
3 | 3 | |
|
4 | 4 | #include <QDialog> |
|
5 | 5 | |
|
6 |
class Q |
|
|
6 | class QGroupBox; | |
|
7 | class QCheckBox; | |
|
7 | 8 | |
|
8 | 9 | class DataSerieDialog : public QDialog |
|
9 | 10 | { |
@@ -12,14 +13,22 public: | |||
|
12 | 13 | explicit DataSerieDialog(QString defaultType, QWidget *parent = 0); |
|
13 | 14 | |
|
14 | 15 | signals: |
|
15 | void accepted(QString series, QString data); | |
|
16 | void accepted(QString series, int columnCount, int rowCount, QString dataCharacteristics, bool labelsDefined); | |
|
16 | 17 | |
|
17 | 18 | public slots: |
|
18 | 19 | void accept(); |
|
19 | 20 | |
|
20 | 21 | private: |
|
21 |
Q |
|
|
22 | QComboBox *m_testDataCombo; | |
|
22 | QGroupBox *seriesTypeSelector(); | |
|
23 | QGroupBox *columnCountSelector(); | |
|
24 | QGroupBox *rowCountSelector(); | |
|
25 | QGroupBox *dataCharacteristicsSelector(); | |
|
26 | QString radioSelection(QGroupBox *groupBox); | |
|
27 | QGroupBox *m_seriesTypeSelector; | |
|
28 | QGroupBox *m_columnCountSelector; | |
|
29 | QGroupBox *m_rowCountSelector; | |
|
30 | QCheckBox *m_labelsSelector; | |
|
31 | QGroupBox *m_dataCharacteristicsSelector; | |
|
23 | 32 | }; |
|
24 | 33 | |
|
25 | 34 | #endif // DATASERIEDIALOG_H |
@@ -63,9 +63,6 MainWidget::MainWidget(QWidget *parent) : | |||
|
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 | ||
|
67 | // force an update to test data | |
|
68 | testDataChanged(0); | |
|
69 | 66 | } |
|
70 | 67 | |
|
71 | 68 | // Combo box for selecting the chart's background |
@@ -168,140 +165,91 void MainWidget::initPieControls() | |||
|
168 | 165 | void MainWidget::addSeries() |
|
169 | 166 | { |
|
170 | 167 | DataSerieDialog dialog(m_defaultSeriesName, this); |
|
171 |
connect(&dialog, SIGNAL(accepted(QString, QString)), |
|
|
168 | connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)), | |
|
169 | this, SLOT(addSeries(QString, int, int, QString, bool))); | |
|
172 | 170 | dialog.exec(); |
|
173 | 171 | } |
|
174 | 172 | |
|
175 | void MainWidget::addSeries(QString series, QString data) | |
|
173 | QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics) | |
|
176 | 174 | { |
|
177 | qDebug() << "addSeries: " << series << " data: " << data; | |
|
178 | m_defaultSeriesName = series; | |
|
179 | ||
|
180 | // TODO: a dedicated data class for storing x and y values | |
|
181 | QList<qreal> x; | |
|
182 | QList<qreal> y; | |
|
183 | ||
|
184 | QBarSet *set0 = new QBarSet; | |
|
185 | QBarSet *set1 = new QBarSet; | |
|
186 | QBarSet *set2 = new QBarSet; | |
|
187 | QBarSet *set3 = new QBarSet; | |
|
188 | QBarSet *set4 = new QBarSet; | |
|
189 | ||
|
190 | if (data == "linear") { | |
|
191 | for (int i = 0; i < 20; i++) { | |
|
192 | x.append(i); | |
|
193 |
|
|
|
175 | // TODO: dataCharacteristics | |
|
176 | QList<RealList> testData; | |
|
177 | for (int j(0); j < columnCount; j++) { | |
|
178 | QList <qreal> newColumn; | |
|
179 | for (int i(0); i < rowCount; i++) { | |
|
180 | if (dataCharacteristics == "Sin") { | |
|
181 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100)); | |
|
182 | } else if (dataCharacteristics == "Sin + random") { | |
|
183 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); | |
|
184 | } else if (dataCharacteristics == "Random") { | |
|
185 | newColumn.append(rand() % 5); | |
|
186 | } else if (dataCharacteristics == "Linear") { | |
|
187 | //newColumn.append(i * (j + 1.0)); | |
|
188 | // TODO: temporary hack to make pie work; prevent zero values: | |
|
189 | newColumn.append(i * (j + 1.0) + 0.1); | |
|
190 | } else { // "constant" | |
|
191 | newColumn.append((j + 1.0)); | |
|
192 | } | |
|
194 | 193 | } |
|
195 | } else if (data == "linear, 1M") { | |
|
196 | // 1 million data points from 0.0001 to 100 | |
|
197 | // TODO: What is the requirement? Should we be able to show this kind of data with | |
|
198 | // reasonable performance, or can we expect the application developer to do "data mining" | |
|
199 | // for us, so that the count of data points given to QtCommercial Chart is always | |
|
200 | // reasonable? | |
|
201 | for (qreal i = 0; i < 100; i += 0.0001) { | |
|
202 | x.append(i); | |
|
203 | y.append(20); | |
|
194 | testData.append(newColumn); | |
|
195 | } | |
|
196 | return testData; | |
|
197 | } | |
|
198 | ||
|
199 | QStringList MainWidget::generateLabels(int count) | |
|
200 | { | |
|
201 | QStringList result; | |
|
202 | for (int i(0); i < count; i++) | |
|
203 | result.append("label" + QString::number(i)); | |
|
204 | return result; | |
|
205 | } | |
|
206 | ||
|
207 | void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled) | |
|
208 | { | |
|
209 | qDebug() << "addSeries: " << seriesName | |
|
210 | << " columnCount: " << columnCount | |
|
211 | << " rowCount: " << rowCount | |
|
212 | << " dataCharacteristics: " << dataCharacteristics | |
|
213 | << " labels enabled: " << labelsEnabled; | |
|
214 | m_defaultSeriesName = seriesName; | |
|
215 | ||
|
216 | QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics); | |
|
217 | ||
|
218 | // Line series and scatter series use similar data | |
|
219 | if (seriesName.contains("line", Qt::CaseInsensitive)) { | |
|
220 | for (int j(0); j < data.count(); j ++) { | |
|
221 | QList<qreal> column = data.at(j); | |
|
222 | QLineChartSeries *series = new QLineChartSeries(); | |
|
223 | for (int i(0); i < column.count(); i++) { | |
|
224 | series->add(i, column.at(i)); | |
|
225 | } | |
|
226 | m_chartWidget->addSeries(series); | |
|
227 | setCurrentSeries(series); | |
|
204 | 228 | } |
|
205 | } else if (data == "SIN") { | |
|
206 |
for (int |
|
|
207 | x.append(i); | |
|
208 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100)); | |
|
229 | } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) { | |
|
230 | for (int j(0); j < data.count(); j++) { | |
|
231 | QList<qreal> column = data.at(j); | |
|
232 | QScatterSeries *series = new QScatterSeries(); | |
|
233 | for (int i(0); i < column.count(); i++) { | |
|
234 | (*series) << QPointF(i, column.at(i)); | |
|
235 | } | |
|
236 | m_chartWidget->addSeries(series); | |
|
237 | setCurrentSeries(series); | |
|
209 | 238 | } |
|
210 | } else if (data == "SIN + random") { | |
|
211 | for (qreal i = 0; i < 100; i += 0.1) { | |
|
212 | x.append(i + (rand() % 5)); | |
|
213 | y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); | |
|
239 | } else if (seriesName.contains("pie", Qt::CaseInsensitive)) { | |
|
240 | QStringList labels = generateLabels(rowCount); | |
|
241 | for (int j(0); j < data.count(); j++) { | |
|
242 | QPieSeries *series = new QPieSeries(); | |
|
243 | QList<qreal> column = data.at(j); | |
|
244 | for (int i(0); i < column.count(); i++) { | |
|
245 | series->add(column.at(i), labels.at(i)); | |
|
246 | } | |
|
247 | m_chartWidget->addSeries(series); | |
|
248 | setCurrentSeries(series); | |
|
214 | 249 | } |
|
215 | } else if (data == "Table, 5 series"){ | |
|
216 | // Create some test data to chart | |
|
217 | *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; | |
|
218 | *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; | |
|
219 | *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; | |
|
220 | *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; | |
|
221 | *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; | |
|
222 | } else { | |
|
223 | // TODO: check if data has a valid file name | |
|
224 | Q_ASSERT(false); | |
|
225 | } | |
|
226 | ||
|
227 | // TODO: color of the series | |
|
228 | QChartSeries *newSeries = 0; | |
|
229 | if (series == "Scatter") { | |
|
230 | QScatterSeries *scatter = new QScatterSeries(); | |
|
231 | for (int i(0); i < x.count() && i < y.count(); i++) | |
|
232 | (*scatter) << QPointF(x.at(i), y.at(i)); | |
|
233 | m_chartWidget->addSeries(scatter); | |
|
234 | } else if (series == "Pie") { | |
|
235 | newSeries = new QPieSeries(); | |
|
236 | m_chartWidget->addSeries(newSeries); | |
|
237 | Q_ASSERT(newSeries->setData(y)); | |
|
238 | } else if (series == "Line") { | |
|
239 | // TODO: adding data to an existing line series does not give any visuals for some reason | |
|
240 | // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine); | |
|
241 | // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries); | |
|
242 | // lineSeries->setColor(Qt::blue); | |
|
243 | // for (int i(0); i < x.count() && i < y.count(); i++) { | |
|
244 | // lineSeries->add(x.at(i), y.at(i)); | |
|
245 | // } | |
|
246 | //Q_ASSERT(newSeries->setData(x, y)); | |
|
247 | QLineChartSeries* series0 = new QLineChartSeries(); | |
|
248 | for (int i(0); i < x.count() && i < y.count(); i++) | |
|
249 | series0->add(x.at(i), y.at(i)); | |
|
250 | m_chartWidget->addSeries(series0); | |
|
251 | newSeries = series0; | |
|
252 | } else if (series == "Bar") { | |
|
253 | qDebug() << "Bar chart series"; | |
|
254 | ||
|
255 | QBarCategory *category = new QBarCategory; | |
|
256 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | |
|
257 | ||
|
258 | QBarChartSeries* series0 = new QBarChartSeries(category, this); | |
|
259 | ||
|
260 | series0->addBarSet(set0); | |
|
261 | series0->addBarSet(set1); | |
|
262 | series0->addBarSet(set2); | |
|
263 | series0->addBarSet(set3); | |
|
264 | series0->addBarSet(set4); | |
|
265 | ||
|
266 | m_chartWidget->addSeries(series0); | |
|
267 | newSeries = series0; | |
|
268 | } else if (series == "StackedBar") { | |
|
269 | qDebug() << "Stacked bar chart series"; | |
|
270 | ||
|
271 | QBarCategory *category = new QBarCategory; | |
|
272 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | |
|
273 | ||
|
274 | QStackedBarChartSeries* series0 = new QStackedBarChartSeries(category, this); | |
|
275 | ||
|
276 | series0->addBarSet(set0); | |
|
277 | series0->addBarSet(set1); | |
|
278 | series0->addBarSet(set2); | |
|
279 | series0->addBarSet(set3); | |
|
280 | series0->addBarSet(set4); | |
|
281 | ||
|
282 | m_chartWidget->addSeries(series0); | |
|
283 | newSeries = series0; | |
|
284 | } else if (series == "PercentBar") { | |
|
285 | qDebug() << "Percent bar chart series"; | |
|
286 | ||
|
287 | QBarCategory *category = new QBarCategory; | |
|
288 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | |
|
289 | ||
|
290 | QPercentBarChartSeries* series0 = new QPercentBarChartSeries(category, this); | |
|
291 | ||
|
292 | series0->addBarSet(set0); | |
|
293 | series0->addBarSet(set1); | |
|
294 | series0->addBarSet(set2); | |
|
295 | series0->addBarSet(set3); | |
|
296 | series0->addBarSet(set4); | |
|
297 | ||
|
298 | m_chartWidget->addSeries(series0); | |
|
299 | newSeries = series0; | |
|
300 | } else { | |
|
301 | qDebug() << "Something weird going on in MainWidget::addSeries"; | |
|
302 | 250 | } |
|
303 | 251 | |
|
304 | setCurrentSeries(newSeries); | |
|
252 | // TODO: bar and other... | |
|
305 | 253 | } |
|
306 | 254 | |
|
307 | 255 | void MainWidget::setCurrentSeries(QChartSeries *series) |
@@ -331,42 +279,6 void MainWidget::setCurrentSeries(QChartSeries *series) | |||
|
331 | 279 | } |
|
332 | 280 | } |
|
333 | 281 | |
|
334 | void MainWidget::testDataChanged(int itemIndex) | |
|
335 | { | |
|
336 | qDebug() << "testDataChanged: " << itemIndex; | |
|
337 | ||
|
338 | // switch (itemIndex) { | |
|
339 | // case 0: { | |
|
340 | // QList<QChartDataPoint> data; | |
|
341 | // for (int x = 0; x < 20; x++) { | |
|
342 | // data.append(QChartDataPoint() << x << x / 2); | |
|
343 | // } | |
|
344 | // m_chartWidget->setData(data); | |
|
345 | // break; | |
|
346 | // } | |
|
347 | // case 1: { | |
|
348 | // QList<QChartDataPoint> data; | |
|
349 | // for (int x = 0; x < 100; x++) { | |
|
350 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100)); | |
|
351 | // } | |
|
352 | // m_chartWidget->setData(data); | |
|
353 | // break; | |
|
354 | // } | |
|
355 | // case 2: { | |
|
356 | // QList<QChartDataPoint> data; | |
|
357 | // for (int x = 0; x < 1000; x++) { | |
|
358 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); | |
|
359 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); | |
|
360 | // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2)); | |
|
361 | // } | |
|
362 | // m_chartWidget->setData(data); | |
|
363 | // break; | |
|
364 | // } | |
|
365 | // default: | |
|
366 | // break; | |
|
367 | // } | |
|
368 | } | |
|
369 | ||
|
370 | 282 | void MainWidget::backgroundChanged(int itemIndex) |
|
371 | 283 | { |
|
372 | 284 | qDebug() << "backgroundChanged: " << itemIndex; |
@@ -11,6 +11,8 class QGridLayout; | |||
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
13 | 13 | |
|
14 | #define RealList QList<qreal> | |
|
15 | ||
|
14 | 16 | class MainWidget : public QWidget |
|
15 | 17 | { |
|
16 | 18 | Q_OBJECT |
@@ -27,8 +29,7 private: | |||
|
27 | 29 | |
|
28 | 30 | private slots: |
|
29 | 31 | void addSeries(); |
|
30 | void addSeries(QString series, QString data); | |
|
31 | void testDataChanged(int itemIndex); | |
|
32 | void addSeries(QString series, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled); | |
|
32 | 33 | void backgroundChanged(int itemIndex); |
|
33 | 34 | void autoScaleChanged(int value); |
|
34 | 35 | void xMinChanged(int value); |
@@ -39,6 +40,8 private slots: | |||
|
39 | 40 | void changeChartTheme(int themeIndex); |
|
40 | 41 | void setPieSizeFactor(double margin); |
|
41 | 42 | void setPiePosition(int position); |
|
43 | QList<RealList> generateTestData(int columnCount, int rowCount, QString dataCharacteristics); | |
|
44 | QStringList generateLabels(int count); | |
|
42 | 45 | |
|
43 | 46 | private: |
|
44 | 47 | QChartView *m_chartWidget; |
General Comments 0
You need to be logged in to leave comments.
Login now