Congratulations to our 2022 Qt Champions!

Draw a moving graph

  • Hello, I would like to draw a moving graph where the graph changes every second for a day period of time.

    I calculate the data to plot the graph from some mathematics equations and keep them in a vector. One parameter used for calculation is time which is extracted from computer time. So for 1 sec, I get something like 300 points.

    One solution I can think of is I collect the data into multidimensional vector where row is time and column is data. Then read each row every second and use paintevent to draw the graph.

    Are there any better methods?

    Or how can I create multidimentional vector? I can get one vector, no problem but how can I get them pile up.

    Until now I can draw the graph. I have a problem with updating them each second.

    Thank you.

  • Lifetime Qt Champion

    Hi and welcome
    What kind of graph is that?
    Have you considered using a plot widget ?
    to get zooming etc for free?

    I wonder instead of multi vector then a data class?

    class GData {
    std:.vector<double> data;

    std:.vector<GData> GraphsUpdates;

    Then you could store more.

    But im not 100% sure why you need to pile them up ?
    If you have 1 sec to draw the points, should be fine or ?

  • @mrjj It's a day and night terminator map. I have an equation to calculate the graph and can plot it on a cylindrical map projection of the world. The graph has 360 points width. And since the terminator depends on hours of the day and days of the year, I would like to update it every second (though we won't see much different from the previous second :)).

    Thank you for your suggestion. I will take a look on those plot widgets and class.

    But im not 100% sure why you need to pile them up ?

    I store my 360 points xy into QVector and use QPainter to draw it. I was thinking of triggering every row each second so that the graph would update as said.

  • So I figured out how to do it.
    First I calculate the graph using the date and time provided by QDateTime. The 360 points xy are kept in vectorA. I copy vectorA into vectorB and use vectorB to draw the graph using QPainter and QPainterPath to fill the upper area.

    Thank you.

Log in to reply