We have seen too many bar progress bars. It is relatively simple to implement. It is always rectangular, and it is not very nice to place it in a square area. With the emergence of css3, ring-like progress bars are increasingly used. However, since IE6/7/8 does not support css3, we can only implement it in other ways. This article uses Raphael to draw a picture. This component encapsulates svg and vml in a unified manner and draws based on different technologies in the browser, So IE can also be used.
First:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12250H2B-0.png "border =" 0 "alt =" "/>
Good results, right? The code is not complex:
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Title> circular progress bar </title>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
- <Script src = "jquery-1.4.2.js"> </script>
- <Script src = "raphael-min.js"> </script>
- <Style>
- # Txt {
- Width: 74px;
- Height: 74px;
- Line-height: 74px;
- Position: absolute;
- Margin-top:-74px;
- Text-align: center;
- Color: #9e9fa3;
- Font-size: 18px;
- Font-family: Arial;
- }
- </Style>
- <Script>
-
- Var demo = {
- Paper: null,
-
- Init: function (){
- // Initialize the Raphael canvas
- This. paper = Raphael ("bg", 74, 74 );
-
- // Draw the basemap first
- This. paper. image ("progressBg.png", 0, 0, 74, 74 );
-
- // Progress ratio: 0 to 1. In this example, we will draw 65%
- // Note that the following algorithm does not support painting 100%. You need to draw the image according to 99.99%.
- Var percent = 0.65,
- DrawPercent = percent> = 1? 0.9999: percent;
-
- // Start to calculate the position of each point, as shown in the following figure.
- // R1 indicates the inner circle radius and r2 indicates the outer circle radius.
- Var r1 = 26, r2 = 31, PI = Math. PI,
- P1 = {
- X: 37,
- Y: 69
- },
- P4 = {
- X: p1.x,
- Y: p1.y-r2 + r1
- },
- P2 = {
- X: p1.x + r2 * Math. sin (2 * PI * (1-drawPercent )),
- Y: p1.y-r2 + r2 * Math. cos (2 * PI * (1-drawPercent ))
- },
- P3 = {
- X: p4.x + r1 * Math. sin (2 * PI * (1-drawPercent )),
- Y: p4.y-r1 + r1 * Math. cos (2 * PI * (1-drawPercent ))
- },
- Path = [
- 'M', p1.x, '', p1.y,
- 'A', r2, '', r2, '0', percent> 0.5? 1: 0, '1', p2.x, '', p2.y,
- 'L', p3.x, '', p3.y,
- 'A', r1, '', r1, '0', percent> 0.5? 1: 0, '0', p4.x, '', p4.y,
- 'Z'
- ]. Join ('');
-
- // Draw a graph using the path method, which consists of two arcs and two straight lines. The algorithm for drawing an arc is shown below:
- This. paper. path (path)
- // Fill in gradient, from # 3f0b3f to # ff66ff
- . Attr ({"stroke-width": 0.5, "stroke": "# c32ec3", "fill": "90-# 3f0b3f-# ff66ff "});
-
- // Display progress text
- $ ("# Txt"). text (Math. round (percent * 100) + "% ");
- }
-
- };
-
- $ (Function (){
- Demo. init ();
- });
- </Script>
- </Head>
- <Body>
-
- <! -- Host the graphic subject -->
- <Div id = "bg"> </div>
- <! -- Carry progress text -->
- <Div id = "txt"> </div>
-
- </Body>
- </Html>
The progress bar consists of two parts: the basemap progressBg.png and the 74 × 74 png. Use the Raphael image method to draw the progress bar first:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12250L102-1.png "border =" 0 "alt =" "/>
The second step is to use Raphael to draw the progress part, which consists of two arcs and two line segments. For details, see:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12250M527-2.png "border =" 0 "alt =" "/>
First, draw a clockwise arc from p1 to p2, then a straight line to p3, then draw an arc clockwise to p4, and then straight line back p1. Then fill it with gradient. Take 65% as an example. The path to describe the image is as follows (basically the svg syntax ):
- M37 69
- A31 31 0 1 1 62.079526825623375 19.778657178933337
- L58.03444185374863 22.7175834403957
- A26 26 0 1 0 37 64
- Z
It looks a little scary. In fact, it can be simply divided into several parts:
◆ The first line indicates moving to (37, 69) as the starting point, that is, p1;
◆ The second line indicates drawing an arc, that is, the section between p1 and p2;
◆ The third line indicates drawing a straight line, that is, the section between p2 and p3;
◆ The fourth line indicates that a circle is drawn, which is between p3 and p4;
◆ The fifth line indicates a closed image, which is equivalent to a connection from p4 back to p1;
In this case, the arc parameters are complex. The parameters are as follows:
- A rx ry x-axis-rotation large-arc-flag sweep-flag x y
Taking the first arc as an example, the parameters are described as follows:
◆ Rx and ry: the length of the semi-axis and semi-short axis of the arc. We use 31 because we draw a positive circle;
◆ X-axix-rotation: the angle between the x-axis and the horizontal direction of the arc segment. Because we have drawn a positive circle, this parameter is useless and is set to 0;
◆ Large-arc-flag: large angle or small angle. Simply put, it is the half side of the circle, and 1 indicates the large angle. Because we drew an arc of 65% and a relatively large half, we entered 1. The program makes a judgment;
◆ Sweep-flag: The direction around the center. 1 indicates clockwise, and 0 indicates counterclockwise. The first arc is clockwise and the second arc is clockwise;
◆ X and y: the end coordinate of the arc;
For more information, refer to this article: Workshop.
PS: If you don't need a gradient, just draw a rough arc, which is much simpler.
PPS: you do not have to draw a positive circle. You can also change it to an elliptical progress bar.
This article from the rabbit nest blog, please be sure to keep this source http://boytnt.blog.51cto.com/966121/1074215