##// END OF EJS Templates
BarChartItems code cleanup
Marek Rosa -
r2305:29e33b79f138
parent child
Show More
@@ -46,8 +46,10 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphics
46 46 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
47 47 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
48 48 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
49 setZValue(ChartPresenter::BarSeriesZValue);
49 setZValue(ChartPresenter::BarSeriesZValue);
50 50 handleDataStructureChanged();
51 handleVisibleChanged();
52 handleUpdatedBars();
51 53 }
52 54
53 55 AbstractBarChartItem::~AbstractBarChartItem()
@@ -89,8 +91,12 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
89 91
90 92 m_layout = layout;
91 93
92 for (int i = 0; i < m_bars.count(); i++)
94 for (int i = 0; i < m_bars.count(); i++) {
93 95 m_bars.at(i)->setRect(layout.at(i));
96 QGraphicsSimpleTextItem *label = m_labels.at(i);
97 label->setPos(layout.at(i).center() - label->boundingRect().center());
98
99 }
94 100 }
95 101 //handlers
96 102
@@ -192,6 +198,7 void AbstractBarChartItem::handleUpdatedBars()
192 198 bar->update();
193 199
194 200 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
201 label->setText(QString("%1").arg(barSet->value(category)));
195 202 label->setFont(barSet->m_labelFont);
196 203 label->setBrush(barSet->m_labelBrush);
197 204 label->update();
@@ -68,7 +68,7 public Q_SLOTS:
68 68 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
69 69 void handleVisibleChanged();
70 70 void handleOpacityChanged();
71 void handleUpdatedBars();
71 virtual void handleUpdatedBars();
72 72
73 73 protected:
74 74
@@ -37,7 +37,6 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
37 37 // Use temporary qreals for accuracy
38 38 qreal categoryCount = m_series->d_func()->categoryCount();
39 39 qreal setCount = m_series->count();
40 // bool barsVisible = m_series->isVisible();
41 40 qreal barWidth = m_series->d_func()->barWidth();
42 41
43 42 int itemIndex(0);
@@ -46,8 +45,7 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
46 45 qreal value = m_series->barSets().at(set)->at(category);
47 46 QRectF rect;
48 47 QPointF topLeft;
49 if (domain()->type() == AbstractDomain::LogXYDomain
50 || domain()->type() == AbstractDomain::LogXLogYDomain)
48 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
51 49 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
52 50 else
53 51 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
@@ -58,71 +56,12 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
58 56 layout.append(rect);
59 57
60 58 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
59 label->setPos(rect.center() - label->boundingRect().center());
61 60 label->setZValue(200);
62 61 itemIndex++;
63 if (!qFuzzyCompare(value, 0))
64 label->setText(QString::number(value));
65 else
66 label->setText(QString(""));
67
68 // label->setFont(m_series->barSets().at(set)->labelFont());
69 // label->setBrush(m_series->barSets().at(set)->labelBrush());
70 62 label->setBrush(Qt::black);
71
72 label->setPos(rect.center() - label->boundingRect().center());
73
74 63 }
75 64 }
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 // }
126 65 return layout;
127 66 }
128 67
@@ -37,8 +37,6 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
37 37 // Use temporary qreals for accuracy
38 38 qreal categoryCount = m_series->d_func()->categoryCount();
39 39 qreal setCount = m_series->count();
40 // bool barsVisible = m_series->isVisible();
41
42 40 qreal barWidth = m_series->d_func()->barWidth();
43 41
44 42 for(int category = 0; category < categoryCount; category++) {
@@ -48,8 +46,7 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
48 46 qreal value = m_series->barSets().at(set)->at(category);
49 47 QRectF rect;
50 48 QPointF topLeft;
51 if (domain()->type() == AbstractDomain::LogXYDomain
52 || domain()->type() == AbstractDomain::LogXLogYDomain)
49 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
53 50 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category + barWidth/2));
54 51 else
55 52 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category + barWidth/2));
@@ -60,60 +57,36 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
60 57 sum +=value;
61 58 }
62 59 }
60 return layout;
61 }
63 62
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);
63 void HorizontalPercentBarChartItem::handleUpdatedBars()
64 {
65 // Handle changes in pen, brush, labels etc.
66 int categoryCount = m_series->d_func()->categoryCount();
67 int setCount = m_series->count();
68 int itemIndex(0);
111 69
112 // itemIndex++;
113 // xPos += rectWidth;
114 // }
115 // }
116 return layout;
70 for (int category = 0; category < categoryCount; category++) {
71 for (int set = 0; set < setCount; set++) {
72 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
73 Bar *bar = m_bars.at(itemIndex);
74 bar->setPen(barSet->m_pen);
75 bar->setBrush(barSet->m_brush);
76 bar->update();
77
78 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
79 int p = m_series->d_func()->percentageAt(set, category) * 100;
80 QString vString(QString::number(p));
81 vString.truncate(3);
82 vString.append("%");
83 label->setText(vString);
84 label->setFont(barSet->m_labelFont);
85 label->setBrush(barSet->m_labelBrush);
86 label->update();
87 itemIndex++;
88 }
89 }
117 90 }
118 91
119 92 #include "moc_horizontalpercentbarchartitem_p.cpp"
@@ -40,6 +40,7 class HorizontalPercentBarChartItem : public AbstractBarChartItem
40 40 Q_OBJECT
41 41 public:
42 42 HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
43 void handleUpdatedBars();
43 44
44 45 private:
45 46 virtual QVector<QRectF> calculateLayout();
@@ -37,8 +37,6 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
37 37 // Use temporary qreals for accuracy
38 38 qreal categoryCount = m_series->d_func()->categoryCount();
39 39 qreal setCount = m_series->count();
40 // bool barsVisible = m_series->isVisible();
41
42 40 qreal barWidth = m_series->d_func()->barWidth();
43 41
44 42 for(int category = 0; category < categoryCount; category++) {
@@ -51,16 +49,14 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
51 49 QPointF bottomRight;
52 50 if (value < 0) {
53 51 bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2));
54 if (domain()->type() == AbstractDomain::LogXYDomain
55 || domain()->type() == AbstractDomain::LogXLogYDomain)
52 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
56 53 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : domain()->minX(), category + barWidth / 2));
57 54 else
58 55 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : 0, category + barWidth / 2));
59 56 negativeSum += value;
60 57 } else {
61 58 bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2));
62 if (domain()->type() == AbstractDomain::LogXYDomain
63 || domain()->type() == AbstractDomain::LogXLogYDomain)
59 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
64 60 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : domain()->minX(), category + barWidth / 2));
65 61 else
66 62 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : 0, category + barWidth / 2));
@@ -71,60 +67,6 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
71 67 layout.append(rect);
72 68 }
73 69 }
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 // }
128 70 return layout;
129 71 }
130 72
@@ -38,89 +38,23 QVector<QRectF> BarChartItem::calculateLayout()
38 38 // Use temporary qreals for accuracy
39 39 qreal categoryCount = m_series->d_func()->categoryCount();
40 40 qreal setCount = m_series->count();
41 // bool barsVisible = m_series->isVisible();
42 41 qreal barWidth = m_series->d_func()->barWidth();
43 // barWidth = 1.8;
44 42
45 int itemIndex(0);
46 43 for(int category = 0; category < categoryCount; category++) {
47 44 for (int set = 0; set < setCount; set++) {
48 45 qreal value = m_series->barSets().at(set)->at(category);
49 46 QRectF rect;
50 47 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value));
51 48 QPointF bottomRight;
52 if (domain()->type() == AbstractDomain::XLogYDomain
53 || domain()->type() == AbstractDomain::LogXLogYDomain)
49 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
54 50 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()));
55 51 else
56 52 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0));
57 53 rect.setTopLeft(topLeft);
58 54 rect.setBottomRight(bottomRight);
59 55 layout.append(rect);
60
61 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
62 label->setZValue(200);
63 itemIndex++;
64 if (!qFuzzyCompare(value, 0))
65 label->setText(QString::number(value));
66 else
67 label->setText(QString(""));
68
69 // label->setFont(m_series->barSets().at(set)->labelFont());
70 // label->setBrush(m_series->barSets().at(set)->labelBrush());
71 label->setBrush(Qt::black);
72
73 label->setPos(rect.center() - label->boundingRect().center());
74
75 56 }
76 57 }
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 // }
124 58 return layout;
125 59 }
126 60
@@ -38,8 +38,6 QVector<QRectF> PercentBarChartItem::calculateLayout()
38 38 // Use temporary qreals for accuracy
39 39 qreal categoryCount = m_series->d_func()->categoryCount();
40 40 qreal setCount = m_series->count();
41 // bool barsVisible = m_series->isVisible();
42
43 41 qreal barWidth = m_series->d_func()->barWidth();
44 42
45 43 for(int category = 0; category < categoryCount; category++) {
@@ -47,11 +45,10 QVector<QRectF> PercentBarChartItem::calculateLayout()
47 45 qreal categorySum = m_series->d_func()->categorySum(category);
48 46 for (int set = 0; set < setCount; set++) {
49 47 qreal value = m_series->barSets().at(set)->at(category);
50 QRectF rect;
48 QRectF rect;
51 49 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum));
52 50 QPointF bottomRight;
53 if (domain()->type() == AbstractDomain::XLogYDomain
54 || domain()->type() == AbstractDomain::LogXLogYDomain)
51 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
55 52 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()));
56 53 else
57 54 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0));
@@ -61,60 +58,36 QVector<QRectF> PercentBarChartItem::calculateLayout()
61 58 sum +=value;
62 59 }
63 60 }
61 return layout;
62 }
64 63
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);
64 void PercentBarChartItem::handleUpdatedBars()
65 {
66 // Handle changes in pen, brush, labels etc.
67 int categoryCount = m_series->d_func()->categoryCount();
68 int setCount = m_series->count();
69 int itemIndex(0);
112 70
113 // itemIndex++;
114 // yPos -= rectHeight;
115 // }
116 // }
117 return layout;
71 for (int category = 0; category < categoryCount; category++) {
72 for (int set = 0; set < setCount; set++) {
73 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
74 Bar *bar = m_bars.at(itemIndex);
75 bar->setPen(barSet->m_pen);
76 bar->setBrush(barSet->m_brush);
77 bar->update();
78
79 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
80 int p = m_series->d_func()->percentageAt(set, category) * 100;
81 QString vString(QString::number(p));
82 vString.truncate(3);
83 vString.append("%");
84 label->setText(vString);
85 label->setFont(barSet->m_labelFont);
86 label->setBrush(barSet->m_labelBrush);
87 label->update();
88 itemIndex++;
89 }
90 }
118 91 }
119 92
120 93 #include "moc_percentbarchartitem_p.cpp"
@@ -43,9 +43,10 class PercentBarChartItem : public AbstractBarChartItem
43 43 Q_OBJECT
44 44 public:
45 45 PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
46 void handleUpdatedBars();
46 47
47 48 private:
48 virtual QVector<QRectF> calculateLayout();
49 virtual QVector<QRectF> calculateLayout();
49 50 };
50 51
51 52 QTCOMMERCIALCHART_END_NAMESPACE
@@ -37,7 +37,6 QVector<QRectF> StackedBarChartItem::calculateLayout()
37 37 // Use temporary qreals for accuracy
38 38 qreal categoryCount = m_series->d_func()->categoryCount();
39 39 qreal setCount = m_series->count();
40 // bool barsVisible = m_series->isVisible();
41 40 qreal barWidth = m_series->d_func()->barWidth();
42 41
43 42 for(int category = 0; category < categoryCount; category++) {
@@ -50,16 +49,14 QVector<QRectF> StackedBarChartItem::calculateLayout()
50 49 QPointF bottomRight;
51 50 if (value < 0) {
52 51 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum));
53 if (domain()->type() == AbstractDomain::XLogYDomain
54 || domain()->type() == AbstractDomain::LogXLogYDomain)
52 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
55 53 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()));
56 54 else
57 55 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0));
58 56 negativeSum += value;
59 57 } else {
60 58 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum));
61 if (domain()->type() == AbstractDomain::XLogYDomain
62 || domain()->type() == AbstractDomain::LogXLogYDomain)
59 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
63 60 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()));
64 61 else
65 62 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0));
@@ -70,64 +67,7 QVector<QRectF> StackedBarChartItem::calculateLayout()
70 67 layout.append(rect);
71 68 }
72 69 }
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 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;
70 return layout;
131 71 }
132 72
133 73 #include "moc_stackedbarchartitem_p.cpp"
General Comments 0
You need to be logged in to leave comments. Login now