MPAndroidChart的折线图、柱状图、饼图的一些属性

MPAndroidChart用的时候源码里好多没有注释,为了方便我就写了一些,可能不完善。

 

 

lineChart.getDescription().setEnabled(false);//设置描述

lineChart.setPinchZoom(true);//设置按比例放缩折线图、柱状图

 //设置自定义的markerView
    MPChartMarkerView markerView = new MPChartMarkerView(barChart.getContext(), R.layout.custom_marker_view);
    barChart.setMarker(markerView);

1.折线图的一些属性

 LineDataSet lineDataSet = new LineDataSet(lineEntries, "name");

           //设置线的颜色
            lineDataSet.setColor(Color.parseColor("#ffffff"));
            //设置线的宽度
            lineDataSet.setLineWidth(2f);
            //设置每个坐标点的圆大小
            lineDataSet.setCircleRadius(3f);
            //设置圆圈的颜色
            lineDataSet.setCircleColor(Color.parseColor(lineList.get(j).getColor()));
            //设置圆圈内部洞的颜色
            lineDataSet.setCircleColorHole(Color.parseColor(lineList.get(j).getColor()));
            //lineDataSet.setValueTextColor(Color.rgb(254,116,139));
            //设置线数据依赖于右侧y轴
            lineDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
            //不绘制线的数据
            lineDataSet.setDrawValues(false);
            lineDataSet.setDrawCircles(true);
            lineDataSet.setHighLightColor(00000000);
            //点击高亮显示
//            lineDataSet.setHighlightEnabled(false);
            // 设置平滑曲线模式
            lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);

2.柱状图的一些属性

 //x坐标轴设置
    IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);//设置自定义的x轴值格式化器
    XAxis xAxis = barChart.getXAxis();//获取x轴
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置
    xAxis.setDrawGridLines(false);//不绘制格网线
    xAxis.setGranularity(1f);//设置最小间隔,防止当放大时,出现重复标签。
    xAxis.setValueFormatter(xAxisFormatter);
    xAxis.setTextSize(xAxisTextSize);//设置标签字体大小
    xAxis.setLabelCount(xAxisValue.size());//设置标签显示的个数

    //y轴设置
    YAxis leftAxis = barChart.getAxisLeft();//获取左侧y轴
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//设置y轴标签显示在外侧
    
    leftAxis.setDrawGridLines(false);
    leftAxis.setDrawLabels(false);//禁止绘制y轴标签
    leftAxis.setDrawAxisLine(false);//禁止绘制y轴

//以下是为了解决 柱状图 左右两边只显示了一半的问题 根据实际情况 而定
xAxis.setAxisMinimum(-0.5f);
xAxis.setAxisMaximum((float) (barChartY.size() - 0.5));
leftAxis.setAxisMaximum(100);//设置Y轴最大值
leftAxis.setAxisMinimum(0);//设置Y轴最小值

    barChart.getAxisRight().setEnabled(false);//禁用右侧y轴

    //图例设置
    Legend legend = barChart.getLegend();
    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
    legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
    legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
    legend.setDrawInside(false);//绘制在chart的外侧
    legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

    legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
    legend.setFormSize(0f);//图例窗体的大小
    legend.setTextSize(16f);//图例文字的大小
 

 //设置柱状图数据
    setBarChartData(barChart, yAxisValue, title, barColor);

    barChart.setExtraBottomOffset(10);//距视图窗口底部的偏移,类似与paddingbottom
    barChart.setExtraTopOffset(30);//距视图窗口顶部的偏移,类似与paddingtop
    barChart.setFitBars(true);//使两侧的柱图完全显示
    barChart.animateX(1500);//数据显示动画,从左往右依次显示

3.饼图的一些属性

mPieChart = (PieChart) findViewById(R.id.mPieChart);

mPieChart.setUsePercentValues(true);//设置value是否用显示百分数,默认为false

mPieChart.getDescription().setEnabled(false);//设置描述

mPieChart.setExtraOffsets(5, 10, 5, 5);//设置饼状图距离上下左右的偏移量

mPieChart.setDragDecelerationFrictionCoef(0.95f);//设置阻尼系数,范围在[0,1]之间,越小饼状图转动越困难

//设置中间文件

mPieChart.setCenterText(generateCenterSpannableText());

// mPieChart.setCenterText("总学生数:100人");

mPieChart.setDrawHoleEnabled(true);//是否绘制饼状图中间的圆

mPieChart.setHoleColor(Color.WHITE);//饼状图中间的圆的绘制颜色

mPieChart.setTransparentCircleColor(Color.WHITE);//设置圆环的颜色

mPieChart.setTransparentCircleAlpha(110);//设置圆环的透明度[0,255]

mPieChart.setHoleRadius(58f);//饼状图中间的圆的半径大小

mPieChart.setTransparentCircleRadius(61f);//设置圆环的半径值

mPieChart.setDrawCenterText(true);//是否绘制中间的文字

mPieChart.setRotationAngle(0);//设置饼状图旋转的角度

// 触摸旋转

mPieChart.setRotationEnabled(true);;//设置饼状图是否可以旋转(默认为true)

mPieChart.setHighlightPerTapEnabled(true);//设置旋转的时候点中的tab是否高亮(默认为true)

//变化监听

mPieChart.setOnChartValueSelectedListener(this);


        
        Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
        // 设置圆盘百分比的字体
        mChart.setValueTypeface(tf);
        // 设置周围字体颜色
        // mChart.setValueTextColor(Color.BLACK);
        // 设置周围文字大小
        // mChart.setValueTextSize(size);
        // 设置圆盘中间区域的字体
        mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf"));
       
        // 饼图又下角的说明文字
        mChart.setDescription("这是一个饼图");
        // 饼图右下角说明文字大小
        // mChart.setDescriptionTextSize(7);
       
        // 设置是否显示饼图中心的空白区 默认显示
        mChart.setDrawHoleEnabled(true);
        
        // draws the corresponding description value into the slice
        // 是否显示x-value的文字
        mChart.setDrawXValues(true);
        // 是否显示y-value的文字
        mChart.setDrawYValues(true);
           
        // 一起使用
        // 单位
        // mChart.setUnit(" g");
        // 设置是否使用单位 默认false
        // mChart.setDrawUnitsInChart(true);
        
        // add a selection listener
        // 设置一个选中监听
        mChart.setOnChartValueSelectedListener(this);
        // 设置是否接收点击事件
        // mChart.setTouchEnabled(false);
        // 设置中间的文字
        mChart.setCenterText("MPAndroidChart\nLibrary");
        // 设置圆盘中间文字大小
        // mChart.setCenterTextSize(size);

        // 设置动画
        mChart.animateXY(1500, 1500);
        // mChart.spin(2000, 0, 360);
        // 设置饼图说明
        Legend l = mChart.getLegend();
        l.setPosition(LegendPosition.RIGHT_OF_CHART);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(5f);
        // 设置是否显示图表说明
        // mChart.setDrawLegend(false);
        //设置X轴动画
        mChart.animateX(1800);
        //设置y轴动画
        mChart.animateY(1800);
        //设置xy轴一起的动画
       mChart.animateXY(1800, 1800);

PieDataSet dataSet = new PieDataSet(entries, "Election Results");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
// add a lot of colors
ArrayList<Integer> colors = new ArrayList<>();

dataSet.setColors(colors);//设置饼块的颜色
//        dataSet.setSelectionShift(0f);

//数据连接线距图形片内部边界的距离,为百分数(0~100f)
        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.2f);//设置连接线的长度
        dataSet.setValueLinePart2Length(0.4f);
        //设置线的颜色和饼块颜色一样
        dataSet.setUsingSliceColorAsValueLineColor(true);
//x,y值在圆外显示(在圆外才会有连接线)
//        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        PieData data = new PieData(dataSet);
//        data.setValueFormatter(new PercentFormatter());

//设值
        data.setValueFormatter(new ValueFormatter() {
            @Override
            public String getPieLabel(float value, PieEntry pieEntry) {
              
                return value;
            }
        });
     //设置字体大小
        data.setValueTextSize(11f);
        //设置字体颜色
       //  data.setValueTextColor(Color.BLACK);

        //设置多种字体颜色值
        data.setValueTextColors(colors);

//设置字体样式
        data.setValueTypeface(tf);
        mChart.setDrawCenterText(false);
        mChart.setData(data);

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值