Showing posts with label Charts. Show all posts
Showing posts with label Charts. Show all posts

Wednesday, June 16, 2010

DataTable To String Array in C# without using a for Loop

Many time we need need to get data from dataset\Datatable to array to pass it to some function or third API like x axis or y axis series of chart controls or to some other reporting or data binding control may be dropdown. it becomes difficult to get the data of a column(s) in array and finally we end up writing a for loop. here is a sample example by which we can simply get array of a column(s) with out a for loop
public static string DataRowToString(DataRow dr)
{
       return dr["columns"].ToString();
}
public string [] DataTableToArray(DataTable dt)
{
     DataRow[] dr = dt.Select();
     string [] strArr= Array.ConvertAll(dr, new Converter(DataRowToString));
     return strArr;
}

Array.ConvertAll() function simply converts the array of one to another so here in above example we are passing dr as an araay of datarows in first arguments. second argument is a Converter delegate type which convert one type to another so in this we will pass a link to delegate which is DataRowToString() function ,the delegate should know the input datatype and output datatype which is datatrow and string in this case respectively. function DataRowToString noe simply takes a datarow and return the string value for the column specified. which can be extended for multiple columns also.

Ref: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/46ef6ca8-7f66-489a-a0f6-b6b7c4200d16

Tuesday, April 6, 2010

A HTML Table based Chart control in ASP.NET\MOSS 2007

Content:
  • Introduction
  • Various options to display charts on web
  • Code Walkthrough
  • Advantages
  • Limitation
Introduction:
A chart is a visual representation of data represented in various code\colors. It represents the summary of large chunk of unorganized data in an easy to understand manner which becomes very helpful in analysis, decision making and trend setting. The extensive usage of data over the web makes it very important control nowadays. Most of the modern web2.0 websites uses charts as one of the techniques to convey data to site visitors.
The article below gives a summary to create a 3d column charts in asp.net which can be modified and used in various other web supported platforms

Various options to display charts on web
Following are options to use chart on web in .net plateform
Microsoft chart controls:
Microsoft recently launched the rich functionality chart controls for asp.net to work with .net framework 3.5. It can be downloaded from the link below and used as any other controls in asp.net
http://www.microsoft.com/downloads/details.aspx?FamilyId=130F7986-BF49-4FE5-9CA8-910AE6EA442C
These chart controls will be part of visual studio 2010 release.
GDI+ charts:
GDI+ charts are traditionally used chart controls over the web. It uses various drawing API of com\.net to draw a picture at server end first and then transfer the picture on the web. These controls are very complex and resources consuming.
Creating 3d and nice look and feel also bit cumbersome
HTML Table based Charts:
The technique used below is to generate line charts with the help of html tables and 3d images. It draws the table calibrate a 3d image to fit in the row as per the given x\y axis value. Hence become very lightweight and supported on any .net framework\ other platforms. Below is the sample screen shot.
Code Walkthrough: There are mainly two code files in this project. A chart user control and a web page to host\call chart
Chart controls expose various properties to set the different parameters of chart like width, column height, y axis values, x axis values etc. DrawGraph() is called to draw chart based on the x axis and y axis values set in the properties. It normalize the values provides for x axis and resize it as per the chart width. Below are the algorithm steps.
  • Define the color image array to denote the x axis values
  • Find out the max value in the x axis collection
  • Get the mod by dividing chart width with max value
  • Draw a table and draw a row for each value
  • For each value in the collection multiply the mod with value to expand and divide chart width with standard param to contract
  • Set this value as the length of image
  • Repeat the colors if color array is less than the value specified
  • Emit the constructed html on div\label tag
 Advantages:
  • It really simple, lightweight, 3d bar chart with nice look and feel
  • The code\logic can be modified to work on any other platform\language
  • A html table base web chart renders well on all type of browsers
  • It can be used in any .net framework\SharePoint
  • Can be customized easily based on the requirements
Limitation:
  • Can’t use similar technique to draw pie charts
code available on request