Sunday, November 2, 2014

How to Bind FusionCharts with C# ?





FusionChart only accept data as XML and Json format so steps for binding dynamic data to FusionChart is

  •                     Query to database and get data in C# object (List<> or Datatable)
  •                     Convert List<> to Json Data by given Function


public string output;         // make the result as public to access it in .aspx

protected void Page_PreRender(object sender, EventArgs e)
    {

   List<Students> ll = (List< Students >)Function();

JavaScriptSerializer jss = new JavaScriptSerializer();

output = jss.Serialize(ll);
}

Now the data is converted to json and saved inside output.


  
  •          Add javascript to .aspx


<script src="FusionCharts/fusionwidgets-xt-enterprise-plus/js/fusioncharts.js"   type="text/javascript"></script>

  
  •            Now Write Code in .aspx


<script type="text/javascript">

            var jsonData = <%=output%>;

            FusionCharts.ready(function () {
               var revenueChart = new FusionCharts({
                    "type": "column3d",
                    "renderAt": "chartContainer",
                    "width": "500",
                    "height": "300",
                    "dataFormat": "json",
                    "dataSource": {
                        "chart": {
                            "caption": "Monthly revenue for last year",
                            "subCaption": "Harry's SuperMart",
                            "xAxisName": "Month",
                            "yAxisName": "Revenues (In USD)",
                            "theme": "fint"
                        },
                        "data": jsonData
                    }
                });

                revenueChart.render();
            })
    </script>

    <div id="chartContainer">FusionCharts XT will load here!</div>

1 comment:

Secure you Asp .NET by Web.config & Global.ascx?

Add to Global.ascx protected void Application_BeginRequest(object sender,EventArgs e)     {         //to remove x frame         Resp...