- > Preview, here you are passing the values in a static way to the chart, but I need to retrieve the information from the database. I have a 'Confima' column, I want to get the total that is true and the total is false.
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutGrafico.cshtml";
}
@section head_scripts{
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">
// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Confirmado', 30],
['Cancelado', 10]
]);
// Set chart options
var options = {'title':'Relação de pesquisas Confirmadas e Canceladas',
'width':600,
'height':500};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
}
<div class="container">
<h1 class="page-header">Gráfico de Pesquisas</h1>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</div>
- > This is my class model, responsible for creating the table in the database.
namespace SistemaGerencial.Models.Gerencial
{
public class PesquisaProduto
{
[Key]
public int PesquisaId { get; set; }
[Display(Name = "Codigo Produto")]
public int CodPro { get; set; }
[Display(Name = "Codigo de Barras")]
public string CodBar { get; set; }
[Display(Name = "Nome Produto")]
public string Nome { get; set; }
[Display(Name = "Valor unitário")]
public decimal Valor { get; set; }
[Display(Name = "Quantidade informada")]
public decimal Qtd { get; set; }
[Display(Name = "Valor Total")]
public decimal Total { get; set; }
[Display(Name = "Produto foi confirmado?")]
public bool Confimado { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DataPesquisa { get; set; } = DateTime.Now;
}
- > Class Controller
private ControleContext db = new ControleContext();
// GET: GraficoPesquisa
public ActionResult Index()
{
return View();
}