I have been searching the internet for a way to fill the data from a controller, the code is programmed in ASP.NET MVC 5 RAZOR.
I show you the code below:
VIEW
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js">
</script>
<script type="text/javascript">
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable();
var options = {
title: 'Inventario físico'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
In the part that says var data = google.visualization.arrayToDataTable (); inside the parentheses is where I will put the driver's query to fill my pie chart.
Next I show you the CONTROLLER method.
//OBTENEMOS DATOS DESDE LA BD PARA GENERAR LA GRAFICA DE PASTEL
public string estadisticasGrafico()
{
DataTable datos = new DataTable();
datos = FunctionInventario279.estadisticasGrafico();
string strDatos = "[['Conteos pendientes', 'Cantidad'],";
foreach (DataRow dr in datos.Rows)
{
strDatos = strDatos + "[";
strDatos = strDatos + "'" + dr[4] + "'" + "," + dr[5];
strDatos = strDatos + "],";
}
strDatos = strDatos + "]";
return strDatos;
}
Once the data has been consulted from the base, the pie chart can be displayed.
The data is sent to call from an SP, through a connection string (method) in which it returns a datatable.