echarts呈现数据表图形
2018-06-22 07:58:23来源:未知 阅读 ()
讲一下echarts的用法,列举了两个图表,一个是单柱图,一个是多柱图,至于饼状图,只许更改echarts的类型就好了
一、首先是要两个div,用来存放两个图表
<div class="div-frm" style="height: 275px; font-family: Microsoft YaHei, Verdana, Arial;"> <div id="barone" style="width: 100%; height: 240px; float: left; font-family: Microsoft YaHei, Verdana, Arial;"></div> </div> <div style="height: 240px; font-family: Microsoft YaHei, Verdana, Arial;"> <div id="barthree" class="chart-container" style="width: 100%; height: 240px; float: left; font-family: Microsoft YaHei, Verdana, Arial;"></div> </div>
二、需要用js从后台获取json数据(可以从cs页面获取,也可以通过一般处理程序,这里用的一般处理程序)
<%--单个柱状图--%> <script type="text/javascript"> //初始化 var dom = document.getElementById("barone"); var myChart = echarts.init(dom); var app = {}; option = null; markPoint = null; //图表使用 var option = { title: { text: '成绩图' }, tooltip: { trigger: 'axis' }, toolbox: { show: true, feature: { mark: { show: true }, dataView: { show: true, readOnly: false }, magicType: { show: true, type: ['line', 'bar'] }, restore: { show: true }, saveAsImage: { show: true } } }, calculable: true, xAxis: [ { type: 'category', data: [""] } ], yAxis: [ { type: 'value' } ], series: [ { type: 'bar', data: [""], //数据显示的地方,ajax markPoint: { data: [ { type: 'max', name: '最大值' }, { type: 'min', name: '最小值' } ] }, markLine: { data: [ { type: 'average', name: '平均值' } ] }, }, ], dataZoom: [ { show: false, start: 0, end: 100 } ] }; if (option && typeof option === "object") { myChart.setOption(option, true); } //获取数据 $.ajax({ url: "echartsData/barone.ashx", //数据来源 data: { type: "bar" }, cache: false, async: false, dataType: 'json', success: function (data) { if (data) { myChart.setOption({ xAxis: [{ data: data.xAxis }], series: [ { data: data.series, itemStyle: { //显示柱状图顶部的数字 normal: { color: '#2d9fd8', label: { show: true, position: 'top', textStyle: { baseline: "bottom" } } } }, barWidth: 20 } ] }); } }, error: function (msg) { alert("系统发生错误1"); } }); </script> <%-- 多个柱状图 --%> <script type="text/javascript"> var dom = document.getElementById("barthree"); var myChart2 = echarts.init(dom); var app = {}; option = null; var option = { title: { text: '成绩表' }, tooltip: { trigger: 'axis' }, legend: { data: ['数学', '语文', '英语'] }, toolbox: { show: true, feature: { mark: { show: true }, dataView: { show: true, readOnly: false }, magicType: { show: true, type: ['line', 'bar'] }, restore: { show: true }, saveAsImage: { show: true } } }, calculable: true, xAxis: [ { type: 'category', data: [""] } ], yAxis: [ { type: 'value' } ], series: [ { name: '访问来源', type: 'bar', data: [""], itemStyle: { normal: { color: '#2d9fd8', label: { show: true, position: 'top', textStyle: { baseline: "bottom" } } } }, markPoint: { data: [ { type: 'max', name: '最大值' }, { type: 'min', name: '最小值' } ] }, markLine: { data: [ { type: 'average', name: '平均值' } ] }, } ], dataZoom: [ { show: false, start: 0, end: 100 } ] }; if (option && typeof option === "object") { myChart2.setOption(option, true); } $.ajax({ url: "echartsData/barthree.ashx", data: { type: "bar" }, cache: false, async: false, dataType: 'json', success: function (data) { if (data) { myChart2.setOption({ legend: [{ data: data.legend }], xAxis: [{ data: data.xAxis }], series: data.series }); } }, error: function (msg) { alert("系统发生错误"); } }); </script>
三、在后台,从数据库读取数据
static string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; JavaScriptSerializer jsS = new JavaScriptSerializer(); List<object> seriesList = new List<object>(); List<string> xAxisList = new List<string>(); string result = ""; public void ProcessRequest(HttpContext context) { string command = context.Request["type"]; switch (command) { case "bar": GetOverView(context); break; }; } public void GetOverView(HttpContext context) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = conStr; conn.Open(); string sql = "select * from Achievement"; SqlDataAdapter myda = new SqlDataAdapter(sql, conStr); DataTable dt = new DataTable(); // 实例化数据表 myda.Fill(dt); // 保存数据 foreach (DataRow dr in dt.Rows) { xAxisList.Add(dr["Name"].ToString()); seriesList.Add(dr["Math"].ToString()); } var newObj = new { xAxis = xAxisList, series = seriesList }; jsS = new JavaScriptSerializer(); result = jsS.Serialize(newObj); context.Response.Write(result); conn.Close(); // 关闭数据库连接 } }
static string conStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; JavaScriptSerializer jsS = new JavaScriptSerializer(); List<object> seriesList = new List<object>(); List<string> xAxisList = new List<string>(); List<string> legendList = new List<string>(); string result = ""; public void ProcessRequest(HttpContext context) { string command = context.Request["type"]; switch (command) { case "bar": GetOverView(context); break; }; } public void GetOverView(HttpContext context) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = conStr; conn.Open(); string sql = "select * from Achievement"; SqlDataAdapter myda = new SqlDataAdapter(sql, conStr); DataTable dt = new DataTable(); // 实例化数据表 myda.Fill(dt); // 保存数据 legendList.Add("数学"); legendList.Add("语文"); legendList.Add("英语"); //设置对应的Series信息 Series UVSeriesObj = new Series(); UVSeriesObj.name = "数学"; UVSeriesObj.type = "bar"; //图呈现 UVSeriesObj.data = new List<object>(); Series UIPSeriesObj = new Series(); UIPSeriesObj.name = "语文"; UIPSeriesObj.type = "bar"; //图呈现 UIPSeriesObj.data = new List<object>(); Series PVSeriesObj = new Series(); PVSeriesObj.name = "英语"; PVSeriesObj.type = "bar"; //图呈现 PVSeriesObj.data = new List<object>(); foreach (DataRow dr in dt.Rows) { xAxisList.Add(dr["Name"].ToString()); UVSeriesObj.data.Add(dr["Math"]); UIPSeriesObj.data.Add(dr["Chinese"]); PVSeriesObj.data.Add(dr["English"]); } seriesList.Add(UVSeriesObj); seriesList.Add(UIPSeriesObj); seriesList.Add(PVSeriesObj); var newObj = new { legend = legendList, //三门学科 xAxis = xAxisList, series = seriesList }; jsS = new JavaScriptSerializer(); result = jsS.Serialize(newObj); context.Response.Write(result); } } public bool IsReusable { get { return false; } } class Series { /// <summary> /// series序列组名称 /// </summary> public string name { get; set; } /// <summary> /// series序列组呈现图表类型(line、column、bar等) /// </summary> public string type { get; set; } /// <summary> /// series序列组的数据为数据类型数组 /// </summary> public List<object> data { get; set; } }
echarts官方api文档:http://echarts.baidu.com/echarts2/doc/example.html
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- .net 数据表格显示控件介绍 2020-03-03
- 总结asp.net 页面逐步呈现的方法 2019-09-02
- ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及 2018-06-22
- asp.net MVC项目开发之统计图echarts饼形图(二) 2018-06-22
- asp.net MVC项目开发之统计图echarts柱状图(一) 2018-06-22
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash