MPAndroidChart3.0自己修改的demo学习折线图和柱状图

直接上代码,xml就基本的LineChart与BarChart  效果图在最后

public class LineChartTestextends AppCompatActivity {

LineChartlc;

BarChartbc,bc1;

TextViewtextView;

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_line_chart_test);

lc = findViewById(R.id.chart1);

bc = findViewById(R.id.barchart1);

bc1 = findViewById(R.id.barchart2);

textView = findViewById(R.id.tev_test);

setXAxis();

setYAxis();

loadData();

setviewListener();

}

private void setviewListener() {

textView.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View v) {

float datas[] = {1,0,3,2,6,7,3,4,9};

List yData =new ArrayList();

for (int i =0; i < datas.length; i++) {

yData.add(new Entry(i, datas[datas.length-i-1]));

}

LineDataSet lineDataSet =new LineDataSet(yData,"");

lineDataSet.setLineWidth(1);//设置线条宽度

                lineDataSet.setColor(Color.BLUE);//设置线条颜色

                lineDataSet.setCircleColor(Color.BLUE);//设置圆点颜色

                lineDataSet.setCircleRadius(2);//设置圆的大小

                lineDataSet.setDrawValues(false);//不绘制文本

//        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线

                lineDataSet.setCubicIntensity(0.1f);//曲线强度0.1

                lineDataSet.setDrawCircleHole(false);//设置为实心圆

                LineData lineData =new LineData(lineDataSet);

lc.setData(lineData);

lc.invalidate();

}

});

}

/**

    * 设置Y轴

    */

    private void setYAxis() {

//得到YAxis对象

        YAxis yAxis =lc.getAxisLeft();

lc.getAxisRight().setEnabled(false);//不显示右侧的y轴

        //设置字体颜色白色

        yAxis.setTextColor(Color.BLUE);

//设置最大值和最小值

        yAxis.setAxisMaximum(10);

yAxis.setAxisMinimum(0);

//设置字体大小

        yAxis.setTextSize(11f);

//字体加粗

        yAxis.setTypeface(Typeface.DEFAULT_BOLD);

yAxis.setDrawAxisLine(false);//不绘制第一条横线

        yAxis.setGridColor(Color.RED);//网格颜色

        //强制只绘制5个Label

//        yAxis.setLabelCount(10, true);

    }

public void setXAxis() {

//得到XAxis对象

        XAxis xAxis =lc.getXAxis();

xAxis.setTextColor(Color.RED);//字体颜色

        //设置最大最小值

//        xAxis.setAxisMaximum(15);

//        xAxis.setAxisMinimum(0);

        xAxis.setGranularity(1f);//设置间隔

//        xAxis.setLabelCount(11, true);

        //将x显示在下方

        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

xAxis.setTextSize(14f);//字体大小

        //自定义格式

        xAxis.setValueFormatter(new IAxisValueFormatter() {

@Override

            public String getFormattedValue(float value, AxisBase axis) {

System.out.println(value);

if (value <100) {

String tep = value +"";

return tep.substring(0, tep.indexOf("."));

}

if (value ==63) {

return "(S)";

}

String tep1 = value +"";

return tep1.substring(0, tep1.indexOf("."));

}

});

xAxis.setLabelCount(20);

xAxis.setDrawGridLines(false);

}

private void loadData() {

//设置边距

        lc.setExtraTopOffset(10);

lc.setExtraBottomOffset(10);

lc.setExtraLeftOffset(10);

lc.setExtraRightOffset(0);

//不绘制图例和标题

        lc.getLegend().setEnabled(false);

lc.getDescription().setEnabled(false);

lc.zoom(1.2f,1,0,0);

//设置背景颜色

//        lc.setBackgroundColor(Color.parseColor("#fff123"));

        float datas[] = {1,5,3,2,6,7,3,4,9};

List yData =new ArrayList();

for (int i =0; i < datas.length; i++) {

yData.add(new Entry(i, datas[i]));

}

LineDataSet lineDataSet =new LineDataSet(yData,"");

lineDataSet.setLineWidth(1);//设置线条宽度

        lineDataSet.setColor(Color.BLUE);//设置线条颜色

        lineDataSet.setCircleColor(Color.BLUE);//设置圆点颜色

        lineDataSet.setCircleRadius(2);//设置圆的大小

        lineDataSet.setDrawValues(false);//不绘制文本

//        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);//设置为贝塞尔曲线

        lineDataSet.setCubicIntensity(0.1f);//曲线强度0.1

        lineDataSet.setDrawCircleHole(false);//设置为实心圆

        LineData lineData =new LineData(lineDataSet);

lc.setData(lineData);

setBarChart();

}

public void setBarChart() {

setDesc();

setLegend();

setXAxis1();

setYAxis1();

loadData1();

}

//设置Title

    private void setDesc() {

Description description =bc.getDescription();

// 获取屏幕中间x 轴的像素坐标

        WindowManager wm = (WindowManager) LineChartTest.this.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics dm =new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(dm);

float x = dm.widthPixels /2;

description.setPosition(x,50);

description.setText("年龄群体车辆违章的占比统计");

description.setTextSize(10f);

description.setTextColor(Color.BLACK);

bc.setDescription(description);

}

//设置图例

    private void setLegend() {

Legend legend =bc.getLegend();

legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);

legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);

legend.setDrawInside(true);

legend.setTextSize(14f);

legend.setTypeface(Typeface.DEFAULT_BOLD);

//        legend.setXOffset(30);

        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);

legend.setTypeface(Typeface.DEFAULT_BOLD);

Legend legend1 =bc1.getLegend();

legend1.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);

legend1.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);

legend1.setDrawInside(true);

legend1.setTextSize(14f);

legend1.setTypeface(Typeface.DEFAULT_BOLD);

//        legend.setXOffset(30);

        legend1.setOrientation(Legend.LegendOrientation.HORIZONTAL);

legend1.setTypeface(Typeface.DEFAULT_BOLD);

}

//设置Y轴

    private void setYAxis1() {

YAxis yAxis =bc.getAxisLeft();

yAxis.setTextSize(14f);

yAxis.setAxisLineWidth(0f);

yAxis.setTextColor(Color.BLACK);

//        yAxis.setXOffset(15);

//        yAxis.setAxisMaximum(800);

        yAxis.setDrawGridLines(true);//显示横线

        yAxis.setAxisMinimum(0);

yAxis.setGranularity(0f);

//        yAxis.setLabelCount(7);

    }

//设置X轴

    private void setXAxis1() {

XAxis xAxis =bc.getXAxis();

xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

xAxis.setTextSize(9f);

xAxis.setTypeface(Typeface.DEFAULT_BOLD);

xAxis.setDrawGridLines(false);

xAxis.setLabelCount(10);

//        xAxis.setAxisMaximum(11f);

//        xAxis.setAxisMinimum(0f);

        xAxis.setGranularity(1f);

//自定义格式

        xAxis.setValueFormatter(new IAxisValueFormatter() {

@Override

            public String getFormattedValue(float value, AxisBase axis) {

//                int tep = (int) (9 - value);

//                return tep + "0后";

                return (int)value +"";

}

});

}

//设置数据

    private void loadData1() {

bc.setExtraOffsets(5,30,0,30);//边距

        bc.animateXY(1000,1000);

bc.setFitBars(true);//否将柱块上的文字绘制在柱块的上面

        bc.getAxisLeft().setDrawAxisLine(false);//去掉左右竖直边线

        bc.getAxisRight().setDrawAxisLine(false);//去掉左右竖直边线

        bc.getAxisRight().setEnabled(false);//不显示右侧的y轴

        bc.zoom(3f,1,0,0);

MyMarkerView mv =new MyMarkerView(this, R.layout.custom_marker_view);

mv.setChartView(bc);

bc.setMarker(mv);

final float d_1[] = {230f,100f,480f,480f,230f,480f,480f,480f,230f,480f};

final float d_2[] = {120f,600f,400f,200f,80f,120f,600f,400f,200f,80f};

List entries =new ArrayList();

List entries1 =new ArrayList();

List entries2 =new ArrayList();

List entries3 =new ArrayList();

for (int i =0; i < d_1.length; i++) {

//使用BarEntry的构造方法的第三个参数来保存需要在柱块上显示的数据

            entries.add(new BarEntry(i, d_1[i], (d_1[i] / (d_1[i] + d_2[i])) *100));

entries1.add(new BarEntry(i, d_2[i], (d_2[i] / (d_1[i] + d_2[i])) *100));

entries2.add(new BarEntry(i, d_2[i], (d_2[i] / (d_1[i] + d_2[i])) *100));

entries3.add(new BarEntry(i, d_2[i], (d_2[i] / (d_1[i] + d_2[i])) *100));

}

BarDataSet barDataSet =new BarDataSet(entries,"无违章");

BarDataSet barDataSet1 =new BarDataSet(entries1,"有违章");

BarDataSet barDataSet2 =new BarDataSet(entries2,"有违章1");

BarDataSet barDataSet3 =new BarDataSet(entries3,"无违章1");

barDataSet.setColor(Color.parseColor("#6D9C00"));

barDataSet.setValueFormatter(new IValueFormatter() {

@Override

            public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {

//获取entry中的数据

                float res = (float) entry.getData();

return String.format("%.1f", res) +"%";

}

});

barDataSet3.setColor(Color.parseColor("#6D9C00"));

barDataSet3.setValueFormatter(new IValueFormatter() {

@Override

            public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {

//获取entry中的数据

                float res = (float) entry.getData();

return String.format("%.1f", res) +"%";

}

});

barDataSet1.setColor(Color.parseColor("#F07408"));

barDataSet1.setValueTextColor(Color.RED);

barDataSet1.setValueFormatter(new IValueFormatter() {

@Override

            public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {

//获取entry中的数据

                float res = (float) entry.getData();

return String.format("%.1f", res) +"%";

}

});

barDataSet2.setColor(Color.parseColor("#fff123"));

barDataSet2.setValueTextColor(Color.BLUE);

barDataSet2.setValueFormatter(new IValueFormatter() {

@Override

            public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {

//获取entry中的数据

                float res = (float) entry.getData();

return String.format("%.1f", res) +"%";

}

});

List barDataSets =new ArrayList();

barDataSets.add(barDataSet);

barDataSets.add(barDataSet1);

barDataSets.add(barDataSet2);

//        barDataSets.add(barDataSet3);

        BarData barData =new BarData(barDataSets);

barData.setBarWidth(0.21f);//设置柱块的宽度

        //参数1:距左边的距离(开始会偏移一个组的距离)

        //参数二:组与组之间的间距

        //参数三:一组柱块之中每个之间的距离  通过 setBarWidth与groupSpace调整柱状与x轴对应  4个情况  4*(0.19+0.01)+0.2=1  3个的情况3*(0.21+0.01)+0.34=1

        barData.groupBars(-0.50f,0.34f,0.01f);

bc.setTouchEnabled(true);

bc.setScaleEnabled(false);

bc.setData(barData);

addbc1();

}

private String[]types = {"银行","非银金融","建筑材料","化工","医药生物","电子","银行","非银金融","建筑材料","化工","医药生物","电子"};

private float[]changes = {27.91f,5.9f,0.4f,17.79f,21.85f,39.58f,27.91f,5.9f,0.4f,17.79f,21.85f,39.58f};

private void addbc1(){

initBarChart(bc1);

setBarChartData(types.length,bc1);

}

private void initBarChart(BarChart bc) {

bc.setExtraOffsets(5,30,0,30);//边距

        bc.animateXY(1000,1000);

bc.setFitBars(true);//否将柱块上的文字绘制在柱块的上面

        bc.getAxisLeft().setDrawAxisLine(false);//去掉左右竖直边线

        bc.getAxisRight().setDrawAxisLine(false);//去掉左右竖直边线

        bc.getAxisRight().setEnabled(false);//不显示右侧的y轴

        bc.zoom(1f,1,0,0);

MyMarkerView mv =new MyMarkerView(this, R.layout.custom_marker_view);

mv.setChartView(bc);

bc.setMarker(mv);

Description description = bc.getDescription();

// 获取屏幕中间x 轴的像素坐标

        WindowManager wm = (WindowManager) LineChartTest.this.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics dm =new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(dm);

float x = dm.widthPixels /2;

description.setPosition(x,50);

description.setText("年龄群体车辆违章的占比统计");

description.setTextSize(10f);

description.setTextColor(Color.BLACK);

bc.setDescription(description);

Legend legend = bc.getLegend();

legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);

legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);

legend.setDrawInside(true);

legend.setTextSize(14f);

legend.setTypeface(Typeface.DEFAULT_BOLD);

//        legend.setXOffset(30);

        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);

legend.setTypeface(Typeface.DEFAULT_BOLD);

YAxis yAxis = bc.getAxisLeft();

yAxis.setTextSize(14f);

yAxis.setAxisLineWidth(0f);

yAxis.setTextColor(Color.BLACK);

//        yAxis.setXOffset(15);

//        yAxis.setAxisMaximum(800);

        yAxis.setDrawGridLines(true);//显示横线

        yAxis.setAxisMinimum(0);

yAxis.setGranularity(0f);

XAxis xAxis = bc.getXAxis();

xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

xAxis.setTextSize(9f);

xAxis.setTypeface(Typeface.DEFAULT_BOLD);

xAxis.setDrawGridLines(false);

xAxis.setLabelCount(10);

//        xAxis.setAxisMaximum(11f);

//        xAxis.setAxisMinimum(0f);

        xAxis.setGranularity(1f);

//自定义格式

        xAxis.setValueFormatter(new IAxisValueFormatter() {

@Override

            public String getFormattedValue(float value, AxisBase axis) {

//                int tep = (int) (9 - value);

//                return tep + "0后";

                return (int)value +"";

}

});

final float d_1[] = {230f,100f,480f,480f,230f,480f,480f,480f,230f,480f};

List entries =new ArrayList();

for (int i =0; i < d_1.length; i++) {

//使用BarEntry的构造方法的第三个参数来保存需要在柱块上显示的数据

            entries.add(new BarEntry(i, d_1[i], d_1[i] +""));

}

BarDataSet barDataSet =new BarDataSet(entries,"无违章");

barDataSet.setColor(Color.parseColor("#6D9C00"));

barDataSet.setValueFormatter(new IValueFormatter() {

@Override

            public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {

//获取entry中的数据

//                float res = (float) entry.getData();

//                return String.format("%.1f", res) + "%";

                return value+"";

}

});

List barDataSets =new ArrayList();

barDataSets.add(barDataSet);

BarData barData =new BarData(barDataSets);

bc.setTouchEnabled(true);

bc.setScaleEnabled(false);

bc.setData(barData);

}

private void setBarChartData(int count, BarChart mChart) {

}

}

效果图


你可能感兴趣的:(MPAndroidChart3.0自己修改的demo学习折线图和柱状图)