用canvas画圆饼图

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<title>小熊圆饼图</title>

<meta charset="gbk">

<script>

//绘制饼图

function drawCircle(canvasId, data_arr, color_arr, text_arr) {

var c = document.getElementById(canvasId);

var ctx = c.getContext("2d");

var radius = c.height / 2 - 20; //半径

var ox = radius + 20,

oy = radius + 20; //圆心

var width = 30,

height = 10; //图例宽和高

var posX = ox * 2 + 20,

posY = 30; //

var textX = posX + width + 5,

textY = posY + 10;

var startAngle = 0; //起始弧度

var endAngle = 0; //结束弧度

for (var i = 0; i < data_arr.length; i++) {

//绘制饼图

endAngle = endAngle + data_arr[i] * Math.PI * 2; //结束弧度

ctx.fillStyle = color_arr[i];

ctx.beginPath();

ctx.moveTo(ox, oy); //移动到到圆心

ctx.arc(ox, oy, radius, startAngle, endAngle, false);

ctx.closePath();

ctx.fill();

startAngle = endAngle; //设置起始弧度

//绘制比例图及文字

ctx.fillStyle = color_arr[i];

ctx.fillRect(posX, posY + 20 * i, width, height);

ctx.moveTo(posX, posY + 20 * i);

ctx.font = ‘bold 12px 微软雅黑‘; //斜体 30像素 微软雅黑字体

ctx.fillStyle = color_arr[i]; //"#000000";

var percent = text_arr[i] + ":" + 100 * data_arr[i] + "%";

ctx.fillText(percent, textX, textY + 20 * i);

}

}

function init() {

//绘制饼图

//比例数据和颜色

var data_arr = [0.2, 0.3, 0.3, 0.2];

var color_arr = ["#FFAA00", "#00AABB", "#73BF00", "#FF4400"];

var text_arr = ["NO1", "NO2", "NO3", "NO4"];

drawCircle("canvas_circle", data_arr, color_arr, text_arr);

}

//页面加载时执行init()函数

window.onload = init;

</script>

</head>

<body>

<p>

<canvas id="canvas_circle" width="500" height="300" style="border:2px solid #5A5AAD;">

浏览器不支持canvas

</canvas>

</p>

</body>

</html>

时间: 2024-12-14 04:56:39

用canvas画圆饼图的相关文章

canvas画圆类似于锯齿指针 angular5

拿到图的时候大致是这样的,里面的圆是有动态效果的,考虑到gif图耗资源,于是想要用canvas画出来: 仔细看图不难发现,这个锯齿圆类似于表盘,计算好弧度,不难实现: 因为项目现在用的框架是angular5,所以获取元素时,要用到ElementRef;直接引用就好: 先来看下页面: import {Component, OnInit, ElementRef, ViewChild, OnDestroy} from "@angular/core";export class LoginCom

canvas画圆方法

cxt.arc(100,100,30,0,Math.PI*2,true); 括号内第一个和第二个参数,代表圆心坐标.第三个参数是圆的半径.第四个参数代表圆周起始位置.0 PI就是起始位置.沿顺时针路线,分别是0.5 PI(正下方),1 PI和1.5 PI(正上方),为画饼图提供了扇形范围的依据.第五个参数是弧长Math.PI*2就是整个圆,Math.PI是半圆.第六个参数是一个布尔值,true是顺时针false是顺时针.

canvas 画圆饼动画 countdown 倒计时

<canvas id="myCanvas" width="1000" height="500" style="border:1px solid #666"></canvas> var canvas = document.getElementById("myCanvas"), ctx = canvas.getContext("2d"), width = canvas

39.canvas画个饼图

css *{ margin: 0; padding: 0; margin-top: 10vh; } body{ text-align: center; } canvas{ margin: 0 auto; border: 1px solid #CCCCCC; } js <script type="text/javascript"> window.addEventListener('load',function(){ var canvas = document.createEl

canvas画圆

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <canvas id="canvas" style="border:1px solid #ccc;display:block;&quo

canvas 画圆

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <canvas id="canvas" style="border:1px solid #ccc;display:block;&quo

canvas 绘圆加边框

HTML5中canvas元素,绘制圆形需要使用路径,开始时要取得图形上下文,首先使用路径来勾勒图形的轮廓,然后设置颜色,进行绘制. arc(cx,cy,radius,start_angle,end_angle,direction); cx 水平坐标  cy 垂直坐标  radius 圆心  start-angel 圆周起始位置 (以圆心为参考点,不是以坐标原点为参考点.下面配图详细解释)  end_angle   圆周结束位置 Math.PI是半圆 Math.PI*2是整个圆 0.5为四分之一 

canvas画简单圆形动画

HTML: 1 <html> 2 <head> 3 <title>canvas画圆</title> 4 <meta http-equiv="content-type" content="text/html" charset="utf-8"> 5 <link rel="stylesheet" href="circle.css"/> 6 &

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

1.首先说一下canvas类: Class OverviewThe Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path,