From 108f6fed2f383be32808a1eb5a6ac82d078d6330 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 11 Jan 2021 13:29:15 +0100 Subject: [PATCH] statistics: print ellipsis in case of too little space In categorical axes all labels were printed leading to a big tohu wa-bohu for two many bins. Therefore, if a label is larger than the space between two ticks, replace by an ellipsis. Adjust the size of the ellipsis (".", ".." or "...") to the available space. Signed-off-by: Berthold Stoeger --- stats/statsaxis.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/stats/statsaxis.cpp b/stats/statsaxis.cpp index d31b5827b..2c5a5d961 100644 --- a/stats/statsaxis.cpp +++ b/stats/statsaxis.cpp @@ -365,9 +365,33 @@ std::pair CategoryAxis::getFirstLastLabel() const return { QString(), QString() }; } +// Get ellipses for a given label size +static QString ellipsis1 = QStringLiteral("․"); +static QString ellipsis2 = QStringLiteral("‥"); +static QString ellipsis3 = QStringLiteral("…"); +static QString getEllipsis(const QFontMetrics &fm, double widthIn) +{ + int width = static_cast(floor(widthIn)); + if (fm.size(Qt::TextSingleLine, ellipsis3).width() < width) + return ellipsis3; + if (fm.size(Qt::TextSingleLine, ellipsis2).width() < width) + return ellipsis2; + if (fm.size(Qt::TextSingleLine, ellipsis1).width() < width) + return ellipsis1; + return QString(); +} + void CategoryAxis::updateLabels() { - // TODO: paint ellipses if space too small + if (labelsText.empty()) + return; + + QFontMetrics fm(labelFont); + double size_per_label = size / static_cast(labelsText.size()) - axisTickWidth; + double fontHeight = fm.height(); + + QString ellipsis = horizontal ? getEllipsis(fm, size_per_label) : QString(); + labels.clear(); ticks.clear(); labels.reserve(labelsText.size()); @@ -375,7 +399,13 @@ void CategoryAxis::updateLabels() double pos = 0.0; addTick(-0.5); for (const QString &s: labelsText) { - addLabel(s, pos); + if (horizontal) { + double width = static_cast(fm.size(Qt::TextSingleLine, s).width()); + addLabel(width < size_per_label ? s : ellipsis, pos); + } else { + if (fontHeight < size_per_label) + addLabel(s, pos); + } addTick(pos + 0.5); pos += 1.0; }