##// END OF EJS Templates
Fix logarithmic axis point calculation...
Titta Heikkala -
r2611:f23ac1927350
parent child
Show More
@@ -1,97 +1,94
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "horizontalbarchartitem_p.h"
22 22 #include "qabstractbarseries_p.h"
23 23 #include "qbarset_p.h"
24 24 #include "bar_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 HorizontalBarChartItem::HorizontalBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item)
29 29 : AbstractBarChartItem(series, item)
30 30 {
31 31 }
32 32
33 33 void HorizontalBarChartItem::initializeLayout()
34 34 {
35 35 qreal categoryCount = m_series->d_func()->categoryCount();
36 36 qreal setCount = m_series->count();
37 37 qreal barWidth = m_series->d_func()->barWidth();
38 38
39 39 m_layout.clear();
40 40 for(int category = 0; category < categoryCount; category++) {
41 41 for (int set = 0; set < setCount; set++) {
42 42 QRectF rect;
43 43 QPointF topLeft;
44 44 QPointF bottomRight;
45 45 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
46 46 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + set/setCount * barWidth), m_validData);
47 47 bottomRight = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + (set + 1)/setCount * barWidth), m_validData);
48 48 } else {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + set/setCount * barWidth), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + (set + 1)/setCount * barWidth), m_validData);
51 51 }
52 52
53 53 if (!m_validData)
54 54 return;
55 55
56 56 rect.setTopLeft(topLeft);
57 57 rect.setBottomRight(bottomRight);
58 58 m_layout.append(rect.normalized());
59 59 }
60 60 }
61 61 }
62 62
63 63 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
64 64 {
65 65 QVector<QRectF> layout;
66 66
67 67 // Use temporary qreals for accuracy
68 68 qreal categoryCount = m_series->d_func()->categoryCount();
69 69 qreal setCount = m_series->count();
70 70 qreal barWidth = m_series->d_func()->barWidth();
71 71
72 72 for(int category = 0; category < categoryCount; category++) {
73 73 for (int set = 0; set < setCount; set++) {
74 74 qreal value = m_series->barSets().at(set)->at(category);
75 75 QRectF rect;
76 76 QPointF topLeft;
77 77 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
78 78 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + set/setCount * barWidth), m_validData);
79 79 else
80 80 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + set/setCount * barWidth), m_validData);
81 81
82 82 QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(value, category - barWidth / 2 + (set + 1)/setCount * barWidth), m_validData);
83 83
84 if (!m_validData)
85 return QVector<QRectF>();
86
87 84 rect.setTopLeft(topLeft);
88 85 rect.setBottomRight(bottomRight);
89 86 layout.append(rect.normalized());
90 87 }
91 88 }
92 89 return layout;
93 90 }
94 91
95 92 #include "moc_horizontalbarchartitem_p.cpp"
96 93
97 94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,128 +1,126
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "horizontalpercentbarchartitem_p.h"
22 22 #include "qabstractbarseries_p.h"
23 23 #include "qbarset_p.h"
24 24 #include "bar_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item)
29 29 : AbstractBarChartItem(series, item)
30 30 {
31 31 }
32 32
33 33 void HorizontalPercentBarChartItem::initializeLayout()
34 34 {
35 35 qreal categoryCount = m_series->d_func()->categoryCount();
36 36 qreal setCount = m_series->count();
37 37 qreal barWidth = m_series->d_func()->barWidth();
38 38
39 39 m_layout.clear();
40 40 for(int category = 0; category < categoryCount; category++) {
41 41 for (int set = 0; set < setCount; set++) {
42 42 QRectF rect;
43 43 QPointF topLeft;
44 44 QPointF bottomRight;
45 45 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
46 46 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2), m_validData);
47 47 bottomRight = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category + barWidth / 2), m_validData);
48 48 } else {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(0, category + barWidth / 2), m_validData);
51 51 }
52 52
53 53 if (!m_validData)
54 54 return;
55 55
56 56 rect.setTopLeft(topLeft);
57 57 rect.setBottomRight(bottomRight);
58 58 m_layout.append(rect.normalized());
59 59 }
60 60 }
61 61 }
62 62
63 63 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
64 64 {
65 65 QVector<QRectF> layout;
66 66
67 67 // Use temporary qreals for accuracy
68 68 qreal categoryCount = m_series->d_func()->categoryCount();
69 69 qreal setCount = m_series->count();
70 70 qreal barWidth = m_series->d_func()->barWidth();
71 71
72 72 for(int category = 0; category < categoryCount; category++) {
73 73 qreal sum = 0;
74 74 qreal categorySum = m_series->d_func()->categorySum(category);
75 75 for (int set = 0; set < setCount; set++) {
76 76 qreal value = m_series->barSets().at(set)->at(category);
77 77 QRectF rect;
78 78 QPointF topLeft;
79 79 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
80 80 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category - barWidth/2), m_validData);
81 81 else
82 82 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category - barWidth/2), m_validData);
83 83 QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(100 * (value + sum)/categorySum, category + barWidth/2), m_validData);
84 84
85 if (!m_validData)
86 return QVector<QRectF>();
87 85 rect.setTopLeft(topLeft);
88 86 rect.setBottomRight(bottomRight);
89 87 layout.append(rect.normalized());
90 88 sum +=value;
91 89 }
92 90 }
93 91 return layout;
94 92 }
95 93
96 94 void HorizontalPercentBarChartItem::handleUpdatedBars()
97 95 {
98 96 // Handle changes in pen, brush, labels etc.
99 97 int categoryCount = m_series->d_func()->categoryCount();
100 98 int setCount = m_series->count();
101 99 int itemIndex(0);
102 100
103 101 for (int category = 0; category < categoryCount; category++) {
104 102 for (int set = 0; set < setCount; set++) {
105 103 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
106 104 Bar *bar = m_bars.at(itemIndex);
107 105 bar->setPen(barSet->m_pen);
108 106 bar->setBrush(barSet->m_brush);
109 107 bar->update();
110 108
111 109 QGraphicsTextItem *label = m_labels.at(itemIndex);
112 110 int p = m_series->d_func()->percentageAt(set, category) * 100;
113 111 QString vString(QString::number(p));
114 112 vString.truncate(3);
115 113 vString.append("%");
116 114 label->setHtml(vString);
117 115 label->setFont(barSet->m_labelFont);
118 116 label->setDefaultTextColor(barSet->m_labelBrush.color());
119 117 label->update();
120 118 itemIndex++;
121 119 }
122 120 }
123 121 }
124 122
125 123 #include "moc_horizontalpercentbarchartitem_p.cpp"
126 124
127 125 QTCOMMERCIALCHART_END_NAMESPACE
128 126
@@ -1,108 +1,107
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "horizontalstackedbarchartitem_p.h"
22 22 #include "qabstractbarseries_p.h"
23 23 #include "qbarset_p.h"
24 24 #include "bar_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 HorizontalStackedBarChartItem::HorizontalStackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item)
29 29 : AbstractBarChartItem(series, item)
30 30 {
31 31 }
32 32
33 33 void HorizontalStackedBarChartItem::initializeLayout()
34 34 {
35 35 qreal categoryCount = m_series->d_func()->categoryCount();
36 36 qreal setCount = m_series->count();
37 37 qreal barWidth = m_series->d_func()->barWidth();
38 38
39 39 m_layout.clear();
40 40 for(int category = 0; category < categoryCount; category++) {
41 41 for (int set = 0; set < setCount; set++) {
42 42 QRectF rect;
43 43 QPointF topLeft;
44 44 QPointF bottomRight;
45 45 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
46 46 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2), m_validData);
47 47 bottomRight = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category + barWidth / 2), m_validData);
48 48 } else {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(0, category + barWidth / 2), m_validData);
51 51 }
52 52
53 53 if (!m_validData)
54 54 return;
55 55
56 56 rect.setTopLeft(topLeft);
57 57 rect.setBottomRight(bottomRight);
58 58 m_layout.append(rect.normalized());
59 59 }
60 60 }
61 61 }
62 62
63 63 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
64 64 {
65 65 QVector<QRectF> layout;
66 66
67 67 // Use temporary qreals for accuracy
68 68 qreal categoryCount = m_series->d_func()->categoryCount();
69 69 qreal setCount = m_series->count();
70 70 qreal barWidth = m_series->d_func()->barWidth();
71 71
72 72 for(int category = 0; category < categoryCount; category++) {
73 73 qreal positiveSum = 0;
74 74 qreal negativeSum = 0;
75 75 for (int set = 0; set < setCount; set++) {
76 76 qreal value = m_series->barSets().at(set)->at(category);
77 77 QRectF rect;
78 78 QPointF topLeft;
79 79 QPointF bottomRight;
80 80 if (value < 0) {
81 81 bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2), m_validData);
82 82 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
83 83 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : domain()->minX(), category + barWidth / 2), m_validData);
84 84 else
85 85 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : 0, category + barWidth / 2), m_validData);
86 86 negativeSum += value;
87 87 } else {
88 88 bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2), m_validData);
89 89 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
90 90 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : domain()->minX(), category + barWidth / 2), m_validData);
91 91 else
92 92 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : 0, category + barWidth / 2), m_validData);
93 93 positiveSum += value;
94 94 }
95 if (!m_validData)
96 return QVector<QRectF>();
95
97 96 rect.setTopLeft(topLeft);
98 97 rect.setBottomRight(bottomRight);
99 98 layout.append(rect.normalized());
100 99 }
101 100 }
102 101 return layout;
103 102 }
104 103
105 104 #include "moc_horizontalstackedbarchartitem_p.cpp"
106 105
107 106 QTCOMMERCIALCHART_END_NAMESPACE
108 107
@@ -1,97 +1,94
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "barchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qabstractbarseries_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarset_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 30 AbstractBarChartItem(series, item)
31 31 {
32 32 }
33 33
34 34 void BarChartItem::initializeLayout()
35 35 {
36 36 qreal categoryCount = m_series->d_func()->categoryCount();
37 37 qreal setCount = m_series->count();
38 38 qreal barWidth = m_series->d_func()->barWidth();
39 39
40 40 m_layout.clear();
41 41 for(int category = 0; category < categoryCount; category++) {
42 42 for (int set = 0; set < setCount; set++) {
43 43 QRectF rect;
44 44 QPointF topLeft;
45 45 QPointF bottomRight;
46 46
47 47 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
48 48 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, domain()->minY()), m_validData);
49 49 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, domain()->minY()), m_validData);
50 50 } else {
51 51 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, 0), m_validData);
52 52 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, 0), m_validData);
53 53 }
54 54
55 55 if (!m_validData)
56 56 return;
57 57 rect.setTopLeft(topLeft);
58 58 rect.setBottomRight(bottomRight);
59 59 m_layout.append(rect.normalized());
60 60 }
61 61 }
62 62 }
63 63
64 64 QVector<QRectF> BarChartItem::calculateLayout()
65 65 {
66 66 QVector<QRectF> layout;
67 67
68 68 // Use temporary qreals for accuracy
69 69 qreal categoryCount = m_series->d_func()->categoryCount();
70 70 qreal setCount = m_series->count();
71 71 qreal barWidth = m_series->d_func()->barWidth();
72 72
73 73 for(int category = 0; category < categoryCount; category++) {
74 74 for (int set = 0; set < setCount; set++) {
75 75 qreal value = m_series->barSets().at(set)->at(category);
76 76 QRectF rect;
77 77 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value), m_validData);
78 78 QPointF bottomRight;
79 79 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
80 80 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()), m_validData);
81 81 else
82 82 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0), m_validData);
83 83
84 if (!m_validData)
85 return QVector<QRectF>();
86
87 84 rect.setTopLeft(topLeft);
88 85 rect.setBottomRight(bottomRight);
89 86 layout.append(rect.normalized());
90 87 }
91 88 }
92 89 return layout;
93 90 }
94 91
95 92 #include "moc_barchartitem_p.cpp"
96 93
97 94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,129 +1,127
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "percentbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qabstractbarseries_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarset_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 30 AbstractBarChartItem(series, item)
31 31 {
32 32 }
33 33
34 34 void PercentBarChartItem::initializeLayout()
35 35 {
36 36 qreal categoryCount = m_series->d_func()->categoryCount();
37 37 qreal setCount = m_series->count();
38 38 qreal barWidth = m_series->d_func()->barWidth();
39 39
40 40 m_layout.clear();
41 41 for(int category = 0; category < categoryCount; category++) {
42 42 for (int set = 0; set < setCount; set++) {
43 43 QRectF rect;
44 44 QPointF topLeft;
45 45 QPointF bottomRight;
46 46
47 47 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
48 48 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
49 49 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
50 50 } else {
51 51 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
52 52 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
53 53 }
54 54
55 55 if (!m_validData)
56 56 return;
57 57
58 58 rect.setTopLeft(topLeft);
59 59 rect.setBottomRight(bottomRight);
60 60 m_layout.append(rect.normalized());
61 61 }
62 62 }
63 63 }
64 64
65 65 QVector<QRectF> PercentBarChartItem::calculateLayout()
66 66 {
67 67 QVector<QRectF> layout;
68 68
69 69 // Use temporary qreals for accuracy
70 70 qreal categoryCount = m_series->d_func()->categoryCount();
71 71 qreal setCount = m_series->count();
72 72 qreal barWidth = m_series->d_func()->barWidth();
73 73
74 74 for(int category = 0; category < categoryCount; category++) {
75 75 qreal sum = 0;
76 76 qreal categorySum = m_series->d_func()->categorySum(category);
77 77 for (int set = 0; set < setCount; set++) {
78 78 qreal value = m_series->barSets().at(set)->at(category);
79 79 QRectF rect;
80 80 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum), m_validData);
81 81 QPointF bottomRight;
82 82 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
83 83 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()), m_validData);
84 84 else
85 85 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0), m_validData);
86 86
87 if (!m_validData)
88 return QVector<QRectF>();
89 87 rect.setTopLeft(topLeft);
90 88 rect.setBottomRight(bottomRight);
91 89 layout.append(rect.normalized());
92 90 sum +=value;
93 91 }
94 92 }
95 93 return layout;
96 94 }
97 95
98 96 void PercentBarChartItem::handleUpdatedBars()
99 97 {
100 98 // Handle changes in pen, brush, labels etc.
101 99 int categoryCount = m_series->d_func()->categoryCount();
102 100 int setCount = m_series->count();
103 101 int itemIndex(0);
104 102
105 103 for (int category = 0; category < categoryCount; category++) {
106 104 for (int set = 0; set < setCount; set++) {
107 105 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
108 106 Bar *bar = m_bars.at(itemIndex);
109 107 bar->setPen(barSet->m_pen);
110 108 bar->setBrush(barSet->m_brush);
111 109 bar->update();
112 110
113 111 QGraphicsTextItem *label = m_labels.at(itemIndex);
114 112 int p = m_series->d_func()->percentageAt(set, category) * 100;
115 113 QString vString(QString::number(p));
116 114 vString.truncate(3);
117 115 vString.append("%");
118 116 label->setHtml(vString);
119 117 label->setFont(barSet->m_labelFont);
120 118 label->setDefaultTextColor(barSet->m_labelBrush.color());
121 119 label->update();
122 120 itemIndex++;
123 121 }
124 122 }
125 123 }
126 124
127 125 #include "moc_percentbarchartitem_p.cpp"
128 126
129 127 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,108 +1,107
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "stackedbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qbarset_p.h"
24 24 #include "qabstractbarseries_p.h"
25 25 #include "qbarset.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 30 AbstractBarChartItem(series, item)
31 31 {
32 32 }
33 33
34 34 void StackedBarChartItem::initializeLayout()
35 35 {
36 36 qreal categoryCount = m_series->d_func()->categoryCount();
37 37 qreal setCount = m_series->count();
38 38 qreal barWidth = m_series->d_func()->barWidth();
39 39
40 40 m_layout.clear();
41 41 for(int category = 0; category < categoryCount; category++) {
42 42 for (int set = 0; set < setCount; set++) {
43 43 QRectF rect;
44 44 QPointF topLeft;
45 45 QPointF bottomRight;
46 46
47 47 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
48 48 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
49 49 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
50 50 } else {
51 51 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
52 52 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
53 53 }
54 54
55 55 if (!m_validData)
56 56 return;
57 57
58 58 rect.setTopLeft(topLeft);
59 59 rect.setBottomRight(bottomRight);
60 60 m_layout.append(rect.normalized());
61 61 }
62 62 }
63 63 }
64 64
65 65 QVector<QRectF> StackedBarChartItem::calculateLayout()
66 66 {
67 67 QVector<QRectF> layout;
68 68 // Use temporary qreals for accuracy
69 69 qreal categoryCount = m_series->d_func()->categoryCount();
70 70 qreal setCount = m_series->count();
71 71 qreal barWidth = m_series->d_func()->barWidth();
72 72
73 73 for(int category = 0; category < categoryCount; category++) {
74 74 qreal positiveSum = 0;
75 75 qreal negativeSum = 0;
76 76 for (int set = 0; set < setCount; set++) {
77 77 qreal value = m_series->barSets().at(set)->at(category);
78 78 QRectF rect;
79 79 QPointF topLeft;
80 80 QPointF bottomRight;
81 81 if (value < 0) {
82 82 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum), m_validData);
83 83 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
84 84 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()), m_validData);
85 85 else
86 86 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0), m_validData);
87 87 negativeSum += value;
88 88 } else {
89 89 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum), m_validData);
90 90 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
91 91 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()), m_validData);
92 92 else
93 93 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0), m_validData);
94 94 positiveSum += value;
95 95 }
96 if (!m_validData)
97 return QVector<QRectF>();
96
98 97 rect.setTopLeft(topLeft);
99 98 rect.setBottomRight(bottomRight);
100 99 layout.append(rect.normalized());
101 100 }
102 101 }
103 102 return layout;
104 103 }
105 104
106 105 #include "moc_stackedbarchartitem_p.cpp"
107 106
108 107 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,263 +1,274
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "logxlogydomain_p.h"
22 22 #include "qabstractaxis_p.h"
23 23 #include "qlogvalueaxis.h"
24 24 #include <qmath.h>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 LogXLogYDomain::LogXLogYDomain(QObject *parent)
29 29 : AbstractDomain(parent),
30 30 m_logLeftX(0),
31 31 m_logRightX(1),
32 32 m_logBaseX(10),
33 33 m_logLeftY(0),
34 34 m_logRightY(1),
35 35 m_logBaseY(10)
36 36 {
37 37 }
38 38
39 39 LogXLogYDomain::~LogXLogYDomain()
40 40 {
41 41 }
42 42
43 43 void LogXLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
44 44 {
45 45 bool axisXChanged = false;
46 46 bool axisYChanged = false;
47 47
48 48 adjustLogDomainRanges(minX, maxX);
49 49 adjustLogDomainRanges(minY, maxY);
50 50
51 51 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
52 52 m_minX = minX;
53 53 m_maxX = maxX;
54 54 axisXChanged = true;
55 55 qreal logMinX = log10(m_minX) / log10(m_logBaseX);
56 56 qreal logMaxX = log10(m_maxX) / log10(m_logBaseX);
57 57 m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX;
58 58 m_logRightX = logMinX > logMaxX ? logMinX : logMaxX;
59 59 if(!m_signalsBlocked)
60 60 emit rangeHorizontalChanged(m_minX, m_maxX);
61 61 }
62 62
63 63 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
64 64 m_minY = minY;
65 65 m_maxY = maxY;
66 66 axisYChanged = true;
67 67 qreal logMinY = log10(m_minY) / log10(m_logBaseY);
68 68 qreal logMaxY = log10(m_maxY) / log10(m_logBaseY);
69 69 m_logLeftY = logMinY < logMaxY ? logMinY : logMaxY;
70 70 m_logRightY = logMinY > logMaxY ? logMinY : logMaxY;
71 71 if (!m_signalsBlocked)
72 72 emit rangeVerticalChanged(m_minY, m_maxY);
73 73 }
74 74
75 75 if (axisXChanged || axisYChanged)
76 76 emit updated();
77 77 }
78 78
79 79 void LogXLogYDomain::zoomIn(const QRectF &rect)
80 80 {
81 81 storeZoomReset();
82 82 qreal logLeftX = rect.left() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX;
83 83 qreal logRightX = rect.right() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX;
84 84 qreal leftX = qPow(m_logBaseX, logLeftX);
85 85 qreal rightX = qPow(m_logBaseX, logRightX);
86 86 qreal minX = leftX < rightX ? leftX : rightX;
87 87 qreal maxX = leftX > rightX ? leftX : rightX;
88 88
89 89 qreal logLeftY = m_logRightY - rect.bottom() * (m_logRightY - m_logLeftY) / m_size.height();
90 90 qreal logRightY = m_logRightY - rect.top() * (m_logRightY - m_logLeftY) / m_size.height();
91 91 qreal leftY = qPow(m_logBaseY, logLeftY);
92 92 qreal rightY = qPow(m_logBaseY, logRightY);
93 93 qreal minY = leftY < rightY ? leftY : rightY;
94 94 qreal maxY = leftY > rightY ? leftY : rightY;
95 95
96 96 setRange(minX, maxX, minY, maxY);
97 97 }
98 98
99 99 void LogXLogYDomain::zoomOut(const QRectF &rect)
100 100 {
101 101 storeZoomReset();
102 102 const qreal factorX = m_size.width() / rect.width();
103 103 const qreal factorY = m_size.height() / rect.height();
104 104
105 105 qreal logLeftX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 - factorX);
106 106 qreal logRIghtX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 + factorX);
107 107 qreal leftX = qPow(m_logBaseX, logLeftX);
108 108 qreal rightX = qPow(m_logBaseX, logRIghtX);
109 109 qreal minX = leftX < rightX ? leftX : rightX;
110 110 qreal maxX = leftX > rightX ? leftX : rightX;
111 111
112 112 qreal newLogMinY = m_logLeftY + (m_logRightY - m_logLeftY) / 2 * (1 - factorY);
113 113 qreal newLogMaxY = m_logLeftY + (m_logRightY - m_logLeftY) / 2 * (1 + factorY);
114 114 qreal leftY = qPow(m_logBaseY, newLogMinY);
115 115 qreal rightY = qPow(m_logBaseY, newLogMaxY);
116 116 qreal minY = leftY < rightY ? leftY : rightY;
117 117 qreal maxY = leftY > rightY ? leftY : rightY;
118 118
119 119 setRange(minX, maxX, minY, maxY);
120 120 }
121 121
122 122 void LogXLogYDomain::move(qreal dx, qreal dy)
123 123 {
124 124 qreal stepX = dx * qAbs(m_logRightX - m_logLeftX) / m_size.width();
125 125 qreal leftX = qPow(m_logBaseX, m_logLeftX + stepX);
126 126 qreal rightX = qPow(m_logBaseX, m_logRightX + stepX);
127 127 qreal minX = leftX < rightX ? leftX : rightX;
128 128 qreal maxX = leftX > rightX ? leftX : rightX;
129 129
130 130 qreal stepY = dy * (m_logRightY - m_logLeftY) / m_size.height();
131 131 qreal leftY = qPow(m_logBaseY, m_logLeftY + stepY);
132 132 qreal rightY = qPow(m_logBaseY, m_logRightY + stepY);
133 133 qreal minY = leftY < rightY ? leftY : rightY;
134 134 qreal maxY = leftY > rightY ? leftY : rightY;
135 135
136 136 setRange(minX, maxX, minY, maxY);
137 137 }
138 138
139 139 QPointF LogXLogYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const
140 140 {
141 const qreal deltaX = m_size.width() / qAbs(m_logRightX - m_logLeftX);
142 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
143 qreal x(0);
144 qreal y(0);
141 145 if (point.x() > 0 && point.y() > 0) {
142 const qreal deltaX = m_size.width() / qAbs(m_logRightX - m_logLeftX);
143 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
144 qreal x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
145 qreal y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY + m_size.height();
146 x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
147 y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY + m_size.height();
146 148 ok = true;
147 return QPointF(x, y);
148 149 } else {
149 qWarning() << "Logarithm of negative value is undefined. Empty layout returned.";
150 qWarning() << "Logarithms of zero and negative values are undefined.";
150 151 ok = false;
151 return QPointF();
152 if (point.x() > 0)
153 x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
154 else
155 x = 0;
156 if (point.y() > 0) {
157 y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY
158 + m_size.height();
159 } else {
160 y = m_size.height();
161 }
152 162 }
163 return QPointF(x, y);
153 164 }
154 165
155 166 QVector<QPointF> LogXLogYDomain::calculateGeometryPoints(const QList<QPointF> &vector) const
156 167 {
157 168 const qreal deltaX = m_size.width() / qAbs(m_logRightX - m_logLeftX);
158 169 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
159 170
160 171 QVector<QPointF> result;
161 172 result.resize(vector.count());
162 173
163 174 for (int i = 0; i < vector.count(); ++i) {
164 175 if (vector[i].x() > 0 && vector[i].y() > 0) {
165 176 qreal x = (log10(vector[i].x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
166 177 qreal y = (log10(vector[i].y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY + m_size.height();
167 178 result[i].setX(x);
168 179 result[i].setY(y);
169 180 } else {
170 qWarning() << "Logarithm of negative value is undefined. Empty layout returned.";
181 qWarning() << "Logarithms of zero and negative values are undefined.";
171 182 return QVector<QPointF>();
172 183 }
173 184 }
174 185 return result;
175 186 }
176 187
177 188 QPointF LogXLogYDomain::calculateDomainPoint(const QPointF &point) const
178 189 {
179 190 const qreal deltaX = m_size.width() / qAbs(m_logRightX - m_logLeftX);
180 191 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
181 192 qreal x = qPow(m_logBaseX, m_logLeftX + point.x() / deltaX);
182 193 qreal y = qPow(m_logBaseY, m_logLeftY + (m_size.height() - point.y()) / deltaY);
183 194 return QPointF(x, y);
184 195 }
185 196
186 197 bool LogXLogYDomain::attachAxis(QAbstractAxis *axis)
187 198 {
188 199 AbstractDomain::attachAxis(axis);
189 200 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
190 201
191 202 if (logAxis && logAxis->orientation() == Qt::Vertical) {
192 203 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
193 204 handleVerticalAxisBaseChanged(logAxis->base());
194 205 }
195 206
196 207 if (logAxis && logAxis->orientation() == Qt::Horizontal) {
197 208 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
198 209 handleHorizontalAxisBaseChanged(logAxis->base());
199 210 }
200 211
201 212 return true;
202 213 }
203 214
204 215 bool LogXLogYDomain::detachAxis(QAbstractAxis *axis)
205 216 {
206 217 AbstractDomain::detachAxis(axis);
207 218 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
208 219
209 220 if (logAxis && logAxis->orientation() == Qt::Vertical)
210 221 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
211 222
212 223 if (logAxis && logAxis->orientation() == Qt::Horizontal)
213 224 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
214 225
215 226 return true;
216 227 }
217 228
218 229 void LogXLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
219 230 {
220 231 m_logBaseY = baseY;
221 232 qreal logMinY = log10(m_minY) / log10(m_logBaseY);
222 233 qreal logMaxY = log10(m_maxY) / log10(m_logBaseY);
223 234 m_logLeftY = logMinY < logMaxY ? logMinY : logMaxY;
224 235 m_logRightY = logMinY > logMaxY ? logMinY : logMaxY;
225 236 emit updated();
226 237 }
227 238
228 239 void LogXLogYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
229 240 {
230 241 m_logBaseX = baseX;
231 242 qreal logMinX = log10(m_minX) / log10(m_logBaseX);
232 243 qreal logMaxX = log10(m_maxX) / log10(m_logBaseX);
233 244 m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX;
234 245 m_logRightX = logMinX > logMaxX ? logMinX : logMaxX;
235 246 emit updated();
236 247 }
237 248
238 249 // operators
239 250
240 251 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
241 252 {
242 253 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX)
243 254 && qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY)
244 255 && qFuzzyIsNull(domain1.m_minX - domain2.m_minX)
245 256 && qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
246 257 }
247 258
248 259
249 260 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const LogXLogYDomain &domain1, const LogXLogYDomain &domain2)
250 261 {
251 262 return !(domain1 == domain2);
252 263 }
253 264
254 265
255 266 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXLogYDomain &domain)
256 267 {
257 268 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
258 269 return dbg.maybeSpace();
259 270 }
260 271
261 272 #include "moc_logxlogydomain_p.cpp"
262 273
263 274 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,240 +1,241
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "logxydomain_p.h"
22 22 #include "qabstractaxis_p.h"
23 23 #include "qlogvalueaxis.h"
24 24 #include <qmath.h>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 LogXYDomain::LogXYDomain(QObject *parent)
29 29 : AbstractDomain(parent),
30 30 m_logLeftX(0),
31 31 m_logRightX(1),
32 32 m_logBaseX(10)
33 33 {
34 34 }
35 35
36 36 LogXYDomain::~LogXYDomain()
37 37 {
38 38 }
39 39
40 40 void LogXYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
41 41 {
42 42 bool axisXChanged = false;
43 43 bool axisYChanged = false;
44 44
45 45 adjustLogDomainRanges(minX, maxX);
46 46
47 47 if (!qFuzzyCompare(m_minX, minX) || !qFuzzyCompare(m_maxX, maxX)) {
48 48 m_minX = minX;
49 49 m_maxX = maxX;
50 50 axisXChanged = true;
51 51 qreal logMinX = log10(m_minX) / log10(m_logBaseX);
52 52 qreal logMaxX = log10(m_maxX) / log10(m_logBaseX);
53 53 m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX;
54 54 m_logRightX = logMinX > logMaxX ? logMinX : logMaxX;
55 55 if(!m_signalsBlocked)
56 56 emit rangeHorizontalChanged(m_minX, m_maxX);
57 57 }
58 58
59 59 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
60 60 m_minY = minY;
61 61 m_maxY = maxY;
62 62 axisYChanged = true;
63 63 if (!m_signalsBlocked)
64 64 emit rangeVerticalChanged(m_minY, m_maxY);
65 65 }
66 66
67 67 if (axisXChanged || axisYChanged)
68 68 emit updated();
69 69 }
70 70
71 71 void LogXYDomain::zoomIn(const QRectF &rect)
72 72 {
73 73 storeZoomReset();
74 74 qreal logLeftX = rect.left() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX;
75 75 qreal logRightX = rect.right() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX;
76 76 qreal leftX = qPow(m_logBaseX, logLeftX);
77 77 qreal rightX = qPow(m_logBaseX, logRightX);
78 78 qreal minX = leftX < rightX ? leftX : rightX;
79 79 qreal maxX = leftX > rightX ? leftX : rightX;
80 80
81 81 qreal dy = spanY() / m_size.height();
82 82 qreal minY = m_minY;
83 83 qreal maxY = m_maxY;
84 84
85 85 minY = maxY - dy * rect.bottom();
86 86 maxY = maxY - dy * rect.top();
87 87
88 88 setRange(minX, maxX, minY, maxY);
89 89 }
90 90
91 91 void LogXYDomain::zoomOut(const QRectF &rect)
92 92 {
93 93 storeZoomReset();
94 94 const qreal factorX = m_size.width() / rect.width();
95 95
96 96 qreal logLeftX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 - factorX);
97 97 qreal logRIghtX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 + factorX);
98 98 qreal leftX = qPow(m_logBaseX, logLeftX);
99 99 qreal rightX = qPow(m_logBaseX, logRIghtX);
100 100 qreal minX = leftX < rightX ? leftX : rightX;
101 101 qreal maxX = leftX > rightX ? leftX : rightX;
102 102
103 103 qreal dy = spanY() / rect.height();
104 104 qreal minY = m_minY;
105 105 qreal maxY = m_maxY;
106 106
107 107 maxY = minY + dy * rect.bottom();
108 108 minY = maxY - dy * m_size.height();
109 109
110 110 setRange(minX, maxX, minY, maxY);
111 111 }
112 112
113 113 void LogXYDomain::move(qreal dx, qreal dy)
114 114 {
115 115 qreal stepX = dx * (m_logRightX - m_logLeftX) / m_size.width();
116 116 qreal leftX = qPow(m_logBaseX, m_logLeftX + stepX);
117 117 qreal rightX = qPow(m_logBaseX, m_logRightX + stepX);
118 118 qreal minX = leftX < rightX ? leftX : rightX;
119 119 qreal maxX = leftX > rightX ? leftX : rightX;
120 120
121 121 qreal y = spanY() / m_size.height();
122 122 qreal minY = m_minY;
123 123 qreal maxY = m_maxY;
124 124
125 125 if (dy != 0) {
126 126 minY = minY + y * dy;
127 127 maxY = maxY + y * dy;
128 128 }
129 129 setRange(minX, maxX, minY, maxY);
130 130 }
131 131
132 132 QPointF LogXYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const
133 133 {
134 const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX);
135 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
134 136
137 qreal x(0);
138 qreal y = (point.y() - m_minY) * -deltaY + m_size.height();
135 139 if (point.x() > 0) {
136 const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX);
137 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
138
139 qreal x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
140 qreal y = (point.y() - m_minY) * -deltaY + m_size.height();
140 x = (log10(point.x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
141 141 ok = true;
142 return QPointF(x, y);
143 142 } else {
144 qWarning() << "Logarithm of negative value is undefined. Empty layout returned.";
143 x = 0;
144 qWarning() << "Logarithms of zero and negative values are undefined.";
145 145 ok = false;
146 return QPointF();
147 146 }
147 return QPointF(x, y);
148 148 }
149 149
150 150 QVector<QPointF> LogXYDomain::calculateGeometryPoints(const QList<QPointF> &vector) const
151 151 {
152 152 const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX);
153 153 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
154 154
155 155 QVector<QPointF> result;
156 156 result.resize(vector.count());
157 157
158 158 for (int i = 0; i < vector.count(); ++i) {
159 159 if (vector[i].x() > 0) {
160 160 qreal x = (log10(vector[i].x()) / log10(m_logBaseX)) * deltaX - m_logLeftX * deltaX;
161 161 qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height();
162 162 result[i].setX(x);
163 163 result[i].setY(y);
164 164 } else {
165 qWarning() << "Logarithm of negative value is undefined. Empty layout returned.";
165 qWarning() << "Logarithms of zero and negative values are undefined.";
166 166 return QVector<QPointF>();
167 167 }
168
168 169 }
169 170 return result;
170 171 }
171 172
172 173 QPointF LogXYDomain::calculateDomainPoint(const QPointF &point) const
173 174 {
174 175 const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX);
175 176 const qreal deltaY = m_size.height() / (m_maxY - m_minY);
176 177 qreal x = qPow(m_logBaseX, m_logLeftX + point.x() / deltaX);
177 178 qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY;
178 179 return QPointF(x, y);
179 180 }
180 181
181 182 bool LogXYDomain::attachAxis(QAbstractAxis *axis)
182 183 {
183 184 AbstractDomain::attachAxis(axis);
184 185 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
185 186
186 187 if (logAxis && logAxis->orientation() == Qt::Horizontal) {
187 188 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
188 189 handleHorizontalAxisBaseChanged(logAxis->base());
189 190 }
190 191
191 192 return true;
192 193 }
193 194
194 195 bool LogXYDomain::detachAxis(QAbstractAxis *axis)
195 196 {
196 197 AbstractDomain::detachAxis(axis);
197 198 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
198 199
199 200 if (logAxis && logAxis->orientation() == Qt::Horizontal)
200 201 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleHorizontalAxisBaseChanged(qreal)));
201 202
202 203 return true;
203 204 }
204 205
205 206 void LogXYDomain::handleHorizontalAxisBaseChanged(qreal baseX)
206 207 {
207 208 m_logBaseX = baseX;
208 209 qreal logMinX = log10(m_minX) / log10(m_logBaseX);
209 210 qreal logMaxX = log10(m_maxX) / log10(m_logBaseX);
210 211 m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX;
211 212 m_logRightX = logMinX > logMaxX ? logMinX : logMaxX;
212 213 emit updated();
213 214 }
214 215
215 216 // operators
216 217
217 218 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const LogXYDomain &domain1, const LogXYDomain &domain2)
218 219 {
219 220 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX)
220 221 && qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY)
221 222 && qFuzzyIsNull(domain1.m_minX - domain2.m_minX)
222 223 && qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
223 224 }
224 225
225 226
226 227 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const LogXYDomain &domain1, const LogXYDomain &domain2)
227 228 {
228 229 return !(domain1 == domain2);
229 230 }
230 231
231 232
232 233 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXYDomain &domain)
233 234 {
234 235 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
235 236 return dbg.maybeSpace();
236 237 }
237 238
238 239 #include "moc_logxydomain_p.cpp"
239 240
240 241 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,236 +1,237
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise 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 "xlogydomain_p.h"
22 22 #include "qabstractaxis_p.h"
23 23 #include "qlogvalueaxis.h"
24 24 #include <qmath.h>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 XLogYDomain::XLogYDomain(QObject *parent)
29 29 : AbstractDomain(parent),
30 30 m_logLeftY(0),
31 31 m_logRightY(1),
32 32 m_logBaseY(10)
33 33 {
34 34 }
35 35
36 36 XLogYDomain::~XLogYDomain()
37 37 {
38 38 }
39 39
40 40 void XLogYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
41 41 {
42 42 bool axisXChanged = false;
43 43 bool axisYChanged = false;
44 44
45 45 adjustLogDomainRanges(minY, maxY);
46 46
47 47 if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) {
48 48 m_minX = minX;
49 49 m_maxX = maxX;
50 50 axisXChanged = true;
51 51 if(!m_signalsBlocked)
52 52 emit rangeHorizontalChanged(m_minX, m_maxX);
53 53 }
54 54
55 55 if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) {
56 56 m_minY = minY;
57 57 m_maxY = maxY;
58 58 axisYChanged = true;
59 59 qreal logMinY = log10(m_minY) / log10(m_logBaseY);
60 60 qreal logMaxY = log10(m_maxY) / log10(m_logBaseY);
61 61 m_logLeftY = logMinY < logMaxY ? logMinY : logMaxY;
62 62 m_logRightY = logMinY > logMaxY ? logMinY : logMaxY;
63 63 if (!m_signalsBlocked)
64 64 emit rangeVerticalChanged(m_minY, m_maxY);
65 65 }
66 66
67 67 if (axisXChanged || axisYChanged)
68 68 emit updated();
69 69 }
70 70
71 71 void XLogYDomain::zoomIn(const QRectF &rect)
72 72 {
73 73 storeZoomReset();
74 74 qreal dx = spanX() / m_size.width();
75 75 qreal maxX = m_maxX;
76 76 qreal minX = m_minX;
77 77
78 78 maxX = minX + dx * rect.right();
79 79 minX = minX + dx * rect.left();
80 80
81 81 qreal logLeftY = m_logRightY - rect.bottom() * (m_logRightY - m_logLeftY) / m_size.height();
82 82 qreal logRightY = m_logRightY - rect.top() * (m_logRightY - m_logLeftY) / m_size.height();
83 83 qreal leftY = qPow(m_logBaseY, logLeftY);
84 84 qreal rightY = qPow(m_logBaseY, logRightY);
85 85 qreal minY = leftY < rightY ? leftY : rightY;
86 86 qreal maxY = leftY > rightY ? leftY : rightY;
87 87
88 88 setRange(minX, maxX, minY, maxY);
89 89 }
90 90
91 91 void XLogYDomain::zoomOut(const QRectF &rect)
92 92 {
93 93 storeZoomReset();
94 94 qreal dx = spanX() / rect.width();
95 95 qreal maxX = m_maxX;
96 96 qreal minX = m_minX;
97 97
98 98 minX = maxX - dx * rect.right();
99 99 maxX = minX + dx * m_size.width();
100 100
101 101 const qreal factorY = m_size.height() / rect.height();
102 102 qreal newLogMinY = m_logLeftY + (m_logRightY - m_logLeftY) / 2 * (1 - factorY);
103 103 qreal newLogMaxY = m_logLeftY + (m_logRightY - m_logLeftY) / 2 * (1 + factorY);
104 104 qreal leftY = qPow(m_logBaseY, newLogMinY);
105 105 qreal rightY = qPow(m_logBaseY, newLogMaxY);
106 106 qreal minY = leftY < rightY ? leftY : rightY;
107 107 qreal maxY = leftY > rightY ? leftY : rightY;
108 108
109 109 setRange(minX, maxX, minY, maxY);
110 110 }
111 111
112 112 void XLogYDomain::move(qreal dx, qreal dy)
113 113 {
114 114 qreal x = spanX() / m_size.width();
115 115 qreal maxX = m_maxX;
116 116 qreal minX = m_minX;
117 117
118 118 if (dx != 0) {
119 119 minX = minX + x * dx;
120 120 maxX = maxX + x * dx;
121 121 }
122 122
123 123 qreal stepY = dy * (m_logRightY - m_logLeftY) / m_size.height();
124 124 qreal leftY = qPow(m_logBaseY, m_logLeftY + stepY);
125 125 qreal rightY = qPow(m_logBaseY, m_logRightY + stepY);
126 126 qreal minY = leftY < rightY ? leftY : rightY;
127 127 qreal maxY = leftY > rightY ? leftY : rightY;
128 128
129 129 setRange(minX, maxX, minY, maxY);
130 130 }
131 131
132 132 QPointF XLogYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const
133 133 {
134 if (point.y() > 0) {
135 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
136 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
134 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
135 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
137 136
138 qreal x = (point.x() - m_minX) * deltaX;
139 qreal y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY + m_size.height();
137 qreal x = (point.x() - m_minX) * deltaX;
138 qreal y(0);
139 if (point.y() > 0) {
140 y = (log10(point.y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY + m_size.height();
140 141 ok = true;
141 return QPointF(x, y);
142 142 } else {
143 qWarning() << "Logarithm of negative value is undefined. Empty layout returned.";
143 y = m_size.height();
144 qWarning() << "Logarithms of zero and negative values are undefined.";
144 145 ok = false;
145 return QPointF();
146 146 }
147 return QPointF(x, y);
147 148 }
148 149
149 150 QVector<QPointF> XLogYDomain::calculateGeometryPoints(const QList<QPointF> &vector) const
150 151 {
151 152 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
152 153 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
153 154
154 155 QVector<QPointF> result;
155 156 result.resize(vector.count());
156 157
157 158 for (int i = 0; i < vector.count(); ++i) {
158 159 if (vector[i].y() > 0) {
159 160 qreal x = (vector[i].x() - m_minX) * deltaX;
160 161 qreal y = (log10(vector[i].y()) / log10(m_logBaseY)) * -deltaY - m_logLeftY * -deltaY + m_size.height();
161 162 result[i].setX(x);
162 163 result[i].setY(y);
163 164 } else {
164 qWarning() << "Logarithm of negative value is undefined. Empty layout returned.";
165 qWarning() << "Logarithms of zero and negative values are undefined.";
165 166 return QVector<QPointF>();
166 167 }
167 168 }
168 169 return result;
169 170 }
170 171
171 172 QPointF XLogYDomain::calculateDomainPoint(const QPointF &point) const
172 173 {
173 174 const qreal deltaX = m_size.width() / (m_maxX - m_minX);
174 175 const qreal deltaY = m_size.height() / qAbs(m_logRightY - m_logLeftY);
175 176 qreal x = point.x() / deltaX + m_minX;
176 177 qreal y = qPow(m_logBaseY, m_logLeftY + (m_size.height() - point.y()) / deltaY);
177 178 return QPointF(x, y);
178 179 }
179 180
180 181 bool XLogYDomain::attachAxis(QAbstractAxis *axis)
181 182 {
182 183 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
183 184
184 185 if (logAxis && logAxis->orientation() == Qt::Vertical) {
185 186 QObject::connect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
186 187 handleVerticalAxisBaseChanged(logAxis->base());
187 188 }
188 189 return AbstractDomain::attachAxis(axis);
189 190 }
190 191
191 192 bool XLogYDomain::detachAxis(QAbstractAxis *axis)
192 193 {
193 194 QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(axis);
194 195
195 196 if (logAxis && logAxis->orientation() == Qt::Vertical)
196 197 QObject::disconnect(logAxis, SIGNAL(baseChanged(qreal)), this, SLOT(handleVerticalAxisBaseChanged(qreal)));
197 198
198 199 return AbstractDomain::detachAxis(axis);
199 200 }
200 201
201 202 void XLogYDomain::handleVerticalAxisBaseChanged(qreal baseY)
202 203 {
203 204 m_logBaseY = baseY;
204 205 qreal logMinY = log10(m_minY) / log10(m_logBaseY);
205 206 qreal logMaxY = log10(m_maxY) / log10(m_logBaseY);
206 207 m_logLeftY = logMinY < logMaxY ? logMinY : logMaxY;
207 208 m_logRightY = logMinY > logMaxY ? logMinY : logMaxY;
208 209 emit updated();
209 210 }
210 211
211 212 // operators
212 213
213 214 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const XLogYDomain &domain1, const XLogYDomain &domain2)
214 215 {
215 216 return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX)
216 217 && qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY)
217 218 && qFuzzyIsNull(domain1.m_minX - domain2.m_minX)
218 219 && qFuzzyIsNull(domain1.m_minY - domain2.m_minY));
219 220 }
220 221
221 222
222 223 bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const XLogYDomain &domain1, const XLogYDomain &domain2)
223 224 {
224 225 return !(domain1 == domain2);
225 226 }
226 227
227 228
228 229 QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const XLogYDomain &domain)
229 230 {
230 231 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
231 232 return dbg.maybeSpace();
232 233 }
233 234
234 235 #include "moc_xlogydomain_p.cpp"
235 236
236 237 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now