Introduction
Google provided an open API for generating the charts which can be used in our commercial web applications. We can generate various graphs using the Google chart API.
The Google Chart API lets you dynamically generate charts. To see the Chart API in action, open up a browser window and copy the following URL into it:
http://chart.apis.google.com/chart?cht=p3&chd=s:hW&chs=250x100&chl=Hello|World
The following types of charts are available:
- Line Chart
- Bar Chart
- Pie Chart
- Venn diagram
- Scatter Plot
The following article demonstrates the usage of graph in ASP.NET.
Use the image tag and place the source there:
<img src="http://chart.apis.google.com/chart?cht=p3&chd=s:asR&chs=400x150&chl=A|B|C" />
We can pass a variety of parameters to enhance the graph. Following are a few:
Graph Title
We can place the chart title using:
e.g.: chtt=Welcome chts
Graph Style
We can place the graph tile color and size of the font:
chts for Title Style : color,size
e.g.: chts=225ff0,20
Graph Colors
We can have different colors:
chco for colors: one or many
e.g.: chco=ff0000,00ff00,0000ff
Passing the Data
The data can be passed using various encoding techniques like simple, text, etc. I have done the simple encoding for you in C#:
public string GenerateGraph()
{
DataTable dt = new DataTable();
dt = dtGraphData;
int maxval = GetMaxVal(dt);
string GReqURL = http:
GTitleColor+","+GTitleSize+"&chtt="+GTitle+"&chco="+GColors+
"&cht=p3&chs="+GWidth+"x"+GHeight;
string simpleEncoding =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
string chartData = string.Empty;
string chartLabels = string.Empty;
StringBuilder strbchartData = new StringBuilder();
StringBuilder strbchartLabels = new StringBuilder();
strbchartData.Append("&chd=s:");
strbchartLabels.Append("&chl=");
int strlen = simpleEncoding.Length - 1;
foreach (DataRow dr in dt.Rows)
{
int arrVal = Convert.ToInt32(dr[2]);
int len = strlen * arrVal / maxval;
strbchartData.Append(simpleEncoding.Substring(len, 1));
strbchartLabels.Append(dr[1] + "|");
}
chartData = strbchartData.ToString();
chartLabels = strbchartLabels.ToString();
chartLabels = chartLabels.Substring(0, chartLabels.Length - 1);
return GReqURL + chartData + chartLabels;
}
The code is written in C#. You can assign all the properties you want to assign and finally call the GenerateGraph
method which returns the string
. Below is the code sample. The entire article is explained using the PIE graph example. The remaining graphs can also be achieved using the same method.
DataTable dt = new DataTable();
dt = GetGraphData();
PieGraph pg = new PieGraph();
pg.GraphColors = "5566AA";
pg.GraphHeight="125";
pg.GraphWidth="250";
pg.GraphTitle = "Welcome to Test Graph";
pg.GraphTitleColor = "112233";
pg.GraphTitleSize = "20";
pg.dtData = dt;
GGReq = pg.GenerateGraph();
this.DataBind();
}
DataTable GetGraphData()
{
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("Select * from GraphSupp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
I took 3 cols id, description and the count and generated a string
and assigned that to the image source.
This is to show how to pass data to the PI graph, we can also perform simple encoding for other graphs...
Steps for Generation of the Graph
- Create a
DataTable
which contains column-2 as labels and column-3 as Data - Create an instance of
PieGraph
(refer to the text doc I have attached) - Assign the properties
- Place the return
string
to the source of the image tag - Graph is generated
Points of Interest
- Easy way of generation of graphs
- Less effort on the web server as there is no need to render the image using Drawing name space
- Enhanced look and feel and open to all
References
History
- 20th January, 2008: Initial post