##// END OF EJS Templates
population pyramid example. Added grouping option to barseries to give some control over layout
sauimone -
r1794:efd4a5fe5972
parent child
Show More
@@ -0,0 +1,93
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 <QBarSeries>
25 #include <QBarSet>
26 #include <QLegend>
27 #include <QBarCategoriesAxis>
28 #include <QHorizontalBarSeries>
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
32 int main(int argc, char *argv[])
33 {
34 QApplication a(argc, argv);
35
36 //![1]
37 QBarSet *male = new QBarSet("Male");
38 QBarSet *female = new QBarSet("Female");
39
40 // From wikipedia
41 *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815
42 << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769;
43
44 *female << 139353 << 143522 << 161212 << 156824 << 163226 << 161766 << 150595 << 170779 << 185979 << 187897 << 196158
45 << 209062 << 152460 << 129230 << 114778 << 109611 << 83845 << 44605 << 22328;
46
47 //![1]
48
49 //![2]
50 QHorizontalBarSeries *series = new QHorizontalBarSeries();
51 series->append(male);
52 series->append(female);
53 series->setBarWidth(0.5);
54 series->setGrouping(false);
55 //![2]
56
57 //![3]
58 QChart* chart = new QChart();
59 chart->addSeries(series);
60 chart->setTitle("Population of Finland in 2005 by age group");
61 chart->createDefaultAxes();
62 chart->setAnimationOptions(QChart::SeriesAnimations);
63 //![3]
64
65 //![4]
66 QStringList categories;
67 categories << "0-4" << "5-9" << "10-14" << "15-19" << "20-24" << "25-29" << "30-34" << "35-39" << "40-44" << "45-49"
68 << "50-54" << "55-59" << "60-64" << "65-69" << "70-74" << "75-79" << "80-84" << "85-89" << "90-";
69
70 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
71 axis->append(categories);
72 chart->setAxisY(axis,series);
73 //![4]
74
75 //![5]
76 chart->legend()->setVisible(true);
77 chart->legend()->setAlignment(Qt::AlignBottom);
78 //![5]
79
80 //![6]
81 QChartView* chartView = new QChartView(chart);
82 chartView->setRenderHint(QPainter::Antialiasing);
83 //![6]
84
85 //![7]
86 QMainWindow window;
87 window.setCentralWidget(chartView);
88 window.resize(400, 800);
89 window.show();
90 //![7]
91
92 return a.exec();
93 }
@@ -0,0 +1,6
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = populationpyramid
6 SOURCES += main.cpp
@@ -1,33 +1,34
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 !include( ../config.pri ) {
2 !include( ../config.pri ) {
3 error( "Couldn't find the config.pri file!" )
3 error( "Couldn't find the config.pri file!" )
4 }
4 }
5
5
6 TEMPLATE = subdirs
6 TEMPLATE = subdirs
7 SUBDIRS += \
7 SUBDIRS += \
8 areachart \
8 areachart \
9 customchart \
9 customchart \
10 linechart \
10 linechart \
11 percentbarchart \
11 percentbarchart \
12 piechart \
12 piechart \
13 piechartdrilldown \
13 piechartdrilldown \
14 presenterchart \
14 presenterchart \
15 scatterchart \
15 scatterchart \
16 scatterinteractions \
16 scatterinteractions \
17 splinechart \
17 splinechart \
18 stackedbarchart \
18 stackedbarchart \
19 stackedbarchartdrilldown \
19 stackedbarchartdrilldown \
20 zoomlinechart \
20 zoomlinechart \
21 modeldata \
21 modeldata \
22 barchart \
22 barchart \
23 legend \
23 legend \
24 barmodelmapper \
24 barmodelmapper \
25 qmlpiechart \
25 qmlpiechart \
26 lineandbar \
26 lineandbar \
27 horizontalbarchart \
27 horizontalbarchart \
28 horizontalstackedbarchart \
28 horizontalstackedbarchart \
29 horizontalpercentbarchart \
29 horizontalpercentbarchart \
30 donut \
30 donut \
31 donutbreakdown \
31 donutbreakdown \
32 scrollchart \
32 scrollchart \
33 datetimeaxis
33 datetimeaxis \
34 populationpyramid
@@ -1,92 +1,91
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
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
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30
30
31 #ifndef ABSTRACTBARCHARTITEM_H
31 #ifndef ABSTRACTBARCHARTITEM_H
32 #define ABSTRACTBARCHARTITEM_H
32 #define ABSTRACTBARCHARTITEM_H
33
33
34 #include "chartitem_p.h"
34 #include "chartitem_p.h"
35 #include "qabstractbarseries.h"
35 #include "qabstractbarseries.h"
36 #include <QPen>
36 #include <QPen>
37 #include <QBrush>
37 #include <QBrush>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class Bar;
41 class Bar;
42 class QAxisCategories;
42 class QAxisCategories;
43 class QChart;
43 class QChart;
44 class AbstractBarAnimation;
44 class AbstractBarAnimation;
45
45
46 class AbstractBarChartItem : public ChartItem
46 class AbstractBarChartItem : public ChartItem
47 {
47 {
48 Q_OBJECT
48 Q_OBJECT
49 public:
49 public:
50 AbstractBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter);
50 AbstractBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter);
51 virtual ~AbstractBarChartItem();
51 virtual ~AbstractBarChartItem();
52
52
53 public:
53 public:
54 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
54 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
55 QRectF boundingRect() const;
55 QRectF boundingRect() const;
56
56
57 virtual QVector<QRectF> calculateLayout() = 0;
57 virtual QVector<QRectF> calculateLayout() = 0;
58 virtual void applyLayout(const QVector<QRectF> &layout);
58 virtual void applyLayout(const QVector<QRectF> &layout);
59 virtual void setAnimation(AbstractBarAnimation* animation);
59 virtual void setAnimation(AbstractBarAnimation* animation);
60 void setLayout(const QVector<QRectF> &layout);
60 void setLayout(const QVector<QRectF> &layout);
61 void updateLayout(const QVector<QRectF> &layout);
61 void updateLayout(const QVector<QRectF> &layout);
62 QRectF geometry() const { return m_rect;}
62 QRectF geometry() const { return m_rect;}
63
63
64 public Q_SLOTS:
64 public Q_SLOTS:
65 void handleDomainUpdated();
65 void handleDomainUpdated();
66 void handleGeometryChanged(const QRectF &size);
66 void handleGeometryChanged(const QRectF &size);
67 void handleLayoutChanged();
67 void handleLayoutChanged();
68 void handleLabelsVisibleChanged(bool visible);
68 void handleLabelsVisibleChanged(bool visible);
69 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
69 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
70 void handleVisibleChanged();
70 void handleVisibleChanged();
71
71
72 protected:
72 protected:
73
73
74 qreal m_domainMinX;
74 qreal m_domainMinX;
75 qreal m_domainMaxX;
75 qreal m_domainMaxX;
76 qreal m_domainMinY;
76 qreal m_domainMinY;
77 qreal m_domainMaxY;
77 qreal m_domainMaxY;
78
78
79 QRectF m_rect;
79 QRectF m_rect;
80 QVector<QRectF> m_layout;
80 QVector<QRectF> m_layout;
81
81
82 AbstractBarAnimation *m_animation;
82 AbstractBarAnimation *m_animation;
83
83
84 // Not owned.
84 QAbstractBarSeries *m_series; // Not owned.
85 QAbstractBarSeries *m_series;
86 QList<Bar *> m_bars;
85 QList<Bar *> m_bars;
87 QList<QGraphicsSimpleTextItem *> m_labels;
86 QList<QGraphicsSimpleTextItem *> m_labels;
88 };
87 };
89
88
90 QTCOMMERCIALCHART_END_NAMESPACE
89 QTCOMMERCIALCHART_END_NAMESPACE
91
90
92 #endif // ABSTRACTBARCHARTITEM_H
91 #endif // ABSTRACTBARCHARTITEM_H
@@ -1,95 +1,106
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "horizontalbarchartitem_p.h"
21 #include "horizontalbarchartitem_p.h"
22 #include "qabstractbarseries_p.h"
22 #include "qabstractbarseries_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "bar_p.h"
24 #include "bar_p.h"
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) :
28 HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) :
29 AbstractBarChartItem(series, presenter)
29 AbstractBarChartItem(series, presenter)
30 {
30 {
31 }
31 }
32
32
33 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
33 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
34 {
34 {
35 QVector<QRectF> layout;
35 QVector<QRectF> layout;
36
36
37 // Use temporary qreals for accuracy
37 // Use temporary qreals for accuracy
38 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal setCount = m_series->count();
39 qreal setCount = m_series->count();
40 bool barsVisible = m_series->isVisible();
40 bool barsVisible = m_series->isVisible();
41
41
42 // Domain:
42 // Domain:
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal height = geometry().height();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
48 qreal scaleX = (width / rangeX);
49 qreal barHeight = (scaleY / setCount) * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
49 qreal barHeight;
50
51 // On horizontal chart barWidth of the barseries means height of the rect.
52 if (m_series->d_func()->m_grouping) {
53 barHeight = (scaleY / setCount) * m_series->d_func()->barWidth();
54 } else {
55 barHeight = scaleY * m_series->d_func()->barWidth();
56 }
50
57
51 int itemIndex(0);
58 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
59 for (int category = 0; category < categoryCount; category++) {
53 qreal xPos = -scaleX * m_domainMinX + geometry().left();
60 qreal xPos = -scaleX * m_domainMinX + geometry().left();
54 for (int set = 0; set < setCount; set++) {
61 for (int set = 0; set < setCount; set++) {
55 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
62 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56
63
57 qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
64 qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
58 yPos += setCount*barHeight/2;
65 if (m_series->d_func()->m_grouping) {
59 yPos -= set*barHeight;
66 yPos += setCount*barHeight/2;
67 yPos -= set*barHeight;
68 } else {
69 yPos += barHeight/2;
70 }
60
71
61 qreal barWidth = barSet->value(category) * scaleX;
72 qreal barWidth = barSet->value(category) * scaleX;
62 Bar* bar = m_bars.at(itemIndex);
73 Bar* bar = m_bars.at(itemIndex);
63
74
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
75 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 layout.append(rect);
76 layout.append(rect);
66 bar->setPen(barSet->m_pen);
77 bar->setPen(barSet->m_pen);
67 bar->setBrush(barSet->m_brush);
78 bar->setBrush(barSet->m_brush);
68 if (qFuzzyIsNull(barHeight)) {
79 if (qFuzzyIsNull(barHeight)) {
69 bar->setVisible(false);
80 bar->setVisible(false);
70 } else {
81 } else {
71 bar->setVisible(barsVisible);
82 bar->setVisible(barsVisible);
72 }
83 }
73
84
74 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
85 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
75
86
76 if (!qFuzzyIsNull(barSet->value(category))) {
87 if (!qFuzzyIsNull(barSet->value(category))) {
77 label->setText(QString::number(barSet->value(category)));
88 label->setText(QString::number(barSet->value(category)));
78 } else {
89 } else {
79 label->setText(QString(""));
90 label->setText(QString(""));
80 }
91 }
81
92
82 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
93 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
83 ,yPos - barHeight/2 - label->boundingRect().height()/2);
94 ,yPos - barHeight/2 - label->boundingRect().height()/2);
84 label->setFont(barSet->m_labelFont);
95 label->setFont(barSet->m_labelFont);
85 label->setBrush(barSet->m_labelBrush);
96 label->setBrush(barSet->m_labelBrush);
86
97
87 itemIndex++;
98 itemIndex++;
88 }
99 }
89 }
100 }
90 return layout;
101 return layout;
91 }
102 }
92
103
93 #include "moc_horizontalbarchartitem_p.cpp"
104 #include "moc_horizontalbarchartitem_p.cpp"
94
105
95 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,784 +1,801
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qabstractbarseries.h"
21 #include "qabstractbarseries.h"
22 #include "qabstractbarseries_p.h"
22 #include "qabstractbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "qvaluesaxis.h"
29 #include "qvaluesaxis.h"
30 #include "qbarcategoriesaxis.h"
30 #include "qbarcategoriesaxis.h"
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 /*!
34 /*!
35 \class QAbstractBarSeries
35 \class QAbstractBarSeries
36 \brief Series for creating a bar chart
36 \brief Series for creating a bar chart
37 \mainclass
37 \mainclass
38
38
39 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
40 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
41 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 and y-value is the height of the bar. The category names are ignored with this series and x-axis
42 shows the x-values.
42 shows the x-values.
43
43
44 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
45 \image examples_barchart.png
45 \image examples_barchart.png
46
46
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 */
48 */
49 /*!
49 /*!
50 \qmlclass AbstractBarSeries QAbstractBarSeries
50 \qmlclass AbstractBarSeries QAbstractBarSeries
51 \inherits QAbstractSeries
51 \inherits QAbstractSeries
52
52
53 The following QML shows how to create a simple bar chart:
53 The following QML shows how to create a simple bar chart:
54 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
54 \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1
55
55
56 \beginfloatleft
56 \beginfloatleft
57 \image demos_qmlchart6.png
57 \image demos_qmlchart6.png
58 \endfloat
58 \endfloat
59 \clearfloat
59 \clearfloat
60 */
60 */
61
61
62 /*!
62 /*!
63 \property QAbstractBarSeries::barWidth
63 \property QAbstractBarSeries::barWidth
64 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
64 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
65 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
65 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
66 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
66 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
67 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
67 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
68 \sa QBarSeries
68 \sa QBarSeries
69 */
69 */
70 /*!
70 /*!
71 \qmlproperty real AbstractBarSeries::barWidth
71 \qmlproperty real AbstractBarSeries::barWidth
72 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
72 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
73 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
73 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
74 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
74 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
75 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
75 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
76 */
76 */
77
77
78 /*!
78 /*!
79 \property QAbstractBarSeries::count
79 \property QAbstractBarSeries::count
80 Holds the number of sets in series.
80 Holds the number of sets in series.
81 */
81 */
82 /*!
82 /*!
83 \qmlproperty int AbstractBarSeries::count
83 \qmlproperty int AbstractBarSeries::count
84 Holds the number of sets in series.
84 Holds the number of sets in series.
85 */
85 */
86
86
87 /*!
87 /*!
88 \property QAbstractBarSeries::labelsVisible
88 \property QAbstractBarSeries::labelsVisible
89 Defines the visibility of the labels in series
89 Defines the visibility of the labels in series
90 */
90 */
91 /*!
91 /*!
92 \qmlproperty bool AbstractBarSeries::labelsVisible
92 \qmlproperty bool AbstractBarSeries::labelsVisible
93 Defines the visibility of the labels in series
93 Defines the visibility of the labels in series
94 */
94 */
95
95
96 /*!
96 /*!
97 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
97 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
98 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
98 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
99 Clicked bar inside set is indexed by \a index
99 Clicked bar inside set is indexed by \a index
100 */
100 */
101 /*!
101 /*!
102 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
102 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
103 The signal is emitted if the user clicks with a mouse on top of BarSet.
103 The signal is emitted if the user clicks with a mouse on top of BarSet.
104 Clicked bar inside set is indexed by \a index
104 Clicked bar inside set is indexed by \a index
105 */
105 */
106
106
107 /*!
107 /*!
108 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
108 \fn void QAbstractBarSeries::hovered(bool status, QBarSet* barset)
109
109
110 The signal is emitted if mouse is hovered on top of series.
110 The signal is emitted if mouse is hovered on top of series.
111 Parameter \a barset is the pointer of barset, where hover happened.
111 Parameter \a barset is the pointer of barset, where hover happened.
112 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
112 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
113 */
113 */
114 /*!
114 /*!
115 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
115 \qmlsignal AbstractBarSeries::onHovered(bool status, BarSet barset)
116
116
117 The signal is emitted if mouse is hovered on top of series.
117 The signal is emitted if mouse is hovered on top of series.
118 Parameter \a barset is the pointer of barset, where hover happened.
118 Parameter \a barset is the pointer of barset, where hover happened.
119 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
119 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
120 */
120 */
121
121
122 /*!
122 /*!
123 \fn void QAbstractBarSeries::countChanged()
123 \fn void QAbstractBarSeries::countChanged()
124 This signal is emitted when barset count has been changed, for example by append or remove.
124 This signal is emitted when barset count has been changed, for example by append or remove.
125 */
125 */
126 /*!
126 /*!
127 \qmlsignal AbstractBarSeries::onCountChanged()
127 \qmlsignal AbstractBarSeries::onCountChanged()
128 This signal is emitted when barset count has been changed, for example by append or remove.
128 This signal is emitted when barset count has been changed, for example by append or remove.
129 */
129 */
130
130
131 /*!
131 /*!
132 \fn void QAbstractBarSeries::labelsVisibleChanged()
132 \fn void QAbstractBarSeries::labelsVisibleChanged()
133 This signal is emitted when labels visibility have changed.
133 This signal is emitted when labels visibility have changed.
134 \sa isLabelsVisible(), setLabelsVisible()
134 \sa isLabelsVisible(), setLabelsVisible()
135 */
135 */
136
136
137 /*!
137 /*!
138 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
138 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
139 This signal is emitted when \a sets have been added to the series.
139 This signal is emitted when \a sets have been added to the series.
140 \sa append(), insert()
140 \sa append(), insert()
141 */
141 */
142 /*!
142 /*!
143 \qmlsignal AbstractBarSeries::onAdded(BarSet barset)
143 \qmlsignal AbstractBarSeries::onAdded(BarSet barset)
144 Emitted when \a barset has been added to the series.
144 Emitted when \a barset has been added to the series.
145 */
145 */
146
146
147 /*!
147 /*!
148 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
148 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
149 This signal is emitted when \a sets have been removed from the series.
149 This signal is emitted when \a sets have been removed from the series.
150 \sa remove()
150 \sa remove()
151 */
151 */
152 /*!
152 /*!
153 \qmlsignal AbstractBarSeries::onRemoved(BarSet barset)
153 \qmlsignal AbstractBarSeries::onRemoved(BarSet barset)
154 Emitted when \a barset has been removed from the series.
154 Emitted when \a barset has been removed from the series.
155 */
155 */
156
156
157 /*!
157 /*!
158 \qmlmethod BarSet AbstractBarSeries::at(int index)
158 \qmlmethod BarSet AbstractBarSeries::at(int index)
159 Returns bar set at \a index. Returns null if the index is not valid.
159 Returns bar set at \a index. Returns null if the index is not valid.
160 */
160 */
161
161
162 /*!
162 /*!
163 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
163 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
164 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.
164 Adds a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
165 For example:
165 For example:
166 \code
166 \code
167 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
167 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
168 myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]);
168 myBarSeries.append("set 2", [Qt.point(0, 1), Qt.point(2, 2.5), Qt.point(3.5, 2.2)]);
169 \endcode
169 \endcode
170 */
170 */
171
171
172 /*!
172 /*!
173 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
173 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
174 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.
174 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
175 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
175 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
176 appended.
176 appended.
177 \sa AbstractBarSeries::append()
177 \sa AbstractBarSeries::append()
178 */
178 */
179
179
180 /*!
180 /*!
181 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
181 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
182 Removes the barset from the series. Returns true if successfull, false otherwise.
182 Removes the barset from the series. Returns true if successfull, false otherwise.
183 */
183 */
184
184
185 /*!
185 /*!
186 \qmlmethod AbstractBarSeries::clear()
186 \qmlmethod AbstractBarSeries::clear()
187 Removes all barsets from the series.
187 Removes all barsets from the series.
188 */
188 */
189
189
190 /*!
190 /*!
191 This is depreciated constructor.
191 This is depreciated constructor.
192 */
192 */
193 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
193 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
194 QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent)
194 QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent)
195 {
195 {
196 }
196 }
197
197
198 /*!
198 /*!
199 Destructs abstractbarseries and owned barsets.
199 Destructs abstractbarseries and owned barsets.
200 */
200 */
201 QAbstractBarSeries::~QAbstractBarSeries()
201 QAbstractBarSeries::~QAbstractBarSeries()
202 {
202 {
203
203
204 }
204 }
205
205
206 /*!
206 /*!
207 \internal
207 \internal
208 */
208 */
209 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
209 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
210 QAbstractSeries(d,parent)
210 QAbstractSeries(d,parent)
211 {
211 {
212 }
212 }
213
213
214 /*!
214 /*!
215 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
215 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
216 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
216 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
217 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
217 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
218 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
218 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
219 */
219 */
220 void QAbstractBarSeries::setBarWidth(qreal width)
220 void QAbstractBarSeries::setBarWidth(qreal width)
221 {
221 {
222 Q_D(QAbstractBarSeries);
222 Q_D(QAbstractBarSeries);
223 d->setBarWidth(width);
223 d->setBarWidth(width);
224 }
224 }
225
225
226 /*!
226 /*!
227 Returns the width of the bars of the series.
227 Returns the width of the bars of the series.
228 \sa setBarWidth()
228 \sa setBarWidth()
229 */
229 */
230 qreal QAbstractBarSeries::barWidth() const
230 qreal QAbstractBarSeries::barWidth() const
231 {
231 {
232 Q_D(const QAbstractBarSeries);
232 Q_D(const QAbstractBarSeries);
233 return d->barWidth();
233 return d->barWidth();
234 }
234 }
235
235
236 /*!
236 /*!
237 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.
237 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.
238 Returns true, if appending succeeded.
238 Returns true, if appending succeeded.
239 */
239 */
240 bool QAbstractBarSeries::append(QBarSet *set)
240 bool QAbstractBarSeries::append(QBarSet *set)
241 {
241 {
242 Q_D(QAbstractBarSeries);
242 Q_D(QAbstractBarSeries);
243 bool success = d->append(set);
243 bool success = d->append(set);
244 if (success) {
244 if (success) {
245 QList<QBarSet*> sets;
245 QList<QBarSet*> sets;
246 sets.append(set);
246 sets.append(set);
247 set->setParent(this);
247 set->setParent(this);
248 emit barsetsAdded(sets);
248 emit barsetsAdded(sets);
249 emit countChanged();
249 emit countChanged();
250 }
250 }
251 return success;
251 return success;
252 }
252 }
253
253
254 /*!
254 /*!
255 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
255 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
256 Returns true, if set was removed.
256 Returns true, if set was removed.
257 */
257 */
258 bool QAbstractBarSeries::remove(QBarSet *set)
258 bool QAbstractBarSeries::remove(QBarSet *set)
259 {
259 {
260 Q_D(QAbstractBarSeries);
260 Q_D(QAbstractBarSeries);
261 bool success = d->remove(set);
261 bool success = d->remove(set);
262 if (success) {
262 if (success) {
263 QList<QBarSet*> sets;
263 QList<QBarSet*> sets;
264 sets.append(set);
264 sets.append(set);
265 set->setParent(0);
265 set->setParent(0);
266 emit barsetsRemoved(sets);
266 emit barsetsRemoved(sets);
267 emit countChanged();
267 emit countChanged();
268 }
268 }
269 return success;
269 return success;
270 }
270 }
271
271
272 /*!
272 /*!
273 Adds a list of barsets to series. Takes ownership of \a sets.
273 Adds a list of barsets to series. Takes ownership of \a sets.
274 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
274 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
275 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
275 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
276 and function returns false.
276 and function returns false.
277 */
277 */
278 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
278 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
279 {
279 {
280 Q_D(QAbstractBarSeries);
280 Q_D(QAbstractBarSeries);
281 bool success = d->append(sets);
281 bool success = d->append(sets);
282 if (success) {
282 if (success) {
283 emit barsetsAdded(sets);
283 emit barsetsAdded(sets);
284 emit countChanged();
284 emit countChanged();
285 }
285 }
286 return success;
286 return success;
287 }
287 }
288
288
289 /*!
289 /*!
290 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.
290 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.
291 Returns true, if inserting succeeded.
291 Returns true, if inserting succeeded.
292
292
293 */
293 */
294 bool QAbstractBarSeries::insert(int index, QBarSet *set)
294 bool QAbstractBarSeries::insert(int index, QBarSet *set)
295 {
295 {
296 Q_D(QAbstractBarSeries);
296 Q_D(QAbstractBarSeries);
297 bool success = d->insert(index, set);
297 bool success = d->insert(index, set);
298 if (success) {
298 if (success) {
299 QList<QBarSet*> sets;
299 QList<QBarSet*> sets;
300 sets.append(set);
300 sets.append(set);
301 emit barsetsAdded(sets);
301 emit barsetsAdded(sets);
302 emit countChanged();
302 emit countChanged();
303 }
303 }
304 return success;
304 return success;
305 }
305 }
306
306
307 /*!
307 /*!
308 Removes all of the bar sets from the series
308 Removes all of the bar sets from the series
309 */
309 */
310 void QAbstractBarSeries::clear()
310 void QAbstractBarSeries::clear()
311 {
311 {
312 Q_D(QAbstractBarSeries);
312 Q_D(QAbstractBarSeries);
313 QList<QBarSet *> sets = barSets();
313 QList<QBarSet *> sets = barSets();
314 bool success = d->remove(sets);
314 bool success = d->remove(sets);
315 if (success) {
315 if (success) {
316 emit barsetsRemoved(sets);
316 emit barsetsRemoved(sets);
317 emit countChanged();
317 emit countChanged();
318 }
318 }
319 }
319 }
320
320
321 /*!
321 /*!
322 Returns number of sets in series.
322 Returns number of sets in series.
323 */
323 */
324 int QAbstractBarSeries::count() const
324 int QAbstractBarSeries::count() const
325 {
325 {
326 Q_D(const QAbstractBarSeries);
326 Q_D(const QAbstractBarSeries);
327 return d->m_barSets.count();
327 return d->m_barSets.count();
328 }
328 }
329
329
330 /*!
330 /*!
331 Returns a list of sets in series. Keeps ownership of sets.
331 Returns a list of sets in series. Keeps ownership of sets.
332 */
332 */
333 QList<QBarSet*> QAbstractBarSeries::barSets() const
333 QList<QBarSet*> QAbstractBarSeries::barSets() const
334 {
334 {
335 Q_D(const QAbstractBarSeries);
335 Q_D(const QAbstractBarSeries);
336 return d->m_barSets;
336 return d->m_barSets;
337 }
337 }
338
338
339 /*!
339 /*!
340 Sets the visibility of labels in series to \a visible
340 Sets the visibility of labels in series to \a visible
341 */
341 */
342 void QAbstractBarSeries::setLabelsVisible(bool visible)
342 void QAbstractBarSeries::setLabelsVisible(bool visible)
343 {
343 {
344 Q_D(QAbstractBarSeries);
344 Q_D(QAbstractBarSeries);
345 if (d->m_labelsVisible != visible) {
345 if (d->m_labelsVisible != visible) {
346 d->setLabelsVisible(visible);
346 d->setLabelsVisible(visible);
347 emit labelsVisibleChanged();
347 emit labelsVisibleChanged();
348 }
348 }
349 }
349 }
350
350
351 /*!
351 /*!
352 Returns the visibility of labels
352 Returns the visibility of labels
353 */
353 */
354 bool QAbstractBarSeries::isLabelsVisible() const
354 bool QAbstractBarSeries::isLabelsVisible() const
355 {
355 {
356 Q_D(const QAbstractBarSeries);
356 Q_D(const QAbstractBarSeries);
357 return d->m_labelsVisible;
357 return d->m_labelsVisible;
358 }
358 }
359
359
360 void QAbstractBarSeries::setGrouping(bool grouping)
361 {
362 Q_D(QAbstractBarSeries);
363 d->setGrouping(grouping);
364 }
365
366
367
360 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
368 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
361
369
362 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
370 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
363 QAbstractSeriesPrivate(q),
371 QAbstractSeriesPrivate(q),
364 m_barWidth(0.5), // Default value is 50% of category width
372 m_barWidth(0.5), // Default value is 50% of category width
365 m_labelsVisible(false),
373 m_labelsVisible(false),
366 m_visible(true)
374 m_visible(true),
375 m_grouping(true)
367 {
376 {
368 }
377 }
369
378
370 int QAbstractBarSeriesPrivate::categoryCount() const
379 int QAbstractBarSeriesPrivate::categoryCount() const
371 {
380 {
372 // No categories defined. return count of longest set.
381 // No categories defined. return count of longest set.
373 int count = 0;
382 int count = 0;
374 for (int i=0; i<m_barSets.count(); i++) {
383 for (int i=0; i<m_barSets.count(); i++) {
375 if (m_barSets.at(i)->count() > count) {
384 if (m_barSets.at(i)->count() > count) {
376 count = m_barSets.at(i)->count();
385 count = m_barSets.at(i)->count();
377 }
386 }
378 }
387 }
379
388
380 return count;
389 return count;
381 }
390 }
382
391
383 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
392 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
384 {
393 {
385 if (width < 0.0) {
394 if (width < 0.0) {
386 width = 0.0;
395 width = 0.0;
387 }
396 }
388 m_barWidth = width;
397 m_barWidth = width;
389 emit updatedBars();
398 emit updatedBars();
390 }
399 }
391
400
392 qreal QAbstractBarSeriesPrivate::barWidth() const
401 qreal QAbstractBarSeriesPrivate::barWidth() const
393 {
402 {
394 return m_barWidth;
403 return m_barWidth;
395 }
404 }
396
405
397 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
406 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
398 {
407 {
399 return m_barSets.at(index);
408 return m_barSets.at(index);
400 }
409 }
401
410
402 void QAbstractBarSeriesPrivate::setVisible(bool visible)
411 void QAbstractBarSeriesPrivate::setVisible(bool visible)
403 {
412 {
404 m_visible = visible;
413 m_visible = visible;
405 emit updatedBars();
414 emit updatedBars();
406 }
415 }
407
416
408 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
417 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
409 {
418 {
410 m_labelsVisible = visible;
419 m_labelsVisible = visible;
411 emit labelsVisibleChanged(visible);
420 emit labelsVisibleChanged(visible);
412 }
421 }
413
422
423 void QAbstractBarSeriesPrivate::setGrouping(bool grouping)
424 {
425 if (m_grouping != grouping) {
426 m_grouping = grouping;
427 emit updatedBars();
428 }
429 }
430
414 qreal QAbstractBarSeriesPrivate::min()
431 qreal QAbstractBarSeriesPrivate::min()
415 {
432 {
416 if (m_barSets.count() <= 0) {
433 if (m_barSets.count() <= 0) {
417 return 0;
434 return 0;
418 }
435 }
419 qreal min = INT_MAX;
436 qreal min = INT_MAX;
420
437
421 for (int i = 0; i < m_barSets.count(); i++) {
438 for (int i = 0; i < m_barSets.count(); i++) {
422 int categoryCount = m_barSets.at(i)->count();
439 int categoryCount = m_barSets.at(i)->count();
423 for (int j = 0; j < categoryCount; j++) {
440 for (int j = 0; j < categoryCount; j++) {
424 qreal temp = m_barSets.at(i)->at(j);
441 qreal temp = m_barSets.at(i)->at(j);
425 if (temp < min)
442 if (temp < min)
426 min = temp;
443 min = temp;
427 }
444 }
428 }
445 }
429 return min;
446 return min;
430 }
447 }
431
448
432 qreal QAbstractBarSeriesPrivate::max()
449 qreal QAbstractBarSeriesPrivate::max()
433 {
450 {
434 if (m_barSets.count() <= 0) {
451 if (m_barSets.count() <= 0) {
435 return 0;
452 return 0;
436 }
453 }
437 qreal max = INT_MIN;
454 qreal max = INT_MIN;
438
455
439 for (int i = 0; i < m_barSets.count(); i++) {
456 for (int i = 0; i < m_barSets.count(); i++) {
440 int categoryCount = m_barSets.at(i)->count();
457 int categoryCount = m_barSets.at(i)->count();
441 for (int j = 0; j < categoryCount; j++) {
458 for (int j = 0; j < categoryCount; j++) {
442 qreal temp = m_barSets.at(i)->at(j);
459 qreal temp = m_barSets.at(i)->at(j);
443 if (temp > max)
460 if (temp > max)
444 max = temp;
461 max = temp;
445 }
462 }
446 }
463 }
447
464
448 return max;
465 return max;
449 }
466 }
450
467
451 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
468 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
452 {
469 {
453 if ((set < 0) || (set >= m_barSets.count())) {
470 if ((set < 0) || (set >= m_barSets.count())) {
454 // No set, no value.
471 // No set, no value.
455 return 0;
472 return 0;
456 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
473 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
457 // No category, no value.
474 // No category, no value.
458 return 0;
475 return 0;
459 }
476 }
460
477
461 return m_barSets.at(set)->at(category);
478 return m_barSets.at(set)->at(category);
462 }
479 }
463
480
464 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
481 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
465 {
482 {
466 if ((set < 0) || (set >= m_barSets.count())) {
483 if ((set < 0) || (set >= m_barSets.count())) {
467 // No set, no value.
484 // No set, no value.
468 return 0;
485 return 0;
469 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
486 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
470 // No category, no value.
487 // No category, no value.
471 return 0;
488 return 0;
472 }
489 }
473
490
474 qreal value = m_barSets.at(set)->at(category);
491 qreal value = m_barSets.at(set)->at(category);
475 qreal sum = categorySum(category);
492 qreal sum = categorySum(category);
476 if ( qFuzzyIsNull(sum) ) {
493 if ( qFuzzyIsNull(sum) ) {
477 return 0;
494 return 0;
478 }
495 }
479
496
480 return value / sum;
497 return value / sum;
481 }
498 }
482
499
483 qreal QAbstractBarSeriesPrivate::categorySum(int category)
500 qreal QAbstractBarSeriesPrivate::categorySum(int category)
484 {
501 {
485 qreal sum(0);
502 qreal sum(0);
486 int count = m_barSets.count(); // Count sets
503 int count = m_barSets.count(); // Count sets
487 for (int set = 0; set < count; set++) {
504 for (int set = 0; set < count; set++) {
488 if (category < m_barSets.at(set)->count())
505 if (category < m_barSets.at(set)->count())
489 sum += m_barSets.at(set)->at(category);
506 sum += m_barSets.at(set)->at(category);
490 }
507 }
491 return sum;
508 return sum;
492 }
509 }
493
510
494 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
511 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
495 {
512 {
496 qreal sum(0);
513 qreal sum(0);
497 int count = m_barSets.count(); // Count sets
514 int count = m_barSets.count(); // Count sets
498 for (int set = 0; set < count; set++) {
515 for (int set = 0; set < count; set++) {
499 if (category < m_barSets.at(set)->count())
516 if (category < m_barSets.at(set)->count())
500 sum += qAbs(m_barSets.at(set)->at(category));
517 sum += qAbs(m_barSets.at(set)->at(category));
501 }
518 }
502 return sum;
519 return sum;
503 }
520 }
504
521
505 qreal QAbstractBarSeriesPrivate::maxCategorySum()
522 qreal QAbstractBarSeriesPrivate::maxCategorySum()
506 {
523 {
507 qreal max = INT_MIN;
524 qreal max = INT_MIN;
508 int count = categoryCount();
525 int count = categoryCount();
509 for (int i = 0; i < count; i++) {
526 for (int i = 0; i < count; i++) {
510 qreal sum = categorySum(i);
527 qreal sum = categorySum(i);
511 if (sum > max)
528 if (sum > max)
512 max = sum;
529 max = sum;
513 }
530 }
514 return max;
531 return max;
515 }
532 }
516
533
517 qreal QAbstractBarSeriesPrivate::minX()
534 qreal QAbstractBarSeriesPrivate::minX()
518 {
535 {
519 if (m_barSets.count() <= 0) {
536 if (m_barSets.count() <= 0) {
520 return 0;
537 return 0;
521 }
538 }
522 qreal min = INT_MAX;
539 qreal min = INT_MAX;
523
540
524 for (int i = 0; i < m_barSets.count(); i++) {
541 for (int i = 0; i < m_barSets.count(); i++) {
525 int categoryCount = m_barSets.at(i)->count();
542 int categoryCount = m_barSets.at(i)->count();
526 for (int j = 0; j < categoryCount; j++) {
543 for (int j = 0; j < categoryCount; j++) {
527 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
544 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
528 if (temp < min)
545 if (temp < min)
529 min = temp;
546 min = temp;
530 }
547 }
531 }
548 }
532 return min;
549 return min;
533 }
550 }
534
551
535 qreal QAbstractBarSeriesPrivate::maxX()
552 qreal QAbstractBarSeriesPrivate::maxX()
536 {
553 {
537 if (m_barSets.count() <= 0) {
554 if (m_barSets.count() <= 0) {
538 return 0;
555 return 0;
539 }
556 }
540 qreal max = INT_MIN;
557 qreal max = INT_MIN;
541
558
542 for (int i = 0; i < m_barSets.count(); i++) {
559 for (int i = 0; i < m_barSets.count(); i++) {
543 int categoryCount = m_barSets.at(i)->count();
560 int categoryCount = m_barSets.at(i)->count();
544 for (int j = 0; j < categoryCount; j++) {
561 for (int j = 0; j < categoryCount; j++) {
545 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
562 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
546 if (temp > max)
563 if (temp > max)
547 max = temp;
564 max = temp;
548 }
565 }
549 }
566 }
550
567
551 return max;
568 return max;
552 }
569 }
553
570
554
571
555 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
572 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
556 {
573 {
557 qreal minX(domain.minX());
574 qreal minX(domain.minX());
558 qreal minY(domain.minY());
575 qreal minY(domain.minY());
559 qreal maxX(domain.maxX());
576 qreal maxX(domain.maxX());
560 qreal maxY(domain.maxY());
577 qreal maxY(domain.maxY());
561
578
562 qreal seriesMinX = this->minX();
579 qreal seriesMinX = this->minX();
563 qreal seriesMaxX = this->maxX();
580 qreal seriesMaxX = this->maxX();
564 qreal y = max();
581 qreal y = max();
565 minX = qMin(minX, seriesMinX - (qreal)0.5);
582 minX = qMin(minX, seriesMinX - (qreal)0.5);
566 minY = qMin(minY, y);
583 minY = qMin(minY, y);
567 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
584 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
568 maxY = qMax(maxY, y);
585 maxY = qMax(maxY, y);
569
586
570 domain.setRange(minX,maxX,minY,maxY);
587 domain.setRange(minX,maxX,minY,maxY);
571 }
588 }
572
589
573 ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
590 ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
574 {
591 {
575 Q_UNUSED(presenter);
592 Q_UNUSED(presenter);
576 qWarning() << "QAbstractBarSeriesPrivate::createGraphics called";
593 qWarning() << "QAbstractBarSeriesPrivate::createGraphics called";
577 return 0;
594 return 0;
578 }
595 }
579
596
580 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
597 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
581 {
598 {
582 Q_Q(QAbstractBarSeries);
599 Q_Q(QAbstractBarSeries);
583 QList<LegendMarker*> markers;
600 QList<LegendMarker*> markers;
584 foreach(QBarSet* set, q->barSets()) {
601 foreach(QBarSet* set, q->barSets()) {
585 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
602 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
586 markers << marker;
603 markers << marker;
587 }
604 }
588
605
589 return markers;
606 return markers;
590 }
607 }
591
608
592 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
609 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
593 {
610 {
594 Q_Q(QAbstractBarSeries);
611 Q_Q(QAbstractBarSeries);
595 if ((m_barSets.contains(set)) || (set == 0)) {
612 if ((m_barSets.contains(set)) || (set == 0)) {
596 // Fail if set is already in list or set is null.
613 // Fail if set is already in list or set is null.
597 return false;
614 return false;
598 }
615 }
599 m_barSets.append(set);
616 m_barSets.append(set);
600 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
617 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
601 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
618 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
602 emit restructuredBars(); // this notifies barchartitem
619 emit restructuredBars(); // this notifies barchartitem
603 if (m_dataset) {
620 if (m_dataset) {
604 m_dataset->updateSeries(q); // this notifies legend
621 m_dataset->updateSeries(q); // this notifies legend
605 }
622 }
606 return true;
623 return true;
607 }
624 }
608
625
609 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
626 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
610 {
627 {
611 Q_Q(QAbstractBarSeries);
628 Q_Q(QAbstractBarSeries);
612 if (!m_barSets.contains(set)) {
629 if (!m_barSets.contains(set)) {
613 // Fail if set is not in list
630 // Fail if set is not in list
614 return false;
631 return false;
615 }
632 }
616 m_barSets.removeOne(set);
633 m_barSets.removeOne(set);
617 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
634 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
618 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
635 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
619 emit restructuredBars(); // this notifies barchartitem
636 emit restructuredBars(); // this notifies barchartitem
620 if (m_dataset) {
637 if (m_dataset) {
621 m_dataset->updateSeries(q); // this notifies legend
638 m_dataset->updateSeries(q); // this notifies legend
622 }
639 }
623 return true;
640 return true;
624 }
641 }
625
642
626 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
643 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
627 {
644 {
628 Q_Q(QAbstractBarSeries);
645 Q_Q(QAbstractBarSeries);
629 foreach (QBarSet* set, sets) {
646 foreach (QBarSet* set, sets) {
630 if ((set == 0) || (m_barSets.contains(set))) {
647 if ((set == 0) || (m_barSets.contains(set))) {
631 // Fail if any of the sets is null or is already appended.
648 // Fail if any of the sets is null or is already appended.
632 return false;
649 return false;
633 }
650 }
634 if (sets.count(set) != 1) {
651 if (sets.count(set) != 1) {
635 // Also fail if same set is more than once in given list.
652 // Also fail if same set is more than once in given list.
636 return false;
653 return false;
637 }
654 }
638 }
655 }
639
656
640 foreach (QBarSet* set, sets) {
657 foreach (QBarSet* set, sets) {
641 m_barSets.append(set);
658 m_barSets.append(set);
642 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
659 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
643 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
660 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
644 }
661 }
645 emit restructuredBars(); // this notifies barchartitem
662 emit restructuredBars(); // this notifies barchartitem
646 if (m_dataset) {
663 if (m_dataset) {
647 m_dataset->updateSeries(q); // this notifies legend
664 m_dataset->updateSeries(q); // this notifies legend
648 }
665 }
649 return true;
666 return true;
650 }
667 }
651
668
652 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
669 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
653 {
670 {
654 Q_Q(QAbstractBarSeries);
671 Q_Q(QAbstractBarSeries);
655 if (sets.count() == 0) {
672 if (sets.count() == 0) {
656 return false;
673 return false;
657 }
674 }
658 foreach (QBarSet* set, sets) {
675 foreach (QBarSet* set, sets) {
659 if ((set == 0) || (!m_barSets.contains(set))) {
676 if ((set == 0) || (!m_barSets.contains(set))) {
660 // Fail if any of the sets is null or is not in series
677 // Fail if any of the sets is null or is not in series
661 return false;
678 return false;
662 }
679 }
663 if (sets.count(set) != 1) {
680 if (sets.count(set) != 1) {
664 // Also fail if same set is more than once in given list.
681 // Also fail if same set is more than once in given list.
665 return false;
682 return false;
666 }
683 }
667 }
684 }
668
685
669 foreach (QBarSet* set, sets) {
686 foreach (QBarSet* set, sets) {
670 m_barSets.removeOne(set);
687 m_barSets.removeOne(set);
671 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
688 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
672 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
689 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
673 }
690 }
674
691
675 emit restructuredBars(); // this notifies barchartitem
692 emit restructuredBars(); // this notifies barchartitem
676 if (m_dataset) {
693 if (m_dataset) {
677 m_dataset->updateSeries(q); // this notifies legend
694 m_dataset->updateSeries(q); // this notifies legend
678 }
695 }
679 return true;
696 return true;
680 }
697 }
681
698
682 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
699 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
683 {
700 {
684 Q_Q(QAbstractBarSeries);
701 Q_Q(QAbstractBarSeries);
685 if ((m_barSets.contains(set)) || (set == 0)) {
702 if ((m_barSets.contains(set)) || (set == 0)) {
686 // Fail if set is already in list or set is null.
703 // Fail if set is already in list or set is null.
687 return false;
704 return false;
688 }
705 }
689 m_barSets.insert(index, set);
706 m_barSets.insert(index, set);
690 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
707 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
691 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
708 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
692 emit restructuredBars(); // this notifies barchartitem
709 emit restructuredBars(); // this notifies barchartitem
693 if (m_dataset) {
710 if (m_dataset) {
694 m_dataset->updateSeries(q); // this notifies legend
711 m_dataset->updateSeries(q); // this notifies legend
695 }
712 }
696 return true;
713 return true;
697 }
714 }
698
715
699 void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis)
716 void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis)
700 {
717 {
701 Q_Q(QAbstractBarSeries);
718 Q_Q(QAbstractBarSeries);
702
719
703 if(axis->type()==QAbstractAxis::AxisTypeCategories) {
720 if(axis->type()==QAbstractAxis::AxisTypeCategories) {
704
721
705 switch(q->type()) {
722 switch(q->type()) {
706
723
707 case QAbstractSeries::SeriesTypeHorizontalBar:
724 case QAbstractSeries::SeriesTypeHorizontalBar:
708 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
725 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
709 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
726 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
710
727
711 if(axis->orientation()==Qt::Vertical)
728 if(axis->orientation()==Qt::Vertical)
712 {
729 {
713 populateCategories(qobject_cast<QBarCategoriesAxis*>(axis));
730 populateCategories(qobject_cast<QBarCategoriesAxis*>(axis));
714 }
731 }
715 break;
732 break;
716 }
733 }
717 case QAbstractSeries::SeriesTypeBar:
734 case QAbstractSeries::SeriesTypeBar:
718 case QAbstractSeries::SeriesTypePercentBar:
735 case QAbstractSeries::SeriesTypePercentBar:
719 case QAbstractSeries::SeriesTypeStackedBar: {
736 case QAbstractSeries::SeriesTypeStackedBar: {
720
737
721 if(axis->orientation()==Qt::Horizontal)
738 if(axis->orientation()==Qt::Horizontal)
722 {
739 {
723 populateCategories(qobject_cast<QBarCategoriesAxis*>(axis));
740 populateCategories(qobject_cast<QBarCategoriesAxis*>(axis));
724 }
741 }
725 break;
742 break;
726 }
743 }
727 default:
744 default:
728 qWarning()<<"Unexpected series type";
745 qWarning()<<"Unexpected series type";
729 break;
746 break;
730
747
731 }
748 }
732 }
749 }
733 }
750 }
734
751
735 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
752 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
736 {
753 {
737 Q_Q(const QAbstractBarSeries);
754 Q_Q(const QAbstractBarSeries);
738
755
739 switch(q->type()) {
756 switch(q->type()) {
740
757
741 case QAbstractSeries::SeriesTypeHorizontalBar:
758 case QAbstractSeries::SeriesTypeHorizontalBar:
742 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
759 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
743 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
760 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
744
761
745 if(orientation==Qt::Vertical)
762 if(orientation==Qt::Vertical)
746 {
763 {
747 return QAbstractAxis::AxisTypeCategories;
764 return QAbstractAxis::AxisTypeCategories;
748 }
765 }
749 break;
766 break;
750 }
767 }
751 case QAbstractSeries::SeriesTypeBar:
768 case QAbstractSeries::SeriesTypeBar:
752 case QAbstractSeries::SeriesTypePercentBar:
769 case QAbstractSeries::SeriesTypePercentBar:
753 case QAbstractSeries::SeriesTypeStackedBar: {
770 case QAbstractSeries::SeriesTypeStackedBar: {
754
771
755 if(orientation==Qt::Horizontal)
772 if(orientation==Qt::Horizontal)
756 {
773 {
757 return QAbstractAxis::AxisTypeCategories;
774 return QAbstractAxis::AxisTypeCategories;
758 }
775 }
759 break;
776 break;
760 }
777 }
761 default:
778 default:
762 qWarning()<<"Unexpected series type";
779 qWarning()<<"Unexpected series type";
763 break;
780 break;
764
781
765 }
782 }
766 return QAbstractAxis::AxisTypeValues;
783 return QAbstractAxis::AxisTypeValues;
767
784
768 }
785 }
769
786
770 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoriesAxis* axis)
787 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoriesAxis* axis)
771 {
788 {
772 QStringList categories;
789 QStringList categories;
773 if(axis->categories().isEmpty()) {
790 if(axis->categories().isEmpty()) {
774 for (int i(1); i < categoryCount()+1; i++)
791 for (int i(1); i < categoryCount()+1; i++)
775 categories << QString::number(i);
792 categories << QString::number(i);
776 axis->append(categories);
793 axis->append(categories);
777 }
794 }
778 }
795 }
779
796
780 #include "moc_qabstractbarseries.cpp"
797 #include "moc_qabstractbarseries.cpp"
781 #include "moc_qabstractbarseries_p.cpp"
798 #include "moc_qabstractbarseries_p.cpp"
782
799
783
800
784 QTCOMMERCIALCHART_END_NAMESPACE
801 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,86 +1,90
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef ABSTRACTBARSERIES_H
21 #ifndef ABSTRACTBARSERIES_H
22 #define ABSTRACTBARSERIES_H
22 #define ABSTRACTBARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class QBarSet;
29 class QBarSet;
30 class QAbstractBarSeriesPrivate;
30 class QAbstractBarSeriesPrivate;
31
31
32 // Container for series
32 // Container for series
33 class QTCOMMERCIALCHART_EXPORT QAbstractBarSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QAbstractBarSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
37 Q_PROPERTY(int count READ count NOTIFY countChanged)
37 Q_PROPERTY(int count READ count NOTIFY countChanged)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39
39
40 protected:
40 protected:
41 //TODO DEPRECIATED
41 //TODO DEPRECIATED
42 explicit QAbstractBarSeries(QObject *parent = 0);
42 explicit QAbstractBarSeries(QObject *parent = 0);
43
43
44 public:
44 public:
45 virtual ~QAbstractBarSeries();
45 virtual ~QAbstractBarSeries();
46
46
47 void setBarWidth(qreal width);
47 void setBarWidth(qreal width);
48 qreal barWidth() const;
48 qreal barWidth() const;
49
49
50 bool append(QBarSet *set);
50 bool append(QBarSet *set);
51 bool remove(QBarSet *set);
51 bool remove(QBarSet *set);
52 bool append(QList<QBarSet* > sets);
52 bool append(QList<QBarSet* > sets);
53 bool insert(int index, QBarSet *set);
53 bool insert(int index, QBarSet *set);
54 int count() const;
54 int count() const;
55 QList<QBarSet*> barSets() const;
55 QList<QBarSet*> barSets() const;
56 void clear();
56 void clear();
57
57
58 void setLabelsVisible(bool visible = true);
58 void setLabelsVisible(bool visible = true);
59 bool isLabelsVisible() const;
59 bool isLabelsVisible() const;
60
60
61 // TODO. Good name for this. This affects how bars are laid out on chart. (for example with pyramid chart)
62 // Also do we support same for stacked and percent bar charts or just normal bar charts?
63 void setGrouping(bool grouping = true);
64
61 protected:
65 protected:
62 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0);
66 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0);
63
67
64 Q_SIGNALS:
68 Q_SIGNALS:
65 void clicked(int index, QBarSet *barset);
69 void clicked(int index, QBarSet *barset);
66 void hovered(bool status, QBarSet *barset);
70 void hovered(bool status, QBarSet *barset);
67 void countChanged();
71 void countChanged();
68 void labelsVisibleChanged();
72 void labelsVisibleChanged();
69
73
70 void barsetsAdded(QList<QBarSet*> sets);
74 void barsetsAdded(QList<QBarSet*> sets);
71 void barsetsRemoved(QList<QBarSet*> sets);
75 void barsetsRemoved(QList<QBarSet*> sets);
72
76
73 protected:
77 protected:
74 Q_DECLARE_PRIVATE(QAbstractBarSeries)
78 Q_DECLARE_PRIVATE(QAbstractBarSeries)
75 friend class AbstractBarChartItem;
79 friend class AbstractBarChartItem;
76 friend class PercentBarChartItem;
80 friend class PercentBarChartItem;
77 friend class StackedBarChartItem;
81 friend class StackedBarChartItem;
78 friend class BarChartItem;
82 friend class BarChartItem;
79 friend class HorizontalBarChartItem;
83 friend class HorizontalBarChartItem;
80 friend class HorizontalStackedBarChartItem;
84 friend class HorizontalStackedBarChartItem;
81 friend class HorizontalPercentBarChartItem;
85 friend class HorizontalPercentBarChartItem;
82 };
86 };
83
87
84 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
85
89
86 #endif // ABSTRACTBARSERIES_H
90 #endif // ABSTRACTBARSERIES_H
@@ -1,101 +1,105
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
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
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QABSTRACTBARSERIES_P_H
30 #ifndef QABSTRACTBARSERIES_P_H
31 #define QABSTRACTBARSERIES_P_H
31 #define QABSTRACTBARSERIES_P_H
32
32
33 #include "qabstractbarseries.h"
33 #include "qabstractbarseries.h"
34 #include "qabstractseries_p.h"
34 #include "qabstractseries_p.h"
35 #include <QStringList>
35 #include <QStringList>
36 #include <QAbstractSeries>
36 #include <QAbstractSeries>
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 class QBarModelMapper;
40 class QBarModelMapper;
41 class QBarCategoriesAxis;
41 class QBarCategoriesAxis;
42
42
43 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
43 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 public:
46 public:
47 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
47 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
48 int categoryCount() const;
48 int categoryCount() const;
49
49
50 void setBarWidth(qreal width);
50 void setBarWidth(qreal width);
51 qreal barWidth() const;
51 qreal barWidth() const;
52
52
53 void setVisible(bool visible);
53 void setVisible(bool visible);
54 void setLabelsVisible(bool visible);
54 void setLabelsVisible(bool visible);
55 void setGrouping(bool grouping);
55
56
56 void scaleDomain(Domain& domain);
57 void scaleDomain(Domain& domain);
57 ChartElement* createGraphics(ChartPresenter* presenter);
58 ChartElement* createGraphics(ChartPresenter* presenter);
58 QList<LegendMarker*> createLegendMarker(QLegend* legend);
59 QList<LegendMarker*> createLegendMarker(QLegend* legend);
59
60
60 void initializeAxis(QAbstractAxis* axis);
61 void initializeAxis(QAbstractAxis* axis);
61 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
62 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
62
63
63 bool append(QBarSet *set);
64 bool append(QBarSet *set);
64 bool remove(QBarSet *set);
65 bool remove(QBarSet *set);
65 bool append(QList<QBarSet* > sets);
66 bool append(QList<QBarSet* > sets);
66 bool remove(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
67 bool insert(int index, QBarSet *set);
68 bool insert(int index, QBarSet *set);
68
69
69 QBarSet* barsetAt(int index);
70 QBarSet* barsetAt(int index);
70 qreal min();
71 qreal min();
71 qreal max();
72 qreal max();
72 qreal valueAt(int set, int category);
73 qreal valueAt(int set, int category);
73 qreal percentageAt(int set, int category);
74 qreal percentageAt(int set, int category);
74 qreal categorySum(int category);
75 qreal categorySum(int category);
75 qreal absoluteCategorySum(int category);
76 qreal absoluteCategorySum(int category);
76 qreal maxCategorySum();
77 qreal maxCategorySum();
77 qreal minX();
78 qreal minX();
78 qreal maxX();
79 qreal maxX();
79
80
80 Q_SIGNALS:
81 Q_SIGNALS:
81 void clicked(int index, QBarSet *barset);
82 void clicked(int index, QBarSet *barset);
82 void updatedBars();
83 void updatedBars();
83 void restructuredBars();
84 void restructuredBars();
84 void labelsVisibleChanged(bool visible);
85 void labelsVisibleChanged(bool visible);
85
86
86 private:
87 private:
87 void populateCategories(QBarCategoriesAxis* axis);
88 void populateCategories(QBarCategoriesAxis* axis);
88
89
89 protected:
90 protected:
90 QList<QBarSet *> m_barSets;
91 QList<QBarSet *> m_barSets;
91 qreal m_barWidth;
92 qreal m_barWidth;
92 bool m_labelsVisible;
93 bool m_labelsVisible;
93 bool m_visible;
94 bool m_visible;
95 bool m_grouping;
94
96
95 private:
97 private:
96 Q_DECLARE_PUBLIC(QAbstractBarSeries)
98 Q_DECLARE_PUBLIC(QAbstractBarSeries)
99 friend class HorizontalBarChartItem;
100 friend class BarChartItem;
97 };
101 };
98
102
99 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
100
104
101 #endif // QABSTRACTBARSERIES_P_H
105 #endif // QABSTRACTBARSERIES_P_H
@@ -1,96 +1,109
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qabstractbarseries_p.h"
24 #include "qabstractbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26 #include "qbarset_p.h"
26 #include "qbarset_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 BarChartItem::BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem::BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) :
31 AbstractBarChartItem(series, presenter)
31 AbstractBarChartItem(series, presenter)
32 {
32 {
33 }
33 }
34
34
35 QVector<QRectF> BarChartItem::calculateLayout()
35 QVector<QRectF> BarChartItem::calculateLayout()
36 {
36 {
37 QVector<QRectF> layout;
37 QVector<QRectF> layout;
38
38
39 // Use temporary qreals for accuracy
39 // Use temporary qreals for accuracy
40 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal categoryCount = m_series->d_func()->categoryCount();
41 qreal setCount = m_series->count();
41 qreal setCount = m_series->count();
42 bool barsVisible = m_series->isVisible();
42 bool barsVisible = m_series->isVisible();
43
43
44 // Domain:
44 // Domain:
45 qreal width = geometry().width();
45 qreal width = geometry().width();
46 qreal height = geometry().height();
46 qreal height = geometry().height();
47 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeY = m_domainMaxY - m_domainMinY;
48 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal rangeX = m_domainMaxX - m_domainMinX;
49 qreal scaleY = (height / rangeY);
49 qreal scaleY = (height / rangeY);
50 qreal scaleX = (width / rangeX);
50 qreal scaleX = (width / rangeX);
51 qreal barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
51 qreal barWidth;
52
53 if (m_series->d_func()->m_grouping) {
54 barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
55 } else {
56 barWidth = scaleX * m_series->d_func()->barWidth();
57 }
52
58
53 int itemIndex(0);
59 int itemIndex(0);
54 for (int category = 0; category < categoryCount; category++) {
60 for (int category = 0; category < categoryCount; category++) {
55 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
61 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 for (int set = 0; set < setCount; set++) {
62 for (int set = 0; set < setCount; set++) {
57 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
63 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
58
64
59 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left();
65 qreal xPos;
60 xPos -= setCount*barWidth/2;
66
61 xPos += set*barWidth;
67 if (m_series->d_func()->m_grouping) {
68 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left();
69 xPos -= setCount*barWidth/2;
70 xPos += set*barWidth;
71 } else {
72 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
73 }
74
62 qreal barHeight = barSet->value(category) * scaleY;
75 qreal barHeight = barSet->value(category) * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
76 Bar* bar = m_bars.at(itemIndex);
64
77
65 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
78 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
66 layout.append(rect);
79 layout.append(rect);
67 bar->setPen(barSet->m_pen);
80 bar->setPen(barSet->m_pen);
68 bar->setBrush(barSet->m_brush);
81 bar->setBrush(barSet->m_brush);
69 if (qFuzzyIsNull(barHeight)) {
82 if (qFuzzyIsNull(barHeight)) {
70 bar->setVisible(false);
83 bar->setVisible(false);
71 } else {
84 } else {
72 bar->setVisible(barsVisible);
85 bar->setVisible(barsVisible);
73 }
86 }
74
87
75 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
88 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
76
89
77 if (!qFuzzyIsNull(barSet->value(category))) {
90 if (!qFuzzyIsNull(barSet->value(category))) {
78 label->setText(QString::number(barSet->value(category)));
91 label->setText(QString::number(barSet->value(category)));
79 } else {
92 } else {
80 label->setText(QString(""));
93 label->setText(QString(""));
81 }
94 }
82
95
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
96 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
97 ,yPos - barHeight/2 - label->boundingRect().height()/2);
85 label->setFont(barSet->m_labelFont);
98 label->setFont(barSet->m_labelFont);
86 label->setBrush(barSet->m_labelBrush);
99 label->setBrush(barSet->m_labelBrush);
87
100
88 itemIndex++;
101 itemIndex++;
89 }
102 }
90 }
103 }
91 return layout;
104 return layout;
92 }
105 }
93
106
94 #include "moc_barchartitem_p.cpp"
107 #include "moc_barchartitem_p.cpp"
95
108
96 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now