前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="echartstest.aspx.cs" Inherits="ModuleWorkFlow.test.echartstest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
</form>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" >
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
myChart.setOption({
title: {
text: '异步数据加载示例'
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: []
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: []
}]
});
var names = []; //类别数组(实际用来盛放X轴坐标值)
var nums = []; //销量数组(实际用来盛放Y坐标值)
$.ajax({
type: "post",
async: false,
url: "echartstest.aspx/showechart",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
success: function (result) {
if (result) {
var dataObj = eval('(' + result.d + ')');
$.each(dataObj, function (index, obj) {
names.push(obj.Name);
nums.push(obj.Id);
//alert(obj.Id);
//alert(obj.Name);
});
// 使用刚指定的配置项和数据显示图表。
myChart.setOption({
xAxis: {
data: names
},//加载数据图表
series: [{
// 根据名字对应到相应的系列
name: '销量',
type: 'bar',
data: nums
}]
});
}
// myChart.setOption(option);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
})
</script>
</html>
后端代码:
using ModuleWorkFlow.Model.System;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ModuleWorkFlow.test
{
public partial class echartstest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string showechart()
{
IList list = new ArrayList();
BrokenReasonInfo bi = new BrokenReasonInfo();
bi.Name = "ceshi";
bi.Id = 1;
list.Add(bi);
bi = new BrokenReasonInfo();
bi.Name = "test";
bi.Id = 2;
list.Add(bi);
return JsonConvert.SerializeObject(list);
}
}
}
显示效果: