Android implements simple bar chart and curve chart example code

preface

A chart lib has been written before, but it is difficult to keep up with the pace of product demand changes due to the speed of development. Therefore, the original chart library is modified to support the integration of table to display the corresponding categories under the chart, replace broken lines with curves, support the display of multiple curves, increase the display animation and add some customizable attributes, It supports the display of horizontal bar chart, superimposed bar chart, and multi curve chart and pie chart. Let's take a look at the detailed introduction.

1. Renderings

2. Use of various charts

1. The pie chart is the same as the original use, but an animation is added. Please refer to the previous article, the use of pie chart.

2. Horizontal multi histogram

2.1 XML layout

And y_ visible_ Num: how many columns does the Y axis need to display

2.2 data setting

3. Superimposed histogram

3.1 XML layout

3.2 data setting, as in 2.2

4. Several key points of realization

4.1 the width needs to be rewritten, onmeasure, because the width of the control is greater than the width of the screen. The width is composed of the displayed X-axis points and spacing, and the width distance occupied by the y-axis coordinate text.

4.2 plan a fixed area, which is not visible in the part beyond the area. This is implemented in the bitmap used before. It always feels awkward. When you read the official source code, you know the canvas clipRect method. When we draw this piece, we call the onDraw method.

4.3 we can basically use valueanimator to realize animation, such as pie chart: one of his drawings is the change of angle from 0 to 360, so we can

Then use mdrawangle to control the angle of each drawing, so that you can have the feeling of drawing from 0-360 degrees. The animation of the histogram is the same, so as to keep unchanged and adapt to changes.

4.4 algorithm of Bezier curve drawing

When drawing Bezier curves, I carefully checked the calculation rules of these control points and calculated two control points according to three points. However, the curve drawn in this way is very smooth within the three points, but it doesn't feel very good when the next fourth point is connected, so I still used the above calculation method to calculate the control points, and I posted the algorithm, The parameters are the X and Y coordinates and bending coefficient of 1, 2 and 3, respectively

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>