MPAndroidChart系列源码解读(三)

ComponentBase
AxisBase
XAxis
Legend
AxisBase
LimitLine
MarkerView

ComponentBase这个抽象类封装了两个轴的共同特性,xy轴的文字的大小颜色字体焦点以及文字在轴上的偏移量相关的set get方法。默认间偏移量5,字体大小10

AxisBase为轴线基类,默认周线与网格线同色Color.GRAY

public abstract class AxisBase extends ComponentBase {

    private int mGridColor = Color.GRAY;

    private float mGridLineWidth = 1f;

    private int mAxisLineColor = Color.GRAY;

    private float mAxisLineWidth = 1f;
    //.........................
  }

提供对轴线网格线的基本属性的set get enable方法

我们可以通过addLimitLine方法添加一个新的轴.超过六条周线会在log日志输出

   /**
     * 添加一个新的轴.
     *
     * @param l
     */
    public void addLimitLine(LimitLine l) {
        mLimitLines.add(l);

        if (mLimitLines.size() > 6) {
            Log.e("MPAndroiChart",
                    "Warning! You have more than 6 LimitLines on your axis, do you really want " +
                            "that?");
        }
    }

对于轴线的操作不仅仅是添加,否则这样就不美了

下面这些方法是对于网格线轴线的设置,限定相应轴上的最大值和最小值,以及回复重置轴上的最大值相关方法

YAxis内部定义的许多常量配置不再细说了,日后会在实战simple里提到,这里只看一些相对重要的知识,labcount的获取源码里面做了重构,在特定情况下不会返回真是数据

 public void setLabelCount(int count, boolean force) {

        if (count > 25)
            count = 25;
        if (count < 2)
            count = 2;

        mLabelCount = count;
        mForceLabels = force;
    }

setShowOnlyMinMax控制轴上的Lable只显示最大值和最小值,setInverted方法控制图标显示倒转

/**
     *只显示最大值和最小值的Lable
     * @param enabled
     */
    public void setShowOnlyMinMax(boolean enabled) {
        mShowOnlyMinMax = enabled;
    }

    /**
     * 图表倒转显示
     *
     * @param enabled
     */
    public void setInverted(boolean enabled) {
        mInverted = enabled;
    }

轴线从0坐标开始这是不推荐的,最好根据数据集来。绘制Y轴上的标签默认位于轴下方,具体使用参考下面

 /**
     * the position of the y-labels relative to the chart
     */
    private YAxisLabelPosition mPosition = YAxisLabelPosition.OUTSIDE_CHART;

    /**
     * enum for the position of the y-labels relative to the chart
     */
    public enum YAxisLabelPosition {
        OUTSIDE_CHART, INSIDE_CHART
    }

XAxis轴的显示值mValues,文字间隔mSpaceBetweenLabels=4,lable显示位置参考下面

  /** the position of the x-labels relative to the chart */
    private XAxisPosition mPosition = XAxisPosition.TOP;

    /** enum for the position of the x-labels relative to the chart */
    public enum XAxisPosition {
        TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
    }

默认文字间隔4,也可以手动设置

   public void setSpaceBetweenLabels(int spaceCharacters) {
        mSpaceBetweenLabels = spaceCharacters;
    }

避免轴的文字标签在显示可见区域的第一个和最后一个标签被遮盖

public void setAvoidFirstLastClipping(boolean enabled) {
        mAvoidFirstLastClipping = enabled;
    }

设置X轴上显示的Lable

 public void setValues(List<String> values) {
        mValues = values;
    }

Legend和LimitLine都是从顶层基类ComponentBase继承的,一般情况不会用到,上面也有提到添加移除轴线的使用,具体实战simple再仔细了解,自定义控件MarkerView是一个抽象的控件,在原作者提供的Simple里面有具体实现类StackedBarsMarkerView,用于触摸图标区域弹出提示,显示对于的数值。核心方法refreshContent回调函数用于刷新视图

 @Override
    public void refreshContent(Entry e, Highlight highlight) {

        if (e instanceof BarEntry) {

            BarEntry be = (BarEntry) e;

            if(be.getVals() != null) {

                // draw the stack value
                tvContent.setText("" + Utils.formatNumber(be.getVals()[highlight.getStackIndex()], 0, true));
            } else {
                tvContent.setText("" + Utils.formatNumber(be.getVal(), 0, true));
            }
        } else {

            tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true));
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值