Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#
Article

DataPlotter - linear or logarithmic display of 2D data

Rate me:
Please Sign up or sign in to vote.
4.81/5 (50 votes)
12 Oct 2003CPOL4 min read 175.9K   6.8K   106   23
The DataPlotter is a .NET control for graphical display of 2D data. For both the X and Y axis a linear or logarithmic (any base) scaling can be selected.

Image 1

Introduction

The DataPlotter is a .NET control for graphical display of 2D data. For both the X and Y axis a linear or logarithmic (any base) scaling can be selected. Also for both axis the data range can be specified.

Maybe it is of interest to somebody else out there. At least it can be used as another sample on how to fiddle with GDI+ in .NET.

Background

For charting in the .NET world there is still the good old MSChart ActiveX control or you have to buy one of the new .NET controls on the market.

Since neither the MSChart control made me happy for my purpose nor did I want to spend money, I decided to start something on my own. This project is nothing fancy or new -- it's just my first steps in .NET graphics.

Using the code

The implementation of DataPlotter is quite simple: It's a .NET Windows control library with a bunch of properties. Everything else happens in the OnPaint function.

These are the properties:

Color ColorDrawThe color of the data line
Color ColorGridThe color of the gird lines
Color ColorBgThe background color
Color ColorAxisThe color of the axis and text
Font FontAxisThe font for the text
long PenWidthThe width of the data line
DrawModeType drawModeDraw mode for the data points (Line, Dot, Bar)
long BorderTopThe internal border at the top
long BorderLeftThe internal border at the left
long BorderBottomThe internal border at the bottom
long BorderRightThe internal border at the right
double XRangeStartThe start of the data range on the x axis
double XRangeEndThe end of the data range on the x axis
double YRangeStartThe start of the data range on the y axis
double YRangeEndThe end of the data range on the y axis
double XGridThe spacing for the linear grid in x direction. Ignored for log. views
double YGridThe spacing for the linear grid in y direction. Ignored for log. views
long XLogBaseThe base for log. views in x direction. If < 2 then a linear view is displayed
long YLogBaseThe base for log. views in y direction. If < 2 then a linear view is displayed
double[] XDataThe data to be displayed for x.
double[] YDataThe data to be displayed for y.

A few hints on some properties:

  • The borders define the inner axis rectangle. Use bigger borders on the left and bottom to have enough space for the scaling numbers.
  • If a LogBase property is below 2 then a linear scaling is selected. Otherwise the number is the base for logarithmic scaling. Typically use 0 for linear and 10 for logarithmic scales.
  • For logarithmic scaling, use full decades for the RangeStart and RangeEnd properties, e.g. 0.01 to 1000 for a LogBase of 10.
  • The Data arrays must be of the same size of course.

A short walkthrough of the OnPaint function:

  • Check the property values for consistency.
  • Prepare the graphical tools. There are pens for the graph, grid lines and axis and a brush for scale numbering.
  • The inner axis rectangle is calculated by deducting the border properties from the client rectangle size.
  • The grid lines are drawn for X and Y directions. Here also the scale numbers are written.
  • Then the axis rectangle is created.
  • The data arrays are transformed into an array of points. The values from RangeStart to RangeEnd are mapped onto points of the axis grid considering the LogBase scales. Invalid points (detected by exception handler) are skipped by reusing the last valid point.
  • The points are drawn according to the DrawMode.

The DataPlotter is an assembly that can be used as shown in the DataPlotterTest application. This demo has four buttons with sample data for the possible linear/logarithmic scalings. There is also a property grid so see/change the DataPlotter's properties.

Where to go from here

The current implementation of DataPlotter served my purpose quite well. But there are many useful extensions and enhancements I can think of:

  • Use more than one set of data to be drawn in different colors in the same diagram.
  • Add description properties like title, names for the axis etc.
  • Extend draw modes, e.g. dotted or dashed lines etc.

Help yourself and let me know about your own enhancements.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
Occupation: SW engineer since 1981.

Business: Control and automation systems.

History
The 80s: Assembler, PL/M, RMX OS.
The 90s: C, C++, pSOS, VxWorks, Structured Analysis, Windows, networking, project management
98-03: WinNT/2000, COM with C++/ATL, UIs with VB6, design with UML, IP protocols
The new millenium: WinXP, .NET (all versions), C#, WF, WCF, WPF, ...

Comments and Discussions

 
GeneralLicence Pin
Thebigfoot13-May-09 3:51
Thebigfoot13-May-09 3:51 
QuestionThe above code is Excellent. And I have one doubt? Pin
Veerappallil28-May-06 23:08
Veerappallil28-May-06 23:08 
QuestionHOw to Integrate Pin
amitmohanty3-Jul-05 21:24
amitmohanty3-Jul-05 21:24 
GeneralSame Problem Pin
selin172-Feb-05 5:49
sussselin172-Feb-05 5:49 
GeneralNeed help plz Pin
ryanvlasveld13-Dec-04 21:20
ryanvlasveld13-Dec-04 21:20 
GeneralLog scale 0.2 - 2000 Pin
xjli24-Nov-04 12:21
xjli24-Nov-04 12:21 
GeneralRe: Log scale 0.2 - 2000 Pin
Hans-Jürgen Schmidt25-Nov-04 6:43
Hans-Jürgen Schmidt25-Nov-04 6:43 
GeneralAdd marker and Tracker in your control Pin
youssef17-Oct-04 12:32
youssef17-Oct-04 12:32 
QuestionHow to decide if the mouse is over the curve Pin
TaoLi28-May-04 16:50
TaoLi28-May-04 16:50 
GeneralHas anybody already.... Pin
re-en3-May-04 3:29
re-en3-May-04 3:29 
GeneralRe: Has anybody already.... Pin
selin172-Feb-05 4:27
sussselin172-Feb-05 4:27 
GeneralRe: Has anybody already.... Pin
dawo25-Jan-07 1:18
dawo25-Jan-07 1:18 
GeneralMSChart and VB6 Pin
truegret4-Mar-04 17:12
truegret4-Mar-04 17:12 
GeneralRe: MSChart and VB6 Pin
Hans-Jürgen Schmidt14-Mar-04 2:50
Hans-Jürgen Schmidt14-Mar-04 2:50 
GeneralVariable Naming conventions Pin
ChristianD29-Nov-03 11:14
ChristianD29-Nov-03 11:14 
GeneralRe: Variable Naming conventions Pin
dl4gbe2-Mar-05 8:46
dl4gbe2-Mar-05 8:46 
GeneralGood Work Pin
Ark15-Nov-03 0:13
Ark15-Nov-03 0:13 
GeneralRe: Good Work Pin
mahesh kumar s23-Jan-04 1:15
mahesh kumar s23-Jan-04 1:15 
GeneralVery useful control Pin
TRiegler31-Oct-03 3:58
TRiegler31-Oct-03 3:58 
GeneralDouble buffering to remove flicker Pin
Drew Noakes15-Oct-03 0:53
Drew Noakes15-Oct-03 0:53 
GeneralRe: Double buffering to remove flicker Pin
Hans-Jürgen Schmidt15-Oct-03 8:11
Hans-Jürgen Schmidt15-Oct-03 8:11 
GeneralRe: Double buffering to remove flicker Pin
os3omaha15-May-05 7:45
os3omaha15-May-05 7:45 
Generalque Pin
Anonymous14-Oct-03 20:55
Anonymous14-Oct-03 20:55 

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.