Sunday, May 1, 2011

How can i use any dataset form SQL in GoogleChart?

How can i use any dataset form SQL in GoogleChart? i really like googleChart. But i want to use Gchart with dataset from Sql' query result in C#?

From stackoverflow
  • Google charts take a formatted string for the data feed as I'm sure you know.

    you will need to loop through your dataset and generate this string then write it out to the page in a Literal control.

    For example

    DataSet d = GetDataSet();// returns your data
    string percentages = string.Empty;
    string names = string.Empty;
    string baseUrl = "http://chart.apis.google.com/chart?cht=p3&chs=250x100";
    
    foreach(DataRow row in d.Tables[0].Rows)
    {
        string tName = row["name"].ToString();
        int value = (int)row["value"];
        names += name + "|";
        percentages += value.ToString() + ",";
    }
    
    names = names.TrimEnd('|');
    percentages = percentages.TrimEnd(',');
    
    string fullUrl = baseUrl + "&chd=t:" + percentages;
    fullUrl += "&chl=" + namesl
    
    
    image1.ImageUrl = fullUrl;
    

0 comments:

Post a Comment