(一) 使用工具 visual studio 2017;Web开发:asp.net
(代码中的js引用路径以及ajax方法调用的url,记得修改哦)
(二) 准备工作(此处写给和我一样小白)
1.动态从后台获取数据,需使用Ajax获取后台Json,为此我们需要做一些准备工作,安装两个包(在vs的NuGet包管理)
一个json的包,一个mvc的包。
2.添加必要的js。
ECharts和jQuery均可在各自官网下载到。Echarts依赖zrender,但好像项目中是否引用并不影响。原谅我对Echarts还只是初识,理解不够深刻。
(三) 开始吧~
然后现在开始我们的小练习。
先准备一个Series类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Series 的摘要说明
/// </summary>
public class Series
{
public string name;
public string type;
public int yAxisIndex;
public List<double> data;
}
Series
然后我们添加一个web服务
是的,就是这个~ 我给的名字叫 wsComm
(VS很智能的告诉我要取消如下注释,然而我一开始仍然没有看到,瞎了大概)
然后我们需要在这里面写一个webmethod,以便在前台进行数据获取(关于webmethod的问题,这里不做详述)。
/// <summary>
/// wsComm 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class wsComm : System.Web.Services.WebService
{ /// <summary>
/// ECharts图表数据获取
/// </summary>
/// <returns></returns>
[WebMethod]
public JsonResult getdataechart()
{
//考虑到图表的category是字符串数组 这里定义一个string的List
List<string> categoryList = new List<string>();
//考虑到Echarts图表需要设置legend内的data数组为series的name集合这里需要定义一个legend数组
List<string> legendList = new List<string>();
//考虑到图表的series数据为一个对象数组 这里额外定义一个series的类
List<Series> seriesList = new List<Series>();
//设置legend数组
legendList.Add("月支出金额"); //这里的名称必须和series的每一组series的name保持一致
legendList.Add("月工作量"); //这里的名称必须和series的每一组series的name保持一致
//填写第一个Series
//定义一个Series对象
Series seriesObj = new Series();
seriesObj.name = "月支出金额";
seriesObj.type = "line"; //线性图呈现
seriesObj.data = new List<double>(); //先初始化 不初始化后面直直接data.Add(x)会报错 //模拟两组数据,都放在二组数组中。该数据你可以从数据库中获取,关于如何从后台数据库进行读取,本文不再详述。
string[,] MonthCost = new string[,] { { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" } };
string[,] ProjectVal = new string[,] { { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" }, { "", "" } };
//设置数据
for (int i = ; i < ; i++)
{
//加入category刻度数组
categoryList.Add(MonthCost[i, ]);
//加入数据值series序列数组 这里提供为了效果只提供一组series数据好了
seriesObj.data.Add(Convert.ToDouble(MonthCost[i, ])); //数据依次递增
}
seriesList.Add(seriesObj);
//填写第二个Series
seriesObj = new Series();
seriesObj.name = "月工作量";
seriesObj.type = "bar"; //线性图呈现
seriesObj.yAxisIndex = ;
seriesObj.data = new List<double>(); //先初始化 不初始化后面直直接data.Add(x)会报错
//设置数据
for (int i = ; i < ; i++)
{
seriesObj.data.Add(Convert.ToDouble(ProjectVal[i, ])); //数据依次递增
}
seriesList.Add(seriesObj);
//最后调用相关函数将List转换为Json
//因为我们需要返回category和series、legend多个对象 这里我们自己在new一个新的对象来封装这两个对象
JsonResult json = new JsonResult();
var newObj = new
{
category = categoryList,
series = seriesList,
legend = legendList
};
json.Data = JsonConvert.SerializeObject(newObj);
return json;
}
}
wsComm
前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/jscript" src="../JS/jquery-3.3.1.js"></script>
<script type="text/jscript" src="../JS/echarts.js"></script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="main" style="height: 400px"></div>
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
//图表显示提示信息
myChart.showLoading({
text: "图表数据正在努力加载..."
});
//定义图表options
var options = {
title: {
text: "测试报表1",
},
//右侧工具栏
toolbox: {
show: true,
feature: {
mark: { show: true },
dataView: { show: true, readOnly: false },
magicType: { show: true, type: ['line', 'bar'] },
restore: { show: true },
saveAsImage: { show: true }
}
},
tooltip: {
trigger: 'axis'
},
legend: {
data: []
},
calculable: true,
xAxis: [
{
type: 'category',
name: '月份',
data: []
}
],
yAxis: [
{
type: 'value',
name: '金额',
axisLabel: {
formatter: '{value} Y'
},
splitArea: { show: true }
},
{
type: 'value',
name: '工作量',
axisLabel: {
formatter: '{value} M3'
},
splitArea: { show: true }
}
],
series: []
};
//通过Ajax获取数据
$.ajax({
type: "POST",
async: false,
contentType: 'application/json; charset=utf-8',
url: "../wsComm.asmx/getdataechart",
dataType: "json", //返回数据形式为json
success: function (result) {
var obj = JSON.parse(result.d.Data); //一定要注意大小写,本语句中,一直把Data写成data,总是取不出数据,耽误了半天
if (result) {
//将返回的category和series对象赋值给options对象内的category和series
//因为xAxis是一个数组 这里需要是xAxis[i]的形式
options.yAxis[].data = obj.value;
options.xAxis[].data = obj.category;
options.series= obj.series;
options.legend.data = obj.legend;
myChart.hideLoading();
myChart.setOption(options);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
</script>
</form>
</body>
</html>
html
嗯,然后就完成了。
参考原文:https://blog.****.net/guoxy_nb/article/details/78943185