Last active
January 18, 2018 06:15
-
-
Save liu402883451/b7e69e11f973027ac8f05df9a59daa2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 设置百分比代码 | |
* 获取最大值距离Y轴最高点的百分比计算得到的float值, | |
* 由最大值柱状高度和最大值距Y轴最高点距离的比值得到,1:1 即50%,此时为100f | |
* 以下为2种情况的数据: | |
* 1、大于等于50%:100f是50% 50f是75% 0f是100% | |
* 2、小于等于50%:100f是50% 200f是1/3 300f是1/4 400f是1/5 | |
* | |
* @return 最大值距离Y轴最高点的百分比计算得到的float值 | |
*/ | |
private float getRealPercent() { | |
int max = aCount; | |
if (max < bCount) max = bCount; | |
if (max < cCount) max = cCount; | |
if (max < dCount) max = dCount; | |
if (max < eCount) max = eCount; | |
double per = (max * 1.0 / companyCount);//数据真实的百分比:0-100 | |
if (per >= 50) { | |
//大于等于50%的公式 | |
return Float.parseFloat(String.valueOf(100f - (per * 100 - 50) * 2)); | |
} else { | |
//小于50%的公式 | |
return Float.parseFloat(String.valueOf((1 / per - 1) * 100)); | |
} | |
} | |
使用: | |
YAxis leftAxis = barChart.getAxisLeft();//左侧的y轴 | |
float realPercentFloat = getRealPercent();//获取最大值距离Y轴最高点的百分比计算得到的float值 | |
leftAxis.setSpaceTop(realPercentFloat); | |
leftAxis.setValueFormatter(new PercentFormatter());//y轴格式化为百分数 | |
此外数据需要设置为百分比,如50%设置为50,若没有达到预期,可以加上leftAxis.setLabelCount(5); | |
实现效果:y轴坐标由0%到100%,不会随着数据值变化改动,并且数据动态适应坐标轴,适用于以百分比展示的柱状图 | |
替代方法设置:leftAxis.setAxisMaximum(100f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment