饼图问题在控制器中使用

时间:2014-09-09 17:56:24

标签: c# asp.net highcharts

我需要用HighCharts绘制一个饼图。我在我的控制器中做到了这一点:

class SimpleClass
{
    public string x { set; get; }
    public int y { set; get; }
}

public ActionResult Index()
{

    var multas = from multa in db.Multa select multa;
    int MultaSinPago = multas.Where(s => s.Estado == "sin pago").Count();
    int MultaPagado = multas.Where(s => s.Estado == "Pagado").Count();


    var results = new List<SimpleClass>();
    results.Add(new SimpleClass { x = "Sin Pago", y = MultaSinPago });
    results.Add(new SimpleClass { x = "Pagado", y = MultaPagado });


    //Create chart Model

    var chart1 = new Highcharts("Chart1");
    chart1
        .InitChart(new Chart() { DefaultSeriesType = ChartTypes.Pie })
        .SetTitle(new Title() { Text = "Multas" })
        .SetSeries(new[] { new Series { Data = new List<SimpleClass>(results) } });

    //pass Chart1Model using ViewBag
    ViewBag.Chart1Model = chart1;

    return View();
}

Data = new List<SimpleClass>(results)不好的地方,我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!..

            var multas = from multa in db.Multa select multa;
            int MultaSinPago = multas.Where(s => s.Estado == "sin pago").Count();
            int MultaPagado = multas.Where(s => s.Estado == "Pagado").Count();
            TempData["CountMulta"] = (MultaPagado + MultaSinPago).ToString();
            Highcharts chart = new Highcharts("chart")
                .SetTitle(new Title() { Text = "Multas"})
                .InitChart(new Chart())
                .SetPlotOptions(new PlotOptions
                {
                    Pie = new PlotOptionsPie
                    {
                        AllowPointSelect = true,
                        Cursor = Cursors.Pointer,
                        ShowInLegend = true,
                        Events = new PlotOptionsPieEvents { Click = "function(event) { alert('The slice was clicked!'); }" },
                        Point = new PlotOptionsPiePoint { Events = new PlotOptionsPiePointEvents { LegendItemClick = "function(event) { if (!confirm('Do you want to toggle the visibility of this slice?')) { return false; } }" } }
                    }
                })
                .SetSeries(new Series
                {
                    Type = ChartTypes.Pie,
                    Name = "Cantidad",
                    Data = new Data(new object[]
                                    {
                                        new object[] { "Pagado", MultaPagado},
                                        new object[] { "Sin pago", MultaSinPago}
                                    })
                });
               ViewBag.Chart1Model = chart;

            return View();

希望这对某人有帮助! :)