##// END OF EJS Templates
new series: groupedbarseries
sauimone -
r1167:494db513d752
parent child
Show More
@@ -0,0 +1,8
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = groupedbarchart
6 SOURCES += main.cpp
7
8 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
@@ -0,0 +1,89
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 <QApplication>
22 #include <QMainWindow>
23 #include <QChartView>
24 #include <QGroupedBarSeries>
25 #include <QBarSet>
26 #include <QLegend>
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
29
30 int main(int argc, char *argv[])
31 {
32 QApplication a(argc, argv);
33
34 //![1]
35 QBarCategories categories;
36 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
37 //![1]
38
39 //![2]
40 QBarSet *set0 = new QBarSet("Jane");
41 QBarSet *set1 = new QBarSet("John");
42 QBarSet *set2 = new QBarSet("Axel");
43 QBarSet *set3 = new QBarSet("Mary");
44 QBarSet *set4 = new QBarSet("Samantha");
45
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
51 //![2]
52
53 //![3]
54 QGroupedBarSeries* series = new QGroupedBarSeries();
55 series->setCategories(categories);
56 series->appendBarSet(set0);
57 series->appendBarSet(set1);
58 series->appendBarSet(set2);
59 series->appendBarSet(set3);
60 series->appendBarSet(set4);
61
62 //![3]
63
64 //![4]
65 QChart* chart = new QChart();
66 chart->addSeries(series);
67 chart->setTitle("Simple grouped barchart example");
68 //![4]
69
70 //![5]
71 chart->legend()->setVisible(true);
72 chart->legend()->setAlignment(QLegend::AlignmentBottom);
73 chart->axisY()->setNiceNumbersEnabled(true);
74 //![5]
75
76 //![6]
77 QChartView* chartView = new QChartView(chart);
78 chartView->setRenderHint(QPainter::Antialiasing);
79 //![6]
80
81 //![7]
82 QMainWindow window;
83 window.setCentralWidget(chartView);
84 window.resize(400, 300);
85 window.show();
86 //![7]
87
88 return a.exec();
89 }
@@ -0,0 +1,87
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 "groupedbarchartitem_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries_p.h"
26 #include "qbarset.h"
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
31 BarChartItem(series, presenter)
32 {
33 }
34
35 QVector<QRectF> GroupedBarChartItem::calculateLayout()
36 {
37 QVector<QRectF> layout;
38
39 // Use temporary qreals for accurancy
40 qreal categoryCount = m_series->categoryCount();
41 qreal setCount = m_series->barsetCount();
42
43 // Domain:
44 qreal width = geometry().width();
45 qreal height = geometry().height();
46 qreal range = m_domainMaxY - m_domainMinY;
47 qreal scale = (height / range);
48 qreal categoryWidth = width / categoryCount;
49 qreal barWidth = categoryWidth / (setCount+1);
50
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
53 qreal xPos = categoryWidth * category + barWidth / 2 + geometry().topLeft().x();
54 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
58 qreal barHeight = barSet->at(category).y() * scale;
59 Bar* bar = m_bars.at(itemIndex);
60
61 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
62 layout.append(rect);
63 bar->setPen(barSet->pen());
64 bar->setBrush(barSet->brush());
65
66 BarLabel* label = m_labels.at(itemIndex);
67
68 if (!qFuzzyIsNull(barSet->at(category).y())) {
69 label->setText(QString::number(barSet->at(category).y()));
70 } else {
71 label->setText(QString(""));
72 }
73
74 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
75 ,yPos - barHeight/2 - label->boundingRect().height()/2);
76 label->setFont(barSet->labelFont());
77
78 itemIndex++;
79 xPos += barWidth;
80 }
81 }
82 return layout;
83 }
84
85 #include "moc_groupedbarchartitem_p.cpp"
86
87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,47
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 GROUPEDBARCHARTITEM_H
22 #define GROUPEDBARCHARTITEM_H
23
24 #include "barchartitem_p.h"
25 #include "qstackedbarseries.h"
26 #include <QGraphicsItem>
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 class GroupedBarChartItem : public BarChartItem
31 {
32 Q_OBJECT
33 public:
34 GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
35
36 private:
37 // From BarChartItem
38 virtual QVector<QRectF> calculateLayout();
39
40 private:
41
42 // Data
43 };
44
45 QTCOMMERCIALCHART_END_NAMESPACE
46
47 #endif // GROUPEDBARCHARTITEM_H
@@ -0,0 +1,108
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 "qgroupedbarseries.h"
22 #include "qgroupedbarseries_p.h"
23 #include "groupedbarchartitem_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30 /*!
31 \class QGroupedBarSeries
32 \brief part of QtCommercial chart API.
33 \mainclass
34
35 QGroupedBarSeries represents a series of data shown as bars. All bars in same category are
36 grouped next to each other. One QGroupedBarSeries can contain multiple QBarSet data sets.
37 QGroupedBarSeries groups the data from sets to categories, which are defined by QStringList.
38
39 See the \l {GroupedbarChart Example} {grouped bar chart example} to learn how to create a grouped bar chart.
40 \image examples_groupedbarchart.png
41
42 \sa QBarSet, QPercentBarSeries, QBarSeries, QStackedBarSeries
43 */
44
45 /*!
46 \fn virtual QSeriesType QGroupedBarSeries::type() const
47 \brief Returns type of series.
48 \sa QSeriesType
49 */
50
51 /*!
52 Constructs empty QGroupedBarSeries. Parameter \a categories defines the categories for chart.
53 QGroupedBarSeries is QObject which is a child of a \a parent.
54 */
55 QGroupedBarSeries::QGroupedBarSeries(QObject *parent)
56 : QBarSeries(*new QGroupedBarSeriesPrivate(this), parent)
57 {
58 }
59
60 QAbstractSeries::SeriesType QGroupedBarSeries::type() const
61 {
62 return QAbstractSeries::SeriesTypeGroupedBar;
63 }
64
65 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66
67 QGroupedBarSeriesPrivate::QGroupedBarSeriesPrivate(/*QBarCategories categories,*/ QGroupedBarSeries *q) : QBarSeriesPrivate(/*categories,*/q)
68 {
69
70 }
71
72 void QGroupedBarSeriesPrivate::scaleDomain(Domain& domain)
73 {
74 qreal minX(domain.minX());
75 qreal minY(domain.minY());
76 qreal maxX(domain.maxX());
77 qreal maxY(domain.maxY());
78 int tickXCount(domain.tickXCount());
79 int tickYCount(domain.tickYCount());
80
81 qreal x = m_categories.count();
82 qreal y = maxCategorySum();
83 minX = qMin(minX, x);
84 minY = qMin(minY, y);
85 maxX = qMax(maxX, x);
86 maxY = qMax(maxY, y);
87 tickXCount = x+1;
88
89 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
90 }
91
92
93 Chart* QGroupedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
94 {
95 Q_Q(QGroupedBarSeries);
96
97 GroupedBarChartItem* bar = new GroupedBarChartItem(q,presenter);
98 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
99 presenter->animator()->addAnimation(bar);
100 }
101 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
102 return bar;
103 }
104
105 #include "moc_qgroupedbarseries.cpp"
106
107 QTCOMMERCIALCHART_END_NAMESPACE
108
@@ -0,0 +1,45
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 GROUPEDBARSERIES_H
22 #define GROUPEDBARSERIES_H
23
24 #include <QStringList>
25 #include <qbarseries.h>
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29 class QGroupedBarSeriesPrivate;
30
31 class QTCOMMERCIALCHART_EXPORT QGroupedBarSeries : public QBarSeries
32 {
33 Q_OBJECT
34 public:
35 explicit QGroupedBarSeries(QObject *parent = 0);
36 QAbstractSeries::SeriesType type() const;
37
38 private:
39 Q_DECLARE_PRIVATE(QGroupedBarSeries)
40 Q_DISABLE_COPY(QGroupedBarSeries)
41 };
42
43 QTCOMMERCIALCHART_END_NAMESPACE
44
45 #endif // GROUPEDBARSERIES_H
@@ -0,0 +1,52
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 QGROUPEDBARSERIES_P_H
31 #define QGROUPEDBARSERIES_P_H
32
33 #include "qbarseries_p.h"
34 #include "domain_p.h"
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
38
39 class QGroupedBarSeriesPrivate: public QBarSeriesPrivate
40 {
41 public:
42 QGroupedBarSeriesPrivate(QGroupedBarSeries* q);
43 Chart* createGraphics(ChartPresenter* presenter);
44 void scaleDomain(Domain& domain);
45
46 private:
47 Q_DECLARE_PUBLIC(QGroupedBarSeries)
48 };
49
50 QTCOMMERCIALCHART_END_NAMESPACE
51
52 #endif // QGROUPEDBARSERIES_P_H
@@ -43,11 +43,11 int main(int argc, char *argv[])
43 43 QBarSet *set3 = new QBarSet("Mary");
44 44 QBarSet *set4 = new QBarSet("Samantha");
45 45
46 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
47 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
48 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
49 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
50 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
46 *set0 << QPointF(0.0, 1) << QPointF(1.0, 2) << QPointF(2.0, 3) << QPointF(3.0, 4) << QPointF(4.0, 5) << QPointF(5.0, 6);
47 *set1 << QPointF(0.2, 2) << QPointF(1.2, 3) << QPointF(2.2, 4) << QPointF(3.2, 5) << QPointF(4.2, 6) << QPointF(5.2, 7);
48 *set2 << QPointF(0.4, 3) << QPointF(1.4, 4) << QPointF(2.4, 5) << QPointF(3.4, 6) << QPointF(4.4, 7) << QPointF(5.4, 8);
49 *set3 << QPointF(0.6, 4) << QPointF(1.6, 5) << QPointF(2.6, 6) << QPointF(3.6, 7) << QPointF(4.6, 8) << QPointF(5.6, 9);
50 *set4 << QPointF(0.8, 5) << QPointF(1.8, 6) << QPointF(2.8, 7) << QPointF(3.8, 8) << QPointF(4.8, 9) << QPointF(5.8, 10);
51 51 //![2]
52 52
53 53 //![3]
@@ -58,6 +58,7 int main(int argc, char *argv[])
58 58 series->appendBarSet(set2);
59 59 series->appendBarSet(set3);
60 60 series->appendBarSet(set4);
61
61 62 //![3]
62 63
63 64 //![4]
@@ -19,4 +19,5 SUBDIRS += \
19 19 stackedbarchart \
20 20 stackedbarchartdrilldown \
21 21 zoomlinechart \
22 modeldata
22 modeldata \
23 groupedbarchart
@@ -5,27 +5,32 SOURCES += \
5 5 $$PWD/bar.cpp \
6 6 $$PWD/barchartitem.cpp \
7 7 $$PWD/percentbarchartitem.cpp \
8 $$PWD/groupedbarchartitem.cpp \
8 9 $$PWD/qbarseries.cpp \
9 10 $$PWD/qbarset.cpp \
10 11 $$PWD/qpercentbarseries.cpp \
11 12 $$PWD/qstackedbarseries.cpp \
13 $$PWD/qgroupedbarseries.cpp \
12 14 $$PWD/stackedbarchartitem.cpp \
13 $$PWD/barlabel.cpp
15 $$PWD/barlabel.cpp \
14 16
15 17 PRIVATE_HEADERS += \
16 18 $$PWD/bar_p.h \
17 19 $$PWD/barchartitem_p.h \
18 20 $$PWD/percentbarchartitem_p.h \
19 21 $$PWD/stackedbarchartitem_p.h \
22 $$PWD/groupedbarchartitem_p.h \
20 23 $$PWD/barlabel_p.h \
21 24 $$PWD/qbarset_p.h \
22 25 $$PWD/qbarseries_p.h \
23 26 $$PWD/qstackedbarseries_p.h\
24 $$PWD/qpercentbarseries_p.h
27 $$PWD/qpercentbarseries_p.h \
28 $$PWD/qgroupedbarseries_p.h \
25 29
26 30 PUBLIC_HEADERS += \
27 31 $$PWD/qbarseries.h \
28 32 $$PWD/qbarset.h \
29 33 $$PWD/qpercentbarseries.h \
30 $$PWD/qstackedbarseries.h
34 $$PWD/qstackedbarseries.h \
35 $$PWD/qgroupedbarseries.h
31 36
@@ -111,30 +111,32 QVector<QRectF> BarChartItem::calculateLayout()
111 111 // Domain:
112 112 qreal width = geometry().width();
113 113 qreal height = geometry().height();
114 qreal range = m_domainMaxY - m_domainMinY;
115 qreal scale = (height / range);
114 qreal rangeY = m_domainMaxY - m_domainMinY;
115 qreal rangeX = m_domainMaxX - m_domainMinX;
116 qreal scaleY = (height / rangeY);
117 qreal scaleX = (width / rangeX);
116 118 qreal categoryWidth = width / categoryCount;
117 qreal barWidth = categoryWidth / (setCount+1);
119 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
118 120
119 121 int itemIndex(0);
120 122 for (int category = 0; category < categoryCount; category++) {
121 qreal xPos = categoryWidth * category + barWidth / 2 + geometry().topLeft().x();
122 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
123 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
123 124 for (int set = 0; set < setCount; set++) {
124 125 QBarSet* barSet = m_series->d_func()->barsetAt(set);
126 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
127 qreal barHeight = barSet->at(category).y() * scaleY;
125 128
126 qreal barHeight = barSet->at(category) * scale;
127 129 Bar* bar = m_bars.at(itemIndex);
128
129 130 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
131
130 132 layout.append(rect);
131 133 bar->setPen(barSet->pen());
132 134 bar->setBrush(barSet->brush());
133 135
134 136 BarLabel* label = m_labels.at(itemIndex);
135 137
136 if (!qFuzzyIsNull(barSet->at(category))) {
137 label->setText(QString::number(barSet->at(category)));
138 if (!qFuzzyIsNull(barSet->at(category).y())) {
139 label->setText(QString::number(barSet->at(category).y()));
138 140 } else {
139 141 label->setText(QString(""));
140 142 }
@@ -144,9 +146,9 QVector<QRectF> BarChartItem::calculateLayout()
144 146 label->setFont(barSet->labelFont());
145 147
146 148 itemIndex++;
147 xPos += barWidth;
148 149 }
149 150 }
151
150 152 return layout;
151 153 }
152 154
@@ -55,7 +55,7 QVector<QRectF> PercentBarChartItem::calculateLayout()
55 55 qreal yPos = height + domainScale * m_domainMinY + geometry().topLeft().y();
56 56 for (int set=0; set < m_series->barsetCount(); set++) {
57 57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 qreal barHeight = barSet->at(category) * percentage * domainScale;
58 qreal barHeight = barSet->at(category).y() * percentage * domainScale;
59 59 Bar* bar = m_bars.at(itemIndex);
60 60 bar->setPen(barSet->pen());
61 61 bar->setBrush(barSet->brush());
@@ -260,9 +260,9 void QBarSeries::setLabelsVisible(bool visible)
260 260
261 261 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
262 262
263 QBarSeriesPrivate::QBarSeriesPrivate(/*QBarCategories categories,*/ QBarSeries *q) :
263 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
264 264 QAbstractSeriesPrivate(q),
265 // m_categories(categories),
265 m_barMargin(0.05), // Default value is 5% of category width
266 266 m_mapCategories(-1),
267 267 m_mapBarBottom(-1),
268 268 m_mapBarTop(-1)
@@ -274,6 +274,22 void QBarSeriesPrivate::setCategories(QBarCategories categories)
274 274 m_categories = categories;
275 275 }
276 276
277 void QBarSeriesPrivate::setBarMargin(qreal margin)
278 {
279 if (margin > 1.0) {
280 margin = 1.0;
281 } else if (margin < 0.0) {
282 margin = 0.0;
283 }
284
285 m_barMargin = margin;
286 emit updatedBars();
287 }
288
289 qreal QBarSeriesPrivate::barMargin()
290 {
291 return m_barMargin;
292 }
277 293
278 294 QBarSet* QBarSeriesPrivate::barsetAt(int index)
279 295 {
@@ -295,7 +311,7 qreal QBarSeriesPrivate::min()
295 311 for (int i = 0; i < m_barSets.count(); i++) {
296 312 int categoryCount = m_barSets.at(i)->count();
297 313 for (int j = 0; j < categoryCount; j++) {
298 qreal temp = m_barSets.at(i)->at(j);
314 qreal temp = m_barSets.at(i)->at(j).y();
299 315 if (temp < min)
300 316 min = temp;
301 317 }
@@ -313,7 +329,7 qreal QBarSeriesPrivate::max()
313 329 for (int i = 0; i < m_barSets.count(); i++) {
314 330 int categoryCount = m_barSets.at(i)->count();
315 331 for (int j = 0; j < categoryCount; j++) {
316 qreal temp = m_barSets.at(i)->at(j);
332 qreal temp = m_barSets.at(i)->at(j).y();
317 333 if (temp > max)
318 334 max = temp;
319 335 }
@@ -332,7 +348,7 qreal QBarSeriesPrivate::valueAt(int set, int category)
332 348 return 0;
333 349 }
334 350
335 return m_barSets.at(set)->at(category);
351 return m_barSets.at(set)->at(category).y();
336 352 }
337 353
338 354 qreal QBarSeriesPrivate::percentageAt(int set, int category)
@@ -345,7 +361,7 qreal QBarSeriesPrivate::percentageAt(int set, int category)
345 361 return 0;
346 362 }
347 363
348 qreal value = m_barSets.at(set)->at(category);
364 qreal value = m_barSets.at(set)->at(category).y();
349 365 qreal sum = categorySum(category);
350 366 if ( qFuzzyIsNull(sum) ) {
351 367 return 0;
@@ -360,7 +376,7 qreal QBarSeriesPrivate::categorySum(int category)
360 376 int count = m_barSets.count(); // Count sets
361 377 for (int set = 0; set < count; set++) {
362 378 if (category < m_barSets.at(set)->count())
363 sum += m_barSets.at(set)->at(category);
379 sum += m_barSets.at(set)->at(category).y();
364 380 }
365 381 return sum;
366 382 }
@@ -371,7 +387,7 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
371 387 int count = m_barSets.count(); // Count sets
372 388 for (int set = 0; set < count; set++) {
373 389 if (category < m_barSets.at(set)->count())
374 sum += qAbs(m_barSets.at(set)->at(category));
390 sum += qAbs(m_barSets.at(set)->at(category).y());
375 391 }
376 392 return sum;
377 393 }
@@ -39,7 +39,7 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
39 39 {
40 40 Q_OBJECT
41 41 public:
42 explicit QBarSeries(/*QBarCategories categories,*/ QObject *parent = 0);
42 explicit QBarSeries(QObject *parent = 0);
43 43 virtual ~QBarSeries();
44 44
45 45 QAbstractSeries::SeriesType type() const;
@@ -55,9 +55,6 public:
55 55 QBarCategories categories() const;
56 56
57 57 void setLabelsVisible(bool visible = true);
58 // TODO:
59 // void setGroupedDrawing(bool on = true); // By default this is on. Bars are grouped next to each other. If off, bars are drawn at their x-position (propably on top of each other)
60 // void setBarMargin(int margin); // Margin that is left between bars (if drawn as grouped bars)
61 58
62 59 void setModel(QAbstractItemModel *model);
63 60 // void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
@@ -75,6 +72,7 protected:
75 72 friend class BarChartItem;
76 73 friend class PercentBarChartItem;
77 74 friend class StackedBarChartItem;
75 friend class GroupedBarChartItem;
78 76 };
79 77
80 78 QTCOMMERCIALCHART_END_NAMESPACE
@@ -15,8 +15,10 class QBarSeriesPrivate : public QAbstractSeriesPrivate
15 15 {
16 16 Q_OBJECT
17 17 public:
18 QBarSeriesPrivate(/*QBarCategories categories,*/ QBarSeries *parent);
18 QBarSeriesPrivate(QBarSeries *parent);
19 19 void setCategories(QBarCategories categories);
20 void setBarMargin(qreal margin);
21 qreal barMargin();
20 22
21 23 void scaleDomain(Domain& domain);
22 24 Chart* createGraphics(ChartPresenter* presenter);
@@ -57,6 +59,7 protected:
57 59 QList<QBarSet *> m_barSets;
58 60 QBarCategories m_categories;
59 61
62 qreal m_barMargin;
60 63 int m_mapCategories;
61 64 int m_mapBarBottom;
62 65 int m_mapBarTop;
@@ -84,12 +84,38 QString QBarSet::name() const
84 84 return d_ptr->m_name;
85 85 }
86 86
87 void QBarSet::append(const QPointF value)
88 {
89 d_ptr->m_values.append(value);
90 emit d_ptr->restructuredBars();
91 }
92
93
94 void QBarSet::append(const QList<QPointF> values)
95 {
96 for (int i=0; i<values.count(); i++) {
97 d_ptr->m_values.append(values.at(i));
98 }
99 emit d_ptr->restructuredBars();
100 }
101
87 102 /*!
88 103 Appends new value \a value to the end of set.
89 104 */
90 105 void QBarSet::append(const qreal value)
91 106 {
92 d_ptr->m_values.append(value);
107 append(QPointF(d_ptr->m_values.count(), value));
108 // d_ptr->m_values.append(value);
109 }
110
111
112 void QBarSet::append(const QList<qreal> values)
113 {
114 int index = d_ptr->m_values.count();
115 for (int i=0; i<values.count(); i++) {
116 d_ptr->m_values.append(QPointF(index,values.at(i)));
117 index++;
118 }
93 119 emit d_ptr->restructuredBars();
94 120 }
95 121
@@ -102,6 +128,12 QBarSet& QBarSet::operator << (const qreal &value)
102 128 return *this;
103 129 }
104 130
131 QBarSet& QBarSet::operator << (const QPointF &value)
132 {
133 append(value);
134 return *this;
135 }
136
105 137 /*!
106 138 Inserts new \a value on the \a index position.
107 139 The value that is currently at this postion is moved to postion index + 1
@@ -109,7 +141,7 QBarSet& QBarSet::operator << (const qreal &value)
109 141 */
110 142 void QBarSet::insert(const int index, const qreal value)
111 143 {
112 d_ptr->m_values.insert(index, value);
144 d_ptr->m_values.insert(index, QPointF(index, value));
113 145 // emit d_ptr->updatedBars();
114 146 }
115 147
@@ -128,14 +160,14 void QBarSet::remove(const int index)
128 160 */
129 161 void QBarSet::replace(const int index, const qreal value)
130 162 {
131 d_ptr->m_values.replace(index,value);
163 d_ptr->m_values.replace(index,QPointF(index,value));
132 164 emit d_ptr->updatedBars();
133 165 }
134 166
135 167 /*!
136 Returns value of set indexed by \a index
168 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF
137 169 */
138 qreal QBarSet::at(const int index) const
170 QPointF QBarSet::at(const int index) const
139 171 {
140 172 if (index < 0 || index >= d_ptr->m_values.count())
141 173 return 0.0;
@@ -146,7 +178,7 qreal QBarSet::at(const int index) const
146 178 /*!
147 179 Returns value of set indexed by \a index
148 180 */
149 qreal QBarSet::operator [] (int index) const
181 QPointF QBarSet::operator [](const int index) const
150 182 {
151 183 return d_ptr->m_values.at(index);
152 184 }
@@ -166,7 +198,8 qreal QBarSet::sum() const
166 198 {
167 199 qreal total(0);
168 200 for (int i=0; i < d_ptr->m_values.count(); i++) {
169 total += d_ptr->m_values.at(i);
201 //total += d_ptr->m_values.at(i);
202 total += d_ptr->m_values.at(i).y();
170 203 }
171 204 return total;
172 205 }
@@ -41,23 +41,19 public:
41 41 void setName(const QString name);
42 42 QString name() const;
43 43
44 // TODO:
45 // void append(const QPointF value); // Appends bar with x-value
46 // void append(const QList<QPointF> value); // Same with list
44 void append(const QPointF value); // Appends bar with x-value
45 void append(const QList<QPointF> values); // Same with list
47 46 void append(const qreal value); // TODO: change so, that count becomes x-value
48
49 // TODO:
50 // void append(const QList<qreal> values); // Append list of values. Using index as x-value
47 void append(const QList<qreal> values); // Append list of values. Using index as x-value
51 48
52 49 QBarSet& operator << (const qreal &value); // TODO: change implementations so, that count becomes x-value
53 // TODO:
54 // QBarSet& operator << (const QPointF &value); // Appends bar with x-value
50 QBarSet& operator << (const QPointF &value); // Appends bar with x-value
55 51
56 52 void insert(const int index, const qreal value); // TODO: internal reindexing (what happens, if there are points with real x values?)
57 53 void remove(const int index); // TODO: internal reindexing (what happens, if there are points with real x values?)
58 54 void replace(const int index, const qreal value);
59 qreal at(const int index) const;
60 qreal operator [] (int index) const;
55 QPointF at(const int index) const;
56 QPointF operator [] (const int index) const;
61 57 int count() const;
62 58 qreal sum() const;
63 59
@@ -23,8 +23,7 Q_SIGNALS:
23 23 public:
24 24 QBarSet * const q_ptr;
25 25 QString m_name;
26 QList<qreal> m_values; // TODO: replace with map (category, value)
27 QMap<QString, qreal> m_mappedValues;
26 QList<QPointF> m_values;
28 27 QPen m_pen;
29 28 QBrush m_brush;
30 29 QPen m_labelPen;
@@ -53,7 +53,7 QVector<QRectF> StackedBarChartItem::calculateLayout()
53 53 for (int set=0; set < m_series->barsetCount(); set++) {
54 54 QBarSet* barSet = m_series->d_func()->barsetAt(set);
55 55
56 qreal barHeight = barSet->at(category) * scale;
56 qreal barHeight = barSet->at(category).y() * scale;
57 57 Bar* bar = m_bars.at(itemIndex);
58 58 bar->setPen(barSet->pen());
59 59 bar->setBrush(barSet->brush());
@@ -62,8 +62,8 QVector<QRectF> StackedBarChartItem::calculateLayout()
62 62
63 63 BarLabel* label = m_labels.at(itemIndex);
64 64
65 if (!qFuzzyIsNull(barSet->at(category))) {
66 label->setText(QString::number(barSet->at(category)));
65 if (!qFuzzyIsNull(barSet->at(category).y())) {
66 label->setText(QString::number(barSet->at(category).y()));
67 67 } else {
68 68 label->setText(QString(""));
69 69 }
@@ -88,7 +88,7 void ChartDataSet::addSeries(QAbstractSeries* series, QAxis *axisY)
88 88
89 89 series->d_ptr->scaleDomain(*domain);
90 90
91 if(series->type() == QAbstractSeries::SeriesTypeBar
91 if(series->type() == QAbstractSeries::SeriesTypeGroupedBar
92 92 || series->type() == QAbstractSeries::SeriesTypeStackedBar
93 93 || series->type() == QAbstractSeries::SeriesTypePercentBar) {
94 94 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
@@ -46,6 +46,7 public:
46 46 SeriesTypeBar,
47 47 SeriesTypeStackedBar,
48 48 SeriesTypePercentBar,
49 SeriesTypeGroupedBar,
49 50 SeriesTypePie,
50 51 SeriesTypeScatter,
51 52 SeriesTypeSpline
@@ -140,7 +140,7 void tst_QBarSet::append()
140 140
141 141 for (int i=0; i<count; i++) {
142 142 m_barset->append(value);
143 QCOMPARE(m_barset->at(i), value);
143 QCOMPARE(m_barset->at(i).y(), value);
144 144 sum += value;
145 145 value += 1.0;
146 146 }
@@ -166,7 +166,7 void tst_QBarSet::appendOperator()
166 166
167 167 for (int i=0; i<count; i++) {
168 168 *m_barset << value;
169 QCOMPARE(m_barset->at(i), value);
169 QCOMPARE(m_barset->at(i).y(), value);
170 170 sum += value;
171 171 value += 1.0;
172 172 }
@@ -185,20 +185,20 void tst_QBarSet::insert()
185 185 QVERIFY(qFuzzyIsNull(m_barset->sum()));
186 186
187 187 m_barset->insert(0, 1.0); // 1.0
188 QCOMPARE(m_barset->at(0), 1.0);
188 QCOMPARE(m_barset->at(0).y(), 1.0);
189 189 QCOMPARE(m_barset->count(), 1);
190 190 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
191 191
192 192 m_barset->insert(0, 2.0); // 2.0 1.0
193 QCOMPARE(m_barset->at(0), 2.0);
194 QCOMPARE(m_barset->at(1), 1.0);
193 QCOMPARE(m_barset->at(0).y(), 2.0);
194 QCOMPARE(m_barset->at(1).y(), 1.0);
195 195 QCOMPARE(m_barset->count(), 2);
196 196 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
197 197
198 198 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
199 QCOMPARE(m_barset->at(1), 3.0);
200 QCOMPARE(m_barset->at(0), 2.0);
201 QCOMPARE(m_barset->at(2), 1.0);
199 QCOMPARE(m_barset->at(1).y(), 3.0);
200 QCOMPARE(m_barset->at(0).y(), 2.0);
201 QCOMPARE(m_barset->at(2).y(), 1.0);
202 202 QCOMPARE(m_barset->count(), 3);
203 203 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
204 204 }
@@ -221,15 +221,15 void tst_QBarSet::remove()
221 221 QCOMPARE(m_barset->sum(), 10.0);
222 222
223 223 m_barset->remove(2); // 1.0 2.0 4.0
224 QCOMPARE(m_barset->at(0), 1.0);
225 QCOMPARE(m_barset->at(1), 2.0);
226 QCOMPARE(m_barset->at(2), 4.0);
224 QCOMPARE(m_barset->at(0).y(), 1.0);
225 QCOMPARE(m_barset->at(1).y(), 2.0);
226 QCOMPARE(m_barset->at(2).y(), 4.0);
227 227 QCOMPARE(m_barset->count(), 3);
228 228 QCOMPARE(m_barset->sum(), 7.0);
229 229
230 230 m_barset->remove(0); // 2.0 4.0
231 QCOMPARE(m_barset->at(0), 2.0);
232 QCOMPARE(m_barset->at(1), 4.0);
231 QCOMPARE(m_barset->at(0).y(), 2.0);
232 QCOMPARE(m_barset->at(1).y(), 4.0);
233 233 QCOMPARE(m_barset->count(), 2);
234 234 QCOMPARE(m_barset->sum(), 6.0);
235 235 }
@@ -255,15 +255,15 void tst_QBarSet::replace()
255 255 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
256 256 QCOMPARE(m_barset->count(), 4);
257 257 QCOMPARE(m_barset->sum(), 14.0);
258 QCOMPARE(m_barset->at(0), 5.0);
258 QCOMPARE(m_barset->at(0).y(), 5.0);
259 259
260 260 m_barset->replace(3, 6.0);
261 261 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
262 262 QCOMPARE(m_barset->sum(), 16.0);
263 QCOMPARE(m_barset->at(0), 5.0);
264 QCOMPARE(m_barset->at(1), 2.0);
265 QCOMPARE(m_barset->at(2), 3.0);
266 QCOMPARE(m_barset->at(3), 6.0);
263 QCOMPARE(m_barset->at(0).y(), 5.0);
264 QCOMPARE(m_barset->at(1).y(), 2.0);
265 QCOMPARE(m_barset->at(2).y(), 3.0);
266 QCOMPARE(m_barset->at(3).y(), 6.0);
267 267 }
268 268
269 269 void tst_QBarSet::at_data()
@@ -281,10 +281,10 void tst_QBarSet::at()
281 281 m_barset->append(3.0);
282 282 m_barset->append(4.0);
283 283
284 QCOMPARE(m_barset->at(0), 1.0);
285 QCOMPARE(m_barset->at(1), 2.0);
286 QCOMPARE(m_barset->at(2), 3.0);
287 QCOMPARE(m_barset->at(3), 4.0);
284 QCOMPARE(m_barset->at(0).y(), 1.0);
285 QCOMPARE(m_barset->at(1).y(), 2.0);
286 QCOMPARE(m_barset->at(2).y(), 3.0);
287 QCOMPARE(m_barset->at(3).y(), 4.0);
288 288 }
289 289
290 290 void tst_QBarSet::atOperator_data()
@@ -302,10 +302,10 void tst_QBarSet::atOperator()
302 302 m_barset->append(3.0);
303 303 m_barset->append(4.0);
304 304
305 QCOMPARE(m_barset->operator [](0), 1.0);
306 QCOMPARE(m_barset->operator [](1), 2.0);
307 QCOMPARE(m_barset->operator [](2), 3.0);
308 QCOMPARE(m_barset->operator [](3), 4.0);
305 QCOMPARE(m_barset->operator [](0).y(), 1.0);
306 QCOMPARE(m_barset->operator [](1).y(), 2.0);
307 QCOMPARE(m_barset->operator [](2).y(), 3.0);
308 QCOMPARE(m_barset->operator [](3).y(), 4.0);
309 309 }
310 310
311 311 void tst_QBarSet::count_data()
General Comments 0
You need to be logged in to leave comments. Login now