• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET使用第三方库实现js图表

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 准备工作
 首先从http://jquery.com/下载最新版本的jQuery,然后从http://www.highcharts.com/下载Highcharts的JavaScript类库。将这两个文件下载到本地的Web项目的JS文件下。
 因 为Highcharts是根据js脚本中的属性设置来生成图表的,因为我们需要在ASP.NET中输出这些脚本。为了方便,周公将生成各种图表的核心逻辑 部分抽取出来作为一个模板,需要的时候从这些模板中读取数据,然后将模板中的标签替换掉,这样就可以输出到客户端了。这些带有标签的JS模板位于 js/template目录下。
 除了输出控制逻辑代码之外,还要在网页的ready客户端事件中调用,如下格式:
 $(document).ready(function() {//这里是逻辑和控制代码});
 
 编码
 为了便于演示,我在创建ASP.NET页面的时候使用了单页模式,整个页面的代码如下:

    <%@ Page Language="C#" %>  
    <%@ Import Namespace="System.Text" %>  
    <%@ Import Namespace="System.IO" %>  硬件资讯
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
      
    <mce:script runat="server"><!--  
        public string result = string.Empty;  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            string graphType = ddlGraphType.SelectedValue;  
              
            graphType = graphType.ToLower();  
            switch (graphType)  
            {  
                case "line": result=OutputLine(); break;  
                case "spline": result = OutputSpline(); break;  
                case "area": result = OutputArea(); break;  
                case "areaspline": result = OutputAreaSpline(); break;  
                case "column": result = OutputColumn(); break;  
                case "bar": result = OutputBar(); break;  
                case "pie": result = OutputPie(); break;  
                case "scatter": result = OutputScatter(); break;  
            }  
        }  
      
        private string OutputLine()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/line.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputSpline()  
        {  女装品牌排行榜
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/spline.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputArea()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/area.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputAreaSpline()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/areaspline.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputColumn()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/column.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputBar()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/bar.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                buffer.Replace("{#subTitle#}", "扇形图表");  
                //下面的数据应该是从数据库来,为了演示,这里直接上了  
                string data = "{name: '1812年',data: [107, 31, 635, 203, 2]}, {name: '1912年',data: [133, 156, 947, 408, 6]}, {name: '2012年',data: [973, 914, 4054, 732, 34]}";  
                buffer.Replace("{#series#}", data);  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputPie()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/pie.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
      
        private string OutputScatter()  
        {  
            string result = string.Empty;  
            using (StreamReader reader = new StreamReader(Server.MapPath("~/js/template/scatter.js"), Encoding.UTF8))  
            {  
                StringBuilder buffer = new StringBuilder(reader.ReadToEnd());  
                buffer.Replace("{#title#}", "周公的演示例子");  
                result = buffer.ToString();  
            }  
            return result;  
        }  
    // --></mce:script>  
      
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
        <title>Hign Charts 实例</title>  
    <mce:script src="js/jquery-1.5.2.min.js" mce_src="js/jquery-1.5.2.min.js" type="text/javascript"></mce:script>  
    <mce:script src="js/highcharts.js" mce_src="js/highcharts.js" type="text/javascript"></mce:script>  
      
    </head>  
    <body>  
        <form ><!--  
            var chart;  
            $(document).ready(function() {  
    <%=result %>  
                });  
                      
              
    // --></mce:script>  
    </body>  
    </html> 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.Net新建和读写XML文档小结发布时间:2022-07-10
下一篇:
ASP.Net本地化/国际化解决方案原理和代码示例发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap