Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
SQL
I have a problem with google visualization BarChart, at first it was visible before I binded it to the Datatable. Now its not visible and when debugging my code the data table has the rows and data. Please Help I believe i have followed the example as it is here

Below its my aspx page where my bar chart is aligned :
<pre lang="HTML">
<td>
             <div>
            <asp:Literal ID="lt" runat="server"></asp:Literal>
             </div> 
            <div id="chart_div" style="width: 550px; height: 400px;"></div>
        </td>



Here is my .CS page :
C#
  protected void Page_Load(object sender, EventArgs e)
    {
       if (Page.IsPostBack == false)
         {
           BindBarChart();
         }
    }

private DataTable GetBarData() 
{
    DataTable dt = new DataTable();
    using (SqlConnection conn = new   SqlConnection(Application_Info.CompanyConnection()))
    {
            string cmd = "select ID, ClientName from Company with(nolock)"
            SqlDataAdapter adp = new SqlDataAdapter(cmd, conn);
            adp.Fill(dt);
            return dt;
    }
}

    private void BindBarChart() 
{
    StringBuilder str = new StringBuilder();
    DataTable dt = new DataTable();

    try
    {
        dt = GetBarData();
        str.Append(@"<script type=text/javascript> google.load( *visualization*, *1*, {packages:[*corechart*]});
                   google.setOnLoadCallback(drawChart);

                    function drawChart() {
                    var data = new google.visualization.DataTable();
                    data.addColumn('number', 'Number');
                    data.addColumn('string', 'ClientName');
                    data.addRows(" + dt.Rows.Count + ");");

        for (int i = 0; i <= dt.Rows.Count - 1; i++)
        {
            str.Append("data.setValue( " + i + "," + 0 + "," + "'" + dt.Rows[i]["Number"].ToString() + "');");
            str.Append("data.setValue(" + i + "," + 1 + "," + dt.Rows[i]["ClientName"].ToString() + ") ;");
        }

        str.Append("var chart = new google.visualization.BarChart(document.getElementById('chart_div'));");
        str.Append("chart.draw(data, {title:'Report', titleTextStyle:{color: 'Blue'},");
        str.Append("vAxis: { title: 'Client', titleTextStyle: { color: 'red' } }");
        str.Append("}); }");
        str.Append("</script>");
        lt.Text = str.ToString().TrimEnd(',').Replace('*', '"');
    }
    catch { }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900