为MPAndroid柱状图添加X轴标签,仅显示数组列表中的第一个标签。

huangapple go评论77阅读模式
英文:

adding x-axis labels to mpandroid bar chart shows only the first label in the array list

问题

我想为我的条形图添加 x 轴标签,但我只能看到第一个标签。

以下是我使用的代码。我已经从我使用的 mpandroidchart 库的文档中提供的演示数据进行了自定义。

public class HomeFragment extends SimpleFragment implements OnChartGestureListener {

    @NonNull
    public static Fragment newInstance() {
        return new HomeFragment();
    }

    private BarChart chart;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);

        BarChart chart = (BarChart) v.findViewById(R.id.chart);
        BarData data = new BarData(getDataSet());
        data.setBarWidth(10f);
        chart.setData(data);
        chart.animateXY(2000, 2000);
        chart.invalidate();
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(true);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues()));

        return v;
    }

    private ArrayList getDataSet() {
        ArrayList dataSets = null;
        ArrayList valueSet1 = new ArrayList();
        // 数据条目的添加
        // ...
        BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Monthly Sales");
        barDataSet1.setColors(ColorTemplate.MATERIAL_COLORS);
        dataSets = new ArrayList();
        dataSets.add(barDataSet1);
        return dataSets;
    }

    private ArrayList getXAxisValues() {
        ArrayList xAxis = new ArrayList();
        // x 轴标签的添加
        // ...
        return xAxis;
    }
}

我对这个库还不太熟悉,所以可能有些错误。我使用这个示例,因为将来从数据库获取数据时可以更容易地进行自定义。我可能做错了什么?

我尝试过在循环中使用这一行代码 xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues().subList(0,i)));,如下所示:

for (int i = 0; i <= 12; i++) {
    xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues().subList(0, i)));
}

但仍然只显示第一个标签。

英文:

I would like to add x-axis labels to my bar chart all I get is the first label only being shown
为MPAndroid柱状图添加X轴标签,仅显示数组列表中的第一个标签。

The code that I am using is as follows. I have customized it from the demo data provided in the documentation of the library that I am using mpandroidchart

      public class HomeFragment extends SimpleFragment implements OnChartGestureListener {
@NonNull
public static Fragment newInstance() {
return new HomeFragment();
}
private BarChart chart;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
BarChart chart = (BarChart) v.findViewById(R.id.chart);
BarData data = new BarData(getDataSet());
data.setBarWidth(10f);
chart.setData(data);
chart.animateXY(2000, 2000);
chart.invalidate();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(true);
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues()));
return v;
}
private ArrayList getDataSet() {
ArrayList dataSets = null;
ArrayList valueSet1 = new ArrayList();
BarEntry v1e1 = new BarEntry(0.f, 0); // Jan
valueSet1.add(v1e1);
BarEntry v1e2 = new BarEntry(20.000f, 2); // Feb
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(40.000f, 4); // Mar
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(60.000f, 6); // Apr
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(80.000f, 8); // May
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(100.000f, 10); // Jun
valueSet1.add(v1e6);
BarEntry v1e7 = new BarEntry(120.000f, 12); // Jan
valueSet1.add(v1e7);
BarEntry v1e8 = new BarEntry(140.000f, 14); // Feb
valueSet1.add(v1e8);
BarEntry v1e9 = new BarEntry(160.000f, 16); // Mar
valueSet1.add(v1e9);
BarEntry v1e10 = new BarEntry(180.000f, 18); // Apr
valueSet1.add(v1e10);
BarEntry v1e11 = new BarEntry(200.000f, 20); // May
valueSet1.add(v1e11);
BarEntry v1e12 = new BarEntry(220.000f, 22); // Jun
valueSet1.add(v1e12);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, &quot;Monthly Sales&quot;);
barDataSet1.setColors(ColorTemplate.MATERIAL_COLORS);
dataSets = new ArrayList();
dataSets.add(barDataSet1);
return dataSets;
}
private ArrayList getXAxisValues() {
ArrayList xAxis = new ArrayList();
xAxis.add(&quot;JAN&quot;);
xAxis.add(&quot;FEB&quot;);
xAxis.add(&quot;MAR&quot;);
xAxis.add(&quot;APR&quot;);
xAxis.add(&quot;MAY&quot;);
xAxis.add(&quot;JUN&quot;);
xAxis.add(&quot;JULY&quot;);
xAxis.add(&quot;AUG&quot;);
xAxis.add(&quot;SEPT&quot;);
xAxis.add(&quot;OCT&quot;);
xAxis.add(&quot;NOV&quot;);
xAxis.add(&quot;DEC&quot;);
return xAxis;
}
}

I am new to this library hence probably I am doing some mistakes, I am using this sample as it will be easy to customize it further to get data from the database. What could I be doing wrong?

I have tried this line xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues())); in a loop as follows

    for(int i=0; i&lt;=12;i++){
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues().subList(0,i)));
}

That too is just displaying the first label only

答案1

得分: 1

你不需要以下的代码部分:

for(int i=0; i<=12;i++){
    xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues().subList(0,i)));
}

你只需要用以下一行代码替换上面的代码:

chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(getXAxisLables()));

而获取标签的方法如下:

private ArrayList<String> getXAxisLabels()
{
    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;
}

请根据你的需求修改getLabels()方法。

英文:

You don't need following code:

for(int i=0; i&lt;=12;i++){
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues().subList(0,i)));
}

All you need is to replace above code with this line:

chart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(getXAxisLables()));

And the method to get labels is:

private ArrayList&lt;String&gt; getXAxisLabels()
{
ArrayList&lt;String&gt; labels = new ArrayList&lt;String&gt; ();
labels.add( &quot;JAN&quot;);
labels.add( &quot;FEB&quot;);
labels.add( &quot;MAR&quot;);
labels.add( &quot;APR&quot;);
labels.add( &quot;MAY&quot;);
labels.add( &quot;JUN&quot;);
return labels;
}

Please modify the getLabels() method in accordance to your requirements.

huangapple
  • 本文由 发表于 2020年8月18日 15:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63463955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定