Pie Chart in ASP.net

923 views
Skip to first unread message

Android Developer

unread,
Aug 5, 2019, 6:12:47 AM8/5/19
to Google Visualization API
Dear Friends,
How I can show the pie chart in asp.net c#
as all my data came from the database so I write the code in aspx page as below

<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">

 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load("current", {packages:["corechart"]});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          <asp:Repeater ID="rptMarkers" runat="server">
    <ItemTemplate>
             {
                 "Trail Type"; '<%# Eval("Trail_Type") %>',
                "Plot"; '<%# Eval("Plot") %>',
            }
    </ItemTemplate>
    <SeparatorTemplate>
        ,
    </SeparatorTemplate>
    </asp:Repeater>
        ]);

        var options = {
          title: 'My Trial Assigment',
          is3D: true,
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
      }
    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="body" Runat="Server">


    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>

</asp:Content>

and CS page code as Below

if (!this.IsPostBack)
        {
            DataTable dt = this.GetData("SP_Name");
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
        }
    }

    protected DataTable GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@ddlyear", "2019");
        cmd.Parameters.AddWithValue("@season", "K");
        cmd.Parameters.AddWithValue("@usercode", "97******");
        cmd.Parameters.AddWithValue("@role", "0");
        cmd.CommandType = CommandType.StoredProcedure;
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }


This code bind the data to
"Trail Type"; '<%# Eval("Trail_Type") %>',
                "Plot"; '<%# Eval("Plot") %>',


as below

var data = google.visualization.arrayToDataTable([
{
"Trail Type"; 'DEM',
"Plot"; '94',
}
,
{
"Trail Type"; 'GCA',
"Plot"; '2',
}
,

]);
But chart not visible on page.

can anyone help me?
Reply all
Reply to author
Forward
0 new messages