Results 1 to 4 of 4

Thread: Real Time Plot & CPUPLOT Example

  1. #1
    Join Date
    Dec 2017
    Posts
    19
    Thanks
    10
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Real Time Plot & CPUPLOT Example

    Hi everyone,
    I want to plot real time data in the form like CPUPLOT example which X axis varies according to time changes and
    the curve is shifted to the left.
    The CPUPLOT example has been decorated by some functions that make it hard a bit for beginners.
    I do not have any idea that how X axis shifted according to time.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Real Time Plot & CPUPLOT Example

    that make it hard a bit for beginners.
    Maybe you shouldn't be trying to run before you have learned how to walk. If you don't know enough C++ or Qt to be able to study an example and understand it, you should probably start with simpler examples and learn from them first.

    I do not have any idea that how X axis shifted according to time.
    The cpuplot example doesn't actually monitor real CPU performance, it is a simulation that uses a set of fake numbers to show a set of simulated real-time curves that get updated by a timer.

    All the real work occurs in the timerEvent() method in cpuplot.cpp.

    In the example, there is a "timeData" array which holds the simulated time values; each time the timer fires, it increases each one of the values in the array by 1 second. Time 0 is the time when the program starts, not the real world time, and each value after that is just one second later.

    Basically, each time the timerEvent occurs, it

    - shifts the performance (y) axis values up by one position,
    - inserts a single new y value at the front of each performance array,
    - increments every time value by 1 to simulate moving forward in time by one second,
    - updates the time axis with the new time range,
    - sets the adjusted data and time array values on their curves
    - and tells the plot to redraw.

    Here is an annotated version of the CpuPlot:: timerEvent() method:

    Qt Code:
    1. void CpuPlot::timerEvent( QTimerEvent * )
    2. {
    3. // This works backward, from the last item in each data array down to -second- entry
    4. // because we want to shift every element up by one position so we can put a new
    5. // value at position 0
    6. // The "datacount" variable goes from 0 to HISTORY - 1 (the maximum size of the arrays)
    7.  
    8. for ( int i = dataCount; i > 0; i-- )
    9. {
    10. for ( int c = 0; c < NCpuData; c++ ) // for each data array
    11. {
    12. if ( i < HISTORY )
    13. data[c].data[i] = data[c].data[i-1]; // shift each entry up by one position
    14. }
    15. }
    16.  
    17. // Put the latest User and System performance values into position 0 of their arrays
    18. // CpuStat::statistic() simply looks up hard-coded values from an array
    19.  
    20. cpuStat.statistic( data[User].data[0], data[System].data[0] );
    21.  
    22. // Compute the new Total and Idle performance values from the new User and System values
    23.  
    24. data[Total].data[0] = data[User].data[0] + data[System].data[0];
    25. data[Idle].data[0] = 100.0 - data[Total].data[0];
    26.  
    27. // Increase the datacount value unless it has already reached the maximum (HISTORY - 1).
    28. // Once datacount reaches its maximum value, the shifting of the data arrays above will just result
    29. // in the earliest values (at data[User].data[HISTORY - 1], for example) being replaced by the values at
    30. // HISTORY - 2 and therefore just being "scrolled" out of the data arrays.
    31.  
    32. if ( dataCount < HISTORY )
    33. dataCount++;
    34.  
    35. // NOW, here's where the time data gets adjusted by adding 1 second to each value in the timeData array
    36. // This results in every time value being shifted forward by 1 second, simulating the movement of time.
    37.  
    38. // The timeData array was initialized in the CpuPlot constructor by putting HISTORY values into it, starting at
    39. // HISTORY - 1 and going backwards to 0 (that is, timeData[0] = HISTORY - 1 and timeData[HISTORY -1] = 0)
    40.  
    41. for ( int j = 0; j < HISTORY; j++ )
    42. timeData[j]++;
    43.  
    44. // and HERE is where the x (time) axis range gets adjusted to reflect the new time range in timeData.
    45. // The axis scale goes backward so that the values for the current time are always the -leftmost- data items.
    46.  
    47. setAxisScale( QwtPlot::xBottom, timeData[HISTORY - 1], timeData[0] );
    48.  
    49. // Finally, the new time and performance data have to be re-set onto the plot. The setRawSamples() method
    50. // tells the plot to use the data from the arrays directly instead of making a copy. (setSamples() makes a copy)
    51.  
    52. for ( int c = 0; c < NCpuData; c++ )
    53. {
    54. data[c].curve->setRawSamples( timeData, data[c].data, dataCount );
    55. }
    56.  
    57. // And finally, finally, tell the plot to redraw itself since the x-axis range and the curves have all changed.
    58.  
    59. replot();
    60. }
    To copy to clipboard, switch view to plain text mode 

    If you still don't understand after this explanation, then the code really is too hard for your current level of knowledge, and you need to start with something more basic.
    Last edited by d_stranz; 5th January 2018 at 21:33.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    CodeFreak (6th January 2018)

  4. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Real Time Plot & CPUPLOT Example

    Quote Originally Posted by d_stranz View Post
    The cpuplot example doesn't actually monitor real CPU performance, it is a simulation.
    Usually it is no simulation - it reads the data from "/proc/stat", what is the API of the kernel to retrieve the real CPU performance !

    Only on rather exotic systems where a proc filesystem doesn't exists ( f.e Windows ) it it falls back on values that have been recorded on my box.

    Uwe

  5. The following user says thank you to Uwe for this useful post:

    CodeFreak (6th January 2018)

  6. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Real Time Plot & CPUPLOT Example

    Usually it is no simulation
    Ah, true if you aren't stuck on Windoze (which I would hardly call "exotic" ). I was just looking at the beginning of the file and the table of hard-coded values.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Real time plot doubts
    By fjld93 in forum Qwt
    Replies: 7
    Last Post: 3rd April 2017, 03:38
  2. How to real time plot?
    By RafaelRSE in forum Qwt
    Replies: 2
    Last Post: 7th August 2014, 18:14
  3. Replies: 3
    Last Post: 12th April 2013, 06:18
  4. real time graph plot
    By robotics in forum Qt Programming
    Replies: 5
    Last Post: 24th May 2011, 05:04
  5. QFileSystemWatcher with a Qwt Real-time plot
    By gen_mass in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2010, 21:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.