MPchart 曲线图、柱状图、饼图

一。加入依赖库,在项目build.gradle下面的dependencies里面加入:

implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

二。因为网上资源比较多,注释也比较清楚,就直接贴上所有代码

1.两个管理类

public class LineChartManager {

    private LineChart lineChart;
    private YAxis leftAxis;   //左边Y轴
    private YAxis rightAxis;  //右边Y轴
    private XAxis xAxis;      //X轴

    public LineChartManager(LineChart mLineChart) {
        this.lineChart = mLineChart;
        leftAxis = lineChart.getAxisLeft();
        rightAxis = lineChart.getAxisRight();
        rightAxis.setDrawLabels(false);//右侧Y轴数据不显示
        xAxis = lineChart.getXAxis();
    }

    /**
     * 初始化LineChart
     */
    private void initLineChart() {
        lineChart.setDrawGridBackground(false);
        //显示边界
        lineChart.setDrawBorders(true);

        //设置动画效果
        lineChart.animateY(1000, Easing.EasingOption.Linear);
        lineChart.animateX(1000, Easing.EasingOption.Linear);

        //折线图例 标签 设置
        Legend legend = lineChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);

        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        //保证Y轴从0开始,不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);
    }

    /**
     * 初始化曲线 每一个LineDataSet代表一条线
     *
     * @param lineDataSet
     * @param color
     * @param mode        折线图是否填充
     */
    private void initLineDataSet(LineDataSet lineDataSet, int color, boolean mode) {
        lineDataSet.setColor(color);
        lineDataSet.setCircleColor(color);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setCircleRadius(3f);
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setValueTextSize(9f);
        //设置折线图填充
        lineDataSet.setDrawFilled(mode);
        lineDataSet.setFormLineWidth(1f);
        lineDataSet.setFormSize(15.f);
        //填充颜色.透明度
//        lineDataSet.setFillColor(Color.BLUE);
        lineDataSet.setFillAlpha(35);
        //线模式为圆滑曲线(默认折线)
        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    }

    /**
     * 展示折线图(一条)
     *
     * @param xAxisValues
     * @param yAxisValues
     * @param label
     * @param color
     */
    public void showLineChart(List xAxisValues, List yAxisValues, String label, int color) {
        initLineChart();
        ArrayList entries = new ArrayList<>();
        for (int i = 0; i < xAxisValues.size(); i++) {
            entries.add(new Entry(xAxisValues.get(i), yAxisValues.get(i)));
        }
        // 每一个LineDataSet代表一条线
        LineDataSet lineDataSet = new LineDataSet(entries, label);
        initLineDataSet(lineDataSet, color, true);

        ArrayList dataSets = new ArrayList<>();
        dataSets.add(lineDataSet);
        LineData data = new LineData(dataSets);
        //设置X轴的刻度数
        xAxis.setLabelCount(xAxisValues.size(), true);
        lineChart.setData(data);
    }

    /**
     * 展示线性图(多条)
     *
     * @param xAxisValues
     * @param yAxisValues 多条曲线Y轴数据集合的集合
     * @param labels
     * @param colours
     */
    public void showLineChart(List xAxisValues, List> yAxisValues, List labels, List colours) {
        initLineChart();
        ArrayList dataSets = new ArrayList<>();
        for (int i = 0; i < yAxisValues.size(); i++) {
            ArrayList entries = new ArrayList<>();
            for (int j = 0; j < yAxisValues.get(i).size(); j++) {
                if (j >= xAxisValues.size()) {
                    j = xAxisValues.size() - 1;
                }
                entries.add(new Entry(xAxisValues.get(j), yAxisValues.get(i).get(j)));
            }
            LineDataSet lineDataSet = new LineDataSet(entries, labels.get(i));

            initLineDataSet(lineDataSet, colours.get(i), false);
            dataSets.add(lineDataSet);
        }
        LineData data = new LineData(dataSets);
        xAxis.setLabelCount(xAxisValues.size(), true);
        lineChart.setData(data);
    }

    /**
     * 设置Y轴值
     *
     * @param max
     * @param min
     * @param labelCount
     */
    public void setYAxis(float max, float min, int labelCount) {
        if (max < min) {
            return;
        }
        leftAxis.setAxisMaximum(max);
        leftAxis.setAxisMinimum(min);
        leftAxis.setLabelCount(labelCount, false);

        rightAxis.setAxisMaximum(max);
        rightAxis.setAxisMinimum(min);
        rightAxis.setLabelCount(labelCount, false);
        lineChart.invalidate();
    }

    /**
     * 设置X轴的值
     *
     * @param max
     * @param min
     * @param labelCount
     */
    public void setXAxis(float max, float min, int labelCount) {
        xAxis.setAxisMaximum(max);
        xAxis.setAxisMinimum(min);
        xAxis.setLabelCount(labelCount, true);

        lineChart.invalidate();
    }

    /**
     * 设置高限制线
     *
     * @param high
     * @param name
     */
    public void setHightLimitLine(float high, String name, int color) {
        if (name == null) {
            name = "高限制线";
        }
        LimitLine hightLimit = new LimitLine(high, name);
        hightLimit.setLineWidth(2f);
        hightLimit.setTextSize(10f);
        hightLimit.setLineColor(color);
        hightLimit.setTextColor(color);
        leftAxis.addLimitLine(hightLimit);
        lineChart.invalidate();
    }

    /**
     * 设置低限制线
     *
     * @param low
     * @param name
     */
    public void setLowLimitLine(int low, String name) {
        if (name == null) {
            name = "低限制线";
        }
        LimitLine hightLimit = new LimitLine(low, name);
        hightLimit.setLineWidth(4f);
        hightLimit.setTextSize(10f);
        leftAxis.addLimitLine(hightLimit);
        lineChart.invalidate();
    }

    /**
     * 设置描述信息
     *
     * @param str
     */
    public void setDescription(String str) {
        Description description = new Description();
        description.setText(str);
        lineChart.setDescription(description);
        lineChart.invalidate();
    }
}

public class BarChartManager {
    private BarChart mBarChart;
    private YAxis leftAxis;
    private YAxis rightAxis;
    private XAxis xAxis;

    private HorizontalBarChart horizontalBarChart;

    public BarChartManager(BarChart barChart) {
        this.mBarChart = barChart;
        leftAxis = mBarChart.getAxisLeft();
        rightAxis = mBarChart.getAxisRight();
        rightAxis.setDrawLabels(false);//右侧Y轴数据不显示
        xAxis = mBarChart.getXAxis();
    }

    public BarChartManager(HorizontalBarChart barChart) {
        this.horizontalBarChart = barChart;
        leftAxis = horizontalBarChart.getAxisLeft();
        rightAxis = horizontalBarChart.getAxisRight();
        rightAxis.setDrawLabels(false);//右侧Y轴数据不显示
        xAxis = horizontalBarChart.getXAxis();
    }

    /**
     * 初始化LineChart
     */
    private void initLineChart() {
        //背景颜色
        mBarChart.setBackgroundColor(Color.WHITE);
        //网格
        mBarChart.setDrawGridBackground(false);
        //背景阴影
        mBarChart.setDrawBarShadow(false);
        mBarChart.setHighlightFullBarEnabled(false);

        //显示边界
        mBarChart.setDrawBorders(true);
        //设置动画效果
        mBarChart.animateY(1000, Easing.EasingOption.Linear);
        mBarChart.animateX(1000, Easing.EasingOption.Linear);

        //折线图例 标签 设置
        Legend legend = mBarChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);

        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        //保证Y轴从0开始,不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);
    }

    /**
     * 初始化LineChart
     */
    private void initHorizontalBarChart() {
        //背景颜色
        horizontalBarChart.setBackgroundColor(Color.WHITE);
        //网格
        horizontalBarChart.setDrawGridBackground(false);
        //背景阴影
        horizontalBarChart.setDrawBarShadow(false);
        horizontalBarChart.setHighlightFullBarEnabled(false);

        //显示边界
        horizontalBarChart.setDrawBorders(true);
        //设置动画效果
        horizontalBarChart.animateY(1000, Easing.EasingOption.Linear);
        horizontalBarChart.animateX(1000, Easing.EasingOption.Linear);

        //折线图例 标签 设置
        Legend legend = horizontalBarChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        legend.setDrawInside(false);

        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        //保证Y轴从0开始,不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);
    }

    /**
     * 展示柱状图(一条)
     *
     * @param xAxisValues
     * @param yAxisValues
     * @param label
     * @param color
     */
    public void showBarChart(List xAxisValues, List yAxisValues, String label, int color) {
        initLineChart();
        ArrayList entries = new ArrayList<>();
        for (int i = 0; i < xAxisValues.size(); i++) {
            entries.add(new BarEntry(xAxisValues.get(i), yAxisValues.get(i)));
        }
        // 每一个BarDataSet代表一类柱状图
        BarDataSet barDataSet = new BarDataSet(entries, label);

        barDataSet.setColor(color);
        barDataSet.setValueTextSize(9f);
        barDataSet.setFormLineWidth(1f);
        barDataSet.setFormSize(15.f);

        ArrayList dataSets = new ArrayList<>();
        dataSets.add(barDataSet);
        BarData data = new BarData(dataSets);
        //设置X轴的刻度数
        xAxis.setLabelCount(xAxisValues.size() - 1, false);
        mBarChart.setData(data);
    }

    /**
     * 展示柱状图(一条/横向)
     *
     * @param xAxisValues
     * @param yAxisValues
     * @param label
     * @param color
     */
    public void showHorizontalBarChart(List xAxisValues, List yAxisValues, String label, int color) {
        initHorizontalBarChart();
        ArrayList entries = new ArrayList<>();
        for (int i = 0; i < xAxisValues.size(); i++) {
            entries.add(new BarEntry(xAxisValues.get(i), yAxisValues.get(i)));
        }
        // 每一个BarDataSet代表一类柱状图
        BarDataSet barDataSet = new BarDataSet(entries, label);

        barDataSet.setColor(color);
        barDataSet.setValueTextSize(9f);
        barDataSet.setFormLineWidth(1f);
        barDataSet.setFormSize(15.f);

        ArrayList dataSets = new ArrayList<>();
        dataSets.add(barDataSet);
        BarData data = new BarData(dataSets);
        //设置X轴的刻度数
        xAxis.setLabelCount(xAxisValues.size() - 1, false);
        horizontalBarChart.setData(data);
    }

    /**
     * 展示柱状图(多条)
     *
     * @param xAxisValues
     * @param yAxisValues
     * @param labels
     * @param colours
     */
    public void showBarChart(List xAxisValues, List> yAxisValues, List labels, List colours) {
        initLineChart();
        BarData data = new BarData();
        for (int i = 0; i < yAxisValues.size(); i++) {
            ArrayList entries = new ArrayList<>();
            for (int j = 0; j < yAxisValues.get(i).size(); j++) {

                entries.add(new BarEntry(xAxisValues.get(j), yAxisValues.get(i).get(j)));
            }
            BarDataSet barDataSet = new BarDataSet(entries, labels.get(i));

            barDataSet.setColor(colours.get(i));
            barDataSet.setValueTextColor(colours.get(i));
            barDataSet.setValueTextSize(10f);
            barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
            data.addDataSet(barDataSet);
        }
        int amount = yAxisValues.size();

        float groupSpace = 0.12f; //柱状图组之间的间距
        float barSpace = (float) ((1 - 0.12) / amount / 10); // x4 DataSet
        float barWidth = (float) ((1 - 0.12) / amount / 10 * 9); // x4 DataSet

        // (0.2 + 0.02) * 4 + 0.08 = 1.00 -> interval per "group"
        xAxis.setLabelCount(xAxisValues.size() - 1, false);
        data.setBarWidth(barWidth);


        data.groupBars(0, groupSpace, barSpace);
        mBarChart.setData(data);
    }


    /**
     * 设置Y轴值
     *
     * @param max
     * @param min
     * @param labelCount
     */
    public void setYAxis(float max, float min, int labelCount) {
        if (max < min) {
            return;
        }
        leftAxis.setAxisMaximum(max);
        leftAxis.setAxisMinimum(min);
        leftAxis.setLabelCount(labelCount, false);

        rightAxis.setAxisMaximum(max);
        rightAxis.setAxisMinimum(min);
        rightAxis.setLabelCount(labelCount, false);
        if (mBarChart != null) {
            mBarChart.invalidate();
        }
        if (horizontalBarChart != null) {
            horizontalBarChart.invalidate();
        }
    }

    /**
     * 设置X轴的值
     *
     * @param max
     * @param min
     * @param labelCount
     */
    public void setXAxis(float max, float min, int labelCount) {
        xAxis.setAxisMaximum(max);
        xAxis.setAxisMinimum(min);
        xAxis.setLabelCount(labelCount, false);
        if (mBarChart != null) {
            mBarChart.invalidate();
        }
        if (horizontalBarChart != null) {
            horizontalBarChart.invalidate();
        }
    }

    /**
     * 设置高限制线
     *
     * @param high
     * @param name
     */
    public void setHightLimitLine(float high, String name, int color) {
        if (name == null) {
            name = "高限制线";
        }
        LimitLine hightLimit = new LimitLine(high, name);
        hightLimit.setLineWidth(4f);
        hightLimit.setTextSize(10f);
        hightLimit.setLineColor(color);
        hightLimit.setTextColor(color);
        leftAxis.addLimitLine(hightLimit);
        mBarChart.invalidate();
    }

    /**
     * 设置低限制线
     *
     * @param low
     * @param name
     */
    public void setLowLimitLine(int low, String name) {
        if (name == null) {
            name = "低限制线";
        }
        LimitLine hightLimit = new LimitLine(low, name);
        hightLimit.setLineWidth(4f);
        hightLimit.setTextSize(10f);
        leftAxis.addLimitLine(hightLimit);
        mBarChart.invalidate();
    }

    /**
     * 设置描述信息
     *
     * @param str
     */
    public void setDescription(String str) {
        Description description = new Description();
        description.setText(str);
        if (mBarChart != null) {
            mBarChart.setDescription(description);
            mBarChart.invalidate();
        }
        if (horizontalBarChart != null) {
            horizontalBarChart.setDescription(description);
            horizontalBarChart.invalidate();
        }
    }
}

2.MAinActivity里面使用:

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    //曲线图
    private LineChart lineChart1;//曲线图带限制线
    private LineChartManager lineChartManager1;
    private LineChart lineChart2;//多条曲线
    private LineChartManager lineChartManager2;
    //柱状图
    private BarChart barChart1;//纵向
    private BarChartManager barChartManager1;
    private BarChart barChart2;
    private BarChartManager barChartManager2;
    private HorizontalBarChart horizontalBarChart;//横向
    private BarChartManager barChartManager3;

    //饼状图
    private PieChart pieChart1;

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

        initView();
        setLineChartData();
        //柱状图
        setBarChart();
        //饼状图
        setPieChart();
    }

    private void setPieChart() {
        pieChart1.setUsePercentValues(true);//使用百分百显示
        pieChart1.setDrawCenterText(true);
        pieChart1.setCenterText("中间文字");
        pieChart1.setCenterTextColor(Color.BLUE);
        pieChart1.setCenterTextSize(16f);
        pieChart1.setDrawHoleEnabled(true);//是否显示圆环中间的洞
        pieChart1.setHoleColor(Color.CYAN);//中间洞的颜色
        pieChart1.setHoleRadius(20f);//设置圆环中间洞的半径
        // 设置圆环透明度及半径
        pieChart1.setTransparentCircleColor(Color.YELLOW);
        pieChart1.setTransparentCircleAlpha(110);
        pieChart1.setTransparentCircleRadius(40);
        pieChart1.setRotationEnabled(true);//设置圆盘是否转动,默认转动
        pieChart1.setRotationAngle(0);//转动角度
        pieChart1.setRotationEnabled(true);//触摸旋转
        pieChart1.setRotationAngle(20);//旋转角度
        Description description = new Description();
        description.setText("饼图测试!");//右下角显示名称
        pieChart1.setDescription(description);
        //比例图
        Legend legend = pieChart1.getLegend();
        legend.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);//位置
        legend.setForm(Legend.LegendForm.CIRCLE);//形状
        //设置X轴动画
        pieChart1.animateX(1500);
        setData(5);

    }

    /**
     * 饼图数据
     *
     * @param count
     */
    private void setData(int count) {
        //每块文字
        ArrayList names = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            names.add(i + 1 + "小块");
        }
        //每块名称及百分比
        ArrayList values = new ArrayList<>();
        values.add(new PieEntry(15, names.get(0)));
        values.add(new PieEntry(35, names.get(1)));
        values.add(new PieEntry(40, names.get(2)));
        values.add(new PieEntry(10, names.get(3)));
        //显示在比例图上
        PieDataSet dataSet = new PieDataSet(values, "颜色指示");
        //设置各饼状图之间的距离
        dataSet.setSliceSpace(3f);
        // 部分区域被选中时多出的长度
        dataSet.setSelectionShift(5f);

        // 设置饼图各个区域颜色
        ArrayList colors = new ArrayList();
        colors.add(Color.RED);
        colors.add(Color.GREEN);
        colors.add(Color.BLUE);
        colors.add(Color.CYAN);
        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
        //设置以百分比显示
        data.setValueFormatter(new PercentFormatter());
        //区域文字的大小
        data.setValueTextSize(16f);
        //设置区域文字的颜色
        data.setValueTextColor(Color.BLACK);
        //设置区域文字的字体
        data.setValueTypeface(Typeface.DEFAULT);
        pieChart1.setData(data);

        //设置是否显示区域文字内容
//        pieChart1.setDrawSliceText(pieChart1.isDrawCenterTextEnabled());
        //设置是否显示区域百分比的值
//        for (IDataSet set : pieChart1.getData().getDataSets()) {
//            set.setDrawValues(!set.isDrawValuesEnabled());
//        }
        // undo all highlights
        pieChart1.highlightValues(null);
        pieChart1.invalidate();
    }


    private void setBarChart() {
        //设置X轴数据
        ArrayList xValues = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            xValues.add((float) i);
        }
        //设置Y轴数据
        List> yValues = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            List yValue = new ArrayList<>();
            //一条柱状图模拟数据
            for (int j = 0; j < 6; j++) {
                yValue.add((float) (Math.random() * 80));
            }
            yValues.add(yValue);
        }
        //颜色集合
        List colors = new ArrayList<>();
        colors.add(Color.RED);
        colors.add(Color.GREEN);
        colors.add(Color.CYAN);
        //线的名字集合
        List names = new ArrayList<>();
        names.add("柱状图");
        names.add("柱状图2");
        names.add("柱状图3");

        //X轴显示字符串------->>
        final List mList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            mList.add(i + 1 + "月");
        }
        XAxis xAxis = barChart1.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mList.get((int) value); //mList为存有月份的集合
            }
        });
        //X轴显示字符串-------<<
        //显示单条柱状图
        barChartManager1.showBarChart(xValues, yValues.get(0), names.get(0), colors.get(0));
        barChartManager1.setDescription("柱状温度图");
        barChartManager1.setYAxis(100, 0, 11);
        //显示柱状组图
        barChartManager2.showBarChart(xValues, yValues, names, colors);
        barChartManager2.setDescription("柱状组温度图");
        barChartManager2.setYAxis(100, 0, 11);
        barChartManager2.setXAxis(6f, 0f, 6);
        //横向柱状图
        barChartManager3.showHorizontalBarChart(xValues, yValues.get(0), "横向柱状图", colors.get(0));
        barChartManager3.setXAxis(6f, 0f, 6);
        barChartManager3.setYAxis(100f, 0f, 11);
        barChartManager3.setDescription("温度");

    }

    private void initView() {
        lineChart1 = findViewById(R.id.line_chart);
        lineChartManager1 = new LineChartManager(lineChart1);
        lineChart2 = findViewById(R.id.line_chart2);
        lineChartManager2 = new LineChartManager(lineChart2);

        barChart1 = findViewById(R.id.bar_chart1);
        barChartManager1 = new BarChartManager(barChart1);
        barChart2 = findViewById(R.id.bar_chart2);
        barChartManager2 = new BarChartManager(barChart2);
        horizontalBarChart = findViewById(R.id.horizontal_bar_chart);
        barChartManager3 = new BarChartManager(horizontalBarChart);

        pieChart1 = findViewById(R.id.pie_chart1);
    }

    private void setLineChartData() {
        //设置X轴数据
        ArrayList xValues = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            xValues.add((float) i);
        }
        //设置Y轴数据
        List> yValues = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            List yValue = new ArrayList<>();
            //一条曲线模拟数据
            for (int j = 0; j < 12; j++) {
                yValue.add((float) (Math.random() * 80));
            }
            yValues.add(yValue);
        }
        //颜色集合
        List colors = new ArrayList<>();
        colors.add(Color.BLUE);
        colors.add(Color.RED);
        colors.add(Color.GREEN);
        colors.add(Color.CYAN);
        //线的名字集合
        List names = new ArrayList<>();
        names.add("一条温度曲线图");
        names.add("温度曲线图2");
        names.add("温度曲线图3");
        names.add("温度曲线图4");
        //设置数据并显示一条曲线
        lineChartManager1.showLineChart(xValues, yValues.get(0), names.get(0), colors.get(0));
        lineChartManager1.setDescription("温度");
        //Y轴0-100 分10格
        lineChartManager1.setYAxis(100, 0, 11);
        //警戒线80 红色
        lineChartManager1.setHightLimitLine(80, "高温报警", Color.RED);

        lineChart1.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
//                Log.e(TAG, "----e:" + e.toString());
            }

            @Override
            public void onNothingSelected() {

            }
        });

        //显示多条曲线
//        lineChart2.setBackgroundColor(Color.GRAY);//表格背景
        lineChartManager2.showLineChart(xValues, yValues, names, colors);
        lineChartManager2.setYAxis(100, 0, 11);
        lineChartManager2.setDescription("多条温度曲线图");
    }
}

3.xml布局:

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mpchart.MainActivity">

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

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

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="lineChart1:曲线图带限制线..." />

                            android:id="@+id/line_chart"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="lineChart2:多条曲线图...." />

                            android:id="@+id/line_chart2"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="单条柱状图...." />

                            android:id="@+id/bar_chart1"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="横向柱状图...." />

                            android:id="@+id/horizontal_bar_chart"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="柱状组图...." />

                            android:id="@+id/bar_chart2"
                android:layout_width="match_parent"
                android:layout_height="300dp" />

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="饼状图..." />

                            android:id="@+id/pie_chart1"
                android:layout_width="match_parent"
                android:layout_height="300dp" />
        
    


有问题可以联系我或网上找答案。

你可能感兴趣的:(MPchart,图表)