Android中使用MPAndroidChart-3绘制图表的基本使用--自学自学笔记

本文详细介绍了如何在Android应用中使用MPAndroidChart-3库进行图表绘制,包括饼状图、折线图、柱状图、复合型图表(折线和柱状)、水平柱状图以及上下对应的柱状图的实现步骤,适合自学Android编程的开发者参考。
摘要由CSDN通过智能技术生成

使用MPAndroidChart-3绘制图表的使用需要添加依赖(因为MPAndroidChart-3为第三方插件,并不是android原生的)依赖如下:

implementation 'com.github.PhilJay:MPAndroidChart:3.0.2'

目录

一、绘制饼状图

二、绘制折线图

三、柱状图

四、复合型--折现和柱状

 五、水平柱状图

 六、上下对应的柱状图


一、绘制饼状图

1、xml布局其实就需要一个控件:com.github.mikephil.charting.charts.PieChart

<com.github.mikephil.charting.charts.PieChart
        android:id="@+id/PieChart"
        android:layout_width="370dp"
        android:layout_height="370dp"
        android:layout_gravity="center"
        />

2、下面是主要的Java代码

public class PieFragment extends Fragment {

    private PieChart pieChart;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pie, container, false);
        into(view);
        SetPie();
        return view;
    }

    private void SetPie() {
        pieChart.animateXY(1000,1000);
        //设置绘不绘制中间空白
        pieChart.setDrawHoleEnabled(false);
        //设置手动旋转
        pieChart.setRotationEnabled(true);
        //设置默认旋转度数
        pieChart.setRotationAngle(0f);
        //取消右下角描述
        pieChart.getDescription().setEnabled(false);
        //设置文字描述为白色
        pieChart.setEntryLabelColor(Color.WHITE);
        //设置文字描述的大小
        pieChart.setEntryLabelTextSize(15f);
        //设置文字描述的样式
        pieChart.setEntryLabelTypeface(Typeface.DEFAULT_BOLD);
        //设置四个方向的偏移
        pieChart.setExtraOffsets(5,5,5,5);
        //设置图标的转动阻力摩擦系数
        pieChart.setDragDecelerationFrictionCoef(0.2f);

//        pieChart.setDrawEntryLabels(false);
        //得到图例
        Legend legend = pieChart.getLegend();
        //设置图例的大小,颜色和样式
        legend.setTextSize(25f);
        legend.setTextColor(Color.BLACK);
        legend.setTypeface(Typeface.DEFAULT_BOLD);
        //设置图例在水平方向的排列方式
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        //设置图例垂直方向的排列方式
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        //设置图例的排列方式
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //设置图例的样式

        //设置数据集合
        List<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(25.8f,"睡觉"));
        entries.add(new PieEntry(54.3f,"学习"));
        entries.add(new PieEntry(19.9f,"运动"));

        //设置显示的颜色
        List<Integer> Colors = new ArrayList<>();
        Colors.add(Color.parseColor("#010101"));
        Colors.add(Color.parseColor("#0000ff"));
        Colors.add(Color.parseColor("#00ff00"));

        //添加数据集合
        PieDataSet dataSet = new PieDataSet(entries,"");
        dataSet.setColors(Colors);
        dataSet.setValueTextSize(12f);
        dataSet.setValueTextColor(Color.WHITE);
        dataSet.setValueTypeface(Typeface.DEFAULT_BOLD);
        dataSet.setDrawValues(true);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.INSIDE_SLICE);
        dataSet.setSelectionShift(5f);
        PieData pieData = new PieData(dataSet);
        pieData.setValueFormatter(new PercentFormatter());
        pieChart.setData(pieData);
    }

    private void into(View view) {
        pieChart = view.findViewById(R.id.PieChart);
    }
}

二、绘制折线图

1、xml文件的布局如下:

<com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lc"
        android:layout_width="375dp"
        android:layout_height="360dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        />

2、对应的Java文件代码

public class zhexianFragment extends Fragment {
    private LineChart lc;
    private YAxis yAxis;
    String weeks[];
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_zhexian, container, false);

        into(root);
        SetDesc();
        SetData();
        SetLegend();
        SetYAxis();
        SetXAxis();
        SetHeightLimit(25f,"高温预警",Color.rgb(255,0,0));
        SetHeightLimit(10f,"低温预警",Color.rgb(0,0,230));
        return root;
    }


    private void into(View view) {
        lc = view.findViewById(R.id.lc);
    }

    /**
     * 设置折线图的警告线
     */
    private void SetHeightLimit(float height,String name,int color){
        //实例化警告线,并传输高度即警告先在图表中的位置,name 警告线叫什么名字
        LimitLine limitLine = new LimitLine(height,name);
        //设置警告先的宽度
        limitLine.setLineWidth(2f);
        //设置警告线上文本的颜色
        limitLine.setTextColor(color);
        //设置警告线的颜色
        limitLine.setLineColor(color);
        //设计警告线上文本的字体类型
        limitLine.setTypeface(Typeface.DEFAULT_BOLD);
        //设计警告线在x轴上的偏移量
//        limitLine.setXOffset();
        //应用警告线
        yAxis.addLimitLine(limitLine);
    }

    /**
     * 创建最原始的折线图,并进行折线线条样式的设置
     */
    private void SetData() {
        float datas[] = {19f,23f,16f,20f,20f,24f};
        float datat[] = {15f,9f,14f,24f,28f,18f};
        /**
         * 一般使用MPAndroidChart画图表的时候都是通过list对象来封装数据
         */
        List<Entry> entries = new ArrayList<>();
        List<Entry> entries1 = new ArrayList<>();
        //循环将数组中的元素放到list集合中
        for (int i = 0;i<datas.length;i++){
            entries.add(new Entry(i,datas[i]));
            entries1.add(new Entry(i,datat[i]));
        }
        //一个LineDataSet对象就是一条曲线,添加数据及图例
        LineDataSet lineDataSet = new LineDataSet(entries,"第一条数据");
        LineDataSet lineDataSet1 = new LineDataSet(entries1,"第二条数据");
        //设置折线圆点半径的大小
        lineDataSet1.setCircleRadius(3);
        //设置折线的宽度
        lineDataSet1.setLineWidth(1);
        //一次性设置所有圆点的颜色
//        lineDataSet.setCircleColor(Color.RED);
        //设置是否空心
        lineDataSet1.setDrawCircleHole(true);
        //依次设置每个点的颜色
        lineDataSet1.setCircleColors(Color.RED,Color.BLACK,Color.GREEN);
        //设置线条下方的填充
        lineDataSet1.setFillColor(Color.DKGRAY);
        //设置折线类型
        lineDataSet1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lineDataSet.setMode(LineDataSet.Mode.LINEAR);
        //设置曲线的Mode强度,0-1
        lineDataSet1.setCubicIntensity(0.2f);
        lineDataSet.setCubicIntensity(0.2f);
        //是否绘制折现下方的填充
        lineDataSet1.setDrawFilled(false);
        //设置折现的颜色,有三个构造方法
        lineDataSet1.setColor(Color.BLUE);
        //提交折线图给试图工具
        LineData lineData = new LineData(lineDataSet,lineDataSet1);
        //实例化MyMarKer
        MyMarker myMarker = new MyMarker(getActivity(),R.layout.marker_);
        //一定要设置该属性,不然有时候在边缘点击的时候可能会超出屏幕
        myMarker.setChartView(lc);
        //应用
        lc.setMarker(myMarker);
        lc.animateXY(1000,1000);
        //设置折线图的背景颜色
        lc.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        //应用视图
        lc.setData(lineData);
    }

    /**
     * 设置标题及其属性
     */
    private void SetDesc(){
        //声明并初始化这个文本设置类
        Description description = new Description();
        //设置折线图的标识文本
        description.setText("这是折线图的标题");
        //设置折线图文本的大小
        description.setTextSize(11f);
        //设置文本样式加粗显示
        description.setTypeface(Typeface.DEFAULT_BOLD);
        //设置文本颜色
        description.setTextColor(Color.RED);
        //设置X轴的偏移量,float值
        description.setXOffset(10f);
        //设置Y轴的偏移量,float值
        description.setYOffset(10f);
        //获取屏幕中间x轴的像素坐标
//        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
//        DisplayMetrics dm = new DisplayMetrics();
//        wm.getDefaultDisplay().getMetrics(dm);
//        float x = dm.widthPixels/2;
        //设置文本坐标,以控件左上角为标准,当x,y等于零的时候,标题不出现;可根据文字个数及大小定义x,y的值1f=x:20;
        description.setPosition(180,20);
        //最后在应用这个标题
        lc.setDescription(description);
    }

    /
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 MPAndroidChart 绘制横向圆角柱状图的步骤如下: 1. 在你的项目添加 MPAndroidChart 的依赖。 2. 在 XML 布局文件添加一个 BarChart 控件。 3. 在代码获取 BarChart 控件的实例,并进行一些基础设置,如设置 X 轴和 Y 轴的属性。 4. 创建一个 BarDataSet 对象,并设置数据。 5. 创建一个 BarData 对象,并将 BarDataSet 对象添加到 BarData 对象。 6. 设置 BarData 对象到 BarChart 控件,并进行一些样式设置,如设置柱状图的颜色、边框宽度等。 7. 最后调用 BarChart 控件的 invalidate() 方法刷新界面即可。 下面是一个示例代码,可以绘制横向圆角柱状图: ```java // 获取 BarChart 控件实例 BarChart barChart = findViewById(R.id.bar_chart); // 设置 X 轴和 Y 轴属性 barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); barChart.getXAxis().setDrawGridLines(false); barChart.getAxisLeft().setDrawGridLines(false); barChart.getAxisRight().setDrawGridLines(false); // 创建 BarDataSet 对象并设置数据 List<BarEntry> entries = new ArrayList<>(); entries.add(new BarEntry(0f, 5f)); entries.add(new BarEntry(1f, 6f)); entries.add(new BarEntry(2f, 7f)); entries.add(new BarEntry(3f, 8f)); entries.add(new BarEntry(4f, 9f)); BarDataSet dataSet = new BarDataSet(entries, "数据"); // 创建 BarData 对象并将 BarDataSet 添加到其 BarData data = new BarData(dataSet); // 设置 BarData 到 BarChart 控件 barChart.setData(data); // 设置柱状图的颜色、边框宽度等 dataSet.setColors(Color.parseColor("#3F51B5")); dataSet.setDrawValues(false); dataSet.setBarBorderWidth(1f); dataSet.setBarBorderColor(Color.parseColor("#3F51B5")); dataSet.setBarBorderRadius(20f); // 刷新界面 barChart.invalidate(); ``` 在上面的示例代码,我们设置了一个数据集合,包含了 5 个柱状图的数据(横坐标分别是 0 到 4),并设置了柱状图的样式,最后调用 invalidate() 方法刷新界面即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值