##// END OF EJS Templates
Default numbering for empty categories in barseries
Michal Klocek -
r1595:f1564e476801
parent child
Show More
@@ -1,385 +1,377
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "themewidget.h"
21 #include "themewidget.h"
22
22
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPieSeries>
24 #include <QPieSeries>
25 #include <QPieSlice>
25 #include <QPieSlice>
26 #include <QAbstractBarSeries>
26 #include <QAbstractBarSeries>
27 #include <QPercentBarSeries>
27 #include <QPercentBarSeries>
28 #include <QStackedBarSeries>
28 #include <QStackedBarSeries>
29 #include <QBarSet>
29 #include <QBarSet>
30 #include <QLineSeries>
30 #include <QLineSeries>
31 #include <QSplineSeries>
31 #include <QSplineSeries>
32 #include <QScatterSeries>
32 #include <QScatterSeries>
33 #include <QAreaSeries>
33 #include <QAreaSeries>
34 #include <QLegend>
34 #include <QLegend>
35 #include <QGridLayout>
35 #include <QGridLayout>
36 #include <QFormLayout>
36 #include <QFormLayout>
37 #include <QComboBox>
37 #include <QComboBox>
38 #include <QSpinBox>
38 #include <QSpinBox>
39 #include <QCheckBox>
39 #include <QCheckBox>
40 #include <QGroupBox>
40 #include <QGroupBox>
41 #include <QLabel>
41 #include <QLabel>
42 #include <QTime>
42 #include <QTime>
43 #include <QCategoriesAxis>
43 #include <QCategoriesAxis>
44
44
45 ThemeWidget::ThemeWidget(QWidget* parent) :
45 ThemeWidget::ThemeWidget(QWidget* parent) :
46 QWidget(parent),
46 QWidget(parent),
47 m_listCount(3),
47 m_listCount(3),
48 m_valueMax(10),
48 m_valueMax(10),
49 m_valueCount(7),
49 m_valueCount(7),
50 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
50 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
51 m_themeComboBox(createThemeBox()),
51 m_themeComboBox(createThemeBox()),
52 m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
52 m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
53 m_animatedComboBox(createAnimationBox()),
53 m_animatedComboBox(createAnimationBox()),
54 m_legendComboBox(createLegendBox())
54 m_legendComboBox(createLegendBox())
55 {
55 {
56 connectSignals();
56 connectSignals();
57 // create layout
57 // create layout
58 QGridLayout* baseLayout = new QGridLayout();
58 QGridLayout* baseLayout = new QGridLayout();
59 QHBoxLayout *settingsLayout = new QHBoxLayout();
59 QHBoxLayout *settingsLayout = new QHBoxLayout();
60 settingsLayout->addWidget(new QLabel("Theme:"));
60 settingsLayout->addWidget(new QLabel("Theme:"));
61 settingsLayout->addWidget(m_themeComboBox);
61 settingsLayout->addWidget(m_themeComboBox);
62 settingsLayout->addWidget(new QLabel("Animation:"));
62 settingsLayout->addWidget(new QLabel("Animation:"));
63 settingsLayout->addWidget(m_animatedComboBox);
63 settingsLayout->addWidget(m_animatedComboBox);
64 settingsLayout->addWidget(new QLabel("Legend:"));
64 settingsLayout->addWidget(new QLabel("Legend:"));
65 settingsLayout->addWidget(m_legendComboBox);
65 settingsLayout->addWidget(m_legendComboBox);
66 settingsLayout->addWidget(m_antialiasCheckBox);
66 settingsLayout->addWidget(m_antialiasCheckBox);
67 settingsLayout->addStretch();
67 settingsLayout->addStretch();
68 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
68 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
69
69
70 //create charts
70 //create charts
71
71
72 QChartView *chartView;
72 QChartView *chartView;
73
73
74 chartView = new QChartView(createAreaChart());
74 chartView = new QChartView(createAreaChart());
75 baseLayout->addWidget(chartView, 1, 0);
75 baseLayout->addWidget(chartView, 1, 0);
76 m_charts << chartView;
76 m_charts << chartView;
77
77
78 chartView = new QChartView(createBarChart(m_valueCount));
78 chartView = new QChartView(createBarChart(m_valueCount));
79 baseLayout->addWidget(chartView, 1, 1);
79 baseLayout->addWidget(chartView, 1, 1);
80 m_charts << chartView;
80 m_charts << chartView;
81
81
82 chartView = new QChartView(createLineChart());
82 chartView = new QChartView(createLineChart());
83 baseLayout->addWidget(chartView, 1, 2);
83 baseLayout->addWidget(chartView, 1, 2);
84 m_charts << chartView;
84 m_charts << chartView;
85
85
86 chartView = new QChartView(createPieChart());
86 chartView = new QChartView(createPieChart());
87 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
87 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
88 baseLayout->addWidget(chartView, 2, 0);
88 baseLayout->addWidget(chartView, 2, 0);
89 m_charts << chartView;
89 m_charts << chartView;
90
90
91 chartView = new QChartView(createSplineChart());
91 chartView = new QChartView(createSplineChart());
92 baseLayout->addWidget(chartView, 2, 1);
92 baseLayout->addWidget(chartView, 2, 1);
93 m_charts << chartView;
93 m_charts << chartView;
94
94
95 chartView = new QChartView(createScatterChart());
95 chartView = new QChartView(createScatterChart());
96 baseLayout->addWidget(chartView, 2, 2);
96 baseLayout->addWidget(chartView, 2, 2);
97 m_charts << chartView;
97 m_charts << chartView;
98
98
99 setLayout(baseLayout);
99 setLayout(baseLayout);
100
100
101 // Set defaults
101 // Set defaults
102 m_antialiasCheckBox->setChecked(true);
102 m_antialiasCheckBox->setChecked(true);
103 updateUI();
103 updateUI();
104 }
104 }
105
105
106 ThemeWidget::~ThemeWidget()
106 ThemeWidget::~ThemeWidget()
107 {
107 {
108 }
108 }
109
109
110 void ThemeWidget::connectSignals()
110 void ThemeWidget::connectSignals()
111 {
111 {
112 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
112 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
113 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
113 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
114 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
114 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
115 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
115 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
116 }
116 }
117
117
118 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
118 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
119 {
119 {
120 DataTable dataTable;
120 DataTable dataTable;
121
121
122 // set seed for random stuff
122 // set seed for random stuff
123 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
123 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
124
124
125 // generate random data
125 // generate random data
126 for (int i(0); i < listCount; i++) {
126 for (int i(0); i < listCount; i++) {
127 DataList dataList;
127 DataList dataList;
128 qreal yValue(0);
128 qreal yValue(0);
129 for (int j(0); j < valueCount; j++) {
129 for (int j(0); j < valueCount; j++) {
130 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
130 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
131 QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
131 QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
132 yValue);
132 yValue);
133 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
133 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
134 dataList << Data(value, label);
134 dataList << Data(value, label);
135 }
135 }
136 dataTable << dataList;
136 dataTable << dataList;
137 }
137 }
138
138
139 return dataTable;
139 return dataTable;
140 }
140 }
141
141
142 QComboBox* ThemeWidget::createThemeBox() const
142 QComboBox* ThemeWidget::createThemeBox() const
143 {
143 {
144 // settings layout
144 // settings layout
145 QComboBox* themeComboBox = new QComboBox();
145 QComboBox* themeComboBox = new QComboBox();
146 themeComboBox->addItem("Light", QChart::ChartThemeLight);
146 themeComboBox->addItem("Light", QChart::ChartThemeLight);
147 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
147 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
148 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
148 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
149 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
149 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
150 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
150 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
151 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
151 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
152 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
152 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
153 return themeComboBox;
153 return themeComboBox;
154 }
154 }
155
155
156 QComboBox* ThemeWidget::createAnimationBox() const
156 QComboBox* ThemeWidget::createAnimationBox() const
157 {
157 {
158 // settings layout
158 // settings layout
159 QComboBox* animationComboBox = new QComboBox();
159 QComboBox* animationComboBox = new QComboBox();
160 animationComboBox->addItem("No Animations", QChart::NoAnimation);
160 animationComboBox->addItem("No Animations", QChart::NoAnimation);
161 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
161 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
162 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
162 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
163 animationComboBox->addItem("All Animations", QChart::AllAnimations);
163 animationComboBox->addItem("All Animations", QChart::AllAnimations);
164 return animationComboBox;
164 return animationComboBox;
165 }
165 }
166
166
167 QComboBox* ThemeWidget::createLegendBox() const
167 QComboBox* ThemeWidget::createLegendBox() const
168 {
168 {
169 QComboBox* legendComboBox = new QComboBox();
169 QComboBox* legendComboBox = new QComboBox();
170 legendComboBox->addItem("No Legend ", 0);
170 legendComboBox->addItem("No Legend ", 0);
171 legendComboBox->addItem("Legend Top", Qt::AlignTop);
171 legendComboBox->addItem("Legend Top", Qt::AlignTop);
172 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
172 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
173 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
173 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
174 legendComboBox->addItem("Legend Right", Qt::AlignRight);
174 legendComboBox->addItem("Legend Right", Qt::AlignRight);
175 return legendComboBox;
175 return legendComboBox;
176 }
176 }
177
177
178 QChart* ThemeWidget::createAreaChart() const
178 QChart* ThemeWidget::createAreaChart() const
179 {
179 {
180 QChart *chart = new QChart();
180 QChart *chart = new QChart();
181 // chart->axisX()->setNiceNumbersEnabled(true);
181 // chart->axisX()->setNiceNumbersEnabled(true);
182 // chart->axisY()->setNiceNumbersEnabled(true);
182 // chart->axisY()->setNiceNumbersEnabled(true);
183 chart->setTitle("Area chart");
183 chart->setTitle("Area chart");
184
184
185 // The lower series initialized to zero values
185 // The lower series initialized to zero values
186 QLineSeries *lowerSeries = 0;
186 QLineSeries *lowerSeries = 0;
187 QString name("Series ");
187 QString name("Series ");
188 int nameIndex = 0;
188 int nameIndex = 0;
189 for (int i(0); i < m_dataTable.count(); i++) {
189 for (int i(0); i < m_dataTable.count(); i++) {
190 QLineSeries *upperSeries = new QLineSeries(chart);
190 QLineSeries *upperSeries = new QLineSeries(chart);
191 for (int j(0); j < m_dataTable[i].count(); j++) {
191 for (int j(0); j < m_dataTable[i].count(); j++) {
192 Data data = m_dataTable[i].at(j);
192 Data data = m_dataTable[i].at(j);
193 if (lowerSeries){
193 if (lowerSeries){
194 const QList<QPointF>& points = lowerSeries->points();
194 const QList<QPointF>& points = lowerSeries->points();
195 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
195 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
196 }else
196 }else
197 upperSeries->append(QPointF(j, data.first.y()));
197 upperSeries->append(QPointF(j, data.first.y()));
198 }
198 }
199 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
199 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
200 area->setName(name + QString::number(nameIndex));
200 area->setName(name + QString::number(nameIndex));
201 nameIndex++;
201 nameIndex++;
202 chart->addSeries(area);
202 chart->addSeries(area);
203 chart->createDefaultAxes();
203 chart->createDefaultAxes();
204 lowerSeries = upperSeries;
204 lowerSeries = upperSeries;
205 }
205 }
206
206
207 return chart;
207 return chart;
208 }
208 }
209
209
210 QChart* ThemeWidget::createBarChart(int valueCount) const
210 QChart* ThemeWidget::createBarChart(int valueCount) const
211 {
211 {
212 QChart* chart = new QChart();
212 QChart* chart = new QChart();
213 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
213 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
214 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
214 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
215 chart->setTitle("Bar chart");
215 chart->setTitle("Bar chart");
216
216
217 QStringList categories;
218 for (int i(0); i < valueCount; i++)
219 categories << QString::number(i);
220
221 QCategoriesAxis* axis = new QCategoriesAxis();
222 axis->append(categories);
223
224 QStackedBarSeries* series = new QStackedBarSeries(chart);
217 QStackedBarSeries* series = new QStackedBarSeries(chart);
225 for (int i(0); i < m_dataTable.count(); i++) {
218 for (int i(0); i < m_dataTable.count(); i++) {
226 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
219 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
227 foreach (Data data, m_dataTable[i])
220 foreach (Data data, m_dataTable[i])
228 *set << data.first.y();
221 *set << data.first.y();
229 series->append(set);
222 series->append(set);
230 }
223 }
231 chart->addSeries(series);
224 chart->addSeries(series);
232 chart->createDefaultAxes();
225 chart->createDefaultAxes();
233 chart->setAxisX(axis, series);
234
226
235 return chart;
227 return chart;
236 }
228 }
237
229
238 QChart* ThemeWidget::createLineChart() const
230 QChart* ThemeWidget::createLineChart() const
239 {
231 {
240 QChart* chart = new QChart();
232 QChart* chart = new QChart();
241 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
233 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
242 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
234 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
243 chart->setTitle("Line chart");
235 chart->setTitle("Line chart");
244
236
245 QString name("Series ");
237 QString name("Series ");
246 int nameIndex = 0;
238 int nameIndex = 0;
247 foreach (DataList list, m_dataTable) {
239 foreach (DataList list, m_dataTable) {
248 QLineSeries *series = new QLineSeries(chart);
240 QLineSeries *series = new QLineSeries(chart);
249 foreach (Data data, list)
241 foreach (Data data, list)
250 series->append(data.first);
242 series->append(data.first);
251 series->setName(name + QString::number(nameIndex));
243 series->setName(name + QString::number(nameIndex));
252 nameIndex++;
244 nameIndex++;
253 chart->addSeries(series);
245 chart->addSeries(series);
254 }
246 }
255 chart->createDefaultAxes();
247 chart->createDefaultAxes();
256
248
257 return chart;
249 return chart;
258 }
250 }
259
251
260 QChart* ThemeWidget::createPieChart() const
252 QChart* ThemeWidget::createPieChart() const
261 {
253 {
262 QChart* chart = new QChart();
254 QChart* chart = new QChart();
263 chart->setTitle("Pie chart");
255 chart->setTitle("Pie chart");
264
256
265 qreal pieSize = 1.0 / m_dataTable.count();
257 qreal pieSize = 1.0 / m_dataTable.count();
266 for (int i = 0; i < m_dataTable.count(); i++) {
258 for (int i = 0; i < m_dataTable.count(); i++) {
267 QPieSeries *series = new QPieSeries(chart);
259 QPieSeries *series = new QPieSeries(chart);
268 foreach (Data data, m_dataTable[i]) {
260 foreach (Data data, m_dataTable[i]) {
269 QPieSlice *slice = series->append(data.second, data.first.y());
261 QPieSlice *slice = series->append(data.second, data.first.y());
270 if (data == m_dataTable[i].first()) {
262 if (data == m_dataTable[i].first()) {
271 slice->setLabelVisible();
263 slice->setLabelVisible();
272 slice->setExploded();
264 slice->setExploded();
273 }
265 }
274 }
266 }
275 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
267 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
276 series->setPieSize(pieSize);
268 series->setPieSize(pieSize);
277 series->setHorizontalPosition(hPos);
269 series->setHorizontalPosition(hPos);
278 series->setVerticalPosition(0.5);
270 series->setVerticalPosition(0.5);
279 chart->addSeries(series);
271 chart->addSeries(series);
280 }
272 }
281
273
282 return chart;
274 return chart;
283 }
275 }
284
276
285 QChart* ThemeWidget::createSplineChart() const
277 QChart* ThemeWidget::createSplineChart() const
286 { // spine chart
278 { // spine chart
287 QChart* chart = new QChart();
279 QChart* chart = new QChart();
288 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
280 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
289 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
281 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
290 chart->setTitle("Spline chart");
282 chart->setTitle("Spline chart");
291 QString name("Series ");
283 QString name("Series ");
292 int nameIndex = 0;
284 int nameIndex = 0;
293 foreach (DataList list, m_dataTable) {
285 foreach (DataList list, m_dataTable) {
294 QSplineSeries *series = new QSplineSeries(chart);
286 QSplineSeries *series = new QSplineSeries(chart);
295 foreach (Data data, list)
287 foreach (Data data, list)
296 series->append(data.first);
288 series->append(data.first);
297 series->setName(name + QString::number(nameIndex));
289 series->setName(name + QString::number(nameIndex));
298 nameIndex++;
290 nameIndex++;
299 chart->addSeries(series);
291 chart->addSeries(series);
300 }
292 }
301 chart->createDefaultAxes();
293 chart->createDefaultAxes();
302 return chart;
294 return chart;
303 }
295 }
304
296
305 QChart* ThemeWidget::createScatterChart() const
297 QChart* ThemeWidget::createScatterChart() const
306 { // scatter chart
298 { // scatter chart
307 QChart* chart = new QChart();
299 QChart* chart = new QChart();
308 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
300 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
309 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
301 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
310 chart->setTitle("Scatter chart");
302 chart->setTitle("Scatter chart");
311 QString name("Series ");
303 QString name("Series ");
312 int nameIndex = 0;
304 int nameIndex = 0;
313 foreach (DataList list, m_dataTable) {
305 foreach (DataList list, m_dataTable) {
314 QScatterSeries *series = new QScatterSeries(chart);
306 QScatterSeries *series = new QScatterSeries(chart);
315 foreach (Data data, list)
307 foreach (Data data, list)
316 series->append(data.first);
308 series->append(data.first);
317 series->setName(name + QString::number(nameIndex));
309 series->setName(name + QString::number(nameIndex));
318 nameIndex++;
310 nameIndex++;
319 chart->addSeries(series);
311 chart->addSeries(series);
320 }
312 }
321 chart->createDefaultAxes();
313 chart->createDefaultAxes();
322 return chart;
314 return chart;
323 }
315 }
324
316
325 void ThemeWidget::updateUI()
317 void ThemeWidget::updateUI()
326 {
318 {
327 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
319 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
328
320
329 if (m_charts.at(0)->chart()->theme() != theme) {
321 if (m_charts.at(0)->chart()->theme() != theme) {
330 foreach (QChartView *chartView, m_charts)
322 foreach (QChartView *chartView, m_charts)
331 chartView->chart()->setTheme(theme);
323 chartView->chart()->setTheme(theme);
332
324
333 QPalette pal = window()->palette();
325 QPalette pal = window()->palette();
334 if (theme == QChart::ChartThemeLight) {
326 if (theme == QChart::ChartThemeLight) {
335 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
327 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
336 pal.setColor(QPalette::WindowText, QRgb(0x404044));
328 pal.setColor(QPalette::WindowText, QRgb(0x404044));
337 } else if (theme == QChart::ChartThemeDark) {
329 } else if (theme == QChart::ChartThemeDark) {
338 pal.setColor(QPalette::Window, QRgb(0x121218));
330 pal.setColor(QPalette::Window, QRgb(0x121218));
339 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
331 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
340 } else if (theme == QChart::ChartThemeBlueCerulean) {
332 } else if (theme == QChart::ChartThemeBlueCerulean) {
341 pal.setColor(QPalette::Window, QRgb(0x40434a));
333 pal.setColor(QPalette::Window, QRgb(0x40434a));
342 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
334 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
343 } else if (theme == QChart::ChartThemeBrownSand) {
335 } else if (theme == QChart::ChartThemeBrownSand) {
344 pal.setColor(QPalette::Window, QRgb(0x9e8965));
336 pal.setColor(QPalette::Window, QRgb(0x9e8965));
345 pal.setColor(QPalette::WindowText, QRgb(0x404044));
337 pal.setColor(QPalette::WindowText, QRgb(0x404044));
346 } else if (theme == QChart::ChartThemeBlueNcs) {
338 } else if (theme == QChart::ChartThemeBlueNcs) {
347 pal.setColor(QPalette::Window, QRgb(0x018bba));
339 pal.setColor(QPalette::Window, QRgb(0x018bba));
348 pal.setColor(QPalette::WindowText, QRgb(0x404044));
340 pal.setColor(QPalette::WindowText, QRgb(0x404044));
349 } else if (theme == QChart::ChartThemeHighContrast) {
341 } else if (theme == QChart::ChartThemeHighContrast) {
350 pal.setColor(QPalette::Window, QRgb(0xffab03));
342 pal.setColor(QPalette::Window, QRgb(0xffab03));
351 pal.setColor(QPalette::WindowText, QRgb(0x181818));
343 pal.setColor(QPalette::WindowText, QRgb(0x181818));
352 } else if (theme == QChart::ChartThemeBlueIcy) {
344 } else if (theme == QChart::ChartThemeBlueIcy) {
353 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
345 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
354 pal.setColor(QPalette::WindowText, QRgb(0x404044));
346 pal.setColor(QPalette::WindowText, QRgb(0x404044));
355 } else {
347 } else {
356 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
348 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
357 pal.setColor(QPalette::WindowText, QRgb(0x404044));
349 pal.setColor(QPalette::WindowText, QRgb(0x404044));
358 }
350 }
359 window()->setPalette(pal);
351 window()->setPalette(pal);
360 }
352 }
361
353
362 bool checked = m_antialiasCheckBox->isChecked();
354 bool checked = m_antialiasCheckBox->isChecked();
363 foreach (QChartView *chart, m_charts)
355 foreach (QChartView *chart, m_charts)
364 chart->setRenderHint(QPainter::Antialiasing, checked);
356 chart->setRenderHint(QPainter::Antialiasing, checked);
365
357
366 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
358 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
367 if (m_charts.at(0)->chart()->animationOptions() != options) {
359 if (m_charts.at(0)->chart()->animationOptions() != options) {
368 foreach (QChartView *chartView, m_charts)
360 foreach (QChartView *chartView, m_charts)
369 chartView->chart()->setAnimationOptions(options);
361 chartView->chart()->setAnimationOptions(options);
370 }
362 }
371
363
372 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
364 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
373
365
374 if (!alignment) {
366 if (!alignment) {
375 foreach (QChartView *chartView, m_charts) {
367 foreach (QChartView *chartView, m_charts) {
376 chartView->chart()->legend()->hide();
368 chartView->chart()->legend()->hide();
377 }
369 }
378 } else {
370 } else {
379 foreach (QChartView *chartView, m_charts) {
371 foreach (QChartView *chartView, m_charts) {
380 chartView->chart()->legend()->setAlignment(alignment);
372 chartView->chart()->legend()->setAlignment(alignment);
381 chartView->chart()->legend()->show();
373 chartView->chart()->legend()->show();
382 }
374 }
383 }
375 }
384 }
376 }
385
377
@@ -1,735 +1,743
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qabstractbarseries.h"
21 #include "qabstractbarseries.h"
22 #include "qabstractbarseries_p.h"
22 #include "qabstractbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include "qvaluesaxis.h"
30 #include "qvaluesaxis.h"
31 #include "qcategoriesaxis.h"
31 #include "qcategoriesaxis.h"
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 /*!
35 /*!
36 \class QAbstractBarSeries
36 \class QAbstractBarSeries
37 \brief Series for creating a bar chart
37 \brief Series for creating a bar chart
38 \mainclass
38 \mainclass
39
39
40 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
40 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
41 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
41 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
42 and y-value is the height of the bar. The category names are ignored with this series and x-axis
42 and y-value is the height of the bar. The category names are ignored with this series and x-axis
43 shows the x-values.
43 shows the x-values.
44
44
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 \image examples_barchart.png
46 \image examples_barchart.png
47
47
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 */
49 */
50 /*!
50 /*!
51 \qmlclass AbstractBarSeries QAbstractBarSeries
51 \qmlclass AbstractBarSeries QAbstractBarSeries
52 \inherits QAbstractSeries
52 \inherits QAbstractSeries
53
53
54 The following QML shows how to create a simple bar chart:
54 The following QML shows how to create a simple bar chart:
55 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
55 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
56
56
57 \beginfloatleft
57 \beginfloatleft
58 \image demos_qmlchart6.png
58 \image demos_qmlchart6.png
59 \endfloat
59 \endfloat
60 \clearfloat
60 \clearfloat
61 */
61 */
62
62
63 /*!
63 /*!
64 \property QAbstractBarSeries::barWidth
64 \property QAbstractBarSeries::barWidth
65 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
65 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
66 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
66 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
67 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
67 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
68 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
68 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
69 \sa QBarSeries
69 \sa QBarSeries
70 */
70 */
71 /*!
71 /*!
72 \qmlproperty real AbstractBarSeries::barWidth
72 \qmlproperty real AbstractBarSeries::barWidth
73 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
73 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
74 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
74 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
75 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
75 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
76 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
76 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
77 */
77 */
78
78
79 /*!
79 /*!
80 \property QAbstractBarSeries::count
80 \property QAbstractBarSeries::count
81 Holds the number of sets in series.
81 Holds the number of sets in series.
82 */
82 */
83 /*!
83 /*!
84 \qmlproperty int AbstractBarSeries::count
84 \qmlproperty int AbstractBarSeries::count
85 Holds the number of sets in series.
85 Holds the number of sets in series.
86 */
86 */
87
87
88 /*!
88 /*!
89 \property QAbstractBarSeries::labelsVisible
89 \property QAbstractBarSeries::labelsVisible
90 Defines the visibility of the labels in series
90 Defines the visibility of the labels in series
91 */
91 */
92 /*!
92 /*!
93 \qmlproperty bool AbstractBarSeries::labelsVisible
93 \qmlproperty bool AbstractBarSeries::labelsVisible
94 Defines the visibility of the labels in series
94 Defines the visibility of the labels in series
95 */
95 */
96
96
97 /*!
97 /*!
98 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
98 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
99 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
99 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
100 Clicked bar inside set is indexed by \a index
100 Clicked bar inside set is indexed by \a index
101 */
101 */
102 /*!
102 /*!
103 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
103 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
104 The signal is emitted if the user clicks with a mouse on top of BarSet.
104 The signal is emitted if the user clicks with a mouse on top of BarSet.
105 Clicked bar inside set is indexed by \a index
105 Clicked bar inside set is indexed by \a index
106 */
106 */
107
107
108 /*!
108 /*!
109 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
109 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
110
110
111 The signal is emitted if mouse is hovered on top of series.
111 The signal is emitted if mouse is hovered on top of series.
112 Parameter \a barset is the pointer of barset, where hover happened.
112 Parameter \a barset is the pointer of barset, where hover happened.
113 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
113 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
114 */
114 */
115 /*!
115 /*!
116 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
116 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
117
117
118 The signal is emitted if mouse is hovered on top of series.
118 The signal is emitted if mouse is hovered on top of series.
119 Parameter \a barset is the pointer of barset, where hover happened.
119 Parameter \a barset is the pointer of barset, where hover happened.
120 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
120 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
121 */
121 */
122
122
123 /*!
123 /*!
124 \fn void QAbstractBarSeries::countChanged()
124 \fn void QAbstractBarSeries::countChanged()
125 This signal is emitted when barset count has been changed, for example by append or remove.
125 This signal is emitted when barset count has been changed, for example by append or remove.
126 */
126 */
127 /*!
127 /*!
128 \qmlsignal AbstractBarSeries::onCountChanged()
128 \qmlsignal AbstractBarSeries::onCountChanged()
129 This signal is emitted when barset count has been changed, for example by append or remove.
129 This signal is emitted when barset count has been changed, for example by append or remove.
130 */
130 */
131
131
132 /*!
132 /*!
133 \fn void QAbstractBarSeries::labelsVisibleChanged()
133 \fn void QAbstractBarSeries::labelsVisibleChanged()
134 This signal is emitted when labels visibility have changed.
134 This signal is emitted when labels visibility have changed.
135 \sa isLabelsVisible(), setLabelsVisible()
135 \sa isLabelsVisible(), setLabelsVisible()
136 */
136 */
137
137
138 /*!
138 /*!
139 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
139 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
140 This signal is emitted when \a sets have been added to the series.
140 This signal is emitted when \a sets have been added to the series.
141 \sa append(), insert()
141 \sa append(), insert()
142 */
142 */
143 /*!
143 /*!
144 \qmlsignal AbstractBarSeries::onAdded(BarSet barset)
144 \qmlsignal AbstractBarSeries::onAdded(BarSet barset)
145 Emitted when \a barset has been added to the series.
145 Emitted when \a barset has been added to the series.
146 */
146 */
147
147
148 /*!
148 /*!
149 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
149 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
150 This signal is emitted when \a sets have been removed from the series.
150 This signal is emitted when \a sets have been removed from the series.
151 \sa remove()
151 \sa remove()
152 */
152 */
153 /*!
153 /*!
154 \qmlsignal AbstractBarSeries::onRemoved(BarSet barset)
154 \qmlsignal AbstractBarSeries::onRemoved(BarSet barset)
155 Emitted when \a barset has been removed from the series.
155 Emitted when \a barset has been removed from the series.
156 */
156 */
157
157
158 /*!
158 /*!
159 \qmlmethod BarSet AbstractBarSeries::at(int index)
159 \qmlmethod BarSet AbstractBarSeries::at(int index)
160 Returns bar set at \a index. Returns null if the index is not valid.
160 Returns bar set at \a index. Returns null if the index is not valid.
161 */
161 */
162
162
163 /*!
163 /*!
164 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
164 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
165 Adds a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
165 Adds a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
166 For example:
166 For example:
167 \code
167 \code
168 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
168 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
169 myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]);
169 myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]);
170 \endcode
170 \endcode
171 */
171 */
172
172
173 /*!
173 /*!
174 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
174 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
175 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
175 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
176 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
176 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
177 appended.
177 appended.
178 \sa AbstractBarSeries::append()
178 \sa AbstractBarSeries::append()
179 */
179 */
180
180
181 /*!
181 /*!
182 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
182 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
183 Removes the barset from the series. Returns true if successfull, false otherwise.
183 Removes the barset from the series. Returns true if successfull, false otherwise.
184 */
184 */
185
185
186 /*!
186 /*!
187 \qmlmethod AbstractBarSeries::clear()
187 \qmlmethod AbstractBarSeries::clear()
188 Removes all barsets from the series.
188 Removes all barsets from the series.
189 */
189 */
190
190
191 /*!
191 /*!
192 Constructs empty QAbstractBarSeries.
192 Constructs empty QAbstractBarSeries.
193 QAbstractBarSeries is QObject which is a child of a \a parent.
193 QAbstractBarSeries is QObject which is a child of a \a parent.
194 */
194 */
195 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
195 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
196 QAbstractSeries(*new QAbstractBarSeriesPrivate(this),parent)
196 QAbstractSeries(*new QAbstractBarSeriesPrivate(this),parent)
197 {
197 {
198 }
198 }
199
199
200 /*!
200 /*!
201 Destructs abstractbarseries and owned barsets.
201 Destructs abstractbarseries and owned barsets.
202 */
202 */
203 QAbstractBarSeries::~QAbstractBarSeries()
203 QAbstractBarSeries::~QAbstractBarSeries()
204 {
204 {
205 Q_D(QAbstractBarSeries);
205 Q_D(QAbstractBarSeries);
206 if(d->m_dataset){
206 if(d->m_dataset){
207 d->m_dataset->removeSeries(this);
207 d->m_dataset->removeSeries(this);
208 }
208 }
209 }
209 }
210
210
211 /*!
211 /*!
212 \internal
212 \internal
213 */
213 */
214 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
214 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
215 QAbstractSeries(d,parent)
215 QAbstractSeries(d,parent)
216 {
216 {
217 }
217 }
218
218
219 /*!
219 /*!
220 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
220 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
221 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
221 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
222 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
222 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
223 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
223 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
224 */
224 */
225 void QAbstractBarSeries::setBarWidth(qreal width)
225 void QAbstractBarSeries::setBarWidth(qreal width)
226 {
226 {
227 Q_D(QAbstractBarSeries);
227 Q_D(QAbstractBarSeries);
228 d->setBarWidth(width);
228 d->setBarWidth(width);
229 }
229 }
230
230
231 /*!
231 /*!
232 Returns the width of the bars of the series.
232 Returns the width of the bars of the series.
233 \sa setBarWidth()
233 \sa setBarWidth()
234 */
234 */
235 qreal QAbstractBarSeries::barWidth() const
235 qreal QAbstractBarSeries::barWidth() const
236 {
236 {
237 Q_D(const QAbstractBarSeries);
237 Q_D(const QAbstractBarSeries);
238 return d->barWidth();
238 return d->barWidth();
239 }
239 }
240
240
241 /*!
241 /*!
242 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
242 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
243 Returns true, if appending succeeded.
243 Returns true, if appending succeeded.
244 */
244 */
245 bool QAbstractBarSeries::append(QBarSet *set)
245 bool QAbstractBarSeries::append(QBarSet *set)
246 {
246 {
247 Q_D(QAbstractBarSeries);
247 Q_D(QAbstractBarSeries);
248 bool success = d->append(set);
248 bool success = d->append(set);
249 if (success) {
249 if (success) {
250 QList<QBarSet*> sets;
250 QList<QBarSet*> sets;
251 sets.append(set);
251 sets.append(set);
252 emit barsetsAdded(sets);
252 emit barsetsAdded(sets);
253 emit countChanged();
253 emit countChanged();
254 }
254 }
255 return success;
255 return success;
256 }
256 }
257
257
258 /*!
258 /*!
259 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
259 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
260 Returns true, if set was removed.
260 Returns true, if set was removed.
261 */
261 */
262 bool QAbstractBarSeries::remove(QBarSet *set)
262 bool QAbstractBarSeries::remove(QBarSet *set)
263 {
263 {
264 Q_D(QAbstractBarSeries);
264 Q_D(QAbstractBarSeries);
265 bool success = d->remove(set);
265 bool success = d->remove(set);
266 if (success) {
266 if (success) {
267 QList<QBarSet*> sets;
267 QList<QBarSet*> sets;
268 sets.append(set);
268 sets.append(set);
269 emit barsetsRemoved(sets);
269 emit barsetsRemoved(sets);
270 emit countChanged();
270 emit countChanged();
271 }
271 }
272 return success;
272 return success;
273 }
273 }
274
274
275 /*!
275 /*!
276 Adds a list of barsets to series. Takes ownership of \a sets.
276 Adds a list of barsets to series. Takes ownership of \a sets.
277 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
277 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
278 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
278 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
279 and function returns false.
279 and function returns false.
280 */
280 */
281 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
281 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
282 {
282 {
283 Q_D(QAbstractBarSeries);
283 Q_D(QAbstractBarSeries);
284 bool success = d->append(sets);
284 bool success = d->append(sets);
285 if (success) {
285 if (success) {
286 emit barsetsAdded(sets);
286 emit barsetsAdded(sets);
287 emit countChanged();
287 emit countChanged();
288 }
288 }
289 return success;
289 return success;
290 }
290 }
291
291
292 /*!
292 /*!
293 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
293 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
294 Returns true, if inserting succeeded.
294 Returns true, if inserting succeeded.
295
295
296 */
296 */
297 bool QAbstractBarSeries::insert(int index, QBarSet *set)
297 bool QAbstractBarSeries::insert(int index, QBarSet *set)
298 {
298 {
299 Q_D(QAbstractBarSeries);
299 Q_D(QAbstractBarSeries);
300 bool success = d->insert(index, set);
300 bool success = d->insert(index, set);
301 if (success) {
301 if (success) {
302 QList<QBarSet*> sets;
302 QList<QBarSet*> sets;
303 sets.append(set);
303 sets.append(set);
304 emit barsetsAdded(sets);
304 emit barsetsAdded(sets);
305 emit countChanged();
305 emit countChanged();
306 }
306 }
307 return success;
307 return success;
308 }
308 }
309
309
310 /*!
310 /*!
311 Removes all of the bar sets from the series
311 Removes all of the bar sets from the series
312 */
312 */
313 void QAbstractBarSeries::clear()
313 void QAbstractBarSeries::clear()
314 {
314 {
315 Q_D(QAbstractBarSeries);
315 Q_D(QAbstractBarSeries);
316 QList<QBarSet *> sets = barSets();
316 QList<QBarSet *> sets = barSets();
317 bool success = d->remove(sets);
317 bool success = d->remove(sets);
318 if (success) {
318 if (success) {
319 emit barsetsRemoved(sets);
319 emit barsetsRemoved(sets);
320 emit countChanged();
320 emit countChanged();
321 }
321 }
322 }
322 }
323
323
324 /*!
324 /*!
325 Returns number of sets in series.
325 Returns number of sets in series.
326 */
326 */
327 int QAbstractBarSeries::count() const
327 int QAbstractBarSeries::count() const
328 {
328 {
329 Q_D(const QAbstractBarSeries);
329 Q_D(const QAbstractBarSeries);
330 return d->m_barSets.count();
330 return d->m_barSets.count();
331 }
331 }
332
332
333 /*!
333 /*!
334 Returns a list of sets in series. Keeps ownership of sets.
334 Returns a list of sets in series. Keeps ownership of sets.
335 */
335 */
336 QList<QBarSet*> QAbstractBarSeries::barSets() const
336 QList<QBarSet*> QAbstractBarSeries::barSets() const
337 {
337 {
338 Q_D(const QAbstractBarSeries);
338 Q_D(const QAbstractBarSeries);
339 return d->m_barSets;
339 return d->m_barSets;
340 }
340 }
341
341
342 /*!
342 /*!
343 Sets the visibility of labels in series to \a visible
343 Sets the visibility of labels in series to \a visible
344 */
344 */
345 void QAbstractBarSeries::setLabelsVisible(bool visible)
345 void QAbstractBarSeries::setLabelsVisible(bool visible)
346 {
346 {
347 Q_D(QAbstractBarSeries);
347 Q_D(QAbstractBarSeries);
348 if (d->m_labelsVisible != visible) {
348 if (d->m_labelsVisible != visible) {
349 d->setLabelsVisible(visible);
349 d->setLabelsVisible(visible);
350 emit labelsVisibleChanged();
350 emit labelsVisibleChanged();
351 }
351 }
352 }
352 }
353
353
354 /*!
354 /*!
355 Returns the visibility of labels
355 Returns the visibility of labels
356 */
356 */
357 bool QAbstractBarSeries::isLabelsVisible() const
357 bool QAbstractBarSeries::isLabelsVisible() const
358 {
358 {
359 Q_D(const QAbstractBarSeries);
359 Q_D(const QAbstractBarSeries);
360 return d->m_labelsVisible;
360 return d->m_labelsVisible;
361 }
361 }
362
362
363 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
363 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
364
364
365 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
365 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
366 QAbstractSeriesPrivate(q),
366 QAbstractSeriesPrivate(q),
367 m_barWidth(0.5), // Default value is 50% of category width
367 m_barWidth(0.5), // Default value is 50% of category width
368 m_labelsVisible(false),
368 m_labelsVisible(false),
369 m_visible(true)
369 m_visible(true)
370 {
370 {
371 }
371 }
372
372
373 int QAbstractBarSeriesPrivate::categoryCount() const
373 int QAbstractBarSeriesPrivate::categoryCount() const
374 {
374 {
375 // No categories defined. return count of longest set.
375 // No categories defined. return count of longest set.
376 int count = 0;
376 int count = 0;
377 for (int i=0; i<m_barSets.count(); i++) {
377 for (int i=0; i<m_barSets.count(); i++) {
378 if (m_barSets.at(i)->count() > count) {
378 if (m_barSets.at(i)->count() > count) {
379 count = m_barSets.at(i)->count();
379 count = m_barSets.at(i)->count();
380 }
380 }
381 }
381 }
382
382
383 return count;
383 return count;
384 }
384 }
385
385
386 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
386 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
387 {
387 {
388 if (width < 0.0) {
388 if (width < 0.0) {
389 width = 0.0;
389 width = 0.0;
390 }
390 }
391 m_barWidth = width;
391 m_barWidth = width;
392 emit updatedBars();
392 emit updatedBars();
393 }
393 }
394
394
395 qreal QAbstractBarSeriesPrivate::barWidth() const
395 qreal QAbstractBarSeriesPrivate::barWidth() const
396 {
396 {
397 return m_barWidth;
397 return m_barWidth;
398 }
398 }
399
399
400 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
400 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
401 {
401 {
402 return m_barSets.at(index);
402 return m_barSets.at(index);
403 }
403 }
404
404
405 void QAbstractBarSeriesPrivate::setVisible(bool visible)
405 void QAbstractBarSeriesPrivate::setVisible(bool visible)
406 {
406 {
407 m_visible = visible;
407 m_visible = visible;
408 emit updatedBars();
408 emit updatedBars();
409 }
409 }
410
410
411 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
411 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
412 {
412 {
413 m_labelsVisible = visible;
413 m_labelsVisible = visible;
414 emit labelsVisibleChanged(visible);
414 emit labelsVisibleChanged(visible);
415 }
415 }
416
416
417 qreal QAbstractBarSeriesPrivate::min()
417 qreal QAbstractBarSeriesPrivate::min()
418 {
418 {
419 if (m_barSets.count() <= 0) {
419 if (m_barSets.count() <= 0) {
420 return 0;
420 return 0;
421 }
421 }
422 qreal min = INT_MAX;
422 qreal min = INT_MAX;
423
423
424 for (int i = 0; i < m_barSets.count(); i++) {
424 for (int i = 0; i < m_barSets.count(); i++) {
425 int categoryCount = m_barSets.at(i)->count();
425 int categoryCount = m_barSets.at(i)->count();
426 for (int j = 0; j < categoryCount; j++) {
426 for (int j = 0; j < categoryCount; j++) {
427 qreal temp = m_barSets.at(i)->at(j);
427 qreal temp = m_barSets.at(i)->at(j);
428 if (temp < min)
428 if (temp < min)
429 min = temp;
429 min = temp;
430 }
430 }
431 }
431 }
432 return min;
432 return min;
433 }
433 }
434
434
435 qreal QAbstractBarSeriesPrivate::max()
435 qreal QAbstractBarSeriesPrivate::max()
436 {
436 {
437 if (m_barSets.count() <= 0) {
437 if (m_barSets.count() <= 0) {
438 return 0;
438 return 0;
439 }
439 }
440 qreal max = INT_MIN;
440 qreal max = INT_MIN;
441
441
442 for (int i = 0; i < m_barSets.count(); i++) {
442 for (int i = 0; i < m_barSets.count(); i++) {
443 int categoryCount = m_barSets.at(i)->count();
443 int categoryCount = m_barSets.at(i)->count();
444 for (int j = 0; j < categoryCount; j++) {
444 for (int j = 0; j < categoryCount; j++) {
445 qreal temp = m_barSets.at(i)->at(j);
445 qreal temp = m_barSets.at(i)->at(j);
446 if (temp > max)
446 if (temp > max)
447 max = temp;
447 max = temp;
448 }
448 }
449 }
449 }
450
450
451 return max;
451 return max;
452 }
452 }
453
453
454 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
454 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
455 {
455 {
456 if ((set < 0) || (set >= m_barSets.count())) {
456 if ((set < 0) || (set >= m_barSets.count())) {
457 // No set, no value.
457 // No set, no value.
458 return 0;
458 return 0;
459 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
459 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
460 // No category, no value.
460 // No category, no value.
461 return 0;
461 return 0;
462 }
462 }
463
463
464 return m_barSets.at(set)->at(category);
464 return m_barSets.at(set)->at(category);
465 }
465 }
466
466
467 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
467 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
468 {
468 {
469 if ((set < 0) || (set >= m_barSets.count())) {
469 if ((set < 0) || (set >= m_barSets.count())) {
470 // No set, no value.
470 // No set, no value.
471 return 0;
471 return 0;
472 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
472 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
473 // No category, no value.
473 // No category, no value.
474 return 0;
474 return 0;
475 }
475 }
476
476
477 qreal value = m_barSets.at(set)->at(category);
477 qreal value = m_barSets.at(set)->at(category);
478 qreal sum = categorySum(category);
478 qreal sum = categorySum(category);
479 if ( qFuzzyIsNull(sum) ) {
479 if ( qFuzzyIsNull(sum) ) {
480 return 0;
480 return 0;
481 }
481 }
482
482
483 return value / sum;
483 return value / sum;
484 }
484 }
485
485
486 qreal QAbstractBarSeriesPrivate::categorySum(int category)
486 qreal QAbstractBarSeriesPrivate::categorySum(int category)
487 {
487 {
488 qreal sum(0);
488 qreal sum(0);
489 int count = m_barSets.count(); // Count sets
489 int count = m_barSets.count(); // Count sets
490 for (int set = 0; set < count; set++) {
490 for (int set = 0; set < count; set++) {
491 if (category < m_barSets.at(set)->count())
491 if (category < m_barSets.at(set)->count())
492 sum += m_barSets.at(set)->at(category);
492 sum += m_barSets.at(set)->at(category);
493 }
493 }
494 return sum;
494 return sum;
495 }
495 }
496
496
497 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
497 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
498 {
498 {
499 qreal sum(0);
499 qreal sum(0);
500 int count = m_barSets.count(); // Count sets
500 int count = m_barSets.count(); // Count sets
501 for (int set = 0; set < count; set++) {
501 for (int set = 0; set < count; set++) {
502 if (category < m_barSets.at(set)->count())
502 if (category < m_barSets.at(set)->count())
503 sum += qAbs(m_barSets.at(set)->at(category));
503 sum += qAbs(m_barSets.at(set)->at(category));
504 }
504 }
505 return sum;
505 return sum;
506 }
506 }
507
507
508 qreal QAbstractBarSeriesPrivate::maxCategorySum()
508 qreal QAbstractBarSeriesPrivate::maxCategorySum()
509 {
509 {
510 qreal max = INT_MIN;
510 qreal max = INT_MIN;
511 int count = categoryCount();
511 int count = categoryCount();
512 for (int i = 0; i < count; i++) {
512 for (int i = 0; i < count; i++) {
513 qreal sum = categorySum(i);
513 qreal sum = categorySum(i);
514 if (sum > max)
514 if (sum > max)
515 max = sum;
515 max = sum;
516 }
516 }
517 return max;
517 return max;
518 }
518 }
519
519
520 qreal QAbstractBarSeriesPrivate::minX()
520 qreal QAbstractBarSeriesPrivate::minX()
521 {
521 {
522 if (m_barSets.count() <= 0) {
522 if (m_barSets.count() <= 0) {
523 return 0;
523 return 0;
524 }
524 }
525 qreal min = INT_MAX;
525 qreal min = INT_MAX;
526
526
527 for (int i = 0; i < m_barSets.count(); i++) {
527 for (int i = 0; i < m_barSets.count(); i++) {
528 int categoryCount = m_barSets.at(i)->count();
528 int categoryCount = m_barSets.at(i)->count();
529 for (int j = 0; j < categoryCount; j++) {
529 for (int j = 0; j < categoryCount; j++) {
530 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
530 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
531 if (temp < min)
531 if (temp < min)
532 min = temp;
532 min = temp;
533 }
533 }
534 }
534 }
535 return min;
535 return min;
536 }
536 }
537
537
538 qreal QAbstractBarSeriesPrivate::maxX()
538 qreal QAbstractBarSeriesPrivate::maxX()
539 {
539 {
540 if (m_barSets.count() <= 0) {
540 if (m_barSets.count() <= 0) {
541 return 0;
541 return 0;
542 }
542 }
543 qreal max = INT_MIN;
543 qreal max = INT_MIN;
544
544
545 for (int i = 0; i < m_barSets.count(); i++) {
545 for (int i = 0; i < m_barSets.count(); i++) {
546 int categoryCount = m_barSets.at(i)->count();
546 int categoryCount = m_barSets.at(i)->count();
547 for (int j = 0; j < categoryCount; j++) {
547 for (int j = 0; j < categoryCount; j++) {
548 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
548 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
549 if (temp > max)
549 if (temp > max)
550 max = temp;
550 max = temp;
551 }
551 }
552 }
552 }
553
553
554 return max;
554 return max;
555 }
555 }
556
556
557
557
558 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
558 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
559 {
559 {
560 qreal minX(domain.minX());
560 qreal minX(domain.minX());
561 qreal minY(domain.minY());
561 qreal minY(domain.minY());
562 qreal maxX(domain.maxX());
562 qreal maxX(domain.maxX());
563 qreal maxY(domain.maxY());
563 qreal maxY(domain.maxY());
564 int tickXCount(domain.tickXCount());
564 int tickXCount(domain.tickXCount());
565 int tickYCount(domain.tickYCount());
565 int tickYCount(domain.tickYCount());
566
566
567 qreal seriesMinX = this->minX();
567 qreal seriesMinX = this->minX();
568 qreal seriesMaxX = this->maxX();
568 qreal seriesMaxX = this->maxX();
569 qreal y = max();
569 qreal y = max();
570 minX = qMin(minX, seriesMinX - 0.5);
570 minX = qMin(minX, seriesMinX - 0.5);
571 minY = qMin(minY, y);
571 minY = qMin(minY, y);
572 maxX = qMax(maxX, seriesMaxX + 0.5);
572 maxX = qMax(maxX, seriesMaxX + 0.5);
573 maxY = qMax(maxY, y);
573 maxY = qMax(maxY, y);
574 tickXCount = categoryCount()+1;
574 tickXCount = categoryCount()+1;
575
575
576 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
576 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
577 }
577 }
578
578
579 Chart* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
579 Chart* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
580 {
580 {
581 Q_Q(QAbstractBarSeries);
581 Q_Q(QAbstractBarSeries);
582
582
583 BarChartItem* bar = new BarChartItem(q,presenter);
583 BarChartItem* bar = new BarChartItem(q,presenter);
584 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
584 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
585 presenter->animator()->addAnimation(bar);
585 presenter->animator()->addAnimation(bar);
586 }
586 }
587 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
587 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
588 return bar;
588 return bar;
589
589
590 }
590 }
591
591
592 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
592 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
593 {
593 {
594 Q_Q(QAbstractBarSeries);
594 Q_Q(QAbstractBarSeries);
595 QList<LegendMarker*> markers;
595 QList<LegendMarker*> markers;
596 foreach(QBarSet* set, q->barSets()) {
596 foreach(QBarSet* set, q->barSets()) {
597 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
597 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
598 markers << marker;
598 markers << marker;
599 }
599 }
600
600
601 return markers;
601 return markers;
602 }
602 }
603
603
604 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
604 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
605 {
605 {
606 Q_Q(QAbstractBarSeries);
606 Q_Q(QAbstractBarSeries);
607 if ((m_barSets.contains(set)) || (set == 0)) {
607 if ((m_barSets.contains(set)) || (set == 0)) {
608 // Fail if set is already in list or set is null.
608 // Fail if set is already in list or set is null.
609 return false;
609 return false;
610 }
610 }
611 m_barSets.append(set);
611 m_barSets.append(set);
612 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
612 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
613 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
613 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
614 emit restructuredBars(); // this notifies barchartitem
614 emit restructuredBars(); // this notifies barchartitem
615 if (m_dataset) {
615 if (m_dataset) {
616 m_dataset->updateSeries(q); // this notifies legend
616 m_dataset->updateSeries(q); // this notifies legend
617 }
617 }
618 return true;
618 return true;
619 }
619 }
620
620
621 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
621 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
622 {
622 {
623 Q_Q(QAbstractBarSeries);
623 Q_Q(QAbstractBarSeries);
624 if (!m_barSets.contains(set)) {
624 if (!m_barSets.contains(set)) {
625 // Fail if set is not in list
625 // Fail if set is not in list
626 return false;
626 return false;
627 }
627 }
628 m_barSets.removeOne(set);
628 m_barSets.removeOne(set);
629 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
629 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
630 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
630 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
631 emit restructuredBars(); // this notifies barchartitem
631 emit restructuredBars(); // this notifies barchartitem
632 if (m_dataset) {
632 if (m_dataset) {
633 m_dataset->updateSeries(q); // this notifies legend
633 m_dataset->updateSeries(q); // this notifies legend
634 }
634 }
635 return true;
635 return true;
636 }
636 }
637
637
638 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
638 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
639 {
639 {
640 Q_Q(QAbstractBarSeries);
640 Q_Q(QAbstractBarSeries);
641 foreach (QBarSet* set, sets) {
641 foreach (QBarSet* set, sets) {
642 if ((set == 0) || (m_barSets.contains(set))) {
642 if ((set == 0) || (m_barSets.contains(set))) {
643 // Fail if any of the sets is null or is already appended.
643 // Fail if any of the sets is null or is already appended.
644 return false;
644 return false;
645 }
645 }
646 if (sets.count(set) != 1) {
646 if (sets.count(set) != 1) {
647 // Also fail if same set is more than once in given list.
647 // Also fail if same set is more than once in given list.
648 return false;
648 return false;
649 }
649 }
650 }
650 }
651
651
652 foreach (QBarSet* set, sets) {
652 foreach (QBarSet* set, sets) {
653 m_barSets.append(set);
653 m_barSets.append(set);
654 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
654 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
655 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
655 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
656 }
656 }
657 emit restructuredBars(); // this notifies barchartitem
657 emit restructuredBars(); // this notifies barchartitem
658 if (m_dataset) {
658 if (m_dataset) {
659 m_dataset->updateSeries(q); // this notifies legend
659 m_dataset->updateSeries(q); // this notifies legend
660 }
660 }
661 return true;
661 return true;
662 }
662 }
663
663
664 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
664 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
665 {
665 {
666 Q_Q(QAbstractBarSeries);
666 Q_Q(QAbstractBarSeries);
667 if (sets.count() == 0) {
667 if (sets.count() == 0) {
668 return false;
668 return false;
669 }
669 }
670 foreach (QBarSet* set, sets) {
670 foreach (QBarSet* set, sets) {
671 if ((set == 0) || (!m_barSets.contains(set))) {
671 if ((set == 0) || (!m_barSets.contains(set))) {
672 // Fail if any of the sets is null or is not in series
672 // Fail if any of the sets is null or is not in series
673 return false;
673 return false;
674 }
674 }
675 if (sets.count(set) != 1) {
675 if (sets.count(set) != 1) {
676 // Also fail if same set is more than once in given list.
676 // Also fail if same set is more than once in given list.
677 return false;
677 return false;
678 }
678 }
679 }
679 }
680
680
681 foreach (QBarSet* set, sets) {
681 foreach (QBarSet* set, sets) {
682 m_barSets.removeOne(set);
682 m_barSets.removeOne(set);
683 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
683 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
684 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
684 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
685 }
685 }
686
686
687 emit restructuredBars(); // this notifies barchartitem
687 emit restructuredBars(); // this notifies barchartitem
688 if (m_dataset) {
688 if (m_dataset) {
689 m_dataset->updateSeries(q); // this notifies legend
689 m_dataset->updateSeries(q); // this notifies legend
690 }
690 }
691 return true;
691 return true;
692 }
692 }
693
693
694 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
694 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
695 {
695 {
696 Q_Q(QAbstractBarSeries);
696 Q_Q(QAbstractBarSeries);
697 if ((m_barSets.contains(set)) || (set == 0)) {
697 if ((m_barSets.contains(set)) || (set == 0)) {
698 // Fail if set is already in list or set is null.
698 // Fail if set is already in list or set is null.
699 return false;
699 return false;
700 }
700 }
701 m_barSets.insert(index, set);
701 m_barSets.insert(index, set);
702 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
702 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
703 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
703 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
704 emit restructuredBars(); // this notifies barchartitem
704 emit restructuredBars(); // this notifies barchartitem
705 if (m_dataset) {
705 if (m_dataset) {
706 m_dataset->updateSeries(q); // this notifies legend
706 m_dataset->updateSeries(q); // this notifies legend
707 }
707 }
708 return true;
708 return true;
709 }
709 }
710
710
711 void QAbstractBarSeriesPrivate::initializeAxisX(QAbstractAxis* axis)
711 void QAbstractBarSeriesPrivate::initializeAxisX(QAbstractAxis* axis)
712 {
712 {
713 Q_UNUSED(axis);
713 if(axis->type()==QAbstractAxis::AxisTypeCategories)
714 {
715 QCategoriesAxis* cataxis = qobject_cast<QCategoriesAxis*>(axis);
716 Q_ASSERT(cataxis);
717 QStringList categories;
718 for (int i(1); i < categoryCount()+1; i++)
719 categories << QString::number(i);
720 cataxis->append(categories);
721 }
714 }
722 }
715
723
716 void QAbstractBarSeriesPrivate::initializeAxisY(QAbstractAxis* axis)
724 void QAbstractBarSeriesPrivate::initializeAxisY(QAbstractAxis* axis)
717 {
725 {
718 Q_UNUSED(axis)
726 Q_UNUSED(axis)
719 }
727 }
720
728
721 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisXType() const
729 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisXType() const
722 {
730 {
723 return QAbstractAxis::AxisTypeCategories;
731 return QAbstractAxis::AxisTypeCategories;
724 }
732 }
725
733
726 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisYType() const
734 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisYType() const
727 {
735 {
728 return QAbstractAxis::AxisTypeValues;
736 return QAbstractAxis::AxisTypeValues;
729 }
737 }
730
738
731 #include "moc_qabstractbarseries.cpp"
739 #include "moc_qabstractbarseries.cpp"
732 #include "moc_qabstractbarseries_p.cpp"
740 #include "moc_qabstractbarseries_p.cpp"
733
741
734
742
735 QTCOMMERCIALCHART_END_NAMESPACE
743 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now