近来由于业务需求,要做图表类型的数据展示,故想到了
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