频道栏目
首页 > 资讯 > Android > 正文

MPAndroidChart缩放问题代码分析

18-07-30        来源:[db:作者]  
收藏   我要投稿

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);//设置滑动的阻力
    }
}
相关TAG标签
上一篇:Android7.1.1出现Toast崩溃问题的解决方案
下一篇:关于禁止 Android Studio 的 Lint 检测 Google search deep link 的方法教程
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站