##// END OF EJS Templates
Refactored model related methods in XYSeries
Marek Rosa -
r1065:f025a5fd5ba2
parent child
Show More
@@ -1,325 +1,249
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "xychartitem_p.h"
22 22 #include "qxyseries.h"
23 23 #include "qxyseries_p.h"
24 24 #include "chartpresenter_p.h"
25 25 #include "chartanimator_p.h"
26 26 #include <QPainter>
27 27 #include <QGraphicsSceneMouseEvent>
28 28 #include <QAbstractItemModel>
29 29
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 //TODO: optimize : remove points which are not visible
34 34
35 35 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
36 36 m_minX(0),
37 37 m_maxX(0),
38 38 m_minY(0),
39 39 m_maxY(0),
40 40 m_series(series)
41 41 {
42 42 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
43 43 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
44 44 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
45 45 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
46 46 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
47 47 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
48 48 }
49 49
50 50 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
51 51 {
52 52 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
53 53 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
54 54 qreal x = (point.x() - m_minX)* deltaX;
55 55 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
56 56 return QPointF(x,y);
57 57 }
58 58
59 59
60 60 QPointF XYChartItem::calculateGeometryPoint(int index) const
61 61 {
62 62 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
63 63 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
64 64 const QList<QPointF>& vector = m_series->points();
65 65 qreal x = (vector[index].x() - m_minX)* deltaX;
66 66 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
67 67 return QPointF(x,y);
68 68 }
69 69
70 70 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
71 71 {
72 72 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
73 73 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
74 74
75 75 QVector<QPointF> result;
76 76 result.resize(m_series->count());
77 77 const QList<QPointF>& vector = m_series->points();
78 78 for (int i = 0; i < m_series->count(); ++i) {
79 79 qreal x = (vector[i].x() - m_minX)* deltaX;
80 80 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
81 81 result[i].setX(x);
82 82 result[i].setY(y);
83 83 }
84 84 return result;
85 85 }
86 86
87 87 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
88 88 {
89 89 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
90 90 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
91 91 qreal x = point.x()/deltaX +m_minX;
92 92 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
93 93 return QPointF(x,y);
94 94 }
95 95
96 96 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
97 97 {
98 98 if (animator()) {
99 99 animator()->updateLayout(this,oldPoints,newPoints,index);
100 100 } else {
101 101 setLayout(newPoints);
102 102 }
103 103 }
104 104
105 105 void XYChartItem::setLayout(QVector<QPointF> &points)
106 106 {
107 107 m_points = points;
108 108 update();
109 109 }
110 110
111 111 //handlers
112 112
113 113 void XYChartItem::handlePointAdded(int index)
114 114 {
115 115 if (m_series->model() == 0) {
116 116 Q_ASSERT(index<m_series->count());
117 117 Q_ASSERT(index>=0);
118 118 }
119 119 QVector<QPointF> points = m_points;
120 120 QPointF point;
121 121 point = calculateGeometryPoint(index);
122 122 points.insert(index, point);
123 123 updateLayout(m_points, points, index);
124 124 update();
125 125 }
126 126
127 127 void XYChartItem::handlePointsAdded(int start, int end)
128 128 {
129 129 if (m_series->model() == 0) {
130 130 for (int i = start; i <= end; i++)
131 131 handlePointAdded(i);
132 132 } else if (m_series->mapCount() != -1 && start >= m_series->mapFirst() + m_series->mapCount()) {
133 133 return;
134 134 } else {
135 135 int addedCount = end - start + 1;
136 136 if (m_series->mapCount() != -1 && addedCount > m_series->mapCount())
137 137 addedCount = m_series->mapCount();
138 138 int first = qMax(start, m_series->mapFirst());
139 int last = qMin(first + addedCount - 1, m_series->mapOrientation() == Qt::Vertical ? m_series->model()->rowCount() - 1 : m_series->model()->columnCount() - 1);
139 int last = qMin(first + addedCount - 1, m_series->count() + m_series->mapFirst() - 1);
140 140 for (int i = first; i <= last; i++) {
141 141 handlePointAdded(i - m_series->mapFirst());
142 142 }
143 143 if (m_series->mapCount() != -1 && m_points.size() > m_series->mapCount())
144 144 for (int i = m_points.size() - 1; i >= m_series->mapCount(); i--)
145 145 handlePointRemoved(i);
146 146 }
147
148 // else {
149 // // series uses model as a data source
150 // int first = m_series->mapFirst();
151 // int count = m_series->mapCount();
152 // int addedCount = end - start + 1;
153 // if (count != -1 && start >= first + count) {
154 // return;
155 // }
156
157 // // adding items to unlimited map
158 // else if (count == -1 && start >= first) {
159 // for (int i = start; i <= end; i++)
160 // handlePointAdded(i - first);
161 // } else if (count == - 1 && start < first) {
162 // // not all newly added items
163 // for (int i = first; i < first + addedCount; i++)
164 // handlePointAdded(i - first);
165 // }
166 // // commented out code below does the same thing, but its more confusing.
167 // // } else if (count == -1) {
168 // // int begin = qMax(start, first);
169 // // for (int i = begin; i < begin + (end - start + 1); i++)
170 // // handlePointAdded(i - first);
171 // // }
172
173 // // adding items to limited map
174 // else if (start >= first) {
175 // // remove the items that will no longer fit into the map
176 // // int toRemove = addedCount - (count - points().size());
177 // for (int i = start; i <= end; i++) {
178 // handlePointAdded(i - first);
179 // }
180 // if (m_points.size() > count)
181 // for (int i = m_points.size() - 1; i >= count; i--)
182 // handlePointRemoved(i);
183 // // update();
184 // } else {
185 // //
186 // for (int i = first; i < first + addedCount; i++) {
187 // handlePointAdded(i - first);
188 // }
189 // if (m_points.size() > count)
190 // for (int i = m_points.size() - 1; i >= count; i--)
191 // handlePointRemoved(i);
192 // }
193 // }
194 147 }
195 148
196 149 void XYChartItem::handlePointRemoved(int index)
197 150 {
198 151 if (m_series->model() == 0) {
199 152 Q_ASSERT(index<m_series->count() + 1);
200 153 Q_ASSERT(index>=0);
201 154 }
202 155 QVector<QPointF> points = m_points;
203 156 points.remove(index);
204 157 updateLayout(m_points, points, index);
205 158 update();
206 159 }
207 160
208 161 void XYChartItem::handlePointsRemoved(int start, int end)
209 162 {
210 163 Q_UNUSED(start)
211 164 Q_UNUSED(end)
212 165 if (m_series->model() == 0) {
213 166 for (int i = end; i >= start; i--)
214 167 handlePointRemoved(i);
215 168 } else {
216 169 // series uses model as a data source
217 int first = m_series->mapFirst();
218 int count = m_series->mapCount();
170 int mapFirst = m_series->mapFirst();
171 int mapCount = m_series->mapCount();
219 172 int removedCount = end - start + 1;
220 if (count != -1 && start >= first + count) {
173 if (mapCount != -1 && start >= mapFirst + mapCount) {
221 174 return;
222 }
223
224 // removing items from unlimited map
225 else if (count == -1 && start >= first) {
226 for (int i = end; i >= start; i--)
227 handlePointRemoved(i - first);
228 } else if (count == - 1 && start < first) {
229 // not all removed items
230 int lastExisting = qMin(first + m_points.size() - 1, first + removedCount - 1);
231 for (int i = lastExisting; i >= first; i--)
232 handlePointRemoved(i - first);
233 }
234
235 // removing items from limited map
236 else if (start >= first) {
237 //
238 int lastExisting = qMin(first + m_points.size() - 1, end);
239 for (int i = lastExisting; i >= start; i--) {
240 handlePointRemoved(i - first);
241 }
242
243 // the map is limited, so after removing the items some new items may have fall within the mapped area
244 int itemsAvailable;
245 if (m_series->mapOrientation() == Qt::Vertical)
246 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
247 else
248 itemsAvailable = m_series->model()->columnCount() - first - m_points.size();
249 int toBeAdded = qMin(itemsAvailable, count - m_points.size());
250 int currentSize = m_points.size();
251 if (itemsAvailable > 0)
252 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
253 handlePointAdded(i);
254 175 } else {
255 // first removed item lays before the mapped area
256 int toRemove = qMin(m_points.size() - 1, removedCount);
257 for (int i = first; i < first + toRemove; i++)
258 handlePointRemoved(0);
259
260 // the map is limited, so after removing the items some new items may have fall into the map
261 int itemsAvailable;
262 if (m_series->mapOrientation() == Qt::Vertical)
263 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
264 else
265 itemsAvailable = m_series->model()->columnCount() - first - m_points.size();
266 int toBeAdded = qMin(itemsAvailable, count - m_points.size());
267 int currentSize = m_points.size();
268 if (itemsAvailable > 0)
269 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
270 handlePointAdded(i);
176 int toRemove = qMin(m_points.size(), removedCount); // first find how many items can actually be removed
177 int first = qMax(start, mapFirst); // get the index of the first item that will be removed.
178 int last = qMin(first + toRemove - 1, m_points.size() + mapFirst - 1); // get the index of the last item that will be removed.
179 for (int i = last; i >= first; i--)
180 handlePointRemoved(i - mapFirst);
181
182 if (mapCount != -1) {
183 int itemsAvailable; // check how many are available to be added
184 if (m_series->mapOrientation() == Qt::Vertical)
185 itemsAvailable = m_series->model()->rowCount() - mapFirst - m_points.size();
186 else
187 itemsAvailable = m_series->model()->columnCount() - mapFirst - m_points.size();
188 int toBeAdded = qMin(itemsAvailable, mapCount - m_points.size()); // add not more items than there is space left to be filled.
189 int currentSize = m_points.size();
190 if (toBeAdded > 0)
191 for (int i = m_points.size(); i < currentSize + toBeAdded; i++) {
192 handlePointAdded(i);
193 }
194 }
271 195 }
272 196 }
273 197
274 198 }
275 199
276 200 void XYChartItem::handlePointReplaced(int index)
277 201 {
278 202 Q_ASSERT(index<m_series->count());
279 203 Q_ASSERT(index>=0);
280 204 QPointF point = calculateGeometryPoint(index);
281 205 QVector<QPointF> points = m_points;
282 206 points.replace(index,point);
283 207 updateLayout(m_points,points,index);
284 208 update();
285 209 }
286 210
287 211 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
288 212 {
289 213 m_minX=minX;
290 214 m_maxX=maxX;
291 215 m_minY=minY;
292 216 m_maxY=maxY;
293 217 if (isEmpty()) return;
294 218 QVector<QPointF> points = calculateGeometryPoints();
295 219 updateLayout(m_points,points);
296 220 update();
297 221 }
298 222
299 223 void XYChartItem::handleGeometryChanged(const QRectF &rect)
300 224 {
301 225 Q_ASSERT(rect.isValid());
302 226 m_size=rect.size();
303 227 m_clipRect=rect.translated(-rect.topLeft());
304 228 setPos(rect.topLeft());
305 229
306 230 if (isEmpty()) return;
307 231 QVector<QPointF> points = calculateGeometryPoints();
308 232 updateLayout(m_points,points);
309 233 update();
310 234 }
311 235
312 236
313 237 bool XYChartItem::isEmpty()
314 238 {
315 239 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY);
316 240 }
317 241
318 242 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
319 243 {
320 244 emit clicked(calculateDomainPoint(event->pos()));
321 245 }
322 246
323 247 #include "moc_xychartitem_p.cpp"
324 248
325 249 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,376 +1,376
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include <QGridLayout>
23 23 #include <QTableView>
24 24 #include <QChart>
25 25 #include <QStyledItemDelegate>
26 26 #include <QLineSeries>
27 27 #include <QSplineSeries>
28 28 #include <QScatterSeries>
29 29 #include "customtablemodel.h"
30 30 #include <QPieSeries>
31 31 #include <QPieSlice>
32 32 #include <QAreaSeries>
33 33 #include <QBarSeries>
34 34 #include <QBarSet>
35 35 #include <QPushButton>
36 36 #include <QRadioButton>
37 37 #include <QLabel>
38 38 #include <QSpinBox>
39 39 #include <QTime>
40 40
41 41 TableWidget::TableWidget(QWidget *parent)
42 42 : QWidget(parent)
43 43 // specialPie(0)
44 44 {
45 45 setGeometry(1900, 100, 1000, 600);
46 46 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
47 47 // create simple model for storing data
48 48 // user's table data model
49 49 m_model = new CustomTableModel;
50 50 m_tableView = new QTableView;
51 51 m_tableView->setModel(m_model);
52 52 m_tableView->setMinimumHeight(300);
53 53 // tableView->setMinimumSize(340, 480);
54 54 // tableView->setItemDelegate(new QStyledItemDelegate);
55 55 m_chart = new QChart;
56 56 m_chart->legend()->setVisible(true);
57 m_chart->setAnimationOptions(QChart::SeriesAnimations);
57 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
58 58 m_chartView = new QChartView(m_chart);
59 59 m_chartView->setRenderHint(QPainter::Antialiasing);
60 60 m_chartView->setMinimumSize(640, 480);
61 61
62 62 // add, remove data buttons
63 63 QPushButton* addRowAboveButton = new QPushButton("Add row above");
64 64 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
65 65
66 66 QPushButton* addRowBelowButton = new QPushButton("Add row below");
67 67 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
68 68
69 69 QPushButton* removeRowButton = new QPushButton("Remove row");
70 70 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
71 71
72 72 QPushButton* specialPieButton = new QPushButton("Test pie");
73 73 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
74 74
75 75
76 76 QLabel *spinBoxLabel = new QLabel("Rows affected:");
77 77
78 78 // spin box for setting number of affected items (add, remove)
79 79 m_linesCountSpinBox = new QSpinBox;
80 80 m_linesCountSpinBox->setRange(1, 10);
81 81 m_linesCountSpinBox->setValue(1);
82 82
83 83 // buttons layout
84 84 QVBoxLayout* buttonsLayout = new QVBoxLayout;
85 85 buttonsLayout->addWidget(spinBoxLabel);
86 86 buttonsLayout->addWidget(m_linesCountSpinBox);
87 87 buttonsLayout->addWidget(addRowAboveButton);
88 88 buttonsLayout->addWidget(addRowBelowButton);
89 89 buttonsLayout->addWidget(removeRowButton);
90 90 buttonsLayout->addWidget(specialPieButton);
91 91 buttonsLayout->addStretch();
92 92
93 93 // chart type radio buttons
94 94 m_lineRadioButton = new QRadioButton("Line");
95 95 m_splineRadioButton = new QRadioButton("Spline");
96 96 m_scatterRadioButton = new QRadioButton("Scatter");
97 97 m_pieRadioButton = new QRadioButton("Pie");
98 98 m_areaRadioButton = new QRadioButton("Area");
99 99 m_barRadioButton = new QRadioButton("Bar");
100 100
101 101 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
102 102 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
103 103 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
104 104 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
105 105 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
106 106 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
107 107 m_lineRadioButton->setChecked(true);
108 108
109 109 // radio buttons layout
110 110 QVBoxLayout* radioLayout = new QVBoxLayout;
111 111 radioLayout->addWidget(m_lineRadioButton);
112 112 radioLayout->addWidget(m_splineRadioButton);
113 113 radioLayout->addWidget(m_scatterRadioButton);
114 114 radioLayout->addWidget(m_pieRadioButton);
115 115 radioLayout->addWidget(m_areaRadioButton);
116 116 radioLayout->addWidget(m_barRadioButton);
117 117 radioLayout->addStretch();
118 118
119 119 // create main layout
120 120 QGridLayout* mainLayout = new QGridLayout;
121 121 mainLayout->addLayout(buttonsLayout, 1, 1);
122 122 mainLayout->addLayout(radioLayout, 2, 1);
123 123 mainLayout->addWidget(m_tableView, 1, 0);
124 124 mainLayout->addWidget(m_chartView, 2, 0);
125 125 setLayout(mainLayout);
126 126 m_lineRadioButton->setFocus();
127 127 }
128 128
129 129 void TableWidget::addRowAbove()
130 130 {
131 131 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
132 132
133 133 }
134 134
135 135 void TableWidget::addRowBelow()
136 136 {
137 137 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
138 138
139 139 }
140 140
141 141 void TableWidget::removeRow()
142 142 {
143 143 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
144 144 }
145 145
146 146 void TableWidget::updateChartType(bool toggle)
147 147 {
148 148 // this if is needed, so that the function is only called once.
149 149 // For the radioButton that was enabled.
150 150 if (toggle) {
151 151 // specialPie = 0;
152 152 m_chart->removeAllSeries();
153 153 m_chart->axisX()->setNiceNumbersEnabled(false);
154 154 m_chart->axisY()->setNiceNumbersEnabled(false);
155 155
156 156 // renable axes of the chart (pie hides them)
157 157 // x axis
158 158 QAxis *axis = m_chart->axisX();
159 159 axis->setAxisVisible(true);
160 160 axis->setGridLineVisible(true);
161 161 axis->setLabelsVisible(true);
162 162
163 163 // y axis
164 164 axis = m_chart->axisY();
165 165 axis->setAxisVisible(true);
166 166 axis->setGridLineVisible(true);
167 167 axis->setLabelsVisible(true);
168 168
169 169 m_model->clearMapping();
170 170
171 171 QString seriesColorHex = "#000000";
172 172 QPen pen;
173 173 pen.setWidth(2);
174 174
175 175 if (m_lineRadioButton->isChecked())
176 176 {
177 177 // series 1
178 178 m_series = new QLineSeries;
179 179 m_series->setModel(m_model);
180 180 m_series->setModelMapping(0,1, Qt::Vertical);
181 181 m_series->setModelMappingRange(4, 4);
182 182 m_chart->addSeries(m_series);
183 183 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
184 184 m_model->addMapping(seriesColorHex, QRect(0, 4, 2, 4));
185 185
186 186 // series 2
187 187 m_series = new QLineSeries;
188 188 m_series->setModel(m_model);
189 189 m_series->setModelMapping(2,3, Qt::Vertical);
190 190 m_chart->addSeries(m_series);
191 191 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
192 192 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
193 193
194 194 // series 3
195 195 m_series = new QLineSeries;
196 196 m_series->setModel(m_model);
197 197 m_series->setModelMapping(4,5, Qt::Vertical);
198 198 m_series->setModelMappingRange(2, -1);
199 199 m_chart->addSeries(m_series);
200 200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
201 201 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
202 202 }
203 203 else if (m_splineRadioButton->isChecked())
204 204 {
205 205 // series 1
206 206 m_series = new QSplineSeries;
207 207 m_series->setModel(m_model);
208 208 m_series->setModelMapping(0,1, Qt::Vertical);
209 209 // m_series->setModelMappingRange(1, 4);
210 210 // series->setModelMapping(0,1, Qt::Horizontal);
211 211 m_chart->addSeries(m_series);
212 212 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
213 213 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
214 214
215 215 // series 2
216 216 m_series = new QSplineSeries;
217 217 m_series->setModel(m_model);
218 218 m_series->setModelMapping(2,3, Qt::Vertical);
219 219 // m_series->setModelMappingRange(0, 0);
220 220 // series->setModelMapping(2,3, Qt::Horizontal);
221 221 m_chart->addSeries(m_series);
222 222 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
223 223 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
224 224
225 225 // series 3
226 226 m_series = new QSplineSeries;
227 227 m_series->setModel(m_model);
228 228 m_series->setModelMapping(4,5, Qt::Vertical);
229 229 // m_series->setModelMappingRange(2, 0);
230 230 // series->setModelMapping(4,5, Qt::Horizontal);
231 231 m_chart->addSeries(m_series);
232 232 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
233 233 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
234 234 }
235 235 else if (m_scatterRadioButton->isChecked())
236 236 {
237 237 // series 1
238 238 m_series = new QScatterSeries;
239 239 m_series->setModel(m_model);
240 240 m_series->setModelMapping(0,1, Qt::Vertical);
241 241 // m_series->setModelMappingRange(2, 0);
242 242 // series->setModelMapping(0,1, Qt::Horizontal);
243 243 m_chart->addSeries(m_series);
244 244
245 245 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
246 246 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
247 247
248 248 // series 2
249 249 m_series = new QScatterSeries;
250 250 m_series->setModel(m_model);
251 251 m_series->setModelMapping(2,3, Qt::Vertical);
252 252 // m_series->setModelMappingRange(1, 6);
253 253 // series->setModelMapping(2,3, Qt::Horizontal);
254 254 m_chart->addSeries(m_series);
255 255
256 256 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
257 257 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
258 258
259 259 // series 3
260 260 m_series = new QScatterSeries;
261 261 m_series->setModel(m_model);
262 262 m_series->setModelMapping(4,5, Qt::Vertical);
263 263 // series->setModelMapping(4,5, Qt::Horizontal);
264 264 m_chart->addSeries(m_series);
265 265 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
266 266 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
267 267 }
268 268 else if (m_pieRadioButton->isChecked())
269 269 {
270 270 // pie 1
271 271 QPieSeries* pieSeries = new QPieSeries;
272 272 pieSeries->setModel(m_model);
273 273 pieSeries->setModelMappingRange(3, 3);
274 274 pieSeries->setModelMapping(0,0, Qt::Vertical);
275 275 pieSeries->setLabelsVisible(true);
276 276 pieSeries->setPieSize(0.35);
277 277 pieSeries->setHorizontalPosition(0.2);
278 278 pieSeries->setVerticalPosition(0.3);
279 279
280 280 m_chart->addSeries(pieSeries);
281 281 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
282 282 m_model->addMapping(seriesColorHex, QRect(0, 3, 1, 3));
283 283
284 284 // pie 2
285 285 pieSeries = new QPieSeries;
286 286 pieSeries->setModel(m_model);
287 287 pieSeries->setModelMappingRange(2, -1);
288 288 pieSeries->setModelMapping(1,1, Qt::Vertical);
289 289 pieSeries->setLabelsVisible(true);
290 290 pieSeries->setPieSize(0.35);
291 291 pieSeries->setHorizontalPosition(0.8);
292 292 pieSeries->setVerticalPosition(0.3);
293 293 m_chart->addSeries(pieSeries);
294 294 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
295 295 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
296 296
297 297 // pie 3
298 298 pieSeries = new QPieSeries;
299 299 pieSeries->setModel(m_model);
300 300 pieSeries->setModelMapping(2,2, Qt::Vertical);
301 301 pieSeries->setLabelsVisible(true);
302 302 pieSeries->setPieSize(0.35);
303 303 pieSeries->setHorizontalPosition(0.5);
304 304 pieSeries->setVerticalPosition(0.75);
305 305 m_chart->addSeries(pieSeries);
306 306 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
307 307 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
308 308
309 309 // // special pie
310 310 // specialPie = new QPieSeries;
311 311 // specialPie->append(17, "1");
312 312 // specialPie->append(45, "2");
313 313 // specialPie->append(77, "3");
314 314 // specialPie->append(37, "4");
315 315 // specialPie->append(27, "5");
316 316 // specialPie->append(47, "6");
317 317 // specialPie->setPieSize(0.35);
318 318 // specialPie->setHorizontalPosition(0.8);
319 319 // specialPie->setVerticalPosition(0.75);
320 320 // specialPie->setLabelsVisible(true);
321 321 // m_chart->addSeries(specialPie);
322 322 }
323 323 else if (m_areaRadioButton->isChecked())
324 324 {
325 325 QLineSeries* upperLineSeries = new QLineSeries;
326 326 upperLineSeries->setModel(m_model);
327 327 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
328 328 // upperLineSeries->setModelMappingRange(1, 5);
329 329 QLineSeries* lowerLineSeries = new QLineSeries;
330 330 lowerLineSeries->setModel(m_model);
331 331 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
332 332 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
333 333 m_chart->addSeries(areaSeries);
334 334 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
335 335 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
336 336 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
337 337 }
338 338 else if (m_barRadioButton->isChecked())
339 339 {
340 340 QBarSeries* barSeries = new QBarSeries(QStringList());
341 341 barSeries->setModel(m_model);
342 342 // barSeries->setModelMappingRange(2, 5);
343 343 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
344 344 m_chart->addSeries(barSeries);
345 345 QList<QBarSet*> barsets = barSeries->barSets();
346 346 for (int i = 0; i < barsets.count(); i++) {
347 347 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
348 348 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
349 349 }
350 350 }
351 351
352 352
353 353 if (!m_barRadioButton->isChecked())
354 354 m_chart->axisX()->setRange(0, 500);
355 355 // m_chart->axisY()->setRange(0, 120);
356 356 m_chart->legend()->setVisible(true);
357 357
358 358 // repaint table view colors
359 359 m_tableView->repaint();
360 360 m_tableView->setFocus();
361 361 }
362 362 }
363 363
364 364 void TableWidget::testPie()
365 365 {
366 366 // if (specialPie) {
367 367 // specialPie->remove(specialPie->slices().at(2));
368 368 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
369 369 // specialPie->append(4, "heloo");
370 370 // }
371 371 }
372 372
373 373 TableWidget::~TableWidget()
374 374 {
375 375
376 376 }
General Comments 0
You need to be logged in to leave comments. Login now