zoukankan      html  css  js  c++  java
  • html5 canvas画饼

    1. [图片] lxdpie.jpg    


    ​2. [文件] lqdpie.html ~ 801B     下载(7)     

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="ru">
    <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
     
    <script type="text/javascript" src="pie.js"></script>
    </head>
    <body>
       
        <fieldset style='position:relative;500px;height:400px;'>
                <legend> 刘强东吃饼</legend>
                <canvas id="p" width="500" height="400"   >Your browser does not support the HTML5 canvas tag.</canvas>
            </fieldset>
     
     
    <script>
     
    var config = {
        id: 'p',
        type: '%',
     
        data: [
    ['刘强东',18.4],['老虎基金',22.1],['HHGL',15.8],['DST',11.2],['BAI',9.5],['Fortune ising',5.3],['Kingdom',5.0],['红杉资本',2.0],
     
     ]
    };
    var p= pie(config);
        p.render();
     
    </script>
     
     
    </body>
    </html>
    3. [文件] pie.js ~ 3KB     
    (function() {
        var Pie = (function() {
         
            var F = function(conf) {
                    this.type=null;
                    this.id=null;
                    this.total=0;
                    this.container=null;
                    this.data=[];
                    this.init(conf);
                    return this;
                };
            F.prototype = {
                defaultBgcolor: ['deeppink', 'mediumslateblue', 'chartreuse', 'goldenrod', "#ffff00", "#2F368F", "#F6A25D", "#2CA8E0", "#77D1F6", '#181818', '#45AB35', "#336699", "#5fD1F6"],
     
                init: function(options) {
                    for (var p in options) {
                        this[p] = options[p];
                    }
                    this.container = document.getElementById(this.id);
                },
                percentize: function() {
     
                    if (this.type && this.type == '%') {
                        var sum = 0;
                        for (var i = 0; i < this.data.length; i++) {
                            sum += this.data[i][1];
                            if (this.data[i + 1] && (sum + this.data[i + 1][1]) > 100) {
                                break;
                            }
                        }
                        if (i != this.data.length) {
                            this.data = this.data.splice(0, i + 1);
                        }
                        if (sum != 100) {
                            this.data.push(['?', Math.ceil(100 - sum), '#282828']);
                        }
                    } else {
                        var sum = 0;
     
                        for (var i = 0; i < this.data.length; i++) {
                            sum += this.data[i][1];
                        }
                        if (0 == this.total) {
                            this.total = sum;
                        }
                        
                        if (this.total - sum > 0) {
     
                            this.data.push(['?', this.total - sum, '#282828']);
                        }http://www.enterdesk.com/special/shouhui/​
     
                        for (var i = 0; i < this.data.length; i++) {
                            this.data[i][1] = Math.round((this.data[i][1] / this.total) * 100);
                        }手绘图片
                        
                    }
      
     
                },
                renderPie: function() {
                    var x = this.container.clientWidth * .33;
                    var y = this.container.clientHeight * .5;
                    var radius = (x > y) ? y : x;
                    var ctx = this.container.getContext("2d");
                    var startPoint = 0;
                    for (var i = 0; i < this.data.length; i++) {
                        if (null == this.data[i][2]) {
                            this.data[i][2] = this.defaultBgcolor[i % this.defaultBgcolor.length];
                        }
                        ctx.fillStyle = this.data[i][2];
                        ctx.beginPath();
                        ctx.moveTo(x, y);
                        ctx.arc(x, y, radius, startPoint, startPoint + Math.PI * 2 * (this.data[i][1] / 100), false);
                        ctx.fill();
                        startPoint += Math.PI * 2 * (this.data[i][1] / 100);
                    }
                    return true;
                },
                renderLabel: function() {
                    var table = ['<table  >'];
                    for (var i = 0; i < this.data.length; i++) {
                        table.push('<tr><td bgcolor="');
                        table.push(this.data[i][2]);
                        table.push('">&nbsp;&nbsp;</td><td>');
                        table.push(this.data[i][0]);
                        table.push("</td><td align=right>");
                        if (this.type && this.type == '%') {
                            table.push(this.data[i][1] + "%");
                        } else {
                            table.push(Math.ceil(this.total * this.data[i][1] / 100));
                        }
                        table.push("</td></tr>");
                    }
                    table.push("</table>");
                    this.container.insertAdjacentHTML("afterEnd", '<div   >  ' + table.join("") + '</div>');
                },
                render: function() {
                    this.percentize();
                    this.renderPie();
                    this.renderLabel();
                    return true;
                }
            };
            var Pie = function(conf) {
                    return new F(conf);
                },
                r;
     
     
     
     
     
            return Pie;
     
        })();
     
     
        window.pie = Pie;
    })(window);
  • 相关阅读:
    廖雪峰的多线程 1
    保持良好的心态 戒骄戒躁
    Break camelCase
    int32 to IPv4 (int32到IPv4地址转换)
    Stop gninnipS My sdroW!
    Find The Parity Outlier 找到奇偶校验异常值
    今日新闻整理 2020-7-31
    改造rabbitmq demo 到 jpa
    Flink实战(110):FLINK-SQL应用场景(11)connector(十九)Flink 与 hive 结合使用(七) Flink Hive Connector 使用
    Hadoop基础(六十):面试题 Hadoop数据切片(二)切片机制源码
  • 原文地址:https://www.cnblogs.com/xkzy/p/3941994.html
Copyright © 2011-2022 走看看