asp.net web +三层架构 实现echarts图表

html

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="echars.aspx.cs" Inherits="ZhPcb.Web.echars" %>

<!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.jsdelivr.net/npm/echarts@5.1.2/dist/echarts.min.js"></script>
     <script src="js/jquery.js"></script>
    <script src="js/jquery-1.11.3.min.js"></script>
</head>
<body>
   <div id="pie-chart" style="width: 800px;height:600px;"></div>
    <script>
        $(document).ready(function () {
            $.ajax({
                type: 'GET',
                url: 'admin/ajax/UserHandler.ashx?param=GetEmployees',
                dataType: 'json',
                success: function (data) {
                    var chartData = data;
                    var myChart = echarts.init(document.getElementById('pie-chart'));
                    myChart.setOption({
                        title: {
                            text: '饼状图',
                            left: 'center',
                        },
                        tooltip: {
                            trigger: 'item',
                            formatter: '{a} <br/>{b}: {c} ({d}%)'
                        },
                        series: [{
                            name: '访问来源',
                            type: 'pie',
                            radius: ['50%', '70%'],
                            avoidLabelOverlap: false,
                            label: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                label: {
                                    show: true,
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            },
                            labelLine: {
                                show: false
                            },
                            data: chartData
                        }]
                    });
                },
                error: function (xhr, status, error) {
                    console.log(error);
                }
            });
        });
    </script>
   
</body>
</html>

DAL:

public  DataTable GetEmployees()
        {
            string query = "select role_type,real_name from dt_manager";

            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    con.Open();
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {

                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        return dt;
                    }
                }
            }
        }

BLL:

public string GetEmployees()
        {
            DAL.OrdersDAL employeeDAL = new DAL.OrdersDAL();
            DataTable dt = employeeDAL.GetEmployees();
            List<Object> dataList = new List<object>();
            foreach (DataRow row in dt.Rows)
            {
                Dictionary<string, object> dataObject = new Dictionary<string, object>();
                dataObject.Add("name", row["real_name"].ToString());
                dataObject.Add("value", row["role_type"]);
                dataList.Add(dataObject);
            }
            string JSONString = JsonConvert.SerializeObject(dataList);
            return JSONString;
        }

一般处理程序用来接收返回值的,你也可以写一个web api

public void GetEmployees()
        {
            BLL.OrdersBLL employeeDAL = new BLL.OrdersBLL();
            string jsonString = employeeDAL.GetEmployees();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(jsonString);
            HttpContext.Current.Response.End();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值