##// END OF EJS Templates
New bar calculate layout
Marek Rosa -
r2303:ebdfe8e2c2c9
parent child
Show More
@@ -37,56 +37,92 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
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 qreal barWidth = m_series->d_func()->barWidth();
42 // AbstractDomain:
43 qreal width = geometry().width();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
49
50 // On horizontal chart barWidth of the barseries means height of the rect.
51 qreal rectHeight = (scaleY / setCount) * m_series->d_func()->barWidth();
52
42
53 int itemIndex(0);
43 int itemIndex(0);
54 for (int category = 0; category < categoryCount; category++) {
44 for(int category = 0; category < categoryCount; category++) {
55 qreal xPos = -scaleX * m_domainMinX + geometry().left();
56 for (int set = 0; set < setCount; set++) {
45 for (int set = 0; set < setCount; set++) {
57 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
46 qreal value = m_series->barSets().at(set)->at(category);
58
47 QRectF rect;
59 qreal yPos = geometry().bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
48 QPointF topLeft;
60 yPos += setCount * rectHeight / 2;
49 if (domain()->type() == AbstractDomain::LogXYDomain
61 yPos -= set * rectHeight;
50 || domain()->type() == AbstractDomain::LogXLogYDomain)
62
51 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
63 qreal rectWidth = barSet->value(category) * scaleX;
52 else
64 Bar *bar = m_bars.at(itemIndex);
53 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
65
54
66 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
55 QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(value, category - barWidth / 2 + (set)/(setCount) * barWidth));
56 rect.setTopLeft(topLeft);
57 rect.setBottomRight(bottomRight);
67 layout.append(rect);
58 layout.append(rect);
68 bar->setPen(barSet->m_pen);
69 bar->setBrush(barSet->m_brush);
70 if (qFuzzyCompare(rectHeight, 0))
71 bar->setVisible(false);
72 else
73 bar->setVisible(barsVisible);
74
59
75 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
60 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
76
61 label->setZValue(200);
77 if (!qFuzzyCompare(barSet->value(category), 0))
62 itemIndex++;
78 label->setText(QString::number(barSet->value(category)));
63 if (!qFuzzyCompare(value, 0))
64 label->setText(QString::number(value));
79 else
65 else
80 label->setText(QString(""));
66 label->setText(QString(""));
81
67
82 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
68 // label->setFont(m_series->barSets().at(set)->labelFont());
83 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
69 // label->setBrush(m_series->barSets().at(set)->labelBrush());
84 label->setFont(barSet->m_labelFont);
70 label->setBrush(Qt::black);
85 label->setBrush(barSet->m_labelBrush);
71
72 label->setPos(rect.center() - label->boundingRect().center());
86
73
87 itemIndex++;
88 }
74 }
89 }
75 }
76
77
78 // // AbstractDomain:
79 // qreal width = geometry().width();
80 // qreal height = geometry().height();
81 // qreal rangeY = m_domainMaxY - m_domainMinY;
82 // qreal rangeX = m_domainMaxX - m_domainMinX;
83 // qreal scaleY = (height / rangeY);
84 // qreal scaleX = (width / rangeX);
85
86 // // On horizontal chart barWidth of the barseries means height of the rect.
87 // qreal rectHeight = (scaleY / setCount) * m_series->d_func()->barWidth();
88
89 // int itemIndex(0);
90 // for (int category = 0; category < categoryCount; category++) {
91 // qreal xPos = -scaleX * m_domainMinX + geometry().left();
92 // for (int set = 0; set < setCount; set++) {
93 // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
94
95 // qreal yPos = geometry().bottom() + (m_domainMinY - barSet->pos(category)) * scaleY;
96 // yPos += setCount * rectHeight / 2;
97 // yPos -= set * rectHeight;
98
99 // qreal rectWidth = barSet->value(category) * scaleX;
100 // Bar *bar = m_bars.at(itemIndex);
101
102 // QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
103 // layout.append(rect);
104 // bar->setPen(barSet->m_pen);
105 // bar->setBrush(barSet->m_brush);
106 // if (qFuzzyCompare(rectHeight, 0))
107 // bar->setVisible(false);
108 // else
109 // bar->setVisible(barsVisible);
110
111 // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
112
113 // if (!qFuzzyCompare(barSet->value(category), 0))
114 // label->setText(QString::number(barSet->value(category)));
115 // else
116 // label->setText(QString(""));
117
118 // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
119 // yPos - rectHeight / 2 - label->boundingRect().height() / 2);
120 // label->setFont(barSet->m_labelFont);
121 // label->setBrush(barSet->m_labelBrush);
122
123 // itemIndex++;
124 // }
125 // }
90 return layout;
126 return layout;
91 }
127 }
92
128
@@ -37,60 +37,82 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
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
42 // AbstractDomain:
43 qreal width = geometry().width();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
49 qreal rectHeight = scaleY * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
50
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
53 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal percentage = (100 / colSum);
55 qreal xPos = -scaleX * m_domainMinX + geometry().left();
56 for (int set = 0; set < setCount; set++) {
57 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
58
59 qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2;
60
41
61 qreal rectWidth = barSet->value(category) * percentage * scaleX;
42 qreal barWidth = m_series->d_func()->barWidth();
62 Bar *bar = m_bars.at(itemIndex);
63
43
64 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
44 for(int category = 0; category < categoryCount; category++) {
65 layout.append(rect);
45 qreal sum = 0;
66 bar->setPen(barSet->m_pen);
46 qreal categorySum = m_series->d_func()->categorySum(category);
67 bar->setBrush(barSet->m_brush);
47 for (int set = 0; set < setCount; set++) {
68 if (qFuzzyCompare(rectHeight, 0))
48 qreal value = m_series->barSets().at(set)->at(category);
69 bar->setVisible(false);
49 QRectF rect;
50 QPointF topLeft;
51 if (domain()->type() == AbstractDomain::LogXYDomain
52 || domain()->type() == AbstractDomain::LogXLogYDomain)
53 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category + barWidth/2));
70 else
54 else
71 bar->setVisible(barsVisible);
55 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category + barWidth/2));
72
56 QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(100 * (value + sum)/categorySum, category - barWidth/2));
73 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
57 rect.setTopLeft(topLeft);
74
58 rect.setBottomRight(bottomRight);
75 if (!qFuzzyCompare(m_series->d_func()->valueAt(set, category), 0)) {
59 layout.append(rect);
76 int p = m_series->d_func()->percentageAt(set, category) * 100;
60 sum +=value;
77 QString vString(QString::number(p));
78 vString.truncate(3);
79 vString.append("%");
80 label->setText(vString);
81 } else {
82 label->setText(QString(""));
83 }
84
85 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
86 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
87 label->setFont(barSet->m_labelFont);
88 label->setBrush(barSet->m_labelBrush);
89
90 itemIndex++;
91 xPos += rectWidth;
92 }
61 }
93 }
62 }
63
64 // // AbstractDomain:
65 // qreal width = geometry().width();
66 // qreal height = geometry().height();
67 // qreal rangeY = m_domainMaxY - m_domainMinY;
68 // qreal rangeX = m_domainMaxX - m_domainMinX;
69 // qreal scaleY = (height / rangeY);
70 // qreal scaleX = (width / rangeX);
71 // qreal rectHeight = scaleY * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
72
73 // int itemIndex(0);
74 // for (int category = 0; category < categoryCount; category++) {
75 // qreal colSum = m_series->d_func()->categorySum(category);
76 // qreal percentage = (100 / colSum);
77 // qreal xPos = -scaleX * m_domainMinX + geometry().left();
78 // for (int set = 0; set < setCount; set++) {
79 // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
80
81 // qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2;
82
83 // qreal rectWidth = barSet->value(category) * percentage * scaleX;
84 // Bar *bar = m_bars.at(itemIndex);
85
86 // QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
87 // layout.append(rect);
88 // bar->setPen(barSet->m_pen);
89 // bar->setBrush(barSet->m_brush);
90 // if (qFuzzyCompare(rectHeight, 0))
91 // bar->setVisible(false);
92 // else
93 // bar->setVisible(barsVisible);
94
95 // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
96
97 // if (!qFuzzyCompare(m_series->d_func()->valueAt(set, category), 0)) {
98 // int p = m_series->d_func()->percentageAt(set, category) * 100;
99 // QString vString(QString::number(p));
100 // vString.truncate(3);
101 // vString.append("%");
102 // label->setText(vString);
103 // } else {
104 // label->setText(QString(""));
105 // }
106
107 // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
108 // yPos - rectHeight / 2 - label->boundingRect().height() / 2);
109 // label->setFont(barSet->m_labelFont);
110 // label->setBrush(barSet->m_labelBrush);
111
112 // itemIndex++;
113 // xPos += rectWidth;
114 // }
115 // }
94 return layout;
116 return layout;
95 }
117 }
96
118
@@ -37,61 +37,94 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
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 // AbstractDomain:
42 qreal barWidth = m_series->d_func()->barWidth();
43 qreal width = geometry().width();
43
44 qreal height = geometry().height();
44 for(int category = 0; category < categoryCount; category++) {
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal positiveSum = 0;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal negativeSum = 0;
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
49 qreal rectHeight = scaleY * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
50
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
53 qreal xMax = -scaleX * m_domainMinX + geometry().left();
54 qreal xMin = -scaleX * m_domainMinX + geometry().left();
55 for (int set = 0; set < setCount; set++) {
47 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
48 qreal value = m_series->barSets().at(set)->at(category);
57
49 QRectF rect;
58 qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2;
50 QPointF topLeft;
59
51 QPointF bottomRight;
60 qreal rectWidth = barSet->value(category) * scaleX;
52 if (value < 0) {
61 Bar *bar = m_bars.at(itemIndex);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2));
62
54 if (domain()->type() == AbstractDomain::LogXYDomain
63 bar->setPen(barSet->m_pen);
55 || domain()->type() == AbstractDomain::LogXLogYDomain)
64 bar->setBrush(barSet->m_brush);
56 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : domain()->minX(), category + barWidth / 2));
65 if (qFuzzyCompare(rectHeight, 0))
57 else
66 bar->setVisible(false);
58 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : 0, category + barWidth / 2));
67 else
59 negativeSum += value;
68 bar->setVisible(barsVisible);
69
70 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
71
72 if (!qFuzzyCompare(barSet->value(category), 0))
73 label->setText(QString::number(barSet->value(category)));
74 else
75 label->setText(QString(""));
76 label->setFont(barSet->m_labelFont);
77 label->setBrush(barSet->m_labelBrush);
78
79 if (rectWidth > 0) {
80 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
81 layout.append(rect);
82 label->setPos(xMax + (rect.width() / 2 - label->boundingRect().width() / 2),
83 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
84 xMax += rectWidth;
85 } else {
60 } else {
86 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
61 bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2));
87 layout.append(rect);
62 if (domain()->type() == AbstractDomain::LogXYDomain
88 label->setPos(xMin + (rect.width() / 2 - label->boundingRect().width() / 2),
63 || domain()->type() == AbstractDomain::LogXLogYDomain)
89 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
64 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : domain()->minX(), category + barWidth / 2));
90 xMin += rectWidth;
65 else
66 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : 0, category + barWidth / 2));
67 positiveSum += value;
91 }
68 }
92 itemIndex++;
69 rect.setTopLeft(topLeft);
70 rect.setBottomRight(bottomRight);
71 layout.append(rect);
93 }
72 }
94 }
73 }
74
75 // // AbstractDomain:
76 // qreal width = geometry().width();
77 // qreal height = geometry().height();
78 // qreal rangeY = m_domainMaxY - m_domainMinY;
79 // qreal rangeX = m_domainMaxX - m_domainMinX;
80 // qreal scaleY = (height / rangeY);
81 // qreal scaleX = (width / rangeX);
82 // qreal rectHeight = scaleY * m_series->d_func()->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
83
84 // int itemIndex(0);
85 // for (int category = 0; category < categoryCount; category++) {
86 // qreal xMax = -scaleX * m_domainMinX + geometry().left();
87 // qreal xMin = -scaleX * m_domainMinX + geometry().left();
88 // for (int set = 0; set < setCount; set++) {
89 // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
90
91 // qreal yPos = (m_domainMinY + 0.5 - barSet->pos(category)) * scaleY + geometry().bottom() - rectHeight / 2;
92
93 // qreal rectWidth = barSet->value(category) * scaleX;
94 // Bar *bar = m_bars.at(itemIndex);
95
96 // bar->setPen(barSet->m_pen);
97 // bar->setBrush(barSet->m_brush);
98 // if (qFuzzyCompare(rectHeight, 0))
99 // bar->setVisible(false);
100 // else
101 // bar->setVisible(barsVisible);
102
103 // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
104
105 // if (!qFuzzyCompare(barSet->value(category), 0))
106 // label->setText(QString::number(barSet->value(category)));
107 // else
108 // label->setText(QString(""));
109 // label->setFont(barSet->m_labelFont);
110 // label->setBrush(barSet->m_labelBrush);
111
112 // if (rectWidth > 0) {
113 // QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
114 // layout.append(rect);
115 // label->setPos(xMax + (rect.width() / 2 - label->boundingRect().width() / 2),
116 // yPos - rectHeight / 2 - label->boundingRect().height() / 2);
117 // xMax += rectWidth;
118 // } else {
119 // QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
120 // layout.append(rect);
121 // label->setPos(xMin + (rect.width() / 2 - label->boundingRect().width() / 2),
122 // yPos - rectHeight / 2 - label->boundingRect().height() / 2);
123 // xMin += rectWidth;
124 // }
125 // itemIndex++;
126 // }
127 // }
95 return layout;
128 return layout;
96 }
129 }
97
130
@@ -38,54 +38,89 QVector<QRectF> BarChartItem::calculateLayout()
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->count();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 // bool barsVisible = m_series->isVisible();
42
42 qreal barWidth = m_series->d_func()->barWidth();
43 // Domain:
43 // barWidth = 1.8;
44 qreal width = geometry().width();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
50 qreal rectWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
51
44
52 int itemIndex(0);
45 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
46 for(int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
55 for (int set = 0; set < setCount; set++) {
47 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
48 qreal value = m_series->barSets().at(set)->at(category);
57
49 QRectF rect;
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left();
50 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value));
59 xPos -= setCount * rectWidth / 2;
51 QPointF bottomRight;
60 xPos += set * rectWidth;
52 if (domain()->type() == AbstractDomain::XLogYDomain
61
53 || domain()->type() == AbstractDomain::LogXLogYDomain)
62 qreal rectHeight = barSet->value(category) * scaleY;
54 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()));
63 Bar *bar = m_bars.at(itemIndex);
64
65 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
66 layout.append(rect);
67 bar->setPen(barSet->m_pen);
68 bar->setBrush(barSet->m_brush);
69 if (qFuzzyIsNull(rectHeight))
70 bar->setVisible(false);
71 else
55 else
72 bar->setVisible(barsVisible);
56 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0));
57 rect.setTopLeft(topLeft);
58 rect.setBottomRight(bottomRight);
59 layout.append(rect);
73
60
74 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
61 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
75
62 label->setZValue(200);
76 if (!qFuzzyIsNull(barSet->value(category)))
63 itemIndex++;
77 label->setText(QString::number(barSet->value(category)));
64 if (!qFuzzyCompare(value, 0))
65 label->setText(QString::number(value));
78 else
66 else
79 label->setText(QString(""));
67 label->setText(QString(""));
80
68
81 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
69 // label->setFont(m_series->barSets().at(set)->labelFont());
82 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
70 // label->setBrush(m_series->barSets().at(set)->labelBrush());
83 label->setFont(barSet->m_labelFont);
71 label->setBrush(Qt::black);
84 label->setBrush(barSet->m_labelBrush);
72
73 label->setPos(rect.center() - label->boundingRect().center());
85
74
86 itemIndex++;
87 }
75 }
88 }
76 }
77
78 // AbstractDomain:
79 // qreal width = geometry().width();
80 // qreal height = geometry().height();
81 // qreal rangeY = m_domainMaxY - m_domainMinY;
82 // qreal rangeX = m_domainMaxX - m_domainMinX;
83 // qreal scaleY = (height / rangeY);
84 // qreal scaleX = (width / rangeX);
85 // qreal rectWidth = (scaleX / setCount) * m_series->d_func()->barWidth();
86
87 // int itemIndex(0);
88 // for (int category = 0; category < categoryCount; category++) {
89 // qreal yPos = height + scaleY * m_domainMinY + geometry().top();
90 // for (int set = 0; set < setCount; set++) {
91 // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
92
93 // qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left();
94 // xPos -= setCount * rectWidth / 2;
95 // xPos += set * rectWidth;barWidth / 2
96
97 // qreal rectHeight = barSet->value(category) * scaleY;
98 // Bar *bar = m_bars.at(itemIndex);
99
100 // QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
101 // layout.append(rect);
102 // bar->setPen(barSet->m_pen);
103 // bar->setBrush(barSet->m_brush);
104 // if (qFuzzyIsNull(rectHeight))
105 // bar->setVisible(false);
106 // else
107 // bar->setVisible(barsVisible);
108
109 // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
110
111 // if (!qFuzzyIsNull(barSet->value(category)))
112 // label->setText(QString::number(barSet->value(category)));
113 // else
114 // label->setText(QString(""));
115
116 // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
117 // yPos - rectHeight / 2 - label->boundingRect().height() / 2);
118 // label->setFont(barSet->m_labelFont);
119 // label->setBrush(barSet->m_labelBrush);
120
121 // itemIndex++;
122 // }
123 // }
89 return layout;
124 return layout;
90 }
125 }
91
126
@@ -38,60 +38,82 QVector<QRectF> PercentBarChartItem::calculateLayout()
38 // Use temporary qreals for accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->count();
40 qreal setCount = m_series->count();
41 bool barsVisible = m_series->isVisible();
41 // bool barsVisible = m_series->isVisible();
42
43 // Domain:
44 qreal width = geometry().width();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
50 qreal rectWidth = scaleX * m_series->d_func()->barWidth();
51
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().top();
57 for (int set = 0; set < setCount; set++) {
58 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
59
42
60 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
43 qreal barWidth = m_series->d_func()->barWidth();
61
44
62 qreal rectHeight = barSet->value(category) * percentage * scaleY;
45 for(int category = 0; category < categoryCount; category++) {
63 Bar *bar = m_bars.at(itemIndex);
46 qreal sum = 0;
64 bar->setPen(barSet->m_pen);
47 qreal categorySum = m_series->d_func()->categorySum(category);
65 bar->setBrush(barSet->m_brush);
48 for (int set = 0; set < setCount; set++) {
66 if (qFuzzyIsNull(rectHeight))
49 qreal value = m_series->barSets().at(set)->at(category);
67 bar->setVisible(false);
50 QRectF rect;
51 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum));
52 QPointF bottomRight;
53 if (domain()->type() == AbstractDomain::XLogYDomain
54 || domain()->type() == AbstractDomain::LogXLogYDomain)
55 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()));
68 else
56 else
69 bar->setVisible(barsVisible);
57 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0));
70
58 rect.setTopLeft(topLeft);
71 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
59 rect.setBottomRight(bottomRight);
72 layout.append(rect);
60 layout.append(rect);
73
61 sum +=value;
74 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
75
76 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) {
77 int p = m_series->d_func()->percentageAt(set, category) * 100;
78 QString vString(QString::number(p));
79 vString.truncate(3);
80 vString.append("%");
81 label->setText(vString);
82 } else {
83 label->setText(QString(""));
84 }
85
86 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
87 yPos - rectHeight / 2 - label->boundingRect().height() / 2);
88 label->setFont(barSet->m_labelFont);
89 label->setBrush(barSet->m_labelBrush);
90
91 itemIndex++;
92 yPos -= rectHeight;
93 }
62 }
94 }
63 }
64
65 // AbstractDomain:
66 // qreal width = geometry().width();
67 // qreal height = geometry().height();
68 // qreal rangeY = m_domainMaxY - m_domainMinY;
69 // qreal rangeX = m_domainMaxX - m_domainMinX;
70 // qreal scaleY = (height / rangeY);
71 // qreal scaleX = (width / rangeX);
72 // qreal rectWidth = scaleX * m_series->d_func()->barWidth();
73
74 // int itemIndex(0);
75 // for (int category = 0; category < categoryCount; category++) {
76 // qreal colSum = m_series->d_func()->categorySum(category);
77 // qreal percentage = (100 / colSum);
78 // qreal yPos = height + scaleY * m_domainMinY + geometry().top();
79 // for (int set = 0; set < setCount; set++) {
80 // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
81
82 // qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
83
84 // qreal rectHeight = barSet->value(category) * percentage * scaleY;
85 // Bar *bar = m_bars.at(itemIndex);
86 // bar->setPen(barSet->m_pen);
87 // bar->setBrush(barSet->m_brush);
88 // if (qFuzzyIsNull(rectHeight))
89 // bar->setVisible(false);
90 // else
91 // bar->setVisible(barsVisible);
92
93 // QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
94 // layout.append(rect);
95
96 // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
97
98 // if (!qFuzzyIsNull(m_series->d_func()->valueAt(set, category))) {
99 // int p = m_series->d_func()->percentageAt(set, category) * 100;
100 // QString vString(QString::number(p));
101 // vString.truncate(3);
102 // vString.append("%");
103 // label->setText(vString);
104 // } else {
105 // label->setText(QString(""));
106 // }
107
108 // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
109 // yPos - rectHeight / 2 - label->boundingRect().height() / 2);
110 // label->setFont(barSet->m_labelFont);
111 // label->setBrush(barSet->m_labelBrush);
112
113 // itemIndex++;
114 // yPos -= rectHeight;
115 // }
116 // }
95 return layout;
117 return layout;
96 }
118 }
97
119
@@ -37,64 +37,97 QVector<QRectF> StackedBarChartItem::calculateLayout()
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 qreal barWidth = m_series->d_func()->barWidth();
42 // Domain:
42
43 qreal width = geometry().width();
43 for(int category = 0; category < categoryCount; category++) {
44 qreal height = geometry().height();
44 qreal positiveSum = 0;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal negativeSum = 0;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
49 qreal rectWidth = scaleX * m_series->d_func()->barWidth();
50
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
53 qreal yMax = height + scaleY * m_domainMinY + geometry().top();
54 qreal yMin = height + scaleY * m_domainMinY + geometry().top();
55 for (int set = 0; set < setCount; set++) {
46 for (int set = 0; set < setCount; set++) {
56 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
47 qreal value = m_series->barSets().at(set)->at(category);
57
48 QRectF rect;
58 qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
49 QPointF topLeft;
59
50 QPointF bottomRight;
60 qreal rectHeight = barSet->value(category) * scaleY;
51 if (value < 0) {
61 Bar *bar = m_bars.at(itemIndex);
52 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum));
62 bar->setPen(barSet->m_pen);
53 if (domain()->type() == AbstractDomain::XLogYDomain
63 bar->setBrush(barSet->m_brush);
54 || domain()->type() == AbstractDomain::LogXLogYDomain)
64 if (qFuzzyIsNull(rectHeight))
55 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()));
65 bar->setVisible(false);
56 else
66 else
57 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0));
67 bar->setVisible(barsVisible);
58 negativeSum += value;
68
69 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
70
71 if (!qFuzzyIsNull(barSet->value(category)))
72 label->setText(QString::number(barSet->value(category)));
73 else
74 label->setText(QString(""));
75
76 label->setFont(barSet->m_labelFont);
77 label->setBrush(barSet->m_labelBrush);
78
79 if (rectHeight < 0) {
80 QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight);
81 layout.append(rect);
82 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
83 yMax - rectHeight / 2 - label->boundingRect().height() / 2);
84 yMax -= rectHeight;
85 } else {
59 } else {
86 QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight);
60 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum));
87 layout.append(rect);
61 if (domain()->type() == AbstractDomain::XLogYDomain
88 label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
62 || domain()->type() == AbstractDomain::LogXLogYDomain)
89 yMin - rectHeight / 2 - label->boundingRect().height() / 2);
63 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()));
90 yMin -= rectHeight;
64 else
65 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0));
66 positiveSum += value;
91 }
67 }
92
68 rect.setTopLeft(topLeft);
93 itemIndex++;
69 rect.setBottomRight(bottomRight);
70 layout.append(rect);
94 }
71 }
95 }
72 }
96
73
97 return layout;
74
75 // AbstractDomain:
76 // qreal width = geometry().width();
77 // qreal height = geometry().height();
78 // qreal rangeY = m_domainMaxY - m_domainMinY;
79 // qreal rangeX = m_domainMaxX - m_domainMinX;
80 // qreal scaleY = (height / rangeY);
81 // qreal scaleX = (width / rangeX);
82 // qreal rectWidth = scaleX * m_series->d_func()->barWidth();
83
84 // int itemIndex(0);
85 // for (int category = 0; category < categoryCount; category++) {
86 // qreal yMax = height + scaleY * m_domainMinY + geometry().top();
87 // qreal yMin = height + scaleY * m_domainMinY + geometry().top();
88 // for (int set = 0; set < setCount; set++) {
89 // QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
90
91 // qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left() - rectWidth / 2;
92
93 // qreal rectHeight = barSet->value(category) * scaleY;
94 // Bar *bar = m_bars.at(itemIndex);
95 // bar->setPen(barSet->m_pen);
96 // bar->setBrush(barSet->m_brush);
97 // if (qFuzzyIsNull(rectHeight))
98 // bar->setVisible(false);
99 // else
100 // bar->setVisible(barsVisible);
101
102 // QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
103
104 // if (!qFuzzyIsNull(barSet->value(category)))
105 // label->setText(QString::number(barSet->value(category)));
106 // else
107 // label->setText(QString(""));
108
109 // label->setFont(barSet->m_labelFont);
110 // label->setBrush(barSet->m_labelBrush);
111
112 // if (rectHeight < 0) {
113 // QRectF rect(xPos, yMax - rectHeight, rectWidth, rectHeight);
114 // layout.append(rect);
115 // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
116 // yMax - rectHeight / 2 - label->boundingRect().height() / 2);
117 // yMax -= rectHeight;
118 // } else {
119 // QRectF rect(xPos, yMin - rectHeight, rectWidth, rectHeight);
120 // layout.append(rect);
121 // label->setPos(xPos + (rect.width() / 2 - label->boundingRect().width() / 2),
122 // yMin - rectHeight / 2 - label->boundingRect().height() / 2);
123 // yMin -= rectHeight;
124 // }
125
126 // itemIndex++;
127 // }
128 // }
129
130 return layout;
98 }
131 }
99
132
100 #include "moc_stackedbarchartitem_p.cpp"
133 #include "moc_stackedbarchartitem_p.cpp"
General Comments 0
You need to be logged in to leave comments. Login now