How to use Chart.Js with ASP.NET MVC

2

Well, I am returning a list of and sending to View . You need to implement this using a graph with Chart.js .

I have the Country class:

[Table("Pais")]
public class Pais
{
    [Key]
    public Guid PaisId { get; set; }

    [Required]
    public String Nome { get; set; }

    public virtual ICollection<FertilidadePorAno> FertilidadePorAno { get; set; }
}

The class FertilityPorn :

[Table("FertilidadePorAno")]
public class FertilidadePorAno
{
    [Key]
    public Guid FertilidadePorAnoId { get; set; }
    public Guid PaisId { get; set; }

    [Required]
    public int Ano { get; set; }

    [Required]
    public decimal Valor { get; set; }

    public virtual Pais Pais { get; set; }
}

And a ParentFertille associative class:

[Table("PaisFertilidadePorAno")]
public class PaisFertilidadePorAno
{
   public Guid PaisFertilidadePorAnoId { get; set; }

    public string  Nome { get; set; }

    public int Ano { get; set; }

    public double Valor { get; set; }
}

My controller looking for the data in the Bank:

 public async Task<ActionResult> Relatorio(Guid id)
 {
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    var entries = db.Database.Connection
   .Query<PaisFertilidadePorAno>(@"SELECT p.Nome, f.Ano, f.Valor from Pais As p
                                                    inner join FertilidadePorAno as f
                                                    on f.PaisId = p.PaisId
                                                    where p.PaisId = @id
                                                    order by f.Ano", new {  Ano = (int?)null, Id = id });
        return View(entries);
    }

Return the data to View which is currently like this:

@model IEnumerable<TaxaDeFertilidade.Models.PaisFertilidadePorAno>

@{
    ViewBag.Title = "Relatorio";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Relatorio</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Nome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Ano)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Valor)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Nome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Ano)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Valor)
        </td>
    </tr>
}

</table>

However, I need to create a chart.js chart with this information coming from the Bank that are the name of the Country , Year and the Fertility Rate < in relation to that year. How do I do this in View ?

    
asked by anonymous 19.01.2017 / 14:32

2 answers

3

A simple way to use it is by using Chart.Mvc .

To use this package, simply install via NuGet with the following command:

  

Install-Package Chart.Mvc

After that, do something like this in your View:

@using Chart.Mvc.ComplexChart;
@using Chart.Mvc.Extensions

<script src="~/Scripts/Chart.js"></script>

@{
    ViewBag.Title = "Home Page";

    var nome = new[] { "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho" };
    var valores = new List<double> { 65, 59, 80, 81, 56, 55, 40 };

    var barChart = new BarChart();
    barChart.ComplexData.Labels.AddRange(nome);

    barChart.ComplexData.Datasets.AddRange(new List<ComplexDataset>
                           {
                              new ComplexDataset
                                  {
                                      Data = valores,
                                      Label = "My First dataset",
                                      FillColor = "rgba(220,220,220,0.2)",
                                      StrokeColor = "rgba(220,220,220,1)",
                                      PointColor = "rgba(220,220,220,1)",
                                      PointStrokeColor = "#fff",
                                      PointHighlightFill = "#fff",
                                      PointHighlightStroke = "rgba(220,220,220,1)",
                                  }
                          });
}

<canvas id="myCanvas" width="400" height="400"></canvas>
@Html.CreateChart("myCanvas", barChart)

Just change the name and values variables to the values you want.

The result will be this:

    
19.01.2017 / 16:54
1

I would advise you to implement your method as JsonResult and pick up the data coming from Json and treat it in your chart.js in the view, as this dotnetfiddle example.

link

I hope I have helped;)

    
19.01.2017 / 17:09