我正在使用MPAndroidChart在我的android应用中显示条形图。但是xAxis标签和条未对齐。
我使用了MPAndroidChart:v3.0.3。在屏幕截图中,条未与标签对齐。我遵循git example。尝试了一些解决方案,但实际上没有任何效果。无法识别问题。条形宽度为.5f
图表设置
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
barChart.getDescription().setEnabled(false);
barChart.setMaxVisibleValueCount(60);
barChart.setPinchZoom(false);
barChart.setScaleEnabled(false);
barChart.setDrawGridBackground(false);
barChart.setFitBars(true);
barChart.animateY(2500);
xAxis设置
XAxis xAxis = barChart.getXAxis();
xAxis.setSpaceMax(0.5f);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawAxisLine(true);
xAxis.setCenterAxisLabels(false);
xAxis.setGranularity(1f);
xAxis.setAxisMinimum(0f);
xAxis.setAxisMaximum(xAxisKeys.size());
xAxis.setLabelCount(xAxisKeys.size(), true);
IAxisValueFormatter xAxisFormatter = new CustomFormatter(xAxisKeys);
xAxis.setValueFormatter(xAxisFormatter);
答案 0 :(得分:0)
请尝试设置此属性
xAxis.setAvoidFirstLastClipping(false);
答案 1 :(得分:0)
尝试从代码中删除以下行。评论它们并检查更改:
xAxis.setAxisMinimum(0f);
xAxis.setAxisMaximum(xAxisKeys.size());
xAxis.setLabelCount(xAxisKeys.size(), true);
然后按照下面的基本图形示例进行操作,之后您可以根据需要通过更改属性来对其进行自定义:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BarChart barChart = (BarChart) findViewById(R.id.chart1);
BarData data = barData();
barChart.setData(data);
barChart.setFitBars(true);
// xAxis customization
XAxis xAxis = barChart.getXAxis();
// Following code have no effect but you can change it if required
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(false);
// Setting position of xAxis
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
// Setting labels to xAxis
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues()));
}
// creating list of x-axis values
private ArrayList<String> getXAxisValues()
{
ArrayList<String> labels = new ArrayList<String> ();
labels.add( "JAN");
labels.add( "FEB");
labels.add( "MAR");
labels.add( "APR");
labels.add( "MAY");
labels.add( "JUN");
return labels;
}
// this method is used to create data for Bar graph
public BarData barData()
{
ArrayList<BarEntry> group1 = new ArrayList<BarEntry>();
group1.add(new BarEntry(0, 3));
group1.add(new BarEntry(1, 1));
group1.add(new BarEntry(2, 4));
group1.add(new BarEntry(3, 7));
group1.add(new BarEntry(4, 3));
group1.add(new BarEntry(5, 8));
BarDataSet barDataSet = new BarDataSet(group1, "Brand 1");
barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(barDataSet);
return barData;
}
}