##// END OF EJS Templates
population pyramid documentation. updated example.
sauimone -
r1866:b747c15751dd
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,45
1 /*!
2 \example examples/populationpyramid
3 \title Population pyramid example
4 \subtitle
5
6 The example shows how to create a bar chart with bars extending in opposite directions. For example purposes we use population data found from wikipedia.
7
8 \image examples_populationpyramid.png
9
10 First we create two barsets and append the data to them. To make another barset grow to left, the values are negated.
11
12 \snippet ../examples/populationpyramid/main.cpp 1
13
14 We create the series and append the barsets to it. The series takes ownership of the barsets. We want the bars to be at same position on the y-axis, so we turn the
15 overlap drawing on. We also set the bar width to be 50% of the category width.
16
17 \snippet ../examples/populationpyramid/main.cpp 2
18
19 Here we create the chart object and add the series to it. We set the title for chart with setTitle and then turn on animations of the series by calling
20 setAnimationOptions(QChart::SeriesAnimations)
21
22 \snippet ../examples/populationpyramid/main.cpp 3
23
24 To have categories displayed on axis, we need to create a QBarCategoryAxis for that. Here we create a category axis with list of categories and
25 set it to be the y-axis of the chart. The chart takes ownership of axis. For x-axis we use default axis, which is created and scaled to series data
26 by calling createDefaultAxes of the chart. Note that the call for createDefaultAxes must be before we set the category axis. Otherwise the default axis will
27 override the category axis.
28 We also set the range for x-axis, since in this case it gives nicer result than autoscaling.
29
30 \snippet ../examples/populationpyramid/main.cpp 4
31
32 We also want to show the legend. To do that, we get the legend pointer from chart and set it to visible. We also place the legend to bottom of the chart by setting it's alignment
33 to Qt::AlignBottom.
34
35 \snippet ../examples/populationpyramid/main.cpp 5
36
37 Finally we add the chart onto a view. We also turn on the antialiasing for the chartView.
38
39 \snippet ../examples/populationpyramid/main.cpp 6
40
41 Chart is ready to be shown. We set the chart to be central widget of the window.
42 We also set the size for the chart window and show it.
43
44 \snippet ../examples/populationpyramid/main.cpp 7
45 */ No newline at end of file
@@ -1,45 +1,46
1 /*!
1 /*!
2 \page examples.html
2 \page examples.html
3 \title Examples
3 \title Examples
4 \keyword Examples
4 \keyword Examples
5
5
6 \raw HTML
6 \raw HTML
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 <tr>
8 <tr>
9 <th class="titleheader" width="33%">
9 <th class="titleheader" width="33%">
10 List of examples
10 List of examples
11 </th>
11 </th>
12 </tr>
12 </tr>
13 <tr>
13 <tr>
14 <td valign="top">
14 <td valign="top">
15 <ul>
15 <ul>
16 <li><a href="examples-areachart.html">Area chart</a></li>
16 <li><a href="examples-areachart.html">Area chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
17 <li><a href="examples-barchart.html">Bar chart</a></li>
18 <li><a href="examples-customchart.html">Custom chart</a></li>
18 <li><a href="examples-customchart.html">Custom chart</a></li>
19 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
19 <li><a href="examples-horizontalbarchart.html">Horizontal bar chart</a></li>
20 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
20 <li><a href="examples-horizontalstackedbarchart.html">Horizontal stacked bar chart</a></li>
21 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
21 <li><a href="examples-horizontalpercentbarchart.html">Horizontal percent bar chart</a></li>
22 <li><a href="examples-legend.html">Legend</a></li>
22 <li><a href="examples-legend.html">Legend</a></li>
23 <li><a href="examples-linechart.html">Line chart</a></li>
23 <li><a href="examples-linechart.html">Line chart</a></li>
24 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
24 <li><a href="examples-lineandbar.html">Line and barchart</a></li>
25 <li><a href="examples-modeldata.html">Model data</a></li>
25 <li><a href="examples-modeldata.html">Model data</a></li>
26 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
26 <li><a href="examples-barmodelmapper.html">Bar chart from model</a></li>
27 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
27 <li><a href="examples-percentbarchart.html">Percent bar chart</a></li>
28 <li><a href="examples-populationpyramid.html">Population pyramid</a></li>
28 <li><a href="examples-piechart.html">Pie chart</a></li>
29 <li><a href="examples-piechart.html">Pie chart</a></li>
29 <li><a href="examples-donutchart.html">Donut chart</a></li>
30 <li><a href="examples-donutchart.html">Donut chart</a></li>
30 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
31 <li><a href="examples-piechartdrilldown.html">Pie chart drilldown</a></li>
31 <li><a href="examples-donut.html">Nested donuts chart</a></li>
32 <li><a href="examples-donut.html">Nested donuts chart</a></li>
32 <li><a href="examples-donutbreakdown.html">Donut chart drilldown</a></li>
33 <li><a href="examples-donutbreakdown.html">Donut chart drilldown</a></li>
33 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
34 <li><a href="examples-presenterchart.html">Presenter chart</a></li>
34 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
35 <li><a href="examples-scatterchart.html">Scatter chart</a></li>
35 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
36 <li><a href="examples-scatterinteractions.html">Scatter interactions</a></li>
36 <li><a href="examples-splinechart.html">Spline chart</a></li>
37 <li><a href="examples-splinechart.html">Spline chart</a></li>
37 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
38 <li><a href="examples-stackedbarchart.html">Stacked bar chart</a></li>
38 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
39 <li><a href="examples-stackedbarchartdrilldown.html">Stacked bar chart drilldown</a></li>
39 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
40 <li><a href="examples-zoomlinechart.html">Zoom line</a></li>
40 </ul>
41 </ul>
41 </td>
42 </td>
42 </tr>
43 </tr>
43 </table>
44 </table>
44 \endraw
45 \endraw
45 */
46 */
@@ -1,94 +1,94
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 <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QBarSeries>
24 #include <QBarSeries>
25 #include <QBarSet>
25 #include <QBarSet>
26 #include <QLegend>
26 #include <QLegend>
27 #include <QBarCategoryAxis>
27 #include <QBarCategoryAxis>
28 #include <QHorizontalBarSeries>
28 #include <QHorizontalBarSeries>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 int main(int argc, char *argv[])
32 int main(int argc, char *argv[])
33 {
33 {
34 QApplication a(argc, argv);
34 QApplication a(argc, argv);
35
35
36 //![1]
36 //![1]
37 QBarSet *male = new QBarSet("Male");
37 QBarSet *male = new QBarSet("Male");
38 QBarSet *female = new QBarSet("Female");
38 QBarSet *female = new QBarSet("Female");
39
39
40 // From wikipedia
40 // Not really negative population. Negated values are used to grow bar to left.
41 *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815
41 *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815
42 << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769;
42 << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769;
43
43
44 *female << 139353 << 143522 << 161212 << 156824 << 163226 << 161766 << 150595 << 170779 << 185979 << 187897 << 196158
44 *female << 139353 << 143522 << 161212 << 156824 << 163226 << 161766 << 150595 << 170779 << 185979 << 187897 << 196158
45 << 209062 << 152460 << 129230 << 114778 << 109611 << 83845 << 44605 << 22328;
45 << 209062 << 152460 << 129230 << 114778 << 109611 << 83845 << 44605 << 22328;
46
46
47 //![1]
47 //![1]
48
48
49 //![2]
49 //![2]
50 QHorizontalBarSeries *series = new QHorizontalBarSeries();
50 QHorizontalBarSeries *series = new QHorizontalBarSeries();
51 series->append(male);
51 series->append(male);
52 series->append(female);
52 series->append(female);
53 series->setBarWidth(0.5);
53 series->setBarWidth(0.5);
54 series->setGrouping(false);
54 series->setOverlap(true);
55 //![2]
55 //![2]
56
56
57 //![3]
57 //![3]
58 QChart* chart = new QChart();
58 QChart* chart = new QChart();
59 chart->addSeries(series);
59 chart->addSeries(series);
60 chart->setTitle("Population of Finland in 2005 by age group");
60 chart->setTitle("Population of Finland in 2005 by age group");
61 chart->createDefaultAxes();
62 chart->setAnimationOptions(QChart::SeriesAnimations);
61 chart->setAnimationOptions(QChart::SeriesAnimations);
63 //![3]
62 //![3]
64
63
65 //![4]
64 //![4]
66 QStringList categories;
65 QStringList categories;
67 categories << "0-4" << "5-9" << "10-14" << "15-19" << "20-24" << "25-29" << "30-34" << "35-39" << "40-44" << "45-49"
66 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-";
67 << "50-54" << "55-59" << "60-64" << "65-69" << "70-74" << "75-79" << "80-84" << "85-89" << "90-";
69
68
70 QBarCategoryAxis* axis = new QBarCategoryAxis();
69 QBarCategoryAxis* axis = new QBarCategoryAxis();
71 axis->append(categories);
70 axis->append(categories);
71 chart->createDefaultAxes();
72 chart->setAxisY(axis,series);
72 chart->setAxisY(axis,series);
73 chart->axisX(series)->setRange(-210000,210000);
73 chart->axisX(series)->setRange(-210000,210000);
74 //![4]
74 //![4]
75
75
76 //![5]
76 //![5]
77 chart->legend()->setVisible(true);
77 chart->legend()->setVisible(true);
78 chart->legend()->setAlignment(Qt::AlignBottom);
78 chart->legend()->setAlignment(Qt::AlignBottom);
79 //![5]
79 //![5]
80
80
81 //![6]
81 //![6]
82 QChartView* chartView = new QChartView(chart);
82 QChartView* chartView = new QChartView(chart);
83 chartView->setRenderHint(QPainter::Antialiasing);
83 chartView->setRenderHint(QPainter::Antialiasing);
84 //![6]
84 //![6]
85
85
86 //![7]
86 //![7]
87 QMainWindow window;
87 QMainWindow window;
88 window.setCentralWidget(chartView);
88 window.setCentralWidget(chartView);
89 window.resize(400, 800);
89 window.resize(400, 600);
90 window.show();
90 window.show();
91 //![7]
91 //![7]
92
92
93 return a.exec();
93 return a.exec();
94 }
94 }
@@ -1,106 +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;
49 qreal barHeight;
50
50
51 // On horizontal chart barWidth of the barseries means height of the rect.
51 // On horizontal chart barWidth of the barseries means height of the rect.
52 if (m_series->d_func()->m_grouping) {
52 if (m_series->d_func()->m_overlap) {
53 barHeight = (scaleY / setCount) * m_series->d_func()->barWidth();
54 } else {
55 barHeight = scaleY * m_series->d_func()->barWidth();
53 barHeight = scaleY * m_series->d_func()->barWidth();
54 } else {
55 barHeight = (scaleY / setCount) * m_series->d_func()->barWidth();
56 }
56 }
57
57
58 int itemIndex(0);
58 int itemIndex(0);
59 for (int category = 0; category < categoryCount; category++) {
59 for (int category = 0; category < categoryCount; category++) {
60 qreal xPos = -scaleX * m_domainMinX + geometry().left();
60 qreal xPos = -scaleX * m_domainMinX + geometry().left();
61 for (int set = 0; set < setCount; set++) {
61 for (int set = 0; set < setCount; set++) {
62 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
62 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
63
63
64 qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
64 qreal yPos = m_rect.bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
65 if (m_series->d_func()->m_grouping) {
65 if (m_series->d_func()->m_overlap) {
66 yPos += barHeight/2;
67 } else {
66 yPos += setCount*barHeight/2;
68 yPos += setCount*barHeight/2;
67 yPos -= set*barHeight;
69 yPos -= set*barHeight;
68 } else {
69 yPos += barHeight/2;
70 }
70 }
71
71
72 qreal barWidth = barSet->value(category) * scaleX;
72 qreal barWidth = barSet->value(category) * scaleX;
73 Bar* bar = m_bars.at(itemIndex);
73 Bar* bar = m_bars.at(itemIndex);
74
74
75 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
75 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
76 layout.append(rect);
76 layout.append(rect);
77 bar->setPen(barSet->m_pen);
77 bar->setPen(barSet->m_pen);
78 bar->setBrush(barSet->m_brush);
78 bar->setBrush(barSet->m_brush);
79 if (qFuzzyIsNull(barHeight)) {
79 if (qFuzzyIsNull(barHeight)) {
80 bar->setVisible(false);
80 bar->setVisible(false);
81 } else {
81 } else {
82 bar->setVisible(barsVisible);
82 bar->setVisible(barsVisible);
83 }
83 }
84
84
85 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
85 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
86
86
87 if (!qFuzzyIsNull(barSet->value(category))) {
87 if (!qFuzzyIsNull(barSet->value(category))) {
88 label->setText(QString::number(barSet->value(category)));
88 label->setText(QString::number(barSet->value(category)));
89 } else {
89 } else {
90 label->setText(QString(""));
90 label->setText(QString(""));
91 }
91 }
92
92
93 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
93 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
94 ,yPos - barHeight/2 - label->boundingRect().height()/2);
94 ,yPos - barHeight/2 - label->boundingRect().height()/2);
95 label->setFont(barSet->m_labelFont);
95 label->setFont(barSet->m_labelFont);
96 label->setBrush(barSet->m_labelBrush);
96 label->setBrush(barSet->m_labelBrush);
97
97
98 itemIndex++;
98 itemIndex++;
99 }
99 }
100 }
100 }
101 return layout;
101 return layout;
102 }
102 }
103
103
104 #include "moc_horizontalbarchartitem_p.cpp"
104 #include "moc_horizontalbarchartitem_p.cpp"
105
105
106 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,834 +1,835
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 "qvalueaxis.h"
29 #include "qvalueaxis.h"
30 #include "qbarcategoryaxis.h"
30 #include "qbarcategoryaxis.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 \a parent
192 \a parent
193 */
193 */
194 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
194 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
195 QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent)
195 QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent)
196 {
196 {
197 }
197 }
198
198
199 /*!
199 /*!
200 Destructs abstractbarseries and owned barsets.
200 Destructs abstractbarseries and owned barsets.
201 */
201 */
202 QAbstractBarSeries::~QAbstractBarSeries()
202 QAbstractBarSeries::~QAbstractBarSeries()
203 {
203 {
204
204
205 }
205 }
206
206
207 /*!
207 /*!
208 \internal
208 \internal
209 */
209 */
210 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
210 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent) :
211 QAbstractSeries(d,parent)
211 QAbstractSeries(d,parent)
212 {
212 {
213 }
213 }
214
214
215 /*!
215 /*!
216 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 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
217 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
217 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
218 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
218 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
219 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
219 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
220 */
220 */
221 void QAbstractBarSeries::setBarWidth(qreal width)
221 void QAbstractBarSeries::setBarWidth(qreal width)
222 {
222 {
223 Q_D(QAbstractBarSeries);
223 Q_D(QAbstractBarSeries);
224 d->setBarWidth(width);
224 d->setBarWidth(width);
225 }
225 }
226
226
227 /*!
227 /*!
228 Returns the width of the bars of the series.
228 Returns the width of the bars of the series.
229 \sa setBarWidth()
229 \sa setBarWidth()
230 */
230 */
231 qreal QAbstractBarSeries::barWidth() const
231 qreal QAbstractBarSeries::barWidth() const
232 {
232 {
233 Q_D(const QAbstractBarSeries);
233 Q_D(const QAbstractBarSeries);
234 return d->barWidth();
234 return d->barWidth();
235 }
235 }
236
236
237 /*!
237 /*!
238 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 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.
239 Returns true, if appending succeeded.
239 Returns true, if appending succeeded.
240 */
240 */
241 bool QAbstractBarSeries::append(QBarSet *set)
241 bool QAbstractBarSeries::append(QBarSet *set)
242 {
242 {
243 Q_D(QAbstractBarSeries);
243 Q_D(QAbstractBarSeries);
244 bool success = d->append(set);
244 bool success = d->append(set);
245 if (success) {
245 if (success) {
246 QList<QBarSet*> sets;
246 QList<QBarSet*> sets;
247 sets.append(set);
247 sets.append(set);
248 set->setParent(this);
248 set->setParent(this);
249 emit barsetsAdded(sets);
249 emit barsetsAdded(sets);
250 emit countChanged();
250 emit countChanged();
251 }
251 }
252 return success;
252 return success;
253 }
253 }
254
254
255 /*!
255 /*!
256 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
256 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
257 was successful.
257 was successful.
258 Returns true, if set was removed.
258 Returns true, if set was removed.
259 */
259 */
260 bool QAbstractBarSeries::remove(QBarSet *set)
260 bool QAbstractBarSeries::remove(QBarSet *set)
261 {
261 {
262 Q_D(QAbstractBarSeries);
262 Q_D(QAbstractBarSeries);
263 bool success = d->remove(set);
263 bool success = d->remove(set);
264 if (success) {
264 if (success) {
265 QList<QBarSet*> sets;
265 QList<QBarSet*> sets;
266 sets.append(set);
266 sets.append(set);
267 set->setParent(0);
267 set->setParent(0);
268 emit barsetsRemoved(sets);
268 emit barsetsRemoved(sets);
269 emit countChanged();
269 emit countChanged();
270 delete set;
270 delete set;
271 set = 0;
271 set = 0;
272 }
272 }
273 return success;
273 return success;
274 }
274 }
275
275
276 /*!
276 /*!
277 Takes a single \a set from the series. Does not delete the barset object.
277 Takes a single \a set from the series. Does not delete the barset object.
278
278
279 NOTE: The series remains as the barset's parent object. You must set the
279 NOTE: The series remains as the barset's parent object. You must set the
280 parent object to take full ownership.
280 parent object to take full ownership.
281
281
282 Returns true if take was successfull.
282 Returns true if take was successfull.
283 */
283 */
284 bool QAbstractBarSeries::take(QBarSet *set)
284 bool QAbstractBarSeries::take(QBarSet *set)
285 {
285 {
286 Q_D(QAbstractBarSeries);
286 Q_D(QAbstractBarSeries);
287 bool success = d->remove(set);
287 bool success = d->remove(set);
288 if (success) {
288 if (success) {
289 QList<QBarSet*> sets;
289 QList<QBarSet*> sets;
290 sets.append(set);
290 sets.append(set);
291 emit barsetsRemoved(sets);
291 emit barsetsRemoved(sets);
292 emit countChanged();
292 emit countChanged();
293 }
293 }
294 return success;
294 return success;
295 }
295 }
296
296
297 /*!
297 /*!
298 Adds a list of barsets to series. Takes ownership of \a sets.
298 Adds a list of barsets to series. Takes ownership of \a sets.
299 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
299 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
300 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
300 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
301 and function returns false.
301 and function returns false.
302 */
302 */
303 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
303 bool QAbstractBarSeries::append(QList<QBarSet* > sets)
304 {
304 {
305 Q_D(QAbstractBarSeries);
305 Q_D(QAbstractBarSeries);
306 bool success = d->append(sets);
306 bool success = d->append(sets);
307 if (success) {
307 if (success) {
308 emit barsetsAdded(sets);
308 emit barsetsAdded(sets);
309 emit countChanged();
309 emit countChanged();
310 }
310 }
311 return success;
311 return success;
312 }
312 }
313
313
314 /*!
314 /*!
315 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.
315 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.
316 Returns true, if inserting succeeded.
316 Returns true, if inserting succeeded.
317
317
318 */
318 */
319 bool QAbstractBarSeries::insert(int index, QBarSet *set)
319 bool QAbstractBarSeries::insert(int index, QBarSet *set)
320 {
320 {
321 Q_D(QAbstractBarSeries);
321 Q_D(QAbstractBarSeries);
322 bool success = d->insert(index, set);
322 bool success = d->insert(index, set);
323 if (success) {
323 if (success) {
324 QList<QBarSet*> sets;
324 QList<QBarSet*> sets;
325 sets.append(set);
325 sets.append(set);
326 emit barsetsAdded(sets);
326 emit barsetsAdded(sets);
327 emit countChanged();
327 emit countChanged();
328 }
328 }
329 return success;
329 return success;
330 }
330 }
331
331
332 /*!
332 /*!
333 Removes all barsets from the series. Deletes removed sets.
333 Removes all barsets from the series. Deletes removed sets.
334 */
334 */
335 void QAbstractBarSeries::clear()
335 void QAbstractBarSeries::clear()
336 {
336 {
337 Q_D(QAbstractBarSeries);
337 Q_D(QAbstractBarSeries);
338 QList<QBarSet *> sets = barSets();
338 QList<QBarSet *> sets = barSets();
339 bool success = d->remove(sets);
339 bool success = d->remove(sets);
340 if (success) {
340 if (success) {
341 emit barsetsRemoved(sets);
341 emit barsetsRemoved(sets);
342 emit countChanged();
342 emit countChanged();
343 foreach (QBarSet* set, sets) {
343 foreach (QBarSet* set, sets) {
344 delete set;
344 delete set;
345 }
345 }
346 }
346 }
347 }
347 }
348
348
349 /*!
349 /*!
350 Returns number of sets in series.
350 Returns number of sets in series.
351 */
351 */
352 int QAbstractBarSeries::count() const
352 int QAbstractBarSeries::count() const
353 {
353 {
354 Q_D(const QAbstractBarSeries);
354 Q_D(const QAbstractBarSeries);
355 return d->m_barSets.count();
355 return d->m_barSets.count();
356 }
356 }
357
357
358 /*!
358 /*!
359 Returns a list of sets in series. Keeps ownership of sets.
359 Returns a list of sets in series. Keeps ownership of sets.
360 */
360 */
361 QList<QBarSet*> QAbstractBarSeries::barSets() const
361 QList<QBarSet*> QAbstractBarSeries::barSets() const
362 {
362 {
363 Q_D(const QAbstractBarSeries);
363 Q_D(const QAbstractBarSeries);
364 return d->m_barSets;
364 return d->m_barSets;
365 }
365 }
366
366
367 /*!
367 /*!
368 Sets the visibility of labels in series to \a visible
368 Sets the visibility of labels in series to \a visible
369 */
369 */
370 void QAbstractBarSeries::setLabelsVisible(bool visible)
370 void QAbstractBarSeries::setLabelsVisible(bool visible)
371 {
371 {
372 Q_D(QAbstractBarSeries);
372 Q_D(QAbstractBarSeries);
373 if (d->m_labelsVisible != visible) {
373 if (d->m_labelsVisible != visible) {
374 d->setLabelsVisible(visible);
374 d->setLabelsVisible(visible);
375 emit labelsVisibleChanged();
375 emit labelsVisibleChanged();
376 }
376 }
377 }
377 }
378
378
379 /*!
379 /*!
380 Returns the visibility of labels
380 Returns the visibility of labels
381 */
381 */
382 bool QAbstractBarSeries::isLabelsVisible() const
382 bool QAbstractBarSeries::isLabelsVisible() const
383 {
383 {
384 Q_D(const QAbstractBarSeries);
384 Q_D(const QAbstractBarSeries);
385 return d->m_labelsVisible;
385 return d->m_labelsVisible;
386 }
386 }
387
387
388 /*!
388 /*!
389 Sets the grouped drawing mode for bars. If \a grouping is true, then the bars
389 Sets the overlap drawing mode for bars. If \a overlap is true, then the bars
390 are drawn at same position inside group. Can be used for example to draw negative bars
390 are drawn at same position inside group. Can be used for example to draw negative bars
391 at same position on axis than positive bars.
391 at same position on axis than positive bars. By default overlap is false and bars are drawn
392 next to each other. Note that this setting doesn't affect stacked and percent bar series.
392 */
393 */
393 void QAbstractBarSeries::setGrouping(bool grouping)
394 void QAbstractBarSeries::setOverlap(bool overlap)
394 {
395 {
395 Q_D(QAbstractBarSeries);
396 Q_D(QAbstractBarSeries);
396 d->setGrouping(grouping);
397 d->setOverlap(overlap);
397 }
398 }
398
399
399
400
400
401
401 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
402 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
402
403
403 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
404 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
404 QAbstractSeriesPrivate(q),
405 QAbstractSeriesPrivate(q),
405 m_barWidth(0.5), // Default value is 50% of category width
406 m_barWidth(0.5), // Default value is 50% of category width
406 m_labelsVisible(false),
407 m_labelsVisible(false),
407 m_visible(true),
408 m_visible(true),
408 m_grouping(true)
409 m_overlap(false)
409 {
410 {
410 }
411 }
411
412
412 int QAbstractBarSeriesPrivate::categoryCount() const
413 int QAbstractBarSeriesPrivate::categoryCount() const
413 {
414 {
414 // No categories defined. return count of longest set.
415 // No categories defined. return count of longest set.
415 int count = 0;
416 int count = 0;
416 for (int i=0; i<m_barSets.count(); i++) {
417 for (int i=0; i<m_barSets.count(); i++) {
417 if (m_barSets.at(i)->count() > count) {
418 if (m_barSets.at(i)->count() > count) {
418 count = m_barSets.at(i)->count();
419 count = m_barSets.at(i)->count();
419 }
420 }
420 }
421 }
421
422
422 return count;
423 return count;
423 }
424 }
424
425
425 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
426 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
426 {
427 {
427 if (width < 0.0) {
428 if (width < 0.0) {
428 width = 0.0;
429 width = 0.0;
429 }
430 }
430 m_barWidth = width;
431 m_barWidth = width;
431 emit updatedBars();
432 emit updatedBars();
432 }
433 }
433
434
434 qreal QAbstractBarSeriesPrivate::barWidth() const
435 qreal QAbstractBarSeriesPrivate::barWidth() const
435 {
436 {
436 return m_barWidth;
437 return m_barWidth;
437 }
438 }
438
439
439 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
440 QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index)
440 {
441 {
441 return m_barSets.at(index);
442 return m_barSets.at(index);
442 }
443 }
443
444
444 void QAbstractBarSeriesPrivate::setVisible(bool visible)
445 void QAbstractBarSeriesPrivate::setVisible(bool visible)
445 {
446 {
446 m_visible = visible;
447 m_visible = visible;
447 emit updatedBars();
448 emit updatedBars();
448 }
449 }
449
450
450 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
451 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
451 {
452 {
452 m_labelsVisible = visible;
453 m_labelsVisible = visible;
453 emit labelsVisibleChanged(visible);
454 emit labelsVisibleChanged(visible);
454 }
455 }
455
456
456 void QAbstractBarSeriesPrivate::setGrouping(bool grouping)
457 void QAbstractBarSeriesPrivate::setOverlap(bool overlap)
457 {
458 {
458 if (m_grouping != grouping) {
459 if (m_overlap != overlap) {
459 m_grouping = grouping;
460 m_overlap = overlap;
460 emit updatedBars();
461 emit updatedBars();
461 }
462 }
462 }
463 }
463
464
464 qreal QAbstractBarSeriesPrivate::min()
465 qreal QAbstractBarSeriesPrivate::min()
465 {
466 {
466 if (m_barSets.count() <= 0) {
467 if (m_barSets.count() <= 0) {
467 return 0;
468 return 0;
468 }
469 }
469 qreal min = INT_MAX;
470 qreal min = INT_MAX;
470
471
471 for (int i = 0; i < m_barSets.count(); i++) {
472 for (int i = 0; i < m_barSets.count(); i++) {
472 int categoryCount = m_barSets.at(i)->count();
473 int categoryCount = m_barSets.at(i)->count();
473 for (int j = 0; j < categoryCount; j++) {
474 for (int j = 0; j < categoryCount; j++) {
474 qreal temp = m_barSets.at(i)->at(j);
475 qreal temp = m_barSets.at(i)->at(j);
475 if (temp < min)
476 if (temp < min)
476 min = temp;
477 min = temp;
477 }
478 }
478 }
479 }
479 return min;
480 return min;
480 }
481 }
481
482
482 qreal QAbstractBarSeriesPrivate::max()
483 qreal QAbstractBarSeriesPrivate::max()
483 {
484 {
484 if (m_barSets.count() <= 0) {
485 if (m_barSets.count() <= 0) {
485 return 0;
486 return 0;
486 }
487 }
487 qreal max = INT_MIN;
488 qreal max = INT_MIN;
488
489
489 for (int i = 0; i < m_barSets.count(); i++) {
490 for (int i = 0; i < m_barSets.count(); i++) {
490 int categoryCount = m_barSets.at(i)->count();
491 int categoryCount = m_barSets.at(i)->count();
491 for (int j = 0; j < categoryCount; j++) {
492 for (int j = 0; j < categoryCount; j++) {
492 qreal temp = m_barSets.at(i)->at(j);
493 qreal temp = m_barSets.at(i)->at(j);
493 if (temp > max)
494 if (temp > max)
494 max = temp;
495 max = temp;
495 }
496 }
496 }
497 }
497
498
498 return max;
499 return max;
499 }
500 }
500
501
501 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
502 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
502 {
503 {
503 if ((set < 0) || (set >= m_barSets.count())) {
504 if ((set < 0) || (set >= m_barSets.count())) {
504 // No set, no value.
505 // No set, no value.
505 return 0;
506 return 0;
506 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
507 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
507 // No category, no value.
508 // No category, no value.
508 return 0;
509 return 0;
509 }
510 }
510
511
511 return m_barSets.at(set)->at(category);
512 return m_barSets.at(set)->at(category);
512 }
513 }
513
514
514 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
515 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
515 {
516 {
516 if ((set < 0) || (set >= m_barSets.count())) {
517 if ((set < 0) || (set >= m_barSets.count())) {
517 // No set, no value.
518 // No set, no value.
518 return 0;
519 return 0;
519 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
520 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
520 // No category, no value.
521 // No category, no value.
521 return 0;
522 return 0;
522 }
523 }
523
524
524 qreal value = m_barSets.at(set)->at(category);
525 qreal value = m_barSets.at(set)->at(category);
525 qreal sum = categorySum(category);
526 qreal sum = categorySum(category);
526 if ( qFuzzyIsNull(sum) ) {
527 if ( qFuzzyIsNull(sum) ) {
527 return 0;
528 return 0;
528 }
529 }
529
530
530 return value / sum;
531 return value / sum;
531 }
532 }
532
533
533 qreal QAbstractBarSeriesPrivate::categorySum(int category)
534 qreal QAbstractBarSeriesPrivate::categorySum(int category)
534 {
535 {
535 qreal sum(0);
536 qreal sum(0);
536 int count = m_barSets.count(); // Count sets
537 int count = m_barSets.count(); // Count sets
537 for (int set = 0; set < count; set++) {
538 for (int set = 0; set < count; set++) {
538 if (category < m_barSets.at(set)->count())
539 if (category < m_barSets.at(set)->count())
539 sum += m_barSets.at(set)->at(category);
540 sum += m_barSets.at(set)->at(category);
540 }
541 }
541 return sum;
542 return sum;
542 }
543 }
543
544
544 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
545 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
545 {
546 {
546 qreal sum(0);
547 qreal sum(0);
547 int count = m_barSets.count(); // Count sets
548 int count = m_barSets.count(); // Count sets
548 for (int set = 0; set < count; set++) {
549 for (int set = 0; set < count; set++) {
549 if (category < m_barSets.at(set)->count())
550 if (category < m_barSets.at(set)->count())
550 sum += qAbs(m_barSets.at(set)->at(category));
551 sum += qAbs(m_barSets.at(set)->at(category));
551 }
552 }
552 return sum;
553 return sum;
553 }
554 }
554
555
555 qreal QAbstractBarSeriesPrivate::maxCategorySum()
556 qreal QAbstractBarSeriesPrivate::maxCategorySum()
556 {
557 {
557 qreal max = INT_MIN;
558 qreal max = INT_MIN;
558 int count = categoryCount();
559 int count = categoryCount();
559 for (int i = 0; i < count; i++) {
560 for (int i = 0; i < count; i++) {
560 qreal sum = categorySum(i);
561 qreal sum = categorySum(i);
561 if (sum > max)
562 if (sum > max)
562 max = sum;
563 max = sum;
563 }
564 }
564 return max;
565 return max;
565 }
566 }
566
567
567 qreal QAbstractBarSeriesPrivate::minX()
568 qreal QAbstractBarSeriesPrivate::minX()
568 {
569 {
569 if (m_barSets.count() <= 0) {
570 if (m_barSets.count() <= 0) {
570 return 0;
571 return 0;
571 }
572 }
572 qreal min = INT_MAX;
573 qreal min = INT_MAX;
573
574
574 for (int i = 0; i < m_barSets.count(); i++) {
575 for (int i = 0; i < m_barSets.count(); i++) {
575 int categoryCount = m_barSets.at(i)->count();
576 int categoryCount = m_barSets.at(i)->count();
576 for (int j = 0; j < categoryCount; j++) {
577 for (int j = 0; j < categoryCount; j++) {
577 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
578 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
578 if (temp < min)
579 if (temp < min)
579 min = temp;
580 min = temp;
580 }
581 }
581 }
582 }
582 return min;
583 return min;
583 }
584 }
584
585
585 qreal QAbstractBarSeriesPrivate::maxX()
586 qreal QAbstractBarSeriesPrivate::maxX()
586 {
587 {
587 if (m_barSets.count() <= 0) {
588 if (m_barSets.count() <= 0) {
588 return 0;
589 return 0;
589 }
590 }
590 qreal max = INT_MIN;
591 qreal max = INT_MIN;
591
592
592 for (int i = 0; i < m_barSets.count(); i++) {
593 for (int i = 0; i < m_barSets.count(); i++) {
593 int categoryCount = m_barSets.at(i)->count();
594 int categoryCount = m_barSets.at(i)->count();
594 for (int j = 0; j < categoryCount; j++) {
595 for (int j = 0; j < categoryCount; j++) {
595 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
596 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
596 if (temp > max)
597 if (temp > max)
597 max = temp;
598 max = temp;
598 }
599 }
599 }
600 }
600
601
601 return max;
602 return max;
602 }
603 }
603
604
604
605
605 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
606 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
606 {
607 {
607 qreal minX(domain.minX());
608 qreal minX(domain.minX());
608 qreal minY(domain.minY());
609 qreal minY(domain.minY());
609 qreal maxX(domain.maxX());
610 qreal maxX(domain.maxX());
610 qreal maxY(domain.maxY());
611 qreal maxY(domain.maxY());
611
612
612 qreal seriesMinX = this->minX();
613 qreal seriesMinX = this->minX();
613 qreal seriesMaxX = this->maxX();
614 qreal seriesMaxX = this->maxX();
614 qreal y = max();
615 qreal y = max();
615 minX = qMin(minX, seriesMinX - (qreal)0.5);
616 minX = qMin(minX, seriesMinX - (qreal)0.5);
616 minY = qMin(minY, y);
617 minY = qMin(minY, y);
617 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
618 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
618 maxY = qMax(maxY, y);
619 maxY = qMax(maxY, y);
619
620
620 domain.setRange(minX,maxX,minY,maxY);
621 domain.setRange(minX,maxX,minY,maxY);
621 }
622 }
622
623
623 ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
624 ChartElement* QAbstractBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
624 {
625 {
625 Q_UNUSED(presenter);
626 Q_UNUSED(presenter);
626 qWarning() << "QAbstractBarSeriesPrivate::createGraphics called";
627 qWarning() << "QAbstractBarSeriesPrivate::createGraphics called";
627 return 0;
628 return 0;
628 }
629 }
629
630
630 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
631 QList<LegendMarker*> QAbstractBarSeriesPrivate::createLegendMarker(QLegend* legend)
631 {
632 {
632 Q_Q(QAbstractBarSeries);
633 Q_Q(QAbstractBarSeries);
633 QList<LegendMarker*> markers;
634 QList<LegendMarker*> markers;
634 foreach(QBarSet* set, q->barSets()) {
635 foreach(QBarSet* set, q->barSets()) {
635 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
636 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
636 markers << marker;
637 markers << marker;
637 }
638 }
638
639
639 return markers;
640 return markers;
640 }
641 }
641
642
642 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
643 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
643 {
644 {
644 Q_Q(QAbstractBarSeries);
645 Q_Q(QAbstractBarSeries);
645 if ((m_barSets.contains(set)) || (set == 0)) {
646 if ((m_barSets.contains(set)) || (set == 0)) {
646 // Fail if set is already in list or set is null.
647 // Fail if set is already in list or set is null.
647 return false;
648 return false;
648 }
649 }
649 m_barSets.append(set);
650 m_barSets.append(set);
650 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
651 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
651 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
652 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
652 emit restructuredBars(); // this notifies barchartitem
653 emit restructuredBars(); // this notifies barchartitem
653 if (m_dataset) {
654 if (m_dataset) {
654 m_dataset->updateSeries(q); // this notifies legend
655 m_dataset->updateSeries(q); // this notifies legend
655 }
656 }
656 return true;
657 return true;
657 }
658 }
658
659
659 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
660 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
660 {
661 {
661 Q_Q(QAbstractBarSeries);
662 Q_Q(QAbstractBarSeries);
662 if (!m_barSets.contains(set)) {
663 if (!m_barSets.contains(set)) {
663 // Fail if set is not in list
664 // Fail if set is not in list
664 return false;
665 return false;
665 }
666 }
666 m_barSets.removeOne(set);
667 m_barSets.removeOne(set);
667 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
668 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
668 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
669 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
669 emit restructuredBars(); // this notifies barchartitem
670 emit restructuredBars(); // this notifies barchartitem
670 if (m_dataset) {
671 if (m_dataset) {
671 m_dataset->updateSeries(q); // this notifies legend
672 m_dataset->updateSeries(q); // this notifies legend
672 }
673 }
673 return true;
674 return true;
674 }
675 }
675
676
676 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
677 bool QAbstractBarSeriesPrivate::append(QList<QBarSet* > sets)
677 {
678 {
678 Q_Q(QAbstractBarSeries);
679 Q_Q(QAbstractBarSeries);
679 foreach (QBarSet* set, sets) {
680 foreach (QBarSet* set, sets) {
680 if ((set == 0) || (m_barSets.contains(set))) {
681 if ((set == 0) || (m_barSets.contains(set))) {
681 // Fail if any of the sets is null or is already appended.
682 // Fail if any of the sets is null or is already appended.
682 return false;
683 return false;
683 }
684 }
684 if (sets.count(set) != 1) {
685 if (sets.count(set) != 1) {
685 // Also fail if same set is more than once in given list.
686 // Also fail if same set is more than once in given list.
686 return false;
687 return false;
687 }
688 }
688 }
689 }
689
690
690 foreach (QBarSet* set, sets) {
691 foreach (QBarSet* set, sets) {
691 m_barSets.append(set);
692 m_barSets.append(set);
692 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
693 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
693 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
694 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
694 }
695 }
695 emit restructuredBars(); // this notifies barchartitem
696 emit restructuredBars(); // this notifies barchartitem
696 if (m_dataset) {
697 if (m_dataset) {
697 m_dataset->updateSeries(q); // this notifies legend
698 m_dataset->updateSeries(q); // this notifies legend
698 }
699 }
699 return true;
700 return true;
700 }
701 }
701
702
702 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
703 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet* > sets)
703 {
704 {
704 Q_Q(QAbstractBarSeries);
705 Q_Q(QAbstractBarSeries);
705 if (sets.count() == 0) {
706 if (sets.count() == 0) {
706 return false;
707 return false;
707 }
708 }
708 foreach (QBarSet* set, sets) {
709 foreach (QBarSet* set, sets) {
709 if ((set == 0) || (!m_barSets.contains(set))) {
710 if ((set == 0) || (!m_barSets.contains(set))) {
710 // Fail if any of the sets is null or is not in series
711 // Fail if any of the sets is null or is not in series
711 return false;
712 return false;
712 }
713 }
713 if (sets.count(set) != 1) {
714 if (sets.count(set) != 1) {
714 // Also fail if same set is more than once in given list.
715 // Also fail if same set is more than once in given list.
715 return false;
716 return false;
716 }
717 }
717 }
718 }
718
719
719 foreach (QBarSet* set, sets) {
720 foreach (QBarSet* set, sets) {
720 m_barSets.removeOne(set);
721 m_barSets.removeOne(set);
721 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
722 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
722 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
723 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
723 }
724 }
724
725
725 emit restructuredBars(); // this notifies barchartitem
726 emit restructuredBars(); // this notifies barchartitem
726 if (m_dataset) {
727 if (m_dataset) {
727 m_dataset->updateSeries(q); // this notifies legend
728 m_dataset->updateSeries(q); // this notifies legend
728 }
729 }
729 return true;
730 return true;
730 }
731 }
731
732
732 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
733 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
733 {
734 {
734 Q_Q(QAbstractBarSeries);
735 Q_Q(QAbstractBarSeries);
735 if ((m_barSets.contains(set)) || (set == 0)) {
736 if ((m_barSets.contains(set)) || (set == 0)) {
736 // Fail if set is already in list or set is null.
737 // Fail if set is already in list or set is null.
737 return false;
738 return false;
738 }
739 }
739 m_barSets.insert(index, set);
740 m_barSets.insert(index, set);
740 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
741 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
741 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
742 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
742 emit restructuredBars(); // this notifies barchartitem
743 emit restructuredBars(); // this notifies barchartitem
743 if (m_dataset) {
744 if (m_dataset) {
744 m_dataset->updateSeries(q); // this notifies legend
745 m_dataset->updateSeries(q); // this notifies legend
745 }
746 }
746 return true;
747 return true;
747 }
748 }
748
749
749 void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis)
750 void QAbstractBarSeriesPrivate::initializeAxis(QAbstractAxis* axis)
750 {
751 {
751 Q_Q(QAbstractBarSeries);
752 Q_Q(QAbstractBarSeries);
752
753
753 if(axis->type()==QAbstractAxis::AxisTypeBarCategory) {
754 if(axis->type()==QAbstractAxis::AxisTypeBarCategory) {
754
755
755 switch(q->type()) {
756 switch(q->type()) {
756
757
757 case QAbstractSeries::SeriesTypeHorizontalBar:
758 case QAbstractSeries::SeriesTypeHorizontalBar:
758 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
759 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
759 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
760 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
760
761
761 if(axis->orientation()==Qt::Vertical)
762 if(axis->orientation()==Qt::Vertical)
762 {
763 {
763 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
764 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
764 }
765 }
765 break;
766 break;
766 }
767 }
767 case QAbstractSeries::SeriesTypeBar:
768 case QAbstractSeries::SeriesTypeBar:
768 case QAbstractSeries::SeriesTypePercentBar:
769 case QAbstractSeries::SeriesTypePercentBar:
769 case QAbstractSeries::SeriesTypeStackedBar: {
770 case QAbstractSeries::SeriesTypeStackedBar: {
770
771
771 if(axis->orientation()==Qt::Horizontal)
772 if(axis->orientation()==Qt::Horizontal)
772 {
773 {
773 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
774 populateCategories(qobject_cast<QBarCategoryAxis*>(axis));
774 }
775 }
775 break;
776 break;
776 }
777 }
777 default:
778 default:
778 qWarning()<<"Unexpected series type";
779 qWarning()<<"Unexpected series type";
779 break;
780 break;
780
781
781 }
782 }
782 }
783 }
783 }
784 }
784
785
785 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
786 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
786 {
787 {
787 Q_Q(const QAbstractBarSeries);
788 Q_Q(const QAbstractBarSeries);
788
789
789 switch(q->type()) {
790 switch(q->type()) {
790
791
791 case QAbstractSeries::SeriesTypeHorizontalBar:
792 case QAbstractSeries::SeriesTypeHorizontalBar:
792 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
793 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
793 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
794 case QAbstractSeries::SeriesTypeHorizontalStackedBar: {
794
795
795 if(orientation==Qt::Vertical)
796 if(orientation==Qt::Vertical)
796 {
797 {
797 return QAbstractAxis::AxisTypeBarCategory;
798 return QAbstractAxis::AxisTypeBarCategory;
798 }
799 }
799 break;
800 break;
800 }
801 }
801 case QAbstractSeries::SeriesTypeBar:
802 case QAbstractSeries::SeriesTypeBar:
802 case QAbstractSeries::SeriesTypePercentBar:
803 case QAbstractSeries::SeriesTypePercentBar:
803 case QAbstractSeries::SeriesTypeStackedBar: {
804 case QAbstractSeries::SeriesTypeStackedBar: {
804
805
805 if(orientation==Qt::Horizontal)
806 if(orientation==Qt::Horizontal)
806 {
807 {
807 return QAbstractAxis::AxisTypeBarCategory;
808 return QAbstractAxis::AxisTypeBarCategory;
808 }
809 }
809 break;
810 break;
810 }
811 }
811 default:
812 default:
812 qWarning()<<"Unexpected series type";
813 qWarning()<<"Unexpected series type";
813 break;
814 break;
814
815
815 }
816 }
816 return QAbstractAxis::AxisTypeValue;
817 return QAbstractAxis::AxisTypeValue;
817
818
818 }
819 }
819
820
820 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis)
821 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis* axis)
821 {
822 {
822 QStringList categories;
823 QStringList categories;
823 if(axis->categories().isEmpty()) {
824 if(axis->categories().isEmpty()) {
824 for (int i(1); i < categoryCount()+1; i++)
825 for (int i(1); i < categoryCount()+1; i++)
825 categories << QString::number(i);
826 categories << QString::number(i);
826 axis->append(categories);
827 axis->append(categories);
827 }
828 }
828 }
829 }
829
830
830 #include "moc_qabstractbarseries.cpp"
831 #include "moc_qabstractbarseries.cpp"
831 #include "moc_qabstractbarseries_p.cpp"
832 #include "moc_qabstractbarseries_p.cpp"
832
833
833
834
834 QTCOMMERCIALCHART_END_NAMESPACE
835 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,89
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 take(QBarSet *set);
52 bool take(QBarSet *set);
53 bool append(QList<QBarSet* > sets);
53 bool append(QList<QBarSet* > sets);
54 bool insert(int index, QBarSet *set);
54 bool insert(int index, QBarSet *set);
55 int count() const;
55 int count() const;
56 QList<QBarSet*> barSets() const;
56 QList<QBarSet*> barSets() const;
57 void clear();
57 void clear();
58
58
59 void setLabelsVisible(bool visible = true);
59 void setLabelsVisible(bool visible = true);
60 bool isLabelsVisible() const;
60 bool isLabelsVisible() const;
61
61
62 void setGrouping(bool grouping = true);
62 void setOverlap(bool overlap = true);
63
63
64 protected:
64 protected:
65 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0);
65 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d,QObject *parent = 0);
66
66
67 Q_SIGNALS:
67 Q_SIGNALS:
68 void clicked(int index, QBarSet *barset);
68 void clicked(int index, QBarSet *barset);
69 void hovered(bool status, QBarSet *barset);
69 void hovered(bool status, QBarSet *barset);
70 void countChanged();
70 void countChanged();
71 void labelsVisibleChanged();
71 void labelsVisibleChanged();
72
72
73 void barsetsAdded(QList<QBarSet*> sets);
73 void barsetsAdded(QList<QBarSet*> sets);
74 void barsetsRemoved(QList<QBarSet*> sets);
74 void barsetsRemoved(QList<QBarSet*> sets);
75
75
76 protected:
76 protected:
77 Q_DECLARE_PRIVATE(QAbstractBarSeries)
77 Q_DECLARE_PRIVATE(QAbstractBarSeries)
78 friend class AbstractBarChartItem;
78 friend class AbstractBarChartItem;
79 friend class PercentBarChartItem;
79 friend class PercentBarChartItem;
80 friend class StackedBarChartItem;
80 friend class StackedBarChartItem;
81 friend class BarChartItem;
81 friend class BarChartItem;
82 friend class HorizontalBarChartItem;
82 friend class HorizontalBarChartItem;
83 friend class HorizontalStackedBarChartItem;
83 friend class HorizontalStackedBarChartItem;
84 friend class HorizontalPercentBarChartItem;
84 friend class HorizontalPercentBarChartItem;
85 };
85 };
86
86
87 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
88
88
89 #endif // ABSTRACTBARSERIES_H
89 #endif // ABSTRACTBARSERIES_H
@@ -1,105 +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 QBarCategoryAxis;
41 class QBarCategoryAxis;
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 void setOverlap(bool overlap);
56
56
57 void scaleDomain(Domain& domain);
57 void scaleDomain(Domain& domain);
58 ChartElement* createGraphics(ChartPresenter* presenter);
58 ChartElement* createGraphics(ChartPresenter* presenter);
59 QList<LegendMarker*> createLegendMarker(QLegend* legend);
59 QList<LegendMarker*> createLegendMarker(QLegend* legend);
60
60
61 void initializeAxis(QAbstractAxis* axis);
61 void initializeAxis(QAbstractAxis* axis);
62 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
62 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
63
63
64 bool append(QBarSet *set);
64 bool append(QBarSet *set);
65 bool remove(QBarSet *set);
65 bool remove(QBarSet *set);
66 bool append(QList<QBarSet* > sets);
66 bool append(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
67 bool remove(QList<QBarSet* > sets);
68 bool insert(int index, QBarSet *set);
68 bool insert(int index, QBarSet *set);
69
69
70 QBarSet* barsetAt(int index);
70 QBarSet* barsetAt(int index);
71 qreal min();
71 qreal min();
72 qreal max();
72 qreal max();
73 qreal valueAt(int set, int category);
73 qreal valueAt(int set, int category);
74 qreal percentageAt(int set, int category);
74 qreal percentageAt(int set, int category);
75 qreal categorySum(int category);
75 qreal categorySum(int category);
76 qreal absoluteCategorySum(int category);
76 qreal absoluteCategorySum(int category);
77 qreal maxCategorySum();
77 qreal maxCategorySum();
78 qreal minX();
78 qreal minX();
79 qreal maxX();
79 qreal maxX();
80
80
81 Q_SIGNALS:
81 Q_SIGNALS:
82 void clicked(int index, QBarSet *barset);
82 void clicked(int index, QBarSet *barset);
83 void updatedBars();
83 void updatedBars();
84 void restructuredBars();
84 void restructuredBars();
85 void labelsVisibleChanged(bool visible);
85 void labelsVisibleChanged(bool visible);
86
86
87 private:
87 private:
88 void populateCategories(QBarCategoryAxis* axis);
88 void populateCategories(QBarCategoryAxis* axis);
89
89
90 protected:
90 protected:
91 QList<QBarSet *> m_barSets;
91 QList<QBarSet *> m_barSets;
92 qreal m_barWidth;
92 qreal m_barWidth;
93 bool m_labelsVisible;
93 bool m_labelsVisible;
94 bool m_visible;
94 bool m_visible;
95 bool m_grouping;
95 bool m_overlap;
96
96
97 private:
97 private:
98 Q_DECLARE_PUBLIC(QAbstractBarSeries)
98 Q_DECLARE_PUBLIC(QAbstractBarSeries)
99 friend class HorizontalBarChartItem;
99 friend class HorizontalBarChartItem;
100 friend class BarChartItem;
100 friend class BarChartItem;
101 };
101 };
102
102
103 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
104
104
105 #endif // QABSTRACTBARSERIES_P_H
105 #endif // QABSTRACTBARSERIES_P_H
@@ -1,109 +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;
51 qreal barWidth;
52
52
53 if (m_series->d_func()->m_grouping) {
53 if (m_series->d_func()->m_overlap) {
54 barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
55 } else {
56 barWidth = scaleX * m_series->d_func()->barWidth();
54 barWidth = scaleX * m_series->d_func()->barWidth();
55 } else {
56 barWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
57 }
57 }
58
58
59 int itemIndex(0);
59 int itemIndex(0);
60 for (int category = 0; category < categoryCount; category++) {
60 for (int category = 0; category < categoryCount; category++) {
61 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
61 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
62 for (int set = 0; set < setCount; set++) {
62 for (int set = 0; set < setCount; set++) {
63 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
63 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
64
64
65 qreal xPos;
65 qreal xPos;
66
66
67 if (m_series->d_func()->m_grouping) {
67 if (m_series->d_func()->m_overlap) {
68 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
69 } else {
68 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left();
70 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left();
69 xPos -= setCount*barWidth/2;
71 xPos -= setCount*barWidth/2;
70 xPos += set*barWidth;
72 xPos += set*barWidth;
71 } else {
72 xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
73 }
73 }
74
74
75 qreal barHeight = barSet->value(category) * scaleY;
75 qreal barHeight = barSet->value(category) * scaleY;
76 Bar* bar = m_bars.at(itemIndex);
76 Bar* bar = m_bars.at(itemIndex);
77
77
78 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
78 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
79 layout.append(rect);
79 layout.append(rect);
80 bar->setPen(barSet->m_pen);
80 bar->setPen(barSet->m_pen);
81 bar->setBrush(barSet->m_brush);
81 bar->setBrush(barSet->m_brush);
82 if (qFuzzyIsNull(barHeight)) {
82 if (qFuzzyIsNull(barHeight)) {
83 bar->setVisible(false);
83 bar->setVisible(false);
84 } else {
84 } else {
85 bar->setVisible(barsVisible);
85 bar->setVisible(barsVisible);
86 }
86 }
87
87
88 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
88 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
89
89
90 if (!qFuzzyIsNull(barSet->value(category))) {
90 if (!qFuzzyIsNull(barSet->value(category))) {
91 label->setText(QString::number(barSet->value(category)));
91 label->setText(QString::number(barSet->value(category)));
92 } else {
92 } else {
93 label->setText(QString(""));
93 label->setText(QString(""));
94 }
94 }
95
95
96 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
96 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
97 ,yPos - barHeight/2 - label->boundingRect().height()/2);
97 ,yPos - barHeight/2 - label->boundingRect().height()/2);
98 label->setFont(barSet->m_labelFont);
98 label->setFont(barSet->m_labelFont);
99 label->setBrush(barSet->m_labelBrush);
99 label->setBrush(barSet->m_labelBrush);
100
100
101 itemIndex++;
101 itemIndex++;
102 }
102 }
103 }
103 }
104 return layout;
104 return layout;
105 }
105 }
106
106
107 #include "moc_barchartitem_p.cpp"
107 #include "moc_barchartitem_p.cpp"
108
108
109 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now