##// END OF EJS Templates
rename functions add() -> append()
Jani Honkonen -
r796:4bbe1f66d4b7
parent child
Show More
@@ -1,346 +1,346
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:BSD$
10 10 ** You may use this file under the terms of the BSD license as follows:
11 11 **
12 12 ** "Redistribution and use in source and binary forms, with or without
13 13 ** modification, are permitted provided that the following conditions are
14 14 ** met:
15 15 ** * Redistributions of source code must retain the above copyright
16 16 ** notice, this list of conditions and the following disclaimer.
17 17 ** * Redistributions in binary form must reproduce the above copyright
18 18 ** notice, this list of conditions and the following disclaimer in
19 19 ** the documentation and/or other materials provided with the
20 20 ** distribution.
21 21 ** * Neither the name of Digia nor the names of its contributors
22 22 ** may be used to endorse or promote products derived from this
23 23 ** software without specific prior written permission.
24 24 **
25 25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 36 ** $QT_END_LICENSE$
37 37 **
38 38 ****************************************************************************/
39 39
40 40 #include "themewidget.h"
41 41
42 42 #include <QChartView>
43 43 #include <QPieSeries>
44 44 #include <QPieSlice>
45 45 #include <QBarSeries>
46 46 #include <QPercentBarSeries>
47 47 #include <QStackedBarSeries>
48 48 #include <QBarSet>
49 49 #include <QLineSeries>
50 50 #include <QSplineSeries>
51 51 #include <QScatterSeries>
52 52 #include <QAreaSeries>
53 53 #include <QGridLayout>
54 54 #include <QFormLayout>
55 55 #include <QComboBox>
56 56 #include <QSpinBox>
57 57 #include <QCheckBox>
58 58 #include <QGroupBox>
59 59 #include <QLabel>
60 60 #include <QTime>
61 61
62 62 ThemeWidget::ThemeWidget(QWidget* parent) :
63 63 QWidget(parent),
64 64 m_listCount(3),
65 65 m_valueMax(100),
66 66 m_valueCount(11),
67 67 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
68 68 m_themeComboBox(createThemeBox()),
69 69 m_antialiasCheckBox(new QCheckBox("Anti aliasing")),
70 70 m_animatedComboBox(createAnimationBox())
71 71 {
72 72
73 73 connectSignals();
74 74 // create layout
75 75 QGridLayout* baseLayout = new QGridLayout();
76 76 QHBoxLayout *settingsLayout = new QHBoxLayout();
77 77 settingsLayout->addWidget(new QLabel("Theme:"));
78 78 settingsLayout->addWidget(m_themeComboBox);
79 79 settingsLayout->addWidget(new QLabel("Animation:"));
80 80 settingsLayout->addWidget(m_animatedComboBox);
81 81 settingsLayout->addWidget(m_antialiasCheckBox);
82 82 settingsLayout->addStretch();
83 83 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
84 84
85 85 //create charts
86 86
87 87 QChartView *chartView;
88 88
89 89 chartView = new QChartView(createAreaChart());
90 90 baseLayout->addWidget(chartView, 1, 0);
91 91 m_charts << chartView;
92 92
93 93 chartView = new QChartView(createBarChart(m_valueCount));
94 94 baseLayout->addWidget(chartView, 1, 1);
95 95 m_charts << chartView;
96 96
97 97 chartView = new QChartView(createLineChart());
98 98 baseLayout->addWidget(chartView, 1, 2);
99 99 m_charts << chartView;
100 100
101 101 chartView = new QChartView(createPieChart());
102 102 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
103 103 baseLayout->addWidget(chartView, 2, 0);
104 104 m_charts << chartView;
105 105
106 106 chartView = new QChartView(createSplineChart());
107 107 baseLayout->addWidget(chartView, 2, 1);
108 108 m_charts << chartView;
109 109
110 110 chartView = new QChartView(createScatterChart());
111 111 baseLayout->addWidget(chartView, 2, 2);
112 112 m_charts << chartView;
113 113
114 114 setLayout(baseLayout);
115 115 }
116 116
117 117 ThemeWidget::~ThemeWidget()
118 118 {
119 119 }
120 120
121 121 void ThemeWidget::connectSignals()
122 122 {
123 123 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
124 124 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
125 125 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
126 126 }
127 127
128 128 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
129 129 {
130 130 DataTable dataTable;
131 131
132 132 // set seed for random stuff
133 133 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
134 134
135 135 // generate random data
136 136 for (int i(0); i < listCount; i++) {
137 137 DataList dataList;
138 138 for (int j(0); j < valueCount; j++) {
139 139 QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax);
140 140 QString label = "Item " + QString::number(i) + ":" + QString::number(j);
141 141 dataList << Data(value, label);
142 142 }
143 143 dataTable << dataList;
144 144 }
145 145
146 146 return dataTable;
147 147 }
148 148
149 149 QComboBox* ThemeWidget::createThemeBox() const
150 150 {
151 151 // settings layout
152 152 QComboBox* themeComboBox = new QComboBox();
153 153 themeComboBox->addItem("Default", QChart::ChartThemeDefault);
154 154 themeComboBox->addItem("Light", QChart::ChartThemeLight);
155 155 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
156 156 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
157 157 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
158 158 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
159 159 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
160 160 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
161 161 return themeComboBox;
162 162 }
163 163
164 164 QComboBox* ThemeWidget::createAnimationBox() const
165 165 {
166 166 // settings layout
167 167 QComboBox* animationComboBox = new QComboBox();
168 168 animationComboBox->addItem("No Animations", QChart::NoAnimation);
169 169 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
170 170 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
171 171 animationComboBox->addItem("All Animations", QChart::AllAnimations);
172 172 return animationComboBox;
173 173 }
174 174
175 175 QChart* ThemeWidget::createAreaChart() const
176 176 {
177 177 // area chart
178 178 QChart *chart = new QChart();
179 179 chart->setTitle("Area chart");
180 180 {
181 181 QString name("Series ");
182 182 int nameIndex = 0;
183 183 for (int i(0); i < m_dataTable.count(); i++) {
184 184 QLineSeries *series1 = new QLineSeries(chart);
185 185 QLineSeries *series2 = new QLineSeries(chart);
186 186 foreach (Data data, m_dataTable[i]) {
187 series1->add(data.first);
188 series2->add(QPointF(data.first.x(), 0.0));
187 series1->append(data.first);
188 series2->append(QPointF(data.first.x(), 0.0));
189 189 }
190 190 QAreaSeries *area = new QAreaSeries(series1, series2);
191 191 area->setName(name + QString::number(nameIndex));
192 192 nameIndex++;
193 193 chart->addSeries(area);
194 194 }
195 195 }
196 196 return chart;
197 197 }
198 198
199 199 QChart* ThemeWidget::createBarChart(int valueCount) const
200 200 {
201 201 // bar chart
202 202 QChart* chart = new QChart();
203 203 chart->setTitle("Bar chart");
204 204 {
205 205 QBarCategories categories;
206 206 // TODO: categories
207 207 for (int i(0); i < valueCount; i++)
208 208 categories << QString::number(i);
209 209 // QBarSeries* series = new QBarSeries(categories, chart);
210 210 // QPercentBarSeries* series = new QPercentBarSeries(categories, chart);
211 211 QStackedBarSeries* series = new QStackedBarSeries(categories, chart);
212 212 for (int i(0); i < m_dataTable.count(); i++) {
213 213 QBarSet *set = new QBarSet("Set" + QString::number(i));
214 214 foreach (Data data, m_dataTable[i])
215 215 *set << data.first.y();
216 216 series->appendBarSet(set);
217 217 }
218 218 chart->addSeries(series);
219 219 }
220 220 return chart;
221 221 }
222 222
223 223 QChart* ThemeWidget::createLineChart() const
224 224 {
225 225 // line chart
226 226 QChart* chart = new QChart();
227 227 chart->setTitle("Line chart");
228 228 QString name("Series ");
229 229 int nameIndex = 0;
230 230 foreach (DataList list, m_dataTable) {
231 231 QLineSeries *series = new QLineSeries(chart);
232 232 foreach (Data data, list)
233 series->add(data.first);
233 series->append(data.first);
234 234 series->setName(name + QString::number(nameIndex));
235 235 nameIndex++;
236 236 chart->addSeries(series);
237 237 }
238 238 return chart;
239 239 }
240 240
241 241 QChart* ThemeWidget::createPieChart() const
242 242 {
243 243 // pie chart
244 244 QChart* chart = new QChart();
245 245 chart->setTitle("Pie chart");
246 246 qreal pieSize = 1.0 / m_dataTable.count();
247 247 for (int i = 0; i < m_dataTable.count(); i++) {
248 248 QPieSeries *series = new QPieSeries(chart);
249 249 foreach (Data data, m_dataTable[i]) {
250 QPieSlice *slice = series->add(data.first.y(), data.second);
250 QPieSlice *slice = series->append(data.first.y(), data.second);
251 251 if (data == m_dataTable[i].first()) {
252 252 slice->setLabelVisible();
253 253 slice->setExploded();
254 254 }
255 255 }
256 256 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
257 257 series->setPieSize(pieSize);
258 258 series->setPiePosition(hPos, 0.5);
259 259 chart->addSeries(series);
260 260 }
261 261
262 262 return chart;
263 263 }
264 264
265 265 QChart* ThemeWidget::createSplineChart() const
266 266 { // spine chart
267 267 QChart* chart = new QChart();
268 268 chart->setTitle("Spline chart");
269 269 QString name("Series ");
270 270 int nameIndex = 0;
271 271 foreach (DataList list, m_dataTable) {
272 272 QSplineSeries *series = new QSplineSeries(chart);
273 273 foreach (Data data, list)
274 series->add(data.first);
274 series->append(data.first);
275 275 series->setName(name + QString::number(nameIndex));
276 276 nameIndex++;
277 277 chart->addSeries(series);
278 278 }
279 279 return chart;
280 280 }
281 281
282 282 QChart* ThemeWidget::createScatterChart() const
283 283 { // scatter chart
284 284 QChart* chart = new QChart();
285 285 chart->setTitle("Scatter chart");
286 286 QString name("Series ");
287 287 int nameIndex = 0;
288 288 foreach (DataList list, m_dataTable) {
289 289 QScatterSeries *series = new QScatterSeries(chart);
290 290 foreach (Data data, list)
291 series->add(data.first);
291 series->append(data.first);
292 292 series->setName(name + QString::number(nameIndex));
293 293 nameIndex++;
294 294 chart->addSeries(series);
295 295 }
296 296 return chart;
297 297 }
298 298
299 299 void ThemeWidget::updateUI()
300 300 {
301 301 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
302 302
303 303 if (m_charts.at(0)->chart()->theme() != theme) {
304 304 foreach (QChartView *chartView, m_charts)
305 305 chartView->chart()->setTheme(theme);
306 306
307 307 QPalette pal = window()->palette();
308 308 if (theme == QChart::ChartThemeLight) {
309 309 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
310 310 pal.setColor(QPalette::WindowText, QRgb(0x404044));
311 311 } else if (theme == QChart::ChartThemeDark) {
312 312 pal.setColor(QPalette::Window, QRgb(0x121218));
313 313 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
314 314 } else if (theme == QChart::ChartThemeBlueCerulean) {
315 315 pal.setColor(QPalette::Window, QRgb(0x40434a));
316 316 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
317 317 } else if (theme == QChart::ChartThemeBrownSand) {
318 318 pal.setColor(QPalette::Window, QRgb(0x9e8965));
319 319 pal.setColor(QPalette::WindowText, QRgb(0x404044));
320 320 } else if (theme == QChart::ChartThemeBlueNcs) {
321 321 pal.setColor(QPalette::Window, QRgb(0x018bba));
322 322 pal.setColor(QPalette::WindowText, QRgb(0x404044));
323 323 } else if (theme == QChart::ChartThemeHighContrast) {
324 324 pal.setColor(QPalette::Window, QRgb(0xffab03));
325 325 pal.setColor(QPalette::WindowText, QRgb(0x181818));
326 326 } else if (theme == QChart::ChartThemeBlueIcy) {
327 327 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
328 328 pal.setColor(QPalette::WindowText, QRgb(0x404044));
329 329 } else {
330 330 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
331 331 pal.setColor(QPalette::WindowText, QRgb(0x404044));
332 332 }
333 333 window()->setPalette(pal);
334 334 }
335 335
336 336 bool checked = m_antialiasCheckBox->isChecked();
337 337 foreach (QChartView *chart, m_charts)
338 338 chart->setRenderHint(QPainter::Antialiasing, checked);
339 339
340 340 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
341 341 if (m_charts.at(0)->chart()->animationOptions() != options) {
342 342 foreach (QChartView *chartView, m_charts)
343 343 chartView->chart()->setAnimationOptions(options);
344 344 }
345 345 }
346 346
@@ -1,83 +1,83
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qlineseries.h>
6 6 #include <qscatterseries.h>
7 7 #include <qbarseries.h>
8 8 #include <qbarset.h>
9 9 #include <qpieseries.h>
10 10 #include <QStringList>
11 11
12 12 QTCOMMERCIALCHART_USE_NAMESPACE
13 13
14 14 int main(int argc, char *argv[])
15 15 {
16 16 QApplication a(argc, argv);
17 17
18 18 //! [1]
19 19 // Create chart view
20 20 QChartView *chartView = new QChartView();
21 21 chartView->setRenderHint(QPainter::Antialiasing);
22 22 chartView->setChartTitle("Simple Line Chart");
23 23 // Add series to the chart
24 24 QLineSeries *line = new QLineSeries();
25 line->add(0.0, 0.8);
26 line->add(1.1, 1.1);
27 line->add(2.0, 2.5);
25 line->append(0.0, 0.8);
26 line->append(1.1, 1.1);
27 line->append(2.0, 2.5);
28 28 chartView->addSeries(line);
29 29 //! [1]
30 30
31 31 chartView->setChartTitle("\'Scietific\' theme");
32 32 //! [2]
33 33 // Change theme
34 34 chartView->setChartTheme(QChart::ChartThemeHighContrast);
35 35 //! [2]
36 36
37 37 chartView->setChartTitle("Simple Pie Chart");
38 38 //! [3]
39 39 // Add pie series
40 40 // ...
41 41 QPieSeries *pie = new QPieSeries();
42 pie->add(3.4, "slice1");
43 pie->add(6.7, "slice2");
42 pie->append(3.4, "slice1");
43 pie->append(6.7, "slice2");
44 44 chartView->addSeries(pie);
45 45 //! [3]
46 46
47 47 chartView->setChartTitle("Simple Scatter Chart");
48 48 //! [4]
49 49 // Add scatter series
50 50 // ...
51 51 QScatterSeries *scatter = new QScatterSeries();
52 52 for (qreal x(0); x < 100; x += 0.5) {
53 53 qreal y = rand() % 100;
54 54 *(scatter) << QPointF(x, y);
55 55 }
56 56 chartView->addSeries(scatter);
57 57 //! [4]
58 58
59 59 chartView->setChartTitle("Simple Bar Chart");
60 60 //! [5]
61 61 // ...
62 62 // Add bar series
63 63 QStringList barCategory;
64 64 barCategory << "Jan"
65 65 << "Feb"
66 66 << "Mar";
67 67 QBarSeries *bar = new QBarSeries(barCategory);
68 68 QBarSet *barSet = new QBarSet("Sales");
69 69 *barSet << 123.2
70 70 << 301.3
71 71 << 285.8;
72 72 bar->addBarSet(barSet);
73 73 chartView->addSeries(bar);
74 74 //! [5]
75 75
76 76 QMainWindow w;
77 77 w.resize(400, 300);
78 78 w.setCentralWidget(chartView);
79 79 w.setWindowFlags(Qt::FramelessWindowHint);
80 80 w.show();
81 81
82 82 return a.exec();
83 83 }
@@ -1,60 +1,60
1 1 #include <QTimer>
2 2 #include <QTime>
3 3 #include <QObject>
4 4 #include <cmath>
5 5 #include <qlineseries.h>
6 6
7 7 QTCOMMERCIALCHART_USE_NAMESPACE
8 8
9 9 #define PI 3.14159265358979
10 10 static const int numPoints =100;
11 11
12 12 class WaveGenerator: public QObject
13 13 {
14 14 Q_OBJECT
15 15
16 16 public:
17 17 WaveGenerator(QLineSeries* series1, QLineSeries* series2) :
18 18 m_series1(series1),
19 19 m_series2(series2),
20 20 m_wave(0),
21 21 m_step(2*PI/numPoints)
22 22 {
23 23
24 24 QTime now = QTime::currentTime();
25 25 qsrand((uint)now.msec());
26 26
27 27 int fluctuate = 100;
28 28
29 29 for (qreal x = 0; x <= 2*PI; x+=m_step) {
30 series1->add(x, fabs(sin(x)*fluctuate));
31 series2->add(x, fabs(cos(x)*fluctuate));
30 series1->append(x, fabs(sin(x)*fluctuate));
31 series2->append(x, fabs(cos(x)*fluctuate));
32 32 }
33 33
34 34 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(update()));
35 35 m_timer.setInterval(5000);
36 36 m_timer.start();
37 37
38 38 };
39 39
40 40 public slots:
41 41 void update()
42 42 {
43 43 int fluctuate;
44 44
45 45 for (qreal i = 0, x = 0; x <= 2*PI; x+=m_step, i++) {
46 46 fluctuate = qrand() % 100;
47 47 m_series1->replace(x, fabs(sin(x)*fluctuate));
48 48 fluctuate = qrand() % 100;
49 49 m_series2->replace(x, fabs(cos(x)*fluctuate));
50 50 }
51 51
52 52 }
53 53
54 54 private:
55 55 QLineSeries* m_series1;
56 56 QLineSeries* m_series2;
57 57 int m_wave;
58 58 qreal m_step;
59 59 QTimer m_timer;
60 60 };
@@ -1,57 +1,57
1 1 #include "chartview.h"
2 2 #include <QSplineSeries>
3 3 #include <QTime>
4 4
5 5 ChartView::ChartView(QWidget* parent):QChartView(parent),
6 6 m_step(1),
7 7 m_x(0),
8 8 m_y(1)
9 9 {
10 10 QTime now = QTime::currentTime();
11 11 qsrand((uint)now.msec());
12 12 setChartTitle("Three random line charts");
13 13
14 14 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
15 15 m_timer.setInterval(1000);
16 16
17 17 m_series0 = new QLineSeries(this);
18 18 QPen blue(Qt::blue);
19 19 blue.setWidth(3);
20 20 m_series0->setPen(blue);
21 21
22 22
23 23 m_series1 = new QSplineSeries(this);
24 24 QPen green(Qt::red);
25 25 green.setWidth(3);
26 26 m_series1->setPen(green);
27 27
28 m_series0->add(m_x,m_y);
29 m_series1->add(m_x,m_y);
28 m_series0->append(m_x,m_y);
29 m_series1->append(m_x,m_y);
30 30
31 31 setChartTitle("Simple EKG chart");
32 32 addSeries(m_series0);
33 33 addSeries(m_series1);
34 34 axisY()->setRange(-5,5);
35 35 axisX()->setRange(-9,1);
36 36 axisX()->setTicksCount(11);
37 37 m_timer.start();
38 38 }
39 39
40 40 ChartView::~ChartView()
41 41 {
42 42
43 43 }
44 44
45 45 void ChartView::handleTimeout()
46 46 {
47 47 m_x+=m_step;
48 48 m_y = qrand() % 5 - 2.5;
49 m_series0->add(m_x,m_y);
50 m_series1->add(m_x,m_y);
49 m_series0->append(m_x,m_y);
50 m_series1->append(m_x,m_y);
51 51 if(m_x>=10)
52 52 {
53 53 m_series0->remove(m_x-10);
54 54 m_series1->remove(m_x-10);
55 55 }
56 56 scrollRight();
57 57 }
@@ -1,89 +1,89
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:BSD$
10 10 ** You may use this file under the terms of the BSD license as follows:
11 11 **
12 12 ** "Redistribution and use in source and binary forms, with or without
13 13 ** modification, are permitted provided that the following conditions are
14 14 ** met:
15 15 ** * Redistributions of source code must retain the above copyright
16 16 ** notice, this list of conditions and the following disclaimer.
17 17 ** * Redistributions in binary form must reproduce the above copyright
18 18 ** notice, this list of conditions and the following disclaimer in
19 19 ** the documentation and/or other materials provided with the
20 20 ** distribution.
21 21 ** * Neither the name of Digia nor the names of its contributors
22 22 ** may be used to endorse or promote products derived from this
23 23 ** software without specific prior written permission.
24 24 **
25 25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 36 ** $QT_END_LICENSE$
37 37 **
38 38 ****************************************************************************/
39 39
40 40 #include <QApplication>
41 41 #include <QMainWindow>
42 42 #include <QChartView>
43 43 #include <QLineSeries>
44 44
45 45 QTCOMMERCIALCHART_USE_NAMESPACE
46 46
47 47 int main(int argc, char *argv[])
48 48 {
49 49 QApplication a(argc, argv);
50 50 //![1]
51 51 QLineSeries* series0 = new QLineSeries();
52 52 QPen blue(Qt::blue);
53 53 blue.setWidth(3);
54 54 series0->setPen(blue);
55 55
56 56 QLineSeries* series1 = new QLineSeries();
57 57 QPen red(Qt::red);
58 58 red.setWidth(3);
59 59 series1->setPen(red);
60 60 //![1]
61 61
62 62 //![2]
63 series0->add(0, 6);
64 series0->add(2, 4);
65 series0->add(3, 8);
66 series0->add(7, 4);
67 series0->add(10, 5);
63 series0->append(0, 6);
64 series0->append(2, 4);
65 series0->append(3, 8);
66 series0->append(7, 4);
67 series0->append(10, 5);
68 68
69 69 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2);
70 70 //![2]
71 71 //![3]
72 72 QChart* chart = new QChart();
73 73
74 74 chart->addSeries(series0);
75 75 chart->addSeries(series1);
76 76 chart->setTitle("Simple line chart example");
77 77 //![3]
78 78 //![4]
79 79 QChartView* chartView = new QChartView(chart);
80 80 chartView->setRenderHint(QPainter::Antialiasing);
81 81 //![4]
82 82 //![5]
83 83 QMainWindow window;
84 84 window.setCentralWidget(chartView);
85 85 window.resize(400, 300);
86 86 window.show();
87 87 //![5]
88 88 return a.exec();
89 89 }
@@ -1,35 +1,35
1 1 #include "multichartwidget.h"
2 2 #include <QVBoxLayout>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qscatterseries.h>
6 6 #include <qpieseries.h>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 MultiChartWidget::MultiChartWidget(QWidget *parent) :
11 11 QWidget(parent)
12 12 {
13 13 QVBoxLayout *l = new QVBoxLayout(this);
14 14
15 15 // Create chart 1 and add a simple pie series onto it
16 16 QChartView *chartView1 = new QChartView();
17 17 l->addWidget(chartView1);
18 18 QPieSeries *pie = new QPieSeries();
19 pie->add(1.1, "label1");
20 pie->add(1.2, "label2");
19 pie->append(1.1, "label1");
20 pie->append(1.2, "label2");
21 21 chartView1->addSeries(pie);
22 22
23 23 // Create chart 2 and add a simple scatter series onto it
24 24 QChartView *chartView2 = new QChartView();
25 25 l->addWidget(chartView2);
26 26 QScatterSeries *scatter = new QScatterSeries();
27 27 *scatter << QPointF(0.5, 5.0)
28 28 << QPointF(1.0, 4.5)
29 29 << QPointF(1.0, 5.5)
30 30 << QPointF(1.5, 5.0)
31 31 << QPointF(2.0, 4.5)
32 32 << QPointF(2.0, 5.5)
33 33 << QPointF(2.5, 5.0);
34 34 chartView2->addSeries(scatter);
35 35 }
@@ -1,84 +1,84
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:BSD$
10 10 ** You may use this file under the terms of the BSD license as follows:
11 11 **
12 12 ** "Redistribution and use in source and binary forms, with or without
13 13 ** modification, are permitted provided that the following conditions are
14 14 ** met:
15 15 ** * Redistributions of source code must retain the above copyright
16 16 ** notice, this list of conditions and the following disclaimer.
17 17 ** * Redistributions in binary form must reproduce the above copyright
18 18 ** notice, this list of conditions and the following disclaimer in
19 19 ** the documentation and/or other materials provided with the
20 20 ** distribution.
21 21 ** * Neither the name of Digia nor the names of its contributors
22 22 ** may be used to endorse or promote products derived from this
23 23 ** software without specific prior written permission.
24 24 **
25 25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 36 ** $QT_END_LICENSE$
37 37 **
38 38 ****************************************************************************/
39 39
40 40 #include <QApplication>
41 41 #include <QMainWindow>
42 42 #include <QChartView>
43 43 #include <QPieSeries>
44 44 #include <QPieSlice>
45 45
46 46 QTCOMMERCIALCHART_USE_NAMESPACE
47 47
48 48 int main(int argc, char *argv[])
49 49 {
50 50 QApplication a(argc, argv);
51 51
52 52 //![1]
53 53 QPieSeries *series = new QPieSeries();
54 series->add(1, "Slice 1");
55 series->add(2, "Slice 2");
56 series->add(3, "Slice 3");
57 series->add(4, "Slice 4");
58 series->add(5, "Slice 5");
54 series->append(1, "Slice 1");
55 series->append(2, "Slice 2");
56 series->append(3, "Slice 3");
57 series->append(4, "Slice 4");
58 series->append(5, "Slice 5");
59 59 //![1]
60 60
61 61 //![2]
62 62 QPieSlice *slice = series->slices().first();
63 63 slice->setExploded();
64 64 slice->setLabelVisible();
65 65 slice->setPen(QPen(Qt::darkGreen, 2));
66 66 slice->setBrush(Qt::green);
67 67 //![2]
68 68 //![3]
69 69 QChart* chart = new QChart();
70 70 chart->addSeries(series);
71 71 chart->setTitle("Simple piechart example");
72 72 //![3]
73 73 //![4]
74 74 QChartView* chartView = new QChartView(chart);
75 75 chartView->setRenderHint(QPainter::Antialiasing);
76 76 //![4]
77 77 //![5]
78 78 QMainWindow window;
79 79 window.setCentralWidget(chartView);
80 80 window.resize(400, 300);
81 81 window.show();
82 82 //![5]
83 83 return a.exec();
84 84 }
@@ -1,97 +1,97
1 1 #include "chartview.h"
2 2 #include <QLineSeries>
3 3 #include <QScatterSeries>
4 4 #include <QSplineSeries>
5 5 #include <QAreaSeries>
6 6 #include <QTime>
7 7
8 8 ChartView::ChartView(QWidget* parent):QChartView(parent),
9 9 m_index(0)
10 10 {
11 11 setChartTitle("Charts presenter");
12 12 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
13 13 m_timer.setInterval(3000);
14 14
15 15 //![1]
16 16 QLineSeries* series0 = new QLineSeries(this);
17 17 QPen blue(Qt::blue);
18 18 blue.setWidth(3);
19 19 series0->setPen(blue);
20 20
21 21 QScatterSeries* series1 = new QScatterSeries(this);
22 22 QPen red(Qt::red);
23 23 red.setWidth(3);
24 24 series1->setPen(red);
25 25 series1->setBrush(Qt::white);
26 26
27 27 QSplineSeries* series2 = new QSplineSeries(this);
28 28 QPen green(Qt::green);
29 29 green.setWidth(3);
30 30 series2->setPen(green);
31 31
32 32 QAreaSeries* series3 = new QAreaSeries(series0);
33 33 QPen yellow(Qt::black);
34 34 yellow.setWidth(3);
35 35 series3->setPen(yellow);
36 36 series3->setBrush(Qt::yellow);
37 37 //![1]
38 38
39 39 //![2]
40 40 int numPoints = 10;
41 41
42 42 for (int x = 0; x <= numPoints; ++x) {
43 43 qreal y = qrand() % 100;
44 series0->add(x,y);
45 series1->add(x,y);
46 series2->add(x,y);
44 series0->append(x,y);
45 series1->append(x,y);
46 series2->append(x,y);
47 47 }
48 48 //![2]
49 49
50 50 //![3]
51 51 m_series<<series0;
52 52 m_titles<<chartTitle()+": LineChart";
53 53 m_series<<series1;
54 54 m_titles<<chartTitle()+": ScatterChart";
55 55 m_series<<series2;
56 56 m_titles<<chartTitle()+": SplineChart";
57 57 m_series<<series3;
58 58 m_titles<<chartTitle()+": AreaChart";
59 59 //![3]
60 60
61 61 addSeries(series0);
62 62 setChartTitle(m_titles.at(0));
63 63
64 64 //![4]
65 65 foreach (QSeries* series, m_series) {
66 66 QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&)));
67 67 }
68 68 //![4]
69 69
70 70 m_timer.start();
71 71 }
72 72
73 73 ChartView::~ChartView()
74 74 {
75 75 if(m_series.size()==0) return;
76 76 removeSeries(m_series.at(m_index));
77 77 qDeleteAll(m_series);
78 78 }
79 79
80 80 //![5]
81 81 void ChartView::handleTimeout()
82 82 {
83 83 if(m_series.size()==0) return;
84 84 removeSeries(m_series.at(m_index));
85 85 m_index++;
86 86 m_index=m_index%m_series.size();
87 87 addSeries(m_series.at(m_index));
88 88 setChartTitle(m_titles.at(m_index));
89 89 }
90 90 //![5]
91 91
92 92 //![6]
93 93 void ChartView::handlePointClicked(const QPointF& point)
94 94 {
95 95 setChartTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y()));
96 96 }
97 97 //![6]
@@ -1,96 +1,96
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:BSD$
10 10 ** You may use this file under the terms of the BSD license as follows:
11 11 **
12 12 ** "Redistribution and use in source and binary forms, with or without
13 13 ** modification, are permitted provided that the following conditions are
14 14 ** met:
15 15 ** * Redistributions of source code must retain the above copyright
16 16 ** notice, this list of conditions and the following disclaimer.
17 17 ** * Redistributions in binary form must reproduce the above copyright
18 18 ** notice, this list of conditions and the following disclaimer in
19 19 ** the documentation and/or other materials provided with the
20 20 ** distribution.
21 21 ** * Neither the name of Digia nor the names of its contributors
22 22 ** may be used to endorse or promote products derived from this
23 23 ** software without specific prior written permission.
24 24 **
25 25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 36 ** $QT_END_LICENSE$
37 37 **
38 38 ****************************************************************************/
39 39
40 40 #include <QtGui/QApplication>
41 41 #include <QMainWindow>
42 42 #include <QChartView>
43 43 #include <QScatterSeries>
44 44
45 45 QTCOMMERCIALCHART_USE_NAMESPACE
46 46
47 47 int main(int argc, char *argv[])
48 48 {
49 49 QApplication a(argc, argv);
50 50 //![1]
51 51 QPen pen(Qt::black);
52 52 pen.setWidth(2);
53 53 QBrush blue(Qt::blue);
54 54 QBrush red(Qt::red);
55 55
56 56 QScatterSeries *series0 = new QScatterSeries();
57 57 series0->setPen(pen);
58 58 series0->setBrush(blue);
59 59 series0->setShape(QScatterSeries::MarkerShapeCircle);
60 60 series0->setSize(15.0);
61 61
62 62 QScatterSeries *series1 = new QScatterSeries();
63 63 series1->setPen(pen);
64 64 series1->setBrush(red);
65 65 series1->setShape(QScatterSeries::MarkerShapeCircle);
66 66 series1->setSize(15.0);
67 67 //![1]
68 68
69 69 //![2]
70 series0->add(0, 6);
71 series0->add(2, 4);
72 series0->add(3, 8);
73 series0->add(7, 4);
74 series0->add(10, 5);
70 series0->append(0, 6);
71 series0->append(2, 4);
72 series0->append(3, 8);
73 series0->append(7, 4);
74 series0->append(10, 5);
75 75
76 76 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2);
77 77 //![2]
78 78 //![3]
79 79 QChart* chart = new QChart();
80 80
81 81 chart->addSeries(series0);
82 82 chart->addSeries(series1);
83 83 chart->setTitle("Simple scatterchart example");
84 84 //![3]
85 85 //![4]
86 86 QChartView* chartView = new QChartView(chart);
87 87 chartView->setRenderHint(QPainter::Antialiasing);
88 88 //![4]
89 89 //![5]
90 90 QMainWindow window;
91 91 window.setCentralWidget(chartView);
92 92 window.resize(400, 300);
93 93 window.show();
94 94 //![5]
95 95 return a.exec();
96 96 }
@@ -1,116 +1,116
1 1 #include "splinewidget.h"
2 2 #include "qchartview.h"
3 3 #include "qlineseries.h"
4 4 #include <QGridLayout>
5 5 #include <QPushButton>
6 6 #include "qchartaxis.h"
7 7 #include <qmath.h>
8 8 #include <QTime>
9 9
10 10 QTCOMMERCIALCHART_USE_NAMESPACE
11 11
12 12 SplineWidget::SplineWidget(QWidget *parent)
13 13 : QWidget(parent)
14 14 {
15 15 // qsrand(time(NULL));
16 16 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
17 17 //! [1]
18 18 //create QSplineSeries
19 19 series = new QSplineSeries(this);
20 20 //! [1]
21 21
22 22 //! [2]
23 23 // customize the series presentation settings
24 24 QPen seriesPen(Qt::blue);
25 25 seriesPen.setWidth(3);
26 26 // series->setPen(seriesPen);
27 27 //! [2]
28 28
29 29 //! [add points to series]
30 30 //add data points to the series
31 series->add(QPointF(150, 100));
32 series->add(QPointF(200, 130));
33 series->add(QPointF(250, 120));
34 series->add(QPointF(300, 140));
35 series->add(QPointF(350, 160));
31 series->append(QPointF(150, 100));
32 series->append(QPointF(200, 130));
33 series->append(QPointF(250, 120));
34 series->append(QPointF(300, 140));
35 series->append(QPointF(350, 160));
36 36 //! [add points to series]
37 37
38 38 QSplineSeries* series2 = new QSplineSeries;
39 39
40 series2->add(QPointF(400, 120));
41 series2->add(QPointF(450, 150));
42 series2->add(QPointF(500, 145));
43 series2->add(QPointF(550, 170));
40 series2->append(QPointF(400, 120));
41 series2->append(QPointF(450, 150));
42 series2->append(QPointF(500, 145));
43 series2->append(QPointF(550, 170));
44 44
45 // series->add(QPointF(600, 190));
46 // series->add(QPointF(650, 210));
47 // series->add(QPointF(700, 190));
48 // series->add(QPointF(750, 180));
49 // series->add(QPointF(800, 170));
45 // series->append(QPointF(600, 190));
46 // series->append(QPointF(650, 210));
47 // series->append(QPointF(700, 190));
48 // series->append(QPointF(750, 180));
49 // series->append(QPointF(800, 170));
50 50 QSplineSeries* series3 = new QSplineSeries;
51 series3->add(QPointF(600, 190));
52 series3->add(QPointF(650, 210));
53 series3->add(QPointF(700, 190));
54 series3->add(QPointF(750, 180));
55 series3->add(QPointF(800, 170));
51 series3->append(QPointF(600, 190));
52 series3->append(QPointF(650, 210));
53 series3->append(QPointF(700, 190));
54 series3->append(QPointF(750, 180));
55 series3->append(QPointF(800, 170));
56 56
57 57 //! [3]
58 58 // create chart view
59 59 QChartView* chart = new QChartView;
60 60 chart->addSeries(series);
61 61 chart->addSeries(series2);
62 62 chart->addSeries(series3);
63 63
64 64 chart->setChartTitle("Spline chart example");
65 65 chart->axisX()->setMax(1500);
66 66 chart->axisY()->setMax(500);
67 67
68 68 chart->setMinimumSize(800,600);
69 69 //! [3]
70 70
71 71 //! [4]
72 72 //add new data point button
73 73 QPushButton* addButton = new QPushButton("Add new point");
74 74 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
75 75
76 76 // remove the last data point in the series
77 77 QPushButton* removeButton = new QPushButton("Remove point");
78 78 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
79 79 //! [4]
80 80
81 81 //! [5]
82 82 //butttons layout
83 83 QVBoxLayout* buttonsLayout = new QVBoxLayout;
84 84 buttonsLayout->addWidget(addButton);
85 85 buttonsLayout->addWidget(removeButton);
86 86 buttonsLayout->addStretch();
87 87
88 88 QGridLayout* mainLayout = new QGridLayout;
89 89 mainLayout->addWidget(chart, 1, 0);
90 90 mainLayout->addLayout(buttonsLayout, 1, 1);
91 91 setLayout(mainLayout);
92 92 //! [5]
93 93 }
94 94
95 95 //! [add point]
96 96 void SplineWidget::addNewPoint()
97 97 {
98 98 if (series->count() > 0)
99 series->add(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100)));
99 series->append(QPointF(series->x(series->count() - 1) + 40 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100)));
100 100 else
101 series->add(QPointF(50, 50 + qrand()%50));
101 series->append(QPointF(50, 50 + qrand()%50));
102 102 }
103 103 //! [add point]
104 104
105 105 //! [remove point]
106 106 void SplineWidget::removePoint()
107 107 {
108 108 if (series->count() > 0)
109 109 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
110 110 }
111 111 //! [remove point]
112 112
113 113 SplineWidget::~SplineWidget()
114 114 {
115 115
116 116 }
@@ -1,43 +1,43
1 1 #include "declarativepieseries.h"
2 2 #include "declarativechart.h"
3 3 #include "qchart.h"
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
8 8 QPieSeries(parent),
9 9 m_chart(0)
10 10 {
11 11 }
12 12
13 13 void DeclarativePieSeries::classBegin()
14 14 {
15 15 }
16 16
17 17 void DeclarativePieSeries::componentComplete()
18 18 {
19 19 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
20 20 if (declarativeChart) {
21 21 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
22 22 Q_ASSERT(chart);
23 23 qDebug() << "parent for pie:" << chart;
24 24 chart->addSeries(this);
25 25 }
26 26 }
27 27
28 28 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::slices()
29 29 {
30 30 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendSlice);
31 31 }
32 32
33 33 void DeclarativePieSeries::appendSlice(QDeclarativeListProperty<QPieSlice> *list,
34 34 QPieSlice *slice)
35 35 {
36 36 DeclarativePieSeries *series = qobject_cast<DeclarativePieSeries *>(list->object);
37 37 if (series)
38 series->add(slice->value(), slice->label());
38 series->append(slice->value(), slice->label());
39 39 }
40 40
41 41 #include "moc_declarativepieseries.cpp"
42 42
43 43 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,27 +1,27
1 1 //#include "DeclarativeXySeries.h"
2 2 #include "declarativexyseries.h"
3 3 #include "qxyseries.h"
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 DeclarativeXySeries::DeclarativeXySeries()
8 8 {
9 9 }
10 10
11 11 DeclarativeXySeries::~DeclarativeXySeries()
12 12 {
13 13 }
14 14
15 15 void DeclarativeXySeries::classBegin()
16 16 {
17 17 }
18 18
19 19 void DeclarativeXySeries::appendPoints(QDeclarativeListProperty<DeclarativeXyPoint> *list,
20 20 DeclarativeXyPoint *element)
21 21 {
22 22 QXYSeries *series = qobject_cast<QXYSeries *>(list->object);
23 23 if (series)
24 series->add(QPointF(element->x(), element->y()));
24 series->append(QPointF(element->x(), element->y()));
25 25 }
26 26
27 27 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,170 +1,170
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 "qareaseries.h"
22 22 #include "qlineseries.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QAreaSeries
28 28 \brief The QAreaSeries class is used for making area charts.
29 29
30 30 \mainclass
31 31
32 32 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
33 33 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
34 34 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
35 35 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
36 36 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
37 37
38 38 \image areachart.png
39 39
40 40 Creating basic area chart is simple:
41 41 \code
42 42 QLineSeries* lineSeries = new QLineSeries();
43 series->add(0, 6);
44 series->add(2, 4);
43 series->append(0, 6);
44 series->append(2, 4);
45 45 QAreaSeries* areaSeries = new QAreaSeries(lineSeries);
46 46 ...
47 47 chartView->addSeries(areaSeries);
48 48 \endcode
49 49 */
50 50
51 51 /*!
52 52 \fn virtual QSeriesType QAreaSeries::type() const
53 53 \brief Returns type of series.
54 54 \sa QSeries, QSeriesType
55 55 */
56 56
57 57 /*!
58 58 \fn QLineSeries* QAreaSeries::upperSeries() const
59 59 \brief Returns upperSeries used to define one of area boundaries.
60 60 */
61 61
62 62 /*!
63 63 \fn QLineSeries* QAreaSeries::lowerSeries() const
64 64 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
65 65 this function return Null pointer.
66 66 */
67 67
68 68 /*!
69 69 \fn QPen QAreaSeries::pen() const
70 70 \brief Returns the pen used to draw line for this series.
71 71 \sa setPen()
72 72 */
73 73
74 74 /*!
75 75 \fn QPen QAreaSeries::brush() const
76 76 \brief Returns the brush used to draw line for this series.
77 77 \sa setBrush()
78 78 */
79 79
80 80 /*!
81 81 \fn bool QAreaSeries::pointsVisible() const
82 82 \brief Returns if the points are drawn for this series.
83 83 \sa setPointsVisible()
84 84 */
85 85
86 86 /*!
87 87 \fn void QAreaSeries::clicked(const QPointF& point)
88 88 \brief Signal is emitted when user clicks the \a point on area chart.
89 89 */
90 90
91 91 /*!
92 92 \fn void QAreaSeries::updated()
93 93 \brief \internal
94 94 */
95 95
96 96 /*!
97 97 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
98 98 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
99 99 When series object is added to QChartView or QChart instance ownerships is transfered.
100 100 */
101 101 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
102 102 : QSeries(upperSeries),
103 103 m_upperSeries(upperSeries),
104 104 m_lowerSeries(lowerSeries),
105 105 m_pointsVisible(false)
106 106 {
107 107 }
108 108
109 109 /*!
110 110 Destroys the object. Series added to QChartView or QChart instances are owned by those,
111 111 and are deleted when mentioned object are destroyed.
112 112 */
113 113 QAreaSeries::~QAreaSeries()
114 114 {
115 115 }
116 116
117 117 /*!
118 118 Sets \a pen used for drawing area outline.
119 119 */
120 120 void QAreaSeries::setPen(const QPen &pen)
121 121 {
122 122 if (m_pen != pen) {
123 123 m_pen = pen;
124 124 emit updated();
125 125 }
126 126 }
127 127
128 128 /*!
129 129 Sets \a brush used for filling the area.
130 130 */
131 131 void QAreaSeries::setBrush(const QBrush &brush)
132 132 {
133 133 if (m_brush != brush) {
134 134 m_brush = brush;
135 135 emit updated();
136 136 }
137 137 }
138 138 /*!
139 139 Sets if data points are \a visible and should be drawn on line.
140 140 */
141 141 void QAreaSeries::setPointsVisible(bool visible)
142 142 {
143 143 if (m_pointsVisible != visible) {
144 144 m_pointsVisible = visible;
145 145 emit updated();
146 146 }
147 147 }
148 148
149 149 //bool QAreaSeries::setModel(QAbstractItemModel* model)
150 150 //{
151 151 // m_upperSeries->setModel(model);
152 152 // if (m_lowerSeries)
153 153 // m_lowerSeries->setModel(model);
154 154 // return true;
155 155 //}
156 156
157 157 //void QAreaSeries::setModelMappingUpper(int modelX, int modelY, Qt::Orientation orientation)
158 158 //{
159 159 // m_upperSeries->setModelMapping(modelX, modelY, orientation);
160 160 //}
161 161
162 162 //void QAreaSeries::setModelMappingLower(int modelX, int modelY, Qt::Orientation orientation)
163 163 //{
164 164 // if (m_lowerSeries)
165 165 // m_lowerSeries->setModelMapping(modelX, modelY, orientation);
166 166 //}
167 167
168 168 #include "moc_qareaseries.cpp"
169 169
170 170 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,105 +1,105
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 "qlineseries.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QLineSeries
27 27 \brief The QLineSeries class is used for making line charts.
28 28
29 29 \mainclass
30 30
31 31 A line chart is used to show information as a series of data points
32 32 connected by straight lines.
33 33
34 34 \image linechart.png
35 35
36 36 Creating basic line chart is simple:
37 37 \code
38 38 QLineSeries* series = new QLineSeries();
39 series->add(0, 6);
40 series->add(2, 4);
39 series->append(0, 6);
40 series->append(2, 4);
41 41 ...
42 42 chartView->addSeries(series);
43 43 \endcode
44 44 */
45 45
46 46 /*!
47 47 \fn virtual QSeriesType QLineSeries::type() const
48 48 \brief Returns type of series.
49 49 \sa QSeries, QSeriesType
50 50 */
51 51
52 52 /*!
53 53 \fn bool QLineSeries::pointsVisible() const
54 54 \brief Returns if the points are drawn for this series.
55 55 \sa setPointsVisible()
56 56 */
57 57
58 58 /*!
59 59 \fn QPen QLineSeries::linePen() const
60 60 \brief Returns the pen used to draw line connecting points.
61 61 \sa setPen()
62 62 */
63 63
64 64 /*!
65 65 Constructs empty series object which is a child of \a parent.
66 66 When series object is added to QChartView or QChart instance ownerships is transfered.
67 67 */
68 68 QLineSeries::QLineSeries(QObject *parent) : QXYSeries(parent),
69 69 m_pointsVisible(false)
70 70 {
71 71
72 72 }
73 73 /*!
74 74 Destroys the object. Series added to QChartView or QChart instances are owned by those,
75 75 and are deleted when mentioned object are destroyed.
76 76 */
77 77 QLineSeries::~QLineSeries()
78 78 {
79 79 }
80 80
81 81 /*!
82 82 Sets if data points are \a visible and should be drawn on line.
83 83 */
84 84 void QLineSeries::setPointsVisible(bool visible)
85 85 {
86 86 if (m_pointsVisible != visible){
87 87 m_pointsVisible = visible;
88 88 emit QXYSeries::updated();
89 89 }
90 90 }
91 91
92 92
93 93 QDebug operator<< (QDebug debug, const QLineSeries series)
94 94 {
95 95 Q_ASSERT(series.m_x.size() == series.m_y.size());
96 96
97 97 int size = series.m_x.size();
98 98
99 99 for (int i=0; i<size; i++) {
100 100 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
101 101 }
102 102 return debug.space();
103 103 }
104 104
105 105 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,657 +1,657
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 "qpieseries.h"
22 22 #include "qpiesliceprivate_p.h"
23 23 #include "qpieseriesprivate_p.h"
24 24 #include <QDebug>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent)
29 29 :QObject(parent),
30 30 q_ptr(parent),
31 31 m_pieRelativeHorPos(0.5),
32 32 m_pieRelativeVerPos(0.5),
33 33 m_pieRelativeSize(0.7),
34 34 m_pieStartAngle(0),
35 35 m_pieEndAngle(360),
36 36 m_total(0)
37 37 {
38 38
39 39 }
40 40
41 41 QPieSeriesPrivate::~QPieSeriesPrivate()
42 42 {
43 43
44 44 }
45 45
46 46 void QPieSeriesPrivate::updateDerivativeData()
47 47 {
48 48 m_total = 0;
49 49
50 50 // nothing to do?
51 51 if (m_slices.count() == 0)
52 52 return;
53 53
54 54 // calculate total
55 55 foreach (QPieSlice* s, m_slices)
56 56 m_total += s->value();
57 57
58 58 // nothing to show..
59 59 if (qFuzzyIsNull(m_total))
60 60 return;
61 61
62 62 // update slice attributes
63 63 qreal sliceAngle = m_pieStartAngle;
64 64 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
65 65 QVector<QPieSlice*> changed;
66 66 foreach (QPieSlice* s, m_slices) {
67 67
68 68 PieSliceData data = s->data_ptr()->m_data;
69 69 data.m_percentage = s->value() / m_total;
70 70 data.m_angleSpan = pieSpan * data.m_percentage;
71 71 data.m_startAngle = sliceAngle;
72 72 sliceAngle += data.m_angleSpan;
73 73
74 74 if (s->data_ptr()->m_data != data) {
75 75 s->data_ptr()->m_data = data;
76 76 changed << s;
77 77 }
78 78 }
79 79
80 80 // emit signals
81 81 foreach (QPieSlice* s, changed)
82 82 emit s->data_ptr()->changed();
83 83 }
84 84
85 85 void QPieSeriesPrivate::sliceChanged()
86 86 {
87 87 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
88 88 updateDerivativeData();
89 89 }
90 90
91 91 void QPieSeriesPrivate::sliceClicked(Qt::MouseButtons buttons)
92 92 {
93 93 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
94 94 Q_ASSERT(m_slices.contains(slice));
95 95 Q_Q(QPieSeries);
96 96 emit q->clicked(slice, buttons);
97 97 }
98 98
99 99 void QPieSeriesPrivate::sliceHoverEnter()
100 100 {
101 101 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
102 102 Q_ASSERT(m_slices.contains(slice));
103 103 Q_Q(QPieSeries);
104 104 emit q->hoverEnter(slice);
105 105 }
106 106
107 107 void QPieSeriesPrivate::sliceHoverLeave()
108 108 {
109 109 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
110 110 Q_ASSERT(m_slices.contains(slice));
111 111 Q_Q(QPieSeries);
112 112 emit q->hoverLeave(slice);
113 113 }
114 114
115 115 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
116 116 {
117 117 Q_UNUSED(bottomRight)
118 118 Q_Q(QPieSeries);
119 119
120 120 if (m_mapOrientation == Qt::Vertical)
121 121 {
122 122 // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
123 123 if (topLeft.column() == m_mapValues)
124 124 if (m_mapValues == m_mapLabels)
125 125 {
126 126 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
127 127 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
128 128 }
129 129 else
130 130 {
131 131 m_slices.at(topLeft.row())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
132 132 }
133 133 else if (topLeft.column() == m_mapLabels)
134 134 m_slices.at(topLeft.row())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
135 135 }
136 136 else
137 137 {
138 138 // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble());
139 139 if (topLeft.row() == m_mapValues)
140 140 if (m_mapValues == m_mapLabels)
141 141 {
142 142 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
143 143 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
144 144 }
145 145 else
146 146 {
147 147 m_slices.at(topLeft.column())->setValue(q->m_model->data(topLeft, Qt::DisplayRole).toDouble());
148 148 }
149 149 else if (topLeft.row() == m_mapLabels)
150 150 m_slices.at(topLeft.column())->setLabel(q->m_model->data(topLeft, Qt::DisplayRole).toString());
151 151 }
152 152 }
153 153
154 154 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
155 155 {
156 156 Q_UNUSED(parent)
157 157 Q_UNUSED(end)
158 158 Q_Q(QPieSeries);
159 159
160 160 QPieSlice* newSlice = new QPieSlice;
161 161 newSlice->setLabelVisible(true);
162 162 if (m_mapOrientation == Qt::Vertical)
163 163 {
164 164 newSlice->setValue(q->m_model->data(q->m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
165 165 newSlice->setLabel(q->m_model->data(q->m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
166 166 }
167 167 else
168 168 {
169 169 newSlice->setValue(q->m_model->data(q->m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
170 170 newSlice->setLabel(q->m_model->data(q->m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
171 171 }
172 172
173 173 q->insert(start, newSlice);
174 174 }
175 175
176 176 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
177 177 {
178 178 Q_UNUSED(parent)
179 179 Q_UNUSED(end)
180 180 Q_Q(QPieSeries);
181 181 q->remove(m_slices.at(start));
182 182 }
183 183
184 184
185 185
186 186 /*!
187 187 \class QPieSeries
188 188 \brief Pie series API for QtCommercial Charts
189 189
190 190 The pie series defines a pie chart which consists of pie slices which are QPieSlice objects.
191 191 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
192 192 The actual slice size is determined by that relative value.
193 193
194 194 By default the pie is defined as a full pie but it can be a partial pie.
195 195 This can be done by setting a starting angle and angle span to the series.
196 196 */
197 197
198 198 /*!
199 199 Constructs a series object which is a child of \a parent.
200 200 */
201 201 QPieSeries::QPieSeries(QObject *parent) :
202 202 QSeries(parent),
203 203 d_ptr(new QPieSeriesPrivate(this))
204 204 {
205 205
206 206 }
207 207
208 208 /*!
209 209 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
210 210 */
211 211 QPieSeries::~QPieSeries()
212 212 {
213 213 // NOTE: d_prt destroyed by QObject
214 214 }
215 215
216 216 /*!
217 217 Returns QChartSeries::SeriesTypePie.
218 218 */
219 219 QSeries::QSeriesType QPieSeries::type() const
220 220 {
221 221 return QSeries::SeriesTypePie;
222 222 }
223 223
224 224 /*!
225 225 Sets an array of \a slices to the series replacing the existing slices.
226 226 Slice ownership is passed to the series.
227 227 */
228 228 void QPieSeries::replace(QList<QPieSlice*> slices)
229 229 {
230 230 clear();
231 add(slices);
231 append(slices);
232 232 }
233 233
234 234 /*!
235 235 Adds an array of \a slices to the series.
236 236 Slice ownership is passed to the series.
237 237 */
238 void QPieSeries::add(QList<QPieSlice*> slices)
238 void QPieSeries::append(QList<QPieSlice*> slices)
239 239 {
240 240 Q_D(QPieSeries);
241 241
242 242 foreach (QPieSlice* s, slices) {
243 243 s->setParent(this);
244 244 d->m_slices << s;
245 245 }
246 246
247 247 d->updateDerivativeData();
248 248
249 249 foreach (QPieSlice* s, slices) {
250 250 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
251 251 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
252 252 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
253 253 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
254 254 }
255 255
256 256 emit added(slices);
257 257 }
258 258
259 259 /*!
260 260 Adds a single \a slice to the series.
261 261 Slice ownership is passed to the series.
262 262 */
263 void QPieSeries::add(QPieSlice* slice)
263 void QPieSeries::append(QPieSlice* slice)
264 264 {
265 add(QList<QPieSlice*>() << slice);
265 append(QList<QPieSlice*>() << slice);
266 266 }
267 267
268 268 /*!
269 269 Adds a single \a slice to the series and returns a reference to the series.
270 270 Slice ownership is passed to the series.
271 271 */
272 272 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
273 273 {
274 add(slice);
274 append(slice);
275 275 return *this;
276 276 }
277 277
278 278
279 279 /*!
280 280 Adds a single slice to the series with give \a value and \a name.
281 281 Slice ownership is passed to the series.
282 282 */
283 QPieSlice* QPieSeries::add(qreal value, QString name)
283 QPieSlice* QPieSeries::append(qreal value, QString name)
284 284 {
285 285 QPieSlice* slice = new QPieSlice(value, name);
286 add(slice);
286 append(slice);
287 287 return slice;
288 288 }
289 289
290 290 void QPieSeries::insert(int i, QPieSlice* slice)
291 291 {
292 292 Q_D(QPieSeries);
293 293 Q_ASSERT(i <= d->m_slices.count());
294 294 slice->setParent(this);
295 295 d->m_slices.insert(i, slice);
296 296
297 297 d->updateDerivativeData();
298 298
299 299 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
300 300 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
301 301 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
302 302 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
303 303
304 304 emit added(QList<QPieSlice*>() << slice);
305 305 }
306 306
307 307 /*!
308 308 Removes a single \a slice from the series and deletes the slice.
309 309
310 310 Do not reference this pointer after this call.
311 311 */
312 312 void QPieSeries::remove(QPieSlice* slice)
313 313 {
314 314 Q_D(QPieSeries);
315 315 if (!d->m_slices.removeOne(slice)) {
316 316 Q_ASSERT(0); // TODO: how should this be reported?
317 317 return;
318 318 }
319 319
320 320 d->updateDerivativeData();
321 321
322 322 emit removed(QList<QPieSlice*>() << slice);
323 323
324 324 delete slice;
325 325 slice = NULL;
326 326 }
327 327
328 328 /*!
329 329 Clears all slices from the series.
330 330 */
331 331 void QPieSeries::clear()
332 332 {
333 333 Q_D(QPieSeries);
334 334 if (d->m_slices.count() == 0)
335 335 return;
336 336
337 337 QList<QPieSlice*> slices = d->m_slices;
338 338 foreach (QPieSlice* s, d->m_slices) {
339 339 d->m_slices.removeOne(s);
340 340 delete s;
341 341 }
342 342
343 343 d->updateDerivativeData();
344 344
345 345 emit removed(slices);
346 346 }
347 347
348 348 /*!
349 349 Counts the number of the slices in this series.
350 350 */
351 351 int QPieSeries::count() const
352 352 {
353 353 Q_D(const QPieSeries);
354 354 return d->m_slices.count();
355 355 }
356 356
357 357 /*!
358 358 Returns true is the series is empty.
359 359 */
360 360 bool QPieSeries::isEmpty() const
361 361 {
362 362 Q_D(const QPieSeries);
363 363 return d->m_slices.isEmpty();
364 364 }
365 365
366 366 /*!
367 367 Returns a list of slices that belong to this series.
368 368 */
369 369 QList<QPieSlice*> QPieSeries::slices() const
370 370 {
371 371 Q_D(const QPieSeries);
372 372 return d->m_slices;
373 373 }
374 374
375 375 /*!
376 376 Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition.
377 377
378 378 The factors are relative to the chart rectangle where:
379 379
380 380 \a relativeHorizontalPosition 0.0 means the absolute left.
381 381 \a relativeHorizontalPosition 1.0 means the absolute right.
382 382 \a relativeVerticalPosition 0.0 means the absolute top.
383 383 \a relativeVerticalPosition 1.0 means the absolute bottom.
384 384
385 385 By default both values are 0.5 which puts the pie in the middle of the chart rectangle.
386 386
387 387 \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize()
388 388 */
389 389 void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition)
390 390 {
391 391 Q_D(QPieSeries);
392 392 if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 ||
393 393 relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0)
394 394 return;
395 395
396 396 if (!qFuzzyIsNull(d->m_pieRelativeHorPos - relativeHorizontalPosition) ||
397 397 !qFuzzyIsNull(d->m_pieRelativeVerPos - relativeVerticalPosition)) {
398 398 d->m_pieRelativeHorPos = relativeHorizontalPosition;
399 399 d->m_pieRelativeVerPos = relativeVerticalPosition;
400 400 emit piePositionChanged();
401 401 }
402 402 }
403 403
404 404 /*!
405 405 Gets the horizontal position of the pie.
406 406
407 407 The returned value is relative to the chart rectangle where:
408 408
409 409 0.0 means the absolute left.
410 410 1.0 means the absolute right.
411 411
412 412 By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle.
413 413
414 414 \sa setPiePosition(), pieVerticalPosition(), setPieSize()
415 415 */
416 416 qreal QPieSeries::pieHorizontalPosition() const
417 417 {
418 418 Q_D(const QPieSeries);
419 419 return d->m_pieRelativeHorPos;
420 420 }
421 421
422 422 /*!
423 423 Gets the vertical position position of the pie.
424 424
425 425 The returned value is relative to the chart rectangle where:
426 426
427 427 0.0 means the absolute top.
428 428 1.0 means the absolute bottom.
429 429
430 430 By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle.
431 431
432 432 \sa setPiePosition(), pieHorizontalPosition(), setPieSize()
433 433 */
434 434 qreal QPieSeries::pieVerticalPosition() const
435 435 {
436 436 Q_D(const QPieSeries);
437 437 return d->m_pieRelativeVerPos;
438 438 }
439 439
440 440 /*!
441 441 Sets the relative size of the pie.
442 442
443 443 The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
444 444
445 445 Default value is 0.7.
446 446
447 447 \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
448 448 */
449 449 void QPieSeries::setPieSize(qreal relativeSize)
450 450 {
451 451 Q_D(QPieSeries);
452 452 if (relativeSize < 0.0 || relativeSize > 1.0)
453 453 return;
454 454
455 455 if (!qFuzzyIsNull(d->m_pieRelativeSize- relativeSize)) {
456 456 d->m_pieRelativeSize = relativeSize;
457 457 emit pieSizeChanged();
458 458 }
459 459 }
460 460
461 461 /*!
462 462 Gets the relative size of the pie.
463 463
464 464 The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle.
465 465
466 466 Default value is 0.7.
467 467
468 468 \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition()
469 469 */
470 470 qreal QPieSeries::pieSize() const
471 471 {
472 472 Q_D(const QPieSeries);
473 473 return d->m_pieRelativeSize;
474 474 }
475 475
476 476
477 477 /*!
478 478 Sets the end angle of the pie.
479 479
480 480 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
481 481
482 482 \a angle must be less than pie end angle. Default value is 0.
483 483
484 484 \sa pieStartAngle(), pieEndAngle(), setPieEndAngle()
485 485 */
486 486 void QPieSeries::setPieStartAngle(qreal angle)
487 487 {
488 488 Q_D(QPieSeries);
489 489
490 490 if (angle < 0 || angle > 360 || angle > d->m_pieEndAngle)
491 491 return;
492 492
493 493 if (!qFuzzyIsNull(angle - d->m_pieStartAngle)) {
494 494 d->m_pieStartAngle = angle;
495 495 d->updateDerivativeData();
496 496 }
497 497 }
498 498
499 499 /*!
500 500 Gets the start angle of the pie.
501 501
502 502 Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360.
503 503
504 504 \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle()
505 505 */
506 506 qreal QPieSeries::pieStartAngle() const
507 507 {
508 508 Q_D(const QPieSeries);
509 509 return d->m_pieStartAngle;
510 510 }
511 511
512 512 /*!
513 513 Sets the end angle of the pie.
514 514
515 515 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
516 516
517 517 \a angle must be greater than start angle.
518 518
519 519 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
520 520 */
521 521 void QPieSeries::setPieEndAngle(qreal angle)
522 522 {
523 523 Q_D(QPieSeries);
524 524
525 525 if (angle < 0 || angle > 360 || angle < d->m_pieStartAngle)
526 526 return;
527 527
528 528 if (!qFuzzyIsNull(angle - d->m_pieEndAngle)) {
529 529 d->m_pieEndAngle = angle;
530 530 d->updateDerivativeData();
531 531 }
532 532 }
533 533
534 534 /*!
535 535 Returns the end angle of the pie.
536 536
537 537 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
538 538
539 539 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
540 540 */
541 541 qreal QPieSeries::pieEndAngle() const
542 542 {
543 543 Q_D(const QPieSeries);
544 544 return d->m_pieEndAngle;
545 545 }
546 546
547 547 /*!
548 548 Sets the all the slice labels \a visible or invisible.
549 549
550 550 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
551 551 */
552 552 void QPieSeries::setLabelsVisible(bool visible)
553 553 {
554 554 Q_D(QPieSeries);
555 555 foreach (QPieSlice* s, d->m_slices)
556 556 s->setLabelVisible(visible);
557 557 }
558 558
559 559 /*!
560 560 Returns the sum of all slice values in this series.
561 561
562 562 \sa QPieSlice::value(), QPieSlice::setValue()
563 563 */
564 564 qreal QPieSeries::total() const
565 565 {
566 566 Q_D(const QPieSeries);
567 567 return d->m_total;
568 568 }
569 569
570 570 /*!
571 571 \fn void QPieSeries::clicked(QPieSlice* slice)
572 572
573 573 This signal is emitted when a \a slice has been clicked.
574 574
575 575 \sa QPieSlice::clicked()
576 576 */
577 577
578 578 /*!
579 579 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
580 580
581 581 This signal is emitted when user has hovered over a \a slice.
582 582
583 583 \sa QPieSlice::hoverEnter()
584 584 */
585 585
586 586 /*!
587 587 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
588 588
589 589 This signal is emitted when user has hovered away from a \a slice.
590 590
591 591 \sa QPieSlice::hoverLeave()
592 592 */
593 593
594 594
595 595
596 596
597 597 bool QPieSeries::setModel(QAbstractItemModel* model)
598 598 {
599 599 Q_D(QPieSeries);
600 600 // disconnect signals from old model
601 601 if(m_model)
602 602 {
603 603 disconnect(m_model, 0, this, 0);
604 604 d->m_mapValues = -1;
605 605 d->m_mapLabels = -1;
606 606 d->m_mapOrientation = Qt::Vertical;
607 607 }
608 608
609 609 // set new model
610 610 if(model)
611 611 {
612 612 m_model = model;
613 613 return true;
614 614 }
615 615 else
616 616 {
617 617 m_model = NULL;
618 618 return false;
619 619 }
620 620 }
621 621
622 622 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
623 623 {
624 624 Q_D(QPieSeries);
625 625
626 626 if (m_model == NULL)
627 627 return;
628 628
629 629 d->m_mapValues = modelValuesLine;
630 630 d->m_mapLabels = modelLabelsLine;
631 631 d->m_mapOrientation = orientation;
632 632
633 633 // connect the signals
634 634 if (d->m_mapOrientation == Qt::Vertical) {
635 635 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
636 636 connect(m_model, SIGNAL(rowsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
637 637 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
638 638 } else {
639 639 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex, QModelIndex)));
640 640 connect(m_model, SIGNAL(columnsInserted(QModelIndex, int, int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
641 641 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
642 642 }
643 643
644 644 // create the initial slices set
645 645 if (d->m_mapOrientation == Qt::Vertical) {
646 646 for (int i = 0; i < m_model->rowCount(); i++)
647 add(m_model->data(m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
647 append(m_model->data(m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
648 648 } else {
649 649 for (int i = 0; i < m_model->columnCount(); i++)
650 add(m_model->data(m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
650 append(m_model->data(m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
651 651 }
652 652 }
653 653
654 654 #include "moc_qpieseries.cpp"
655 655 #include "moc_qpieseriesprivate_p.cpp"
656 656
657 657 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,100 +1,100
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 #ifndef PIESERIES_H
22 22 #define PIESERIES_H
23 23
24 24 #include <qseries.h>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27 class QPieSeriesPrivate;
28 28 class QPieSlice;
29 29
30 30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries
31 31 {
32 32 Q_OBJECT
33 33
34 34 public:
35 35 QPieSeries(QObject *parent = 0);
36 36 virtual ~QPieSeries();
37 37
38 38 public: // from QChartSeries
39 39 QSeriesType type() const;
40 40
41 41 public:
42 42
43 43 // slice setters
44 void add(QPieSlice* slice);
45 void add(QList<QPieSlice*> slices);
44 void append(QPieSlice* slice);
45 void append(QList<QPieSlice*> slices);
46 46 void insert(int i, QPieSlice* slice);
47 47 void replace(QList<QPieSlice*> slices);
48 48 void remove(QPieSlice* slice);
49 49 void clear();
50 50
51 51 // slice getters
52 52 QList<QPieSlice*> slices() const;
53 53
54 54 // calculated data
55 55 int count() const;
56 56 bool isEmpty() const;
57 57 qreal total() const;
58 58
59 59 // pie customization
60 60 void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition);
61 61 qreal pieHorizontalPosition() const;
62 62 qreal pieVerticalPosition() const;
63 63 void setPieSize(qreal relativeSize);
64 64 qreal pieSize() const;
65 65 void setPieStartAngle(qreal startAngle);
66 66 qreal pieStartAngle() const;
67 67 void setPieEndAngle(qreal endAngle);
68 68 qreal pieEndAngle() const;
69 69
70 70 // convenience function
71 71 QPieSeries& operator << (QPieSlice* slice);
72 QPieSlice* add(qreal value, QString name);
72 QPieSlice* append(qreal value, QString name);
73 73 void setLabelsVisible(bool visible = true);
74 74
75 75 // data from model
76 76 bool setModel(QAbstractItemModel* model);
77 77 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
78 78
79 79 Q_SIGNALS:
80 80 void clicked(QPieSlice* slice, Qt::MouseButtons buttons);
81 81 void hoverEnter(QPieSlice* slice);
82 82 void hoverLeave(QPieSlice* slice);
83 83 void added(QList<QPieSlice*> slices);
84 84 void removed(QList<QPieSlice*> slices);
85 85 void piePositionChanged();
86 86 void pieSizeChanged();
87 87
88 88 private:
89 89 QPieSeriesPrivate * const d_ptr;
90 90 Q_DECLARE_PRIVATE(QPieSeries)
91 91 Q_DISABLE_COPY(QPieSeries)
92 92
93 93 public:
94 94 typedef QPieSeriesPrivate * const DataPtr;
95 95 inline DataPtr &data_ptr() { return d_ptr; }
96 96 };
97 97
98 98 QTCOMMERCIALCHART_END_NAMESPACE
99 99
100 100 #endif // PIESERIES_H
@@ -1,433 +1,433
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 "qpieslice.h"
22 22 #include "qpiesliceprivate_p.h"
23 23 #include "qpieseries.h"
24 24 #include "qpieseriesprivate_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 /*!
29 29 \class QPieSlice
30 30 \brief Defines a slice in pie series.
31 31
32 32 Holds all the data of a single slice in a QPieSeries and provides the means
33 33 to modify slice data and customize the visual appearance of the slice.
34 34
35 35 It also provides the means to customize user interaction with the slice by
36 36 providing signals for clicking and hover events.
37 37 */
38 38
39 39 /*!
40 40 \property QPieSlice::label
41 41
42 42 Label of the slice.
43 43 */
44 44
45 45 /*!
46 46 \property QPieSlice::value
47 47
48 48 Value of the slice.
49 49 */
50 50
51 51 /*!
52 52 Constructs an empty slice with a \a parent.
53 53
54 54 Note that QPieSeries takes ownership of the slice when it is set/added.
55 55
56 \sa QPieSeries::replace(), QPieSeries::add()
56 \sa QPieSeries::replace(), QPieSeries::append()
57 57 */
58 58 QPieSlice::QPieSlice(QObject *parent)
59 59 :QObject(parent),
60 60 d_ptr(new QPieSlicePrivate(this))
61 61 {
62 62
63 63 }
64 64
65 65 /*!
66 66 Constructs an empty slice with given \a value, \a label and a \a parent.
67 67 Note that QPieSeries takes ownership of the slice when it is set/added.
68 \sa QPieSeries::replace(), QPieSeries::add()
68 \sa QPieSeries::replace(), QPieSeries::append()
69 69 */
70 70 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
71 71 :QObject(parent),
72 72 d_ptr(new QPieSlicePrivate(this))
73 73 {
74 74 Q_D(QPieSlice);
75 75 d->m_data.m_value = value;
76 76 d->m_data.m_labelText = label;
77 77 }
78 78
79 79 /*!
80 80 Destroys the slice.
81 81 User should not delete the slice if it has been added to the series.
82 82 */
83 83 QPieSlice::~QPieSlice()
84 84 {
85 85 delete d_ptr;
86 86 }
87 87
88 88 /*!
89 89 Gets the value of the slice.
90 90 Note that all values in the series
91 91 \sa setValue()
92 92 */
93 93 qreal QPieSlice::value() const
94 94 {
95 95 Q_D(const QPieSlice);
96 96 return d->m_data.m_value;
97 97 }
98 98
99 99 /*!
100 100 Gets the label of the slice.
101 101 \sa setLabel()
102 102 */
103 103 QString QPieSlice::label() const
104 104 {
105 105 Q_D(const QPieSlice);
106 106 return d->m_data.m_labelText;
107 107 }
108 108
109 109 /*!
110 110 Returns true if label is set as visible.
111 111 \sa setLabelVisible()
112 112 */
113 113 bool QPieSlice::isLabelVisible() const
114 114 {
115 115 Q_D(const QPieSlice);
116 116 return d->m_data.m_isLabelVisible;
117 117 }
118 118
119 119 /*!
120 120 Returns true if slice is exloded from the pie.
121 121 \sa setExploded(), setExplodeDistanceFactor()
122 122 */
123 123 bool QPieSlice::isExploded() const
124 124 {
125 125 Q_D(const QPieSlice);
126 126 return d->m_data.m_isExploded;
127 127 }
128 128
129 129 /*!
130 130 Returns the explode distance factor.
131 131
132 132 The factor is relative to pie radius. For example:
133 133 1.0 means the distance is the same as the radius.
134 134 0.5 means the distance is half of the radius.
135 135
136 136 Default value is 0.15.
137 137
138 138 \sa setExplodeDistanceFactor()
139 139 */
140 140 qreal QPieSlice::explodeDistanceFactor() const
141 141 {
142 142 Q_D(const QPieSlice);
143 143 return d->m_data.m_explodeDistanceFactor;
144 144 }
145 145
146 146 /*!
147 147 Returns the percentage of this slice compared to all slices in the same series.
148 148 The returned value ranges from 0 to 1.0.
149 149
150 150 Updated internally after the slice is added to the series.
151 151 */
152 152 qreal QPieSlice::percentage() const
153 153 {
154 154 Q_D(const QPieSlice);
155 155 return d->m_data.m_percentage;
156 156 }
157 157
158 158 /*!
159 159 Returns the starting angle of this slice in the series it belongs to.
160 160
161 161 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
162 162
163 163 Updated internally after the slice is added to the series.
164 164 */
165 165 qreal QPieSlice::startAngle() const
166 166 {
167 167 Q_D(const QPieSlice);
168 168 return d->m_data.m_startAngle;
169 169 }
170 170
171 171 /*!
172 172 Returns the end angle of this slice in the series it belongs to.
173 173
174 174 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
175 175
176 176 Updated internally after the slice is added to the series.
177 177 */
178 178 qreal QPieSlice::endAngle() const
179 179 {
180 180 Q_D(const QPieSlice);
181 181 return d->m_data.m_startAngle + d->m_data.m_angleSpan;
182 182 }
183 183
184 184 /*!
185 185 Returns the pen used to draw this slice.
186 186 \sa setSlicePen()
187 187 */
188 188 QPen QPieSlice::pen() const
189 189 {
190 190 Q_D(const QPieSlice);
191 191 return d->m_data.m_slicePen;
192 192 }
193 193
194 194 /*!
195 195 Returns the brush used to draw this slice.
196 196 \sa setSliceBrush()
197 197 */
198 198 QBrush QPieSlice::brush() const
199 199 {
200 200 Q_D(const QPieSlice);
201 201 return d->m_data.m_sliceBrush;
202 202 }
203 203
204 204 /*!
205 205 Returns the pen used to draw the label in this slice.
206 206 \sa setLabelArmPen()
207 207 */
208 208 QPen QPieSlice::labelPen() const
209 209 {
210 210 Q_D(const QPieSlice);
211 211 return d->m_data.m_labelPen;
212 212 }
213 213
214 214 /*!
215 215 Returns the font used to draw label in this slice.
216 216 \sa setLabelFont()
217 217 */
218 218 QFont QPieSlice::labelFont() const
219 219 {
220 220 Q_D(const QPieSlice);
221 221 return d->m_data.m_labelFont;
222 222 }
223 223
224 224 /*!
225 225 Gets the label arm length factor.
226 226
227 227 The factor is relative to pie radius. For example:
228 228 1.0 means the length is the same as the radius.
229 229 0.5 means the length is half of the radius.
230 230
231 231 Default value is 0.15
232 232
233 233 \sa setLabelArmLengthFactor()
234 234 */
235 235 qreal QPieSlice::labelArmLengthFactor() const
236 236 {
237 237 Q_D(const QPieSlice);
238 238 return d->m_data.m_labelArmLengthFactor;
239 239 }
240 240
241 241 /*!
242 242 \fn void QPieSlice::clicked()
243 243
244 244 This signal is emitted when user has clicked the slice.
245 245
246 246 \sa QPieSeries::clicked()
247 247 */
248 248
249 249 /*!
250 250 \fn void QPieSlice::hoverEnter()
251 251
252 252 This signal is emitted when user has hovered over the slice.
253 253
254 254 \sa QPieSeries::hoverEnter()
255 255 */
256 256
257 257 /*!
258 258 \fn void QPieSlice::hoverLeave()
259 259
260 260 This signal is emitted when user has hovered away from the slice.
261 261
262 262 \sa QPieSeries::hoverLeave()
263 263 */
264 264
265 265 /*!
266 266 \fn void QPieSlice::changed()
267 267
268 268 This signal emitted when something has changed in the slice.
269 269
270 270 \sa QPieSeries::changed()
271 271 */
272 272
273 273 /*!
274 274 Sets the \a value of this slice.
275 275 \sa value()
276 276 */
277 277 void QPieSlice::setValue(qreal value)
278 278 {
279 279 Q_D(QPieSlice);
280 280 if (!qFuzzyIsNull(d->m_data.m_value - value)) {
281 281 d->m_data.m_value = value;
282 282
283 283 QPieSeries *series = qobject_cast<QPieSeries*>(parent());
284 284 if (series)
285 285 series->data_ptr()->updateDerivativeData(); // will emit changed()
286 286 else
287 287 emit changed();
288 288 }
289 289 }
290 290
291 291 /*!
292 292 Sets the \a label of the slice.
293 293 \sa label()
294 294 */
295 295 void QPieSlice::setLabel(QString label)
296 296 {
297 297 Q_D(QPieSlice);
298 298 if (d->m_data.m_labelText != label) {
299 299 d->m_data.m_labelText = label;
300 300 emit changed();
301 301 }
302 302 }
303 303
304 304 /*!
305 305 Sets the label \a visible in this slice.
306 306 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
307 307 */
308 308 void QPieSlice::setLabelVisible(bool visible)
309 309 {
310 310 Q_D(QPieSlice);
311 311 if (d->m_data.m_isLabelVisible != visible) {
312 312 d->m_data.m_isLabelVisible = visible;
313 313 emit changed();
314 314 }
315 315 }
316 316
317 317 /*!
318 318 Sets this slice \a exploded.
319 319 \sa isExploded(), explodeDistanceFactor()
320 320 */
321 321 void QPieSlice::setExploded(bool exploded)
322 322 {
323 323 Q_D(QPieSlice);
324 324 if (d->m_data.m_isExploded != exploded) {
325 325 d->m_data.m_isExploded = exploded;
326 326 emit changed();
327 327 }
328 328 }
329 329
330 330 /*!
331 331 Sets the explode distance \a factor.
332 332
333 333 The factor is relative to pie radius. For example:
334 334 1.0 means the distance is the same as the radius.
335 335 0.5 means the distance is half of the radius.
336 336
337 337 Default value is 0.15
338 338
339 339 \sa explodeDistanceFactor()
340 340 */
341 341 void QPieSlice::setExplodeDistanceFactor(qreal factor)
342 342 {
343 343 Q_D(QPieSlice);
344 344 if (!qFuzzyIsNull(d->m_data.m_explodeDistanceFactor - factor)) {
345 345 d->m_data.m_explodeDistanceFactor = factor;
346 346 emit changed();
347 347 }
348 348 }
349 349
350 350 /*!
351 351 Sets the \a pen used to draw this slice.
352 352 Note that applying a theme will override this.
353 353 \sa slicePen()
354 354 */
355 355 void QPieSlice::setPen(const QPen &pen)
356 356 {
357 357 Q_D(QPieSlice);
358 358 if (d->m_data.m_slicePen != pen) {
359 359 d->m_data.m_slicePen = pen;
360 360 d->m_data.m_slicePen.setThemed(false);
361 361 emit changed();
362 362 }
363 363 }
364 364
365 365 /*!
366 366 Sets the \a brush used to draw this slice.
367 367 Note that applying a theme will override this.
368 368 \sa sliceBrush()
369 369 */
370 370 void QPieSlice::setBrush(const QBrush &brush)
371 371 {
372 372 Q_D(QPieSlice);
373 373 if (d->m_data.m_sliceBrush != brush) {
374 374 d->m_data.m_sliceBrush = brush;
375 375 d->m_data.m_sliceBrush.setThemed(false);
376 376 emit changed();
377 377 }
378 378 }
379 379
380 380 /*!
381 381 Sets the \a pen used to draw the label in this slice.
382 382 Note that applying a theme will override this.
383 383 \sa labelArmPen()
384 384 */
385 385 void QPieSlice::setLabelPen(const QPen &pen)
386 386 {
387 387 Q_D(QPieSlice);
388 388 if (d->m_data.m_labelPen != pen) {
389 389 d->m_data.m_labelPen = pen;
390 390 d->m_data.m_labelPen.setThemed(false);
391 391 emit changed();
392 392 }
393 393 }
394 394
395 395 /*!
396 396 Sets the \a font used to draw the label in this slice.
397 397 Note that applying a theme will override this.
398 398 \sa labelFont()
399 399 */
400 400 void QPieSlice::setLabelFont(const QFont &font)
401 401 {
402 402 Q_D(QPieSlice);
403 403 if (d->m_data.m_labelFont != font) {
404 404 d->m_data.m_labelFont = font;
405 405 d->m_data.m_labelFont.setThemed(false);
406 406 emit changed();
407 407 }
408 408 }
409 409
410 410 /*!
411 411 Sets the label arm length \a factor.
412 412
413 413 The factor is relative to pie radius. For example:
414 414 1.0 means the length is the same as the radius.
415 415 0.5 means the length is half of the radius.
416 416
417 417 Default value is 0.15
418 418
419 419 \sa labelArmLengthFactor()
420 420 */
421 421 void QPieSlice::setLabelArmLengthFactor(qreal factor)
422 422 {
423 423 Q_D(QPieSlice);
424 424 if (!qFuzzyIsNull(d->m_data.m_labelArmLengthFactor - factor)) {
425 425 d->m_data.m_labelArmLengthFactor = factor;
426 426 emit changed();
427 427 }
428 428 }
429 429
430 430 #include "moc_qpieslice.cpp"
431 431 #include "moc_qpiesliceprivate_p.cpp"
432 432
433 433 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,118
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 "qscatterseries.h"
22 22 #include "qchart.h"
23 23
24 24 /*!
25 25 \class QScatterSeries
26 26 \brief The QScatterSeries class is used for making scatter charts.
27 27
28 28 \mainclass
29 29
30 30 The scatter data is displayed as a collection of points on the chart. Each point determines the position on the horizontal axis
31 31 and the vertical axis.
32 32
33 33 \image scatterchart.png
34 34
35 35 Creating basic scatter chart is simple:
36 36 \code
37 37 QScatterSeries* series = new QScatterSeries();
38 series->add(0, 6);
39 series->add(2, 4);
38 series->append(0, 6);
39 series->append(2, 4);
40 40 ...
41 41 chartView->addSeries(series);
42 42 \endcode
43 43 */
44 44
45 45 /*!
46 46 \enum QScatterSeries::MarkerShape
47 47
48 48 This enum describes the shape used when rendering marker items.
49 49
50 50 \value MarkerShapeCircle
51 51 \value MarkerShapeRectangle
52 52 */
53 53
54 54 /*!
55 55 \fn QChartSeriesType QScatterSeries::type() const
56 56 \brief Returns QChartSeries::SeriesTypeScatter.
57 57 \sa QSeries, QSeriesType
58 58 */
59 59
60 60 QTCOMMERCIALCHART_BEGIN_NAMESPACE
61 61
62 62 /*!
63 63 Constructs a series object which is a child of \a parent.
64 64 */
65 65 QScatterSeries::QScatterSeries(QObject *parent) :
66 66 QXYSeries(parent),
67 67 m_shape(QScatterSeries::MarkerShapeCircle),
68 68 m_size(15.0)
69 69 {
70 70 }
71 71
72 72 /*!
73 73 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
74 74 */
75 75 QScatterSeries::~QScatterSeries()
76 76 {
77 77 }
78 78
79 79 /*!
80 80 Returns the shape used for drawing markers.
81 81 */
82 82 QScatterSeries::MarkerShape QScatterSeries::shape() const
83 83 {
84 84 return m_shape;
85 85 }
86 86
87 87 /*!
88 88 Overrides the default shape of the marker items with a user defined \a shape. The default shape
89 89 is defined by chart theme setting.
90 90 */
91 91 void QScatterSeries::setShape(MarkerShape shape)
92 92 {
93 93 if (m_shape != shape) {
94 94 m_shape = shape;
95 95 emit QXYSeries::updated();
96 96 }
97 97 }
98 98
99 99 /*!
100 100 Returns the size of the marker items.
101 101 */
102 102 qreal QScatterSeries::size() const
103 103 {
104 104 return m_size;
105 105 }
106 106
107 107 /*!
108 108 Set the \a size of the marker items. The default size is 9.0.
109 109 */
110 110 void QScatterSeries::setSize(qreal size)
111 111 {
112 112 if (!qFuzzyIsNull(m_size - size)) {
113 113 m_size = size;
114 114 emit updated();
115 115 }
116 116 }
117 117
118 118 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,510 +1,510
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 "qxyseries.h"
22 22
23 23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 24
25 25 /*!
26 26 \class QXYSeries
27 27 \brief The QXYSeries class is a base class for line, spline and scatter series.
28 28 */
29 29
30 30 /*!
31 31 \fn QPen QXYSeries::pen() const
32 32 \brief Returns pen used to draw points for series.
33 33 \sa setPen()
34 34 */
35 35
36 36 /*!
37 37 \fn QBrush QXYSeries::brush() const
38 38 \brief Returns brush used to draw points for series.
39 39 \sa setBrush()
40 40 */
41 41
42 42 /*!
43 43 \fn void QXYSeries::clicked(const QPointF& point)
44 44 \brief Signal is emitted when user clicks the \a point on chart.
45 45 */
46 46
47 47 /*!
48 48 \fn void QXYSeries::pointReplaced(int index)
49 49 \brief \internal \a index
50 50 */
51 51
52 52 /*!
53 53 \fn void QXYSeries::pointAdded(int index)
54 54 \brief \internal \a index
55 55 */
56 56
57 57 /*!
58 58 \fn void QXYSeries::pointRemoved(int index)
59 59 \brief \internal \a index
60 60 */
61 61
62 62 /*!
63 63 \fn void QXYSeries::updated()
64 64 \brief \internal
65 65 */
66 66
67 67 /*!
68 68 Constructs empty series object which is a child of \a parent.
69 69 When series object is added to QChartView or QChart instance ownerships is transfered.
70 70 */
71 71 QXYSeries::QXYSeries(QObject *parent):QSeries(parent)
72 72 {
73 73 m_mapX = -1;
74 74 m_mapY = -1;
75 75 m_mapFirst = 0;
76 76 m_mapCount = 0;
77 77 m_mapLimited = false;
78 78 m_mapOrientation = Qt::Vertical;
79 79 // m_mapYOrientation = Qt::Vertical;
80 80 }
81 81 /*!
82 82 Destroys the object. Series added to QChartView or QChart instances are owned by those,
83 83 and are deleted when mentioned object are destroyed.
84 84 */
85 85 QXYSeries::~QXYSeries()
86 86 {
87 87 }
88 88
89 89 /*!
90 90 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
91 91 */
92 void QXYSeries::add(qreal x,qreal y)
92 void QXYSeries::append(qreal x,qreal y)
93 93 {
94 94 Q_ASSERT(m_x.size() == m_y.size());
95 95 m_x<<x;
96 96 m_y<<y;
97 97 emit pointAdded(m_x.size()-1);
98 98 }
99 99
100 100 /*!
101 101 This is an overloaded function.
102 102 Adds data \a point to the series. Points are connected with lines on the chart.
103 103 */
104 void QXYSeries::add(const QPointF &point)
104 void QXYSeries::append(const QPointF &point)
105 105 {
106 add(point.x(),point.y());
106 append(point.x(),point.y());
107 107 }
108 108
109 109 /*!
110 110 This is an overloaded function.
111 111 Adds list of data \a points to the series. Points are connected with lines on the chart.
112 112 */
113 void QXYSeries::add(const QList<QPointF> points)
113 void QXYSeries::append(const QList<QPointF> points)
114 114 {
115 115 foreach(const QPointF& point , points) {
116 add(point.x(),point.y());
116 append(point.x(),point.y());
117 117 }
118 118 }
119 119
120 120 /*!
121 121 Modifies \a y value for given \a x a value.
122 122 */
123 123 void QXYSeries::replace(qreal x,qreal y)
124 124 {
125 125 int index = m_x.indexOf(x);
126 126 m_x[index] = x;
127 127 m_y[index] = y;
128 128 emit pointReplaced(index);
129 129 }
130 130
131 131 /*!
132 132 This is an overloaded function.
133 133 Replaces current y value of for given \a point x value with \a point y value.
134 134 */
135 135 void QXYSeries::replace(const QPointF &point)
136 136 {
137 137 replace(point.x(),point.y());
138 138 }
139 139
140 140 /*!
141 141 Removes first \a x value and related y value.
142 142 */
143 143 void QXYSeries::remove(qreal x)
144 144 {
145 145 int index = m_x.indexOf(x);
146 146
147 147 if (index == -1) return;
148 148
149 149 m_x.remove(index);
150 150 m_y.remove(index);
151 151
152 152 emit pointRemoved(index);
153 153 }
154 154
155 155 /*!
156 156 Removes current \a x and \a y value.
157 157 */
158 158 void QXYSeries::remove(qreal x,qreal y)
159 159 {
160 160 int index =-1;
161 161 do {
162 162 index = m_x.indexOf(x,index+1);
163 163 } while (index !=-1 && m_y.at(index)!=y);
164 164
165 165 if (index==-1) return;
166 166
167 167 m_x.remove(index);
168 168 m_y.remove(index);
169 169 emit pointRemoved(index);
170 170 }
171 171
172 172 /*!
173 173 Removes current \a point x value. Note \a point y value is ignored.
174 174 */
175 175 void QXYSeries::remove(const QPointF &point)
176 176 {
177 177 remove(point.x(),point.y());
178 178 }
179 179
180 180 /*!
181 181 Removes all data points from the series.
182 182 */
183 183 void QXYSeries::removeAll()
184 184 {
185 185 m_x.clear();
186 186 m_y.clear();
187 187 }
188 188
189 189 /*!
190 190 \internal \a pos
191 191 */
192 192 qreal QXYSeries::x(int pos) const
193 193 {
194 194 if (m_model) {
195 195 if (m_mapOrientation == Qt::Vertical)
196 196 // consecutive data is read from model's column
197 197 return m_model->data(m_model->index(pos + m_mapFirst, m_mapX), Qt::DisplayRole).toDouble();
198 198 else
199 199 // consecutive data is read from model's row
200 200 return m_model->data(m_model->index(m_mapX, pos + m_mapFirst), Qt::DisplayRole).toDouble();
201 201 } else {
202 202 // model is not specified, return the data from series' internal data store
203 203 return m_x.at(pos);
204 204 }
205 205 }
206 206
207 207 /*!
208 208 \internal \a pos
209 209 */
210 210 qreal QXYSeries::y(int pos) const
211 211 {
212 212 if (m_model) {
213 213 if (m_mapOrientation == Qt::Vertical)
214 214 // consecutive data is read from model's column
215 215 return m_model->data(m_model->index(pos + m_mapFirst, m_mapY), Qt::DisplayRole).toDouble();
216 216 else
217 217 // consecutive data is read from model's row
218 218 return m_model->data(m_model->index(m_mapY, pos + m_mapFirst), Qt::DisplayRole).toDouble();
219 219 } else {
220 220 // model is not specified, return the data from series' internal data store
221 221 return m_y.at(pos);
222 222 }
223 223 }
224 224
225 225 /*!
226 226 Returns number of data points within series.
227 227 */
228 228 int QXYSeries::count() const
229 229 {
230 230 Q_ASSERT(m_x.size() == m_y.size());
231 231
232 232 if (m_model) {
233 233 if (m_mapOrientation == Qt::Vertical) {
234 234 // data is in a column. Return the number of mapped items if the model's column have enough items
235 235 // or the number of items that can be mapped
236 236 if (m_mapLimited)
237 237 return qMin(m_mapCount, qMax(m_model->rowCount() - m_mapFirst, 0));
238 238 else
239 239 return qMax(m_model->rowCount() - m_mapFirst, 0);
240 240 } else {
241 241 // data is in a row. Return the number of mapped items if the model's row have enough items
242 242 // or the number of items that can be mapped
243 243 if (m_mapLimited)
244 244 return qMin(m_mapCount, qMax(m_model->columnCount() - m_mapFirst, 0));
245 245 else
246 246 return qMax(m_model->columnCount() - m_mapFirst, 0);
247 247 }
248 248 }
249 249
250 250 // model is not specified, return the number of points in the series internal data store
251 251 return m_x.size();
252 252 }
253 253
254 254 /*!
255 255 Returns the data points of the series.
256 256 */
257 257 QList<QPointF> QXYSeries::data()
258 258 {
259 259 QList<QPointF> data;
260 260 for (int i(0); i < m_x.count() && i < m_y.count(); i++)
261 261 data.append(QPointF(m_x.at(i), m_y.at(i)));
262 262 return data;
263 263 }
264 264
265 265
266 266 /*!
267 267 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
268 268 pen from chart theme is used.
269 269 \sa QChart::setChartTheme()
270 270 */
271 271 void QXYSeries::setPen(const QPen &pen)
272 272 {
273 273 if (pen != m_pen) {
274 274 m_pen = pen;
275 275 emit updated();
276 276 }
277 277 }
278 278
279 279 /*!
280 280 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
281 281 from chart theme setting is used.
282 282 \sa QChart::setChartTheme()
283 283 */
284 284
285 285 void QXYSeries::setBrush(const QBrush &brush)
286 286 {
287 287 if (brush != m_brush) {
288 288 m_brush = brush;
289 289 emit updated();
290 290 }
291 291 }
292 292
293 293
294 294 /*!
295 295 Stream operator for adding a data \a point to the series.
296 \sa add()
296 \sa append()
297 297 */
298 298
299 299 QXYSeries& QXYSeries::operator<< (const QPointF &point)
300 300 {
301 add(point);
301 append(point);
302 302 return *this;
303 303 }
304 304
305 305
306 306 /*!
307 307 Stream operator for adding a list of \a points to the series.
308 \sa add()
308 \sa append()
309 309 */
310 310
311 311 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points)
312 312 {
313 add(points);
313 append(points);
314 314 return *this;
315 315 }
316 316
317 317
318 318 void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
319 319 {
320 320 Q_UNUSED(bottomRight)
321 321
322 322 if (m_mapOrientation == Qt::Vertical) {
323 323 if (topLeft.row() >= m_mapFirst && (!m_mapLimited || topLeft.row() < m_mapFirst + m_mapCount))
324 324 emit pointReplaced(topLeft.row() - m_mapFirst);
325 325 } else {
326 326 if (topLeft.column() >= m_mapFirst && (!m_mapLimited || topLeft.column() < m_mapFirst + m_mapCount))
327 327 emit pointReplaced(topLeft.column() - m_mapFirst);
328 328 }
329 329 }
330 330
331 331 void QXYSeries::modelDataAboutToBeAdded(QModelIndex parent, int start, int end)
332 332 {
333 333 Q_UNUSED(parent)
334 334 // Q_UNUSED(end)
335 335
336 336 if (m_mapLimited) {
337 337 if (start >= m_mapFirst + m_mapCount) {
338 338 // the added data is below mapped area
339 339 // therefore it has no relevance
340 340 return;
341 341 } else {
342 342 // the added data is in the mapped area or before it and update is needed
343 343
344 344 // check how many mapped items there is currently (before new items are added)
345 345 // if the number of items currently is equal the m_mapCount then some needs to be removed from xychartitem
346 346 // internal storage before new ones can be added
347 347
348 348 int itemsToRemove = qMin(count() - (start - m_mapFirst), end - start + 1);
349 349 if (m_mapCount == count()) {
350 350 for (int i = 0; i < itemsToRemove; i++)
351 351 emit pointRemoved(count() - 1 - i);
352 352 }
353 353 }
354 354 } else {
355 355 // map is not limited (it includes all the items starting from m_mapFirst till the end of model)
356 356 // nothing to do
357 357 // emit pointAdded(qMax(start - m_mapFirst, 0));
358 358 }
359 359 }
360 360
361 361 void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end)
362 362 {
363 363 Q_UNUSED(parent)
364 364 // Q_UNUSED(end)
365 365
366 366 if (m_mapLimited) {
367 367 if (start >= m_mapFirst + m_mapCount) {
368 368 // the added data is below mapped area
369 369 // therefore it has no relevance
370 370 return;
371 371 } else {
372 372 // the added data is in the mapped area or before it
373 373 // update needed
374 374 if (count() > 0) {
375 375 for (int i = 0; i < qMin(m_mapCount - (start - m_mapFirst), end - start + 1); i++)
376 376 emit pointAdded(qMax(start + i - m_mapFirst, 0));
377 377 }
378 378 }
379 379 } else {
380 380 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
381 381 for (int i = 0; i < end - start + 1; i++)
382 382 emit pointAdded(qMax(start + i - m_mapFirst, 0));
383 383 }
384 384 }
385 385
386 386 void QXYSeries::modelDataAboutToBeRemoved(QModelIndex parent, int start, int end)
387 387 {
388 388 Q_UNUSED(parent)
389 389 // Q_UNUSED(end)
390 390
391 391 if (m_mapLimited) {
392 392 if (start >= m_mapFirst + m_mapCount) {
393 393 // the removed data is below mapped area
394 394 // therefore it has no relevance
395 395 return;
396 396 } else {
397 397 // the removed data is in the mapped area or before it
398 398 // update needed
399 399
400 400 // check how many items need to be removed from the xychartitem storage
401 401 // the number equals the number of items that are removed and that lay before
402 402 // or in the mapped area. Items that lay beyond the map do not count
403 403 // the max is the current number of items in storage (count())
404 404 int itemsToRemove = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - start + 1);
405 405 for (int i = 0; i < itemsToRemove; i++)
406 406 emit pointRemoved(qMax(start - m_mapFirst, 0));
407 407 }
408 408 } else {
409 409 // map is not limited (it included all the items starting from m_mapFirst till the end of model)
410 410 for (int i = 0; i < end - start + 1; i++)
411 411 emit pointRemoved(qMax(start - m_mapFirst, 0));
412 412 }
413 413 }
414 414
415 415 void QXYSeries::modelDataRemoved(QModelIndex parent, int start, int end)
416 416 {
417 417 Q_UNUSED(parent)
418 418 Q_UNUSED(end)
419 419
420 420 // how many items there were before data was removed
421 421 // int oldCount = count() - 1;
422 422
423 423 if (m_mapLimited) {
424 424 if (start >= m_mapFirst + m_mapCount) {
425 425 // the removed data is below mapped area
426 426 // therefore it has no relevance
427 427 return;
428 428 } else {
429 429 // if the current items count in the whole model is bigger than the index of the last item
430 430 // that was removed than it means there are some extra items available
431 431 int extraItemsAvailable = 0;
432 432 if (m_mapOrientation == Qt::Vertical) {
433 433 extraItemsAvailable = qMax(m_model->rowCount() - end, 0);
434 434 } else {
435 435 extraItemsAvailable = qMax(m_model->columnCount() - end, 0);
436 436 }
437 437
438 438 // if there are excess items available (below the mapped area) use them to repopulate mapped area
439 439 int removedItemsCount = qMin(count(), qMin(end, m_mapFirst + m_mapCount - 1) - qMax(start, m_mapFirst) + 1);
440 440 int toBeAdded = qMin(extraItemsAvailable, removedItemsCount);
441 441 for (int k = 0; k < toBeAdded; k++)
442 442 emit pointAdded(qMax(start - m_mapFirst, m_mapFirst) + k);
443 443 }
444 444 } else {
445 445 // data was removed from XYSeries interal storage. Nothing more to do
446 446 }
447 447 }
448 448
449 449 bool QXYSeries::setModel(QAbstractItemModel *model) {
450 450
451 451 // disconnect signals from old model
452 452 if (m_model) {
453 453 disconnect(m_model, 0, this, 0);
454 454 m_mapX = -1;
455 455 m_mapY = -1;
456 456 m_mapFirst = 0;
457 457 m_mapCount = 0;
458 458 m_mapLimited = false;
459 459 m_mapOrientation = Qt::Vertical;
460 460 }
461 461
462 462 // set new model
463 463 if (model) {
464 464 m_model = model;
465 465 return true;
466 466 } else {
467 467 m_model = 0;
468 468 return false;
469 469 }
470 470 }
471 471
472 472 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
473 473 {
474 474 if (m_model == 0)
475 475 return;
476 476 m_mapX = modelX;
477 477 m_mapY = modelY;
478 478 m_mapFirst = 0;
479 479 m_mapOrientation = orientation;
480 480 if (m_mapOrientation == Qt::Vertical) {
481 481 // m_mapCount = m_model->rowCount();
482 482 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
483 483 connect(m_model,SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
484 484 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
485 485 connect(m_model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
486 486 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
487 487 } else {
488 488 // m_mapCount = m_model->columnCount();
489 489 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
490 490 connect(m_model,SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeAdded(QModelIndex,int,int)));
491 491 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
492 492 connect(m_model, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(modelDataAboutToBeRemoved(QModelIndex,int,int)));
493 493 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
494 494 }
495 495 }
496 496
497 497 void QXYSeries::setModelMappingShift(int first, int count)
498 498 {
499 499 m_mapFirst = first;
500 500 if (count == 0) {
501 501 m_mapLimited = false;
502 502 } else {
503 503 m_mapCount = count;
504 504 m_mapLimited = true;
505 505 }
506 506 }
507 507
508 508 #include "moc_qxyseries.cpp"
509 509
510 510 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,100 +1,100
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 #ifndef QXYSERIES_H_
22 22 #define QXYSERIES_H_
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qseries.h>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QSeries
32 32 {
33 33 Q_OBJECT
34 34 protected:
35 35 QXYSeries(QObject *parent = 0);
36 36 virtual ~QXYSeries();
37 37
38 38 public:
39 void add(qreal x, qreal y);
40 void add(const QPointF &point);
41 void add(const QList<QPointF> points);
39 void append(qreal x, qreal y);
40 void append(const QPointF &point);
41 void append(const QList<QPointF> points);
42 42 void replace(qreal x,qreal y);
43 43 void replace(const QPointF &point);
44 44 void remove(qreal x);
45 45 void remove(qreal x, qreal y);
46 46 void remove(const QPointF &point);
47 47 void removeAll();
48 48
49 49 int count() const;
50 50 qreal x(int pos) const;
51 51 qreal y(int pos) const;
52 52 QList<QPointF> data();
53 53
54 54 QXYSeries& operator << (const QPointF &point);
55 55 QXYSeries& operator << (const QList<QPointF> points);
56 56
57 57 void setPen(const QPen &pen);
58 58 QPen pen() const {return m_pen;}
59 59 void setBrush(const QBrush &brush);
60 60 QBrush brush() const {return m_brush;}
61 61
62 62 bool setModel(QAbstractItemModel *model);
63 63 QAbstractItemModel* model() const { return m_model; }
64 64
65 65 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
66 66 virtual void setModelMappingShift(int first, int count = 0);
67 67
68 68 private Q_SLOTS:
69 69 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
70 70 void modelDataAboutToBeAdded(QModelIndex parent, int start, int end);
71 71 void modelDataAdded(QModelIndex parent, int start, int end);
72 72 void modelDataAboutToBeRemoved(QModelIndex parent, int start, int end);
73 73 void modelDataRemoved(QModelIndex parent, int start, int end);
74 74
75 75 Q_SIGNALS:
76 76 void clicked(const QPointF &point);
77 77 void updated();
78 78 void pointReplaced(int index);
79 79 void pointRemoved(int index);
80 80 void pointAdded(int index);
81 81
82 82 protected:
83 83 QVector<qreal> m_x;
84 84 QVector<qreal> m_y;
85 85
86 86 QPen m_pen;
87 87 QBrush m_brush;
88 88
89 89 int m_mapX;
90 90 int m_mapY;
91 91 int m_mapFirst;
92 92 int m_mapCount;
93 93 bool m_mapLimited;
94 94 Qt::Orientation m_mapOrientation;
95 95 int tempItemsRemoved;
96 96 };
97 97
98 98 QTCOMMERCIALCHART_END_NAMESPACE
99 99
100 100 #endif
General Comments 0
You need to be logged in to leave comments. Login now