一.项目介绍
利用前端Highchart,以及ajax向后台获取数据,绘制动态柱状图。hightchart其他实例可查看官网文档。[Highchart](https://www.highcharts.com.cn)
二.准备工作
先搭建项目以及所要访问的数据库及数据,这里不做赘述。
然后编写Controller代码,用于提供数据。

 [HttpPost]
public JsonResult GetDataList(int BeformDays, int Type)
{
JsonResultData json = new JsonResultData();
json.datas.Add(new Detail {
name="测试总数"
});
json.datas.Add(new Detail
{
name = "测试失败"
});
json.datas.Add(new Detail
{
name = "测试成功"
});
var list=_dataContext.Responses.ToList();
if (Type == )
{
foreach (var item in list)
{
json.names.Add(item.ClientName);
//json.testSuccessNum.Add(item.TestSuccessNum);
json.datas.FirstOrDefault(o => o.name == "测试总数").data .Add(item.TestNum);
json.datas.FirstOrDefault(o => o.name == "测试成功").data.Add(item.TestSuccessNum);
json.datas.FirstOrDefault(o => o.name == "测试失败").data.Add(item.TestNum-item.TestSuccessNum);
}
}
return Json(json);
}

二.前端代码
先引用需要用到的js包,如下:

```

<script src="http://cdn.highcharts.com.cn/highcharts/highcharts.js"></script>
<script src="https://code.highcharts.com.cn/jquery/jquery-1.8.3.min.js"></script>
<script src="https://code.highcharts.com.cn/highcharts/modules/exporting.js"></script>
<script src="https://code.highcharts.com.cn/highcharts/modules/data.js"></script>
<script src="https://code.highcharts.com.cn/highcharts/modules/series-label.js"></script>
<script src="https://code.highcharts.com.cn/highcharts/modules/oldie.js"></script>
<script src="https://code.highcharts.com.cn/highcharts-plugins/highcharts-zh_CN.js"> </script>
<script src="https://code.highcharts.com.cn/jquery/jquery-1.8.3.min.js"></script>  

js代码如下:

 <script type="text/javascript">
//DrawHistogram();
$(document).ready(function () {
DrawHistogram("container");//页面加载,异步运行该方法
});
setInterval(function () {
DrawHistogram("container");
}, 1000 * 60);//定时刷新数据
function DrawHistogram(id) {
var chart;
//获取数据
$.ajax({
async: false,
type: 'post',
datatype: 'json',
url: '/home/getdatalist',
data: { BeformDays: 7, Type: 1 },
success: function (Data) {
Name = Data.names;
//highchants样式渲染
chart = new Highcharts.Chart({
chart: {
renderTo: id,//放置图表的容器
plotBackgroundColor: null,//绘图背景颜色
plotBorderWidth: null,//绘图边框宽度
defaultSeriesType: 'column'//图表类型样式line, spline, area, areaspline, column, bar, pie , scatter这些样式随你选
},
title: {
text: '柱状图统计'
},
subtitle: {
text: ''//副标题
},
xAxis: {//X轴数据
categories: Data.names,//请求到的json数据
labels: {
rotation: 0, //字体倾斜
align: 'right',
style: { font: 'normal 13px 宋体' }
}
},
yAxis: {//Y轴显示文字
title: {
text: '个数/个'
}
},
//点击事件
tooltip: {
enabled: true,//是否显示tooltip
formatter: function () {
return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + Highcharts.numberFormat(this.y, 1);
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: true//是否显示title
}
},
series: Data.datas//json
});
//setInterval("getjson()", 3000);
}
});
}
</script> ```
在html页面添加容器,html代码如下: ```
<body>
<!--存放内容-->
<div id="container">
</div>
</body>

效果图:

最新文章

  1. 运动规划 (Motion Planning): MoveIt! 与 OMPL
  2. C——第一阶段结束
  3. HTML5学习(六)---------SVG 与Canvas
  4. 06_例子讲解:rlPlanDemo
  5. WPF界面设计技巧(4)—自定义列表项样式
  6. Linux/UNIX先进I/O
  7. Swift三元条件运算
  8. h5 测试关注点
  9. inner join 与 left join 之间的区别
  10. python之路--反射
  11. base64encode 编码原理
  12. [Java]Spring框架
  13. Ubuntu16.04配置apache+php+mysql
  14. 如何实现字符串的翻转,不用php库函数翻转字符串
  15. BZOJ2844: albus就是要第一个出场(线性基)
  16. [转].Net Core上用于代替System.Drawing的类库
  17. Python urllib模块详解
  18. 24个节点测试Linux VPS/服务器速度一键脚本使用 附服务器配置
  19. LightOJ - 1027 A Dangerous Maze —— 期望
  20. oracle 代码块

热门文章

  1. django-URL匹配(二)
  2. Java 干货之深入理解Java泛型
  3. 学习笔记07axps页面
  4. 学习笔记15_ASP母版页
  5. [知识图谱]利用py2neo从Neo4j数据库获取数据
  6. Data Compression Category
  7. centos创建kvm虚拟机
  8. Maven配置setting.xml详细说明
  9. 考试T3麻将
  10. 吐血推荐珍藏的Visual Studio Code插件