I'm struggling with dynamic data, C# MVC and Google Charts for a while now. What I try to achieve is reading out a database file and create a Google chart for each group.
I saw an example like http://www.aspdotnet-pools.com/2014/07/dynamic-google-scatter-chart-in-aspnet.html but dont know in that example how to handle dynamic data in the controller file.
Controller:
namespace AppRepo.Controllers
{
public class RepositoriesController : Controller
{
private Database1Entities db = new Database1Entities();
// GET: Repositories
public ActionResult Index()
{
return View(db.Repositories.ToList());
}
public ActionResult Grouped()
{
return View(db.Repositories.ToList());
}
public ActionResult Charts()
{
return View(db.Repositories.ToList());
}
}
VIEW file:
@model List<AppRepo.Models.Repository>
@{
ViewBag.Title = "Charts";
}
@section Scripts
{
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', { packages: ['corechart'] });
</script>
<script type="text/javascript">
google.setOnLoadCallback(drawChart);
function drawChart() {
var tdata = new google.visualization.DataTable();
var data = [{ "Name": "A", "Qty": 1 }, { "Name": "B", "Qty": 2 }, { "Name": "C", "Qty": 3 }, { "Name": "D", "Qty": 4 }];
tdata.addColumn('string', 'Name');
tdata.addColumn('number', 'Qty');
for (var i = 0; i < data.length; i++) {
tdata.addRow([data[i].Name, data[i].Qty]);
}
var options = {
width: 1000,
height: 563,
//title: '@Model.Select(r => r.BackupServer)',
hAxis: {
title: 'Date',
gridlines: { count: 10 }
},
vAxis: {
title: '% Free'
}
};
chart = new google.visualization.ColumnChart(document.getElementById('divchart'));
chart.draw(tdata,options);
}
</script>
}
<div id='divchart'>
</div>
I also get the following error now:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[AppRepo.Models.Repository]', but this dictionary requires a model item of type 'AppRepo.Models.Repository'.