zoukankan      html  css  js  c++  java
  • echarts呈现数据表图形

    讲一下echarts的用法,列举了两个图表,一个是单柱图,一个是多柱图,至于饼状图,只许更改echarts的类型就好了

      一、首先是要两个div,用来存放两个图表

      <div class="div-frm" style="height: 275px; font-family: Microsoft YaHei, Verdana, Arial;">
            <div id="barone" style=" 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=" 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

  • 相关阅读:
    汤姆猫解决打印日志乱码问题
    oracel如何将timestamp转化为date类型
    oracle排序分页
    批量删除某一后缀的临时表
    oracle分组排序后获取每组行号
    cxf中隐藏服务列表
    怎么从一张表中查询数据插入到另一张表中
    weblogic创建域
    java多线程
    Linux常用命令大全
  • 原文地址:https://www.cnblogs.com/liuchenxing/p/9122954.html
Copyright © 2011-2022 走看看