当前位置:   article > 正文

MPAndroidChart3使用详解9:RadarChart(雷达图)顶角显示圆点——重写RadarChart控件添加顶角圆点绘制方案_mpandroidchart 圆形雷达图

mpandroidchart 圆形雷达图

目录

 

1 思路:

2 实现:

3 效果:


1 思路:

查看RadarChart控件源码,找到绘制网线的相关源码,获取到各个顶点的坐标后,便可以在各个顶点绘制圆。

Step 1:既然圆点是在网线各个顶点,那便跟绘制网线相关联,我们可以从绘制网线的公开方法入手去查找,最先想到就是setDrawWeb方法,直接影响网线的绘制,这个方法也很简单只设置了mDrawWeb的值。

Step 2:接下来我们要查找变量mDrawWeb在绘制中的实际已应用,绘制便跟onDraw方法有关。可以看出绘制网线时,调用了mRenderer变量的drawExtras(canvas)

Step 3:查找mRenderer变量的具体对象

Step 4:查找RadarChartRenderer对象中drawExtras(canvas)方法到底做了什么事情。

Step 5:drawExtras(canvas)方法中如下这段代码是绘制每段网线的,从中可以获取到各个顶点的坐标。

Step 6:通过该坐标在里面进行圆点的绘制,由于我们无法直接编辑源码,我们还需要重写RadarChart和RadarChartRenderer这两个类。

2 实现:

Step 1:新建我们自己的RadarChartRenderer类继承RadarChartRenderer,重写drawWeb(Canvas c)方法,里面内容直接拷贝原方法代码后在指定加上如下这段代码。

  1. for (int j = 0; j < labelCount; j++) {
  2. for (int i = 0; i < mChart.getData().getEntryCount(); i++) {
  3. float r = (mChart.getYAxis().mEntries[j] - mChart.getYChartMin()) * factor;
  4. Utils.getPosition(center, r, sliceangle * i + rotationangle, p1out);
  5. Utils.getPosition(center, r, sliceangle * (i + 1) + rotationangle, p2out);
  6. c.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, mWebPaint);
  7. // start(加上下面这几行代码,用于绘制每个顶角上的圆)
  8. if (mDrawAngleCircle && j == labelCount - 1) {
  9. mAngleCirclePaint.setColor(mAngleCircleColors == null || mAngleCircleColors.length == 0 ? Color.BLACK : this.mAngleCircleColors[i % mAngleCircleColors.length]);
  10. c.drawCircle(p2out.x, p2out.y, mAngleCircleRadius, mAngleCirclePaint);
  11. }
  12. // end
  13. }
  14. }

完整代码:

  1. import android.graphics.Canvas;
  2. import android.graphics.Color;
  3. import android.graphics.Paint;
  4. import com.github.mikephil.charting.animation.ChartAnimator;
  5. import com.github.mikephil.charting.charts.RadarChart;
  6. import com.github.mikephil.charting.utils.MPPointF;
  7. import com.github.mikephil.charting.utils.Utils;
  8. import com.github.mikephil.charting.utils.ViewPortHandler;
  9. public class RadarChartRenderer extends com.github.mikephil.charting.renderer.RadarChartRenderer {
  10. private Paint mAngleCirclePaint;
  11. private float mAngleCircleRadius = Utils.convertDpToPixel(5f);
  12. private int[] mAngleCircleColors;
  13. private boolean mDrawAngleCircle = false;
  14. public RadarChartRenderer(RadarChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
  15. super(chart, animator, viewPortHandler);
  16. mAngleCirclePaint = new Paint();
  17. }
  18. @Override
  19. protected void drawWeb(Canvas c) {
  20. float sliceangle = mChart.getSliceAngle();
  21. // calculate the factor that is needed for transforming the value to
  22. // pixels
  23. float factor = mChart.getFactor();
  24. float rotationangle = mChart.getRotationAngle();
  25. MPPointF center = mChart.getCenterOffsets();
  26. // draw the web lines that come from the center
  27. mWebPaint.setStrokeWidth(mChart.getWebLineWidth());
  28. mWebPaint.setColor(mChart.getWebColor());
  29. mWebPaint.setAlpha(mChart.getWebAlpha());
  30. final int xIncrements = 1 + mChart.getSkipWebLineCount();
  31. int maxEntryCount = mChart.getData().getMaxEntryCountSet().getEntryCount();
  32. MPPointF p = MPPointF.getInstance(0,0);
  33. for (int i = 0; i < maxEntryCount; i += xIncrements) {
  34. Utils.getPosition(
  35. center,
  36. mChart.getYRange() * factor,
  37. sliceangle * i + rotationangle,
  38. p);
  39. c.drawLine(center.x, center.y, p.x, p.y, mWebPaint);
  40. }
  41. MPPointF.recycleInstance(p);
  42. // draw the inner-web
  43. mWebPaint.setStrokeWidth(mChart.getWebLineWidthInner());
  44. mWebPaint.setColor(mChart.getWebColorInner());
  45. mWebPaint.setAlpha(mChart.getWebAlpha());
  46. int labelCount = mChart.getYAxis().mEntryCount;
  47. MPPointF p1out = MPPointF.getInstance(0,0);
  48. MPPointF p2out = MPPointF.getInstance(0,0);
  49. for (int j = 0; j < labelCount; j++) {
  50. for (int i = 0; i < mChart.getData().getEntryCount(); i++) {
  51. float r = (mChart.getYAxis().mEntries[j] - mChart.getYChartMin()) * factor;
  52. Utils.getPosition(center, r, sliceangle * i + rotationangle, p1out);
  53. Utils.getPosition(center, r, sliceangle * (i + 1) + rotationangle, p2out);
  54. c.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, mWebPaint);
  55. // start(加上下面这几行代码,用于绘制每个顶角上的圆)
  56. if (mDrawAngleCircle && j == labelCount - 1) {
  57. mAngleCirclePaint.setColor(mAngleCircleColors == null || mAngleCircleColors.length == 0 ? Color.BLACK : this.mAngleCircleColors[i % mAngleCircleColors.length]);
  58. c.drawCircle(p2out.x, p2out.y, mAngleCircleRadius, mAngleCirclePaint);
  59. }
  60. // end
  61. }
  62. }
  63. MPPointF.recycleInstance(p1out);
  64. MPPointF.recycleInstance(p2out);
  65. }
  66. /**
  67. * 顶角圆点颜色
  68. * @param colors 颜色
  69. */
  70. public void setAngleCircleColor(int[] colors) {
  71. this.mAngleCircleColors = colors;
  72. }
  73. /**
  74. * 顶角圆点半径
  75. * @param radius 半径
  76. */
  77. public void setAngleCircleRadius(float radius) {
  78. this.mAngleCircleRadius = Utils.convertDpToPixel(radius);
  79. }
  80. /**
  81. * 是否绘制顶角圆点
  82. * @param draw
  83. */
  84. public void setDrawAngleCircle(boolean draw) {
  85. this.mDrawAngleCircle = draw;
  86. }
  87. }

 Step 2:新建我们自己的RadarChart类继承RadarChart,重写init()方法。

  1. @Override
  2. protected void init() {
  3. super.init();
  4. this.mRenderer = new RadarChartRenderer(this, mAnimator, mViewPortHandler);
  5. }

完整代码:

  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. public class RadarChart extends com.github.mikephil.charting.charts.RadarChart {
  4. public RadarChart(Context context) {
  5. super(context);
  6. }
  7. public RadarChart(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. }
  10. public RadarChart(Context context, AttributeSet attrs, int defStyle) {
  11. super(context, attrs, defStyle);
  12. }
  13. @Override
  14. protected void init() {
  15. super.init();
  16. this.mRenderer = new RadarChartRenderer(this, mAnimator, mViewPortHandler);
  17. }
  18. /**
  19. * 是否绘制顶角圆点
  20. * @param draw
  21. */
  22. public void setDrawAngleCircle(boolean draw) {
  23. ((RadarChartRenderer) this.mRenderer).setDrawAngleCircle(draw);
  24. }
  25. /**
  26. * 顶角圆点颜色
  27. * @param colors 颜色
  28. */
  29. public void setAngleCircleColor(int[] colors) {
  30. ((RadarChartRenderer) this.mRenderer).setAngleCircleColor(colors);
  31. }
  32. /**
  33. * 顶角圆点半径
  34. * @param radius 半径
  35. */
  36. public void setAngleCircleRadius(float radius) {
  37. ((RadarChartRenderer) this.mRenderer).setAngleCircleRadius(radius);
  38. }
  39. }

  Step 3:在界面中,使用我们自己RadarChart,再开启绘制顶点圆点,设置颜色。

  1. rc.setDrawAngleCircle(true);
  2. rc.setAngleCircleColor(new int[] {
  3. Color.parseColor("#36a9ce"),
  4. Color.parseColor("#33ff66"),
  5. Color.parseColor("#ef5aa1"),
  6. Color.parseColor("#ff0000"),
  7. Color.parseColor("#6600ff")
  8. });

 

3 效果:

雷达图的使用教程:https://blog.csdn.net/Honiler/article/details/90407390 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/114701?site
推荐阅读
相关标签
  

闽ICP备14008679号