##// END OF EJS Templates
Better support for negative values in stacked barcharts. Negative values are stacked from zero to negative direction. Positive values are stacked from zero to positive direction.
sauimone -
r1897:c0fc53c8ab57
parent child
Show More
@@ -50,7 +50,8 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
50 50
51 51 int itemIndex(0);
52 52 for (int category = 0; category < categoryCount; category++) {
53 qreal xPos = -scaleX * m_domainMinX + geometry().left();
53 qreal xMax = -scaleX * m_domainMinX + geometry().left();
54 qreal xMin = -scaleX * m_domainMinX + geometry().left();
54 55 for (int set = 0; set < setCount; set++) {
55 56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 57
@@ -59,8 +60,6 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
59 60 qreal barWidth = barSet->value(category) * scaleX;
60 61 Bar* bar = m_bars.at(itemIndex);
61 62
62 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
63 layout.append(rect);
64 63 bar->setPen(barSet->m_pen);
65 64 bar->setBrush(barSet->m_brush);
66 65 if (qFuzzyIsNull(barHeight)) {
@@ -76,14 +75,23 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
76 75 } else {
77 76 label->setText(QString(""));
78 77 }
79
80 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
81 ,yPos - barHeight/2 - label->boundingRect().height()/2);
82 78 label->setFont(barSet->m_labelFont);
83 79 label->setBrush(barSet->m_labelBrush);
84 80
81 if (barWidth > 0) {
82 QRectF rect(xMax, yPos - barHeight, barWidth, barHeight);
83 layout.append(rect);
84 label->setPos(xMax + (rect.width()/2 - label->boundingRect().width()/2)
85 ,yPos - barHeight/2 - label->boundingRect().height()/2);
86 xMax += barWidth;
87 } else {
88 QRectF rect(xMin, yPos - barHeight, barWidth, barHeight);
89 layout.append(rect);
90 label->setPos(xMin + (rect.width()/2 - label->boundingRect().width()/2)
91 ,yPos - barHeight/2 - label->boundingRect().height()/2);
92 xMin += barWidth;
93 }
85 94 itemIndex++;
86 xPos += barWidth;
87 95 }
88 96 }
89 97 return layout;
@@ -66,10 +66,9 void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain& domain)
66 66 qreal maxY(domain.maxY());
67 67
68 68 qreal y = categoryCount();
69 qreal x = maxCategorySum();
70 minX = qMin(minX, x);
69 minX = qMin(minX, bottom());
71 70 minY = qMin(minY, - (qreal)0.5);
72 maxX = qMax(maxX, x);
71 maxX = qMax(maxX, top());
73 72 maxY = qMax(maxY, y - (qreal)0.5);
74 73
75 74 domain.setRange(minX,maxX,minY,maxY);
@@ -188,15 +188,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
188 188 */
189 189
190 190 /*!
191 This is depreciated constructor.
192 \a parent
193 */
194 QAbstractBarSeries::QAbstractBarSeries(QObject *parent) :
195 QAbstractSeries(*(QAbstractBarSeriesPrivate*)(0),parent)
196 {
197 }
198
199 /*!
200 191 Destructs abstractbarseries and owned barsets.
201 192 */
202 193 QAbstractBarSeries::~QAbstractBarSeries()
@@ -602,6 +593,68 qreal QAbstractBarSeriesPrivate::maxX()
602 593 return max;
603 594 }
604 595
596 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
597 {
598 // Returns top (sum of all positive values) of category.
599 // Returns 0, if all values are negative
600 qreal top(0);
601 int count = m_barSets.count();
602 for (int set = 0; set < count; set++) {
603 if (category < m_barSets.at(set)->count()) {
604 qreal temp = m_barSets.at(set)->at(category);
605 if (temp > 0) {
606 top += temp;
607 }
608 }
609 }
610 return top;
611 }
612
613 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
614 {
615 // Returns bottom (sum of all negative values) of category
616 // Returns 0, if all values are positive
617 qreal bottom(0);
618 int count = m_barSets.count();
619 for (int set = 0; set < count; set++) {
620 if (category < m_barSets.at(set)->count()) {
621 qreal temp = m_barSets.at(set)->at(category);
622 if (temp < 0) {
623 bottom += temp;
624 }
625 }
626 }
627 return bottom;
628 }
629
630 qreal QAbstractBarSeriesPrivate::top()
631 {
632 // Returns top of all categories
633 qreal top(0);
634 int count = categoryCount();
635 for (int i=0; i<count; i++) {
636 qreal temp = categoryTop(i);
637 if (temp > top) {
638 top = temp;
639 }
640 }
641 return top;
642 }
643
644 qreal QAbstractBarSeriesPrivate::bottom()
645 {
646 // Returns bottom of all categories
647 qreal bottom(0);
648 int count = categoryCount();
649 for (int i=0; i<count; i++) {
650 qreal temp = categoryBottom(i);
651 if (temp < bottom) {
652 bottom = temp;
653 }
654 }
655 return bottom;
656 }
657
605 658
606 659 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
607 660 {
@@ -37,10 +37,6 class QTCOMMERCIALCHART_EXPORT QAbstractBarSeries : public QAbstractSeries
37 37 Q_PROPERTY(int count READ count NOTIFY countChanged)
38 38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39 39
40 protected:
41 //TODO DEPRECIATED
42 explicit QAbstractBarSeries(QObject *parent = 0);
43
44 40 public:
45 41 virtual ~QAbstractBarSeries();
46 42
@@ -77,6 +77,10 public:
77 77 qreal maxCategorySum();
78 78 qreal minX();
79 79 qreal maxX();
80 qreal categoryTop(int category);
81 qreal categoryBottom(int category);
82 qreal top();
83 qreal bottom();
80 84
81 85 Q_SIGNALS:
82 86 void clicked(int index, QBarSet *barset);
@@ -97,16 +97,14 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
97 97 qreal maxY(domain.maxY());
98 98
99 99 qreal x = categoryCount();
100 qreal y = maxCategorySum();
101 100 minX = qMin(minX, - (qreal)0.5);
102 minY = qMin(minY, y);
101 minY = qMin(minY, bottom());
103 102 maxX = qMax(maxX, x - (qreal)0.5);
104 maxY = qMax(maxY, y);
103 maxY = qMax(maxY, top());
105 104
106 105 domain.setRange(minX,maxX,minY,maxY);
107 106 }
108 107
109
110 108 ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
111 109 {
112 110 Q_Q(QStackedBarSeries);
@@ -50,7 +50,8 QVector<QRectF> StackedBarChartItem::calculateLayout()
50 50
51 51 int itemIndex(0);
52 52 for (int category = 0; category < categoryCount; category++) {
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
53 qreal yMax = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yMin = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 55 for (int set=0; set < setCount; set++) {
55 56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 57
@@ -66,9 +67,6 QVector<QRectF> StackedBarChartItem::calculateLayout()
66 67 bar->setVisible(barsVisible);
67 68 }
68 69
69 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
70 layout.append(rect);
71
72 70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
73 71
74 72 if (!qFuzzyIsNull(barSet->value(category))) {
@@ -76,13 +74,24 QVector<QRectF> StackedBarChartItem::calculateLayout()
76 74 } else {
77 75 label->setText(QString(""));
78 76 }
79
80 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
81 ,yPos - barHeight/2 - label->boundingRect().height()/2);
82 77 label->setFont(barSet->m_labelFont);
83 78 label->setBrush(barSet->m_labelBrush);
79
80 if (barHeight < 0) {
81 QRectF rect(xPos, yMax-barHeight, barWidth, barHeight);
82 layout.append(rect);
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
84 ,yMax - barHeight/2 - label->boundingRect().height()/2);
85 yMax -= barHeight;
86 } else {
87 QRectF rect(xPos, yMin-barHeight, barWidth, barHeight);
88 layout.append(rect);
89 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
90 ,yMin - barHeight/2 - label->boundingRect().height()/2);
91 yMin -= barHeight;
92 }
93
84 94 itemIndex++;
85 yPos -= barHeight;
86 95 }
87 96 }
88 97
General Comments 0
You need to be logged in to leave comments. Login now