##// END OF EJS Templates
BarChartItems code cleanup
Marek Rosa -
r2305:29e33b79f138
parent child
Show More
@@ -48,6 +48,8 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphics
48 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
48 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
49 setZValue(ChartPresenter::BarSeriesZValue);
49 setZValue(ChartPresenter::BarSeriesZValue);
50 handleDataStructureChanged();
50 handleDataStructureChanged();
51 handleVisibleChanged();
52 handleUpdatedBars();
51 }
53 }
52
54
53 AbstractBarChartItem::~AbstractBarChartItem()
55 AbstractBarChartItem::~AbstractBarChartItem()
@@ -89,8 +91,12 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
89
91
90 m_layout = layout;
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 m_bars.at(i)->setRect(layout.at(i));
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 //handlers
101 //handlers
96
102
@@ -192,6 +198,7 void AbstractBarChartItem::handleUpdatedBars()
192 bar->update();
198 bar->update();
193
199
194 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
200 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
201 label->setText(QString("%1").arg(barSet->value(category)));
195 label->setFont(barSet->m_labelFont);
202 label->setFont(barSet->m_labelFont);
196 label->setBrush(barSet->m_labelBrush);
203 label->setBrush(barSet->m_labelBrush);
197 label->update();
204 label->update();
@@ -68,7 +68,7 public Q_SLOTS:
68 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
68 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
69 void handleVisibleChanged();
69 void handleVisibleChanged();
70 void handleOpacityChanged();
70 void handleOpacityChanged();
71 void handleUpdatedBars();
71 virtual void handleUpdatedBars();
72
72
73 protected:
73 protected:
74
74
@@ -37,7 +37,6 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();
41 qreal barWidth = m_series->d_func()->barWidth();
40 qreal barWidth = m_series->d_func()->barWidth();
42
41
43 int itemIndex(0);
42 int itemIndex(0);
@@ -46,8 +45,7 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
46 qreal value = m_series->barSets().at(set)->at(category);
45 qreal value = m_series->barSets().at(set)->at(category);
47 QRectF rect;
46 QRectF rect;
48 QPointF topLeft;
47 QPointF topLeft;
49 if (domain()->type() == AbstractDomain::LogXYDomain
48 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
50 || domain()->type() == AbstractDomain::LogXLogYDomain)
51 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
49 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
52 else
50 else
53 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
51 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2 + (set + 1)/(setCount) * barWidth));
@@ -58,71 +56,12 QVector<QRectF> HorizontalBarChartItem::calculateLayout()
58 layout.append(rect);
56 layout.append(rect);
59
57
60 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
58 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
59 label->setPos(rect.center() - label->boundingRect().center());
61 label->setZValue(200);
60 label->setZValue(200);
62 itemIndex++;
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 label->setBrush(Qt::black);
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 return layout;
65 return layout;
127 }
66 }
128
67
@@ -37,8 +37,6 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();
41
42 qreal barWidth = m_series->d_func()->barWidth();
40 qreal barWidth = m_series->d_func()->barWidth();
43
41
44 for(int category = 0; category < categoryCount; category++) {
42 for(int category = 0; category < categoryCount; category++) {
@@ -48,8 +46,7 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
48 qreal value = m_series->barSets().at(set)->at(category);
46 qreal value = m_series->barSets().at(set)->at(category);
49 QRectF rect;
47 QRectF rect;
50 QPointF topLeft;
48 QPointF topLeft;
51 if (domain()->type() == AbstractDomain::LogXYDomain
49 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
52 || domain()->type() == AbstractDomain::LogXLogYDomain)
53 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category + barWidth/2));
50 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category + barWidth/2));
54 else
51 else
55 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category + barWidth/2));
52 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category + barWidth/2));
@@ -60,60 +57,36 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
60 sum +=value;
57 sum +=value;
61 }
58 }
62 }
59 }
60 return layout;
61 }
63
62
64 // // AbstractDomain:
63 void HorizontalPercentBarChartItem::handleUpdatedBars()
65 // qreal width = geometry().width();
64 {
66 // qreal height = geometry().height();
65 // Handle changes in pen, brush, labels etc.
67 // qreal rangeY = m_domainMaxY - m_domainMinY;
66 int categoryCount = m_series->d_func()->categoryCount();
68 // qreal rangeX = m_domainMaxX - m_domainMinX;
67 int setCount = m_series->count();
69 // qreal scaleY = (height / rangeY);
68 int itemIndex(0);
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
69
112 // itemIndex++;
70 for (int category = 0; category < categoryCount; category++) {
113 // xPos += rectWidth;
71 for (int set = 0; set < setCount; set++) {
114 // }
72 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
115 // }
73 Bar *bar = m_bars.at(itemIndex);
116 return layout;
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 #include "moc_horizontalpercentbarchartitem_p.cpp"
92 #include "moc_horizontalpercentbarchartitem_p.cpp"
@@ -40,6 +40,7 class HorizontalPercentBarChartItem : public AbstractBarChartItem
40 Q_OBJECT
40 Q_OBJECT
41 public:
41 public:
42 HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
42 HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
43 void handleUpdatedBars();
43
44
44 private:
45 private:
45 virtual QVector<QRectF> calculateLayout();
46 virtual QVector<QRectF> calculateLayout();
@@ -37,8 +37,6 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();
41
42 qreal barWidth = m_series->d_func()->barWidth();
40 qreal barWidth = m_series->d_func()->barWidth();
43
41
44 for(int category = 0; category < categoryCount; category++) {
42 for(int category = 0; category < categoryCount; category++) {
@@ -51,16 +49,14 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
51 QPointF bottomRight;
49 QPointF bottomRight;
52 if (value < 0) {
50 if (value < 0) {
53 bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2));
51 bottomRight = domain()->calculateGeometryPoint(QPointF(value + negativeSum, category - barWidth / 2));
54 if (domain()->type() == AbstractDomain::LogXYDomain
52 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
55 || domain()->type() == AbstractDomain::LogXLogYDomain)
56 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : domain()->minX(), category + barWidth / 2));
53 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : domain()->minX(), category + barWidth / 2));
57 else
54 else
58 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : 0, category + barWidth / 2));
55 topLeft = domain()->calculateGeometryPoint(QPointF(set ? negativeSum : 0, category + barWidth / 2));
59 negativeSum += value;
56 negativeSum += value;
60 } else {
57 } else {
61 bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2));
58 bottomRight = domain()->calculateGeometryPoint(QPointF(value + positiveSum, category - barWidth / 2));
62 if (domain()->type() == AbstractDomain::LogXYDomain
59 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
63 || domain()->type() == AbstractDomain::LogXLogYDomain)
64 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : domain()->minX(), category + barWidth / 2));
60 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : domain()->minX(), category + barWidth / 2));
65 else
61 else
66 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : 0, category + barWidth / 2));
62 topLeft = domain()->calculateGeometryPoint(QPointF(set ? positiveSum : 0, category + barWidth / 2));
@@ -71,60 +67,6 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
71 layout.append(rect);
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 return layout;
70 return layout;
129 }
71 }
130
72
@@ -38,89 +38,23 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();
42 qreal barWidth = m_series->d_func()->barWidth();
41 qreal barWidth = m_series->d_func()->barWidth();
43 // barWidth = 1.8;
44
42
45 int itemIndex(0);
46 for(int category = 0; category < categoryCount; category++) {
43 for(int category = 0; category < categoryCount; category++) {
47 for (int set = 0; set < setCount; set++) {
44 for (int set = 0; set < setCount; set++) {
48 qreal value = m_series->barSets().at(set)->at(category);
45 qreal value = m_series->barSets().at(set)->at(category);
49 QRectF rect;
46 QRectF rect;
50 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value));
47 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value));
51 QPointF bottomRight;
48 QPointF bottomRight;
52 if (domain()->type() == AbstractDomain::XLogYDomain
49 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
53 || domain()->type() == AbstractDomain::LogXLogYDomain)
54 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()));
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()));
55 else
51 else
56 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0));
52 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0));
57 rect.setTopLeft(topLeft);
53 rect.setTopLeft(topLeft);
58 rect.setBottomRight(bottomRight);
54 rect.setBottomRight(bottomRight);
59 layout.append(rect);
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 return layout;
58 return layout;
125 }
59 }
126
60
@@ -38,8 +38,6 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();
42
43 qreal barWidth = m_series->d_func()->barWidth();
41 qreal barWidth = m_series->d_func()->barWidth();
44
42
45 for(int category = 0; category < categoryCount; category++) {
43 for(int category = 0; category < categoryCount; category++) {
@@ -50,8 +48,7 QVector<QRectF> PercentBarChartItem::calculateLayout()
50 QRectF rect;
48 QRectF rect;
51 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum));
49 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum));
52 QPointF bottomRight;
50 QPointF bottomRight;
53 if (domain()->type() == AbstractDomain::XLogYDomain
51 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
54 || domain()->type() == AbstractDomain::LogXLogYDomain)
55 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()));
52 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()));
56 else
53 else
57 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0));
54 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0));
@@ -61,60 +58,36 QVector<QRectF> PercentBarChartItem::calculateLayout()
61 sum +=value;
58 sum +=value;
62 }
59 }
63 }
60 }
61 return layout;
62 }
64
63
65 // AbstractDomain:
64 void PercentBarChartItem::handleUpdatedBars()
66 // qreal width = geometry().width();
65 {
67 // qreal height = geometry().height();
66 // Handle changes in pen, brush, labels etc.
68 // qreal rangeY = m_domainMaxY - m_domainMinY;
67 int categoryCount = m_series->d_func()->categoryCount();
69 // qreal rangeX = m_domainMaxX - m_domainMinX;
68 int setCount = m_series->count();
70 // qreal scaleY = (height / rangeY);
69 int itemIndex(0);
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
70
113 // itemIndex++;
71 for (int category = 0; category < categoryCount; category++) {
114 // yPos -= rectHeight;
72 for (int set = 0; set < setCount; set++) {
115 // }
73 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
116 // }
74 Bar *bar = m_bars.at(itemIndex);
117 return layout;
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 #include "moc_percentbarchartitem_p.cpp"
93 #include "moc_percentbarchartitem_p.cpp"
@@ -43,6 +43,7 class PercentBarChartItem : public AbstractBarChartItem
43 Q_OBJECT
43 Q_OBJECT
44 public:
44 public:
45 PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
45 PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
46 void handleUpdatedBars();
46
47
47 private:
48 private:
48 virtual QVector<QRectF> calculateLayout();
49 virtual QVector<QRectF> calculateLayout();
@@ -37,7 +37,6 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();
41 qreal barWidth = m_series->d_func()->barWidth();
40 qreal barWidth = m_series->d_func()->barWidth();
42
41
43 for(int category = 0; category < categoryCount; category++) {
42 for(int category = 0; category < categoryCount; category++) {
@@ -50,16 +49,14 QVector<QRectF> StackedBarChartItem::calculateLayout()
50 QPointF bottomRight;
49 QPointF bottomRight;
51 if (value < 0) {
50 if (value < 0) {
52 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum));
51 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum));
53 if (domain()->type() == AbstractDomain::XLogYDomain
52 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
54 || domain()->type() == AbstractDomain::LogXLogYDomain)
55 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()));
53 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()));
56 else
54 else
57 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0));
55 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0));
58 negativeSum += value;
56 negativeSum += value;
59 } else {
57 } else {
60 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum));
58 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum));
61 if (domain()->type() == AbstractDomain::XLogYDomain
59 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
62 || domain()->type() == AbstractDomain::LogXLogYDomain)
63 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()));
60 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()));
64 else
61 else
65 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0));
62 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0));
@@ -70,63 +67,6 QVector<QRectF> StackedBarChartItem::calculateLayout()
70 layout.append(rect);
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
General Comments 0
You need to be logged in to leave comments. Login now