Android dynamic pie chart example code
For the requirements in the project, the mpandroidchart library was found at that time, which can be implemented, but it's just a requirement. I don't feel very comfortable referring to such a large library. I plan to customize one myself.
1、 Effect drawing first
Update graph
2、 GitHub
Code address, welcome to correct https://github.com/MNXP/XPPieChart
3、 Train of thought
1. Hollow diagram (draw a small circle in the center of a large circle) 2. Calculate the angle according to the data 3. Obtain the current painted angle according to the animation 4. Obtain the color used by paint according to the current angle 5. Dynamically paint the parts to be painted and the parts already painted (the most important)
4、 Realize
1. Hollow diagram (draw a small circle in the center of a large circle) initialization data
2. Calculate the angle according to the data
Use recursion to ensure that the sum of the values of cakevalues must be 100, and then calculate the angle according to the value
3. Gets the currently painted angle from the animation
Curangle is the current drawing angle, and drawarc () is the drawing method
4. Gets the color used by paint based on the current angle
Calculate the current number of items according to the current angle through paint. SetColor (color. Parsecolor (cakevalues. Get (colorindex). Getcolors()); To set the color of the paint
5. Dynamically draw the part to be drawn and the part already drawn
The most important step, my needs are 4 categories, with different colors
Draw the sector of the current color, the starting position of curstartangle sector, and the ending position of cursweetangle sector
Draw the fan that has been drawn. Judge the drawn sector according to curitem
Draw a circle in the center
6. Special attention
Isfirst determines whether it is enough to draw for the first time (after drawing, press home to enter the background and enter again. Dynamic drawing is not required)
Isdrawbyanim determines whether animation is required
Drawcake() draws a pie chart statically
to update
Add stereo effect and extract configuration parameters
Used in XML
The above is a simple implementation of dynamic drawing pie chart, which needs to be improved. I hope it will be helpful for everyone's learning, and I hope you can support programming tips.