Thursday, May 3, 2012

How to binding the data to a chart in asp.net

Title:How to bind data to chart control in asp.net using c#.net

Description:
Binding data to a chart control is pretty simple in asp.net.The below example will show how to set the  student marks and exam types.For this two arrays String[],Double[] are used to get the count of class subject and exams which are given to chart x,y axis values
String[] sItems = new String[10];
Double[] iValue = new Double[10];
int j;
string s = "select count (*) as count from class_subject where classid='4'";
Con.Open();
cmd = new SqlCommand(s, Con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
count = int.Parse(dr["count"].ToString());
}
Con.Close();
string sql = "select count (*) as count from exams e, marks m
where m.examid=e.examid and m.admissionno='" +
Session["admissionnumber"] + "'";
Con.Open();
cmd = new SqlCommand(sql, Con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
exams = int.Parse(dr["count"].ToString());
}
Con.Close();
string sql1 = "select e.examtype as examname,ms.marks as obtainedmarks,mm.maximumfrom  marks_subject ms,marks m,exams
e,maxmarks mm  where e.examid=m.examid and  m.marksid=ms.marksid and
e.examid=mm.examid and  m.admissionno='" + Session["admissionnumber"]
+ "' and ms.subjectid='17'";
Con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql1, Con);
Con.Close();
ds.Tables.Clear();
da.Fill(ds);
for (j = 0; j < exams; j++)
{
sItems[j] = ds.Tables[0].Rows[j][0].ToString();
obt = int.Parse(ds.Tables[0].Rows[j][1].ToString());
max = int.Parse(ds.Tables[0].Rows[j][2].ToString());
totalmarks = count * max;
percentage = (double)obt / totalmarks * 100;
string rounded = percentage.ToString("#0.00");
iValue[j] = double.Parse(rounded.ToString());
}
sItems[j] = "maximum % ";
iValue[j] = 100;
// axis values
markschart.YAxisValues = iValue;
markschart.YAxisItems = sItems;
markschart.Visible = true;
markschart.ChartTitle = "Marks chart:";
markschart.XAxisTitle = "(units display percentage)";

No comments:

Bel