Skip to content

Instantly share code, notes, and snippets.

@jgdoncel
Last active January 28, 2019 10:39
Show Gist options
  • Save jgdoncel/31e33dedf6576c0408fd7ddd69f42ace to your computer and use it in GitHub Desktop.
Save jgdoncel/31e33dedf6576c0408fd7ddd69f42ace to your computer and use it in GitHub Desktop.
Clase para generar objetos compatibles con Google Chart (https://developers.google.com/chart/interactive/docs/reference#dataparam)

Clase para generar objetos compatibles con Google Chart (https://developers.google.com/chart/interactive/docs/reference#dataparam)

Ejemplo de uso:

@using GoogleChart;
@{
    string json;

    var rootObject = new RootObject()
    {
        cols = new List<Col>
        {
            new Col {id = "1", label = "Name", type = "string"},
            new Col {id = "2", label = "2010", type = "number"},
            new Col {id = "3", label = "2015", type = "number"}
        },
        rows = new List<Row>()
        {
            new Row 
            {
                c = new List<C> { 
                    new C { v = "SPAIN" },
                    new C { v = "215" },
                    new C { v = "250" }
                }
            },
            new Row 
            {
                c = new List<C> { 
                    new C { v = "UK" },
                    new C { v = "122.3" },
                    new C { v = "180" }
                }
            }
        }
    };

    json = Json.Encode(rootObject);

    Response.ContentType = "application/json";
    @Html.Raw(json)

}
using System.Collections.Generic;
namespace GoogleChart {
public class Col
{
public string id { get; set; }
public string label { get; set; }
public string type { get; set; }
}
public class C
{
public string v { get; set; }
}
public class Row
{
public List<C> c { get; set; }
}
public class RootObject
{
public List<Col> cols { get; set; }
public List<Row> rows { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment