tnblog
首页
视频
资源
登录

图形化报表

5146人阅读 2019/11/30 16:05 总访问:185886 评论:0 收藏:0 手机
分类: .Net

//MVC 实现引进jquery-1.9.1.min.js,highcharts.js

@{

ViewBag.Title = “Index3”;

}




  1. <script src="~/Content/jquery-1.9.1.min.js"></script>
  2. <script src="~/Content/highcharts/js/highcharts.js"></script>
  3. <script type="text/javascript">
  4.     //Highcharts.setOptions({
  5.     //    colors: ["#ffabcd", "#abcdff", "#aacc66"]
  6.     //});
  7.     $(function ({
  8.         var parms = {
  9.             chart: {
  10.                 type'spline'
  11.             },
  12.             title: {
  13.                 text'每日平均温度',
  14.                 x-20 //center
  15.             },
  16.             subtitle: {
  17.                 text'来源: 中国气象局',
  18.                 x-20
  19.             },
  20.             xAxis: {
  21.                 categories: ['一月''二月''三月''四月''五月''六月',
  22.                     '七月''八月''九月''十月''十一月''十二月']
  23.             },
  24.             yAxis: {
  25.                 title: {
  26.                     text'温度 (°C)'
  27.                 },
  28.                 plotLines: [{
  29.                     value0,
  30.                     width1,
  31.                     color'#808080'
  32.                 }]
  33.             },
  34.             tooltip: {
  35.                 valueSuffix'°C'
  36.             },
  37.             legend: {
  38.                 layout'vertical',
  39.                 align'right',
  40.                 verticalAlign'middle',
  41.                 borderWidth0
  42.             },
  43.             plotOptions: {
  44.                 line: {
  45.                     dataLabels: {
  46.                         enabledtrue
  47.                     },
  48.                     enableMouseTrackingfalse
  49.                 },
  50.                 column: {
  51.                     dataLabels: {
  52.                         enabledtrue
  53.                     },
  54.                     enableMouseTrackingfalse
  55.                 }
  56.             },
  57.             series: []
  58.         };
  59. //Ajax的get请求获取具体的数据
  60.         $.get('http://localhost:53782/MSChart/GetData'function (data{
  61.             console.log(data);
  62.             parms.series = data;
  63.             $('#container').highcharts(parms);//把数据填充到容器里,最终输出
  64.         });
  65.     });
  66. </script>
  1.   
  2. <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

1

//后台实现

数据库设计


create table City

(

Id int primary key identity(1,1),

CityName nvarchar(64)

)


select * from City


create table Wather

(

Id int primary key identity(1,1),

Y decimal(4,2),

X int,

CityId int

)

–drop table Wather

select * from Wather where CityId in (1,2)


insert City values(‘重庆’)

insert City values(‘西永’)

insert City values(‘北京’)


insert Wather values(90,1,1)

insert Wather values(99.5,5,1)

insert Wather values(55.7,9,1)

insert Wather values(15.7,10,1)

insert Wather values(-15.7,11,1)

insert Wather values(67.4,1,1)

insert Wather values(45.5,5,1)

insert Wather values(85.7,3,1)

insert Wather values(95.4,0,1)

insert Wather values(-5.27,2,1)

insert Wather values(99.4,0,1)

insert Wather values(-3.7,3.2,1)


insert Wather values(-50,6,2)

insert Wather values(-9.5,8,2)

insert Wather values(-5.7,10,2)

insert Wather values(-10,6,2)

insert Wather values(-9.5,8,2)

insert Wather values(-2.3,3,2)


insert Wather values(88.5,5,2)

insert Wather values(92.7,9,2)

insert Wather values(92,9,2)

insert Wather values(82.5,5,2)

insert Wather values(45.7,2,2)

insert Wather values(62,3,2)


insert Wather values(20,1,3)

1

insert Wather values(26,2,3)

insert Wather values(-10,3,3)

insert Wather values(40,1,3)

insert Wather values(25,5,3)

insert Wather values(-50,8,3)

insert Wather values(83,1,3)

insert Wather values(56.7,4.3,3)

insert Wather values(-10,9.1,3)

insert Wather values(40,6.4,3)

insert Wather values(89,4.9,3)

insert Wather values(-70,6.7,3)


//视图如下:

//调用数据库

控制器内实现:

  1. public ActionResult Index3()
  2. {
  3.         return View();
  4.     }
  5.     public ActionResult GetData()
  6.     {
  7.         //组装好前段需要的数据格式
  8.         List<HighChatModel> highChatModels = new List<HighChatModel>();
  9.         CityDAL cityDAL = new CityDAL();
  10.         WatherDAL watherDAL = new WatherDAL();
  11.         List<City> cities = cityDAL.GetCities();
  12.         //筛选所有需要返回数据的城市id
  13.         List<int?> cityIds = cities.Select(a => (int?)a.Id).ToList();
  14.         List<Wather> needWathers = watherDAL.GetWatherByCityIds(cityIds);
  15.         foreach (City item in cities)
  16.         {
  17.             HighChatModel highChatModel = new HighChatModel();
  18.             highChatModel.name = item.CityName;
  19.             //通过城市id拿到城市对应的温度
  20.             List<Wather> wathers = needWathers.Where(a => a.CityId == item.Id).ToList();
  21.             //组装好数据
  22.             List<decimal?> rwather = new List<decimal?>();
  23.             foreach (Wather wather in wathers)
  24.             {
  25.                 rwather.Add(wather.Y);
  26.             }
  27.             highChatModel.data = rwather;
  28.             highChatModels.Add(highChatModel);
  29.         }
  30.         return Json(highChatModels, JsonRequestBehavior.AllowGet);
  31.     }


 

EF:实体层的应用

  1. public class WatherDAL
  2. {
  3.     public List<Wather> GetWather()
  4.     {
  5.         weitherEntities mydbEntities = new weitherEntities();
  6.         List<Wather> cities = mydbEntities.Wather.ToList();
  7.         return cities;
  8.     }
  9.     public List<Wather> GetWatherByCity(int cityId)
  10.     {
  11.         weitherEntities mydbEntities = new weitherEntities();
  12.         List<Wather> cities = mydbEntities.Wather.Where(a => a.CityId == cityId).ToList();
  13.         return cities;
  14.     }
  15.     public List<Wather> GetWatherByCityIds(List<int?> cityIds)
  16.     {
  17.         weitherEntities mydbEntities = new weitherEntities();
  18.         List<Wather> cities = mydbEntities.Wather.Where(a => cityIds.Contains(a.CityId)).ToList();
  19.         return cities;
  20.     }
  21. }

 

调用不同类型最终实现效果不同: type: ‘spline’//spline ——— 样条曲线 column ————柱型 pie — 饼 line —线状 area – 区域图

//areaspline — 面积样条曲线 bar — 两边展开 scatter ---- 点型

1.spline ——— 样条曲线

2.column ————柱型


3. pie — 饼状

4.line —线状

5.area – 区域图

6.areaspline — 面积样条曲线

7.bar — 两边展开

8.scatter ---- 点型


//基本就这些了


评价
人若没梦想,那跟咸鱼有啥样
排名
20
文章
32
粉丝
7
评论
21
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
这个世界很大,告别的方式有多少种,人生的正确答案就有多少个。