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();
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

View File

@ -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<QFontMetricsF> 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;
}

View File

@ -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: