Android图表库MPAndroidChart如何对y轴取整及对y轴上对应显示的数据取整数

铁石心肠 1年前 ⋅ 678 阅读

转载:https://blog.csdn.net/wqbs369/article/details/78376703?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

1 y轴取整 ,如下图

y轴显示小数

####解决方法

 YAxis leftAxis = mChart.getAxisLeft();
   leftAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
	        public String getFormattedValue(float value,    AxisBase axis) {
                return (int) value + "";
            }
        });

y 轴对应显示的值取整

y 轴对应显示的值为小数

解决办法

data.setValueFormatter(new MonthlyIntegerYValueFormatter());

  LineDataSet set1;
        if (valueList != null && mChart != null) {
            mChart.getXAxis().setLabelCount(valueList.size(), true);
        }
        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(valueList);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(valueList, "DataSet 1");
            set1.setMode(LineDataSet.Mode.LINEAR);
            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
            set1.setColor(chartColor);
            set1.setDrawValues(true);
            set1.setDrawCircleHole(false);
            set1.setDrawCircles(true);
            set1.setCircleColor(chartColor);
            set1.setValueTextSize(9f);
            set1.setDrawFilled(false);
            set1.setCircleRadius(4f);
            set1.setLineWidth(3f);
            set1.setFillAlpha(255);
//            if (Utils.getSDKInt() >= 18) {
//                // fill drawable only supported on api level 18 and above
//                Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.fade_blue);
//                set1.setFillDrawable(drawable);
//            } else {
//                set1.setFillColor(getResources().getColor(R.color.color_main_blue));
//            }
            set1.setHighLightColor(Color.TRANSPARENT);//设置高亮色

            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            dataSets.add(set1); // add the datasets
            // create a data object with the datasets
            LineData data = new LineData(dataSets);
            //此为y轴数值取整关键步骤
            data.setValueFormatter(new MonthlyIntegerYValueFormatter());
            // set data
            mChart.setData(data);
        }

MonthlyIntegerYValueFormatter 源码


public class MonthlyIntegerYValueFormatter implements IValueFormatter {
    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return (int) (value) + "";
    }
}

全部评论: 0

    我有话说:

    咨询问题

    点击这里给我发消息

    淘宝咨询

    博主承接代做业务

    真·送云服务器

    咨询博主