@@ -10,48 +10,48 @@ export default class Graph extends Component {
10
10
}
11
11
12
12
componentDidMount ( ) {
13
- this . draw ( ) ;
13
+ this . createGraph ( ) ;
14
14
}
15
15
16
- draw ( ) {
17
- let outerCanvasContext = this . refs . canvas . getContext ( "2d" ) ;
18
- outerCanvasContext . beginPath ( ) ;
19
- outerCanvasContext . arc ( 100 , 75 , 50 , 0 , 2 * Math . PI ) ;
20
- outerCanvasContext . strokeStyle = "#000000" ;
21
- outerCanvasContext . stroke ( ) ;
22
- outerCanvasContext . save ( ) ;
23
- let innerCanvasContext = this . refs . canvas . getContext ( "2d" ) ;
24
- innerCanvasContext . beginPath ( ) ;
25
- let divideIntoParts = 5 , currDivision = 0 ;
26
- let x1 = 100 , y1 = 75 ;
27
- this . update ( currDivision , divideIntoParts , innerCanvasContext , x1 , y1 ) ;
16
+ createGraph ( ) {
17
+ let canvasContext = this . refs [ this . props . name ] . getContext ( '2d' ) , timeToTakeInSeconds = 25 , current = 0 ;
18
+ this . clearAndDraw ( canvasContext , timeToTakeInSeconds , current ) ;
28
19
}
29
20
30
- update ( currIteration , divideIntoParts , innerCanvasContext , x1 , y1 ) {
31
- if ( currIteration < divideIntoParts ) {
32
- let angle = ( this . props . percentage / 100 ) * 2 * ( currIteration + 1 ) / divideIntoParts * Math . PI ;
33
- console . log ( 'currDivision :: ' , currIteration ) ;
34
- innerCanvasContext . clearRect ( 0 , 0 , innerCanvasContext . width , innerCanvasContext . height ) ;
35
- innerCanvasContext . arc ( x1 , y1 , 45 , 0 , angle ) ;
36
- innerCanvasContext . strokeStyle = this . props . color ;
37
- innerCanvasContext . font = "15px Arial" ;
38
- // innerCanvasContext.fillText(this.state.percentage, x1, y1);
39
- innerCanvasContext . closePath ( ) ;
40
- innerCanvasContext . stroke ( ) ;
41
- this . setState ( { percentage : ( currIteration + 1 ) / divideIntoParts * this . props . percentage } ) ;
42
- setTimeout ( ( ) => {
43
- this . update ( ++ currIteration , divideIntoParts , innerCanvasContext , x1 , y1 , this . state . percentage ) ;
44
- } , 1000 ) ;
21
+ clearAndDraw ( canvasContext , timeToTakeInSeconds , current ) {
22
+ let innerRadius = 45 , outerRadius = 50 , startAngle = 0 , fullCircleAngle = 2 * Math . PI ,
23
+ currText = ( current * this . props . percentage / timeToTakeInSeconds ) ,
24
+ currAngle = ( currText * fullCircleAngle ) / 100 ;
25
+ canvasContext . clearRect ( 0 , 0 , 400 , 400 ) ;
26
+ this . drawArc ( canvasContext , 100 , 100 , outerRadius , startAngle , fullCircleAngle , null , "black" ) ;
27
+ this . drawArc ( canvasContext , 100 , 100 , innerRadius , startAngle , currAngle ,
28
+ ( current * this . props . percentage ) / timeToTakeInSeconds ) ;
29
+ current ++ ;
30
+ if ( current < timeToTakeInSeconds + 1 ) {
31
+ requestAnimationFrame ( ( ) => {
32
+ this . clearAndDraw ( canvasContext , timeToTakeInSeconds , current ) ;
33
+ } ) ;
45
34
}
46
35
}
47
36
37
+ drawArc ( context , xPos , yPos , radius , startAngle , endAngle , text , color ) {
38
+ context . beginPath ( ) ;
39
+ context . arc ( xPos , yPos , radius , startAngle , endAngle ) ;
40
+ if ( text ) {
41
+ context . fillText ( text , xPos - 8 , yPos + 10 ) ;
42
+ context . font = "15px Arial" ;
43
+ }
44
+ context . strokeStyle = color ? color : this . props . color ;
45
+ context . stroke ( ) ;
46
+ }
47
+
48
48
render ( ) {
49
- let borderStyle = { border : "2px" } , percentageStyle = { "marginLeft" : "-100px" } ,
49
+ let borderStyle = { border : "2px" } , // percentageStyle = {"marginLeft": "-100px"},
50
50
nameStyle = { "marginLeft" : "-86px" } ;
51
51
return (
52
52
< span >
53
- < canvas ref = "canvas" width = "300" height = "150" style = { borderStyle } > </ canvas >
54
- < div style = { percentageStyle } > { this . state . percentage } </ div >
53
+ < canvas ref = { this . props . name } width = "300" height = "150" style = { borderStyle } > </ canvas >
54
+ { /* <div style={percentageStyle}>{this.state.percentage}</div>*/ }
55
55
< div style = { nameStyle } > { this . props . name } </ div >
56
56
</ span >
57
57
) ;
0 commit comments