diff --git a/limereport/items/charts/lrgridlineschart.cpp b/limereport/items/charts/lrgridlineschart.cpp index 3ed300f..6ce6411 100644 --- a/limereport/items/charts/lrgridlineschart.cpp +++ b/limereport/items/charts/lrgridlineschart.cpp @@ -5,12 +5,26 @@ void GridLinesChart::paintChart(QPainter *painter, QRectF chartRect) { updateMinAndMaxValues(); - const qreal valuesHMargin = this->valuesHMargin(painter); - const qreal valuesVMargin = this->valuesVMargin(painter); - const qreal hPadding = this->hPadding(chartRect); const qreal vPadding = this->vPadding(chartRect); + const qreal valuesVMargin = this->valuesVMargin(painter); + const qreal topOffset = painter->fontMetrics().height(); + + QRectF gridRect = chartRect.adjusted( + hPadding, + vPadding + valuesVMargin + topOffset, + -hPadding * 3, + -vPadding + ); + + // Adapt font for horizontal axis + painter->setFont(adaptFont((gridRect.width() - this->valuesHMargin(painter)) / xAxisData().segmentCount() * 0.8, + painter->font(), + xAxisData())); + + const qreal valuesHMargin = this->valuesHMargin(painter); + QRectF calcRect = horizontalLabelsRect( painter, chartRect.adjusted( @@ -20,14 +34,7 @@ void GridLinesChart::paintChart(QPainter *painter, QRectF chartRect) -vPadding ) ); - const qreal barsShift = calcRect.height(); - const qreal topOffset = painter->fontMetrics().height(); - QRectF gridRect = chartRect.adjusted( - hPadding, - vPadding + valuesVMargin + topOffset, - -hPadding * 3, - -(vPadding + barsShift) - ); + gridRect.adjust(0, 0, 0, -calcRect.height()); if (!m_chartItem->horizontalAxisOnTop()) { // Draw labels above the grid diff --git a/limereport/items/lrchartitem.cpp b/limereport/items/lrchartitem.cpp index b97259d..401b92f 100644 --- a/limereport/items/lrchartitem.cpp +++ b/limereport/items/lrchartitem.cpp @@ -777,7 +777,7 @@ void AbstractSeriesChart::paintHorizontalGrid(QPainter *painter, QRectF gridRect painter->setRenderHint(QPainter::Antialiasing,false); qreal hStep = (gridRect.width() - painter->fontMetrics().boundingRect(QString::number(maxValue())).width()) / segmentCount; - painter->setFont(adaptValuesFont(hStep-4, painter->font())); + painter->setFont(adaptFont(hStep-4, painter->font(), yAxisData)); QPointF textPos; if (m_chartItem->horizontalAxisOnTop()) { @@ -846,10 +846,6 @@ void AbstractSeriesChart::paintGrid(QPainter *painter, QRectF gridRect) const qreal hStep = (gridRect.width() - valuesHMargin - gridOffset.width()) / xAxisSegmentCount; const qreal textPositionHOffset = valuesHMargin * 0.2; - // TODO_ES horizontal axis labels doesn't adapt font - - painter->setFont(adaptValuesFont(hStep-4, painter->font())); - // Vertical axis lines const QTextOption verticalTextOption(Qt::AlignRight); for (int i = 0 ; i < yAxisLineCount ; i++ ) { @@ -956,16 +952,19 @@ QFont AbstractSeriesChart::adaptLabelsFont(QRectF rect, QFont font) return tmpFont; } -QFont AbstractSeriesChart::adaptValuesFont(qreal width, QFont font) +QFont AbstractSeriesChart::adaptFont(qreal width, QFont font, const AxisData &axisData) { - QString strValue = QString::number(maxValue()); QFont tmpFont = font; + const int axisLineCount = axisData.segmentCount() + 1; QScopedPointer fm(new QFontMetricsF(tmpFont)); - qreal curWidth = fm->boundingRect(strValue).width(); - while (curWidth > width && tmpFont.pixelSize() > 1){ - tmpFont.setPixelSize(tmpFont.pixelSize() - 1); - fm.reset(new QFontMetricsF(tmpFont)); - curWidth = fm->boundingRect(strValue).width(); + for (int i = 0 ; i < axisLineCount ; i++) { + QString strValue = axisLabel(i, axisData); + qreal curWidth = fm->boundingRect(strValue).width(); + while (curWidth > width && tmpFont.pixelSize() > 1){ + tmpFont.setPixelSize(tmpFont.pixelSize() - 1); + fm.reset(new QFontMetricsF(tmpFont)); + curWidth = fm->boundingRect(strValue).width(); + } } return tmpFont; } diff --git a/limereport/items/lrchartitem.h b/limereport/items/lrchartitem.h index e524fb2..a2f40a8 100644 --- a/limereport/items/lrchartitem.h +++ b/limereport/items/lrchartitem.h @@ -110,7 +110,7 @@ protected: virtual qreal valuesHMargin(QPainter *painter); virtual qreal valuesVMargin(QPainter *painter); virtual QFont adaptLabelsFont(QRectF rect, QFont font); - virtual QFont adaptValuesFont(qreal width, QFont font); + virtual QFont adaptFont(qreal width, QFont font, const AxisData &axisData); virtual QString axisLabel(int i, const AxisData &axisData); private: