Chart.js实现饼图

摘要:
页面:导入静态脚本:或下载脚本:

页面:

引入静态脚本:

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
   <script src="https://code.highcharts.com/highcharts.js"></script>

或下载脚本:

 <script src="http://t.zoukankan.com/@Url.Content("~/Scripts/HighChart/highcharts.js")" type="text/javascript"></script>
    <script src="http://t.zoukankan.com/@Url.Content("~/Scripts/HighChart/exporting.js")" type="text/javascript"></script>
    <script src="http://t.zoukankan.com/@Url.Content("~/Scripts/HighChart/grid.js")" type="text/javascript"></script>
  <script type="text/javascript">

   $(document).ready(function () {

 $(function () {

                showPieChart();
            });

 });

        function showPieChart() {

            var url = "@HttpContext.Current.Request.ApplicationPath" == "/" ? "" : "@HttpContext.Current.Request.ApplicationPath";
            $.ajaxSetup({
                cache: false
            });
            $.ajax({
                type: 'get',
                url: 'http://' + "@HttpContext.Current.Request.Url.Authority" + url + '/Approve/getDateToChart',
                async: false,
                dataType: 'json',

                contentType: 'application/json; charset=utf-8',

                success: function (data1) {
                    debugger;
                    var arr = new Array();

                    for (i in data1) {

                        var a = data1[i];
                        var value = [a.colName + " (@Resources.ApproveListResource.count:" + a.count + ") ", a.percent]
                        arr.push(value);

                    }


                    var chart1 = new Highcharts.chart('container', {
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false,
                            borderWidth: 0,
                            type: 'pie'
                        },
                        title: {
                            text: '@Resources.ApproveListResource.prWatingApproval'
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        credits: {
                            enabled: false
                        },
                        exporting: {
                            enabled: false
                        },

                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                                    style: {
                                        fontSize: "13px",
                                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                    }
                                }
                            }
                        },
                        series: [{
                            name: 'Brands',
                            colorByPoint: true,
                            data: arr
                        }]


                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    swal(errorThrown);
                }
            });
        }

后台:

[HttpGet]
        public ActionResult getDateToChart()
        {
            CultureInfo usCulture = Thread.CurrentThread.CurrentUICulture;
            ViewBag.lang = usCulture.Name == "zh-TW" ? "zh-hk" : "en-US";
            string lang = ViewBag.lang;
            string dataChart = "";
            DataTable dtList = new DataTable();
            dtList = approve.waitingApprovalList(badge);
            DataRow[] drPR = dtList.Select();
            List<PieChartModel> compList = new List<PieChartModel>();
            List<string> allComp=new List<string>();
            for (int i = 0; i < drPR.Length; i++)
            {
                string comp = drPR[i]["comp_code"].ToString().Trim();
                allComp.Add(comp);
            }
            List<string> distinctComp = allComp.Distinct().ToList();
            int distinctCompCount = 0;
            foreach(var c in distinctComp)
            {
                int count = 0;
                distinctCompCount++;
                for (int j = 0; j < drPR.Length; j++)
                {
                    string comp = drPR[j]["comp_code"].ToString().Trim();
                    if(comp == c)
                    {
                        count++;
                    }
                }
                PieChartModel p = new PieChartModel();
                p.colName = c;
                p.count = count;
                p.percent = Convert.ToDouble(count) / Convert.ToDouble(drPR.Length);
                p.percent *= 100;
               
                compList.Add(p);
                //if (distinctCompCount == 1)
                //    dataChart = "[";
                //dataChart += "{name: '" + p.colName + "(PR:" + p.count + ")',y:" + p.percent + ",sliced: true,selected: true} ";
                //if (distinctCompCount != distinctComp.Count)
                //    dataChart += ",";
                //else
                //    dataChart += "]";
                //{
                //                name: 'Microsoft Internet Explorer',
                //                y: 56.33
                //            }, {
                //                name: 'Chrome',
                //                y: 24.03,
                //                sliced: true,
                //                selected: true
                //            }, {
                //                name: 'Firefox',
                //                y: 10.38
                //            }, {
                //                name: 'Safari',
                //                y: 4.77
                //            }, {
                //                name: 'Opera',
                //                y: 0.91
                //            }, {
                //                name: 'Proprietary or Undetectable',
                //                y: 0.2
                //            }
            }

            return Json(compList, JsonRequestBehavior.AllowGet);

        }

免责声明:文章转载自《Chart.js实现饼图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇StackExchange.Redis.DLL 操作redis加强版《Java编程思想》阅读笔记6下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python 网络爬虫(二)

一、编写第一个网络爬虫 为了抓取网站,我们需要下载含有感兴趣的网页,该过程一般被称为爬取(crawling)。爬取一个网站有多种方法,而选择哪种方法更加合适,则取决于目标网站的结构。 首先探讨如何安全的下载网页,让后介绍3中爬去网站的常见方法: -- 爬取网站地图; -- 遍历每个网页的数据库 ID; -- 跟踪网页链接; 1、下载网页 要想爬取网页,我们...

网易云课堂js学习笔记

javascript:用来在页面中编写特效的,和html/css一样都是由浏览器解析的 javascript语言:一、js如何运行的(javaScript,jscript,vbscript,applet)二、输出alert(什么类型都可以)document.write(字符串) 三、如何在html中使用js1.使用<script></sc...

浏览器和服务器 对http请求(post get) url长度限制

1. GET URL长度限制 在Http1.1协议中并没有提出针对URL的长度进行限制,RFC协议里面是这样描述的,HTTP协议并不对URI的长度做任何的限制,服务器端 必须能够处理任何它们所提供服务多能接受的URI,并且能够处理无限长度的URI,如果服务器不能处理过长的URI,那么应该返回414状态码。 虽然Http协议规定了,但是Web服务器和浏览器...

秒杀方案

方案1 秒杀系统独立部署,甚至使用独立域名,使其与网站完全隔离 重新设计秒杀商品页面,不使用网站原来的商品详细页面,页面内容静态化,用户请求不需要经过应用服务 因为秒杀新增的网络带宽,必须和运营商重新购买或者租借。为了减轻网站服务器的压力,需要将秒杀商品页面缓存在CDN,同样需要和CDN服务商临时租借新增的出口带宽 为了避免用户直接访问下单页面URL,需要将...

JSONP原理解析

前言 我工作以来接触的第一个项目就是前后端分离的,前端静态文件有自己独立域名,通过接口来获取数据进行渲染等操作。 跨域的方法不需要多言,随便一搜,就有很多,但最常用不外乎jsonp和CORS。jsonp着重于前端,也算是前端Hack技巧,CORS重于后端,服务端需要配置的地方会较多。 这篇解析一下jsonp的实现原理。 基本原理 基本原理很容易说明白,在h...

Ubuntu 16.04设置rc.local开机启动命令/脚本的方法

Ubuntu 16.04设置rc.local开机启动命令/脚本的方法       Ubuntu 16.04设置rc.local开机启动命令/脚本的方法(通过update-rc.d管理Ubuntu开机启动程序/服务) 注意:rc.local脚本里面启动的用户默认为root权限。 一、rc.local脚本 rc.local脚本是一个Ubuntu开...