|
ASPChart.Net V1.01
Data Chart Generation Library
In our
Logmatix tutorial application we discussed how classes in System.Drawing
and System.Drawing.Imaging namespace can be utilized to create
image files to display data in graphical form. We also discussed some of the
problem you may run into when using this technique of generating image files on
the fly and then including them in the asp:Image server control.
The problem with that project was that the drawing and imaging library was not
very object oriented and the two types of graphs that we generated, bar and
pie, were kind of hard coded. So we decided to generate a very general purpose
Chart Engine which could be used for generating all kind of charts.
And this library is not limited to generating images for ASP.Net. It is very
generic and can be used in desktop applications too.
There are various kinds of charts that can be used to represent data in
different visual formats. Following is a list that contains some of these
types.
-
Bar Chart 2D
-
Bar Chart 3D
-
Pie Chart 2D
-
Pie Chart 3D
-
Line 2D
-
Line 3D
-
Point
-
Spline Graph
-
Area Chart
-
Column
-
Stacked Column
-
Candle Stick
This is not a complete list. We are still researching on it and trying to
gather information on each and every kind of graph and provide implementation
for that.
Implementation
We have tried to provide as much object-oriented implementation as possible so
that the library can be extended easily. At the top of the hierarchy is abstract
base class Graph. All other graph types derive from this class. In
this version of the library we have only included implementation of BarGraph2D
and BarGraph3D classes. As the names of the classes suggest that
these classes are used to generate 2D and 3D Bar Graphs.
The base class and derived classes expose a lot of properties and methods that
can be used to customize the display of graphs. Following a samll list of
properties that can be set before drawing the graph.
-
Title - Used to set the title of chart
-
TitleFont - Font to be used for displaying title
-
HasGridLines - Flag indicating of grid lines are to drawn or not
-
HasBorder - Sets flag to indicate if border around chart is to be drawn or not
This is kust a very small list of peoprty that can be used for cistomization.
Take a look at the source for complete list of properties and methods.
How to supply data
Graph class has a public method SetGraphData
that takes a XmlDocument as input parameter. Data needs to
supplied in xml format. It follows the following schema.
<chartdatalist>
<chartdataisland>
<chartdata>
<x>
<y>
</chartdata>
</chartdataisland>
</chartdatalist>
This schema is not finalized. We are still working on it to incorporate data
for all kinds of graphs.
Output
Graph class has an virtual method DrawGraph
which will called by the client to generate the chart. This method takes two
parameters, ImageFormat and Stream. You can specify
any image format that is supported by System.Imaging.ImageFormat class.
Second parameter can be supplied as any Stream object and the
implementation will store the generated image in it.
virtual public bool DrawGraph(ImageFormat imgFormat, ref Stream obOutputStream)
{
}
How to use it
We have included a small ASP.Net application in the included source code. It
adds a asp:Image control at run time to an asp:PlaceHolder
control to display a data chart. A hardcoded xml file is included in the sample
application to procide data for generating charts.
private void AddDataChartControl()
{
BarGraph3D dataChart = null;
WebUIImage chartImage = null;
Stream chartFile = null;
XmlDocument obXmlDoc = null;
string strFileName = "DataChart.jpg";
string strDataXmlFile = "ChartData.xml";
try
{
dataChart = new BarGraph3D();
chartImage = new WebUIImage();
chartFile = new FileStream(Server.MapPath(strFileName),
FileMode.Create, FileAccess.ReadWrite);
obXmlDoc = new XmlDocument();
obXmlDoc.Load(Server.MapPath(strDataXmlFile));
dataChart.Title = "Bar Graph 3D";
dataChart.HasGridLines = true;
dataChart.HasLegends = true;
dataChart.Width = 300;
dataChart.Height = 300;
dataChart.MinXValue = 0;
dataChart.MaxXValue = 100;
dataChart.MinYValue = -20;
dataChart.MaxYValue = 100;
dataChart.SetGraphData (obXmlDoc);
dataChart.DrawGraph(ImageFormat.Jpeg, ref chartFile);
chartFile.Close();
dataChartHolder.Controls.Add(chartImage);
chartImage.ImageUrl = strFileName;
chartFile = null;
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
finally
{
if (null != chartFile)
{
chartFile.Close();
}
}
}
Can the library be used in Windows Forms applications?
Yes, the library has complete support for using it in Windows Forms
application. From version 0.87, support has been added to provide Graphics
object as input parameter to DrawGraph method. The engine will not
create any Graphics object for drawing, instead it will use the
one provided by the caller.
private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Load XML data file.
XmlDocument obXmlDoc = null;
BarGraph3D obGraph = null;
try
{
obXmlDoc = new XmlDocument();
obXmlDoc.Load("BarChartData.xml");
obGraph = new BarGraph3D();
obGraph.BackgroundColor = Color.Azure;
obGraph.UseGradientColors = true;
obGraph.Backgroud = BackgroundType.LinearGradient;
obGraph.LinearGradientType = LGradientType.ULeftToLRight;
obGraph.YTickSpacing = 100;
obGraph.Title = "Workers In U.S Labor Force";
obGraph.IslandSpacing = 40.0F;
obGraph.RightSpacing = 40.0F;
obGraph.DrawXTickerText = false;
obGraph.DrawYTickerText = false;
obGraph.HasGridLines = true;
obGraph.HasLegends = true;
obGraph.HasTickers = true;
obGraph.LegendAlignment = LegendAlignmentType.Horizontal;
obGraph.MinXValue = 0;
obGraph.MaxXValue = 100;
obGraph.MinYValue = 0;
obGraph.MaxYValue = 1000;
obGraph.AxisPaddingTop = 60;
obGraph.AxisPaddingBottom = 60;
obGraph.AxisPaddingLeft = 30;
obGraph.AxisPaddingRight = 30;
obGraph.SetGraphData (obXmlDoc);
obGraph.DrawGraph(((Control)sender).CreateGraphics());
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
Some of the used of this library can be listed as follows.
- Medical Charting Software
- Financial Charting Software
- Real time financial reporting software
- Enterprise Reporting Softwares
- And more....
Final note
Please feel free to send your suggestions directly to us at
support@netomatix.com.
|