如何在MPAndroidChart中设置xAxis的字符串值? [英] How to set String value of xAxis in MPAndroidChart?

查看:195
本文介绍了如何在MPAndroidChart中设置xAxis的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作折线图,但是我无法在xAxis中显示值字符串,我使用了 MPAndroidChart 到LineChart.请帮助我如何添加字符串值以及实际上要问的很多问题

I want to make Line Chart Graph, but i have problem to show value string in xAxis, im used Library Github from MPAndroidChart to LineChart. Please help me how to add String Value and actually to much question i want ask

private void drawLineChartLine(){

        private float[] yDataL = {40, 60, 70, 80};
        private String[] xDataL = {"Week 1", "Week 1" , "Week 3" , "Week 4"};


        ArrayList<Entry> yEntrys = new ArrayList<>();

        final ArrayList<String> xEntrys = new ArrayList<>();

        for(int i = 0; i < yDataL.length; i++){
            yEntrys.add(new Entry(yDataL[i] ,i));
        }

        for(int i = 1; i < xDataL.length; i++){
            xEntrys.add(xDataL[i]);
        }

        //create the data set
        LineDataSet lineDataset = new LineDataSet(yEntrys, "assa");

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);


        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xEntrys.get((int) value);
            }
        });

        LineData lineData = new LineData(lineDataset);
        lineChart.setData(lineData);
        lineChart.invalidate();
}

我遇到错误

无效的索引40,大小为6

Invalid index 40, size is 6

代码

xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xEntrys.get((int) value);
            }
        });

推荐答案

public class IndexAxisValueFormatter extends ValueFormatter
{
    private String[] mValues = new String[] {};
private int mValueCount = 0;

/**
 * An empty constructor.
 * Use `setValues` to set the axis labels.
 */
public IndexAxisValueFormatter() {
}

/**
 * Constructor that specifies axis labels.
 *
 * @param values The values string array
 */
public IndexAxisValueFormatter(String[] values) {
    if (values != null)
        setValues(values);
}

/**
 * Constructor that specifies axis labels.
 *
 * @param values The values string array
 */
public IndexAxisValueFormatter(Collection<String> values) {
    if (values != null)
        setValues(values.toArray(new String[values.size()]));
}

@Override
public String getFormattedValue(float value, AxisBase axisBase) {
    int index = Math.round(value);

    if (index < 0 || index >= mValueCount || index != (int)value)
        return "";

    return mValues[index];
}

public String[] getValues()
{
    return mValues;
}

public void setValues(String[] values)
{
    if (values == null)
        values = new String[] {};

    this.mValues = values;
    this.mValueCount = values.length;
}
}

在尝试显示条形图的方法中添加以下代码.

Add the below code in the method where you are trying to display Bar Chart.

final String[] weekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Your List / array with String Values For X-axis Labels

// Set the value formatter 
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(weekdays));

解决了!编码愉快.

这篇关于如何在MPAndroidChart中设置xAxis的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆