Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chart HtmlHelper for Asp.net Mvc

Are there any HtmlHelper Extensions for Google Chart Api? (I like to use for some basic charts, e.g. Pie Chart, Bar Chart)

Soe Moe

like image 922
Soe Moe Avatar asked Oct 08 '09 04:10

Soe Moe


2 Answers

Google says that you insert a chart like this:

<img src="http://chart.apis.google.com/chart?
    chs=250x100
    &amp;chd=t:60,40
    &amp;cht=p3
    &amp;chl=Hello|World" 
    alt="Sample chart" 
/>

So it should be easy enough to write an HtmlHelper like this (untested):

namespace System.Web.Mvc.Html
{
    public static class GoogleChartHelpers
    {
        public static string GoogleChart
            (string cht, string chd, string chs, string chl)
        {
            return "<img source='http://chart.apis.google.com/chart?cht=" + cht 
                 + "&amp;chd=" + chd 
                 + "&amp;chs=" + chs 
                 + "&amp;chl=" + chl + "' />;
        }
    }
}

and call it like this:

<%= Html.GoogleChart("P3","t:60,40","250x100","Hello|World") %>

which should insert this into your page:

alt text

like image 131
Robert Harvey Avatar answered Oct 21 '22 09:10

Robert Harvey


A C# wrapper for the Google Chart API.

http://code.google.com/p/googlechartsharp/

Usage examples

http://code.google.com/p/googlechartsharp/wiki/UsageExamples

I'm sure you could create a HTMLHelper that incorporates this wrapper class to make it even easier.

like image 41
David Avatar answered Oct 21 '22 08:10

David