当前位置:   article > 正文

asp.net使用echarts展示图表数据_echart c#

echart c#

百度echarts开发插件包下载地址:http://download.csdn.net/detail/taomanman/8850699





  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EchartDemo.aspx.cs" Inherits="AT.Web.Demo.EchartDemo" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Echart图表示例</title>
  7. <script src="../Themes/Scripts/jquery-1.8.2.min.js"></script>
  8. <link href="../Themes/Styles/Site.css" rel="stylesheet" type="text/css" />
  9. <script src="../Themes/Scripts/FunctionJS.js" type="text/javascript"></script>
  10. <script type="text/javascript">
  11. //初始化
  12. $(function () {
  13. $('#table2').hide();
  14. })
  15. //点击切换面板
  16. var IsFixedTableLoad = 1;
  17. function panel(obj) {
  18. if (obj == 1) {
  19. $('#table1').show();
  20. $('#table2').hide();
  21. } else if (obj == 2) {
  22. $('#table1').hide();
  23. $("#table2").show();
  24. }
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <form id="form1" runat="server">
  30. <div class="frmtop">
  31. <table style="padding: 0px; margin: 0px; height: 100%;" cellpadding="0" cellspacing="0">
  32. <tr>
  33. <td id="menutab" style="vertical-align: bottom;">
  34. <div id="tab0" class="Tabsel" οnclick="GetTabClick(this);panel(1)">
  35. 能耗动态
  36. </div>
  37. <div id="tab1" class="Tabremovesel" οnclick="GetTabClick(this);panel(2);">
  38. 填报动态
  39. </div>
  40. </td>
  41. </tr>
  42. </table>
  43. </div>
  44. <div class="div-frm" style="height: 275px;">
  45. <%--能耗动态--%>
  46. <div id="table1">
  47. <div id="divEnergy" style="width: 610px; height: 220px;"></div>
  48. </div>
  49. <%--填报动态--%>
  50. <div id="table2">
  51. <div id="divReport" style="width: 750px; height: 250px;"></div>
  52. </div>
  53. </div>
  54. </form>
  55. </body>
  56. </html>
  57. <script src="../Themes/Scripts/echarts/echarts.js"></script>
  58. <script src="../Themes/Scripts/echarts/theme/macarons.js"></script>
  59. <script type="text/javascript">
  60. var cityClick = '';
  61. function GetUnitEnergy(unitName) {
  62. GetCityEnergy(unitName, 2);
  63. }
  64. require.config({
  65. paths: {
  66. echarts: '../Themes/Scripts/echarts'
  67. }
  68. });
  69. require(
  70. [
  71. 'echarts',
  72. 'echarts/chart/bar',
  73. 'echarts/chart/line',
  74. 'echarts/chart/pie',
  75. 'echarts/chart/funnel'
  76. ],
  77. DrawEChart //异步加载的回调函数绘制图表
  78. );
  79. var myEnergyChart;
  80. var myReportChart;
  81. //创建ECharts图表方法
  82. function DrawEChart(ec) {
  83. //--- 折柱 ---
  84. myEnergyChart = ec.init(document.getElementById('divEnergy'), macarons);
  85. //定义图表options
  86. var options = {
  87. title: {
  88. text: "",
  89. subtext: ""
  90. },
  91. /*color: ['#0099FF', '#00CC00', '#008080', '#CC6600', '#CC00CC', '#0033CC','#003300', '#FF0000'],*/
  92. tooltip: {
  93. trigger: 'axis'
  94. },
  95. legend: {
  96. data: [" "]
  97. },
  98. toolbox: {
  99. show: true,
  100. feature: {
  101. mark: { show: false },
  102. dataView: { show: false, readOnly: false },
  103. magicType: { show: true, type: ['line', 'bar'] },
  104. restore: { show: true },
  105. saveAsImage: { show: false }
  106. }
  107. },
  108. grid: {
  109. x: 60,
  110. y: 30,
  111. x2: 5,
  112. y2: 30
  113. },
  114. calculable: true,
  115. xAxis: [
  116. {
  117. type: 'category',
  118. data: [" "]
  119. }
  120. ],
  121. yAxis: [
  122. {
  123. type: 'value',
  124. axisLabel: {
  125. fomatter: "."
  126. },
  127. splitArea: { show: true }
  128. }
  129. ],
  130. series: [{
  131. name: " ",
  132. type: "line",
  133. data: [0]
  134. }]
  135. };
  136. //选择一个空图表
  137. myEnergyChart.setOption(options);
  138. myReportChart = ec.init(document.getElementById('divReport'), macarons);
  139. //选择一个空图表
  140. myReportChart.setOption(options);
  141. // 默认加载填报
  142. GetReport("1");
  143. // 默认加载省直
  144. GetUnitEnergy('34');
  145. }
  146. ///点击按钮获取图表数据采用ajax方式
  147. function GetCityEnergy(cityCode, level) {
  148. //获得图表的options对象
  149. var options = myEnergyChart.getOption();
  150. //通过Ajax获取数据
  151. $.ajax({
  152. type: "post",
  153. async: false, //同步执行
  154. url: "GetChartData.aspx?type=energyData",
  155. dataType: "json", //返回数据形式为json
  156. success: function (result) {
  157. if (result) {
  158. //将返回的category和series对象赋值给options对象内的category和series
  159. //因为xAxis是一个数组 这里需要是xAxis[i]的形式
  160. options.xAxis[0].data = result.category;
  161. options.series = result.series;
  162. options.legend.data = result.legend;
  163. myEnergyChart.setOption(options);
  164. myEnergyChart.refresh();
  165. }
  166. },
  167. error: function (errorMsg) {
  168. alert("图表数据加载失败!");
  169. }
  170. });
  171. }
  172. ///点击按钮获取图表数据采用ajax方式
  173. function GetReport(code) {
  174. //获得图表的options对象
  175. var options = myReportChart.getOption();
  176. //通过Ajax获取数据
  177. $.ajax({
  178. type: "post",
  179. async: false, //同步执行
  180. url: "GetChartData.aspx?type=reportData",
  181. dataType: "json", //返回数据形式为json
  182. success: function (result) {
  183. if (result) {
  184. //将返回的category和series对象赋值给options对象内的category和series
  185. //因为xAxis是一个数组 这里需要是xAxis[i]的形式
  186. //myReportChart.clear();
  187. options.xAxis[0].data = result.category;
  188. options.series = result.series;
  189. options.legend.data = result.legend;
  190. myReportChart.setOption(options);
  191. }
  192. },
  193. error: function (errorMsg) {
  194. alert("图表数据加载失败!");
  195. }
  196. });
  197. }
  198. //初始加载图表数据
  199. $(function () {
  200. GetCityEnergy("", "");
  201. GetUnitEnergy("");
  202. });
  203. </script>


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace AT.Web.Demo
  8. {
  9. public partial class EchartDemo : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. }
  15. }


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetChartData.aspx.cs" Inherits="AT.Web.Demo.GetChartData" %>

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. namespace AT.Web.Demo
  11. {
  12. public partial class GetChartData : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. string type = Request["type"];
  17. if (!string.IsNullOrEmpty(type))
  18. {
  19. switch (type)
  20. {
  21. case "energyData":
  22. GetEnergyDataAjaxData("", "");
  23. break;
  24. case "reportData":
  25. GetReportDataAjaxData("");
  26. break;
  27. }
  28. }
  29. }
  30. /// <summary>
  31. /// 获得数据且用Json格式数据返回
  32. /// </summary>
  33. private void GetEnergyDataAjaxData(string level, string code)
  34. {
  35. //考虑到图表的category是字符串数组 这里定义一个string的List
  36. List<string> categoryList = new List<string>();
  37. //考虑到图表的series数据为一个对象数组 这里额外定义一个series的类
  38. List<Series> seriesList = new List<Series>();
  39. //考虑到Echarts图表需要设置legend内的data数组为series的name集合这里需要定义一个legend数组
  40. List<string> legendList = new List<string>();
  41. DataTable dt = null;
  42. using (SqlConnection con = new SqlConnection("Data Source=.;User ID=sa;Password=111111;Database=AT_DB;"))
  43. {
  44. string strSQL = "select top 12 * from T_EchartData1";
  45. using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
  46. {
  47. DataSet ds = new DataSet();
  48. adapter.Fill(ds);
  49. dt = ds.Tables[0];
  50. }
  51. }
  52. //Response.Write(dt.Rows.Count+"条数据!");
  53. //设置legend数组
  54. legendList.Add("今年能耗"); //这里的名称必须和series的每一组series的name保持一致
  55. legendList.Add("去年能耗"); //这里的名称必须和series的每一组series的name保持一致
  56. Series thisSeriesObj = new Series();
  57. thisSeriesObj.id = 0;
  58. thisSeriesObj.name = "今年能耗";
  59. thisSeriesObj.type = "line"; //线性图呈现
  60. thisSeriesObj.data = new List<object>();
  61. thisSeriesObj.itemStyle = new itemStyle { normal = new normal { color = "#0099FF" } };
  62. Series lastSeriesObj = new Series();
  63. lastSeriesObj.id = 1;
  64. lastSeriesObj.name = "去年能耗";
  65. lastSeriesObj.type = "line"; //线性图呈现
  66. lastSeriesObj.data = new List<object>();
  67. lastSeriesObj.itemStyle = new itemStyle { normal = new normal { color = "#00CC00" } };
  68. foreach (DataRow dr in dt.Rows)
  69. {
  70. categoryList.Add(dr[0].ToString() + "月");
  71. thisSeriesObj.data.Add(dr[1]);
  72. lastSeriesObj.data.Add(dr[2]);
  73. }
  74. //将sereis对象压入sereis数组列表内
  75. seriesList.Add(thisSeriesObj);
  76. seriesList.Add(lastSeriesObj);
  77. //最后调用相关函数将List转换为Json
  78. //因为我们需要返回category和series、legend多个对象 这里我们自己在new一个新的对象来封装这两个对象
  79. var newObj = new
  80. {
  81. category = categoryList,
  82. series = seriesList,
  83. legend = legendList
  84. };
  85. //Response返回新对象的json数据
  86. Response.Write(JsonConvert.SerializeObject(newObj, Formatting.None));
  87. Response.End();
  88. }
  89. /// <summary>
  90. /// 获得数据且用Json格式数据返回
  91. /// </summary>
  92. private void GetReportDataAjaxData(string code)
  93. {
  94. //考虑到图表的category是字符串数组 这里定义一个string的List
  95. List<string> categoryList = new List<string>();
  96. //考虑到图表的series数据为一个对象数组 这里额外定义一个series的类
  97. List<Series> seriesList = new List<Series>();
  98. //考虑到Echarts图表需要设置legend内的data数组为series的name集合这里需要定义一个legend数组
  99. List<string> legendList = new List<string>();
  100. DataTable dt = null;
  101. using (SqlConnection con = new SqlConnection("Data Source=.;User ID=sa;Password=111111;Database=AT_DB;"))
  102. {
  103. string strSQL = "select * from T_EchartData2";
  104. using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
  105. {
  106. DataSet ds = new DataSet();
  107. adapter.Fill(ds);
  108. dt = ds.Tables[0];
  109. }
  110. }
  111. //Response.Write(dt.Rows.Count + "条数据!");
  112. //设置legend数组
  113. legendList.Add("电"); //这里的名称必须和series的每一组series的name保持一致
  114. legendList.Add("水"); //这里的名称必须和series的每一组series的name保持一致
  115. legendList.Add("燃煤"); //这里的名称必须和series的每一组series的name保持一致
  116. legendList.Add("天然气"); //这里的名称必须和series的每一组series的name保持一致
  117. legendList.Add("汽油"); //这里的名称必须和series的每一组series的name保持一致
  118. legendList.Add("柴油"); //这里的名称必须和series的每一组series的name保持一致
  119. legendList.Add("热力"); //这里的名称必须和series的每一组series的name保持一致
  120. legendList.Add("其他"); //这里的名称必须和series的每一组series的name保持一致
  121. Series dianSeries = new Series();
  122. dianSeries.id = 0;
  123. dianSeries.name = "电";
  124. dianSeries.type = "line"; //线性图呈现
  125. dianSeries.data = new List<object>();
  126. dianSeries.itemStyle = new itemStyle { normal = new normal { color = "#0099FF" } };
  127. Series shuiSeries = new Series();
  128. shuiSeries.id = 1;
  129. shuiSeries.name = "水";
  130. shuiSeries.type = "line"; //线性图呈现
  131. shuiSeries.data = new List<object>();
  132. shuiSeries.itemStyle = new itemStyle { normal = new normal { color = "#00CC00" } };
  133. Series yuanmeiSeries = new Series();
  134. yuanmeiSeries.id = 2;
  135. yuanmeiSeries.name = "燃煤";
  136. yuanmeiSeries.type = "line"; //线性图呈现
  137. yuanmeiSeries.data = new List<object>();
  138. yuanmeiSeries.itemStyle = new itemStyle { normal = new normal { color = "#008080" } };
  139. Series tianranqiSeries = new Series();
  140. tianranqiSeries.id = 3;
  141. tianranqiSeries.name = "天然气";
  142. tianranqiSeries.type = "line"; //线性图呈现
  143. tianranqiSeries.data = new List<object>();
  144. tianranqiSeries.itemStyle = new itemStyle { normal = new normal { color = "#CC6600" } };
  145. Series qiyouSeries = new Series();
  146. qiyouSeries.id = 4;
  147. qiyouSeries.name = "汽油";
  148. qiyouSeries.type = "line"; //线性图呈现
  149. qiyouSeries.data = new List<object>();
  150. qiyouSeries.itemStyle = new itemStyle { normal = new normal { color = "#CC00CC" } };
  151. Series chaiyouSeries = new Series();
  152. chaiyouSeries.id = 5;
  153. chaiyouSeries.name = "柴油";
  154. chaiyouSeries.type = "line"; //线性图呈现
  155. chaiyouSeries.data = new List<object>();
  156. chaiyouSeries.itemStyle = new itemStyle { normal = new normal { color = "#0033CC" } };
  157. Series reliSeries = new Series();
  158. reliSeries.id = 6;
  159. reliSeries.name = "热力";
  160. reliSeries.type = "line"; //线性图呈现
  161. reliSeries.data = new List<object>();
  162. reliSeries.itemStyle = new itemStyle { normal = new normal { color = "#003300" } };
  163. Series qitaSeries = new Series();
  164. qitaSeries.id = 7;
  165. qitaSeries.name = "其他";
  166. qitaSeries.type = "line"; //线性图呈现
  167. qitaSeries.data = new List<object>();
  168. qitaSeries.itemStyle = new itemStyle { normal = new normal { color = "#FF0000" } };
  169. foreach (DataRow dr in dt.Rows)
  170. {
  171. categoryList.Add(string.Format("{0}年{1}月", dr["ReportYear"], dr["ReportMonth"]));
  172. dianSeries.data.Add(string.IsNullOrEmpty(dr["DIAN"].ToString()) ? 0 : dr["DIAN"]);
  173. shuiSeries.data.Add(string.IsNullOrEmpty(dr["SHUI"].ToString()) ? 0 : dr["SHUI"]);
  174. yuanmeiSeries.data.Add(string.IsNullOrEmpty(dr["YUANMEI"].ToString()) ? 0 : dr["YUANMEI"]);
  175. tianranqiSeries.data.Add(string.IsNullOrEmpty(dr["TIANRQ"].ToString()) ? 0 : dr["TIANRQ"]);
  176. qiyouSeries.data.Add(string.IsNullOrEmpty(dr["QIYOU"].ToString()) ? 0 : dr["QIYOU"]);
  177. chaiyouSeries.data.Add(string.IsNullOrEmpty(dr["CHAIYOU"].ToString()) ? 0 : dr["CHAIYOU"]);
  178. reliSeries.data.Add(string.IsNullOrEmpty(dr["RELI"].ToString()) ? 0 : dr["RELI"]);
  179. qitaSeries.data.Add(string.IsNullOrEmpty(dr["QTNY"].ToString()) ? 0 : dr["QTNY"]);
  180. }
  181. //将sereis对象压入sereis数组列表内
  182. seriesList.Add(dianSeries);
  183. seriesList.Add(shuiSeries);
  184. seriesList.Add(yuanmeiSeries);
  185. seriesList.Add(tianranqiSeries);
  186. seriesList.Add(qiyouSeries);
  187. seriesList.Add(chaiyouSeries);
  188. seriesList.Add(reliSeries);
  189. seriesList.Add(qitaSeries);
  190. //最后调用相关函数将List转换为Json
  191. //因为我们需要返回category和series、legend多个对象 这里我们自己在new一个新的对象来封装这两个对象
  192. var newObj = new
  193. {
  194. category = categoryList,
  195. series = seriesList,
  196. legend = legendList
  197. };
  198. //Response返回新对象的json数据
  199. Response.Write(JsonConvert.SerializeObject(newObj, Formatting.None));
  200. Response.End();
  201. }
  202. }
  203. /// <summary>
  204. /// 定义一个Series类 设置其每一组sereis的一些基本属性
  205. /// </summary>
  206. class Series
  207. {
  208. /// <summary>
  209. /// sereis序列组id
  210. /// </summary>
  211. public int id
  212. {
  213. get;
  214. set;
  215. }
  216. /// <summary>
  217. /// series序列组名称
  218. /// </summary>
  219. public string name
  220. {
  221. get;
  222. set;
  223. }
  224. /// <summary>
  225. /// series序列组呈现图表类型(line、column、bar等)
  226. /// </summary>
  227. public string type
  228. {
  229. get;
  230. set;
  231. }
  232. /// <summary>
  233. /// series序列组的itemstyle
  234. /// </summary>
  235. public object itemStyle
  236. {
  237. get;
  238. set;
  239. }
  240. /// <summary>
  241. /// series序列组的数据为数据类型数组
  242. /// </summary>
  243. public List<object> data
  244. {
  245. get;
  246. set;
  247. }
  248. }
  249. class normal
  250. {
  251. /// <summary>
  252. /// color
  253. /// </summary>
  254. public string color
  255. {
  256. get;
  257. set;
  258. }
  259. }
  260. class itemStyle
  261. {
  262. /// <summary>
  263. /// normal
  264. /// </summary>
  265. public object normal
  266. {
  267. get;
  268. set;
  269. }
  270. }
  271. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/114726
推荐阅读