使用canvas繪制一個(gè)簡(jiǎn)單的餅圖

本案例為書本的一個(gè)例子。
該地址為書本代碼鏈接: https://github.com/ikcamp/Efficient-Mobile-Web-FE-Development/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/>
    <meta content="telephone=no" name="format-detection"/>
    <meta content="address=no" name="format-detection"/>
    <meta name="msapplication-tap-highlight" content="no" />
    <title>使用Canvas繪制一個(gè)簡(jiǎn)單的餅圖</title>
</head>
<body>
    <canvas class="pie-chart" width="850" height="500" style="transform: scale(0.5);transform-origin: 0 0"></canvas>
    <script type="text/javascript">
        let PieChart = function(selector, options) {
            let canvas = "string" === typeof selector ? document.querySelector(selector) : null;
            if(canvas === null) return false;
            let defaultOptions = {
                radius: 200,
                legendParms: {
                    font: "24px Arial",
                    x: 30,
                    y: 30,
                    margin: 50,
                    width: 40,
                    height: 24
                }
            }
            this.context = canvas.getContext("2d");
            this.width = canvas.getAttribute("width") || 300;
            this.height = canvas.getAttribute("height") || 300;
            this.options = Object.assign(defaultOptions, options);
        };
        PieChart.prototype.load = function(data) {
            data.forEach(item => this.count ? this.count += item.value : this.count = item.value);
            this.data = data;
            return this;
        };
        PieChart.prototype.render = function() {
            let _generateLegend = (item, index) => {
                this.context.fillRect(
                    this.options.legendParms.x, 
                    this.options.legendParms.y + index * this.options.legendParms.margin, 
                    this.options.legendParms.width, 
                    this.options.legendParms.height
                );
                this.context.font = this.options.legendParms.font;
                this.context.fillText(
                    item.title, 
                    this.options.legendParms.y + this.options.legendParms.margin, 
                    (index + 1) * this.options.legendParms.margin
                );
            };
            let temparc = 0;
            this.data.forEach((item, index) => {
                item.color = `#${('00000'+(Math.random()*0x1000000<<0).toString(16)).substr(-6)}`;
                this.context.beginPath();
                this.context.moveTo(this.width / 2, this.height / 2);
                let startarc = temparc, endarc =  startarc + (item.value / this.count) * Math.PI * 2;
                this.context.arc(
                    this.width / 2, 
                    this.height / 2, 
                    this.options.radius, 
                    startarc, 
                    endarc, 
                    false
                );
                this.context.closePath();
                this.context.fillStyle = item.color;
                this.context.fill();
                temparc = endarc;
                if (this.options.legend) {
                    _generateLegend(item, index);
                }
            });
            return this;           
        };
        const data = [
            {title: "滬江網(wǎng)校", value: 1024}, 
            {title: "滬江小D", value: 512}, 
            {title: "滬江學(xué)習(xí)", value: 256}, 
            {title: "開心詞場(chǎng)", value: 920}
        ];
        let pie = new PieChart(".pie-chart", {legend: true});
        pie.load(data).render();
    </script>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • code4app.com 這網(wǎng)站不錯(cuò),收集各種 iOS App 開發(fā)可以用到的代碼示例 cocoacontrols...
    winsonink閱讀 1,317評(píng)論 0 0
  • 「天地不仁 以萬(wàn)物為芻狗」的解釋是:天地?zé)o所謂仁,也無所謂不仁。天地生了萬(wàn)物,天地看待萬(wàn)物是一樣的,不對(duì)誰(shuí)特別好,...
    曾曉超閱讀 1,004評(píng)論 4 2
  • 毋庸置疑,好的事情總會(huì)到來。而當(dāng)它來晚時(shí),也不失為一種驚喜。 “現(xiàn)在的人不愁吃不愁穿,那么當(dāng)下年輕人剩下最多的問題...
    小顧事閱讀 611評(píng)論 0 1

友情鏈接更多精彩內(nèi)容