Tips for calculating the percentage of pie charts in a Canvas, canvas

Source: Internet
Author: User
Tags drawtext

Tips for calculating the percentage of pie charts in a Canvas, canvas

How to mark the percentage at the center of each slice on a Pie Chart. First upload and finally:




The Code is as follows:

Public class MyViewOne extends View {


Private int ScrWidth, ScrHeight;
Private float rx, ry;


Public MyViewOne (Context context ){
Super (context );
// TODO Auto-generated constructor stub


This. setLayerType (View. LAYER_TYPE_SOFTWARE, null );


// Screen information
DisplayMetrics dm = getResources (). getDisplayMetrics ();
ScrHeight = dm. heightPixels;
ScrWidth = dm. widthPixels;
}


@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );


Canvas. drawColor (Color. WHITE );

// Paint brush Initialization
Paint PaintArc = new Paint ();
PaintArc. setColor (Color. RED );

Paint PaintGree = new Paint ();
PaintGree. setColor (Color. GREEN );
PaintGree. setStyle (Style. FILL );

Paint PaintBlue = new Paint ();
PaintBlue. setColor (Color. BLUE );
PaintBlue. setStyle (Style. STROKE );

Paint PaintYellow = new Paint ();
PaintYellow. setColor (Color. YELLOW );
PaintYellow. setStyle (Style. FILL );

// Anti-aliasing
PaintArc. setAntiAlias (true );
PaintYellow. setAntiAlias (true );
PaintGree. setAntiAlias (true );

PaintBlue. setTextSize (12 );

Float cirX = ScrWidth/2;
Float cirY = ScrHeight/3;
Float radius = ScrHeight/5;
// Draw a circle first to determine the position of the lower display
Canvas. drawCircle (cirX, cirY, radius, PaintGree );

Float arcLeft = cirX-radius;
Float arcTop = cirY-radius;
Float arcRight = cirX + radius;
Float arcBottom = cirY + radius;
RectF arcRF0 = new RectF (arcLeft, arcTop, arcRight, arcBottom );

// Pie chart title
// Canvas. drawText ("author: xiongchuanliang", 60, ScrHeight-270, PaintBlue );

// Location Calculation class
XChartCalc xcalc = new XChartCalc ();

// Actual radius used for calculation
Float calcRadius = radius/2;

// Initial Angle
Float pAngle1 = 130f;
Float pAngle2 = 40f;
Float pAngle3 = 360f-pAngle1-pAngle2;

// Fill the slice
Canvas. drawArc (arcRF0, 0, pAngle1, true, PaintArc );

// Calculate and add the percentage of 130% on the slice center.
Xcalc. CalcArcEndPointXY (cirX, cirY, calcRadius, pAngle1/2 );
Canvas. drawText (Float. toString (pAngle1) + "%", xcalc. getPosX (), xcalc. getPosY (), PaintBlue );


// Fill the slice
Canvas. drawArc (arcRF0, pAngle1, pAngle2, true, PaintYellow );

// Calculate and add the percentage of 40% on the slice center.
Xcalc. CalcArcEndPointXY (cirX, cirY, calcRadius, pAngle1 + pAngle2/2 );
Canvas. drawText (Float. toString (pAngle2) + "%", xcalc. getPosX (), xcalc. getPosY (), PaintBlue );


// Calculate and add the percentage of 190% on the slice center.
Xcalc. CalcArcEndPointXY (cirX, cirY, calcRadius, pAngle1 + pAngle2 + pAngle3/2 );
Canvas. drawText (Float. toString (pAngle3) + "%", xcalc. getPosX (), xcalc. getPosY (), PaintBlue );


}


}



Public class XChartCalc {
// Position
Private float posX = 0.0f;
Private float posY = 0.0f;






// Calculate the xy coordinate of the final sector Ray and the arc intersection Based on the Center Coordinate, radius, and slice angle.
Public void CalcArcEndPointXY (float cirX, float cirY, float radius,
Float cirAngle ){


// Converts degrees to radians
Float arcAngle = (float) (Math. PI * cirAngle/180.0 );
If (cirAngle <90 ){
PosX = cirX + (float) (Math. cos (arcAngle) * radius;
PosY = cirY + (float) (Math. sin (arcAngle) * radius;
} Else if (cirAngle = 90 ){
PosX = cirX;
PosY = cirY + radius;
} Else if (cirAngle> 90 & cirAngle <180 ){
ArcAngle = (float) (Math. PI * (180-cirAngle)/180.0 );
PosX = cirX-(float) (Math. cos (arcAngle) * radius;
PosY = cirY + (float) (Math. sin (arcAngle) * radius;
} Else if (cirAngle = 180 ){
PosX = cirX-radius;
PosY = cirY;
} Else if (cirAngle> 180 & cirAngle <270 ){
ArcAngle = (float) (Math. PI * (cirAngle-180)/180.0 );
PosX = cirX-(float) (Math. cos (arcAngle) * radius;
PosY = cirY-(float) (Math. sin (arcAngle) * radius;
} Else if (cirAngle = 270 ){
PosX = cirX;
PosY = cirY-radius;
} Else {
ArcAngle = (float) (Math. PI * (360-cirAngle)/180.0 );
PosX = cirX + (float) (Math. cos (arcAngle) * radius;
PosY = cirY-(float) (Math. sin (arcAngle) * radius;
}


}




Public float getPosX (){
Return posX;
}


Public float getPosY (){
Return posY;
}
}


Related Article

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.