当前位置: 移动技术网 > IT编程>移动开发>Android > MPAndroidChart缩放问题代码分析

MPAndroidChart缩放问题代码分析

2018年09月29日  | 移动技术网IT编程  | 我要评论

ca1255,武林风新西兰,只缘身在此山中全诗

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<entry> 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<ilinedataset> 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);//设置滑动的阻力
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网