This may have been answered before and if so I'm sorry, but I haven't found anything in my searches. I am trying to display a chart with a moving 'window' of values similar to Task Manager's display of CPU and Memory usage. The DataSource of my chart is being constantly updated and I want to only display the last 60 values added to the DataSource. Any help of this would be greatly appreciated.
There are, of course, other ways to achieve the window movement. For example David was explaining you could accomplish something by hard coding the range of the x axis, and then you would just need to make sure the "current" entries were the only ones to fall in that range and be displayed. If you can provide more information about the format of your data source and how you are obtaining it for this updating display, we could further advise.
I was hoping there would be a cleaner solution than that. I need to keep a reference of all the values and just show the most recent entries. I guess I will need to maintain a separate DataTable in addition to the DataSource to achieve this?
Here is some sample code behind:
public partial class Form1 : Form
{
private Timer _timer;
private DataTable _data;
private Random _rand = new Random();
public Form1()
InitializeComponent();
_timer = new Timer();
_timer.Interval = 1000;
_timer.Tick += new EventHandler(timer_Tick);
_timer.Enabled = true;
_data = new DataTable();
_data.Columns.Add(new DataColumn("Timestamp", typeof(DateTime)));
_data.Columns.Add(new DataColumn("Value", typeof(double)));
for (int i = 0; i < 60; i++)
AddNewListItem();
}
ultraChart1.DataSource = _data;
ultraChart1.Data.SwapRowsAndColumns = true;
ultraChart1.DataBind();
void AddNewListItem()
DataRow dr = _data.NewRow();
dr["Timestamp"] = DateTime.Now;
dr["Value"] = _rand.NextDouble() * 100.00;
_data.Rows.Add(dr);
if (_data.Rows.Count > 60)
_data.Rows.RemoveAt(0);
void timer_Tick(object sender, EventArgs e)
-Graham
I recommend removing the first point from the chart's datasource each time a point is added.
it is also possible to achieve this by setting
theChart.Axis.X.RangeType = AxisRangeType.Auto
theChart.Axis.X.RangeMin = 0
theChart.Axis.X.RangeMax = 60