本文介绍了Mpandroidchart 文本未正确显示在折线图的 x 轴上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Mpandroidchart.我创建了一个折线图并尝试在我的 xaxis 上放置字符串.

I am working on Mpandroidchart. I have created a line chart and tried to place strings on my xaxis.

final ArrayList<String> xAxisLabel = new ArrayList<>(Arrays.asList("March","April","May"));



private ArrayList<Entry> lineChartDataSet(){
    ArrayList<Entry> dataSet = new ArrayList<Entry>();

    dataSet.add(new Entry(1,40));
    dataSet.add(new Entry(2,10));
    dataSet.add(new Entry(3,15));    
    return  dataSet;


}

 lineDataSet = new LineDataSet(lineChartDataSet(),"data set");
 XAxis xAxis = lineChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);

    //String setter in x-Axis
    xAxis.setValueFormatter(new com.github.mikephil.charting.formatter.IndexAxisValueFormatter(xAxisLabel));
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);

输出

在上图中,您可以看到月份名称未正确放置.即 march 不显示,只显示 aprilmay.

In the above image, you can see that month names are not properly placed. i.e march is not displaying and only april and may are shown.

预期产出

我该怎么做?

任何帮助将不胜感激

推荐答案

在 MPAndroidChart v3.1.0 版中,您可以使用自定义的 ValueFormatter 代替 IndexAxisValueFormatter 并覆盖 >String getAxisLabel(float value, AxisBase axis) 方法,您可以在其中定义自己的逻辑来替换每个 X 值对应的字符串.基于您的数据集的示例如下所示:

Instead of IndexAxisValueFormatter in MPAndroidChart version v3.1.0 you can use a custom ValueFormatter and override String getAxisLabel(float value, AxisBase axis) method where you can define your own logic to replace for each X value its corresponding String. Example based on your data Set will be like below:

xAxis.setValueFormatter(new ValueFormatter() {
  @Override
  public String getAxisLabel(float value, AxisBase axis) {
     String label = "";
     if(value == 1)
       label = "March";
     else if(value == 2)
       label = "April";
     else if(value == 3)
       label = "May";
     return label;
   }
}); 

结果如下:

这篇关于Mpandroidchart 文本未正确显示在折线图的 x 轴上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 21:49