講一下echarts的用法,列舉了兩個圖表,一個是單柱圖,一個是多柱圖,至於餅狀圖,只許更改echarts的類型就好了 一、首先是要兩個div,用來存放兩個圖表 二、需要用js從後臺獲取json數據(可以從cs頁面獲取,也可以通過一般處理程式,這裡用的一般處理程式) <%--單個柱狀圖--%> <s ...
講一下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>JS代碼
三、在後臺,從資料庫讀取數據
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