Skip Navigation Links Log In
Google Charts C# Wrapper for ASP.NET
Recently Google release the Google Chart API, a way of dynamically generating charts by simply entering a URL into a web browser. The only complicated part is generating the url in the correct format, and because I can see myself making a lot of use of this I've written a complete wrapper for Google Charts in C#. It's available as a free download.
As far as I know the wrapper is complete: you can create all the chart types (Line chart, Scatter plot, Bar chart, Venn diagram, Pie chart), including all the variants, and all the different options like background gradients, shape markers and range markers have been reproduced. All of the graphs on this page were created using this Google Chart C# wrapper.
Coding with the C# wrapper should be pretty simple. Just stick an image on your page, add a reference to the GoogleCharts dll, then call the appropriate method to get the url of the chart. There's one url method for each type of graph. For each only the chart size and data are required - if you don't want it just null that value.
To make things even easier I've included an ASP.NET website in the download with some sample code (including lots of comments) showing you how to create each graph. The whole source code is also available, so if it turns out that I have ballsed something up or you'd like to add something else you can, although if you find a bug, let me know and I'll probably fix it for you. Feel free to do whatever you like with this code, rewrite it, redistribute it, whatever. I'd be nice if you could drop me a quick line if you do end up using it though, just for my own interest.
Here's some example code for a Pie Chart:
        // 1. Set the size of the chart. The numbers are measured in pixels.
        ChartSize pieChartSize = new ChartSize(500, 200);

        // 2. Set the title for your chart. You also specify the size in pixels of the title and the colour. 
        ChartTitle pieChartTitle = new ChartTitle("My Profanity Usage By Cause", 15, Color.Black);

        // 3. Set the data for your chart. 
        // The data should be a collection of floats between 0.0f and 100.0f.
        List<float> pieChartData = new List<float>();
        pieChartData.Add(5.0f);
        pieChartData.Add(5.0f);
        pieChartData.Add(5.0f);
        pieChartData.Add(10.0f);
        pieChartData.Add(70.0f);

        // 4. Add the labels to your chart.
        // The order of the labels must match the data.
        List<string> pieChartLabels = new List<string>();
        pieChartLabels.Add("Injury");
        pieChartLabels.Add("Irony");
        pieChartLabels.Add("Misc");
        pieChartLabels.Add("Segfaults");
        pieChartLabels.Add("Mariokart");

        // 5. The colours of the different pie chart sections.
        // Again the order of the colours must match the data.
        List<Color> pieChartColors = new List<Color>();
        pieChartColors.Add(Color.FromArgb(109, 21, 133));
        pieChartColors.Add(Color.FromArgb(139, 21, 133));
        pieChartColors.Add(Color.FromArgb(169, 21, 133));
        pieChartColors.Add(Color.FromArgb(199, 21, 133));
        pieChartColors.Add(Color.FromArgb(229, 21, 133));

        // 6. Generate the url by calling the appropriate GoogleChart method.
        this.PieChart.ImageUrl = GoogleChart.GetPieChartUrl(pieChartSize, pieChartData, 
        pieChartColors, pieChartTitle, null, pieChartLabels);