##// END OF EJS Templates
correct x-positioning for barcharts
sauimone -
r1228:0d57f24390a8
parent child
Show More
@@ -43,19 +43,23 QVector<QRectF> GroupedBarChartItem::calculateLayout()
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal range = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal scale = (height / range);
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
48 qreal categoryWidth = width / categoryCount;
50 qreal categoryWidth = width / categoryCount;
49 qreal barWidth = categoryWidth / (setCount+1);
51 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
50
52
51 int itemIndex(0);
53 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
54 for (int category = 0; category < categoryCount; category++) {
53 qreal xPos = categoryWidth * category + barWidth / 2 + geometry().topLeft().x();
55 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
56 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
58
58 qreal barHeight = barSet->at(category).y() * scale;
59 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
60 xPos -= setCount*barWidth/2;
61 xPos += set*barWidth;
62 qreal barHeight = barSet->at(category).y() * scaleY;
59 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
60
64
61 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
@@ -76,7 +80,6 QVector<QRectF> GroupedBarChartItem::calculateLayout()
76 label->setFont(barSet->labelFont());
80 label->setFont(barSet->labelFont());
77
81
78 itemIndex++;
82 itemIndex++;
79 xPos += barWidth;
80 }
83 }
81 }
84 }
82 return layout;
85 return layout;
@@ -37,25 +37,30 QVector<QRectF> PercentBarChartItem::calculateLayout()
37 QVector<QRectF> layout;
37 QVector<QRectF> layout;
38
38
39 // Use temporary qreals for accurancy
39 // Use temporary qreals for accurancy
40 qreal width = geometry().width();
41 qreal height = geometry().height();
42
43 qreal categoryCount = m_series->categoryCount();
40 qreal categoryCount = m_series->categoryCount();
44 qreal barWidth = width / (m_series->categoryCount() * 2);
41 qreal setCount = m_series->barsetCount();
45 qreal xStep = width / categoryCount;
46 qreal xPos = xStep / 2 - barWidth / 2 + geometry().topLeft().x();
47
42
48 qreal range = m_domainMaxY - m_domainMinY;
43 // Domain:
49 qreal domainScale = (height / range);
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 categoryWidth = width / categoryCount;
51 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
50
52
51 int itemIndex(0);
53 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
54 for (int category = 0; category < categoryCount; category++) {
53 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal percentage = (100 / colSum);
56 qreal percentage = (100 / colSum);
55 qreal yPos = height + domainScale * m_domainMinY + geometry().topLeft().y();
57 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 for (int set=0; set < m_series->barsetCount(); set++) {
58 for (int set=0; set < setCount; set++) {
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 qreal barHeight = barSet->at(category).y() * percentage * domainScale;
60
61 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
62
63 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
59 Bar* bar = m_bars.at(itemIndex);
64 Bar* bar = m_bars.at(itemIndex);
60 bar->setPen(barSet->pen());
65 bar->setPen(barSet->pen());
61 bar->setBrush(barSet->brush());
66 bar->setBrush(barSet->brush());
@@ -80,7 +85,6 QVector<QRectF> PercentBarChartItem::calculateLayout()
80 itemIndex++;
85 itemIndex++;
81 yPos -= barHeight;
86 yPos -= barHeight;
82 }
87 }
83 xPos += xStep;
84 }
88 }
85 return layout;
89 return layout;
86 }
90 }
@@ -614,9 +614,9 void QBarSeriesPrivate::scaleDomain(Domain& domain)
614
614
615 qreal x = categoryCount();
615 qreal x = categoryCount();
616 qreal y = max();
616 qreal y = max();
617 minX = qMin(minX, x);
617 minX = qMin(minX, x) - 0.5;
618 minY = qMin(minY, y);
618 minY = qMin(minY, y);
619 maxX = qMax(maxX, x);
619 maxX = qMax(maxX, x) - 0.5;
620 maxY = qMax(maxY, y);
620 maxY = qMax(maxY, y);
621 tickXCount = x+1;
621 tickXCount = x+1;
622
622
@@ -81,9 +81,9 void QGroupedBarSeriesPrivate::scaleDomain(Domain& domain)
81
81
82 qreal x = q->categoryCount();
82 qreal x = q->categoryCount();
83 qreal y = max();
83 qreal y = max();
84 minX = qMin(minX, x);
84 minX = qMin(minX, x) - 0.5;
85 minY = qMin(minY, y);
85 minY = qMin(minY, y);
86 maxX = qMax(maxX, x);
86 maxX = qMax(maxX, x) - 0.5;
87 maxY = qMax(maxY, y);
87 maxY = qMax(maxY, y);
88 tickXCount = x+1;
88 tickXCount = x+1;
89
89
@@ -80,8 +80,8 void QPercentBarSeriesPrivate::scaleDomain(Domain& domain)
80 int tickYCount(domain.tickYCount());
80 int tickYCount(domain.tickYCount());
81
81
82 qreal x = q->categoryCount();
82 qreal x = q->categoryCount();
83 minX = qMin(minX, x);
83 minX = qMin(minX, x) - 0.5;
84 maxX = qMax(maxX, x);
84 maxX = qMax(maxX, x) - 0.5;
85 minY = 0;
85 minY = 0;
86 maxY = 100;
86 maxY = 100;
87 tickXCount = x+1;
87 tickXCount = x+1;
@@ -81,9 +81,9 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
81
81
82 qreal x = q->categoryCount();
82 qreal x = q->categoryCount();
83 qreal y = maxCategorySum();
83 qreal y = maxCategorySum();
84 minX = qMin(minX, x);
84 minX = qMin(minX, x) - 0.5;
85 minY = qMin(minY, y);
85 minY = qMin(minY, y);
86 maxX = qMax(maxX, x);
86 maxX = qMax(maxX, x) - 0.5;
87 maxY = qMax(maxY, y);
87 maxY = qMax(maxY, y);
88 tickXCount = x+1;
88 tickXCount = x+1;
89
89
@@ -36,24 +36,28 QVector<QRectF> StackedBarChartItem::calculateLayout()
36 {
36 {
37 QVector<QRectF> layout;
37 QVector<QRectF> layout;
38 // Use temporary qreals for accurancy
38 // Use temporary qreals for accurancy
39 qreal categoryCount = m_series->categoryCount();
40 qreal setCount = m_series->barsetCount();
39
41
40 // Domain:
42 // Domain:
41 qreal range = m_domainMaxY - m_domainMinY;
42 qreal height = geometry().height();
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal scale = (height / range);
44 qreal height = geometry().height();
45 qreal categotyCount = m_series->categoryCount();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal barWidth = width / (categotyCount * 2);
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal xStep = width / categotyCount;
47 qreal scaleY = (height / rangeY);
48 qreal xPos = xStep / 2 - barWidth / 2 + geometry().topLeft().x();
48 qreal scaleX = (width / rangeX);
49 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
49
51
50 int itemIndex(0);
52 int itemIndex(0);
51 for (int category = 0; category < categotyCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
52 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
53 for (int set=0; set < m_series->barsetCount(); set++) {
55 for (int set=0; set < m_series->barsetCount(); set++) {
54 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
55
57
56 qreal barHeight = barSet->at(category).y() * scale;
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
59
60 qreal barHeight = barSet->at(category).y() * scaleY;
57 Bar* bar = m_bars.at(itemIndex);
61 Bar* bar = m_bars.at(itemIndex);
58 bar->setPen(barSet->pen());
62 bar->setPen(barSet->pen());
59 bar->setBrush(barSet->brush());
63 bar->setBrush(barSet->brush());
@@ -74,7 +78,6 QVector<QRectF> StackedBarChartItem::calculateLayout()
74 itemIndex++;
78 itemIndex++;
75 yPos -= barHeight;
79 yPos -= barHeight;
76 }
80 }
77 xPos += xStep;
78 }
81 }
79
82
80 return layout;
83 return layout;
General Comments 0
You need to be logged in to leave comments. Login now