久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

html5中canvas圖表實(shí)現(xiàn)柱狀圖的示例

本篇文章主要介紹了html5中canvas圖表實(shí)現(xiàn)柱狀圖的示例,本文使用canvas來實(shí)現(xiàn)一個(gè)圖表,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前幾天用到了圖表庫(kù),其中百度的ECharts,感覺做得最好,看它默認(rèn)用的是canvas,canvas圖表在處理大數(shù)據(jù)方面比svg要好。那我也用canvas來實(shí)現(xiàn)一個(gè)圖表庫(kù)吧,感覺不會(huì)太難,先實(shí)現(xiàn)個(gè)簡(jiǎn)單的柱狀圖。

效果如下:

html5中canvas圖表實(shí)現(xiàn)柱狀圖的示例

主要功能點(diǎn)包括:

  1. 文本的繪制
  2. XY軸的繪制;
  3. 數(shù)據(jù)分組繪制;
  4. 數(shù)據(jù)動(dòng)畫的實(shí)現(xiàn);
  5. 鼠標(biāo)事件的處理。

使用方式

首先我們看一下使用方式,參考了部分ECharts的使用方式,先傳入要顯示圖表的html標(biāo)簽,接著調(diào)用init,初始化的同時(shí)傳入數(shù)據(jù)。

var con=document.getElementById('container');
    var chart=new Bar(con);
    chart.init({
        title:'全年降雨量柱狀圖',
        xAxis:{// x軸
            data:['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
        },
        yAxis:{//y軸
            name:'水量',
            formatter:'{value} ml'
        },
        series:[//分組數(shù)據(jù)
            {
                name:'東部降水量',
                data:[62,20,17,45,100,56,19,38,50,120,56,130]
            },
            {
                name:'西部降水量',
                data:[52,10,17,25,60,39,19,48,70,30,56,8]
            },
            {
                name:'南部降水量',
                data:[12,10,17,25,27,39,50,38,100,30,56,90]
            },
            {
                color:'hsla(270,80%,60%,1)',
                name:'北部降水量',
                data:[12,30,17,25,7,39,49,38,60,30,56,10]
            }
        ]
    });

圖表基類,我們后面還要寫餅圖,折線圖,所以把公共的部分抽出來。注意canvas.style.width與canvas.width是不一樣的,前者會(huì)拉伸圖形,后者才是我們正常用的,不會(huì)拉伸圖形。在這里這樣寫先擴(kuò)大再縮小是為了解決canvas繪制文字時(shí)模糊的問題。

class Chart{
        constructor(container){
            this.container=container;
            this.canvas=document.createElement('canvas');
            this.ctx=this.canvas.getContext('2d');
            this.W=1000*2;
            this.H=600*2;
            this.padding=120;
            this.paddingTop=50;
            this.title='';
            this.legend=[];
            this.series=[];
            //通過縮小一倍,解決字體模糊問題
            this.canvas.width=this.W;
            this.canvas.height=this.H;
            this.canvas.style.width = this.W/2 + 'px';
            this.canvas.style.height = this.H/2 + 'px';
        }
    }

柱狀圖初始化,調(diào)用es6中的Object.assign(this,opt),這個(gè)相當(dāng)于JQ中的extend方法,把屬性復(fù)制到當(dāng)前實(shí)例。同時(shí)還建了個(gè)tip屬性,這是個(gè)html標(biāo)簽,后面顯示數(shù)據(jù)信息用。接著繪制圖形,然后綁定鼠標(biāo)事件。

class Bar extends Chart{
    constructor(container){
        super(container);
        this.xAxis={};
        this.yAxis=[];
        this.animateArr=[];
    }
    init(opt){
        Object.assign(this,opt);
        if(!this.container)return;
        this.container.style.position='relative';
        this.tip=document.createElement('div');
        this.tip.style.cssText='display: none; position: absolute; opacity: 0.5; background: #000; color: #fff; border-radius: 5px; padding: 5px; font-size: 8px; z-index: 99;';
        this.container.appendChild(this.canvas);
        this.container.appendChild(this.tip);
        this.draw();
        this.bindEvent();
    }
    draw(){//繪制

    }
    showInfo(){//顯示信息

    }
    animate(){//執(zhí)行動(dòng)畫

    }
    showData(){//顯示數(shù)據(jù)

    }

繪制XY軸

首先繪制標(biāo)題,接著XY軸,然后遍歷分組數(shù)據(jù)series,里面有復(fù)雜的計(jì)算,然后繪制XY軸的刻度,繪制分組標(biāo)簽,最后是繪制數(shù)據(jù)。數(shù)據(jù)項(xiàng)series中是分組數(shù)據(jù),它跟X軸的xAxis.data一一對(duì)應(yīng)。每個(gè)項(xiàng)可以自定義名稱和顏色,沒有指定的話,名稱賦予nunamed和自動(dòng)生成顏色。這里還用legend屬性記錄下了標(biāo)簽列表信息,因?yàn)楹罄m(xù)鼠標(biāo)點(diǎn)擊判斷是否點(diǎn)中用的上。

canvas主要知識(shí)點(diǎn):

  1. 分組標(biāo)簽使用了arcTo方法,這樣就能繪制出圓角的效果。
  2. 繪制文本使用了measureText方法,可以用來測(cè)量文字所占寬度,這樣就可以調(diào)整下一次繪制的位置,避免位置沖突。
  3. translate位移方法,可以放在繪制上下文(save和restore的中間)中,這樣可以避免復(fù)雜的位置運(yùn)算。
draw(){
    var that=this,
        ctx=this.ctx,
        canvas=this.canvas,
        W=this.W,
        H=this.H,
        padding=this.padding,
        paddingTop=this.paddingTop,
        xl=0,xs=0,xdis=W-padding*2,//x軸單位數(shù),每個(gè)單位長(zhǎng)度,x軸總長(zhǎng)度
        yl=0,ys=0,ydis=H-padding*2-paddingTop;//y軸單位數(shù),每個(gè)單位長(zhǎng)度,y軸總長(zhǎng)度

    ctx.fillStyle='hsla(0,0%,20%,1)';
    ctx.strokeStyle='hsla(0,0%,10%,1)';
    ctx.lineWidth=1;
    ctx.textAlign='center';
    ctx.textBaseLine='middle';
    ctx.font='24px arial';

    ctx.clearRect(0,0,W,H);
    if(this.title){
        ctx.save();
        ctx.textAlign='left';
        ctx.font='bold 40px arial';
        ctx.fillText(this.title,padding-50,70);
        ctx.restore();
    }
    if(this.yAxis&&this.yAxis.name){
        ctx.fillText(this.yAxis.name,padding,padding+paddingTop-30);
    }

    // x軸
    ctx.save();
    ctx.beginPath();
    ctx.translate(padding,H-padding);
    ctx.moveTo(0,0);
    ctx.lineTo(W-2*padding,0);
    ctx.stroke();
    // x軸刻度
    if(this.xAxis&&(xl=this.xAxis.data.length)){
        xs=(W-2*padding)/xl;
        this.xAxis.data.forEach((obj,i)=>{
            var x=xs*(i+1);
            ctx.moveTo(x,0);
            ctx.lineTo(x,10);
            ctx.stroke();
            ctx.fillText(obj,x-xs/2,40);
        });
    }
    ctx.restore();

    // y軸
    ctx.save();
    ctx.beginPath();
    ctx.strokeStyle='hsl(220,100%,50%)';
    ctx.translate(padding,H-padding);
    ctx.moveTo(0,0);
    ctx.lineTo(0,2*padding+paddingTop-H);
    ctx.stroke();
    ctx.restore();

    if(this.series.length){         
        var curr,txt,dim,info,item,tw=0;
        for(var i=0;i<this.series.length;i++){
            item=this.series[i];
            if(!item.data||!item.data.length){
                this.series.splice(i--,1);continue;
            }
            // 賦予沒有顏色的項(xiàng)
            if(!item.color){
                var hsl=i%2?180+20*i/2:20*(i-1);
                item.color='hsla('+hsl+',70%,60%,1)';
            }
            item.name=item.name||'unnamed';

            // 畫分組標(biāo)簽
            ctx.save();
            ctx.translate(padding+W/4,paddingTop+40);
            that.legend.push({
                hide:item.hide||false,
                name:item.name,
                color:item.color,
                x:padding+that.W/4+i*90+tw,
                y:paddingTop+40,
                w:60,
                h:30,
                r:5
            });
            ctx.textAlign='left';
            ctx.fillStyle=item.color;
            ctx.strokeStyle=item.color;
            roundRect(ctx,i*90+tw,0,60,30,5);
            ctx.globalAlpha=item.hide?0.3:1;
            ctx.fill();
            ctx.fillText(item.name,i*90+tw+70,26);
            tw+=ctx.measureText(item.name).width;//計(jì)算字符長(zhǎng)度
            ctx.restore();

            if(item.hide)continue;
            //計(jì)算數(shù)據(jù)在Y軸刻度
            if(!info){
                info=calculateY(item.data.slice(0,xl));
            }
            curr=calculateY(item.data.slice(0,xl));
            if(curr.max>info.max){
                info=curr;
            }
        }

        if(!info) return;
        yl=info.num;
        ys=ydis/yl;

        //畫Y軸刻度
        ctx.save();
        ctx.fillStyle='hsl(200,100%,60%)';
        ctx.translate(padding,H-padding);
        for(var i=0;i<=yl;i++){
            ctx.beginPath();
            ctx.strokeStyle='hsl(220,100%,50%)';
            ctx.moveTo(-10,-Math.floor(ys*i));
            ctx.lineTo(0,-Math.floor(ys*i));
            ctx.stroke();

            ctx.beginPath();
            ctx.strokeStyle='hsla(0,0%,80%,1)';
            ctx.moveTo(0,-Math.floor(ys*i));
            ctx.lineTo(xdis,-Math.floor(ys*i));
            ctx.stroke();

            ctx.textAlign='right';
            dim=Math.min(Math.floor(info.step*i),info.max);
            txt=this.yAxis.formatter?this.yAxis.formatter.replace('{value}',dim):dim;
            ctx.fillText(txt,-20,-ys*i+10);
        }
        ctx.restore();
        //畫數(shù)據(jù)
        this.showData(xl,xs,info.max);
    }
}

繪制數(shù)據(jù)

【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。

相關(guān)文檔推薦

主站蜘蛛池模板: 99久久免费精品国产男女高不卡 | 午夜精品久久 | 老司机午夜性大片 | 精品一区二区三区日本 | 看亚洲a级一级毛片 | 真人毛片 | 日美女逼逼 | 日韩毛片免费看 | 91原创视频在线观看 | 成人av免费| 精品国产伦一区二区三区观看说明 | 操久久 | 精品视频国产 | 男女黄网站| 欧美一级欧美三级在线观看 | 美女爽到呻吟久久久久 | 精品国产精品一区二区夜夜嗨 | 午夜小电影 | 日韩理论电影在线观看 | 日韩精品一区二区三区在线观看 | 国产激情视频网站 | 精品亚洲永久免费精品 | 日日干日日色 | 乳色吐息在线观看 | 二区国产 | 综合久久综合久久 | 国产最新视频在线 | 不用播放器看的av | 日韩精品在线免费观看 | 福利视频网址 | 日韩精品极品视频在线观看免费 | 一区二区三区久久 | 黄色三级免费网站 | 在线视频一区二区三区 | 色视频网站 | 日韩电影一区二区三区 | 人妖av| 日韩视频一级 | 超碰在线人 | 国产视频日韩 | 亚洲精品天堂 |