diff --git a/src/barchart/groupedbarchartitem.cpp b/src/barchart/groupedbarchartitem.cpp index 9cb0b08..23a7332 100644 --- a/src/barchart/groupedbarchartitem.cpp +++ b/src/barchart/groupedbarchartitem.cpp @@ -43,19 +43,23 @@ QVector GroupedBarChartItem::calculateLayout() // Domain: qreal width = geometry().width(); qreal height = geometry().height(); - qreal range = m_domainMaxY - m_domainMinY; - qreal scale = (height / range); + qreal rangeY = m_domainMaxY - m_domainMinY; + qreal rangeX = m_domainMaxX - m_domainMinX; + qreal scaleY = (height / rangeY); + qreal scaleX = (width / rangeX); qreal categoryWidth = width / categoryCount; - qreal barWidth = categoryWidth / (setCount+1); + qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); int itemIndex(0); for (int category = 0; category < categoryCount; category++) { - qreal xPos = categoryWidth * category + barWidth / 2 + geometry().topLeft().x(); - qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y(); + qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); for (int set = 0; set < setCount; set++) { QBarSet* barSet = m_series->d_func()->barsetAt(set); - qreal barHeight = barSet->at(category).y() * scale; + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left(); + xPos -= setCount*barWidth/2; + xPos += set*barWidth; + qreal barHeight = barSet->at(category).y() * scaleY; Bar* bar = m_bars.at(itemIndex); QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); @@ -76,7 +80,6 @@ QVector GroupedBarChartItem::calculateLayout() label->setFont(barSet->labelFont()); itemIndex++; - xPos += barWidth; } } return layout; diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index 2bb360a..ad45202 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -37,25 +37,30 @@ QVector PercentBarChartItem::calculateLayout() QVector layout; // Use temporary qreals for accurancy - qreal width = geometry().width(); - qreal height = geometry().height(); - qreal categoryCount = m_series->categoryCount(); - qreal barWidth = width / (m_series->categoryCount() * 2); - qreal xStep = width / categoryCount; - qreal xPos = xStep / 2 - barWidth / 2 + geometry().topLeft().x(); + qreal setCount = m_series->barsetCount(); - qreal range = m_domainMaxY - m_domainMinY; - qreal domainScale = (height / range); + // Domain: + qreal width = geometry().width(); + qreal height = geometry().height(); + qreal rangeY = m_domainMaxY - m_domainMinY; + qreal rangeX = m_domainMaxX - m_domainMinX; + qreal scaleY = (height / rangeY); + qreal scaleX = (width / rangeX); + qreal categoryWidth = width / categoryCount; + qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); int itemIndex(0); for (int category = 0; category < categoryCount; category++) { qreal colSum = m_series->d_func()->categorySum(category); qreal percentage = (100 / colSum); - qreal yPos = height + domainScale * m_domainMinY + geometry().topLeft().y(); - for (int set=0; set < m_series->barsetCount(); set++) { + qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); + for (int set=0; set < setCount; set++) { QBarSet* barSet = m_series->d_func()->barsetAt(set); - qreal barHeight = barSet->at(category).y() * percentage * domainScale; + + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + + qreal barHeight = barSet->at(category).y() * percentage * scaleY; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->pen()); bar->setBrush(barSet->brush()); @@ -80,7 +85,6 @@ QVector PercentBarChartItem::calculateLayout() itemIndex++; yPos -= barHeight; } - xPos += xStep; } return layout; } diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 59544a4..7432ec0 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -614,9 +614,9 @@ void QBarSeriesPrivate::scaleDomain(Domain& domain) qreal x = categoryCount(); qreal y = max(); - minX = qMin(minX, x); + minX = qMin(minX, x) - 0.5; minY = qMin(minY, y); - maxX = qMax(maxX, x); + maxX = qMax(maxX, x) - 0.5; maxY = qMax(maxY, y); tickXCount = x+1; diff --git a/src/barchart/qgroupedbarseries.cpp b/src/barchart/qgroupedbarseries.cpp index 6919c27..d576737 100644 --- a/src/barchart/qgroupedbarseries.cpp +++ b/src/barchart/qgroupedbarseries.cpp @@ -81,9 +81,9 @@ void QGroupedBarSeriesPrivate::scaleDomain(Domain& domain) qreal x = q->categoryCount(); qreal y = max(); - minX = qMin(minX, x); + minX = qMin(minX, x) - 0.5; minY = qMin(minY, y); - maxX = qMax(maxX, x); + maxX = qMax(maxX, x) - 0.5; maxY = qMax(maxY, y); tickXCount = x+1; diff --git a/src/barchart/qpercentbarseries.cpp b/src/barchart/qpercentbarseries.cpp index 28f4d64..13d04e0 100644 --- a/src/barchart/qpercentbarseries.cpp +++ b/src/barchart/qpercentbarseries.cpp @@ -80,8 +80,8 @@ void QPercentBarSeriesPrivate::scaleDomain(Domain& domain) int tickYCount(domain.tickYCount()); qreal x = q->categoryCount(); - minX = qMin(minX, x); - maxX = qMax(maxX, x); + minX = qMin(minX, x) - 0.5; + maxX = qMax(maxX, x) - 0.5; minY = 0; maxY = 100; tickXCount = x+1; diff --git a/src/barchart/qstackedbarseries.cpp b/src/barchart/qstackedbarseries.cpp index 7c72dc3..a6c3326 100644 --- a/src/barchart/qstackedbarseries.cpp +++ b/src/barchart/qstackedbarseries.cpp @@ -81,9 +81,9 @@ void QStackedBarSeriesPrivate::scaleDomain(Domain& domain) qreal x = q->categoryCount(); qreal y = maxCategorySum(); - minX = qMin(minX, x); + minX = qMin(minX, x) - 0.5; minY = qMin(minY, y); - maxX = qMax(maxX, x); + maxX = qMax(maxX, x) - 0.5; maxY = qMax(maxY, y); tickXCount = x+1; diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index 385cd96..e87d9b7 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -36,24 +36,28 @@ QVector StackedBarChartItem::calculateLayout() { QVector layout; // Use temporary qreals for accurancy + qreal categoryCount = m_series->categoryCount(); + qreal setCount = m_series->barsetCount(); // Domain: - qreal range = m_domainMaxY - m_domainMinY; - qreal height = geometry().height(); qreal width = geometry().width(); - qreal scale = (height / range); - qreal categotyCount = m_series->categoryCount(); - qreal barWidth = width / (categotyCount * 2); - qreal xStep = width / categotyCount; - qreal xPos = xStep / 2 - barWidth / 2 + geometry().topLeft().x(); + qreal height = geometry().height(); + qreal rangeY = m_domainMaxY - m_domainMinY; + qreal rangeX = m_domainMaxX - m_domainMinX; + qreal scaleY = (height / rangeY); + qreal scaleX = (width / rangeX); + qreal categoryWidth = width / categoryCount; + qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); int itemIndex(0); - for (int category = 0; category < categotyCount; category++) { - qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y(); + for (int category = 0; category < categoryCount; category++) { + qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y(); for (int set=0; set < m_series->barsetCount(); set++) { QBarSet* barSet = m_series->d_func()->barsetAt(set); - qreal barHeight = barSet->at(category).y() * scale; + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + + qreal barHeight = barSet->at(category).y() * scaleY; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->pen()); bar->setBrush(barSet->brush()); @@ -74,7 +78,6 @@ QVector StackedBarChartItem::calculateLayout() itemIndex++; yPos -= barHeight; } - xPos += xStep; } return layout;