<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Truncated labels in Y-axis of QChartView]]></title><description><![CDATA[<p dir="auto">G'day everyone.</p>
<p dir="auto">I have a panel of tens of QChartViews and they are all displayed with the labels in the Y-axis truncated (replaced with ...).  The values range from 0.0 to 2.0.  In order for make them use as much space as possible to be displayed correctly in the restricted space of the window, I do this:</p>
<pre><code>......
            //create a chart object to contain all the data series in the same chart
            QChart *chart = new QChart();
            {
                chart-&gt;legend()-&gt;hide();
               .....
                chart-&gt;layout()-&gt;setContentsMargins(2, 2, 2, 2);
                chart-&gt;setMargins(QMargins(2, 2, 2, 2));
            }
.......
        //-------create the sum chart's axes once-----------------
        QValueAxis *axisY = dynamic_cast&lt;QValueAxis *&gt;( chart-&gt;axisY() );
        if( ! axisY ) {
            axisY = new QValueAxis();
            axisY-&gt;setRange( 0.0, 2.0 );
            axisY-&gt;applyNiceNumbers();
            axisY-&gt;setLabelFormat("%i");
        }
        QValueAxis *axisX = dynamic_cast&lt;QValueAxis *&gt;( chart-&gt;axisX() );
        if( ! axisX ) {
            axisX = new QValueAxis();
            axisX-&gt;setRange( hInitial, hFinal );
            axisX-&gt;applyNiceNumbers();
            axisX-&gt;setLabelFormat("%i");
        }
        //-------------------------------------------------
</code></pre>
<p dir="auto">But only the labels in X-axis are correctly displayed.</p>
<p dir="auto">Here is the result:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/dba449e7-81f5-4427-852a-0b8172cde88f.png" alt="0_1553171182270_transiography_dialog.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Notice the charts in the last column: the labels in X-axis are OK.  But the labels in Y-axis, despite the same layout settings and low range (0.0 - 2.0) are truncated.</p>
<p dir="auto">Any help will be much appreciated.</p>
]]></description><link>https://forum.qt.io/topic/100964/truncated-labels-in-y-axis-of-qchartview</link><generator>RSS for Node</generator><lastBuildDate>Wed, 17 Jun 2026 00:43:12 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/100964.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 21 Mar 2019 12:29:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Truncated labels in Y-axis of QChartView on Wed, 21 Jun 2023 19:50:38 GMT]]></title><description><![CDATA[<p dir="auto">I'm not sure how this happened to the previous two question askers, but for me the solution was quite simple.  Qt works just fine at displaying the plot axis data so long as you:</p>
<ol>
<li>Have enough space to do so</li>
<li>Don't mess with the default settings</li>
</ol>
<p dir="auto">In my case, I had the axis tick count set pretty high which was causing the labels to squish together.  If you leave the tick counts at their default, or let Qt decide, it should work just fine.</p>
]]></description><link>https://forum.qt.io/post/762278</link><guid isPermaLink="true">https://forum.qt.io/post/762278</guid><dc:creator><![CDATA[AlexB89]]></dc:creator><pubDate>Wed, 21 Jun 2023 19:50:38 GMT</pubDate></item><item><title><![CDATA[Reply to Truncated labels in Y-axis of QChartView on Thu, 10 Mar 2022 12:45:34 GMT]]></title><description><![CDATA[<p dir="auto">Hi</p>
<p dir="auto">Eventhough this thread is quite old, I faced a similar issue with this truncated axis labels. I found a workaround which is working for me by removing and adding the axes again to the chart.</p>
<p dir="auto">Here is the example code I use (precondition: axes must be added once to chart and series before):</p>
<pre><code>
    auto axes = chart()-&gt;axes();
    QHash&lt;QAbstractSeries*, QList&lt;QAbstractAxis*&gt;&gt; seriesAxes;
    for (auto serie : series) {
        auto attachedAxes = serie-&gt;attachedAxes();
        for (auto axis : attachedAxes) {
            if (axes.contains(axis)) {
                auto &amp;serieAxes = seriesAxes[serie];
                serieAxes.append(axis);
            }
        }
    }
    for (auto axis : axes) {
        chart()-&gt;removeAxis(axis);
        auto alignment = axis-&gt;alignment();
        chart()-&gt;addAxis(axis, alignment);
    }
    QHashIterator&lt;QAbstractSeries*, QList&lt;QAbstractAxis*&gt;&gt; iSeriesAxes(seriesAxes);
    while (iSeriesAxes.hasNext()) {
        auto serie = iSeriesAxes.next().key();
        auto axes = iSeriesAxes.value();
        for (auto axis : axes) {
            serie-&gt;attachAxis(axis);
        }
    }
</code></pre>
<p dir="auto">Cheers,<br />
Reto</p>
]]></description><link>https://forum.qt.io/post/705900</link><guid isPermaLink="true">https://forum.qt.io/post/705900</guid><dc:creator><![CDATA[Reto]]></dc:creator><pubDate>Thu, 10 Mar 2022 12:45:34 GMT</pubDate></item><item><title><![CDATA[Reply to Truncated labels in Y-axis of QChartView on Sat, 21 Mar 2020 23:44:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/paulocarvalhorj">@<bdi>PauloCarvalhoRJ</bdi></a> Dear Paulo,</p>
<p dir="auto">I am facing a very similar issue. I do not have that many charts to draw, yet too many labels on a Y axis. The QChart are so badly shaped, that it seems it is impossible to easily fix it. A first option, is to change the pixel size of the font used to draw the labels in the resize event.</p>
<pre><code>void yourCharView::resizeEvent(QResizeEvent *event)
{
    if(series.size() &gt; 0) {
        QFont font = this-&gt;axis_y_stack-&gt;labelsFont();
        int pixel_size = event-&gt;size().height() / series.size() / 3;
        font.setPixelSize(pixel_size);
        this-&gt;axis_y-&gt;setLabelsFont(font);
    }

    QChartView::resizeEvent(event);
}
</code></pre>
<p dir="auto">(this-&gt;y_axis beeing your y axis)</p>
<p dir="auto">But it might prove insufficient, as in my case.</p>
<p dir="auto">Labels are truncated in verticalaxis.cpp (for the vertical axes) :</p>
<pre><code>QRectF boundingRect;
// don't truncate empty labels
if (text.isEmpty()) {
       labelItem-&gt;setHtml(text);
 } else {
       qreal labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding());
       QString truncatedText = ChartPresenter::truncatedText(axis()-&gt;labelsFont(), text,
                                                                 axis()-&gt;labelsAngle(),
                                                                 availableSpace,
                                                                 labelHeight, boundingRect);
       labelItem-&gt;setTextWidth(ChartPresenter::textBoundingRect(axis()-&gt;labelsFont(),
                                                                    truncatedText).width());
       labelItem-&gt;setHtml(truncatedText);
}
</code></pre>
<p dir="auto">The truncation code lies here :</p>
<pre><code>QString ChartPresenter::truncatedText(const QFont &amp;font, const QString &amp;text, qreal angle,
                                      qreal maxWidth, qreal maxHeight, QRectF &amp;boundingRect)
{
    QString truncatedString(text);
    boundingRect = textBoundingRect(font, truncatedString, angle);
    if (boundingRect.width() &gt; maxWidth || boundingRect.height() &gt; maxHeight) {
        // It can be assumed that almost any amount of string manipulation is faster
        // than calculating one bounding rectangle, so first prepare a list of truncated strings
        // to try.
        static QRegularExpression truncateMatcher(QStringLiteral("&amp;#?[0-9a-zA-Z]*;$"));
........
        static QLatin1String ellipsis("...");
........
        // Default to "..." if nothing fits
        if (bestIndex == count) {
            boundingRect = textBoundingRect(font, ellipsis, angle);
            truncatedString = ellipsis;
        } else {
            truncatedString = testStrings.at(bestIndex);
        }
    }
    return truncatedString;
}
</code></pre>
<p dir="auto">I can't think of any way to interfere with this and avoid turning my nice labels into three dots...</p>
<p dir="auto">Any input from anyone else ? We can't even change the labelPadding() !!!!</p>
<pre><code> inline qreal labelPadding() const { return qreal(4.0); }
</code></pre>
<p dir="auto">=&gt; I will hide the presenter labels and draw the labels myself, overriding the paintEvent.</p>
<p dir="auto">Cheers</p>
<p dir="auto">Côme</p>
]]></description><link>https://forum.qt.io/post/583933</link><guid isPermaLink="true">https://forum.qt.io/post/583933</guid><dc:creator><![CDATA[Comeylo]]></dc:creator><pubDate>Sat, 21 Mar 2020 23:44:54 GMT</pubDate></item></channel></rss>