Click here to Skip to main content
Click here to Skip to main content

Cristi Potlog's Chart Control for .NET

By , 24 Jul 2005
 

ChartControl Sample Image.

Introduction

There are a few samples of chart controls out there and a myriad of commercial chart controls. It seems that there is a large market and a strong need for such controls.

I will introduce here a sample chart control just for the sake of making an example. I can't guarantee, though, that it would fit any real world application requirements.

Background

The sample chart uses basic .NET drawing libraries, and there are no third party components. I will illustrate first a basic time-series line chart.

Using the code

To use the control, you need to add a reference to the compiled ChartControl.dll or you can use the source directly by adding the library to your solution. This should provide more flexibility and you can also change the code to fit your particular needs.

The control should appear on the toolbox now (the sample images assume that you are using source code reference):

ChartControl on the toolbox.

You can create a new form on your application project and place a chart control on the form. Set the Dock property to DockStyle.Fill and you will have something like this:

ChartControl with no settings and data.

You can start making changes to the default chart's properties to improve the visual effects of the chart:

ChartControl properties settings.

For a complete list of the settings, you can view the sample project. Then you can have something like what you see in the image at the top of the article.

Chart control details

The chart control is designed from a series of helper classes that hold specific information about how the control should be displayed. You'll notice that these classes are nested within each other so we can have a coherent model for the chart's components.

ChartTitleSettings class

Represents the display setting for a title of the chart control. Through its properties you can change the appearance of the title, both color and font, and also the text that will be displayed.

This class is used to represent the title of the chart, the title of the axis of the chart, and the title of the series.

ChartLabelsSettings class

Represents the display setting for labels of the chart control. The difference between a label and a title is that in a label you cannot control the text that is displayed. A label has, also, the possibility to rotate the text that it displays. You can choose not to display the label by setting the Visible property to False. You can obtain the same effect on a title by setting its title to an empty string.

This class is used to represent the labels that appear next to the chart's series values and the chart's axis pointers.

ChartLineSettings class

Represents the display setting for a line of the chart control. Through its properties you can set the color, weight and dash style of the line, as well as the fact that the line is visible or not.

This class is used to represent the lines of the chart's grid, axis, series and values projections, as well as the border lines of the chart's legend.

ChartGridSettings class

Represents the display setting for the grid of the chart control. You can set the settings of the grid line through a property of type ChartLineSettings. You can also specify the density of the grid by setting the XAxisStyle and YAxisStyle properties.

ChartMarkSettings class

Represents the display setting for a value mark of the chart control. You can change the appearance of the marks of the chart series value by setting fill color and border color and by selecting the shape of the mark from a list of predefined values:

/// <summary>
/// Specifies the shapes of the value series marks for the chart control.
/// </summary>
public enum ChartMarkShapes
{
  None,
  Circle,
  Diamond,
  Square,
  Triangle
}

ChartProjectionsSettings class

Represents the display setting for the projections of the chart control. A projection is a line drawn from the value mark to the time axis to pin point the exact time frame of the value. It consists of a line and a label close to the axis that indicates the time.

You can control the appearance of the line and of the label through properties of type ChartLineSettings and ChartLabelSettings. You can specify if the projection should be displayed or not by setting the boolean Visible property.

ChartSeriesSettings class

Represents the setting for the time series of the chart control. This is a complex class compared with previous ones. You can control the settings of the series including the values to be displayed.

ChartLegendSettings class

Represents the display setting for the legend of the chart control. Through this class' properties, you can specify whether the legend will appear or not, the position at which it will be displayed, and the appearance of the border line of the legend.

Possible values for Position are:

/// <summary>
/// Specifies the position at which the legend will be displayed on the chart control.
/// </summary>
public enum ChartMarkShapes
{
  Top,
  Left,
  Right,
  Bottom
}

ChartTimeScaleSettings class

Represents the setting for the time axis scale of the chart control. It provides properties to control the limits of the time scale.

ChartValueScaleSettings class

Represents the setting for the values axis scale of the chart control. It provides properties to control the limits of the value scale.

ChartTimeAxisSettings and ChartValueAxisSettings class

ChartTimeAxisSettings represents the display setting for the time axis of the chart control.

ChartValueAxisSettings represents the display setting for the value axis of the chart control.

Both these classes are derived from ChartAxisSettingsBase abstract class which provides the basic elements of the axis such as title, label, line and tick styles. Each class adds a Scale property of ChartTimeScaleSettings type and ChartValueScaleSettings type, respectively.

ChartSeriesValue and ChartSeriesValueCollection classes

ChartSeriesValue represents a value from the time series of the chart control. This class has two properties Date and Value that specify the data that should appear on the chart representing a certain value at a specific point in time.

ChartSeriesValueCollection represents a collection of values from the time series of the chart control.

Implementation notes class

This is to be considered work in progress. This is not the final nor the complete version of the control. I released the code so that I can get some feedback from you in order to add more features to it as well as to refine the design, especially the event model design.

Future directions

  • Multiple series support.
  • Designer support through custom editors.
  • More chart types support (e.g., bar chart, pie chart).

History

  • July 11th 2005. Initial release.
  • July 22nd 2005. Article contents update.

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

About the Author

Cristi Potlog
Software Developer
Romania Romania
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAdding a Third Y AxismemberMember 31521618 Mar '12 - 5:50 
Can you add a third Y Axis to the right of the Y2 Axis?
I have three plots with different Y values against a common X Axis.
GeneralNew VersionmemberAkramany29 Jul '09 - 2:50 
Hi Cristi,
 
Your chart realy is very great, i have upgraded it by adding the following:
 
1. 3 Labels
2. 3 Legends
3. 3 Lines
4. 3 Series
 
http://196.219.66.58/stat2.png[^]
 
http://196.219.66.58/chartcontrol.rar[^]
----------------------------------------------------------------
Akram Sabry Mohammed
GIS Manager
GEOService Group
2 Taher EL Tanahi st.-Heliopolise-Cairo-Egypt
Tel: +202-22402624
Fax: +202-26441650
Email : akram@gsg-eg.com
Website:http://www.gsg-eg.com
Generalnice effort but...membercinamon14 Jun '09 - 11:15 
if you're looking for something that can handle a serious project zedgraph is the way,
this chart control is too limited and too simple
GeneralChartTimeUnitsmemberOmar.Pessoa27 Apr '09 - 10:34 
Hi... ChartTimeUnits does not make anything?
 
I like to set last month day, to Scale.
 
But I dont know how to do it.
 
I try set this code at your sample:
 
this.chart1.XAxis.Scale.BaseUnit = ChartTimeUnits.Months;
 
but this does not work...
 
Thanks. Pretty cool!
 
Omar - VC++ Programmer

GeneralNew versionmemberRovira25 Sep '07 - 22:42 
Hi!
 
I would like to know if is there a new version of this chart?
 
BR,
 
Yoniel
GeneralRe: New versionmemberCristi Potlog7 Oct '07 - 11:32 
No, sorry.
 
--
Cristi Potlog

GeneralRe: New versionmemberdchris_med12 Feb '09 - 21:15 
tu lucrii la SIUI nu? Smile | :)
AnswerRe: New versionmemberCristi Potlog13 Feb '09 - 22:16 
Esti putin off-topic, dar raspunsul e da.
 
--
Cristi Potlog

QuestionDouble Values on X-Axis?memberTitty16 Mar '07 - 0:25 
First of all, great job Cool | :cool: ...
Now i have a question: Confused | :confused:
how can i set double values to x-axis instead of data values?
 
Thank you and best regards.
Have a nice day! Smile | :)
 
Titty Cool | :cool:
GeneralAnother free chartmemberZvikabh6 Feb '07 - 4:06 
Also check out this free version of a commercial chart control. Seems pretty good to me.
 
-- Zvika

GeneralJFreeChartmemberpescheH25 Jan '07 - 22:47 
I was desperately looking for something like a JFreeChart library - which I experienced very successfully - in the .NET world. Unfortunately there seems to be nothing but commercial Dundas and the like, so I will have a look at your Release.
 
Since you ask for feedback, just have look at the free project http://www.jfree.org/ to get any ideas about charting...

Questionis it possible?membersam8sam23 Nov '06 - 0:14 
is it possible to change the Y-axis values to display text. lets say a point is ("text1",date).
QuestionHow to make a curvememberBugsaway9 Aug '06 - 10:51 
Hi,
 
I made the following code but nothing shows up in the chart. Whats wrong?
 
CristiPotlog.ChartControl.ChartSeriesValue cv = new CristiPotlog.ChartControl.ChartSeriesValue();
 
chart1.Name = "Temp log";
chart1.TabIndex = 0;
chart1.XAxis.Scale.Maximum = (DateTime)arLog.GetValue(0, 0);
chart1.XAxis.Scale.Minimum = (DateTime)arLog.GetValue(0, cntLog - 1);
 
for (int i = 0; i < cntLog ; i++)
{
cv.Date = (DateTime)arLog.GetValue(0, i);
cv.Value = Convert.ToDecimal(arLog.GetValue(1, i));
chart1.Series.Values.Add(cv);
}
chart1.Update();

 
thanks HFJ.

AnswerRe: How to make a curvememberCristi Potlog14 Aug '06 - 10:30 
First of all, are you sure that the first item in the arLog vector is the smallest value and the last is the largest?
If so, please send me a sample of the values in the vaector to experiment with those values.
 
--
Cristi Potlog

GeneralGreat Chart but...memberAndyc7616 Mar '06 - 1:09 
..if i'm not wrong I can visualize only one line of datas..a good improvement would be make possible to view multiple lines as MsChart does..
 
However good work!
 
Bye
 
Andy
 
-- modified at 7:09 Thursday 16th March, 2006
AnswerRe: Great Chart but...memberCristi Potlog16 Mar '06 - 3:07 
As you can see from the Future Directions sections my first priority will be Multiple series support.
The thing is though, that I didn't worked on this chart for quite a while now.
 
I hope I will come with a new version for .NET framework by the end of this month.
And support for multiple series too... Smile | :)
 
Greetings,
 
--
Cristi Potlog
GeneralRe: Great Chart but...memberDave Lenz15 May '06 - 17:46 
Could you kindly upload your new version if its done!
 
Thank you.
Generaltime scale / multiple seriesmemberCole Cranford31 Jan '06 - 12:29 
Excellent chart, very impressive.
 
Two questions about additional capabilities:
- Can seconds, minutes, and hours be used as baseunits for the x-axis time scale? I am needing to focus on datasets spanning a period measured in seconds.
- Have you implemented a multiple series capability? It would be nice to directly compare multiple series on the same chart.
 
Thanks.
 
Cole Cranford
GeneralSelect parts of chart with mousememberwizdd10 Jan '06 - 5:52 
How would one go about adding doubleclick events to the chart elements to change the appearance? As an example, a double click on the marker in the legend would bring up a menu to select another marker (other shape, color, etc) and then the chart would need to be redrawn with the new selection. I am particularly struggling with the events part of this and understanding on how to find out what part of the chart is clicked on. Any help or hints are appreciated.
Thanks,
-Rene
AnswerRe: Select parts of chart with mousememberCristi Potlog10 Jan '06 - 21:17 
That would be a trick indeed.
The control is just a sample. It wiil need a lot of work to become the real thing.
I consider giving it a second look someday and even updrading it.
A feature list would be nice too.
 
--
Cristi Potlog
 
-- modified at 9:08 Thursday 16th March, 2006
General"Measure" eventsmemberwizdd10 Jan '06 - 5:45 
What is the purpose of the "measure" events just before each element of the chart is drawn?
 
Thanks,
-Rene

GeneralRe: "Measure" eventsmemberCristi Potlog10 Jan '06 - 21:12 
No. The Measure events are intended for custom drawing.
That is for changing the size or color of the items to be drawn.
 
--
Cristi Potlog
GeneralIs the hourmembernegralina525 Aug '05 - 22:02 

With a Major Unit of 1 on X axis:
If you have Series Date 1/01 23:00 the value will be displayed at 1/01 but if the value is 1/01 1:00 the value will be displayed at 31/12 last year.
GeneralImpovementmembernegralina525 Aug '05 - 21:22 
I found the code very useful but I found something and you should be aware of this.
If you modify the MajorUnit on X axis(time) the series values will not match the time axis.
The current step for X axis is 7.Please see the values for a step of 1.
I think some code must be inserted in Chart.OnDrawChartLines(...).


AnswerRe: ImpovementmemberCristi Potlog28 Aug '05 - 21:11 
Thanx.
I will look into it.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Jul 2005
Article Copyright 2005 by Cristi Potlog
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid