Mason
Mason

Reputation: 55

C# Chart class not rendering in view

I'm trying to test charting functionality in my C#.NET 4.0 MVC application following this guide.

Here is my View code:

@{
    Layout = null;
}

@{
    var myChart = new Chart(width: 600, height: 400)
   .AddTitle("Employees")
   .AddSeries(chartType: "column",
      xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
      yValues: new[] { "2", "6", "4", "5", "3" })
   .Write();


}

When I run the application it just gives me a bunch of ASCII characters:

￿￿￿JFIF``￿￿C  $.' ",#(7),01444'9=82<.342￿￿C 2!!22222222222222222222222222222222222222222222222222￿￿￿X"￿￿ ￿￿￿}!1AQa"q2￿￿￿#B￿￿R￿$3br￿ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿ ￿￿￿w!1AQaq"2￿B￿￿￿￿#3R￿￿$4￿&'()56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿?￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ (￿￿ +￿￿￿￿L￿L￿1V￿￿Fwc￿￿￿@u￿QU￿￿n￿￿￿￿￿￿:￿j￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿(￿￿￿￿ ￿P￿￿￿￿￿'￿;￿￿ա￿2￿￿n￿d￿￿￿￿4￿x ￿￿￿￿￿I￿￿v {Ϳ￿K￿￿￿mv￿￿#￿Y￿@P;￿kݖ ^N7f￿+￿￿ ￿=￿h￿u￿E@c￿￿￿o ￿￿qG￿￿￿(￿￿￿ܒI￿)V￿0A￿￿￿￿￿￿￿￿￿A}￿￿{￿￿6￿￿￿￿ND￿￿￿!￿\￿￿￿C￿W￿ 3￿￿￿Q￿ 3￿￿￿Q￿￿}v￿￿￿N񦥭￿￿￿￿Z4￿ ￿￿bc￿￿￿y￿?￿x￿￿Z￿f￿￿￿Ft￿￿foN￿jG￿/C￿￿￿￿￿x￿￿Z￿f￿￿￿￿9￿￿￿k2΂￿￿￿h￿￿h￿￿￿￿￿E￿￿￿￿￿ş￿"￿￿￿￿￿￿￿￿￿￿￿￿ c￿-Fg:￿CjҨV￿￿ ￿NH￿1￿yEs￿l￿￿?￿￿}￿￿￿￿"￿AEs￿l￿￿?￿￿}￿￿￿￿"￿AEs￿l￿￿?￿￿}￿￿￿￿"￿

Any thoughts on how to troubleshoot/proceed? I'm just looking for a simple bar graph solution. Thanks!

Upvotes: 0

Views: 199

Answers (1)

user3246175
user3246175

Reputation: 21

OK, I have the same problem, and I solve using another logic, and works fine:

I remove de partial view (cshtml) and change the signature on Controller to return a Chart type:

        public Chart GetGrafico(int dias, int idDepartamento, int idPgto)
    {
        try
        {
            int idEmpresa = login.GetUsuario(System.Web.HttpContext.Current.User.Identity.Name).IdEmpresa;
            DateTime inicial = DateTime.Today.Date;
            DateTime final = inicial.AddDays(dias);
            var grafico = service.GetGrafico(inicial, final, idEmpresa, idDepartamento, idPgto);

            Chart myChart = new Chart(800, 600)
                .AddTitle("Vencimentos futuros")
                .AddSeries(
                name: "Vencimentos",
                xValue: grafico.Select(x => x.Dia).ToArray(),
                yValues: grafico.Select(x => x.Valor).ToArray()).Write();                 

            return myChart;
        }
        catch (Exception)
        {
            throw;
        }
    }

Then in the Index.cshtml I just add a image tag:

<img id="img" />

<script type="text/javascript">
$('#gerarGrafico').click(function () {
    $('#img').attr('src', '/Erp/Grafico/GetGrafico?dias=30&idDepartamento=0&idPgto=0');
});

So now when a click in the button, the jQuery function just set the attr src of image, it works fine.

Upvotes: 1

Related Questions