MPAndroidChart学习笔记----LineChart

本文介绍了一种在Android应用中使用LineChart组件绘制折线图的方法,包括初始化图表样式、设置折线属性及动画效果等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上一个类:
package com.puhui.microfinance.ui.user;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import com.jn.chart.animation.Easing;
import com.jn.chart.charts.LineChart;
import com.jn.chart.components.Legend;
import com.jn.chart.components.LimitLine;
import com.jn.chart.components.XAxis;
import com.jn.chart.components.YAxis;
import com.jn.chart.data.Entry;
import com.jn.chart.data.LineData;
import com.jn.chart.data.LineDataSet;
import com.jn.chart.interfaces.datasets.ILineDataSet;
import com.jn.chart.utils.Utils;
import com.puhui.microfinance.R;

import java.util.ArrayList;

/**
 * Copyright: 人人普惠
 * Created by TangJian on 2017/3/6.
 * Description:
 * Modified: by TangJian on 2017/3/6.
 */

public class LineChartManager {

    private static String lineName = null;

    /**
     * @param context    上下文
     * @param mLineChart 折线图控件
     * @param xValues    折线在x轴的值
     * @param yValue     折线在y轴的值
     * @Description 创建两条折线
     */
    public static void initSingleLineChart(Context context, LineChart mLineChart, ArrayList<String> xValues,
                                           ArrayList<Entry> yValue) {
        initDataStyle(context, mLineChart);
        //设置折线的样式
        LineDataSet dataSet = new LineDataSet(yValue, lineName);
        dataSet.enableDashedLine(10f, 0f, 0f);
        dataSet.enableDashedHighlightLine(10f, 0f, 0f);
        dataSet.setColor(Color.BLACK);
        dataSet.setCircleColor(Color.BLACK);
        dataSet.setLineWidth(1f);
        dataSet.setCircleRadius(2f); //折线点半径
        dataSet.setDrawCircleHole(false);
        dataSet.setValueTextSize(9f);
        dataSet.setDrawFilled(true); //设置是否填充,只有该值为true时,下面的fade_red才有效
        dataSet.setDrawValues(true);  //设置是否显示每个点的值
        dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);  //设置是曲线还是直线
//        dataSet.setValueFormatter(new PercentFormatter(new DecimalFormat("%").format()));
	//设置折线阴影
        if (Utils.getSDKInt() >= 18) {
            // fill drawable only supported on api level 18 and above
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.fade_red);
            dataSet.setFillDrawable(drawable);
        } else {
            dataSet.setFillColor(Color.BLACK);
        }

        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(dataSet);

        //构建一个LineData  dataSets放入
        LineData lineData = new LineData(xValues, dataSets);
        //将数据插入
        mLineChart.setData(lineData);

        //设置动画效果
        mLineChart.animateY(2000, Easing.EasingOption.Linear);
        mLineChart.animateX(2000, Easing.EasingOption.Linear);
        mLineChart.invalidate();
    }

    /**
     * @param mLineChart
     * @Description:初始化图表的样式
     */
    private static void initDataStyle(Context context, LineChart mLineChart) {
        //设置图表是否支持触控操作
        mLineChart.setTouchEnabled(false);
        mLineChart.setScaleEnabled(false);
        mLineChart.setDrawGridBackground(false);
        // no description text
//        mLineChart.getDescription().setEnabled(false);
        // enable scaling and dragging
        mLineChart.setDragEnabled(false);  //拖拽
        mLineChart.setScaleEnabled(false); //缩放
        // mChart.setScaleXEnabled(true);
        // mChart.setScaleYEnabled(true);
        // if disabled, scaling can be done on x- and y-axis separately
        mLineChart.setPinchZoom(false);

        //设置点击折线点时,显示其数值
//        MyMakerView mv = new MyMakerView(context, R.layout.item_mark_layout);
//        mLineChart.setMarkerView(mv);
        //设置折线的描述的样式(默认在图表的左下角)
        Legend title = mLineChart.getLegend();
        title.setForm(Legend.LegendForm.LINE);
        //设置x轴的样式
        XAxis xAxis = mLineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //设置X轴所在位置,是顶部还是底部
//        xAxis.setAxisLineColor(Color.parseColor("#66CDAA"));
        xAxis.setAxisLineWidth(1);
        xAxis.setDrawGridLines(false);
        //设置是否显示x        xAxis.setEnabled(true);

        Typeface tf = Typeface.createFromAsset(context.getAssets(), "OpenSans-Regular.ttf");

        //在改了YAxisRenderer类第130140行代码后,此处第二个参数需设置为空
        LimitLine ll1 = new LimitLine(3000f, "");
        ll1.setLineWidth(1f);
	//第一个参数:虚线长度; 第二个参数:虚线间隔长度; 第三个参数controls the starting point.
        ll1.enableDashedLine(10f, 10f, 0f);
        ll1.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);//限制描述显示的位置
        ll1.setTextSize(10f);
        ll1.setTypeface(tf);

        //设置左边y轴的样式
        YAxis yAxisLeft = mLineChart.getAxisLeft();
//        yAxisLeft.setAxisLineColor(Color.parseColor("#66CDAA"));
        yAxisLeft.setAxisLineWidth(1);
        yAxisLeft.addLimitLine(ll1);
        yAxisLeft.setDrawGridLines(false);

        yAxisLeft.setAxisMaxValue(10000f);
        yAxisLeft.setAxisMinValue(500f);
        //leftAxis.setYOffset(20f);
        //第一个参数:虚线长度; 第二个参数:虚线间隔长度; 第三个参数controls the starting point.
        yAxisLeft.enableGridDashedLine(10f, 10f, 0f);
        yAxisLeft.setDrawZeroLine(false);
//        yAxisLeft.setLabelCount(4, true);  //改变Y轴分布情况
        yAxisLeft.setShowOnlyMinMax(true);

        //设置右边y轴的样式
        YAxis yAxisRight = mLineChart.getAxisRight();
        yAxisRight.setEnabled(false);

        mLineChart.getLegend().setForm(Legend.LegendForm.EMPTY);

    }

    /**
     * @param name
     * @Description:设置折线的名称
     */
    public static void setLineName(String name) {
        lineName = name;
    }
}
简单修改了下YAxisRenderer.java类中的代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值