Update font size if there is not enough space

This commit is contained in:
Emil Sawicki 2022-03-05 17:36:37 +01:00
parent 2ce293f85d
commit 47ba3d0b4f

View File

@ -677,14 +677,27 @@ void AbstractChart::prepareLegendToPaint(QRectF &legendRect, QPainter *painter)
case ChartItem::LegendAlignBottomLeft: case ChartItem::LegendAlignBottomLeft:
case ChartItem::LegendAlignBottomCenter: case ChartItem::LegendAlignBottomCenter:
case ChartItem::LegendAlignBottomRight: { case ChartItem::LegendAlignBottomRight: {
// TODO_ES handle resize horizontal legend (wrapping text) const qreal maxWidth = legendRect.width() * 0.9;
qreal legendWidth = std::accumulate(m_legendColumnWidths.cbegin(), m_legendColumnWidths.cend(), 0.0);
if (legendWidth < maxWidth) {
return;
}
while ( (legendWidth > maxWidth) && tmpFont.pixelSize() > 1) {
tmpFont.setPixelSize(tmpFont.pixelSize() - 1);
calcChartLegendSize(tmpFont, legendRect.width());
legendWidth = std::accumulate(m_legendColumnWidths.cbegin(), m_legendColumnWidths.cend(), 0.0);
}
painter->setFont(tmpFont);
legendRect = calcChartLegendRect(tmpFont, legendRect, true, 0, 0);
break; break;
} }
case ChartItem::LegendAlignRightTop: case ChartItem::LegendAlignRightTop:
case ChartItem::LegendAlignRightCenter: case ChartItem::LegendAlignRightCenter:
case ChartItem::LegendAlignRightBottom: case ChartItem::LegendAlignRightBottom:
QSizeF legendSize = calcChartLegendSize(tmpFont, legendRect.width()); QSizeF legendSize = calcChartLegendSize(tmpFont, legendRect.width());
if ((legendSize.height() > legendRect.height() || legendSize.width() > legendRect.width())) { if ((legendSize.height() <= legendRect.height() && legendSize.width() <= legendRect.width())) {
return;
}
while ((legendSize.height() > legendRect.height() || legendSize.width() > legendRect.width()) while ((legendSize.height() > legendRect.height() || legendSize.width() > legendRect.width())
&& tmpFont.pixelSize() > 1) && tmpFont.pixelSize() > 1)
{ {
@ -693,7 +706,6 @@ void AbstractChart::prepareLegendToPaint(QRectF &legendRect, QPainter *painter)
} }
painter->setFont(tmpFont); painter->setFont(tmpFont);
legendRect = calcChartLegendRect(tmpFont, legendRect, true, 0, 0); legendRect = calcChartLegendRect(tmpFont, legendRect, true, 0, 0);
}
break; break;
} }
} }
@ -1203,6 +1215,7 @@ QVector<qreal> AbstractChart::legendColumnWidths() const
void AbstractBarChart::paintChartLegend(QPainter *painter, QRectF legendRect) void AbstractBarChart::paintChartLegend(QPainter *painter, QRectF legendRect)
{ {
prepareLegendToPaint(legendRect, painter); prepareLegendToPaint(legendRect, painter);
// TODO_ES after calculating bottom legend size, handle difference of sizes
painter->setPen(Qt::black); painter->setPen(Qt::black);
painter->setRenderHint(QPainter::Antialiasing,false); painter->setRenderHint(QPainter::Antialiasing,false);
if (m_chartItem->drawLegendBorder()) if (m_chartItem->drawLegendBorder())