Important: Please read the Qt Code of Conduct -

QChart QLegend handle jitter due to rapid updates

  • Hi,
    I'm using QChart for plotting a graph, and I have a QLineSeries for each value I'm plotting. I constantly fetch the values from an arduino connected to a sensor and add the values to the chart. I'm setting the "name" of the line series to the latest value:


    so the legend updates every time to newVal. The problem I'm facing is that I get the values too rapidly, so there's a lot of jitter because the QLegend on the chart keeps resizing to the preferred value. I want the legend to only expand if the preferred size is more than the current size, so I tried setting the horizontal size policy to QSizePolicy::MinimumExpanding like so:


    but that doesn't do the trick unfortunately :(.

    Any help would be highly appreciated. If you need a demonstration, I have a short video clip that I tried uploading, but currently I "don't have enough permissions for it", considering this is my first forum post. If you need to see it, I can send it to you by email.

    Thanks a lot!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since the values are coming in to rapidly to be really useful, why not slow down the update of the name ? There's no use updating it a hundred time per second.

  • Yeah, that would be one solution, and I might end up going with it if nothing else works out. My only concern is that the number and the chart would be out of sync and it would be misleading. But is there a way to achieve what I'm trying to do right now?

  • Lifetime Qt Champion

    I dont know QChart very well but i was wondering if
    you detach the legends() and controlled the sizing if that would help ?

  • @Ambyjkl Short story: I implemented my own legend and hide the QtCharts legend for many reasons.

    I needed to maximize the chart area and wanted to position the legend as a completely disconnected object. That is powered by a ListModel, which has a QML var (I pass a name, series ref. and a real value placeholder). In as realtime as you can get - I can see the last series value drawn and as a number, change colors, series selections... it's very possible.

    I share the others' concerns you're updating too fast for no visible benefit - there's no reason to update so fast due to our eyes not seeing. If there's business logic needing action at high sample rates keep it in c++.

    There's other ways than blunt force, are you values frequently the same across those fast updates?
    I have the potential to do chart updates around 14ms (tested) but my data baud in the real world is nowhere near. Even so, I only signal to change on a value that actually does change.

    Identical values do not propogate in my application, an component initializing can do a one off value read, then bind to the property changed signals or just wait for the next update.

    I also predetermine the maximum legend and internal item sizes on loading and restrict text to the containers not let the content go nuts like that. I know the longest series names and the maximum values to be written so I know max char lengths, I know the legend component dimensions, the rest is layouts and text metrics fun.

  • @6thC cool story, thanks for the detailed reply! I've realized that QLegend isn't the best solution for me, so I ended up doing something similar to what you did. I'm using labels with a monospaced font and splitting the whole and fractional parts, so it's a lot nicer now