Tuesday 22 May 2018

Generate google chart using common function in asp.net c#


//////////////// JS

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

/////////////////////////HTML

         <asp:Literal ID="ltScripts" runat="server"></asp:Literal>
        <div id="chart_div" style="width: 660px; height: 400px;"></div>


/////////////////////CS


  public string Generate_GoogleChart(DataTable dt, string ChartType, string ChartID, string FunctionName, string Is3D, string ChartTitle,string vAxis,string hAxis,string SeriesType,string NoOfSeries,string TypeOfCombo,string IspieHole)
    {
        StringBuilder strScript = new StringBuilder();
        strScript.Append(@"<script type='text/javascript'>google.load('visualization', '1', {packages: ['corechart']}); </script> <script type='text/javascript'>function " + FunctionName + "() { var data = google.visualization.arrayToDataTable([[");
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (i < dt.Columns.Count - 1)
            {
                strScript.Append(@"'" + dt.Columns[i].ColumnName + "',");
            }
            else
            {
                strScript.Append(@"'" + dt.Columns[i].ColumnName + "'");
            }
        }

        strScript.Append(@"],");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            strScript.Append(@"[");
            for (int c = 0; c < dt.Columns.Count; c++)
            {
                if (c == 0)
                    strScript.Append("'" + dt.Rows[i][c] + "',");
                else
                {
                    if (c < dt.Columns.Count - 1)
                    { strScript.Append("" + dt.Rows[i][c] + ","); }
                    else
                    {
                        strScript.Append("" + dt.Rows[i][c] + "");
                    }
                }
            }
            strScript.Append(@"],");

        }
        strScript.Remove(strScript.Length - 1, 1);
        strScript.Append("]);");
        string OtherOptions = "";
        if (!ChartType.ToLower().Contains("pie"))
        {
            OtherOptions = " ,vAxis: { title: '" + vAxis + "' }, hAxis: { title: '" + hAxis + "' }";
            if (ChartType.ToLower().Contains("Combo"))
            {
                OtherOptions = OtherOptions + ", seriesType: '" + SeriesType + "', series: { " + NoOfSeries + ": { type: '" + TypeOfCombo + "'} ";
            }
       
        }
        else
        {
           // OtherOptions = IspieHole == "" ? "" : ", pieHole: 0.5";  //
        }

        strScript.Append(@" var options = {title: '" + ChartTitle + "',is3D: " + Is3D + " " + OtherOptions + "}; ");
        strScript.Append(@"var chart = new google.visualization." + ChartType + "(document.getElementById('" + ChartID + "')); chart.draw(data, options);}google.setOnLoadCallback(" + FunctionName + ");");
        strScript.Append(" </script>");
        return strScript.ToString();
    }

///////////////////////Pass variable

private void BindChart()
    {
        DataTable dsChartData = new DataTable();       
            string chrt = Generate_GoogleChart(dsChartData, "BarChart", "chart_div", "drawVisualization", "false", "Monthly Coffee Production by Country", "Cups", "Month", "", "", "", "");
            ltScripts.Text = chrt;
        }
        catch
        {
        }
    }

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...