Android MPAndroidChart使用教程和源码分析(四)

一.概述

    MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。

GitHub地址:

https://github.com/PhilJay/MPAndroidChart


二.实例讲解

下面先以BarChart为例讲解一下在Chart类的基础上,开发者为BarChart准备的实例化好的组成部分。

经过前两篇文章的分析,我们知道一个基本的Chart实例应该是是有这么几部分组成的:

(1)DataRenderer(数据渲染器)

(2)Legend(图例)

(3)Axis(坐标轴)

(4)Listener(一开始我们分析,Chart类的监听器有两个,但是经过源码分析,我们得知Chart的基本监听器有三个)

(5)Animator:动画显示
(6)Data:图表数据(根源的数据怎么能忘记呢)
(7)MarkerView
(8)HighLighter(高亮覆盖显示)
(9)ViewPortHandler(暂时理解为绘图区域)
下面呢,我将以BarChart为例讲解Chart的这些基本组成部分都是怎么被具像化,怎么样工作的:
昨天的blog中我们讲解chart类中最重要的DataRenderer 这个类的工作方式,并且讲解了如何使用Matrix(transformer和mViewPortHandler中的matrix)和Buffer类完成对Chartitem位置的确定。以及我们怎么确认高亮,并且绘制高亮。
今天我们把剩下的部分讲解一下,主要有坐标轴,图例,MarkerView,ViewPortHandler这四个部分。
首先我们讲AxisRenderer,这是关于坐标轴的渲染器,首先我们看一下BarChart的 XAxisRendererBarChart

public XAxisRendererBarChart(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans,
        BarChart chart) {
    super(viewPortHandler, xAxis, trans);
    this.mChart = chart;
}

/**
 * draws the x-labels on the specified y-position
 * 
 * @param pos
 */
@Override
protected void drawLabels(Canvas c, float pos, PointF anchor) {

    final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
    // pre allocate to save performance (dont allocate in loop)
    float[] position = new float[] {
            0f, 0f
    };
    BarData bd = mChart.getData();
    int step = bd.getDataSetCount();
    for (int i = mMinX; i <= mMaxX; i += mXAxis.mAxisLabelModulus) {
        position[0] = i * step + i * bd.getGroupSpace()
                + bd.getGroupSpace() / 2f;//
        // consider groups (center label for each group)
        if (step > 1) {
            position[0] += ((float) step - 1f) / 2f;
        }
        mTrans.pointValuesToPixel(position);
        if (mViewPortHandler.isInBoundsX(position[0]) && i >= 0
                && i < mXAxis.getValues().size()) {

            String label = mXAxis.getValues().get(i);//得到标签数值

            if (mXAxis.isAvoidFirstLastClippingEnabled()) {

                // avoid clipping of the last
                if (i == mXAxis.getValues().size() - 1) {
                    float width = Utils.calcTextWidth(mAxisLabelPaint, label);

                    if (position[0] + width / 2.f > mViewPortHandler.contentRight())
                        position[0] = mViewPortHandler.contentRight() - (width / 2.f);

                    // avoid clipping of the first
                } else if (i == 0) {

                    float width = Utils.
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值