Click here to Skip to main content
13,351,661 members (40,899 online)
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 20 Jan 2008

Google Charts in .NET

, 21 Jan 2008
Rate this:
Please Sign up or sign in to vote.
Google Charts in .NET


Google provided an open API for generating the charts which can be used in our commercial web applications. We can generate various graphs using the Google chart API.

The Google Chart API lets you dynamically generate charts. To see the Chart API in action, open up a browser window and copy the following URL into it:|World  

The following types of charts are available:

  • Line Chart
  • Bar Chart
  • Pie Chart
  • Venn diagram
  • Scatter Plot

The following article demonstrates the usage of graph in ASP.NET.

Use the image tag and place the source there:

<img src="|B|C" />

We can pass a variety of parameters to enhance the graph. Following are a few:

Graph Title

We can place the chart title using:

e.g.: chtt=Welcome chts

Graph Style

We can place the graph tile color and size of the font:

chts for Title Style : color,size

e.g.: chts=225ff0,20

Graph Colors

We can have different colors:

chco for colors: one or many

e.g.: chco=ff0000,00ff00,0000ff

Passing the Data

The data can be passed using various encoding techniques like simple, text, etc. I have done the simple encoding for you in C#:

public string GenerateGraph()
    //Get the Data to draw the graph
    DataTable dt = new DataTable();
    dt = dtGraphData;

    //Get the max vals in the Data
    int maxval = GetMaxVal(dt);    

    //        cht=p3&chs=400x200&chd=s:asR&chl=A|B|C
    string GReqURL =

    //chts for Title Style : color,size
    //chtt for title text 
    //chco for colors: one or many

    string simpleEncoding = 
    string chartData = string.Empty;
    string chartLabels = string.Empty;
    StringBuilder strbchartData = new StringBuilder();
    StringBuilder strbchartLabels = new StringBuilder();


    int strlen = simpleEncoding.Length - 1;
    foreach (DataRow dr in dt.Rows)
        //Generate the chart DATA
        int arrVal = Convert.ToInt32(dr[2]);
        int len = strlen * arrVal / maxval;
        strbchartData.Append(simpleEncoding.Substring(len, 1));

        //Generate the Chart Labels
        strbchartLabels.Append(dr[1] + "|");

    //Converting the string builder to string
    chartData = strbchartData.ToString();
    chartLabels = strbchartLabels.ToString();

    chartLabels = chartLabels.Substring(0, chartLabels.Length - 1);
    return GReqURL + chartData + chartLabels;

The code is written in C#. You can assign all the properties you want to assign and finally call the GenerateGraph method which returns the string. Below is the code sample. The entire article is explained using the PIE graph example. The remaining graphs can also be achieved using the same method.

    DataTable dt = new DataTable();
    //Get the Data the data table and pass it         
    dt = GetGraphData();                
    PieGraph pg = new PieGraph();
    pg.GraphColors = "5566AA";
    pg.GraphTitle = "Welcome to Test Graph";
    pg.GraphTitleColor = "112233";
    pg.GraphTitleSize = "20";
    pg.dtData = dt;
    GGReq = pg.GenerateGraph();        

DataTable GetGraphData()
    SqlConnection con = new SqlConnection(connectionstring);
    SqlCommand cmd = new SqlCommand("Select * from GraphSupp", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    return dt;

I took 3 cols id, description and the count and generated a string and assigned that to the image source.
This is to show how to pass data to the PI graph, we can also perform simple encoding for other graphs...

Steps for Generation of the Graph

  1. Create a DataTable which contains column-2 as labels and column-3 as Data
  2. Create an instance of PieGraph (refer to the text doc I have attached)
  3. Assign the properties
  4. Place the return string to the source of the image tag
  5. Graph is generated

Points of Interest

  1. Easy way of generation of graphs
  2. Less effort on the web server as there is no need to render the image using Drawing name space
  3. Enhanced look and feel and open to all



  • 20th January, 2008: Initial post


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


About the Author

Madhukar Mudunuru
Technical Lead Imaginnovate TechSolutions
India India
Spends free time, working with new stuff from Microsoft and Web technologies. Recently started working on "Ruby on Rails" and other open source technologies.

You may also be interested in...

Comments and Discussions

QuestionTrigger select event Pin
Member 830220628-Oct-14 21:03
memberMember 830220628-Oct-14 21:03 
GeneralGoogle charts for window Pin
irafN78621-Jan-08 4:58
memberirafN78621-Jan-08 4:58 
GeneralRe: Google charts for window Pin
Madhukar Mudunuru21-Jan-08 18:14
memberMadhukar Mudunuru21-Jan-08 18:14 
GeneralWhere's the project Pin
Dewey20-Jan-08 22:23
memberDewey20-Jan-08 22:23 
GeneralRe: Where's the project Pin
Madhukar Mudunuru20-Jan-08 23:49
memberMadhukar Mudunuru20-Jan-08 23:49 
GeneralRe: Where's the project Pin
Dewey23-Jan-08 23:28
memberDewey23-Jan-08 23:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 21 Jan 2008
Article Copyright 2008 by Madhukar Mudunuru
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid