EChartsDemo-饼图01


一、Controller 后台代码(模拟数据)

using MaybeEleven.EchartsDemo.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MaybeEleven.EchartsDemo.Controllers
{
    public class HomeController : Controller
    {
        private int index = 0;

        [HttpGet]
        public ActionResult GetDepartmentEcharts()
        {
            if (Request.IsAjaxRequest())
            {
                List Students = new List
            {
                new Student{
                    Id=++index,
                    Name="张三",
                    CreateTime=DateTime.Now,
                    ModifyTime=null,
                    Department="骨科"
                },
                 new Student{
                    Id=++index,
                    Name="张三",
                    CreateTime=DateTime.Now,
                    ModifyTime=null,
                     Department="眼科"
                },
                  new Student{
                    Id=++index,
                    Name="李四",
                    CreateTime=new DateTime(2020,7,11),
                    ModifyTime=null,
                     Department="眼科"
                },
                   new Student{
                    Id=++index,
                    Name="赵六",
                     CreateTime=new DateTime(2019,7,11),
                    ModifyTime=null
                },
                    new Student{
                    Id=++index,
                    Name="张三是",
                    CreateTime=new DateTime(2018,7,11),
                    ModifyTime=null,
                     Department="妇科"
                },
                     new Student{
                    Id=++index,
                    Name="范德萨发生的",
                    CreateTime=new DateTime(2018,3,11),
                    ModifyTime=null,
                     Department="眼科"
                },
                      new Student{
                    Id=++index,
                    Name="赵六",
                     CreateTime=new DateTime(2019,7,11),
                    ModifyTime=null,
                     Department="耳鼻喉科"
                },
                    new Student{
                    Id=++index,
                    Name="张三是",
                    CreateTime=new DateTime(2003,7,11),
                    ModifyTime=null,
                     Department="耳鼻喉科"
                },
                     new Student{
                    Id=++index,
                    Name="范德萨发生的",
                    CreateTime=new DateTime(2002,3,11),
                    ModifyTime=null,
                     Department="眼科"
                },
                      new Student{
                    Id=++index,
                    Name="赵六",
                     CreateTime=new DateTime(2016,7,11),
                    ModifyTime=null,
                     Department="妇科"
                },
                    new Student{
                    Id=++index,
                    Name="张三是",
                    CreateTime=new DateTime(2015,7,11),
                    ModifyTime=null
                },
                     new Student{
                    Id=++index,
                    Name="范德萨发生的",
                    CreateTime=new DateTime(2018,3,11),
                    ModifyTime=null,
                     Department="骨科"
                },
            };

                var data = Students.GroupBy(g => g.CreateTime.Year)
                    .Select(g => new EchartsDepartmentEntity
                    {
                        value = g.Count(),
                        name = g.Key.ToString()
                    }).OrderBy(x => x.name);
                return Json(new { data = data }, JsonRequestBehavior.AllowGet);
            }

            return View();
        }

      
    }
}

二、实体类

 1 using System;
 2 
 3 namespace MaybeEleven.EchartsDemo.Models
 4 {
 5     public class Student
 6     {
 7 
 8         public int Id { get; set; }
 9 
10         public string Name { get; set; }
11 
12         public DateTime CreateTime { get; set; }
13 
14         public DateTime? ModifyTime { get; set; }
15 
16         public string Department { get; set; }
17     }
18 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace MaybeEleven.EchartsDemo.Models
 7 {
 8     public class EchartsEntity
 9     {
10         public string name { get; set; }
11         public dynamic value { get; set; }
12     }
13 
14     public class EchartsDepartmentEntity : EchartsEntity
15     {
16         public string category { get; set; }
17     }
18 }

三、渲染视图(ECharts5+jQuery 插件自己准备)+function返回echarts中需要的数据

@{
    Layout = null;
}

"en">

    "UTF-8" />
    "X-UA-Compatible" content="IE=edge" />
    "viewport" content="width=device-width, initial-scale=1.0" />
    年手术例数统计
    
    
    


    
"container">