{
// This works backward, from the last item in each data array down to -second- entry
// because we want to shift every element up by one position so we can put a new
// value at position 0
// The "datacount" variable goes from 0 to HISTORY - 1 (the maximum size of the arrays)
for ( int i = dataCount; i > 0; i-- )
{
for ( int c = 0; c < NCpuData; c++ ) // for each data array
{
if ( i < HISTORY )
data[c].data[i] = data[c].data[i-1]; // shift each entry up by one position
}
}
// Put the latest User and System performance values into position 0 of their arrays
// CpuStat::statistic() simply looks up hard-coded values from an array
cpuStat.statistic( data[User].data[0], data[System].data[0] );
// Compute the new Total and Idle performance values from the new User and System values
data[Total].data[0] = data[User].data[0] + data[System].data[0];
data[Idle].data[0] = 100.0 - data[Total].data[0];
// Increase the datacount value unless it has already reached the maximum (HISTORY - 1).
// Once datacount reaches its maximum value, the shifting of the data arrays above will just result
// in the earliest values (at data[User].data[HISTORY - 1], for example) being replaced by the values at
// HISTORY - 2 and therefore just being "scrolled" out of the data arrays.
if ( dataCount < HISTORY )
dataCount++;
// NOW, here's where the time data gets adjusted by adding 1 second to each value in the timeData array
// This results in every time value being shifted forward by 1 second, simulating the movement of time.
// The timeData array was initialized in the CpuPlot constructor by putting HISTORY values into it, starting at
// HISTORY - 1 and going backwards to 0 (that is, timeData[0] = HISTORY - 1 and timeData[HISTORY -1] = 0)
for ( int j = 0; j < HISTORY; j++ )
timeData[j]++;
// and HERE is where the x (time) axis range gets adjusted to reflect the new time range in timeData.
// The axis scale goes backward so that the values for the current time are always the -leftmost- data items.
setAxisScale
( QwtPlot::xBottom, timeData
[HISTORY
- 1], timeData
[0] );
// Finally, the new time and performance data have to be re-set onto the plot. The setRawSamples() method
// tells the plot to use the data from the arrays directly instead of making a copy. (setSamples() makes a copy)
for ( int c = 0; c < NCpuData; c++ )
{
data[c].curve->setRawSamples( timeData, data[c].data, dataCount );
}
// And finally, finally, tell the plot to redraw itself since the x-axis range and the curves have all changed.
replot();
}
void CpuPlot::timerEvent( QTimerEvent * )
{
// This works backward, from the last item in each data array down to -second- entry
// because we want to shift every element up by one position so we can put a new
// value at position 0
// The "datacount" variable goes from 0 to HISTORY - 1 (the maximum size of the arrays)
for ( int i = dataCount; i > 0; i-- )
{
for ( int c = 0; c < NCpuData; c++ ) // for each data array
{
if ( i < HISTORY )
data[c].data[i] = data[c].data[i-1]; // shift each entry up by one position
}
}
// Put the latest User and System performance values into position 0 of their arrays
// CpuStat::statistic() simply looks up hard-coded values from an array
cpuStat.statistic( data[User].data[0], data[System].data[0] );
// Compute the new Total and Idle performance values from the new User and System values
data[Total].data[0] = data[User].data[0] + data[System].data[0];
data[Idle].data[0] = 100.0 - data[Total].data[0];
// Increase the datacount value unless it has already reached the maximum (HISTORY - 1).
// Once datacount reaches its maximum value, the shifting of the data arrays above will just result
// in the earliest values (at data[User].data[HISTORY - 1], for example) being replaced by the values at
// HISTORY - 2 and therefore just being "scrolled" out of the data arrays.
if ( dataCount < HISTORY )
dataCount++;
// NOW, here's where the time data gets adjusted by adding 1 second to each value in the timeData array
// This results in every time value being shifted forward by 1 second, simulating the movement of time.
// The timeData array was initialized in the CpuPlot constructor by putting HISTORY values into it, starting at
// HISTORY - 1 and going backwards to 0 (that is, timeData[0] = HISTORY - 1 and timeData[HISTORY -1] = 0)
for ( int j = 0; j < HISTORY; j++ )
timeData[j]++;
// and HERE is where the x (time) axis range gets adjusted to reflect the new time range in timeData.
// The axis scale goes backward so that the values for the current time are always the -leftmost- data items.
setAxisScale( QwtPlot::xBottom, timeData[HISTORY - 1], timeData[0] );
// Finally, the new time and performance data have to be re-set onto the plot. The setRawSamples() method
// tells the plot to use the data from the arrays directly instead of making a copy. (setSamples() makes a copy)
for ( int c = 0; c < NCpuData; c++ )
{
data[c].curve->setRawSamples( timeData, data[c].data, dataCount );
}
// And finally, finally, tell the plot to redraw itself since the x-axis range and the curves have all changed.
replot();
}
To copy to clipboard, switch view to plain text mode
Bookmarks