Update font calculation

This commit is contained in:
Emil Sawicki 2022-01-30 17:02:24 +01:00
parent 25dff69679
commit 74c0f52369
3 changed files with 30 additions and 24 deletions

View File

@ -5,12 +5,26 @@ void GridLinesChart::paintChart(QPainter *painter, QRectF chartRect)
{ {
updateMinAndMaxValues(); updateMinAndMaxValues();
const qreal valuesHMargin = this->valuesHMargin(painter);
const qreal valuesVMargin = this->valuesVMargin(painter);
const qreal hPadding = this->hPadding(chartRect); const qreal hPadding = this->hPadding(chartRect);
const qreal vPadding = this->vPadding(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( QRectF calcRect = horizontalLabelsRect(
painter, painter,
chartRect.adjusted( chartRect.adjusted(
@ -20,14 +34,7 @@ void GridLinesChart::paintChart(QPainter *painter, QRectF chartRect)
-vPadding -vPadding
) )
); );
const qreal barsShift = calcRect.height(); gridRect.adjust(0, 0, 0, -calcRect.height());
const qreal topOffset = painter->fontMetrics().height();
QRectF gridRect = chartRect.adjusted(
hPadding,
vPadding + valuesVMargin + topOffset,
-hPadding * 3,
-(vPadding + barsShift)
);
if (!m_chartItem->horizontalAxisOnTop()) { if (!m_chartItem->horizontalAxisOnTop()) {
// Draw labels above the grid // Draw labels above the grid

View File

@ -777,7 +777,7 @@ void AbstractSeriesChart::paintHorizontalGrid(QPainter *painter, QRectF gridRect
painter->setRenderHint(QPainter::Antialiasing,false); painter->setRenderHint(QPainter::Antialiasing,false);
qreal hStep = (gridRect.width() - painter->fontMetrics().boundingRect(QString::number(maxValue())).width()) / segmentCount; 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; QPointF textPos;
if (m_chartItem->horizontalAxisOnTop()) { 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 hStep = (gridRect.width() - valuesHMargin - gridOffset.width()) / xAxisSegmentCount;
const qreal textPositionHOffset = valuesHMargin * 0.2; 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 // 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++ ) {
@ -956,16 +952,19 @@ QFont AbstractSeriesChart::adaptLabelsFont(QRectF rect, QFont font)
return tmpFont; 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; QFont tmpFont = font;
const int axisLineCount = axisData.segmentCount() + 1;
QScopedPointer<QFontMetricsF> fm(new QFontMetricsF(tmpFont)); QScopedPointer<QFontMetricsF> fm(new QFontMetricsF(tmpFont));
qreal curWidth = fm->boundingRect(strValue).width(); for (int i = 0 ; i < axisLineCount ; i++) {
while (curWidth > width && tmpFont.pixelSize() > 1){ QString strValue = axisLabel(i, axisData);
tmpFont.setPixelSize(tmpFont.pixelSize() - 1); qreal curWidth = fm->boundingRect(strValue).width();
fm.reset(new QFontMetricsF(tmpFont)); while (curWidth > width && tmpFont.pixelSize() > 1){
curWidth = fm->boundingRect(strValue).width(); tmpFont.setPixelSize(tmpFont.pixelSize() - 1);
fm.reset(new QFontMetricsF(tmpFont));
curWidth = fm->boundingRect(strValue).width();
}
} }
return tmpFont; return tmpFont;
} }

View File

@ -110,7 +110,7 @@ protected:
virtual qreal valuesHMargin(QPainter *painter); virtual qreal valuesHMargin(QPainter *painter);
virtual qreal valuesVMargin(QPainter *painter); virtual qreal valuesVMargin(QPainter *painter);
virtual QFont adaptLabelsFont(QRectF rect, QFont font); 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); virtual QString axisLabel(int i, const AxisData &axisData);
private: private: