如何在 MPAndroidChart 的左轴上设置中间值的周期?
How can I set a period for intermediate values on left axis of MPAndroidChart?
我想在周期为 100 的 BarChart
和 CombinedChart
的左轴上显示值。例如,如果我有三个值,10、120、250 table 的左轴应该有值 0、100、200 和 300 作为参考。取而代之的是,我得到的是默认值,周期为 40。我想更改这个中间范围。
我知道如何设置最小值-最大值的范围值,但不知道如何设置中间值的范围。
是否可以修改 table 左轴上的中间值范围?
提前致谢!
不确定我是否正确理解了你的问题的意图,但如果你希望你的轴显示 100 的倍数的值,你可以尝试这样的事情:
YAxis leftAxis = mBarChart.getAxisLeft();
leftAxis.setGranularity(100f);
leftAxis.setGranularityEnabled(true);
请参阅 setGranularity
and setGranularityEnabled
for more information about granularity and also check out this answer 的文档。
是的,可以使用
更改中间范围
mBarChart.getAxisLeft().setGranularity(100f);
mBarChart.getAxisLeft().setGranularityEnabled(true);
但如果您想动态设置粒度,则实施 IAxisValueFormatter 并比较 return 值以获得差异并将粒度设置为该差异。
private float yAxisScaleDifference = -1;
private boolean granularitySet = false;
//[10,120,250]
mBarChart.getAxisLeft().setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float v, AxisBase axisBase) {
if(!granularitySet) {
if(yAxisScaleDifference == -1) {
yAxisScaleDifference = v; //10
}
else {
float diff = v - yAxisScaleDifference; //120 - 10 = 110
if(diff >= 1000) {
yAxisLeft.setGranularity(1000f);
}
else if(diff >= 100) {
yAxisLeft.setGranularity(100f); //set to 100
}
else if(diff >= 1f) {
yAxisLeft.setGranularity(1f);
}
granularitySet =true;
}
}
return val;
}
});
另一个例子:
say Y-Axis returns [1200,3400,8000,9000....]
first time: 1200
second time: 3400 - 1200 = 2200
set to 1000
如果差异不均匀,您必须使用数组来存储差异并取平均值以获得正确的粒度。
我想在周期为 100 的 BarChart
和 CombinedChart
的左轴上显示值。例如,如果我有三个值,10、120、250 table 的左轴应该有值 0、100、200 和 300 作为参考。取而代之的是,我得到的是默认值,周期为 40。我想更改这个中间范围。
我知道如何设置最小值-最大值的范围值,但不知道如何设置中间值的范围。
是否可以修改 table 左轴上的中间值范围?
提前致谢!
不确定我是否正确理解了你的问题的意图,但如果你希望你的轴显示 100 的倍数的值,你可以尝试这样的事情:
YAxis leftAxis = mBarChart.getAxisLeft();
leftAxis.setGranularity(100f);
leftAxis.setGranularityEnabled(true);
请参阅 setGranularity
and setGranularityEnabled
for more information about granularity and also check out this answer 的文档。
是的,可以使用
更改中间范围mBarChart.getAxisLeft().setGranularity(100f);
mBarChart.getAxisLeft().setGranularityEnabled(true);
但如果您想动态设置粒度,则实施 IAxisValueFormatter 并比较 return 值以获得差异并将粒度设置为该差异。
private float yAxisScaleDifference = -1;
private boolean granularitySet = false;
//[10,120,250]
mBarChart.getAxisLeft().setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float v, AxisBase axisBase) {
if(!granularitySet) {
if(yAxisScaleDifference == -1) {
yAxisScaleDifference = v; //10
}
else {
float diff = v - yAxisScaleDifference; //120 - 10 = 110
if(diff >= 1000) {
yAxisLeft.setGranularity(1000f);
}
else if(diff >= 100) {
yAxisLeft.setGranularity(100f); //set to 100
}
else if(diff >= 1f) {
yAxisLeft.setGranularity(1f);
}
granularitySet =true;
}
}
return val;
}
});
另一个例子:
say Y-Axis returns [1200,3400,8000,9000....]
first time: 1200
second time: 3400 - 1200 = 2200
set to 1000
如果差异不均匀,您必须使用数组来存储差异并取平均值以获得正确的粒度。