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