Click here to Skip to main content
16,014,631 members
Articles / Programming Languages / C#
Article

Realtime Chart and Graph in One

Rate me:
Please Sign up or sign in to vote.
4.59/5 (11 votes)
16 May 2007 154.1K   10.4K   99   27
A basic Chart and Graph control.

Sample Image - ChartGraph.png

Introduction

I worked on a project in which I needed a graph control. Later on, I was also required to use a chart control. So I thought, hey, let's just combine them both into one nice and simple control.

Using the code

This control is very simple to use. First, we must create the control and initialize it.

C#
//Creating the control
private ChartControl.ChartControlForm chartControlForm1;
chartControlForm1 = new ChartControl.ChartControlForm();
//Initializing the Control
chartControlForm1.InitChart();

Now we can start adding values.

C#
//Adding Values to the control
float ValueAdd;
ValueAdd = 5.3;
chartControlForm1.AddValue(ValueAdd);

As we add the values, the control automatically redraws itself, so there is no need to call any function.

The variable in charge of the Graph/Chart mode is OpenType. Please note that its default value is ChartControlOpenType.Bar.

Change to Graph mode:

C#
chartControlForm1.OpenType = ChartControl.ChartControlOpenType.Graph;

Change to Chart mode:

C#
chartControlForm1.OpenType = ChartControl.ChartControlOpenType.Bar;

Additional Features

Loading from an array/list can easily be done by using the overloaded function LoadFromValues (may receive a float Array or an ArrayList).

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


Written By
Israel Israel
I've been into programming for the last 5 years, doing all sort of stuff, from the old dos borland c++ to C#.NET.

Comments and Discussions

 
QuestionReal time data from serial port Pin
Member 128684421-Dec-16 1:22
Member 128684421-Dec-16 1:22 
QuestionThe plot do not appear Pin
Member 120466279-Oct-15 7:30
Member 120466279-Oct-15 7:30 
QuestionCharting tools Pin
Member 1027882916-Sep-13 21:28
Member 1027882916-Sep-13 21:28 
QuestionMarco Pin
Marco Antonio Rios Ramirez23-Nov-12 17:19
Marco Antonio Rios Ramirez23-Nov-12 17:19 
QuestionReset chart data Pin
Member 87076244-Apr-12 4:28
Member 87076244-Apr-12 4:28 
QuestionRealTime graph Pin
cicekbilal20023-Feb-12 22:51
cicekbilal20023-Feb-12 22:51 
AnswerRe: RealTime graph Pin
Mazen el Senih6-Feb-12 7:46
professionalMazen el Senih6-Feb-12 7:46 
Generalreply! Pin
luongvn26-Feb-11 2:35
luongvn26-Feb-11 2:35 
GeneralRe: reply! Pin
Roy Reznik14-May-11 7:54
Roy Reznik14-May-11 7:54 
GeneralWeb Version Pin
girishmeena21-Sep-10 0:12
girishmeena21-Sep-10 0:12 
QuestionHow could i adjust with all values when array exceed range of values? Pin
huynhdangthai27-Jul-10 23:52
huynhdangthai27-Jul-10 23:52 
QuestionI got some problem with it, help please.. Pin
rpurnama15-Feb-10 18:41
rpurnama15-Feb-10 18:41 
Generalcannot run... Pin
yanadi30-May-09 6:02
yanadi30-May-09 6:02 
AnswerRe: cannot run... Pin
Roy Reznik26-Jun-09 3:04
Roy Reznik26-Jun-09 3:04 
GeneralMy Solution for the exception Pin
rcn2118-May-09 16:10
rcn2118-May-09 16:10 
GeneralRe: My Solution for the exception Pin
Roy Reznik26-Jun-09 3:10
Roy Reznik26-Jun-09 3:10 
GeneralRe: My Solution for the exception Pin
NeoDimension5-Aug-09 2:15
NeoDimension5-Aug-09 2:15 
GeneralRe: My Solution for the exception Pin
astramiki23-Dec-17 11:19
astramiki23-Dec-17 11:19 
GeneralChange bar width Pin
elcharro10-Aug-07 5:59
elcharro10-Aug-07 5:59 
GeneralImplementing realtime graph [modified] Pin
jamezun18-Jun-07 21:30
jamezun18-Jun-07 21:30 
GeneralThanks for the update! Pin
Marc Leger19-May-07 8:51
Marc Leger19-May-07 8:51 
QuestionLoad Array Pin
Gwannoes10-May-07 23:04
Gwannoes10-May-07 23:04 
AnswerRe: Load Array Pin
Roy Reznik16-May-07 10:10
Roy Reznik16-May-07 10:10 
Generaltest application issues and chart control issues Pin
ToothRobber10-Nov-06 10:09
professionalToothRobber10-Nov-06 10:09 
Hello Roy

I had problems with you test applicaiton and its solution and had to build my own. To test your application I created an applicaiton that would generated random data and pass it to you control using a background thread.

If you would like my test application please drop me a message.

I found that your control generates an exception after a minute or two of running on the random number generator.


----
if (Values[i] < 0) // Less than 0, UnderColor is used
{
if (IntCmp(Values[i], Values[i - 1]) < 0) //if it goes above 0

----
The Error

{"Index was outside the bounds of the array."}
" at ChartControl.ChartControlForm.DrawChart() in C:\\code\\NET\\Controls\\ChartControl\\ChartControlForm.cs:line 283\r\n at ChartControl.ChartControlForm.AddValue(Single val) in C:\\code\\NET\\Controls\\ChartControl\\ChartControlForm.cs:line 147\r\n at ChartTrialProgram.frmMain.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in C:\\code\\NET\\Controls\\ChartControl\\ChartTrialProgram\\frmMain.cs:line 66\r\n at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)\r\n at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)"

To fix this I would use a circular buffer for you data values this would keep the data list to a prodictable size.

Also the background (green and black grid) moves and I think your control work look better if you could stop this.

Robert
GeneralRe: test application issues and chart control issues Pin
Roy Reznik10-Nov-06 10:19
Roy Reznik10-Nov-06 10:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.