Android 缩放坐标轴,Android应用开发之MPAndroidChart缩放问题代码分析

本文将带你了解Android应用开发之MPAndroidChart缩放问题代码分析,希望本文对大家学Android有所帮助。

MPAndroidChart缩放问题代码分析

public void initChart(LineChart chart,int   bgGridColor,int textColor,boolean showXAxis,boolean setMinZero,boolean showY)   {        if (bgGridColor ==   0)            bgGridColor   =   Color.parseColor("#50ffffff");        if   (textColor ==   0)            textColor   = Color.parseColor("#ffffff");        chart.setDrawGridBackground(false);//是否绘制网格背景颜色        chart.getDescription().setEnabled(false);        chart.setDrawBorders(false);//是否在折线图上添加边框        chart.getAxisRight().setEnabled(false);   // 右边的坐标轴        chart.getXAxis().setEnabled(showXAxis);        if   (showY)   {            chart.setViewPortOffsets(10f,   10f, 10f, 10f);        } else   {//            点击图标上圆点显示mark//              MyMarkerView mv = new MyMarkerView(this,   R.layout.custom_marker_view,textColor);//              mv.setOffset();//              mv.setChartView(chart); // For bounds   control//              chart.setMarker(mv);            chart.setViewPortOffsets(45f,   20f, 45f,   4f);        }//          chart.setBackgroundColor(color);//设置背景色        chart.setTouchEnabled(true);        chart.setDragEnabled(true);//现在chart的缩放问题主要在这里,如果只设置scaleenable=true,只有在全屏显示所有点集合时才能进行x y分别缩放,如果在后面设置了// X Y的rang显示范围时。则只能进行Y轴的缩放,只有这样分别设置,才能在设置显示点数限制时进行分别x y手指缩放        chart.setScaleYEnabled(true);        chart.setScaleXEnabled(true);        chart.setScaleEnabled(true);        chart.setPinchZoom(false);        chart.setDragDecelerationFrictionCoef(0.5f);        chart.setNoDataText(resource.getString(R.string.pmDataIsNull));        chart.setNoDataTextColor(textColor);        chart.setNoDataTextTypeface(Typeface.DEFAULT_BOLD);         XAxis   xAxis = chart.getXAxis();        xAxis.setEnabled(true);        xAxis.setDrawLabels(true);        if   (showY)            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);//设置x轴在下方        else            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);        //画限制线条//          LimitLine xLimit = new   LimitLine(10f,"");        //画虚线        xAxis.enableGridDashedLine(10f,   10f,   0.2f);        xAxis.setCenterAxisLabels(true);        xAxis.setDrawGridLines(true);//是否绘制x轴网格        xAxis.setDrawAxisLine(false);//是否绘制x轴        xAxis.setTextColor(textColor);        xAxis.setTextSize(10);        xAxis.setYOffset(-0.5f);        xAxis.setXOffset(-10f);        xAxis.setGridColor(bgGridColor);        xAxis.setGranularity(1f);        xAxis.setAvoidFirstLastClipping(true);         YAxis   yAxis =   chart.getAxisLeft();        yAxis.setValueFormatter(new   IAxisValueFormatter() {            @Override            public   String getFormattedValue(float value, AxisBase axis)   {                if   (value<0){                    return   "";                }                return   String.valueOf((int)value);            }        });        yAxis.setEnabled(true);        yAxis.setDrawLabels(true);        //设置最小值为零        if   (setMinZero)            yAxis.setAxisMinimum(0);        if   (showY) {            yAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);            yAxis.setXOffset(5f);        }   else {            yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);            yAxis.setXOffset(30f);        }        xAxis.enableGridDashedLine(10f,   5f,   0.2f);        yAxis.setCenterAxisLabels(true);        yAxis.setDrawGridLines(true);//是否绘制横向轴网格        yAxis.setDrawAxisLine(false);//是否绘制x轴        yAxis.setTextColor(textColor);        yAxis.setTextSize(12);        yAxis.setYOffset(-8f);        yAxis.setGridColor(bgGridColor);         //显示曲线的描述        Legend   mLegend =   chart.getLegend();        mLegend.setEnabled(false);        //显示图例//          if (showY)   {//              mLegend.setEnabled(false);//        }else   {//              mLegend.setEnabled(true);//              mLegend.setXOffset(100);//              mLegend.setTextSize(10);//              mLegend.setTextColor(textColor);//              mLegend.setYOffset(0);//              mLegend.setXEntrySpace(10);//              mLegend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//              mLegend.setForm(Legend.LegendForm.CIRCLE);//图例样式 (CIRCLE圆形;LINE线性;SQUARE是方块)//          }    }该方法一般放在baseActivity里面,方便继承类好调用 这是设置数据的例子,仅供参考 private void refreshChart(byte[] bytes)   {    ArrayList values = new   ArrayList<>();    int sumForm =   0;    for (int i = 0; i < bytes.length; i += 2)   {        byte[] data = new   byte[]{bytes[i], bytes[i +   1]};        int i1 =   HexUtil.bytes2IntLittle(data)*3;        values.add(new   Entry(i, i1));        sumForm =   i1;    }    fogTv.setText(String.valueOf(sumForm));    LineDataSet   set;    if (chart.getData()!=null && chart.getData().getDataSetCount()>0){        set   = (LineDataSet)   chart.getData().getDataSetByIndex(0);        set.setValues(values);        chart.getData().notifyDataChanged();        chart.notifyDataSetChanged();    }   else {        int i =   resource.getColor(R.color.main_color);        set   = new CustomLineData(values, "DataSet   1");        set.setColor(i);        set.setDrawValues(true);        set.setValueTextColor(i);        set.setDrawCircles(true);        set.setCircleColor(i);        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);//这是点的形状圆形        set.setDrawFilled(true);        set.setFillAlpha(150);        ArrayList   dataSets = new ArrayList<>();        dataSets.add(set);        LineData   data = new   LineData(dataSets);        chart.setData(data);        chart.animateXY(1500,1500);        chart.invalidate();        chart.setVisibleXRange(0,12);//设置x轴显示范围,如果不设置会一次加载所有的点,很难看        chart.setDragDecelerationFrictionCoef(0.9f);//设置滑动的阻力    }}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值