##// END OF EJS Templates
fix to baranimations leaving crap on screen with negative bars
sauimone -
r1824:4fa1dedc5aa7
parent child
Show More
@@ -1,93 +1,94
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QBarSeries>
25 25 #include <QBarSet>
26 26 #include <QLegend>
27 27 #include <QBarCategoryAxis>
28 28 #include <QHorizontalBarSeries>
29 29
30 30 QTCOMMERCIALCHART_USE_NAMESPACE
31 31
32 32 int main(int argc, char *argv[])
33 33 {
34 34 QApplication a(argc, argv);
35 35
36 36 //![1]
37 37 QBarSet *male = new QBarSet("Male");
38 38 QBarSet *female = new QBarSet("Female");
39 39
40 40 // From wikipedia
41 41 *male << -145596 << -149894 << -167327 << -164118 << -170710 << -169408 << -158395 << -176975 << -191803 << -191198 << -196815
42 42 << -207826 << -145517 << -113204 << -90986 << -70909 << -40013 << -15887 << -5769;
43 43
44 44 *female << 139353 << 143522 << 161212 << 156824 << 163226 << 161766 << 150595 << 170779 << 185979 << 187897 << 196158
45 45 << 209062 << 152460 << 129230 << 114778 << 109611 << 83845 << 44605 << 22328;
46 46
47 47 //![1]
48 48
49 49 //![2]
50 50 QHorizontalBarSeries *series = new QHorizontalBarSeries();
51 51 series->append(male);
52 52 series->append(female);
53 53 series->setBarWidth(0.5);
54 54 series->setGrouping(false);
55 55 //![2]
56 56
57 57 //![3]
58 58 QChart* chart = new QChart();
59 59 chart->addSeries(series);
60 60 chart->setTitle("Population of Finland in 2005 by age group");
61 61 chart->createDefaultAxes();
62 62 chart->setAnimationOptions(QChart::SeriesAnimations);
63 63 //![3]
64 64
65 65 //![4]
66 66 QStringList categories;
67 67 categories << "0-4" << "5-9" << "10-14" << "15-19" << "20-24" << "25-29" << "30-34" << "35-39" << "40-44" << "45-49"
68 68 << "50-54" << "55-59" << "60-64" << "65-69" << "70-74" << "75-79" << "80-84" << "85-89" << "90-";
69 69
70 70 QBarCategoryAxis* axis = new QBarCategoryAxis();
71 71 axis->append(categories);
72 72 chart->setAxisY(axis,series);
73 chart->axisX(series)->setRange(-210000,210000);
73 74 //![4]
74 75
75 76 //![5]
76 77 chart->legend()->setVisible(true);
77 78 chart->legend()->setAlignment(Qt::AlignBottom);
78 79 //![5]
79 80
80 81 //![6]
81 82 QChartView* chartView = new QChartView(chart);
82 83 chartView->setRenderHint(QPainter::Antialiasing);
83 84 //![6]
84 85
85 86 //![7]
86 87 QMainWindow window;
87 88 window.setCentralWidget(chartView);
88 89 window.resize(400, 800);
89 90 window.show();
90 91 //![7]
91 92
92 93 return a.exec();
93 94 }
@@ -1,61 +1,61
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "baranimation_p.h"
22 22 #include "abstractbarchartitem_p.h"
23 23 #include <QTimer>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QRectF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 BarAnimation::BarAnimation(AbstractBarChartItem *item)
30 30 :AbstractBarAnimation(item)
31 31 {
32 32
33 33 }
34 34
35 35 BarAnimation::~BarAnimation()
36 36 {
37 37 }
38 38
39 39 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
40 40 {
41 41 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
42 42 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
43 43 QVector<QRectF> result;
44 44
45 45 Q_ASSERT(startVector.count() == endVector.count());
46 46
47 47 for(int i = 0; i < startVector.count(); i++) {
48 48 qreal w = endVector[i].width();
49 49 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
50 50 qreal x = endVector[i].topLeft().x();
51 51 qreal y = endVector[i].topLeft().y() + endVector[i].height() - h;
52 52
53 53 QRectF value(x,y,w,h);
54 result << value;
54 result << value.normalized();
55 55 }
56 56 return qVariantFromValue(result);
57 57 }
58 58
59 59 #include "moc_baranimation_p.cpp"
60 60
61 61 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,62
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "horizontalbaranimation_p.h"
22 22 #include "abstractbarchartitem_p.h"
23 23 #include <QTimer>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QRectF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 HorizontalBarAnimation::HorizontalBarAnimation(AbstractBarChartItem *item) :
30 30 AbstractBarAnimation(item)
31 31 {
32 32 }
33 33
34 34 HorizontalBarAnimation::~HorizontalBarAnimation()
35 35 {
36 36
37 37 }
38 38
39 39
40 40 QVariant HorizontalBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 41 {
42 42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 44 QVector<QRectF> result;
45 45
46 46 Q_ASSERT(startVector.count() == endVector.count());
47 47
48 48 for(int i = 0; i < startVector.count(); i++) {
49 49 qreal h = endVector[i].height();
50 50 qreal w = startVector[i].width() + ((endVector[i].width() - startVector[i].width()) * progress);
51 51 qreal x = endVector[i].left();
52 52 qreal y = endVector[i].top();
53 53
54 54 QRectF value(x,y,w,h);
55 result << value;
55 result << value.normalized();
56 56 }
57 57 return qVariantFromValue(result);
58 58 }
59 59
60 60 #include "moc_horizontalbaranimation_p.cpp"
61 61
62 62 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,64
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "horizontalpercentbaranimation_p.h"
22 22 #include "abstractbarchartitem_p.h"
23 23 #include <QTimer>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QRectF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 HorizontalPercentBarAnimation::HorizontalPercentBarAnimation(AbstractBarChartItem *item) :
30 30 AbstractBarAnimation(item)
31 31 {
32 32 }
33 33
34 34 HorizontalPercentBarAnimation::~HorizontalPercentBarAnimation()
35 35 {
36 36
37 37 }
38 38
39 39
40 40 QVariant HorizontalPercentBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 41 {
42 42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 44 QVector<QRectF> result;
45 45
46 46 Q_ASSERT(startVector.count() == endVector.count());
47 47
48 48 qreal xAxis = m_item->geometry().x();
49 49
50 50 for(int i = 0; i < startVector.count(); i++) {
51 51 qreal h = endVector[i].height();
52 52 qreal w = endVector[i].width() * progress;
53 53 qreal x = xAxis + ((endVector[i].left() - xAxis) * progress);
54 54 qreal y = endVector[i].top();
55 55
56 56 QRectF value(x,y,w,h);
57 result << value;
57 result << value.normalized();
58 58 }
59 59 return qVariantFromValue(result);
60 60 }
61 61
62 62 #include "moc_horizontalpercentbaranimation_p.cpp"
63 63
64 64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,64
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "horizontalstackedbaranimation_p.h"
22 22 #include "abstractbarchartitem_p.h"
23 23 #include <QTimer>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QRectF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 HorizontalStackedBarAnimation::HorizontalStackedBarAnimation(AbstractBarChartItem *item) :
30 30 AbstractBarAnimation(item)
31 31 {
32 32 }
33 33
34 34 HorizontalStackedBarAnimation::~HorizontalStackedBarAnimation()
35 35 {
36 36
37 37 }
38 38
39 39
40 40 QVariant HorizontalStackedBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 41 {
42 42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 44 QVector<QRectF> result;
45 45
46 46 Q_ASSERT(startVector.count() == endVector.count());
47 47
48 48 qreal xAxis = m_item->geometry().x();
49 49
50 50 for(int i = 0; i < startVector.count(); i++) {
51 51 qreal h = endVector[i].height();
52 52 qreal w = endVector[i].width() * progress;
53 53 qreal x = xAxis + ((endVector[i].left() - xAxis) * progress);
54 54 qreal y = endVector[i].top();
55 55
56 56 QRectF value(x,y,w,h);
57 result << value;
57 result << value.normalized();
58 58 }
59 59 return qVariantFromValue(result);
60 60 }
61 61
62 62 #include "moc_horizontalstackedbaranimation_p.cpp"
63 63
64 64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,64
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "percentbaranimation_p.h"
22 22 #include "percentbarchartitem_p.h"
23 23 #include <QTimer>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QRectF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 PercentBarAnimation::PercentBarAnimation(PercentBarChartItem *item)
30 30 :AbstractBarAnimation(item)
31 31 {
32 32 setDuration(ChartAnimationDuration);
33 33 setEasingCurve(QEasingCurve::OutQuart);
34 34 }
35 35
36 36 PercentBarAnimation::~PercentBarAnimation()
37 37 {
38 38 }
39 39
40 40 QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 41 {
42 42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 44 QVector<QRectF> result;
45 45
46 46 Q_ASSERT(startVector.count() == endVector.count());
47 47
48 48 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
49 49
50 50 for(int i = 0; i < startVector.count(); i++) {
51 51 qreal w = endVector[i].width();
52 52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 53 qreal x = endVector[i].topLeft().x();
54 54 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
55 55
56 56 QRectF value(x,y,w,h);
57 result << value;
57 result << value.normalized();
58 58 }
59 59 return qVariantFromValue(result);
60 60 }
61 61
62 62 #include "moc_percentbaranimation_p.cpp"
63 63
64 64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,63
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "stackedbaranimation_p.h"
22 22 #include "stackedbarchartitem_p.h"
23 23 #include <QTimer>
24 24
25 25 Q_DECLARE_METATYPE(QVector<QRectF>)
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 StackedBarAnimation::StackedBarAnimation(StackedBarChartItem *item)
30 30 :AbstractBarAnimation(item)
31 31 {
32 32 setEasingCurve(QEasingCurve::OutQuart);
33 33 }
34 34
35 35 StackedBarAnimation::~StackedBarAnimation()
36 36 {
37 37 }
38 38
39 39 QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
40 40 {
41 41 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
42 42 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
43 43 QVector<QRectF> result;
44 44
45 45 Q_ASSERT(startVector.count() == endVector.count());
46 46
47 47 qreal yAxis = m_item->geometry().height() + m_item->geometry().y();
48 48
49 49 for(int i = 0; i < startVector.count(); i++) {
50 50 qreal w = endVector[i].width();
51 51 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
52 52 qreal x = endVector[i].topLeft().x();
53 53 qreal y = yAxis + ((endVector[i].topLeft().y() - yAxis) * progress);
54 54
55 55 QRectF value(x,y,w,h);
56 result << value;
56 result << value.normalized();
57 57 }
58 58 return qVariantFromValue(result);
59 59 }
60 60
61 61 #include "moc_stackedbaranimation_p.cpp"
62 62
63 63 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now