Handle horizontal bars

This commit is contained in:
Emil Sawicki 2022-01-27 19:20:44 +01:00
parent 5dac77083f
commit d0830a7a37
2 changed files with 29 additions and 14 deletions

View File

@ -20,7 +20,9 @@ void HorizontalBarChart::paintChart(QPainter *painter, QRectF chartRect)
paintHorizontalGrid(painter, chartRect.adjusted( paintHorizontalGrid(painter, chartRect.adjusted(
hPadding(chartRect) + barsShift, hPadding(chartRect) + barsShift,
vPadding(chartRect), vPadding(chartRect),
-(hPadding(chartRect)),-vPadding(chartRect))); -(hPadding(chartRect)),
-vPadding(chartRect)));
paintHorizontalBars(painter, chartRect.adjusted( paintHorizontalBars(painter, chartRect.adjusted(
hPadding(chartRect) + barsShift, hPadding(chartRect) + barsShift,
vPadding(chartRect) * 2, vPadding(chartRect) * 2,
@ -36,25 +38,33 @@ void HorizontalBarChart::paintHorizontalBars(QPainter *painter, QRectF barsRect)
painter->save(); painter->save();
painter->setRenderHint(QPainter::Antialiasing,false); painter->setRenderHint(QPainter::Antialiasing,false);
const AxisData &yAxisData = this->yAxisData(); const AxisData &yAxisData = this->yAxisData();
const qreal delta = yAxisData.delta(); const qreal delta = yAxisData.delta();
qreal vStep = (barsRect.height()-painter->fontMetrics().height()) / valuesCount() / seriesCount(); const qreal verticalOffset = painter->fontMetrics().height();
qreal vStep = (barsRect.height() - verticalOffset) / valuesCount() / seriesCount();
qreal hStep = (barsRect.width()-painter->fontMetrics().boundingRect(QString::number(maxValue())).width()) / delta; qreal hStep = (barsRect.width()-painter->fontMetrics().boundingRect(QString::number(maxValue())).width()) / delta;
if (!m_chartItem->series().isEmpty() && (m_chartItem->itemMode() != DesignMode)){ if (!m_chartItem->series().isEmpty() && (m_chartItem->itemMode() != DesignMode)){
int curSeries = 0; qreal curVOffset = barsRect.top();
if (m_chartItem->horizontalAxisOnTop()) {
curVOffset += verticalOffset;
}
foreach (SeriesItem* series, m_chartItem->series()) { foreach (SeriesItem* series, m_chartItem->series()) {
qreal curVOffset = curSeries*vStep+barsRect.top();
painter->setBrush(series->color()); painter->setBrush(series->color());
qreal y = curVOffset;
foreach (qreal value, series->data()->values()) { foreach (qreal value, series->data()->values()) {
painter->drawRect(QRectF((-minValue()*hStep)+barsRect.left(), curVOffset, value*hStep, vStep)); painter->drawRect(QRectF((-minValue()*hStep)+barsRect.left(), y, value*hStep, vStep));
curVOffset+=vStep*seriesCount(); y+=vStep*seriesCount();
} }
curSeries++; curVOffset += vStep;
} }
} else { } else {
qreal curVOffset = barsRect.top(); qreal curVOffset = barsRect.top();
if (m_chartItem->horizontalAxisOnTop()) {
curVOffset += verticalOffset;
}
int curColor = 0; int curColor = 0;
for (int i=0; i<9; ++i){ for (int i=0; i<9; ++i){
if (curColor==3) curColor=0; if (curColor==3) curColor=0;

View File

@ -633,7 +633,6 @@ void AbstractSeriesChart::updateMinAndMaxValues()
m_yAxisData = AxisData(minYValue, maxYValue); m_yAxisData = AxisData(minYValue, maxYValue);
m_xAxisData = AxisData(minXValue, maxXValue); m_xAxisData = AxisData(minXValue, maxXValue);
m_xAxisData.setReverseDirection(true);
} }
qreal AbstractSeriesChart::hPadding(QRectF chartRect) qreal AbstractSeriesChart::hPadding(QRectF chartRect)
@ -761,13 +760,18 @@ void AbstractSeriesChart::paintHorizontalGrid(QPainter *painter, QRectF gridRect
painter->setFont(adaptValuesFont(hStep-4, painter->font())); painter->setFont(adaptValuesFont(hStep-4, painter->font()));
QPointF textPos;
if (m_chartItem->horizontalAxisOnTop()) {
textPos.setY(gridRect.top());
} else {
textPos.setY(gridRect.bottom() - painter->fontMetrics().height());
}
for (int i = 0 ; i < lineCount ; i++ ) { for (int i = 0 ; i < lineCount ; i++ ) {
painter->drawText(QRectF(gridRect.left() + 4 + hStep * i, gridRect.bottom() - painter->fontMetrics().height(), const qreal x = gridRect.left() + hStep * i;
hStep, painter->fontMetrics().height()), textPos.setX(x + 4);
painter->drawText(QRectF(textPos, QSizeF(hStep, painter->fontMetrics().height())),
axisLabel(i, yAxisData)); axisLabel(i, yAxisData));
painter->drawLine( gridRect.left()+hStep*i, gridRect.bottom(), painter->drawLine(x, gridRect.bottom(), x, gridRect.top());
gridRect.left()+hStep*i, gridRect.top());
} }
painter->restore(); painter->restore();
} }
@ -790,7 +794,6 @@ void AbstractSeriesChart::paintVerticalGrid(QPainter *painter, QRectF gridRect)
const QTextOption verticalTextOption(Qt::AlignRight); const QTextOption verticalTextOption(Qt::AlignRight);
for (int i = 0 ; i < lineCount ; i++ ) { for (int i = 0 ; i < lineCount ; i++ ) {
const qreal y = vStep * i; const qreal y = vStep * i;
// TODO_ES handle horizontalAxisOnTop
painter->drawText(QRectF(gridRect.bottomLeft()-QPointF(textPositionOffset,y+halfFontHeight), painter->drawText(QRectF(gridRect.bottomLeft()-QPointF(textPositionOffset,y+halfFontHeight),
QSizeF(valuesHMargin,fontHeight)), QSizeF(valuesHMargin,fontHeight)),
axisLabel(i, yAxisData), axisLabel(i, yAxisData),
@ -823,6 +826,8 @@ void AbstractSeriesChart::paintGrid(QPainter *painter, QRectF gridRect)
const qreal vStep = gridRect.height() / yAxisSegmentCount; const qreal vStep = gridRect.height() / yAxisSegmentCount;
const qreal hStep = (gridRect.width() - valuesHMargin - gridOffset) / xAxisSegmentCount; const qreal hStep = (gridRect.width() - valuesHMargin - gridOffset) / xAxisSegmentCount;
painter->setFont(adaptValuesFont(hStep-4, painter->font()));
// Vertical axis lines // Vertical axis lines
const QTextOption verticalTextOption(Qt::AlignRight); const QTextOption verticalTextOption(Qt::AlignRight);
for (int i = 0 ; i < yAxisLineCount ; i++ ) { for (int i = 0 ; i < yAxisLineCount ; i++ ) {