com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1画图

MPAndroidChart画图

导入架包,是3.1.0版本的哦。

implementation 'com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1'

xml布局中设置图形显示的位置,加id。

 <com.github.mikephil.charting.charts.PieChart

       android:id="@+id/pieChart"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

Java类中的核心代码!!!

public class Paintyuan extends AppCompatActivity {
    PieChart pieChart;
    ImageView imageView;
    ArrayList<JSONObject> arrayList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuan);
        
        pieChart = findViewById(R.id.pieChart);
        PieData pieData = getChartData(2, 100);
        showChart(pieChart, pieData);
    }

    public void showChart(PieChart pieChart, PieData pieData) {
        pieChart.setHoleRadius(0f);
        pieChart.setTransparentCircleRadius(0f);
        pieChart.setRotationEnabled(false);
        pieChart.setExtraOffsets(0, 12, 0, 12);
        pieChart.setData(pieData);
        Legend legend = pieChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        String de = "平台上车辆违章占比";
        Description description = new Description();
        description.setText(de);
        description.setTextSize(20);
        description.setTextAlign(Paint.Align.CENTER);
        description.setPosition(900, 50);
        pieChart.setDescription(description);

    }
    public PieData getChartData(float count, float ranges) {
        ArrayList<PieEntry> xValues = new ArrayList<PieEntry>();
        xValues.add(new PieEntry(40, "有违章"));
        xValues.add(new PieEntry(60, "无违章"));
        PieDataSet pieDataSet = new PieDataSet(xValues, null);
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(Color.BLUE);
        colors.add(Color.RED);
        pieDataSet.setColors(colors);
        PieData pieData = new PieData(pieDataSet);
        return pieData;
    }
}

com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1画图_第1张图片
让你伤脑子的饼图就完成了!

感觉少了点,那再加个折线图吧!

   <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lc"
        android:layout_width="600dp"
        android:layout_marginLeft="50dp"
        android:layout_height="300dp"/>
public class MainActivity extends AppCompatActivity {
    private LineChart lc;

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


        lc = (LineChart) findViewById(R.id.lc);
        lc.setExtraOffsets(24f, 24f, 24f, 24f);
//        lc.setDescription(null);
        setLegend(); // 设置图例
        setYAxis(); // 设置Y轴
        setXAxis(); // 设置X轴
        setLineData();

    }

    private void setLegend() {
        Legend legend = lc.getLegend();
//        legend.setFormSize(0);

    }

    private void setYAxis() {
        YAxis yAxis = lc.getAxisLeft();
        yAxis.setDrawGridLines(false);
        yAxis.setAxisMaximum(10);
        yAxis.setAxisMinimum(-20);
        lc.getAxisRight().setEnabled(false);

    }
    private void setXAxis() {
        XAxis xAxis = lc.getXAxis();
        xAxis.setAxisMaximum(5);
        xAxis.setAxisMinimum(0);
        xAxis.setLabelCount(5);
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    }

    private void setLineData() {
        ArrayList<Entry> yValues = new ArrayList<Entry>();
        float[] arr = new float[]{-12,-2,-10,-9,-12,-10};
        for (int i = 0; i < arr.length; i++) {
            yValues.add(new Entry(i , arr[i]));
        }
        LineDataSet lineDataSet = new LineDataSet(yValues, null);
        lineDataSet.setLineWidth(2);
        lineDataSet.setColor(Color.GRAY);
        lineDataSet.setCircleColor(Color.GRAY);
        LineData lineData = new LineData(lineDataSet);
        lineData.setValueTextSize(15);
        lc.setData(lineData);

    }


}

com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1画图_第2张图片
第一次发博客,有些不足的地方请告知下

你可能感兴趣的:(com.mysugr.MPAndroidChart:MPAndroidChart:3.1.0-mysugr-1画图)