Use canvas to write a doughnut chart.

Source: Internet
Author: User

The principle uses canvas to draw:

First step: Draw a great circle

Part II: Draw a fan

Part III: Draw a small circle

Overlay each other.

Final effect:

Now on the code:

(function($) {$.fn.drawpic=function(opts) {vardefaults={canvasbj:"White",//Canvas BackgroundCanvaswidth:50, Canvasheight:50, BIGBG:"Red",//Large Circle BackgroundBIGRADIUS:50,//Circle RadiusRingcolor: "Yellow",//Ring ColorSMALLBG: "White",//small round backgroundSmallradius:30,//Small Circle Radiusdeg:0.25,//percent, 90 degreesText: "90/360", TextColor:"Black", Textfont:"Normal 14px Microsoft ya Black"        }; functionDraw (id,opt) {varcanvas=document.getElementById (ID); if(canvas==NULL){                return false; }            varCtx=canvas.getcontext (' 2d '); //Fill Canvasctx.fillstyle=OPT.CANVASBJ; Ctx.fillrect (0,0, Opts.canvaswidth,opts.canvasheight);            Ctx.beginpath (); //Reset Canvas StartCtx.translate (OPTS.CANVASWIDTH/2, OPTS.CANVASHEIGHT/2); //go to a new startCtx.moveto (0,0); //draw a great circleCtx.arc (0,0,opts.bigradius,0,math.pi*2,true);            Ctx.closepath (); Ctx.fillstyle=OPTS.BIGBG;            Ctx.fill (); //Start Drawing SectorCtx.beginpath (); Ctx.moveto (0, 0); //drawing ArcsCtx.arc (0, 0, Opts.bigradius, 0, -1*opts.deg*360*math.pi/180,true);//Closed PathCtx.closepath (); Ctx.fillstyle=Opts.ringcolor;                        Ctx.fill (); //draw a small circleCtx.beginpath (); Ctx.moveto (0,0); Ctx.arc (0,0,opts.smallradius,0,math.pi*2,true);            Ctx.closepath (); Ctx.fillstyle=OPTS.SMALLBG;                        Ctx.fill (); //Draw TextCtx.moveto (0,0);//move stroke to OriginCtx.fillstyle=opts.textcolor;//text colorCtx.font=opts.textfont;//textctx.textalign= "center";//Center horizontally relative to OriginCtx.textbaseline= "Middle";//Center vertically relative to OriginCtx.filltext (opts.text,0,0);//Start Writing} opts=$.extend ({},defaults,opts);  This. each (function(){            varobj=$ ( This); varcurrentcanvas= "Canvas" + (NewDate ()). GetTime (); Obj.html ("<canvas id=" "+currentcanvas+" ' width= ' "+opts.canvaswidth+" ' height= ' "+opts.canvasheight+" > Your device does not support!</ Canvas> ");        Draw (currentcanvas,opts);    }); }}) (JQuery);

HTML code and calls:

<div class= "Pie1" ></div> <script type= "Text/javascript" >        $(". Pie1"). Drawpic ({canvasbj:"#ccc",//Canvas Backgroundcanvaswidth:400, Canvasheight:300, BIGBG:"Red",//Large Circle BackgroundBIGRADIUS:80,//Circle RadiusRingcolor: "Yellow",//Ring ColorSMALLBG: "White",//small round backgroundSMALLRADIUS:65,//Small Circle Radiusdeg:0.25,//percentageText: "90/360", TextColor:"Black", Textfont:"Normal 30px Microsoft ya Black"        }); </script>

Use canvas to write a doughnut chart.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.