Search This Blog

Thursday, July 14, 2011

How to use FusionCharts in Windows .NET Applications

Steps to run graphical charts dinamically in your application

Step 1::
Create a folder to store xml files(In this folder dinamically xml will be created based on the values comes from the
database).

step 2::
Create a folder to store javascript and swf files in your application.

Now come to the coding part
step 3::
Take a literal control in your aspx page where you want to show the graph

step 4::

Part 1::
Put this function when you want to get data in graph monthwise

public void showmonths()
    {
        SqlParameter[] showreportmonthwise = new SqlParameter[]
        {
            new SqlParameter("@emailid",Session["emailid"].ToString()),
            new SqlParameter("@testname",Label1.Text.Trim())
        };
        DataSet objDs = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "sp_your_query",
showreportmonthwise);
        if (objDs.Tables[0].Rows.Count != 0)
        {
            string str = "<chart caption='Test Report' xAxisName='Months' yAxisName='Marks' numberPostfix='%'
showValues='0' formatNumberScale='0' showBorder='1'>";
            //str = showmonths();
            foreach (DataRow dr in objDs.Tables[0].Rows)
            {
                monthname = dr[1].ToString();
                marks = dr[0].ToString();
                //str = str + "<set label='" + monthname + "'value='" + marks + "'/>";//' value='" + d + "'  
                str = str + "<set label='" + monthname + "' value='" + marks + "' />";
            }
            str = str + "</chart>";
            DataSet ds = new DataSet();
            //c = Convert.ToString(ds);
            ds.ReadXml(new StringReader(str));
            ds.WriteXml(Server.MapPath("~/xml_files/wholereport.xml"));
            Literal3.Text = FusionCharts.RenderChart("FusionCharts/Column3D.swf", "xml_files/wholereport.xml", "",
"myFirst1", "600", "300", false, true);
        }
    }

Part 2::
Put this function when you want to get the data user wise

private void showresult_user_wise()
    {
        SqlParameter[] showreportuserwise = new SqlParameter[]
        {
            //new SqlParameter("@emailid",Session["emailid"].ToString()),
            new SqlParameter("@testname",Label1.Text.Trim())
        };
        DataSet objDs = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "sp_userwise",
showreportuserwise);
        if (objDs.Tables[0].Rows.Count != 0)
        {
            string str = "<chart caption='Test Report' xAxisName='User' yAxisName='Marks' numberPrefix='%' showValues='0'
formatNumberScale='0' showBorder='1'>";
            //str = showmonths();
            foreach (DataRow dr in objDs.Tables[0].Rows)
            {
                username = dr[1].ToString();
                score = dr[0].ToString();
                //str = str + "<set label='" + monthname + "'value='" + marks + "'/>";//' value='" + d + "'  
                str = str + "<set label='" + username + "' value='" + score + "' />";
            }
            str = str + "</chart>";
            DataSet ds = new DataSet();
            //c = Convert.ToString(ds);
            ds.ReadXml(new StringReader(str));
            ds.WriteXml(Server.MapPath("~/xml_files/userwise.xml"));
            Literal2.Text = FusionCharts.RenderChart("FusionCharts/Column3D.swf", "xml_files/userwise.xml", "", "myFirst2",
"600", "300", false, true);
        }
    }

Part 3::
This function is used to get the data in pie chart
public void showmonths_piechart()
    {
        SqlParameter[] showreportmonthwise = new SqlParameter[]
        {
            new SqlParameter("@emailid",Session["emailid"].ToString()),
            new SqlParameter("@testname",Label1.Text.Trim())
        };
        DataSet objDs = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure,
"sp_monthwise_in_singlequery", showreportmonthwise);
        if (objDs.Tables[0].Rows.Count != 0)
        {
            string str = "<chart caption='Test Report' palette='2' animation='1' numberPostfix='%' formatNumberScale='0'
pieSliceDepth='30' startingAngle='125'>";
            //str = showmonths();
            foreach (DataRow dr in objDs.Tables[0].Rows)
            {
                monthname = dr[1].ToString();
                marks = dr[0].ToString();
                //str = str + "<set label='" + monthname + "'value='" + marks + "'/>";//' value='" + d + "'  
                str = str + "<set label='" + monthname + "' value='" + marks + "' />";
            }
            str = str + "</chart>";
            DataSet ds = new DataSet();
            //c = Convert.ToString(ds);
            ds.ReadXml(new StringReader(str));
            ds.WriteXml(Server.MapPath("~/xml_files/" + Convert.ToString(Session["emailid"]) + "piechart.xml"));
            //Literal3.Text = FusionCharts.RenderChart("FusionCharts/Pie3D.swf", "xml_files/wholereport.xml", "",
"myFirst1", "600", "300", false, true);
            Literal4.Text = FusionCharts.RenderChart("FusionCharts/Pie3D.swf", "xml_files/" + Convert.ToString(Session
["emailid"]) + "piechart.xml", "", "myfirst_piechart", "300", "200", false, true);
        }
    }