ARTICLE

A variety of Chart Engines using GDI+

Posted by Daniel Stefanescu Articles | Visual Basic 2010 June 18, 2003
The attached source code library is a Chart engine, which is written using GDI+ and VB.NET.
Download Files:
 
Reader Level:

The original code came from Scott Guthrie's chart engine example. The differences from original code are:

  • Some of chart's attributes are exposed as properties.

    • ChartGenerator.ChartLine.Color
    • ChartGenerator.ChartLine.Style
    • ChartGenerator.ChartLine.Width

 

  • The above are used for setting the properties of each line from chart.

    • ChartGenerator.Engine.ImageBackgroundColor
    • ChartGenerator.Engine.ImageHeight
    • ChartGenerator.Engine.ImageWidth
    • ChartGenerator.Engine.IsScaled - determines when the chart will be scaled or not.

 

  • The minimum and maximum points are calculated inside using Array methods. We copied the Y values of each line into an array, sorted the array and using the first and the last member of array determined the maximum and the minimum.

Testing code: 

.....
Imports ChartEngine.ChartGenerator
.....
'initing the chart's data
Dim myChartData As New ChartData
Dim XValues As Long() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
myChartData.XValues = XValues
'line 1
Dim Line1 As New ChartLine
Dim YValues1 As Long() = {1280, 1280, 1280, 1270, 1270, 1280, 1270, 1260, 1260}
Line1.ChartLine.LinesValues = YValues1
myChartData.Lines.Add(Line1)
'line 2
Dim Line2 As New ChartLine
Dim YValues2 As Long() = {1200, 1180, 1296, 1370, 1257, 1120, 1270, 1230, 1290}
Line2.ChartLine.LinesValues = YValues2
Line2.Color = Color.Yellow
Line2.Width = 2
Line2.Style = Drawing.Drawing2D.DashStyle.Dash
myChartData.Lines.Add(Line2)
'calling the engine
Dim oEngine As New ChartEngine.ChartGenerator.Engine
oEngine.ImageHeight = 200
oEngine.ImageWidth = 300
oEngine.IsScaled =
True
oEngine.ImageBackgroundColor = color.LightGray
' Render BitMap Stream Back To Client
Dim StockBitMap As Bitmap = oEngine.DrawChart(myChartData)
StockBitMap.Save(Response.OutputStream, ImageFormat.JPEG)
.....

and the testing code for C# version of engine: 

.....
using ChartGenerator; 
.....
ChartData myChartData =
new ChartData() ;
float[] XValues = {1, 2, 3, 4, 5, 6, 7, 8, 9};
myChartData.XValues = XValues;
//line 1
ChartLine Line1 = new ChartLine ();
float[] YValues1 = {1280, 1280, 1280, 1270, 1270, 1280, 1270, 1260, 1260};
Line1.Values = YValues1;
Line1.Color = Color.Black ;
myChartData.Lines.Add(Line1);
//line 2
ChartLine Line2 = new ChartLine ();
float[] YValues2 ={1200, 1180, 1296, 1370, 1257, 1120, 1270, 1230, 1290};
Line2.Values = YValues2;
Line2.Color = Color.Yellow ;
Line2.Width = 2;
Line2.Style = DashStyle.Dash;
myChartData.Lines.Add(Line2);
//calling the engine
ChartGenerator.Engine oEngine = new Engine();
oEngine.ImageHeight = 200;
oEngine.ImageWidth = 300;
oEngine.ImageBackgroundColor = Color.LightGray ;
oEngine.IsScaled =
true ;
// Render BitMap Stream Back To Client
Bitmap StockBitMap = oEngine.DrawChart(myChartData);
StockBitMap.Save(Response.OutputStream, ImageFormat.JPEG);

and the result is:



You can easily add the code for grid and axis to have a ready-for-production engine.

share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor