Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF Real-time Monitor Control (Rolling Monitor)

0.00/5 (No votes)
25 Apr 2008 2  
An article introducing the WPF Realtime Monitor Control, for creating a real-time view of continuous data

Code version 1.0.0.1

^^^ Monitor Demo

monitor2.jpg

^^^ Rolling Monitor as used in CIRIP (A Computationally Intelligent Railway Intervention Planner) for monitoring connection count and thread count.

Introduction

This article presents the WPF Rolling Monitor aka Realtime Monitor Control. It is used to present real-time continuous data in a simple to use control with automatic updates. The monitor displays data in a similar manor to that of the Microsoft Windows Task Manager > Performance CPU Usage History view.

Using the Code

The monitor is built around two classes, RollingSeries and RollingMonitor. RollingMonitor is the control added to the interface, while RollingSeries represents a continous stream of data.

RollingSeries

The constructor accepts a reference to an instantiated RollingMonitor and a NextValueDelegate. The NextValueDelegate...

public delegate double NextValueDelegate();

... returns the current value of continues trend of data (e.g. current CPU usage).

RollingSeries has the following accessor properties;

  • IsRunning - Pauses the thread reading from the NextValueDelegate. Note that pausing a RollingSeries does not automatically pause all other RollingSeries associated with a specific RollingMonitor.
  • LineBrush - The Brush used for rendering the data on the monitor.
  • LineThickness - The thickness of the rendered line on the monitor.

RollingMonitor

As previously stated, the RollingMonitor class is the UIElement added to the user interface which displays the data of the RollingSeries. It has a number of properties for customising the look and feel of the displayed output;

  • MaxValue - The maximum value the monitor is capable of displaying; any value greater than this will be rendered at the max value level (see demo example).
  • MinValue - The minimum value the monitor is capable of displaying; any value lower than this will be rendered at the min value level.
  • MaintainHistoryCount* - The number of previous history data to maintain; higher values result in higher computational complexity of rendering and greater memory usage, however too low a value and the whole monitor will not be filled.
  • UpdateInterval - The amount of time (in milliseconds) before the next data is retrieved; a higher value slows the speed of the monitor down and hence the MaintainHistoryCount may be lowered resulting in lower computational complexity. Lower values of UpdateInterval results in jerkier looking animation.
  • MinValueSpacing - Used to set the minimum with between UpdateInterval spacing so that data does not become mashed when the control is resized relatively narrow.

*Note in version 1.0.0.1 the parameter of RollingMonitor must be set prior to creating RollingSeries; this will be improved upon in later versions.

Example Code

Window1.xaml

<Window x:Class="WPFRollingMonitorDemonstration.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:rolling="clr-namespace:DNBSoft.WPF.RollingMonitor;assembly=WPFRollingMonitor"
    Title="Rolling Monitor Demonstration" Height="100" Width="300">
    <rolling:RollingMonitor Name="monitor" Margin="5"/>
</Window>

Window1.xaml.cs

namespace WPFRollingMonitorDemonstration
{
    public partial class Window1 : Window
    {
        private Random random = new Random();

        public Window1()
        {
            InitializeComponent();

            RollingSeries s1 = new RollingSeries(monitor, new RollingSeries.NextValueDelegate(NextValue));
            RollingSeries s2 = new RollingSeries(monitor, new RollingSeries.NextValueDelegate(NextValue));
            s2.LineBrush = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            s2.LineThickness = 0.25;
            RollingSeries s3 = new RollingSeries(monitor, new RollingSeries.NextValueDelegate(NextValue));
            s3.LineBrush = new SolidColorBrush(Color.FromRgb(0, 255, 0));
            s3.LineThickness = 0.25;

            monitor.MaxValue = 25;
        }

        private double NextValue()
        {
            return random.Next(0, 100);
        }
    }
}

Three instances of RollingSeries are created, all initialised with the same RollingMonitor and a delegate returning a random number between 0 and 100. Note however that the maximum value for the rolling monitor is set to 25, so any value above 25 appears as a value of 25 (see above).

Todo

  • Allow modification of RollingMonitor parameters after RollingSeries have been added.
  • Monitor data series customisation via user interface.
  • Performance optimisation.
  • Automatic scaling.
  • Axis labels.

History

Version 1.0.0.0 - Initial build

Additional Licensing Notes

Please feel free to use this in your work, however please be aware that a modified The Code Project Open License (CPOL) is in use; basically it is the same as the standard license except that this code must not be used for commercial or not-for-profit commercial use without prior authorisation. Please see license.txt or license.pdf in the included source and demo files.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here