MPAndroidChart 整理

这篇博客详细介绍了如何在Android项目中使用MPAndroidChart库进行数据图表的展示。作者提供了集成步骤、布局和Activity代码示例,以及相关类的代码片段。此外,还分享了自定义MarkerView和RadarMarkerview的实现,并提示了资源文件的处理方法。通过提供的下载链接,读者可以获取完整的可运行代码。
摘要由CSDN通过智能技术生成

近来由于业务需求,要做图表类型的数据展示,故想到了

MPAndroidChart

先上图:

 

由于是Pad上的截图,所以图片有点大;

首先说MPAndroidChart的集成;

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
maven { url "https://jitpack.io" }

 

集成后直接上代码:

1、布局代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="年份柱状图" />

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/yearBarChart"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_centerVertical="true" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="带文字的柱状图" />

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/mBarChart"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_centerVertical="true" />
            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="横向的柱状图" />

                <com.github.mikephil.charting.charts.HorizontalBarChart
                    android:id="@+id/hBarChart"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_centerVertical="true" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="雷达图" />

                <com.github.mikephil.charting.charts.RadarChart
                    android:id="@+id/radarChart"
                    android:layout_width="match_parent"
                    android:layout_height="200dp" />
            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="折现统计图" />

                <com.github.mikephil.charting.charts.LineChart
                    android:id="@+id/lineChart"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_centerVertical="true" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="折线图第二种" />

                <com.github.mikephil.charting.charts.LineChart
                    android:id="@+id/lineChart2"
                    android:layout_width="match_parent"
                    android:layout_height="200dp" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="第三种折线图" />

                <com.github.mikephil.charting.charts.LineChart
                    android:id="@+id/lineChart3"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_centerVertical="true" />
            </LinearLayout>



        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="饼状图" />

            <com.github.mikephil.charting.charts.PieChart
                android:id="@+id/mPieChart"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

 

2、再上Activity代码:

public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {

    private BarChart yearBarChart;
    private BarChart mBarChart;
    private HorizontalBarChart hBarChart;
    private RadarChart radarChart;
    private LineChart lineChart;
    private LineChart lineChart2;
    private LineChart lineChart3;
    private PieChart mPieChart;
    private Typeface tf;

    protected final String[] parties = new String[] {
            "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
            "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
            "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
            "Party Y", "Party Z"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initYearBarChart();
        initmBarChar();
        inithBarChart();
        initRadarChart();
        initLineC
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值