Created
May 28, 2012 19:40
Working with Google Visualizations in C# MVC and Razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Index() | |
{ | |
ViewBag.Data = new Bortosky.Google.Visualization.GoogleDataTable(ExampleTable()).GetJson(); | |
return View(); | |
} | |
private static DataTable ExampleTable() | |
{ | |
var dt = new DataTable(); | |
dt.Columns.Add("Date", typeof(System.DateTime)).Caption = "Date"; | |
dt.Columns.Add("Views", typeof(int)).Caption = "Views"; | |
Random random = new Random(); | |
for (var i = 1; i < 30; i++) | |
{ | |
int views = random.Next(0, 100); | |
dt.Rows.Add(new object[] { DateTime.Now.AddDays(i), views }); | |
} | |
return dt; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="https://www.google.com/jsapi"></script> | |
<script type="text/javascript"> | |
google.load("visualization", "1", { packages: ["corechart"] }); | |
google.setOnLoadCallback(drawChart); | |
function drawChart() { | |
var data = new google.visualization.DataTable('@(Html.Raw(ViewBag.Data))'); | |
var options = { | |
title: 'Company Performance' | |
}; | |
var chart = new google.visualization.LineChart(document.getElementById('chart_div')); | |
chart.draw(data, options); | |
} | |
</script> | |
<div id="chart_div" style="width: 900px; height: 500px;"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change is code
var data = new google.visualization.DataTable(@(Html.Raw(ViewBag.Data)));