##// END OF EJS Templates
Refactor CategoriesAxis to BarCategoriesAxis part 2 of 2
Michal Klocek -
r1613:da4a379949e0
parent child
Show More
@@ -0,0 +1,299
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "qbarcategoriesaxis.h"
22 #include "qbarcategoriesaxis_p.h"
23 #include "chartcategoriesaxisx_p.h"
24 #include "chartcategoriesaxisy_p.h"
25 #include <qmath.h>
26 #include <QDebug>
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent):
31 QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent)
32 {
33 }
34
35 QBarCategoriesAxis::~QBarCategoriesAxis()
36 {
37 }
38
39 QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
40 {
41
42 }
43
44 /*!
45 Appends \a categories to axis
46 */
47 void QBarCategoriesAxis::append(const QStringList &categories)
48 {
49 Q_D(QBarCategoriesAxis);
50 if (d->m_categories.isEmpty()) {
51 d->m_categories.append(categories);
52 setRange(categories.first(),categories.last());
53 }else{
54 d->m_categories.append(categories);
55 }
56
57 emit categoriesChanged();
58 }
59
60 /*!
61 Appends \a category to axis
62 */
63 void QBarCategoriesAxis::append(const QString &category)
64 {
65 Q_D(QBarCategoriesAxis);
66 if (d->m_categories.isEmpty()) {
67 d->m_categories.append(category);
68 setRange(category,category);
69 }else{
70 d->m_categories.append(category);
71 }
72 emit categoriesChanged();
73 }
74
75 /*!
76 Removes \a category from axis
77 */
78 void QBarCategoriesAxis::remove(const QString &category)
79 {
80 Q_D(QBarCategoriesAxis);
81 if (d->m_categories.contains(category)) {
82 d->m_categories.removeAt(d->m_categories.indexOf(category));
83 setRange(d->m_categories.first(),d->m_categories.last());
84 emit categoriesChanged();
85 }
86 }
87
88 /*!
89 Inserts \a category to axis at \a index
90 */
91 void QBarCategoriesAxis::insert(int index, const QString &category)
92 {
93 Q_D(QBarCategoriesAxis);
94 if (d->m_categories.isEmpty()) {
95 d->m_categories.insert(index,category);
96 setRange(category,category);
97 }else{
98
99 }
100 emit categoriesChanged();
101 }
102
103 /*!
104 Removes all categories.
105 */
106 void QBarCategoriesAxis::clear()
107 {
108 Q_D(QBarCategoriesAxis);
109 d->m_categories.clear();
110 setRange(QString::null,QString::null);
111 emit categoriesChanged();
112 }
113
114 void QBarCategoriesAxis::setCategories(const QStringList &categories)
115 {
116 Q_D(QBarCategoriesAxis);
117 if(d->m_categories!=categories){
118 d->m_categories = categories;
119 setRange(categories.first(),categories.last());
120 emit categoriesChanged();
121 }
122 }
123
124 QStringList QBarCategoriesAxis::categories()
125 {
126 Q_D(QBarCategoriesAxis);
127 return d->m_categories;
128 }
129
130 /*!
131 Returns number of categories.
132 */
133 int QBarCategoriesAxis::count() const
134 {
135 Q_D(const QBarCategoriesAxis);
136 return d->m_categories.count();
137 }
138
139 /*!
140 Returns category at \a index. Index must be valid.
141 */
142 QString QBarCategoriesAxis::at(int index) const
143 {
144 Q_D(const QBarCategoriesAxis);
145 return d->m_categories.at(index);
146 }
147
148 /*!
149 Sets minimum category to \a min.
150 */
151 void QBarCategoriesAxis::setMin(const QString& min)
152 {
153 Q_D(QBarCategoriesAxis);
154 setRange(min,d->m_maxCategory);
155 }
156
157 /*!
158 Returns minimum category.
159 */
160 QString QBarCategoriesAxis::min() const
161 {
162 Q_D(const QBarCategoriesAxis);
163 return d->m_minCategory;
164 }
165
166 /*!
167 Sets maximum category to \a max.
168 */
169 void QBarCategoriesAxis::setMax(const QString& max)
170 {
171 Q_D(QBarCategoriesAxis);
172 setRange(d->m_minCategory,max);
173 }
174
175 /*!
176 Returns maximum category
177 */
178 QString QBarCategoriesAxis::max() const
179 {
180 Q_D(const QBarCategoriesAxis);
181 return d->m_maxCategory;
182 }
183
184 /*!
185 Sets range from \a minCategory to \a maxCategory
186 */
187 void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
188 {
189 Q_D(QBarCategoriesAxis);
190
191 int minIndex = d->m_categories.indexOf(minCategory);
192 if (minIndex == -1) {
193 return;
194 }
195 int maxIndex = d->m_categories.indexOf(maxCategory);
196 if (maxIndex == -1) {
197 return;
198 }
199
200 if (maxIndex <= minIndex) {
201 // max must be greater than min
202 return;
203 }
204
205 bool changed = false;
206 if (!qFuzzyIsNull(d->m_min - (minIndex))) {
207 d->m_minCategory = minCategory;
208 d->m_min = minIndex;
209 emit minChanged(minCategory);
210 changed = true;
211 }
212
213 if (!qFuzzyIsNull(d->m_max - (maxIndex))) {
214 d->m_max = maxIndex;
215 d->m_maxCategory = maxCategory;
216 emit maxChanged(maxCategory);
217 changed = true;
218 }
219
220 if ((changed)) {
221 d->emitRange();
222 emit categoriesChanged();
223 }
224 }
225
226 /*!
227 Returns the type of axis.
228 */
229 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
230 {
231 return AxisTypeCategories;
232 }
233
234 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
235
236 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
237 QAbstractAxisPrivate(q)
238 {
239
240 }
241
242 QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate()
243 {
244
245 }
246
247 void QBarCategoriesAxisPrivate::setMin(const QVariant &min)
248 {
249 setRange(min,m_maxCategory);
250 }
251
252 void QBarCategoriesAxisPrivate::setMax(const QVariant &max)
253 {
254 setRange(m_minCategory,max);
255 }
256
257 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
258 {
259 Q_Q(QBarCategoriesAxis);
260 QString value1 = min.toString();
261 QString value2 = max.toString();
262 q->setRange(value1,value2);
263 }
264
265 int QBarCategoriesAxisPrivate::ticksCount() const
266 {
267 return m_categories.count()+1;
268 }
269
270 void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
271 {
272 m_min = min;
273 m_max = max;
274 m_ticksCount = count;
275 }
276
277 ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
278 {
279 Q_Q( QBarCategoriesAxis);
280 if(m_orientation == Qt::Vertical){
281 return new ChartCategoriesAxisY(q,presenter);
282 }else{
283 return new ChartCategoriesAxisX(q,presenter);
284 }
285 }
286
287 void QBarCategoriesAxisPrivate::emitRange()
288 {
289 Q_Q( QBarCategoriesAxis);
290 if(!q->signalsBlocked()) {
291 emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false);
292 }
293 }
294
295
296 #include "moc_qbarcategoriesaxis.cpp"
297 #include "moc_qbarcategoriesaxis_p.cpp"
298
299 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,76
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef QBARCATEGORIESAXIS_H
22 #define QBARCATEGORIESAXIS_H
23
24 #include "qabstractaxis.h"
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
28 class QBarCategoriesAxisPrivate;
29
30 class QTCOMMERCIALCHART_EXPORT QBarCategoriesAxis : public QAbstractAxis
31 {
32 Q_OBJECT
33 Q_PROPERTY(QStringList categories READ categories WRITE setCategories NOTIFY categoriesChanged)
34 Q_PROPERTY(QString min READ min WRITE setMin NOTIFY minChanged)
35 Q_PROPERTY(QString max READ max WRITE setMax NOTIFY maxChanged)
36
37 public:
38 explicit QBarCategoriesAxis(QObject *parent = 0);
39 ~QBarCategoriesAxis();
40
41 protected:
42 QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent = 0);
43
44 public:
45 AxisType type() const;
46 void append(const QStringList &categories);
47 void append(const QString &category);
48 void remove(const QString &category);
49 void insert(int index, const QString &category);
50 void clear();
51 void setCategories(const QStringList &categories);
52 QStringList categories();
53 int count() const;
54 QString at(int index) const;
55
56 //range handling
57 void setMin(const QString& minCategory);
58 QString min() const;
59 void setMax(const QString& maxCategory);
60 QString max() const;
61 void setRange(const QString& minCategory, const QString& maxCategory);
62
63 Q_SIGNALS:
64 void categoriesChanged();
65 void minChanged(const QString &min);
66 void maxChanged(const QString &max);
67 void rangeChanged(const QString &min, const QString &max);
68
69 private:
70 Q_DECLARE_PRIVATE(QBarCategoriesAxis)
71 Q_DISABLE_COPY(QBarCategoriesAxis)
72 };
73
74 QTCOMMERCIALCHART_END_NAMESPACE
75
76 #endif // QCATEGORIESAXIS_H
@@ -0,0 +1,74
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30 #ifndef QBARCATEGORIESAXIS_P_H
31 #define QBARCATEGORIESAXIS_P_H
32
33 #include "qbarcategoriesaxis.h"
34 #include "qabstractaxis_p.h"
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
38 class QBarCategoriesAxisPrivate : public QAbstractAxisPrivate
39 {
40 Q_OBJECT
41
42 public:
43 QBarCategoriesAxisPrivate(QBarCategoriesAxis *q);
44 ~QBarCategoriesAxisPrivate();
45
46 public:
47 ChartAxis* createGraphics(ChartPresenter* presenter);
48 void emitRange();
49
50 private:
51 //range handling
52 void setMin(const QVariant &min);
53 void setMax(const QVariant &max);
54 void setRange(const QVariant &min, const QVariant &max);
55 int ticksCount() const;
56
57 Q_SIGNALS:
58 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
59
60 public Q_SLOTS:
61 void handleAxisRangeChanged(qreal min, qreal max,int count);
62
63 private:
64 QStringList m_categories;
65 QString m_minCategory;
66 QString m_maxCategory;
67
68 private:
69 Q_DECLARE_PUBLIC(QBarCategoriesAxis)
70 };
71
72 QTCOMMERCIALCHART_END_NAMESPACE
73
74 #endif // QBARCATEGORIESAXIS_P_H
@@ -1,377 +1,377
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 "themewidget.h"
22 22
23 23 #include <QChartView>
24 24 #include <QPieSeries>
25 25 #include <QPieSlice>
26 26 #include <QAbstractBarSeries>
27 27 #include <QPercentBarSeries>
28 28 #include <QStackedBarSeries>
29 29 #include <QBarSet>
30 30 #include <QLineSeries>
31 31 #include <QSplineSeries>
32 32 #include <QScatterSeries>
33 33 #include <QAreaSeries>
34 34 #include <QLegend>
35 35 #include <QGridLayout>
36 36 #include <QFormLayout>
37 37 #include <QComboBox>
38 38 #include <QSpinBox>
39 39 #include <QCheckBox>
40 40 #include <QGroupBox>
41 41 #include <QLabel>
42 42 #include <QTime>
43 #include <QCategoriesAxis>
43 #include <QBarCategoriesAxis>
44 44
45 45 ThemeWidget::ThemeWidget(QWidget* parent) :
46 46 QWidget(parent),
47 47 m_listCount(3),
48 48 m_valueMax(10),
49 49 m_valueCount(7),
50 50 m_dataTable(generateRandomData(m_listCount,m_valueMax,m_valueCount)),
51 51 m_themeComboBox(createThemeBox()),
52 52 m_antialiasCheckBox(new QCheckBox("Anti-aliasing")),
53 53 m_animatedComboBox(createAnimationBox()),
54 54 m_legendComboBox(createLegendBox())
55 55 {
56 56 connectSignals();
57 57 // create layout
58 58 QGridLayout* baseLayout = new QGridLayout();
59 59 QHBoxLayout *settingsLayout = new QHBoxLayout();
60 60 settingsLayout->addWidget(new QLabel("Theme:"));
61 61 settingsLayout->addWidget(m_themeComboBox);
62 62 settingsLayout->addWidget(new QLabel("Animation:"));
63 63 settingsLayout->addWidget(m_animatedComboBox);
64 64 settingsLayout->addWidget(new QLabel("Legend:"));
65 65 settingsLayout->addWidget(m_legendComboBox);
66 66 settingsLayout->addWidget(m_antialiasCheckBox);
67 67 settingsLayout->addStretch();
68 68 baseLayout->addLayout(settingsLayout, 0, 0, 1, 3);
69 69
70 70 //create charts
71 71
72 72 QChartView *chartView;
73 73
74 74 chartView = new QChartView(createAreaChart());
75 75 baseLayout->addWidget(chartView, 1, 0);
76 76 m_charts << chartView;
77 77
78 78 chartView = new QChartView(createBarChart(m_valueCount));
79 79 baseLayout->addWidget(chartView, 1, 1);
80 80 m_charts << chartView;
81 81
82 82 chartView = new QChartView(createLineChart());
83 83 baseLayout->addWidget(chartView, 1, 2);
84 84 m_charts << chartView;
85 85
86 86 chartView = new QChartView(createPieChart());
87 87 chartView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); // funny things happen if the pie slice labels no not fit the screen...
88 88 baseLayout->addWidget(chartView, 2, 0);
89 89 m_charts << chartView;
90 90
91 91 chartView = new QChartView(createSplineChart());
92 92 baseLayout->addWidget(chartView, 2, 1);
93 93 m_charts << chartView;
94 94
95 95 chartView = new QChartView(createScatterChart());
96 96 baseLayout->addWidget(chartView, 2, 2);
97 97 m_charts << chartView;
98 98
99 99 setLayout(baseLayout);
100 100
101 101 // Set defaults
102 102 m_antialiasCheckBox->setChecked(true);
103 103 updateUI();
104 104 }
105 105
106 106 ThemeWidget::~ThemeWidget()
107 107 {
108 108 }
109 109
110 110 void ThemeWidget::connectSignals()
111 111 {
112 112 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
113 113 connect(m_antialiasCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
114 114 connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
115 115 connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI()));
116 116 }
117 117
118 118 DataTable ThemeWidget::generateRandomData(int listCount,int valueMax,int valueCount) const
119 119 {
120 120 DataTable dataTable;
121 121
122 122 // set seed for random stuff
123 123 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
124 124
125 125 // generate random data
126 126 for (int i(0); i < listCount; i++) {
127 127 DataList dataList;
128 128 qreal yValue(0);
129 129 for (int j(0); j < valueCount; j++) {
130 130 yValue = yValue + (qreal) (qrand() % valueMax) / (qreal) valueCount;
131 131 QPointF value((j + (qreal) rand() / (qreal) RAND_MAX) * ((qreal) m_valueMax / (qreal) valueCount),
132 132 yValue);
133 133 QString label = "Slice " + QString::number(i) + ":" + QString::number(j);
134 134 dataList << Data(value, label);
135 135 }
136 136 dataTable << dataList;
137 137 }
138 138
139 139 return dataTable;
140 140 }
141 141
142 142 QComboBox* ThemeWidget::createThemeBox() const
143 143 {
144 144 // settings layout
145 145 QComboBox* themeComboBox = new QComboBox();
146 146 themeComboBox->addItem("Light", QChart::ChartThemeLight);
147 147 themeComboBox->addItem("Blue Cerulean", QChart::ChartThemeBlueCerulean);
148 148 themeComboBox->addItem("Dark", QChart::ChartThemeDark);
149 149 themeComboBox->addItem("Brown Sand", QChart::ChartThemeBrownSand);
150 150 themeComboBox->addItem("Blue NCS", QChart::ChartThemeBlueNcs);
151 151 themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
152 152 themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
153 153 return themeComboBox;
154 154 }
155 155
156 156 QComboBox* ThemeWidget::createAnimationBox() const
157 157 {
158 158 // settings layout
159 159 QComboBox* animationComboBox = new QComboBox();
160 160 animationComboBox->addItem("No Animations", QChart::NoAnimation);
161 161 animationComboBox->addItem("GridAxis Animations", QChart::GridAxisAnimations);
162 162 animationComboBox->addItem("Series Animations", QChart::SeriesAnimations);
163 163 animationComboBox->addItem("All Animations", QChart::AllAnimations);
164 164 return animationComboBox;
165 165 }
166 166
167 167 QComboBox* ThemeWidget::createLegendBox() const
168 168 {
169 169 QComboBox* legendComboBox = new QComboBox();
170 170 legendComboBox->addItem("No Legend ", 0);
171 171 legendComboBox->addItem("Legend Top", Qt::AlignTop);
172 172 legendComboBox->addItem("Legend Bottom", Qt::AlignBottom);
173 173 legendComboBox->addItem("Legend Left", Qt::AlignLeft);
174 174 legendComboBox->addItem("Legend Right", Qt::AlignRight);
175 175 return legendComboBox;
176 176 }
177 177
178 178 QChart* ThemeWidget::createAreaChart() const
179 179 {
180 180 QChart *chart = new QChart();
181 181 // chart->axisX()->setNiceNumbersEnabled(true);
182 182 // chart->axisY()->setNiceNumbersEnabled(true);
183 183 chart->setTitle("Area chart");
184 184
185 185 // The lower series initialized to zero values
186 186 QLineSeries *lowerSeries = 0;
187 187 QString name("Series ");
188 188 int nameIndex = 0;
189 189 for (int i(0); i < m_dataTable.count(); i++) {
190 190 QLineSeries *upperSeries = new QLineSeries(chart);
191 191 for (int j(0); j < m_dataTable[i].count(); j++) {
192 192 Data data = m_dataTable[i].at(j);
193 193 if (lowerSeries){
194 194 const QList<QPointF>& points = lowerSeries->points();
195 195 upperSeries->append(QPointF(j, points[i].y() + data.first.y()));
196 196 }else
197 197 upperSeries->append(QPointF(j, data.first.y()));
198 198 }
199 199 QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
200 200 area->setName(name + QString::number(nameIndex));
201 201 nameIndex++;
202 202 chart->addSeries(area);
203 203 chart->createDefaultAxes();
204 204 lowerSeries = upperSeries;
205 205 }
206 206
207 207 return chart;
208 208 }
209 209
210 210 QChart* ThemeWidget::createBarChart(int valueCount) const
211 211 {
212 212 QChart* chart = new QChart();
213 213 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
214 214 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
215 215 chart->setTitle("Bar chart");
216 216
217 217 QStackedBarSeries* series = new QStackedBarSeries(chart);
218 218 for (int i(0); i < m_dataTable.count(); i++) {
219 219 QBarSet *set = new QBarSet("Bar set " + QString::number(i));
220 220 foreach (Data data, m_dataTable[i])
221 221 *set << data.first.y();
222 222 series->append(set);
223 223 }
224 224 chart->addSeries(series);
225 225 chart->createDefaultAxes();
226 226
227 227 return chart;
228 228 }
229 229
230 230 QChart* ThemeWidget::createLineChart() const
231 231 {
232 232 QChart* chart = new QChart();
233 233 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
234 234 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
235 235 chart->setTitle("Line chart");
236 236
237 237 QString name("Series ");
238 238 int nameIndex = 0;
239 239 foreach (DataList list, m_dataTable) {
240 240 QLineSeries *series = new QLineSeries(chart);
241 241 foreach (Data data, list)
242 242 series->append(data.first);
243 243 series->setName(name + QString::number(nameIndex));
244 244 nameIndex++;
245 245 chart->addSeries(series);
246 246 }
247 247 chart->createDefaultAxes();
248 248
249 249 return chart;
250 250 }
251 251
252 252 QChart* ThemeWidget::createPieChart() const
253 253 {
254 254 QChart* chart = new QChart();
255 255 chart->setTitle("Pie chart");
256 256
257 257 qreal pieSize = 1.0 / m_dataTable.count();
258 258 for (int i = 0; i < m_dataTable.count(); i++) {
259 259 QPieSeries *series = new QPieSeries(chart);
260 260 foreach (Data data, m_dataTable[i]) {
261 261 QPieSlice *slice = series->append(data.second, data.first.y());
262 262 if (data == m_dataTable[i].first()) {
263 263 slice->setLabelVisible();
264 264 slice->setExploded();
265 265 }
266 266 }
267 267 qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count());
268 268 series->setPieSize(pieSize);
269 269 series->setHorizontalPosition(hPos);
270 270 series->setVerticalPosition(0.5);
271 271 chart->addSeries(series);
272 272 }
273 273
274 274 return chart;
275 275 }
276 276
277 277 QChart* ThemeWidget::createSplineChart() const
278 278 { // spine chart
279 279 QChart* chart = new QChart();
280 280 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
281 281 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
282 282 chart->setTitle("Spline chart");
283 283 QString name("Series ");
284 284 int nameIndex = 0;
285 285 foreach (DataList list, m_dataTable) {
286 286 QSplineSeries *series = new QSplineSeries(chart);
287 287 foreach (Data data, list)
288 288 series->append(data.first);
289 289 series->setName(name + QString::number(nameIndex));
290 290 nameIndex++;
291 291 chart->addSeries(series);
292 292 }
293 293 chart->createDefaultAxes();
294 294 return chart;
295 295 }
296 296
297 297 QChart* ThemeWidget::createScatterChart() const
298 298 { // scatter chart
299 299 QChart* chart = new QChart();
300 300 //TODO: chart->axisX()->setNiceNumbersEnabled(true);
301 301 //TODO: chart->axisY()->setNiceNumbersEnabled(true);
302 302 chart->setTitle("Scatter chart");
303 303 QString name("Series ");
304 304 int nameIndex = 0;
305 305 foreach (DataList list, m_dataTable) {
306 306 QScatterSeries *series = new QScatterSeries(chart);
307 307 foreach (Data data, list)
308 308 series->append(data.first);
309 309 series->setName(name + QString::number(nameIndex));
310 310 nameIndex++;
311 311 chart->addSeries(series);
312 312 }
313 313 chart->createDefaultAxes();
314 314 return chart;
315 315 }
316 316
317 317 void ThemeWidget::updateUI()
318 318 {
319 319 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
320 320
321 321 if (m_charts.at(0)->chart()->theme() != theme) {
322 322 foreach (QChartView *chartView, m_charts)
323 323 chartView->chart()->setTheme(theme);
324 324
325 325 QPalette pal = window()->palette();
326 326 if (theme == QChart::ChartThemeLight) {
327 327 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
328 328 pal.setColor(QPalette::WindowText, QRgb(0x404044));
329 329 } else if (theme == QChart::ChartThemeDark) {
330 330 pal.setColor(QPalette::Window, QRgb(0x121218));
331 331 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
332 332 } else if (theme == QChart::ChartThemeBlueCerulean) {
333 333 pal.setColor(QPalette::Window, QRgb(0x40434a));
334 334 pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
335 335 } else if (theme == QChart::ChartThemeBrownSand) {
336 336 pal.setColor(QPalette::Window, QRgb(0x9e8965));
337 337 pal.setColor(QPalette::WindowText, QRgb(0x404044));
338 338 } else if (theme == QChart::ChartThemeBlueNcs) {
339 339 pal.setColor(QPalette::Window, QRgb(0x018bba));
340 340 pal.setColor(QPalette::WindowText, QRgb(0x404044));
341 341 } else if (theme == QChart::ChartThemeHighContrast) {
342 342 pal.setColor(QPalette::Window, QRgb(0xffab03));
343 343 pal.setColor(QPalette::WindowText, QRgb(0x181818));
344 344 } else if (theme == QChart::ChartThemeBlueIcy) {
345 345 pal.setColor(QPalette::Window, QRgb(0xcee7f0));
346 346 pal.setColor(QPalette::WindowText, QRgb(0x404044));
347 347 } else {
348 348 pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
349 349 pal.setColor(QPalette::WindowText, QRgb(0x404044));
350 350 }
351 351 window()->setPalette(pal);
352 352 }
353 353
354 354 bool checked = m_antialiasCheckBox->isChecked();
355 355 foreach (QChartView *chart, m_charts)
356 356 chart->setRenderHint(QPainter::Antialiasing, checked);
357 357
358 358 QChart::AnimationOptions options(m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt());
359 359 if (m_charts.at(0)->chart()->animationOptions() != options) {
360 360 foreach (QChartView *chartView, m_charts)
361 361 chartView->chart()->setAnimationOptions(options);
362 362 }
363 363
364 364 Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt());
365 365
366 366 if (!alignment) {
367 367 foreach (QChartView *chartView, m_charts) {
368 368 chartView->chart()->legend()->hide();
369 369 }
370 370 } else {
371 371 foreach (QChartView *chartView, m_charts) {
372 372 chartView->chart()->legend()->setAlignment(alignment);
373 373 chartView->chart()->legend()->show();
374 374 }
375 375 }
376 376 }
377 377
@@ -1,92 +1,92
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QBarSeries>
25 25 #include <QBarSet>
26 26 #include <QLegend>
27 #include <QCategoriesAxis>
27 #include <QBarCategoriesAxis>
28 28
29 29 QTCOMMERCIALCHART_USE_NAMESPACE
30 30
31 31 int main(int argc, char *argv[])
32 32 {
33 33 QApplication a(argc, argv);
34 34
35 35 //![1]
36 36 QBarSet *set0 = new QBarSet("Jane");
37 37 QBarSet *set1 = new QBarSet("John");
38 38 QBarSet *set2 = new QBarSet("Axel");
39 39 QBarSet *set3 = new QBarSet("Mary");
40 40 QBarSet *set4 = new QBarSet("Samantha");
41 41
42 42 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
43 43 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
44 44 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
45 45 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
46 46 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
47 47 //![1]
48 48
49 49 //![2]
50 50 QBarSeries* series = new QBarSeries();
51 51 series->append(set0);
52 52 series->append(set1);
53 53 series->append(set2);
54 54 series->append(set3);
55 55 series->append(set4);
56 56
57 57 //![2]
58 58
59 59 //![3]
60 60 QChart* chart = new QChart();
61 61 chart->addSeries(series);
62 62 chart->setTitle("Barchart example");
63 63 chart->createDefaultAxes();
64 64 //![3]
65 65
66 66 //![4]
67 67 QStringList categories;
68 68 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
69 69 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
70 70 axis->append(categories);
71 71 chart->setAxisX(axis,series);
72 72 //![4]
73 73
74 74 //![5]
75 75 chart->legend()->setVisible(true);
76 76 chart->legend()->setAlignment(Qt::AlignBottom);
77 77 //![5]
78 78
79 79 //![6]
80 80 QChartView* chartView = new QChartView(chart);
81 81 chartView->setRenderHint(QPainter::Antialiasing);
82 82 //![6]
83 83
84 84 //![7]
85 85 QMainWindow window;
86 86 window.setCentralWidget(chartView);
87 87 window.resize(400, 300);
88 88 window.show();
89 89 //![7]
90 90
91 91 return a.exec();
92 92 }
@@ -1,112 +1,112
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "tablewidget.h"
22 22 #include "customtablemodel.h"
23 23 #include <QGridLayout>
24 24 #include <QTableView>
25 25 #include <QChart>
26 26 #include <QChartView>
27 27 #include <QLineSeries>
28 28 #include <QVXYModelMapper>
29 29 #include <QBarSeries>
30 30 #include <QBarSet>
31 31 #include <QVBarModelMapper>
32 32 #include <QHeaderView>
33 #include <QCategoriesAxis>
33 #include <QBarCategoriesAxis>
34 34
35 35 QTCOMMERCIALCHART_USE_NAMESPACE
36 36
37 37 TableWidget::TableWidget(QWidget *parent)
38 38 : QWidget(parent)
39 39 {
40 40 // create simple model for storing data
41 41 // user's table data model
42 42 //! [1]
43 43 CustomTableModel *model = new CustomTableModel;
44 44 //! [1]
45 45
46 46 //! [2]
47 47 // create table view and add model to it
48 48 QTableView *tableView = new QTableView;
49 49 tableView->setModel(model);
50 50 tableView->setMinimumWidth(300);
51 51 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
52 52 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
53 53 //! [2]
54 54
55 55 //! [3]
56 56 QChart *chart = new QChart;
57 57 chart->setAnimationOptions(QChart::AllAnimations);
58 58 //! [3]
59 59
60 60 // series 1
61 61 //! [4]
62 62 QBarSeries *series = new QBarSeries;
63 63
64 64 int first = 3;
65 65 int count = 5;
66 66 QVBarModelMapper *mapper = new QVBarModelMapper(this);
67 67 mapper->setFirstBarSetColumn(1);
68 68 mapper->setLastBarSetColumn(4);
69 69 mapper->setFirstRow(first);
70 70 mapper->setRowCount(count);
71 71 mapper->setSeries(series);
72 72 mapper->setModel(model);
73 73 chart->addSeries(series);
74 74 //! [4]
75 75
76 76 //! [5]
77 77 // for storing color hex from the series
78 78 QString seriesColorHex = "#000000";
79 79
80 80 // get the color of the series and use it for showing the mapped area
81 81 QList<QBarSet*> barsets = series->barSets();
82 82 for (int i = 0; i < barsets.count(); i++) {
83 83 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
84 84 model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count()));
85 85 }
86 86 //! [5]
87 87
88 88 //! [6]
89 89 QStringList categories;
90 90 categories << "April" << "May" << "June" << "July" << "August";
91 91 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
92 92 axis->append(categories);
93 93 chart->createDefaultAxes();
94 94 chart->setAxisX(axis, series);
95 95 //! [6]
96 96
97 97 //! [7]
98 98 QChartView *chartView = new QChartView(chart);
99 99 chartView->setRenderHint(QPainter::Antialiasing);
100 100 chartView->setMinimumSize(640, 480);
101 101 //! [7]
102 102
103 103 //! [8]
104 104 // create main layout
105 105 QGridLayout* mainLayout = new QGridLayout;
106 106 mainLayout->addWidget(tableView, 1, 0);
107 107 mainLayout->addWidget(chartView, 1, 1);
108 108 mainLayout->setColumnStretch(1, 1);
109 109 mainLayout->setColumnStretch(0, 0);
110 110 setLayout(mainLayout);
111 111 //! [8]
112 112 }
@@ -1,92 +1,92
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QPercentBarSeries>
25 25 #include <QBarSet>
26 26 #include <QLegend>
27 #include <QCategoriesAxis>
27 #include <QBarCategoriesAxis>
28 28
29 29 QTCOMMERCIALCHART_USE_NAMESPACE
30 30
31 31 int main(int argc, char *argv[])
32 32 {
33 33 QApplication a(argc, argv);
34 34
35 35 //![1]
36 36 QBarSet *set0 = new QBarSet("Jane");
37 37 QBarSet *set1 = new QBarSet("John");
38 38 QBarSet *set2 = new QBarSet("Axel");
39 39 QBarSet *set3 = new QBarSet("Mary");
40 40 QBarSet *set4 = new QBarSet("Samantha");
41 41
42 42 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
43 43 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
44 44 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
45 45 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
46 46 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
47 47 //![1]
48 48
49 49 //![2]
50 50 QPercentBarSeries* series = new QPercentBarSeries();
51 51 series->append(set0);
52 52 series->append(set1);
53 53 series->append(set2);
54 54 series->append(set3);
55 55 series->append(set4);
56 56 //![2]
57 57
58 58 //![3]
59 59 QChart* chart = new QChart();
60 60 chart->addSeries(series);
61 61 chart->setTitle("Percentbarchart example");
62 62 //![3]
63 63
64 64 //![4]
65 65 QStringList categories;
66 66 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
67 67 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
68 68 axis->append(categories);
69 69 chart->createDefaultAxes();
70 70 chart->setAxisX(axis,series);
71 71 //![4]
72 72
73 73 //![5]
74 74 chart->legend()->setVisible(true);
75 75 chart->legend()->setAlignment(Qt::AlignBottom);
76 76 //![5]
77 77
78 78 //![6]
79 79 QChartView* chartView = new QChartView(chart);
80 80 chartView->setRenderHint(QPainter::Antialiasing);
81 81 //![6]
82 82
83 83 //![7]
84 84 QMainWindow window;
85 85 window.setCentralWidget(chartView);
86 86 window.resize(400, 300);
87 87 window.show();
88 88 //![7]
89 89
90 90 return a.exec();
91 91 }
92 92
@@ -1,92 +1,92
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 <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QStackedBarSeries>
25 25 #include <QBarSet>
26 26 #include <QLegend>
27 #include <QCategoriesAxis>
27 #include <QBarCategoriesAxis>
28 28
29 29 QTCOMMERCIALCHART_USE_NAMESPACE
30 30
31 31 int main(int argc, char *argv[])
32 32 {
33 33 QApplication a(argc, argv);
34 34
35 35 //![1]
36 36 QBarSet *set0 = new QBarSet("Jane");
37 37 QBarSet *set1 = new QBarSet("John");
38 38 QBarSet *set2 = new QBarSet("Axel");
39 39 QBarSet *set3 = new QBarSet("Mary");
40 40 QBarSet *set4 = new QBarSet("Samantha");
41 41
42 42 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
43 43 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
44 44 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
45 45 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
46 46 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
47 47 //![1]
48 48
49 49 //![2]
50 50 QStackedBarSeries* series = new QStackedBarSeries();
51 51 series->append(set0);
52 52 series->append(set1);
53 53 series->append(set2);
54 54 series->append(set3);
55 55 series->append(set4);
56 56 //![2]
57 57
58 58 //![3]
59 59 QChart* chart = new QChart();
60 60 chart->addSeries(series);
61 61 chart->setTitle("Stackedbarchart example");
62 62 chart->createDefaultAxes();
63 63 //![3]
64 64
65 65 //![4]
66 66 QStringList categories;
67 67 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
68 68 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
69 69 axis->append(categories);
70 70 chart->setAxisX(axis,series);
71 71 //![4]
72 72
73 73 //![5]
74 74 chart->legend()->setVisible(true);
75 75 chart->legend()->setAlignment(Qt::AlignBottom);
76 76 //![5]
77 77
78 78 //![6]
79 79 QChartView* chartView = new QChartView(chart);
80 80 chartView->setRenderHint(QPainter::Antialiasing);
81 81 //![6]
82 82
83 83 //![7]
84 84 QMainWindow window;
85 85 window.setCentralWidget(chartView);
86 86 window.resize(400, 300);
87 87 window.show();
88 88 //![7]
89 89
90 90 return a.exec();
91 91 }
92 92
@@ -1,59 +1,59
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 "drilldownchart.h"
22 #include <QCategoriesAxis>
22 #include <QBarCategoriesAxis>
23 23
24 24 QTCOMMERCIALCHART_USE_NAMESPACE
25 25
26 26 DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 27 : QChart(parent, wFlags)
28 28 ,m_currentSeries(0)
29 29 {
30 30 }
31 31
32 32 void DrilldownChart::changeSeries(DrilldownBarSeries *series)
33 33 {
34 34 if (m_currentSeries) {
35 35 removeSeries(m_currentSeries);
36 36 }
37 37
38 38 m_currentSeries = series;
39 39
40 40 // Reset axis
41 41 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
42 42 axis->append(m_currentSeries->categories());
43 43
44 44 addSeries(series);
45 45
46 46 createDefaultAxes();
47 47 setAxisX(axis,series);
48 48
49 49 setTitle(series->name());
50 50 }
51 51
52 52 void DrilldownChart::handleClicked(int index, QBarSet *barset)
53 53 {
54 54 Q_UNUSED(barset)
55 55 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
56 56 changeSeries(series->drilldownSeries(index));
57 57 }
58 58
59 59 #include "moc_drilldownchart.cpp"
@@ -1,516 +1,516
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 "declarativechart.h"
22 22 #include <QPainter>
23 23 #include "declarativelineseries.h"
24 24 #include "declarativeareaseries.h"
25 25 #include "declarativebarseries.h"
26 26 #include "declarativepieseries.h"
27 27 #include "declarativesplineseries.h"
28 28 #include "declarativescatterseries.h"
29 #include "qcategoriesaxis.h"
29 #include "qbarcategoriesaxis.h"
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 /*!
34 34 \qmlclass ChartView DeclarativeChart
35 35
36 36 ChartView element is the parent that is responsible for showing different chart series types.
37 37
38 38 The following QML shows how to create a simple chart with one pie series:
39 39 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 1
40 40 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 2
41 41 \snippet ../examples/qmlpiechart/qml/qmlpiechart/main.qml 3
42 42
43 43 \beginfloatleft
44 44 \image examples_qmlpiechart.png
45 45 \endfloat
46 46 \clearfloat
47 47 */
48 48
49 49 /*!
50 50 \qmlproperty Theme ChartView::theme
51 51 Theme defines the visual appearance of the chart, including for example colors, fonts, line
52 52 widths and chart background.
53 53 */
54 54
55 55 /*!
56 56 \qmlproperty Animation ChartView::animation
57 57 Animation configuration of the chart. One of ChartView.NoAnimation, ChartView.GridAxisAnimations,
58 58 ChartView.SeriesAnimations or ChartView.AllAnimations.
59 59 */
60 60
61 61 /*!
62 62 \qmlproperty Font ChartView::titleFont
63 63 The title font of the chart
64 64
65 65 See the \l {Font} {QML Font Element} for detailed documentation.
66 66 */
67 67
68 68 /*!
69 69 \qmlproperty string ChartView::title
70 70 The title of the chart, shown on top of the chart.
71 71 \sa ChartView::titleColor
72 72 */
73 73
74 74 /*!
75 75 \qmlproperty string ChartView::titleColor
76 76 The color of the title text.
77 77 */
78 78
79 79 /*!
80 80 \qmlproperty Axis ChartView::axisX
81 81 The x-axis of the chart.
82 82 */
83 83
84 84 /*!
85 85 \qmlproperty Axis ChartView::axisY
86 86 The default y-axis of the chart.
87 87 */
88 88
89 89 /*!
90 90 \qmlproperty Legend ChartView::legend
91 91 The legend of the chart. Legend lists all the series, pie slices and bar sets added on the chart.
92 92 */
93 93
94 94 /*!
95 95 \qmlproperty int ChartView::count
96 96 The count of series added to the chart.
97 97 */
98 98
99 99 /*!
100 100 \qmlproperty color ChartView::backgroundColor
101 101 The color of the chart's background. By default background color is defined by chart theme.
102 102 \sa ChartView::theme
103 103 */
104 104
105 105 /*!
106 106 \qmlproperty bool ChartView::dropShadowEnabled
107 107 The chart's border drop shadow. Set to true to enable drop shadow.
108 108 */
109 109
110 110 /*!
111 111 \qmlproperty real ChartView::topMargin
112 112 The space between the top of chart view and the top of the plot area. The title (if non-empty) is drawn on top margin
113 113 area of the chart view. Top margin area is also used by legend, if aligned to top.
114 114 */
115 115
116 116 /*!
117 117 \qmlproperty real ChartView::bottomMargin
118 118 The space between the bottom of chart view and the bottom of the plot area. The bottom margin area may be used by
119 119 legend (if aligned to bottom), x-axis, x-axis labels and x-axis tick marks.
120 120 */
121 121
122 122 /*!
123 123 \qmlproperty real ChartView::leftMargin
124 124 The space between the left side of chart view and the left side of the plot area. The left margin area may be used by
125 125 legend (if aligned to left), y-axis, y-axis labels and y-axis tick marks.
126 126 */
127 127
128 128 /*!
129 129 \qmlproperty real ChartView::rightMargin
130 130 The space between the right side of chart view and the right side of the plot area. The right margin area may be used
131 131 by legend (if aligned to right).
132 132 */
133 133
134 134 /*!
135 135 \qmlmethod AbstractSeries ChartView::series(int index)
136 136 Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with
137 137 the count property of the chart.
138 138 */
139 139
140 140 /*!
141 141 \qmlmethod AbstractSeries ChartView::series(string name)
142 142 Returns the first series on the chart with \a name. If there is no series with that name, returns null.
143 143 */
144 144
145 145 /*!
146 146 \qmlmethod AbstractSeries ChartView::createSeries(SeriesType type, string name)
147 147 Creates a series object of \a type to the chart. For example:
148 148 \code
149 149 var scatter = chartView.createSeries(ChartView.SeriesTypeScatter, "scatter series");
150 150 scatter.markerSize = 22;
151 151 scatter.append(1.1, 2.0);
152 152 \endcode
153 153 */
154 154
155 155 /*!
156 156 \qmlmethod Axis ChartView::axisY(QAbstractSeries *series)
157 157 The y-axis of the series. This is the same as the default y-axis of the chart as multiple y-axes are not yet supported.
158 158 */
159 159
160 160 /*!
161 161 \qmlmethod ChartView::zoomY(real factor)
162 162 Zooms in by \a factor on the center of the chart.
163 163 */
164 164
165 165 /*!
166 166 \qmlmethod ChartView::scrollLeft(real pixels)
167 167 Scrolls to left by \a pixels. This is a convenience function that suits for example for key navigation.
168 168 \sa Axis::min, Axis::max
169 169 */
170 170
171 171 /*!
172 172 \qmlmethod ChartView::scrollRight(real pixels)
173 173 Scrolls to right by \a pixels. This is a convenience function that suits for example for key navigation.
174 174 \sa Axis::min, Axis::max
175 175 */
176 176
177 177 /*!
178 178 \qmlmethod ChartView::scrollUp(real pixels)
179 179 Scrolls up by \a pixels. This is a convenience function that suits for example for key navigation.
180 180 \sa Axis::min, Axis::max
181 181 */
182 182
183 183 /*!
184 184 \qmlmethod ChartView::scrollDown(real pixels)
185 185 Scrolls down by \a pixels. This is a convenience function that suits for example for key navigation.
186 186 \sa Axis::min, Axis::max
187 187 */
188 188
189 189 /*!
190 190 \qmlsignal ChartView::onTopMarginChanged(real margin)
191 191 The top margin of the chart view has changed to \a margin. This may happen for example if you modify font size
192 192 related properties of the legend or chart title.
193 193 */
194 194
195 195 /*!
196 196 \qmlsignal ChartView::onBottomMarginChanged(real margin)
197 197 The bottom margin of the chart view has changed to \a margin. This may happen for example if you modify font size
198 198 related properties of the legend or chart title.
199 199 */
200 200
201 201 /*!
202 202 \qmlsignal ChartView::onLeftMarginChanged(real margin)
203 203 The left margin of the chart view has changed to \a margin. This may happen for example if you modify font size
204 204 related properties of the legend or chart title.
205 205 */
206 206
207 207 /*!
208 208 \qmlsignal ChartView::onRightMarginChanged(real margin)
209 209 The right margin of the chart view has changed to \a margin. This may happen for example if you modify font size
210 210 related properties of the legend or chart title.
211 211 */
212 212
213 213 DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent)
214 214 : QDeclarativeItem(parent),
215 215 m_chart(new QChart(this))
216 216 {
217 217 setFlag(QGraphicsItem::ItemHasNoContents, false);
218 218 // m_chart->axisX()->setNiceNumbersEnabled(false);
219 219 m_chartMargins = m_chart->margins();
220 220 connect(m_chart, SIGNAL(marginsChanged(QRectF)), this, SLOT(handleMarginsChanged(QRectF)));
221 221 }
222 222
223 223 void DeclarativeChart::handleMarginsChanged(QRectF newMargins)
224 224 {
225 225 if (m_chartMargins.top() != newMargins.top())
226 226 topMarginChanged(m_chart->margins().top());
227 227 if (m_chartMargins.bottom() != newMargins.bottom())
228 228 bottomMarginChanged(m_chart->margins().bottom());
229 229 if (m_chartMargins.left() != newMargins.left())
230 230 leftMarginChanged(m_chart->margins().left());
231 231 if (m_chartMargins.right() != newMargins.right())
232 232 rightMarginChanged(m_chart->margins().right());
233 233
234 234 m_chartMargins = m_chart->margins();
235 235 }
236 236
237 237 DeclarativeChart::~DeclarativeChart()
238 238 {
239 239 delete m_chart;
240 240 }
241 241
242 242 void DeclarativeChart::childEvent(QChildEvent *event)
243 243 {
244 244 if (event->type() == QEvent::ChildAdded) {
245 245 if (qobject_cast<QAbstractSeries *>(event->child())) {
246 246 m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child()));
247 247 }
248 248 }
249 249 }
250 250
251 251 void DeclarativeChart::componentComplete()
252 252 {
253 253 foreach(QObject *child, children()) {
254 254 if (qobject_cast<QAbstractSeries *>(child)) {
255 255 // qDebug() << "DeclarativeChart::componentComplete(), add: " << child;
256 256 // TODO: how about optional y-axis?
257 257 m_chart->addSeries(qobject_cast<QAbstractSeries *>(child));
258 258 }else if(qobject_cast<QAbstractAxis *>(child)){
259 259
260 260 }
261 261 }
262 262 QDeclarativeItem::componentComplete();
263 263 }
264 264
265 265 void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
266 266 {
267 267 // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height();
268 268 if (newGeometry.isValid()) {
269 269 if (newGeometry.width() > 0 && newGeometry.height() > 0) {
270 270 m_chart->resize(newGeometry.width(), newGeometry.height());
271 271 }
272 272 }
273 273 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
274 274 }
275 275
276 276 void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
277 277 {
278 278 Q_UNUSED(option)
279 279 Q_UNUSED(widget)
280 280
281 281 // TODO: optimized?
282 282 painter->setRenderHint(QPainter::Antialiasing, true);
283 283 }
284 284
285 285 void DeclarativeChart::setTheme(DeclarativeChart::Theme theme)
286 286 {
287 287 QChart::ChartTheme chartTheme = (QChart::ChartTheme) theme;
288 288 if (chartTheme != m_chart->theme())
289 289 m_chart->setTheme(chartTheme);
290 290 }
291 291
292 292 DeclarativeChart::Theme DeclarativeChart::theme()
293 293 {
294 294 return (DeclarativeChart::Theme) m_chart->theme();
295 295 }
296 296
297 297 void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations)
298 298 {
299 299 QChart::AnimationOption animationOptions = (QChart::AnimationOption) animations;
300 300 if (animationOptions != m_chart->animationOptions())
301 301 m_chart->setAnimationOptions(animationOptions);
302 302 }
303 303
304 304 DeclarativeChart::Animation DeclarativeChart::animationOptions()
305 305 {
306 306 if (m_chart->animationOptions().testFlag(QChart::AllAnimations))
307 307 return DeclarativeChart::AllAnimations;
308 308 else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations))
309 309 return DeclarativeChart::GridAxisAnimations;
310 310 else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations))
311 311 return DeclarativeChart::SeriesAnimations;
312 312 else
313 313 return DeclarativeChart::NoAnimation;
314 314 }
315 315
316 316 void DeclarativeChart::setTitle(QString title)
317 317 {
318 318 if (title != m_chart->title())
319 319 m_chart->setTitle(title);
320 320 }
321 321 QString DeclarativeChart::title()
322 322 {
323 323 return m_chart->title();
324 324 }
325 325
326 326 QAbstractAxis *DeclarativeChart::axisX(QAbstractSeries *series)
327 327 {
328 328 return m_chart->axisX(series);
329 329 }
330 330
331 331 QAbstractAxis *DeclarativeChart::axisY(QAbstractSeries *series)
332 332 {
333 333 return m_chart->axisY(series);
334 334 }
335 335
336 336 QLegend *DeclarativeChart::legend()
337 337 {
338 338 return m_chart->legend();
339 339 }
340 340
341 341 void DeclarativeChart::setTitleColor(QColor color)
342 342 {
343 343 QBrush b = m_chart->titleBrush();
344 344 if (color != b.color()) {
345 345 b.setColor(color);
346 346 m_chart->setTitleBrush(b);
347 347 emit titleColorChanged(color);
348 348 }
349 349 }
350 350
351 351 QFont DeclarativeChart::titleFont() const
352 352 {
353 353 return m_chart->titleFont();
354 354 }
355 355
356 356 void DeclarativeChart::setTitleFont(const QFont& font)
357 357 {
358 358 m_chart->setTitleFont(font);
359 359 }
360 360
361 361 QColor DeclarativeChart::titleColor()
362 362 {
363 363 return m_chart->titleBrush().color();
364 364 }
365 365
366 366 void DeclarativeChart::setBackgroundColor(QColor color)
367 367 {
368 368 QBrush b = m_chart->backgroundBrush();
369 369 if (b.style() != Qt::SolidPattern || color != b.color()) {
370 370 b.setStyle(Qt::SolidPattern);
371 371 b.setColor(color);
372 372 m_chart->setBackgroundBrush(b);
373 373 emit backgroundColorChanged();
374 374 }
375 375 }
376 376
377 377 QColor DeclarativeChart::backgroundColor()
378 378 {
379 379 return m_chart->backgroundBrush().color();
380 380 }
381 381
382 382 int DeclarativeChart::count()
383 383 {
384 384 return m_chart->series().count();
385 385 }
386 386
387 387 void DeclarativeChart::setDropShadowEnabled(bool enabled)
388 388 {
389 389 if (enabled != m_chart->isDropShadowEnabled()) {
390 390 m_chart->setDropShadowEnabled(enabled);
391 391 dropShadowEnabledChanged(enabled);
392 392 }
393 393 }
394 394
395 395 bool DeclarativeChart::dropShadowEnabled()
396 396 {
397 397 return m_chart->isDropShadowEnabled();
398 398 }
399 399
400 400 qreal DeclarativeChart::topMargin()
401 401 {
402 402 return m_chart->margins().top();
403 403 }
404 404
405 405 qreal DeclarativeChart::bottomMargin()
406 406 {
407 407 return m_chart->margins().bottom();
408 408 }
409 409
410 410 qreal DeclarativeChart::leftMargin()
411 411 {
412 412 return m_chart->margins().left();
413 413 }
414 414
415 415 qreal DeclarativeChart::rightMargin()
416 416 {
417 417 return m_chart->margins().right();
418 418 }
419 419
420 420 void DeclarativeChart::zoom(qreal factor)
421 421 {
422 422 m_chart->zoom(factor);
423 423 }
424 424
425 425 void DeclarativeChart::scrollLeft(qreal pixels)
426 426 {
427 427 m_chart->scroll(pixels, 0);
428 428 }
429 429
430 430 void DeclarativeChart::scrollRight(qreal pixels)
431 431 {
432 432 m_chart->scroll(-pixels, 0);
433 433 }
434 434
435 435 void DeclarativeChart::scrollUp(qreal pixels)
436 436 {
437 437 m_chart->scroll(0, pixels);
438 438 }
439 439
440 440 void DeclarativeChart::scrollDown(qreal pixels)
441 441 {
442 442 m_chart->scroll(0, -pixels);
443 443 }
444 444
445 445 QAbstractSeries *DeclarativeChart::series(int index)
446 446 {
447 447 if (index < m_chart->series().count()) {
448 448 return m_chart->series().at(index);
449 449 }
450 450 return 0;
451 451 }
452 452
453 453 QAbstractSeries *DeclarativeChart::series(QString seriesName)
454 454 {
455 455 foreach(QAbstractSeries *series, m_chart->series()) {
456 456 if (series->name() == seriesName)
457 457 return series;
458 458 }
459 459 return 0;
460 460 }
461 461
462 462 QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name)
463 463 {
464 464 QAbstractSeries *series = 0;
465 465 switch (type) {
466 466 case DeclarativeChart::SeriesTypeLine:
467 467 series = new DeclarativeLineSeries();
468 468 break;
469 469 case DeclarativeChart::SeriesTypeArea:
470 470 series = new DeclarativeAreaSeries();
471 471 break;
472 472 case DeclarativeChart::SeriesTypeStackedBar:
473 473 // TODO
474 474 break;
475 475 case DeclarativeChart::SeriesTypePercentBar:
476 476 // TODO
477 477 break;
478 478 case DeclarativeChart::SeriesTypeBar:
479 479 series = new DeclarativeBarSeries();
480 480 break;
481 481 case DeclarativeChart::SeriesTypePie:
482 482 series = new DeclarativePieSeries();
483 483 break;
484 484 case DeclarativeChart::SeriesTypeScatter:
485 485 series = new DeclarativeScatterSeries();
486 486 break;
487 487 case DeclarativeChart::SeriesTypeSpline:
488 488 series = new DeclarativeSplineSeries();
489 489 break;
490 490 default:
491 491 qWarning() << "Illegal series type";
492 492 }
493 493 series->setName(name);
494 494 m_chart->addSeries(series);
495 495 m_chart->createDefaultAxes();
496 496 return series;
497 497 }
498 498
499 499 void DeclarativeChart::setAxisX(QAbstractAxis* axis, QAbstractSeries *series)
500 500 {
501 501 m_chart->setAxisX(axis,series);
502 502 }
503 503
504 504 void DeclarativeChart::setAxisY(QAbstractAxis* axis, QAbstractSeries *series)
505 505 {
506 506 m_chart->setAxisY(axis,series);
507 507 }
508 508
509 509 void DeclarativeChart::createDefaultAxes()
510 510 {
511 511 m_chart->createDefaultAxes();
512 512 }
513 513
514 514 #include "moc_declarativechart.cpp"
515 515
516 516 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,104 +1,104
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 <QtDeclarative/qdeclarativeextensionplugin.h>
22 22 #include <QtDeclarative/qdeclarative.h>
23 23 #include "qchart.h"
24 24 #include "qabstractaxis.h"
25 25 #include "qvaluesaxis.h"
26 #include "qcategoriesaxis.h"
26 #include "qbarcategoriesaxis.h"
27 27 #include "declarativechart.h"
28 28 #include "declarativexypoint.h"
29 29 #include "declarativelineseries.h"
30 30 #include "declarativesplineseries.h"
31 31 #include "declarativeareaseries.h"
32 32 #include "declarativescatterseries.h"
33 33 #include "declarativebarseries.h"
34 34 #include "declarativepieseries.h"
35 35 #include <QVXYModelMapper>
36 36 #include <QHXYModelMapper>
37 37 #include <QHPieModelMapper>
38 38 #include <QVPieModelMapper>
39 39 #include <QHBarModelMapper>
40 40 #include <QVBarModelMapper>
41 41
42 42 QTCOMMERCIALCHART_BEGIN_NAMESPACE
43 43
44 44 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
45 45 {
46 46 Q_OBJECT
47 47 public:
48 48 virtual void registerTypes(const char *uri)
49 49 {
50 50 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
51 51
52 52 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView");
53 53 qmlRegisterType<DeclarativeXYPoint>(uri, 1, 0, "XYPoint");
54 54 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
55 55 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
56 56 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
57 57 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
58 58 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
59 59 qmlRegisterType<DeclarativeStackedBarSeries>(uri, 1, 0, "StackedBarSeries");
60 60 qmlRegisterType<DeclarativePercentBarSeries>(uri, 1, 0, "PercentBarSeries");
61 61 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
62 62 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
63 63 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
64 64 qmlRegisterType<QHXYModelMapper>(uri, 1, 0, "HXYModelMapper");
65 65 qmlRegisterType<QVXYModelMapper>(uri, 1, 0, "VXYModelMapper");
66 66 qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper");
67 67 qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper");
68 68 qmlRegisterType<QHBarModelMapper>(uri, 1, 0, "HBarModelMapper");
69 69 qmlRegisterType<QVBarModelMapper>(uri, 1, 0, "VBarModelMapper");
70 70 qmlRegisterType<QValuesAxis>(uri, 1, 0, "ValuesAxis");
71 71 qmlRegisterType<QBarCategoriesAxis>(uri, 1, 0, "BarCategoriesAxis");
72 72
73 73 qmlRegisterUncreatableType<QLegend>(uri, 1, 0, "Legend",
74 74 QLatin1String("Trying to create uncreatable: Legend."));
75 75 qmlRegisterUncreatableType<QXYSeries>(uri, 1, 0, "XYSeries",
76 76 QLatin1String("Trying to create uncreatable: XYSeries."));
77 77 qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel",
78 78 QLatin1String("Trying to create uncreatable: AbstractItemModel."));
79 79 qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper",
80 80 QLatin1String("Trying to create uncreatable: XYModelMapper."));
81 81 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
82 82 QLatin1String("Trying to create uncreatable: PieModelMapper."));
83 83 qmlRegisterUncreatableType<QBarModelMapper>(uri, 1, 0, "BarModelMapper",
84 84 QLatin1String("Trying to create uncreatable: BarModelMapper."));
85 85 qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries",
86 86 QLatin1String("Trying to create uncreatable: AbstractSeries."));
87 87 qmlRegisterUncreatableType<QAbstractBarSeries>(uri, 1, 0, "AbstractBarSeries",
88 88 QLatin1String("Trying to create uncreatable: AbstractBarSeries."));
89 89 qmlRegisterUncreatableType<QAbstractAxis>(uri, 1, 0, "AbstractAxis",
90 90 QLatin1String("Trying to create uncreatable: AbstractAxis."));
91 91 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
92 92 QLatin1String("Trying to create uncreatable: PieModelMapper."));
93 93 qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper",
94 94 QLatin1String("Trying to create uncreatable: XYModelMapper."));
95 95 }
96 96 };
97 97
98 98 #include "plugin.moc"
99 99
100 100 QTCOMMERCIALCHART_END_NAMESPACE
101 101
102 102 QTCOMMERCIALCHART_USE_NAMESPACE
103 103
104 104 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,31 +1,30
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/chartaxis.cpp \
6 # $$PWD/chartaxisx.cpp \
7 # $$PWD/chartaxisy.cpp \
8 6 $$PWD/chartvaluesaxisx.cpp \
9 7 $$PWD/chartvaluesaxisy.cpp \
10 8 $$PWD/chartcategoriesaxisx.cpp \
11 9 $$PWD/chartcategoriesaxisy.cpp \
10 $$PWD/qbarcategoriesaxis.cpp \
12 11 $$PWD/qcategoriesaxis.cpp \
13 12 $$PWD/qvaluesaxis.cpp \
14 13 $$PWD/qabstractaxis.cpp
15 14
16 15 PRIVATE_HEADERS += \
17 16 $$PWD/chartaxis_p.h \
18 # $$PWD/chartaxisx_p.h \
19 # $$PWD/chartaxisy_p.h \
20 17 $$PWD/chartvaluesaxisx_p.h \
21 18 $$PWD/chartvaluesaxisy_p.h \
22 19 $$PWD/chartcategoriesaxisx_p.h \
23 20 $$PWD/chartcategoriesaxisy_p.h \
21 $$PWD/qbarcategoriesaxis_p.h \
24 22 $$PWD/qcategoriesaxis_p.h \
25 23 $$PWD/qvaluesaxis_p.h \
26 24 $$PWD/qabstractaxis_p.h
27 25
28 26 PUBLIC_HEADERS += \
27 $$PWD/qbarcategoriesaxis.h \
29 28 $$PWD/qcategoriesaxis.h \
30 29 $$PWD/qvaluesaxis.h \
31 30 $$PWD/qabstractaxis.h
@@ -1,106 +1,106
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 "chartcategoriesaxisx_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "chartanimator_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QDebug>
27 27 #include <QFontMetrics>
28 #include <QCategoriesAxis>
28 #include <QBarCategoriesAxis>
29 29
30 30 static int label_padding = 5;
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
35 35 m_categoriesAxis(axis)
36 36 {
37 37
38 38 }
39 39
40 40 ChartCategoriesAxisX::~ChartCategoriesAxisX()
41 41 {
42 42 }
43 43
44 44 QVector<qreal> ChartCategoriesAxisX::calculateLayout() const
45 45 {
46 46 Q_ASSERT(m_ticksCount>=2);
47 47
48 48 QVector<qreal> points;
49 49 points.resize(m_ticksCount);
50 50
51 51 // TODO: shift logic
52 52 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
53 53 for (int i = 0; i < m_ticksCount; ++i) {
54 54 int x = i * deltaX + m_rect.left();
55 55 points[i] = x;
56 56 }
57 57 return points;
58 58 }
59 59
60 60 void ChartCategoriesAxisX::updateGeometry()
61 61 {
62 62 const QVector<qreal>& layout = ChartAxis::layout();
63 63
64 64 m_minWidth = 0;
65 65 m_minHeight = 0;
66 66
67 67 if(layout.isEmpty()) return;
68 68
69 69 QStringList ticksList;
70 70
71 71 createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories());
72 72
73 73 QList<QGraphicsItem *> lines = m_grid->childItems();
74 74 QList<QGraphicsItem *> labels = m_labels->childItems();
75 75 QList<QGraphicsItem *> shades = m_shades->childItems();
76 76 QList<QGraphicsItem *> axis = m_axis->childItems();
77 77
78 78 Q_ASSERT(labels.size() == ticksList.size());
79 79 Q_ASSERT(layout.size() == ticksList.size());
80 80
81 81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
82 82 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
83 83
84 84 for (int i = 1; i < layout.size(); ++i) {
85 85 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
86 86 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
87 87 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i-1));
88 88
89 89 labelItem->setText(ticksList.at(i-1));
90 90 const QRectF& rect = labelItem->boundingRect();
91 91 QPointF center = rect.center();
92 92 labelItem->setTransformOriginPoint(center.x(), center.y());
93 93 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
94 94 m_minWidth+=rect.width();
95 95 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
96 96
97 97 if ((i+1)%2 && i>1) {
98 98 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
99 99 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
100 100 }
101 101 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
102 102 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
103 103 }
104 104 }
105 105
106 106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,104 +1,104
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 "chartcategoriesaxisy_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "chartanimator_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QDebug>
27 27 #include <QFontMetrics>
28 #include <QCategoriesAxis>
28 #include <QBarCategoriesAxis>
29 29
30 30 static int label_padding = 5;
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 ChartCategoriesAxisY::ChartCategoriesAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
35 35 {
36 36 }
37 37
38 38 ChartCategoriesAxisY::~ChartCategoriesAxisY()
39 39 {
40 40 }
41 41
42 42 QVector<qreal> ChartCategoriesAxisY::calculateLayout() const
43 43 {
44 44 Q_ASSERT(m_ticksCount>=2);
45 45
46 46 QVector<qreal> points;
47 47 points.resize(m_ticksCount);
48 48
49 49 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
50 50 for (int i = 0; i < m_ticksCount; ++i) {
51 51 int y = i * -deltaY + m_rect.bottom();
52 52 points[i] = y;
53 53 }
54 54
55 55 return points;
56 56 }
57 57
58 58 void ChartCategoriesAxisY::updateGeometry()
59 59 {
60 60 const QVector<qreal>& layout = ChartAxis::layout();
61 61
62 62 m_minWidth = 0;
63 63 m_minHeight = 0;
64 64
65 65 if(layout.isEmpty()) return;
66 66
67 67 QStringList ticksList;
68 68
69 69 createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories());
70 70
71 71 QList<QGraphicsItem *> lines = m_grid->childItems();
72 72 QList<QGraphicsItem *> labels = m_labels->childItems();
73 73 QList<QGraphicsItem *> shades = m_shades->childItems();
74 74 QList<QGraphicsItem *> axis = m_axis->childItems();
75 75
76 76 Q_ASSERT(labels.size() == ticksList.size());
77 77 Q_ASSERT(layout.size() == ticksList.size());
78 78
79 79 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
80 80 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
81 81
82 82 for (int i = 1; i < layout.size(); ++i) {
83 83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 84 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
85 85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 86
87 87 labelItem->setText(ticksList.at(i));
88 88 const QRectF& rect = labelItem->boundingRect();
89 89 QPointF center = rect.center();
90 90 labelItem->setTransformOriginPoint(center.x(), center.y());
91 91 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
92 92 m_minWidth+=rect.width();
93 93 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
94 94
95 95 if ((i+1)%2 && i>1) {
96 96 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
97 97 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
98 98 }
99 99 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
100 100 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
101 101 }
102 102 }
103 103
104 104 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,297 +1,149
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 "qcategoriesaxis.h"
22 22 #include "qcategoriesaxis_p.h"
23 23 #include "chartcategoriesaxisx_p.h"
24 24 #include "chartcategoriesaxisy_p.h"
25 25 #include <qmath.h>
26 26 #include <QDebug>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 QBarCategoriesAxis::QBarCategoriesAxis(QObject *parent):
31 QAbstractAxis(*new QBarCategoriesAxisPrivate(this),parent)
30 QCategoriesAxis::QCategoriesAxis(QObject *parent):
31 QValuesAxis(*new QCategoriesAxisPrivate(this),parent)
32 32 {
33 33 }
34 34
35 QBarCategoriesAxis::~QBarCategoriesAxis()
35 QCategoriesAxis::~QCategoriesAxis()
36 36 {
37 37 }
38 38
39 QBarCategoriesAxis::QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent):QAbstractAxis(d,parent)
39 QCategoriesAxis::QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent):QValuesAxis(d,parent)
40 40 {
41 41
42 42 }
43 43
44 44 /*!
45 45 Appends \a categories to axis
46 46 */
47 void QBarCategoriesAxis::append(const QStringList &categories)
48 {
49 Q_D(QBarCategoriesAxis);
50 if (d->m_categories.isEmpty()) {
51 d->m_categories.append(categories);
52 setRange(categories.first(),categories.last());
53 }else{
54 d->m_categories.append(categories);
47 void QCategoriesAxis::append(const QString& category, qreal x)
48 {
49 Q_D(QCategoriesAxis);
50 if (!d->m_categories.contains(category))
51 {
52 if(d->m_categories.isEmpty()){
53 Range range(d->m_categoryMinimum,x);
54 d->m_categoriesMap.insert(category,range);
55 d->m_categories.append(category);
56 }else{
57 Range range = d->m_categoriesMap.value(d->m_categories.last());
58 d->m_categoriesMap.insert(category,Range(range.first,x));
59 d->m_categories.append(category);
60 }
61 setRange(d->m_min,x);
55 62 }
56
57 emit categoriesChanged();
58 63 }
59 64
60 /*!
61 Appends \a category to axis
62 */
63 void QBarCategoriesAxis::append(const QString &category)
65 void QCategoriesAxis::setFisrtCategoryMinimum(qreal x)
64 66 {
65 Q_D(QBarCategoriesAxis);
66 if (d->m_categories.isEmpty()) {
67 d->m_categories.append(category);
68 setRange(category,category);
69 }else{
70 d->m_categories.append(category);
71 }
72 emit categoriesChanged();
67 Q_D(QCategoriesAxis);
68 if(d->m_categories.isEmpty()){
69 d->m_categoryMinimum=x;
70 }else{
71 Range range = d->m_categoriesMap.value(d->m_categories.first());
72 d->m_categoriesMap.insert(d->m_categories.first(),Range(x,range.second));
73 setRange(x,d->m_min);
74 }
73 75 }
74 76
75 77 /*!
76 78 Removes \a category from axis
77 79 */
78 void QBarCategoriesAxis::remove(const QString &category)
79 {
80 Q_D(QBarCategoriesAxis);
81 if (d->m_categories.contains(category)) {
82 d->m_categories.removeAt(d->m_categories.indexOf(category));
83 setRange(d->m_categories.first(),d->m_categories.last());
84 emit categoriesChanged();
85 }
86 }
87
88 /*!
89 Inserts \a category to axis at \a index
90 */
91 void QBarCategoriesAxis::insert(int index, const QString &category)
92 {
93 Q_D(QBarCategoriesAxis);
94 if (d->m_categories.isEmpty()) {
95 d->m_categories.insert(index,category);
96 setRange(category,category);
97 }else{
98
99 }
100 emit categoriesChanged();
101 }
102
103 /*!
104 Removes all categories.
105 */
106 void QBarCategoriesAxis::clear()
80 void QCategoriesAxis::remove(const QString &category)
107 81 {
108 Q_D(QBarCategoriesAxis);
109 d->m_categories.clear();
110 setRange(QString::null,QString::null);
111 emit categoriesChanged();
82 Q_UNUSED(category);
83 //TODO
112 84 }
113 85
114 void QBarCategoriesAxis::setCategories(const QStringList &categories)
86 QStringList QCategoriesAxis::categories()
115 87 {
116 Q_D(QBarCategoriesAxis);
117 if(d->m_categories!=categories){
118 d->m_categories = categories;
119 setRange(categories.first(),categories.last());
120 emit categoriesChanged();
121 }
122 }
123
124 QStringList QBarCategoriesAxis::categories()
125 {
126 Q_D(QBarCategoriesAxis);
88 Q_D(QCategoriesAxis);
127 89 return d->m_categories;
128 90 }
129 91
130 92 /*!
131 93 Returns number of categories.
132 94 */
133 int QBarCategoriesAxis::count() const
95 int QCategoriesAxis::count() const
134 96 {
135 Q_D(const QBarCategoriesAxis);
97 Q_D(const QCategoriesAxis);
136 98 return d->m_categories.count();
137 99 }
138 100
139 101 /*!
140 Returns category at \a index. Index must be valid.
141 */
142 QString QBarCategoriesAxis::at(int index) const
143 {
144 Q_D(const QBarCategoriesAxis);
145 return d->m_categories.at(index);
146 }
147
148 /*!
149 Sets minimum category to \a min.
150 */
151 void QBarCategoriesAxis::setMin(const QString& min)
152 {
153 Q_D(QBarCategoriesAxis);
154 setRange(min,d->m_maxCategory);
155 }
156
157 /*!
158 Returns minimum category.
159 */
160 QString QBarCategoriesAxis::min() const
161 {
162 Q_D(const QBarCategoriesAxis);
163 return d->m_minCategory;
164 }
165
166 /*!
167 Sets maximum category to \a max.
168 */
169 void QBarCategoriesAxis::setMax(const QString& max)
170 {
171 Q_D(QBarCategoriesAxis);
172 setRange(d->m_minCategory,max);
173 }
174
175 /*!
176 Returns maximum category
177 */
178 QString QBarCategoriesAxis::max() const
179 {
180 Q_D(const QBarCategoriesAxis);
181 return d->m_maxCategory;
182 }
183
184 /*!
185 Sets range from \a minCategory to \a maxCategory
186 */
187 void QBarCategoriesAxis::setRange(const QString& minCategory, const QString& maxCategory)
188 {
189 Q_D(QBarCategoriesAxis);
190
191 int minIndex = d->m_categories.indexOf(minCategory);
192 if (minIndex == -1) {
193 return;
194 }
195 int maxIndex = d->m_categories.indexOf(maxCategory);
196 if (maxIndex == -1) {
197 return;
198 }
199
200 if (maxIndex <= minIndex) {
201 // max must be greater than min
202 return;
203 }
204
205 bool changed = false;
206 if (!qFuzzyIsNull(d->m_min - (minIndex))) {
207 d->m_minCategory = minCategory;
208 d->m_min = minIndex;
209 changed = true;
210 }
211
212 if (!qFuzzyIsNull(d->m_max - (maxIndex))) {
213 d->m_max = maxIndex;
214 d->m_maxCategory = maxCategory;
215 changed = true;
216 }
217
218 if ((changed)) {
219 d->emitRange();
220 emit categoriesChanged();
221 }
222 }
223
224 /*!
225 102 Returns the type of axis.
226 103 */
227 QAbstractAxis::AxisType QBarCategoriesAxis::type() const
104 QAbstractAxis::AxisType QCategoriesAxis::type() const
228 105 {
229 106 return AxisTypeCategories;
230 107 }
231 108
232 109 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233 110
234 QBarCategoriesAxisPrivate::QBarCategoriesAxisPrivate(QBarCategoriesAxis* q):
235 QAbstractAxisPrivate(q)
111 QCategoriesAxisPrivate::QCategoriesAxisPrivate(QCategoriesAxis* q):
112 QValuesAxisPrivate(q),
113 m_categoryMinimum(0)
236 114 {
237 115
238 116 }
239 117
240 QBarCategoriesAxisPrivate::~QBarCategoriesAxisPrivate()
118 QCategoriesAxisPrivate::~QCategoriesAxisPrivate()
241 119 {
242 120
243 121 }
244 122
245 void QBarCategoriesAxisPrivate::setMin(const QVariant &min)
246 {
247 setRange(min,m_maxCategory);
248 }
249
250 void QBarCategoriesAxisPrivate::setMax(const QVariant &max)
251 {
252 setRange(m_minCategory,max);
253 }
254
255 void QBarCategoriesAxisPrivate::setRange(const QVariant &min, const QVariant &max)
256 {
257 Q_Q(QBarCategoriesAxis);
258 QString value1 = min.toString();
259 QString value2 = max.toString();
260 q->setRange(value1,value2);
261 }
262
263 int QBarCategoriesAxisPrivate::ticksCount() const
123 int QCategoriesAxisPrivate::ticksCount() const
264 124 {
265 125 return m_categories.count()+1;
266 126 }
267 127
268 void QBarCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
128 void QCategoriesAxisPrivate::handleAxisRangeChanged(qreal min, qreal max,int count)
269 129 {
270 130 m_min = min;
271 131 m_max = max;
272 132 m_ticksCount = count;
273 133 }
274 134
275 ChartAxis* QBarCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
135 ChartAxis* QCategoriesAxisPrivate::createGraphics(ChartPresenter* presenter)
276 136 {
277 Q_Q( QBarCategoriesAxis);
137 Q_UNUSED(presenter);
138 // Q_Q( QCategoriesAxis);
278 139 if(m_orientation == Qt::Vertical){
279 return new ChartCategoriesAxisY(q,presenter);
140 return 0;
280 141 }else{
281 return new ChartCategoriesAxisX(q,presenter);
142 return 0;
282 143 }
283 144 }
284 145
285 void QBarCategoriesAxisPrivate::emitRange()
286 {
287 Q_Q( QBarCategoriesAxis);
288 if(!q->signalsBlocked()) {
289 emit changed(m_min -0.5, m_max +0.5, qCeil(m_max + 0.5) -qCeil(m_min - 0.5) +1, false);
290 }
291 }
292
293
294 146 #include "moc_qcategoriesaxis.cpp"
295 147 #include "moc_qcategoriesaxis_p.cpp"
296 148
297 149 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,64
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 QCATEGORIESAXIS_H
22 22 #define QCATEGORIESAXIS_H
23 23
24 24 #include "qabstractaxis.h"
25 #include "qvaluesaxis.h"
25 26
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 28
28 class QBarCategoriesAxisPrivate;
29 class QCategoriesAxisPrivate;
29 30
30 class QTCOMMERCIALCHART_EXPORT QBarCategoriesAxis : public QAbstractAxis
31 class QTCOMMERCIALCHART_EXPORT QCategoriesAxis : public QValuesAxis
31 32 {
32 33 Q_OBJECT
33 Q_PROPERTY(QStringList categories READ categories WRITE setCategories NOTIFY categoriesChanged)
34 Q_PROPERTY(QString min READ min WRITE setMin NOTIFY minChanged)
35 Q_PROPERTY(QString max READ max WRITE setMax NOTIFY maxChanged)
36 34
37 35 public:
38 explicit QBarCategoriesAxis(QObject *parent = 0);
39 ~QBarCategoriesAxis();
36 explicit QCategoriesAxis(QObject *parent = 0);
37 ~QCategoriesAxis();
40 38
41 39 protected:
42 QBarCategoriesAxis(QBarCategoriesAxisPrivate &d,QObject *parent = 0);
40 QCategoriesAxis(QCategoriesAxisPrivate &d,QObject *parent = 0);
43 41
44 42 public:
45 43 AxisType type() const;
46 void append(const QStringList &categories);
47 void append(const QString &category);
48 void remove(const QString &category);
49 void insert(int index, const QString &category);
50 void clear();
51 void setCategories(const QStringList &categories);
44
45 void append(const QString& category, qreal interval = 1);
46 void remove(const QString& category);
47
48 void setFisrtCategoryMinimum(qreal x);
49
50 qreal categoryMin(const QString& category) const;
51 qreal categoryMax(const QString& category) const;
52
52 53 QStringList categories();
53 54 int count() const;
54 QString at(int index) const;
55
56 //range handling
57 void setMin(const QString& minCategory);
58 QString min() const;
59 void setMax(const QString& maxCategory);
60 QString max() const;
61 void setRange(const QString& minCategory, const QString& maxCategory);
62 55
63 Q_SIGNALS:
64 void categoriesChanged();
65 void minChanged(const QString &min);
66 void maxChanged(const QString &max);
67 void rangeChanged(const QString &min, const QString &max);
68 56
69 57 private:
70 Q_DECLARE_PRIVATE(QBarCategoriesAxis)
71 Q_DISABLE_COPY(QBarCategoriesAxis)
58 Q_DECLARE_PRIVATE(QCategoriesAxis)
59 Q_DISABLE_COPY(QCategoriesAxis)
72 60 };
73 61
74 62 QTCOMMERCIALCHART_END_NAMESPACE
75 63
76 64 #endif // QCATEGORIESAXIS_H
@@ -1,74 +1,71
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QCATEGORIESAXIS_P_H
31 31 #define QCATEGORIESAXIS_P_H
32 32
33 33 #include "qcategoriesaxis.h"
34 #include "qabstractaxis_p.h"
34 #include "qvaluesaxis_p.h"
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 class QBarCategoriesAxisPrivate : public QAbstractAxisPrivate
38
39 typedef QPair<qreal, qreal> Range;
40
41 class QCategoriesAxisPrivate : public QValuesAxisPrivate
39 42 {
40 43 Q_OBJECT
41 44
42 45 public:
43 QBarCategoriesAxisPrivate(QBarCategoriesAxis *q);
44 ~QBarCategoriesAxisPrivate();
46 QCategoriesAxisPrivate(QCategoriesAxis *q);
47 ~QCategoriesAxisPrivate();
48
45 49
46 50 public:
47 51 ChartAxis* createGraphics(ChartPresenter* presenter);
48 void emitRange();
49
50 private:
51 //range handling
52 void setMin(const QVariant &min);
53 void setMax(const QVariant &max);
54 void setRange(const QVariant &min, const QVariant &max);
55 52 int ticksCount() const;
56 53
57 54 Q_SIGNALS:
58 55 void changed(qreal min, qreal max, int tickCount,bool niceNumbers);
59 56
60 57 public Q_SLOTS:
61 58 void handleAxisRangeChanged(qreal min, qreal max,int count);
62 59
63 60 private:
61 QMap<QString , Range> m_categoriesMap;
64 62 QStringList m_categories;
65 QString m_minCategory;
66 QString m_maxCategory;
63 qreal m_categoryMinimum;
67 64
68 65 private:
69 Q_DECLARE_PUBLIC(QBarCategoriesAxis)
66 Q_DECLARE_PUBLIC(QCategoriesAxis)
70 67 };
71 68
72 69 QTCOMMERCIALCHART_END_NAMESPACE
73 70
74 71 #endif // QCATEGORIESAXIS_P_H
@@ -1,743 +1,743
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 "qabstractbarseries.h"
22 22 #include "qabstractbarseries_p.h"
23 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "domain_p.h"
26 26 #include "legendmarker_p.h"
27 27 #include "chartdataset_p.h"
28 28 #include "charttheme_p.h"
29 29 #include "chartanimator_p.h"
30 30 #include "qvaluesaxis.h"
31 #include "qcategoriesaxis.h"
31 #include "qbarcategoriesaxis.h"
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 /*!
36 36 \class QAbstractBarSeries
37 37 \brief Series for creating a bar chart
38 38 \mainclass
39 39
40 40 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
41 41 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
42 42 and y-value is the height of the bar. The category names are ignored with this series and x-axis
43 43 shows the x-values.
44 44
45 45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 46 \image examples_barchart.png
47 47
48 48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 49 */
50 50 /*!
51 51 \qmlclass AbstractBarSeries QAbstractBarSeries
52 52 \inherits QAbstractSeries
53 53
54 54 The following QML shows how to create a simple bar chart:
55 55 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
56 56
57 57 \beginfloatleft
58 58 \image demos_qmlchart6.png
59 59 \endfloat
60 60 \clearfloat
61 61 */
62 62
63 63 /*!
64 64 \property QAbstractBarSeries::barWidth
65 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 66 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
67 67 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
68 68 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
69 69 \sa QBarSeries
70 70 */
71 71 /*!
72 72 \qmlproperty real AbstractBarSeries::barWidth
73 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 74 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
75 75 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
76 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 80 \property QAbstractBarSeries::count
81 81 Holds the number of sets in series.
82 82 */
83 83 /*!
84 84 \qmlproperty int AbstractBarSeries::count
85 85 Holds the number of sets in series.
86 86 */
87 87
88 88 /*!
89 89 \property QAbstractBarSeries::labelsVisible
90 90 Defines the visibility of the labels in series
91 91 */
92 92 /*!
93 93 \qmlproperty bool AbstractBarSeries::labelsVisible
94 94 Defines the visibility of the labels in series
95 95 */
96 96
97 97 /*!
98 98 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
99 99 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
100 100 Clicked bar inside set is indexed by \a index
101 101 */
102 102 /*!
103 103 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
104 104 The signal is emitted if the user clicks with a mouse on top of BarSet.
105 105 Clicked bar inside set is indexed by \a index
106 106 */
107 107
108 108 /*!
109 109 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
110 110
111 111 The signal is emitted if mouse is hovered on top of series.
112 112 Parameter \a barset is the pointer of barset, where hover happened.
113 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 116 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
117 117
118 118 The signal is emitted if mouse is hovered on top of series.
119 119 Parameter \a barset is the pointer of barset, where hover happened.
120 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 124 \fn void QAbstractBarSeries::countChanged()
125 125 This signal is emitted when barset count has been changed, for example by append or remove.
126 126 */
127 127 /*!
128 128 \qmlsignal AbstractBarSeries::onCountChanged()
129 129 This signal is emitted when barset count has been changed, for example by append or remove.
130 130 */
131 131
132 132 /*!
133 133 \fn void QAbstractBarSeries::labelsVisibleChanged()
134 134 This signal is emitted when labels visibility have changed.
135 135 \sa isLabelsVisible(), setLabelsVisible()
136 136 */
137 137
138 138 /*!
139 139 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
140 140 This signal is emitted when \a sets have been added to the series.
141 141 \sa append(), insert()
142 142 */
143 143 /*!
144 144 \qmlsignal AbstractBarSeries::onAdded(BarSet barset)
145 145 Emitted when \a barset has been added to the series.
146 146 */
147 147
148 148 /*!
149 149 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
150 150 This signal is emitted when \a sets have been removed from the series.
151 151 \sa remove()
152 152 */
153 153 /*!
154 154 \qmlsignal AbstractBarSeries::onRemoved(BarSet barset)
155 155 Emitted when \a barset has been removed from the series.
156 156 */
157 157
158 158 /*!
159 159 \qmlmethod BarSet AbstractBarSeries::at(int index)
160 160 Returns bar set at \a index. Returns null if the index is not valid.
161 161 */
162 162
163 163 /*!
164 164 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
165 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 166 For example:
167 167 \code
168 168 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
169 169 myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]);
170 170 \endcode
171 171 */
172 172
173 173 /*!
174 174 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
175 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 176 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
177 177 appended.
178 178 \sa AbstractBarSeries::append()
179 179 */
180 180
181 181 /*!
182 182 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
183 183 Removes the barset from the series. Returns true if successfull, false otherwise.
184 184 */
185 185
186 186 /*!
187 187 \qmlmethod AbstractBarSeries::clear()
188 188 Removes all barsets from the series.
189 189 */
190 190
191 191 /*!
192 192 Constructs empty QAbstractBarSeries.
193 193 QAbstractBarSeries is QObject which is a child of a \a parent.
194 194 */
195 195 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
196 196 QAbstractSeries(*new QAbstractBarSeriesPrivate(this),parent)
197 197 {
198 198 }
199 199
200 200 /*!
201 201 Destructs abstractbarseries and owned barsets.
202 202 */
203 203 QAbstractBarSeries::~QAbstractBarSeries()
204 204 {
205 205 Q_D(QAbstractBarSeries);
206 206 if(d->m_dataset){
207 207 d->m_dataset->removeSeries(this);
208 208 }
209 209 }
210 210
211 211 /*!
212 212 \internal
213 213 */
214 214 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
215 215 QAbstractSeries(d,parent)
216 216 {
217 217 }
218 218
219 219 /*!
220 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 221 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
222 222 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
223 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 225 void QAbstractBarSeries::setBarWidth(qreal width)
226 226 {
227 227 Q_D(QAbstractBarSeries);
228 228 d->setBarWidth(width);
229 229 }
230 230
231 231 /*!
232 232 Returns the width of the bars of the series.
233 233 \sa setBarWidth()
234 234 */
235 235 qreal QAbstractBarSeries::barWidth() const
236 236 {
237 237 Q_D(const QAbstractBarSeries);
238 238 return d->barWidth();
239 239 }
240 240
241 241 /*!
242 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 243 Returns true, if appending succeeded.
244 244 */
245 245 bool QAbstractBarSeries::append(QBarSet *set)
246 246 {
247 247 Q_D(QAbstractBarSeries);
248 248 bool success = d->append(set);
249 249 if (success) {
250 250 QList<QBarSet*> sets;
251 251 sets.append(set);
252 252 emit barsetsAdded(sets);
253 253 emit countChanged();
254 254 }
255 255 return success;
256 256 }
257 257
258 258 /*!
259 259 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
260 260 Returns true, if set was removed.
261 261 */
262 262 bool QAbstractBarSeries::remove(QBarSet *set)
263 263 {
264 264 Q_D(QAbstractBarSeries);
265 265 bool success = d->remove(set);
266 266 if (success) {
267 267 QList<QBarSet*> sets;
268 268 sets.append(set);
269 269 emit barsetsRemoved(sets);
270 270 emit countChanged();
271 271 }
272 272 return success;
273 273 }
274 274
275 275 /*!
276 276 Adds a list of barsets to series. Takes ownership of \a sets.
277 277 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
278 278 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
279 279 and function returns false.
280 280 */
281 281 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
282 282 {
283 283 Q_D(QAbstractBarSeries);
284 284 bool success = d->append(sets);
285 285 if (success) {
286 286 emit barsetsAdded(sets);
287 287 emit countChanged();
288 288 }
289 289 return success;
290 290 }
291 291
292 292 /*!
293 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 294 Returns true, if inserting succeeded.
295 295
296 296 */
297 297 bool QAbstractBarSeries::insert(int index, QBarSet *set)
298 298 {
299 299 Q_D(QAbstractBarSeries);
300 300 bool success = d->insert(index, set);
301 301 if (success) {
302 302 QList<QBarSet*> sets;
303 303 sets.append(set);
304 304 emit barsetsAdded(sets);
305 305 emit countChanged();
306 306 }
307 307 return success;
308 308 }
309 309
310 310 /*!
311 311 Removes all of the bar sets from the series
312 312 */
313 313 void QAbstractBarSeries::clear()
314 314 {
315 315 Q_D(QAbstractBarSeries);
316 316 QList<QBarSet *> sets = barSets();
317 317 bool success = d->remove(sets);
318 318 if (success) {
319 319 emit barsetsRemoved(sets);
320 320 emit countChanged();
321 321 }
322 322 }
323 323
324 324 /*!
325 325 Returns number of sets in series.
326 326 */
327 327 int QAbstractBarSeries::count() const
328 328 {
329 329 Q_D(const QAbstractBarSeries);
330 330 return d->m_barSets.count();
331 331 }
332 332
333 333 /*!
334 334 Returns a list of sets in series. Keeps ownership of sets.
335 335 */
336 336 QList<QBarSet*> QAbstractBarSeries::barSets() const
337 337 {
338 338 Q_D(const QAbstractBarSeries);
339 339 return d->m_barSets;
340 340 }
341 341
342 342 /*!
343 343 Sets the visibility of labels in series to \a visible
344 344 */
345 345 void QAbstractBarSeries::setLabelsVisible(bool visible)
346 346 {
347 347 Q_D(QAbstractBarSeries);
348 348 if (d->m_labelsVisible != visible) {
349 349 d->setLabelsVisible(visible);
350 350 emit labelsVisibleChanged();
351 351 }
352 352 }
353 353
354 354 /*!
355 355 Returns the visibility of labels
356 356 */
357 357 bool QAbstractBarSeries::isLabelsVisible() const
358 358 {
359 359 Q_D(const QAbstractBarSeries);
360 360 return d->m_labelsVisible;
361 361 }
362 362
363 363 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
364 364
365 365 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
366 366 QAbstractSeriesPrivate(q),
367 367 m_barWidth(0.5), // Default value is 50% of category width
368 368 m_labelsVisible(false),
369 369 m_visible(true)
370 370 {
371 371 }
372 372
373 373 int QAbstractBarSeriesPrivate::categoryCount() const
374 374 {
375 375 // No categories defined. return count of longest set.
376 376 int count = 0;
377 377 for (int i=0; i<m_barSets.count(); i++) {
378 378 if (m_barSets.at(i)->count() > count) {
379 379 count = m_barSets.at(i)->count();
380 380 }
381 381 }
382 382
383 383 return count;
384 384 }
385 385
386 386 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
387 387 {
388 388 if (width < 0.0) {
389 389 width = 0.0;
390 390 }
391 391 m_barWidth = width;
392 392 emit updatedBars();
393 393 }
394 394
395 395 qreal QAbstractBarSeriesPrivate::barWidth() const
396 396 {
397 397 return m_barWidth;
398 398 }
399 399
400 400 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
401 401 {
402 402 return m_barSets.at(index);
403 403 }
404 404
405 405 void QAbstractBarSeriesPrivate::setVisible(bool visible)
406 406 {
407 407 m_visible = visible;
408 408 emit updatedBars();
409 409 }
410 410
411 411 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
412 412 {
413 413 m_labelsVisible = visible;
414 414 emit labelsVisibleChanged(visible);
415 415 }
416 416
417 417 qreal QAbstractBarSeriesPrivate::min()
418 418 {
419 419 if (m_barSets.count() <= 0) {
420 420 return 0;
421 421 }
422 422 qreal min = INT_MAX;
423 423
424 424 for (int i = 0; i < m_barSets.count(); i++) {
425 425 int categoryCount = m_barSets.at(i)->count();
426 426 for (int j = 0; j < categoryCount; j++) {
427 427 qreal temp = m_barSets.at(i)->at(j);
428 428 if (temp < min)
429 429 min = temp;
430 430 }
431 431 }
432 432 return min;
433 433 }
434 434
435 435 qreal QAbstractBarSeriesPrivate::max()
436 436 {
437 437 if (m_barSets.count() <= 0) {
438 438 return 0;
439 439 }
440 440 qreal max = INT_MIN;
441 441
442 442 for (int i = 0; i < m_barSets.count(); i++) {
443 443 int categoryCount = m_barSets.at(i)->count();
444 444 for (int j = 0; j < categoryCount; j++) {
445 445 qreal temp = m_barSets.at(i)->at(j);
446 446 if (temp > max)
447 447 max = temp;
448 448 }
449 449 }
450 450
451 451 return max;
452 452 }
453 453
454 454 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
455 455 {
456 456 if ((set < 0) || (set >= m_barSets.count())) {
457 457 // No set, no value.
458 458 return 0;
459 459 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
460 460 // No category, no value.
461 461 return 0;
462 462 }
463 463
464 464 return m_barSets.at(set)->at(category);
465 465 }
466 466
467 467 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
468 468 {
469 469 if ((set < 0) || (set >= m_barSets.count())) {
470 470 // No set, no value.
471 471 return 0;
472 472 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
473 473 // No category, no value.
474 474 return 0;
475 475 }
476 476
477 477 qreal value = m_barSets.at(set)->at(category);
478 478 qreal sum = categorySum(category);
479 479 if ( qFuzzyIsNull(sum) ) {
480 480 return 0;
481 481 }
482 482
483 483 return value / sum;
484 484 }
485 485
486 486 qreal QAbstractBarSeriesPrivate::categorySum(int category)
487 487 {
488 488 qreal sum(0);
489 489 int count = m_barSets.count(); // Count sets
490 490 for (int set = 0; set < count; set++) {
491 491 if (category < m_barSets.at(set)->count())
492 492 sum += m_barSets.at(set)->at(category);
493 493 }
494 494 return sum;
495 495 }
496 496
497 497 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
498 498 {
499 499 qreal sum(0);
500 500 int count = m_barSets.count(); // Count sets
501 501 for (int set = 0; set < count; set++) {
502 502 if (category < m_barSets.at(set)->count())
503 503 sum += qAbs(m_barSets.at(set)->at(category));
504 504 }
505 505 return sum;
506 506 }
507 507
508 508 qreal QAbstractBarSeriesPrivate::maxCategorySum()
509 509 {
510 510 qreal max = INT_MIN;
511 511 int count = categoryCount();
512 512 for (int i = 0; i < count; i++) {
513 513 qreal sum = categorySum(i);
514 514 if (sum > max)
515 515 max = sum;
516 516 }
517 517 return max;
518 518 }
519 519
520 520 qreal QAbstractBarSeriesPrivate::minX()
521 521 {
522 522 if (m_barSets.count() <= 0) {
523 523 return 0;
524 524 }
525 525 qreal min = INT_MAX;
526 526
527 527 for (int i = 0; i < m_barSets.count(); i++) {
528 528 int categoryCount = m_barSets.at(i)->count();
529 529 for (int j = 0; j < categoryCount; j++) {
530 530 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
531 531 if (temp < min)
532 532 min = temp;
533 533 }
534 534 }
535 535 return min;
536 536 }
537 537
538 538 qreal QAbstractBarSeriesPrivate::maxX()
539 539 {
540 540 if (m_barSets.count() <= 0) {
541 541 return 0;
542 542 }
543 543 qreal max = INT_MIN;
544 544
545 545 for (int i = 0; i < m_barSets.count(); i++) {
546 546 int categoryCount = m_barSets.at(i)->count();
547 547 for (int j = 0; j < categoryCount; j++) {
548 548 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
549 549 if (temp > max)
550 550 max = temp;
551 551 }
552 552 }
553 553
554 554 return max;
555 555 }
556 556
557 557
558 558 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
559 559 {
560 560 qreal minX(domain.minX());
561 561 qreal minY(domain.minY());
562 562 qreal maxX(domain.maxX());
563 563 qreal maxY(domain.maxY());
564 564 int tickXCount(domain.tickXCount());
565 565 int tickYCount(domain.tickYCount());
566 566
567 567 qreal seriesMinX = this->minX();
568 568 qreal seriesMaxX = this->maxX();
569 569 qreal y = max();
570 570 minX = qMin(minX, seriesMinX - 0.5);
571 571 minY = qMin(minY, y);
572 572 maxX = qMax(maxX, seriesMaxX + 0.5);
573 573 maxY = qMax(maxY, y);
574 574 tickXCount = categoryCount()+1;
575 575
576 576 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
577 577 }
578 578
579 579 Chart* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
580 580 {
581 581 Q_Q(QAbstractBarSeries);
582 582
583 583 BarChartItem* bar = new BarChartItem(q,presenter);
584 584 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
585 585 presenter->animator()->addAnimation(bar);
586 586 }
587 587 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
588 588 return bar;
589 589
590 590 }
591 591
592 592 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
593 593 {
594 594 Q_Q(QAbstractBarSeries);
595 595 QList<LegendMarker*> markers;
596 596 foreach(QBarSet* set, q->barSets()) {
597 597 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
598 598 markers << marker;
599 599 }
600 600
601 601 return markers;
602 602 }
603 603
604 604 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
605 605 {
606 606 Q_Q(QAbstractBarSeries);
607 607 if ((m_barSets.contains(set)) || (set == 0)) {
608 608 // Fail if set is already in list or set is null.
609 609 return false;
610 610 }
611 611 m_barSets.append(set);
612 612 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
613 613 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
614 614 emit restructuredBars(); // this notifies barchartitem
615 615 if (m_dataset) {
616 616 m_dataset->updateSeries(q); // this notifies legend
617 617 }
618 618 return true;
619 619 }
620 620
621 621 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
622 622 {
623 623 Q_Q(QAbstractBarSeries);
624 624 if (!m_barSets.contains(set)) {
625 625 // Fail if set is not in list
626 626 return false;
627 627 }
628 628 m_barSets.removeOne(set);
629 629 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
630 630 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
631 631 emit restructuredBars(); // this notifies barchartitem
632 632 if (m_dataset) {
633 633 m_dataset->updateSeries(q); // this notifies legend
634 634 }
635 635 return true;
636 636 }
637 637
638 638 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
639 639 {
640 640 Q_Q(QAbstractBarSeries);
641 641 foreach (QBarSet* set, sets) {
642 642 if ((set == 0) || (m_barSets.contains(set))) {
643 643 // Fail if any of the sets is null or is already appended.
644 644 return false;
645 645 }
646 646 if (sets.count(set) != 1) {
647 647 // Also fail if same set is more than once in given list.
648 648 return false;
649 649 }
650 650 }
651 651
652 652 foreach (QBarSet* set, sets) {
653 653 m_barSets.append(set);
654 654 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
655 655 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
656 656 }
657 657 emit restructuredBars(); // this notifies barchartitem
658 658 if (m_dataset) {
659 659 m_dataset->updateSeries(q); // this notifies legend
660 660 }
661 661 return true;
662 662 }
663 663
664 664 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
665 665 {
666 666 Q_Q(QAbstractBarSeries);
667 667 if (sets.count() == 0) {
668 668 return false;
669 669 }
670 670 foreach (QBarSet* set, sets) {
671 671 if ((set == 0) || (!m_barSets.contains(set))) {
672 672 // Fail if any of the sets is null or is not in series
673 673 return false;
674 674 }
675 675 if (sets.count(set) != 1) {
676 676 // Also fail if same set is more than once in given list.
677 677 return false;
678 678 }
679 679 }
680 680
681 681 foreach (QBarSet* set, sets) {
682 682 m_barSets.removeOne(set);
683 683 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
684 684 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
685 685 }
686 686
687 687 emit restructuredBars(); // this notifies barchartitem
688 688 if (m_dataset) {
689 689 m_dataset->updateSeries(q); // this notifies legend
690 690 }
691 691 return true;
692 692 }
693 693
694 694 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
695 695 {
696 696 Q_Q(QAbstractBarSeries);
697 697 if ((m_barSets.contains(set)) || (set == 0)) {
698 698 // Fail if set is already in list or set is null.
699 699 return false;
700 700 }
701 701 m_barSets.insert(index, set);
702 702 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
703 703 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
704 704 emit restructuredBars(); // this notifies barchartitem
705 705 if (m_dataset) {
706 706 m_dataset->updateSeries(q); // this notifies legend
707 707 }
708 708 return true;
709 709 }
710 710
711 711 void QAbstractBarSeriesPrivate::initializeAxisX(QAbstractAxis* axis)
712 712 {
713 713 if(axis->type()==QAbstractAxis::AxisTypeCategories)
714 714 {
715 715 QBarCategoriesAxis* cataxis = qobject_cast<QBarCategoriesAxis*>(axis);
716 716 Q_ASSERT(cataxis);
717 717 QStringList categories;
718 718 for (int i(1); i < categoryCount()+1; i++)
719 719 categories << QString::number(i);
720 720 cataxis->append(categories);
721 721 }
722 722 }
723 723
724 724 void QAbstractBarSeriesPrivate::initializeAxisY(QAbstractAxis* axis)
725 725 {
726 726 Q_UNUSED(axis)
727 727 }
728 728
729 729 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisXType() const
730 730 {
731 731 return QAbstractAxis::AxisTypeCategories;
732 732 }
733 733
734 734 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisYType() const
735 735 {
736 736 return QAbstractAxis::AxisTypeValues;
737 737 }
738 738
739 739 #include "moc_qabstractbarseries.cpp"
740 740 #include "moc_qabstractbarseries_p.cpp"
741 741
742 742
743 743 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,446 +1,446
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 "chartdataset_p.h"
22 22 #include "qchart.h"
23 23 #include "qvaluesaxis.h"
24 #include "qcategoriesaxis.h"
24 #include "qbarcategoriesaxis.h"
25 25 #include "qvaluesaxis_p.h"
26 26 #include "qabstractseries_p.h"
27 27 #include "qabstractbarseries.h"
28 28 #include "qstackedbarseries.h"
29 29 #include "qpercentbarseries.h"
30 30 #include "qpieseries.h"
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 ChartDataSet::ChartDataSet(QChart *parent):QObject(parent),
35 35 m_domainIndex(0)
36 36 {
37 37
38 38 }
39 39
40 40 ChartDataSet::~ChartDataSet()
41 41 {
42 42 removeAllSeries();
43 43 }
44 44
45 45 void ChartDataSet::addSeries(QAbstractSeries* series)
46 46 {
47 47 Domain* domain = m_seriesDomainMap.value(series);
48 48
49 49 if(domain) {
50 50 qWarning() << "Can not add series. Series already on the chart";
51 51 return;
52 52 }
53 53
54 54 series->setParent(this); // take ownership
55 55
56 56 domain = new Domain(series);
57 57
58 58 m_seriesDomainMap.insert(series,domain);
59 59
60 60 series->d_ptr->scaleDomain(*domain);
61 61
62 62 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
63 63
64 64 int key=0;
65 65 while (i.hasNext()) {
66 66 i.next();
67 67 if(i.key()!=key) {
68 68 break;
69 69 }
70 70 key++;
71 71 }
72 72
73 73 m_indexSeriesMap.insert(key,series);
74 74
75 75 series->d_ptr->m_chart = qobject_cast<QChart*>(parent());
76 76 series->d_ptr->m_dataset = this;
77 77
78 78 emit seriesAdded(series,domain);
79 79
80 80 }
81 81
82 82 void ChartDataSet::createDefaultAxes()
83 83 {
84 84
85 85 if(m_seriesDomainMap.isEmpty()) return;
86 86
87 87 QAbstractAxis::AxisTypes typeX(0);
88 88 QAbstractAxis::AxisTypes typeY(0);
89 89
90 90 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
91 91 while (i.hasNext()) {
92 92 i.next();
93 93 removeAxes(i.key());
94 94 }
95 95
96 96 i.toFront();
97 97
98 98 while (i.hasNext()) {
99 99 i.next();
100 100 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
101 101 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
102 102 if(axisX) typeX&=axisX->type();
103 103 else typeX|=i.key()->d_ptr->defaultAxisXType();
104 104 if(axisY) typeY&=axisY->type();
105 105 else typeY|=i.key()->d_ptr->defaultAxisYType();
106 106 }
107 107
108 108
109 109 if(typeX.testFlag(QAbstractAxis::AxisTypeValues) && typeX.testFlag(QAbstractAxis::AxisTypeCategories))
110 110 {
111 111 i.toFront();
112 112 while (i.hasNext()) {
113 113 i.next();
114 114 QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisXType());
115 115 i.key()->d_ptr->initializeAxisX(axis);
116 116 addAxisX(axis,i.key());
117 117 emit axisAdded(axis,i.value());
118 118 }
119 119
120 120 }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){
121 121 QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeX)));
122 122 i.toFront();
123 123 while (i.hasNext()) {
124 124 i.next();
125 125 i.key()->d_ptr->initializeAxisX(axis);
126 126 addAxisX(axis,i.key());
127 127 }
128 128 emit axisAdded(axis,i.value());
129 129
130 130 }
131 131
132 132 if(typeY.testFlag(QAbstractAxis::AxisTypeValues) && typeY.testFlag(QAbstractAxis::AxisTypeCategories))
133 133 {
134 134 i.toFront();
135 135 while (i.hasNext()) {
136 136 i.next();
137 137 QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisYType());
138 138 i.key()->d_ptr->initializeAxisY(axis);
139 139 addAxisY(axis,i.key());
140 140 emit axisAdded(axis,i.value());
141 141 }
142 142
143 143 }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){
144 144 QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeY)));
145 145 i.toFront();
146 146 while (i.hasNext()) {
147 147 i.next();
148 148 i.key()->d_ptr->initializeAxisY(axis);
149 149 addAxisY(axis,i.key());
150 150 }
151 151 emit axisAdded(axis,i.value());
152 152
153 153 }
154 154 }
155 155
156 156
157 157 QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type)
158 158 {
159 159 QAbstractAxis* axis =0;
160 160
161 161 switch(type) {
162 162 case QAbstractAxis::AxisTypeValues:
163 163 axis = new QValuesAxis(this);
164 164 break;
165 165 case QAbstractAxis::AxisTypeCategories:
166 166 axis = new QBarCategoriesAxis(this);
167 167 break;
168 168 default:
169 169 axis = 0;
170 170 break;
171 171 }
172 172
173 173 return axis;
174 174 }
175 175
176 176 void ChartDataSet::addAxisX(QAbstractAxis* axis,QAbstractSeries* series) {
177 177 Domain* domain = m_seriesDomainMap.value(series);
178 178 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
179 179 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
180 180 axis->d_ptr->m_orientation=Qt::Horizontal;
181 181 m_seriesAxisXMap.insert(series,axis);
182 182 }
183 183
184 184 void ChartDataSet::addAxisY(QAbstractAxis* axis,QAbstractSeries* series) {
185 185 Domain* domain = m_seriesDomainMap.value(series);
186 186 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
187 187 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
188 188 axis->d_ptr->m_orientation=Qt::Vertical;
189 189 m_seriesAxisYMap.insert(series,axis);
190 190 }
191 191
192 192 void ChartDataSet::removeSeries(QAbstractSeries* series)
193 193 {
194 194 Domain* domain = m_seriesDomainMap.take(series);
195 195
196 196 if(!domain) {
197 197 qWarning()<<"Can not remove series. Series not found on the chart.";
198 198 }
199 199
200 200 emit seriesRemoved(series);
201 201
202 202 delete domain;
203 203 domain = 0;
204 204
205 205 int key = seriesIndex(series);
206 206 Q_ASSERT(key!=-1);
207 207
208 208 m_indexSeriesMap.remove(key);
209 209
210 210 series->setParent(0);
211 211 series->d_ptr->m_chart = 0;
212 212 series->d_ptr->m_dataset = 0;
213 213
214 214 removeAxes(series);
215 215 }
216 216
217 217 void ChartDataSet::removeAxes(QAbstractSeries* series)
218 218 {
219 219 QAbstractAxis* axisX = m_seriesAxisXMap.take(series);
220 220
221 221 if(axisX) {
222 222 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
223 223 int x = axesX.indexOf(axisX);
224 224
225 225 if(x==-1) {
226 226 emit axisRemoved(axisX);
227 227 axisX->deleteLater();
228 228 }
229 229 }
230 230
231 231 QAbstractAxis* axisY = m_seriesAxisYMap.take(series);
232 232
233 233 if(axisY) {
234 234 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
235 235
236 236 int y = axesY.indexOf(axisY);
237 237
238 238 if(y==-1) {
239 239 emit axisRemoved(axisY);
240 240 axisY->deleteLater();
241 241 }
242 242 }
243 243 }
244 244
245 245 void ChartDataSet::removeAllSeries()
246 246 {
247 247 QList<QAbstractSeries*> series = m_seriesDomainMap.keys();
248 248 foreach(QAbstractSeries *s , series) {
249 249 removeSeries(s);
250 250 }
251 251
252 252 Q_ASSERT(m_seriesAxisXMap.count()==0);
253 253 Q_ASSERT(m_seriesAxisXMap.count()==0);
254 254 Q_ASSERT(m_seriesDomainMap.count()==0);
255 255
256 256 qDeleteAll(series);
257 257 }
258 258
259 259 void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size)
260 260 {
261 261 //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates
262 262
263 263 blockAxisSignals(true);
264 264
265 265 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
266 266
267 267 while (i.hasNext()) {
268 268 i.next();
269 269 i.value()->zoomIn(rect,size);
270 270 }
271 271
272 272 blockAxisSignals(false);
273 273
274 274 }
275 275
276 276 void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size)
277 277 {
278 278 //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates
279 279
280 280 blockAxisSignals(true);
281 281
282 282 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
283 283
284 284 while (i.hasNext()) {
285 285 i.next();
286 286 i.value()->zoomOut(rect,size);
287 287 }
288 288
289 289 blockAxisSignals(false);
290 290 }
291 291
292 292 void ChartDataSet::blockAxisSignals(bool enabled)
293 293 {
294 294 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
295 295
296 296 while (i.hasNext()) {
297 297 i.next();
298 298 QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key());
299 299 QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key());
300 300 if(axisX) axisX->blockSignals(enabled);
301 301 if(axisY) axisY->blockSignals(enabled);
302 302 }
303 303 }
304 304
305 305 int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type)
306 306 {
307 307 int count=0;
308 308 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
309 309 while (i.hasNext()) {
310 310 i.next();
311 311 if(i.key()->type()==type) count++;
312 312 }
313 313 return count;
314 314 }
315 315
316 316 int ChartDataSet::seriesIndex(QAbstractSeries *series)
317 317 {
318 318 QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap);
319 319 while (i.hasNext()) {
320 320 i.next();
321 321 if (i.value() == series)
322 322 return i.key();
323 323 }
324 324 return -1;
325 325 }
326 326
327 327 QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const
328 328 {
329 329 if(series == 0) return m_seriesAxisXMap.begin().value();
330 330 return m_seriesAxisXMap.value(series);
331 331 }
332 332
333 333 QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const
334 334 {
335 335 if(series == 0) return m_seriesAxisYMap.begin().value();
336 336 return m_seriesAxisYMap.value(series);
337 337 }
338 338
339 339 void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis)
340 340 {
341 341 Q_ASSERT(axis);
342 342 Domain* domain = m_seriesDomainMap.value(series);
343 343
344 344 if(!domain) {
345 345 qWarning() << "Series not found on the chart.";
346 346 return;
347 347 }
348 348
349 349 if(axis->d_ptr->m_orientation==Qt::Vertical) {
350 350 qWarning()<<"Axis already defined as axis Y";
351 351 return;
352 352 }
353 353
354 354 QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series);
355 355 QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values();
356 356
357 357 if(oldAxis) {
358 358
359 359 int x = axesX.indexOf(oldAxis);
360 360 if(x==-1) {
361 361 emit axisRemoved(oldAxis);
362 362 oldAxis->deleteLater();
363 363 }
364 364 }
365 365
366 366 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool)));
367 367 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
368 368
369 369 int x = axesX.indexOf(axis);
370 370 if(x==-1) {
371 371 axis->d_ptr->m_orientation=Qt::Horizontal;
372 372 emit axisAdded(axis,domain);
373 373 }
374 374
375 375 m_seriesAxisXMap.insert(series,axis);
376 376 axis->d_ptr->emitRange();
377 377 }
378 378
379 379 void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis)
380 380 {
381 381 Q_ASSERT(axis);
382 382 Domain* domain = m_seriesDomainMap.value(series);
383 383
384 384 if(!domain) {
385 385 qWarning() << "Series not found on the chart.";
386 386 return;
387 387 }
388 388
389 389 if(axis->d_ptr->m_orientation==Qt::Horizontal) {
390 390 qWarning()<<"Axis already defined as axis X";
391 391 return;
392 392 }
393 393
394 394 QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series);
395 395 QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values();
396 396
397 397 if(oldAxis) {
398 398 int y = axesY.indexOf(oldAxis);
399 399 if(y==-1) {
400 400 emit axisRemoved(oldAxis);
401 401 oldAxis->deleteLater();
402 402 }
403 403 }
404 404
405 405 QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool)));
406 406 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int)));
407 407
408 408 int y = axesY.indexOf(axis);
409 409 if(y==-1) {
410 410 axis->d_ptr->m_orientation=Qt::Vertical;
411 411 emit axisAdded(axis,domain);
412 412 }
413 413
414 414 m_seriesAxisYMap.insert(series,axis);
415 415 axis->d_ptr->emitRange();
416 416 }
417 417
418 418 Domain* ChartDataSet::domain(QAbstractSeries *series) const
419 419 {
420 420 return m_seriesDomainMap.value(series);
421 421 }
422 422
423 423 void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size)
424 424 {
425 425 blockAxisSignals(true);
426 426 QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap);
427 427 while (i.hasNext()) {
428 428 i.next();
429 429 i.value()->move(dx,dy,size);
430 430 }
431 431 blockAxisSignals(false);
432 432 }
433 433
434 434 QList<QAbstractSeries*> ChartDataSet::series() const
435 435 {
436 436 return m_seriesAxisXMap.keys();
437 437 }
438 438
439 439 void ChartDataSet::updateSeries(QAbstractSeries *series)
440 440 {
441 441 emit seriesUpdated(series);
442 442 }
443 443
444 444 #include "moc_chartdataset_p.cpp"
445 445
446 446 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,624 +1,624
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 <QtTest/QtTest>
22 22 #include <qabstractaxis.h>
23 23 #include <qvaluesaxis.h>
24 #include <qcategoriesaxis.h>
24 #include <qbarcategoriesaxis.h>
25 25 #include <qlineseries.h>
26 26 #include <qareaseries.h>
27 27 #include <qscatterseries.h>
28 28 #include <qsplineseries.h>
29 29 #include <qpieseries.h>
30 30 #include <qbarseries.h>
31 31 #include <qpercentbarseries.h>
32 32 #include <qstackedbarseries.h>
33 33 #include <private/chartdataset_p.h>
34 34 #include <private/domain_p.h>
35 35 #include <tst_definitions.h>
36 36
37 37 QTCOMMERCIALCHART_USE_NAMESPACE
38 38
39 39 Q_DECLARE_METATYPE(Domain *)
40 40 Q_DECLARE_METATYPE(QAbstractAxis *)
41 41 Q_DECLARE_METATYPE(QAbstractSeries *)
42 42 Q_DECLARE_METATYPE(QList<QAbstractSeries *>)
43 43 Q_DECLARE_METATYPE(QList<QAbstractAxis *>)
44 44 Q_DECLARE_METATYPE(QLineSeries *)
45 45
46 46 class tst_ChartDataSet: public QObject {
47 47
48 48 Q_OBJECT
49 49
50 50 public Q_SLOTS:
51 51 void initTestCase();
52 52 void cleanupTestCase();
53 53 void init();
54 54 void cleanup();
55 55
56 56 private Q_SLOTS:
57 57 void chartdataset_data();
58 58 void chartdataset();
59 59 void addSeries_data();
60 60 void addSeries();
61 61 void setAxisX_data();
62 62 void setAxisX();
63 63 void setAxisY_data();
64 64 void setAxisY();
65 65 void removeSeries_data();
66 66 void removeSeries();
67 67 void removeAllSeries_data();
68 68 void removeAllSeries();
69 69 void seriesCount_data();
70 70 void seriesCount();
71 71 void seriesIndex_data();
72 72 void seriesIndex();
73 73 void domain_data();
74 74 void domain();
75 75 void zoomInDomain_data();
76 76 void zoomInDomain();
77 77 void zoomOutDomain_data();
78 78 void zoomOutDomain();
79 79 void scrollDomain_data();
80 80 void scrollDomain();
81 81
82 82 private:
83 83 ChartDataSet* m_dataset;
84 84 };
85 85
86 86 void tst_ChartDataSet::initTestCase()
87 87 {
88 88 qRegisterMetaType<Domain*>();
89 89 qRegisterMetaType<QAbstractAxis*>();
90 90 qRegisterMetaType<QAbstractSeries*>();
91 91 }
92 92
93 93 void tst_ChartDataSet::cleanupTestCase()
94 94 {
95 95 }
96 96
97 97 void tst_ChartDataSet::init()
98 98 {
99 99 m_dataset = new ChartDataSet();
100 100 }
101 101
102 102
103 103 void tst_ChartDataSet::cleanup()
104 104 {
105 105 QList<QAbstractSeries*> series = m_dataset->series();
106 106 foreach(QAbstractSeries* serie, series)
107 107 {
108 108 m_dataset->removeSeries(serie);
109 109 }
110 110 }
111 111
112 112 void tst_ChartDataSet::chartdataset_data()
113 113 {
114 114 }
115 115
116 116 void tst_ChartDataSet::chartdataset()
117 117 {
118 118 QVERIFY(m_dataset->axisX(0) == 0);
119 119 QVERIFY(m_dataset->axisY(0) == 0);
120 120 QLineSeries* series = new QLineSeries(this);
121 121 QCOMPARE(m_dataset->seriesIndex(series),-1);
122 122 QVERIFY(m_dataset->domain(series) == 0);
123 123 QVERIFY(m_dataset->axisX(series) == 0);
124 124 QVERIFY(m_dataset->axisY(series) == 0);
125 125 m_dataset->createDefaultAxes();
126 126 }
127 127
128 128
129 129 void tst_ChartDataSet::addSeries_data()
130 130 {
131 131 QTest::addColumn<QAbstractSeries*>("series");
132 132
133 133 QAbstractSeries* line = new QLineSeries(this);
134 134 QAbstractSeries* area = new QAreaSeries(static_cast<QLineSeries*>(line));
135 135 QAbstractSeries* scatter = new QScatterSeries(this);
136 136 QAbstractSeries* spline = new QSplineSeries(this);
137 137 QAbstractSeries* pie = new QPieSeries(this);
138 138 QAbstractSeries* bar = new QBarSeries(this);
139 139 QAbstractSeries* percent = new QPercentBarSeries(this);
140 140 QAbstractSeries* stacked = new QStackedBarSeries(this);
141 141
142 142 QTest::newRow("line") << line;
143 143 QTest::newRow("area") << area;
144 144 QTest::newRow("scatter") << scatter;
145 145 QTest::newRow("spline") << spline;
146 146 QTest::newRow("pie") << pie;
147 147 QTest::newRow("bar") << bar;
148 148 QTest::newRow("percent") << percent;
149 149 QTest::newRow("stacked") << stacked;
150 150 }
151 151
152 152 void tst_ChartDataSet::addSeries()
153 153 {
154 154
155 155 QFETCH(QAbstractSeries*, series);
156 156
157 157 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*, Domain *)));
158 158 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)));
159 159 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
160 160 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
161 161
162 162 m_dataset->addSeries(series);
163 163 m_dataset->createDefaultAxes();
164 164 if(series->type()==QAbstractSeries::SeriesTypePie){
165 165 TRY_COMPARE(spy0.count(), 0);
166 166 }else{
167 167 TRY_COMPARE(spy0.count(), 2);
168 168 }
169 169 TRY_COMPARE(spy1.count(), 0);
170 170 TRY_COMPARE(spy2.count(), 1);
171 171 TRY_COMPARE(spy3.count(), 0);
172 172 }
173 173
174 174
175 175 void tst_ChartDataSet::setAxisX_data()
176 176 {
177 177
178 178 QTest::addColumn<QList<QAbstractSeries*> >("seriesList");
179 179 QTest::addColumn<QList<QAbstractAxis*> >("axisList");
180 180 QTest::addColumn<int>("axisCount");
181 181
182 182 QAbstractSeries* line = new QLineSeries(this);
183 183 QAbstractSeries* area = new QAreaSeries(static_cast<QLineSeries*>(line));
184 184 QAbstractSeries* scatter = new QScatterSeries(this);
185 185 QAbstractSeries* spline = new QSplineSeries(this);
186 186 QAbstractSeries* pie = new QPieSeries(this);
187 187 QAbstractSeries* bar = new QBarSeries(this);
188 188 QAbstractSeries* percent = new QPercentBarSeries(this);
189 189 QAbstractSeries* stacked = new QStackedBarSeries(this);
190 190
191 191 QTest::newRow("line,spline,scatter: axis 0 axis1 axis 2")
192 192 << (QList<QAbstractSeries*>() << line << spline << scatter)
193 193 << (QList<QAbstractAxis*>() << new QValuesAxis(this) << new QValuesAxis(this) << new QValuesAxis(this)) << 3;
194 194
195 195 QTest::newRow("area: axis 0") << (QList<QAbstractSeries*>() << area)
196 196 << (QList<QAbstractAxis*>() << new QValuesAxis(this)) << 1;
197 197
198 198 QList<QAbstractAxis*> axes0;
199 199 axes0 << new QValuesAxis(this) << new QValuesAxis(this);
200 200 axes0 << axes0.last();
201 201 QTest::newRow("line,spline,scatter: axis 0 axis1 axis 1")
202 202 << (QList<QAbstractSeries*>() << line << spline << scatter)
203 203 << axes0 << 2;
204 204 //TODO: add more test cases
205 205 }
206 206
207 207 void tst_ChartDataSet::setAxisX()
208 208 {
209 209 QFETCH(QList<QAbstractSeries*>, seriesList);
210 210 QFETCH(QList<QAbstractAxis*>, axisList);
211 211 QFETCH(int, axisCount);
212 212
213 213 Q_ASSERT(seriesList.count() == axisList.count());
214 214
215 215 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis *,Domain*)));
216 216 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis *)));
217 217 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *,Domain*)));
218 218 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
219 219
220 220 foreach(QAbstractSeries* series, seriesList){
221 221 m_dataset->addSeries(series);
222 222 }
223 223
224 224 TRY_COMPARE(spy0.count(), 0);
225 225 TRY_COMPARE(spy1.count(), 0);
226 226 TRY_COMPARE(spy2.count(), seriesList.count());
227 227 TRY_COMPARE(spy3.count(), 0);
228 228
229 229 QSignalSpy spy4(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)));
230 230 QSignalSpy spy5(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)));
231 231 QSignalSpy spy6(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *,Domain*)));
232 232 QSignalSpy spy7(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
233 233
234 234 for(int i=0 ; i < seriesList.count(); i++){
235 235 m_dataset->setAxisX(seriesList.at(i),axisList.at(i));
236 236 }
237 237
238 238 TRY_COMPARE(spy4.count(), axisCount);
239 239 TRY_COMPARE(spy5.count(), 0);
240 240 TRY_COMPARE(spy6.count(), 0);
241 241 TRY_COMPARE(spy7.count(), 0);
242 242
243 243 for(int i=0 ; i < seriesList.count(); i++){
244 244 QVERIFY(m_dataset->axisX(seriesList.at(i)) == axisList.at(i));
245 245 }
246 246 }
247 247
248 248 void tst_ChartDataSet::setAxisY_data()
249 249 {
250 250 setAxisX_data();
251 251 }
252 252
253 253 void tst_ChartDataSet::setAxisY()
254 254 {
255 255 QFETCH(QList<QAbstractSeries*>, seriesList);
256 256 QFETCH(QList<QAbstractAxis*>, axisList);
257 257 QFETCH(int, axisCount);
258 258
259 259 Q_ASSERT(seriesList.count() == axisList.count());
260 260
261 261 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)));
262 262 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)));
263 263 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *,Domain*)));
264 264 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
265 265
266 266 foreach(QAbstractSeries* series, seriesList){
267 267 m_dataset->addSeries(series);
268 268 }
269 269
270 270 TRY_COMPARE(spy0.count(), 0);
271 271 TRY_COMPARE(spy1.count(), 0);
272 272 TRY_COMPARE(spy2.count(), seriesList.count());
273 273 TRY_COMPARE(spy3.count(), 0);
274 274
275 275 QSignalSpy spy4(m_dataset, SIGNAL(axisAdded(QAbstractAxis*,Domain*)));
276 276 QSignalSpy spy5(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)));
277 277 QSignalSpy spy6(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *,Domain*)));
278 278 QSignalSpy spy7(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
279 279
280 280 for(int i=0 ; i < seriesList.count(); i++){
281 281 m_dataset->setAxisY(seriesList.at(i),axisList.at(i));
282 282 }
283 283
284 284 TRY_COMPARE(spy4.count(), axisCount);
285 285 TRY_COMPARE(spy5.count(), 0);
286 286 TRY_COMPARE(spy6.count(), 0);
287 287 TRY_COMPARE(spy7.count(), 0);
288 288
289 289 for(int i=0 ; i < seriesList.count(); i++){
290 290 QVERIFY(m_dataset->axisY(seriesList.at(i)) == axisList.at(i));
291 291 }
292 292 }
293 293
294 294 void tst_ChartDataSet::removeSeries_data()
295 295 {
296 296 addSeries_data();
297 297 }
298 298
299 299 void tst_ChartDataSet::removeSeries()
300 300 {
301 301 QFETCH(QAbstractSeries*, series);
302 302
303 303 m_dataset->addSeries(series);
304 304 m_dataset->createDefaultAxes();
305 305
306 306 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*, Domain *)));
307 307 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*)));
308 308 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
309 309 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
310 310
311 311 m_dataset->removeSeries(series);
312 312
313 313 TRY_COMPARE(spy0.count(), 0);
314 314 if (series->type() == QAbstractSeries::SeriesTypePie) {
315 315 TRY_COMPARE(spy1.count(), 0);
316 316 }
317 317 else {
318 318 TRY_COMPARE(spy1.count(), 2);
319 319 }
320 320 TRY_COMPARE(spy2.count(), 0);
321 321 TRY_COMPARE(spy3.count(), 1);
322 322 }
323 323
324 324 void tst_ChartDataSet::removeAllSeries_data()
325 325 {
326 326 QTest::addColumn<QList<QAbstractSeries*> >("seriesList");
327 327 QTest::addColumn<QList<QAbstractAxis*> >("axisList");
328 328 QTest::addColumn<int>("axisCount");
329 329
330 330 QTest::newRow("line,spline,scatter: axis 0 axis1 axis 2")
331 331 << (QList<QAbstractSeries*>() << new QLineSeries(this) << new QSplineSeries(this)
332 332 << new QScatterSeries(this))
333 333 << (QList<QAbstractAxis*>() << new QValuesAxis(this) << new QValuesAxis(this)
334 334 << new QValuesAxis(this)) << 3;
335 335 //TODO:
336 336 }
337 337
338 338 void tst_ChartDataSet::removeAllSeries()
339 339 {
340 340 QFETCH(QList<QAbstractSeries*>, seriesList);
341 341 QFETCH(QList<QAbstractAxis*>, axisList);
342 342 QFETCH(int, axisCount);
343 343
344 344 foreach(QAbstractSeries* series, seriesList) {
345 345 m_dataset->addSeries(series);
346 346 }
347 347
348 348 for (int i = 0; i < seriesList.count(); i++) {
349 349 m_dataset->setAxisX(seriesList.at(i), axisList.at(i));
350 350 }
351 351
352 352 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis *, Domain *)));
353 353 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis *)));
354 354 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
355 355 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
356 356
357 357 m_dataset->removeAllSeries();
358 358
359 359 TRY_COMPARE(spy0.count(), 0);
360 360 TRY_COMPARE(spy1.count(), axisCount);
361 361 TRY_COMPARE(spy2.count(), 0);
362 362 TRY_COMPARE(spy3.count(), seriesList.count());
363 363 }
364 364
365 365
366 366 void tst_ChartDataSet::seriesCount_data()
367 367 {
368 368 QTest::addColumn<QList<QAbstractSeries*> >("seriesList");
369 369 QTest::addColumn<int>("seriesCount");
370 370
371 371 QTest::newRow("line,line, line, spline 3") << (QList<QAbstractSeries*>() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) ) << 3;
372 372 QTest::newRow("scatter,scatter, line, line 2") << (QList<QAbstractSeries*>() << new QScatterSeries(this) << new QScatterSeries(this) << new QLineSeries(this) << new QLineSeries(this) ) << 2;
373 373 }
374 374
375 375 void tst_ChartDataSet::seriesCount()
376 376 {
377 377 QFETCH(QList<QAbstractSeries*>, seriesList);
378 378 QFETCH(int, seriesCount);
379 379
380 380 foreach(QAbstractSeries* series, seriesList){
381 381 m_dataset->addSeries(series);
382 382 }
383 383
384 384 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis *, Domain *)));
385 385 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis *)));
386 386 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
387 387 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
388 388
389 389 QCOMPARE(m_dataset->seriesCount(seriesList.at(0)->type()),seriesCount);
390 390 TRY_COMPARE(spy0.count(), 0);
391 391 TRY_COMPARE(spy1.count(), 0);
392 392 TRY_COMPARE(spy2.count(), 0);
393 393 TRY_COMPARE(spy3.count(), 0);
394 394 }
395 395
396 396 void tst_ChartDataSet::seriesIndex_data()
397 397 {
398 398 QTest::addColumn<QList<QAbstractSeries*> >("seriesList");
399 399
400 400 QTest::newRow("line,line, line, spline") << (QList<QAbstractSeries*>() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) );
401 401 QTest::newRow("scatter,scatter, line, line") << (QList<QAbstractSeries*>() << new QScatterSeries(this) << new QScatterSeries(this) << new QLineSeries(this) << new QLineSeries(this) );
402 402 }
403 403
404 404 void tst_ChartDataSet::seriesIndex()
405 405 {
406 406
407 407 QFETCH(QList<QAbstractSeries*>, seriesList);
408 408
409 409 foreach(QAbstractSeries* series, seriesList) {
410 410 m_dataset->addSeries(series);
411 411 }
412 412
413 413 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis *,Domain*)));
414 414 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis *)));
415 415 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries*,Domain*)));
416 416 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries*)));
417 417
418 418 for (int i = 0; i < seriesList.count(); i++) {
419 419 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
420 420 }
421 421
422 422 TRY_COMPARE(spy0.count(), 0);
423 423 TRY_COMPARE(spy1.count(), 0);
424 424 TRY_COMPARE(spy2.count(), 0);
425 425 TRY_COMPARE(spy3.count(), 0);
426 426
427 427 foreach(QAbstractSeries* series, seriesList) {
428 428 m_dataset->removeSeries(series);
429 429 }
430 430
431 431 for (int i = 0; i < seriesList.count(); i++) {
432 432 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1);
433 433 }
434 434
435 435 foreach(QAbstractSeries* series, seriesList) {
436 436 m_dataset->addSeries(series);
437 437 }
438 438
439 439 for (int i = 0; i < seriesList.count(); i++) {
440 440 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
441 441 }
442 442
443 443 m_dataset->removeSeries(seriesList.at(1));
444 444
445 445 for (int i = 0; i < seriesList.count(); i++) {
446 446 if (i != 1)
447 447 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
448 448 else
449 449 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1);
450 450 }
451 451
452 452 m_dataset->addSeries(seriesList.at(1));
453 453
454 454 for (int i = 0; i < seriesList.count(); i++) {
455 455 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
456 456 }
457 457
458 458 m_dataset->removeSeries(seriesList.at(2));
459 459
460 460 for (int i = 0; i < seriesList.count(); i++) {
461 461 if (i != 2)
462 462 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
463 463 else
464 464 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1);
465 465 }
466 466
467 467 m_dataset->removeSeries(seriesList.at(0));
468 468
469 469 for (int i = 0; i < seriesList.count(); i++) {
470 470 if (i != 2 && i != 0)
471 471 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
472 472 else
473 473 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), -1);
474 474 }
475 475
476 476 m_dataset->addSeries(seriesList.at(2));
477 477 m_dataset->addSeries(seriesList.at(0));
478 478
479 479 for (int i = 0; i < seriesList.count(); i++) {
480 480 if (i == 2)
481 481 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), 0);
482 482 else if (i == 0)
483 483 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), 2);
484 484 else
485 485 QCOMPARE(m_dataset->seriesIndex(seriesList.at(i)), i);
486 486 }
487 487
488 488 }
489 489
490 490 void tst_ChartDataSet::domain_data()
491 491 {
492 492 addSeries_data();
493 493 }
494 494
495 495 void tst_ChartDataSet::domain()
496 496 {
497 497 QFETCH(QAbstractSeries*, series);
498 498
499 499 QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis *, Domain *)));
500 500 QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis *)));
501 501 QSignalSpy spy2(m_dataset, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)));
502 502 QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)));
503 503
504 504 m_dataset->addSeries(series);
505 505 QVERIFY(m_dataset->domain(series));
506 506
507 507
508 508 TRY_COMPARE(spy0.count(), 0);
509 509 TRY_COMPARE(spy1.count(), 0);
510 510 TRY_COMPARE(spy2.count(), 1);
511 511
512 512 QList<QVariant> arguments = spy2.takeFirst();
513 513 Domain *domain = (Domain *) arguments.at(1).value<Domain *>();
514 514 QVERIFY(m_dataset->domain(series) == domain);
515 515
516 516 TRY_COMPARE(spy3.count(), 0);
517 517
518 518 }
519 519
520 520 void tst_ChartDataSet::zoomInDomain_data()
521 521 {
522 522 QTest::addColumn<bool >("sameAxis");
523 523 QTest::addColumn<QList<QAbstractSeries*> >("seriesList");
524 524 QTest::newRow("sameAxis: line,line, line, spline") << true << (QList<QAbstractSeries*>() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) );
525 525 QTest::newRow("separeateAxis: line,line, line, spline") << false << (QList<QAbstractSeries*>() << new QLineSeries(this) << new QLineSeries(this) << new QLineSeries(this) << new QSplineSeries(this) );
526 526 }
527 527
528 528 void tst_ChartDataSet::zoomInDomain()
529 529 {
530 530 QFETCH(bool, sameAxis);
531 531 QFETCH(QList<QAbstractSeries*>, seriesList);
532 532
533 533 foreach(QAbstractSeries* series, seriesList) {
534 534 m_dataset->addSeries(series);
535 535 }
536 536
537 537 if(sameAxis) m_dataset->createDefaultAxes();
538 538
539 539 QList<QSignalSpy*> spyList;
540 540
541 541 foreach(QAbstractSeries* series, seriesList) {
542 542 spyList << new QSignalSpy(m_dataset->domain(series),SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
543 543 }
544 544
545 545 m_dataset->zoomInDomain(QRect(0, 0, 100, 100), QSize(1000, 1000));
546 546
547 547 foreach(QSignalSpy* spy, spyList) {
548 548 TRY_COMPARE(spy->count(), 1);
549 549 }
550 550
551 551 qDeleteAll(spyList);
552 552 }
553 553
554 554
555 555
556 556 void tst_ChartDataSet::zoomOutDomain_data()
557 557 {
558 558 zoomInDomain_data();
559 559 }
560 560
561 561 void tst_ChartDataSet::zoomOutDomain()
562 562 {
563 563 QFETCH(bool, sameAxis);
564 564 QFETCH(QList<QAbstractSeries*>, seriesList);
565 565
566 566 foreach(QAbstractSeries* series, seriesList) {
567 567 m_dataset->addSeries(series);
568 568 }
569 569
570 570 if (sameAxis)
571 571 m_dataset->createDefaultAxes();
572 572
573 573 QList<QSignalSpy*> spyList;
574 574
575 575 foreach(QAbstractSeries* series, seriesList) {
576 576 spyList << new QSignalSpy(m_dataset->domain(series), SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
577 577 }
578 578
579 579 m_dataset->zoomOutDomain(QRect(0, 0, 100, 100), QSize(1000, 1000));
580 580
581 581 foreach(QSignalSpy* spy, spyList) {
582 582 TRY_COMPARE(spy->count(), 1);
583 583 }
584 584
585 585 qDeleteAll (spyList);
586 586 }
587 587
588 588 void tst_ChartDataSet::scrollDomain_data()
589 589 {
590 590 zoomInDomain_data();
591 591 }
592 592
593 593 void tst_ChartDataSet::scrollDomain()
594 594 {
595 595 QFETCH(bool, sameAxis);
596 596 QFETCH(QList<QAbstractSeries*>, seriesList);
597 597
598 598 foreach(QAbstractSeries* series, seriesList) {
599 599 m_dataset->addSeries(series);
600 600 }
601 601
602 602 if (sameAxis)
603 603 m_dataset->createDefaultAxes();
604 604
605 605 QList<QSignalSpy*> spyList;
606 606
607 607 foreach(QAbstractSeries* series, seriesList) {
608 608 spyList
609 609 << new QSignalSpy(m_dataset->domain(series),
610 610 SIGNAL(domainChanged(qreal,qreal,qreal,qreal)));
611 611 }
612 612
613 613 m_dataset->scrollDomain(10, 10, QSize(1000, 1000));
614 614
615 615 foreach(QSignalSpy* spy, spyList) {
616 616 TRY_COMPARE(spy->count(), 1);
617 617 }
618 618
619 619 qDeleteAll(spyList);
620 620 }
621 621
622 622 QTEST_MAIN(tst_ChartDataSet)
623 623 #include "tst_chartdataset.moc"
624 624
@@ -1,357 +1,357
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 "mainwidget.h"
22 22 #include "dataseriedialog.h"
23 23 #include "qchartview.h"
24 24 #include "qpieseries.h"
25 25 #include "qscatterseries.h"
26 26 #include "qlineseries.h"
27 27 #include <qareaseries.h>
28 28 #include <qsplineseries.h>
29 29 #include <qbarset.h>
30 30 #include <qbarseries.h>
31 31 #include <qstackedbarseries.h>
32 32 #include <qpercentbarseries.h>
33 33 #include <QPushButton>
34 34 #include <QComboBox>
35 35 #include <QSpinBox>
36 36 #include <QCheckBox>
37 37 #include <QGridLayout>
38 38 #include <QHBoxLayout>
39 39 #include <QLabel>
40 40 #include <QSpacerItem>
41 41 #include <QMessageBox>
42 42 #include <cmath>
43 43 #include <QDebug>
44 44 #include <QStandardItemModel>
45 #include <QCategoriesAxis>
45 #include <QBarCategoriesAxis>
46 46
47 47
48 48 QTCOMMERCIALCHART_USE_NAMESPACE
49 49
50 50 MainWidget::MainWidget(QWidget *parent) :
51 51 QWidget(parent),
52 52 m_addSerieDialog(0),
53 53 m_chart(0)
54 54 {
55 55 m_chart = new QChart();
56 56
57 57 // Grid layout for the controls for configuring the chart widget
58 58 QGridLayout *grid = new QGridLayout();
59 59 QPushButton *addSeriesButton = new QPushButton("Add series");
60 60 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
61 61 grid->addWidget(addSeriesButton, 0, 1);
62 62 initBackroundCombo(grid);
63 63 initScaleControls(grid);
64 64 initThemeCombo(grid);
65 65 initCheckboxes(grid);
66 66
67 67 // add row with empty label to make all the other rows static
68 68 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
69 69 grid->setRowStretch(grid->rowCount() - 1, 1);
70 70
71 71 // Create chart view with the chart
72 72 m_chartView = new QChartView(m_chart, this);
73 73 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
74 74
75 75 // Another grid layout as a main layout
76 76 QGridLayout *mainLayout = new QGridLayout();
77 77 mainLayout->addLayout(grid, 0, 0);
78 78 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
79 79 setLayout(mainLayout);
80 80 }
81 81
82 82 // Combo box for selecting the chart's background
83 83 void MainWidget::initBackroundCombo(QGridLayout *grid)
84 84 {
85 85 QComboBox *backgroundCombo = new QComboBox(this);
86 86 backgroundCombo->addItem("Color");
87 87 backgroundCombo->addItem("Gradient");
88 88 backgroundCombo->addItem("Image");
89 89 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
90 90 this, SLOT(backgroundChanged(int)));
91 91
92 92 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
93 93 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
94 94 }
95 95
96 96 // Scale related controls (auto-scale vs. manual min-max values)
97 97 void MainWidget::initScaleControls(QGridLayout *grid)
98 98 {
99 99 m_autoScaleCheck = new QCheckBox("Automatic scaling");
100 100 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
101 101 // Allow setting also non-sense values (like -2147483648 and 2147483647)
102 102 m_xMinSpin = new QSpinBox();
103 103 m_xMinSpin->setMinimum(INT_MIN);
104 104 m_xMinSpin->setMaximum(INT_MAX);
105 105 m_xMinSpin->setValue(0);
106 106 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
107 107 m_xMaxSpin = new QSpinBox();
108 108 m_xMaxSpin->setMinimum(INT_MIN);
109 109 m_xMaxSpin->setMaximum(INT_MAX);
110 110 m_xMaxSpin->setValue(10);
111 111 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
112 112 m_yMinSpin = new QSpinBox();
113 113 m_yMinSpin->setMinimum(INT_MIN);
114 114 m_yMinSpin->setMaximum(INT_MAX);
115 115 m_yMinSpin->setValue(0);
116 116 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
117 117 m_yMaxSpin = new QSpinBox();
118 118 m_yMaxSpin->setMinimum(INT_MIN);
119 119 m_yMaxSpin->setMaximum(INT_MAX);
120 120 m_yMaxSpin->setValue(10);
121 121 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
122 122
123 123 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
124 124 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
125 125 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
126 126 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
127 127 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
128 128 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
129 129 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
130 130 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
131 131 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
132 132
133 133 m_autoScaleCheck->setChecked(true);
134 134 }
135 135
136 136 // Combo box for selecting theme
137 137 void MainWidget::initThemeCombo(QGridLayout *grid)
138 138 {
139 139 QComboBox *chartTheme = new QComboBox();
140 140 chartTheme->addItem("Default");
141 141 chartTheme->addItem("Light");
142 142 chartTheme->addItem("Blue Cerulean");
143 143 chartTheme->addItem("Dark");
144 144 chartTheme->addItem("Brown Sand");
145 145 chartTheme->addItem("Blue NCS");
146 146 chartTheme->addItem("High Contrast");
147 147 chartTheme->addItem("Blue Icy");
148 148 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
149 149 this, SLOT(changeChartTheme(int)));
150 150 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
151 151 grid->addWidget(chartTheme, 8, 1);
152 152 }
153 153
154 154 // Different check boxes for customizing chart
155 155 void MainWidget::initCheckboxes(QGridLayout *grid)
156 156 {
157 157 // TODO: setZoomEnabled slot has been removed from QChartView -> Re-implement zoom on/off
158 158 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
159 159 // connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartView, SLOT(setZoomEnabled(bool)));
160 160 zoomCheckBox->setChecked(true);
161 161 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
162 162
163 163 QCheckBox *aliasCheckBox = new QCheckBox("Anti-alias");
164 164 connect(aliasCheckBox, SIGNAL(toggled(bool)), this, SLOT(antiAliasToggled(bool)));
165 165 aliasCheckBox->setChecked(false);
166 166 grid->addWidget(aliasCheckBox, grid->rowCount(), 0);
167 167 }
168 168
169 169 void MainWidget::antiAliasToggled(bool enabled)
170 170 {
171 171 m_chartView->setRenderHint(QPainter::Antialiasing, enabled);
172 172 }
173 173
174 174 void MainWidget::addSeries()
175 175 {
176 176 if (!m_addSerieDialog) {
177 177 m_addSerieDialog = new DataSerieDialog(this);
178 178 connect(m_addSerieDialog, SIGNAL(accepted(QString,int,int,QString,bool)),
179 179 this, SLOT(addSeries(QString,int,int,QString,bool)));
180 180 }
181 181 m_addSerieDialog->exec();
182 182 }
183 183
184 184 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
185 185 {
186 186 // TODO: dataCharacteristics
187 187 QList<RealList> testData;
188 188 for (int j(0); j < columnCount; j++) {
189 189 QList <qreal> newColumn;
190 190 for (int i(0); i < rowCount; i++) {
191 191 if (dataCharacteristics == "Sin") {
192 192 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
193 193 } else if (dataCharacteristics == "Sin + random") {
194 194 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
195 195 } else if (dataCharacteristics == "Random") {
196 196 newColumn.append(rand() % 10 + (qreal) rand() / (qreal) RAND_MAX);
197 197 } else if (dataCharacteristics == "Linear") {
198 198 //newColumn.append(i * (j + 1.0));
199 199 // TODO: temporary hack to make pie work; prevent zero values:
200 200 newColumn.append(i * (j + 1.0) + 0.1);
201 201 } else { // "constant"
202 202 newColumn.append((j + 1.0));
203 203 }
204 204 }
205 205 testData.append(newColumn);
206 206 }
207 207 return testData;
208 208 }
209 209
210 210 QStringList MainWidget::generateLabels(int count)
211 211 {
212 212 QStringList result;
213 213 for (int i(0); i < count; i++)
214 214 result.append("label" + QString::number(i));
215 215 return result;
216 216 }
217 217
218 218 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
219 219 {
220 220 qDebug() << "addSeries: " << seriesName
221 221 << " columnCount: " << columnCount
222 222 << " rowCount: " << rowCount
223 223 << " dataCharacteristics: " << dataCharacteristics
224 224 << " labels enabled: " << labelsEnabled;
225 225 m_defaultSeriesName = seriesName;
226 226
227 227 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
228 228
229 229 // Line series and scatter series use similar data
230 230 if (seriesName == "Line") {
231 231 for (int j(0); j < data.count(); j ++) {
232 232 QList<qreal> column = data.at(j);
233 233 QLineSeries *series = new QLineSeries();
234 234 series->setName("line" + QString::number(j));
235 235 for (int i(0); i < column.count(); i++)
236 236 series->append(i, column.at(i));
237 237 m_chart->addSeries(series);
238 238 }
239 239 } else if (seriesName == "Area") {
240 240 // TODO: lower series for the area?
241 241 for (int j(0); j < data.count(); j ++) {
242 242 QList<qreal> column = data.at(j);
243 243 QLineSeries *lineSeries = new QLineSeries();
244 244 for (int i(0); i < column.count(); i++)
245 245 lineSeries->append(i, column.at(i));
246 246 QAreaSeries *areaSeries = new QAreaSeries(lineSeries);
247 247 areaSeries->setName("area" + QString::number(j));
248 248 m_chart->addSeries(areaSeries);
249 249 }
250 250 } else if (seriesName == "Scatter") {
251 251 for (int j(0); j < data.count(); j++) {
252 252 QList<qreal> column = data.at(j);
253 253 QScatterSeries *series = new QScatterSeries();
254 254 series->setName("scatter" + QString::number(j));
255 255 for (int i(0); i < column.count(); i++)
256 256 series->append(i, column.at(i));
257 257 m_chart->addSeries(series);
258 258 }
259 259 } else if (seriesName == "Pie") {
260 260 QStringList labels = generateLabels(rowCount);
261 261 for (int j(0); j < data.count(); j++) {
262 262 QPieSeries *series = new QPieSeries();
263 263 QList<qreal> column = data.at(j);
264 264 for (int i(0); i < column.count(); i++)
265 265 series->append(labels.at(i), column.at(i));
266 266 m_chart->addSeries(series);
267 267 }
268 268 } else if (seriesName == "Bar"
269 269 || seriesName == "Stacked bar"
270 270 || seriesName == "Percent bar") {
271 271 QStringList category;
272 272 QStringList labels = generateLabels(rowCount);
273 273 foreach(QString label, labels)
274 274 category << label;
275 275 QAbstractBarSeries* series = 0;
276 276 if (seriesName == "Bar") {
277 277 series = new QBarSeries(this);
278 278 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
279 279 axis->append(category);
280 280 m_chart->setAxisX(axis,series);
281 281 } else if (seriesName == "Stacked bar") {
282 282 series = new QStackedBarSeries(this);
283 283 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
284 284 axis->append(category);
285 285 m_chart->setAxisX(axis,series);
286 286 } else {
287 287 series = new QPercentBarSeries(this);
288 288 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
289 289 axis->append(category);
290 290 m_chart->setAxisX(axis,series);
291 291 }
292 292
293 293 for (int j(0); j < data.count(); j++) {
294 294 QList<qreal> column = data.at(j);
295 295 QBarSet *set = new QBarSet("set" + QString::number(j));
296 296 for (int i(0); i < column.count(); i++)
297 297 *set << column.at(i);
298 298 series->append(set);
299 299 }
300 300
301 301 m_chart->addSeries(series);
302 302 } else if (seriesName == "Spline") {
303 303 for (int j(0); j < data.count(); j ++) {
304 304 QList<qreal> column = data.at(j);
305 305 QSplineSeries *series = new QSplineSeries();
306 306 for (int i(0); i < column.count(); i++)
307 307 series->append(i, column.at(i));
308 308 m_chart->addSeries(series);
309 309 }
310 310 }
311 311 m_chart->createDefaultAxes();
312 312 }
313 313
314 314 void MainWidget::backgroundChanged(int itemIndex)
315 315 {
316 316 qDebug() << "backgroundChanged: " << itemIndex;
317 317 }
318 318
319 319 void MainWidget::autoScaleChanged(int value)
320 320 {
321 321 if (value) {
322 322 // TODO: enable auto scaling
323 323 } else {
324 324 // TODO: set scaling manually (and disable auto scaling)
325 325 }
326 326
327 327 m_xMinSpin->setEnabled(!value);
328 328 m_xMaxSpin->setEnabled(!value);
329 329 m_yMinSpin->setEnabled(!value);
330 330 m_yMaxSpin->setEnabled(!value);
331 331 }
332 332
333 333 void MainWidget::xMinChanged(int value)
334 334 {
335 335 qDebug() << "xMinChanged: " << value;
336 336 }
337 337
338 338 void MainWidget::xMaxChanged(int value)
339 339 {
340 340 qDebug() << "xMaxChanged: " << value;
341 341 }
342 342
343 343 void MainWidget::yMinChanged(int value)
344 344 {
345 345 qDebug() << "yMinChanged: " << value;
346 346 }
347 347
348 348 void MainWidget::yMaxChanged(int value)
349 349 {
350 350 qDebug() << "yMaxChanged: " << value;
351 351 }
352 352
353 353 void MainWidget::changeChartTheme(int themeIndex)
354 354 {
355 355 qDebug() << "changeChartTheme: " << themeIndex;
356 356 m_chart->setTheme((QChart::ChartTheme) themeIndex);
357 357 }
General Comments 0
You need to be logged in to leave comments. Login now