##// 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 int itemIndex(0);
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
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 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
55 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56
57
@@ -59,8 +60,6 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
59 qreal barWidth = barSet->value(category) * scaleX;
60 qreal barWidth = barSet->value(category) * scaleX;
60 Bar* bar = m_bars.at(itemIndex);
61 Bar* bar = m_bars.at(itemIndex);
61
62
62 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
63 layout.append(rect);
64 bar->setPen(barSet->m_pen);
63 bar->setPen(barSet->m_pen);
65 bar->setBrush(barSet->m_brush);
64 bar->setBrush(barSet->m_brush);
66 if (qFuzzyIsNull(barHeight)) {
65 if (qFuzzyIsNull(barHeight)) {
@@ -76,14 +75,23 QVector<QRectF> HorizontalStackedBarChartItem::calculateLayout()
76 } else {
75 } else {
77 label->setText(QString(""));
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 label->setFont(barSet->m_labelFont);
78 label->setFont(barSet->m_labelFont);
83 label->setBrush(barSet->m_labelBrush);
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 itemIndex++;
94 itemIndex++;
86 xPos += barWidth;
87 }
95 }
88 }
96 }
89 return layout;
97 return layout;
@@ -66,10 +66,9 void QHorizontalStackedBarSeriesPrivate::scaleDomain(Domain& domain)
66 qreal maxY(domain.maxY());
66 qreal maxY(domain.maxY());
67
67
68 qreal y = categoryCount();
68 qreal y = categoryCount();
69 qreal x = maxCategorySum();
69 minX = qMin(minX, bottom());
70 minX = qMin(minX, x);
71 minY = qMin(minY, - (qreal)0.5);
70 minY = qMin(minY, - (qreal)0.5);
72 maxX = qMax(maxX, x);
71 maxX = qMax(maxX, top());
73 maxY = qMax(maxY, y - (qreal)0.5);
72 maxY = qMax(maxY, y - (qreal)0.5);
74
73
75 domain.setRange(minX,maxX,minY,maxY);
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 Destructs abstractbarseries and owned barsets.
191 Destructs abstractbarseries and owned barsets.
201 */
192 */
202 QAbstractBarSeries::~QAbstractBarSeries()
193 QAbstractBarSeries::~QAbstractBarSeries()
@@ -602,6 +593,68 qreal QAbstractBarSeriesPrivate::maxX()
602 return max;
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 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
659 void QAbstractBarSeriesPrivate::scaleDomain(Domain& domain)
607 {
660 {
@@ -37,10 +37,6 class QTCOMMERCIALCHART_EXPORT QAbstractBarSeries : public QAbstractSeries
37 Q_PROPERTY(int count READ count NOTIFY countChanged)
37 Q_PROPERTY(int count READ count NOTIFY countChanged)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
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 public:
40 public:
45 virtual ~QAbstractBarSeries();
41 virtual ~QAbstractBarSeries();
46
42
@@ -77,6 +77,10 public:
77 qreal maxCategorySum();
77 qreal maxCategorySum();
78 qreal minX();
78 qreal minX();
79 qreal maxX();
79 qreal maxX();
80 qreal categoryTop(int category);
81 qreal categoryBottom(int category);
82 qreal top();
83 qreal bottom();
80
84
81 Q_SIGNALS:
85 Q_SIGNALS:
82 void clicked(int index, QBarSet *barset);
86 void clicked(int index, QBarSet *barset);
@@ -97,16 +97,14 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
97 qreal maxY(domain.maxY());
97 qreal maxY(domain.maxY());
98
98
99 qreal x = categoryCount();
99 qreal x = categoryCount();
100 qreal y = maxCategorySum();
101 minX = qMin(minX, - (qreal)0.5);
100 minX = qMin(minX, - (qreal)0.5);
102 minY = qMin(minY, y);
101 minY = qMin(minY, bottom());
103 maxX = qMax(maxX, x - (qreal)0.5);
102 maxX = qMax(maxX, x - (qreal)0.5);
104 maxY = qMax(maxY, y);
103 maxY = qMax(maxY, top());
105
104
106 domain.setRange(minX,maxX,minY,maxY);
105 domain.setRange(minX,maxX,minY,maxY);
107 }
106 }
108
107
109
110 ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
108 ChartElement* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
111 {
109 {
112 Q_Q(QStackedBarSeries);
110 Q_Q(QStackedBarSeries);
@@ -50,7 +50,8 QVector<QRectF> StackedBarChartItem::calculateLayout()
50
50
51 int itemIndex(0);
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
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 for (int set=0; set < setCount; set++) {
55 for (int set=0; set < setCount; set++) {
55 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56 QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
56
57
@@ -66,9 +67,6 QVector<QRectF> StackedBarChartItem::calculateLayout()
66 bar->setVisible(barsVisible);
67 bar->setVisible(barsVisible);
67 }
68 }
68
69
69 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
70 layout.append(rect);
71
72 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
73
71
74 if (!qFuzzyIsNull(barSet->value(category))) {
72 if (!qFuzzyIsNull(barSet->value(category))) {
@@ -76,13 +74,24 QVector<QRectF> StackedBarChartItem::calculateLayout()
76 } else {
74 } else {
77 label->setText(QString(""));
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 label->setFont(barSet->m_labelFont);
77 label->setFont(barSet->m_labelFont);
83 label->setBrush(barSet->m_labelBrush);
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 itemIndex++;
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