如何在MPAndroidChart中设置xAxis的字符串值?

时间:2017-07-26 07:39:52

标签: android linechart mpandroidchart

我想制作折线图,但我有问题在xAxis中显示值字符串,我使用了从MPAndroidChart到LineChart的Library Github。请帮我如何添加字符串值,实际上我想问的问题

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

代码中的

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

8 个答案:

答案 0 :(得分:3)

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;
}
}

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

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));

解决了!编码愉快。

答案 1 :(得分:2)

您正在访问超出范围的ArrayList索引,因为您正在尝试将值用作索引。您需要找到值的索引才能返回它。

xAxis.setValueFormatter(new IAxisValueFormatter() {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
       for (int i = 0 ; i < yEntrys.size(); ++i) {
           if (yEntrys.get(i).equals(value)) {
               return xEntrys.get(i);
           }
       }
       return null;
    }
});

答案 2 :(得分:1)

简单的实现

            XAxis xAxis = mChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setDrawGridLines(true);
            xAxis.setGranularity(1f);
            xAxis.setGranularityEnabled(true);
            final String xVal[]={"Val1","Val2","Val3"};
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return xVal[(int) value-1]; // xVal is a string array
                }

            });

答案 3 :(得分:1)

这里是动态字符串标签(XAxis)的简单方法,

XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new IndexAxisValueFormatter(getAreaCount));

public ArrayList<String> getAreaCount() {

            ArrayList<String> label = new ArrayList<>();
            for (int i = 0; i < areaList.size(); i++)
                label.add(areaList.get(i).getTopicName());
            return label;
        }

答案 4 :(得分:1)

根据新版本v3.1.0, IAxisValueFormatter 方法已弃用。

因此,使用 com.github.mikephil.charting.formatter.IndexAxisValueFormatter 我们可以在x轴上显示月份列表

private List<Entry> getIncomeEntries() {
ArrayList<Entry> incomeEntries = new ArrayList<>();

incomeEntries.add(new Entry(1, 11300));
incomeEntries.add(new Entry(2, 1390));
incomeEntries.add(new Entry(3, 1190));
incomeEntries.add(new Entry(4, 7200));
incomeEntries.add(new Entry(5, 4790));
incomeEntries.add(new Entry(6, 4500));
incomeEntries.add(new Entry(7, 8000));
incomeEntries.add(new Entry(8, 7034));
incomeEntries.add(new Entry(9, 4307));
incomeEntries.add(new Entry(10, 8762));
incomeEntries.add(new Entry(11, 4355));
incomeEntries.add(new Entry(12, 6000));
return incomeEntries.subList(0, 12);
}

在Oncreate方法中

 ArrayList<ILineDataSet> dataSets = new ArrayList<>();
List<String> xAxisValues = new ArrayList<>(Arrays.asList("Jan", "Feb", "March", "April", "May", "June","July", "August", "September", "October", "November", "Decemeber"));
List<Entry> incomeEntries = getIncomeEntries();
dataSets = new ArrayList<>();
LineDataSet set1;

set1 = new LineDataSet(incomeEntries, "Income");
set1.setColor(Color.rgb(65, 168, 121));
set1.setValueTextColor(Color.rgb(55, 70, 73));
set1.setValueTextSize(10f);
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSets.add(set1);

//customization
LineChart mLineGraph = findByViewId(R.id.line_chart);
mLineGraph.setTouchEnabled(true);
mLineGraph.setDragEnabled(true);
mLineGraph.setScaleEnabled(false);
mLineGraph.setPinchZoom(false);
mLineGraph.setDrawGridBackground(false);
mLineGraph.setExtraLeftOffset(15);
mLineGraph.setExtraRightOffset(15);
//to hide background lines
mLineGraph.getXAxis().setDrawGridLines(false);
mLineGraph.getAxisLeft().setDrawGridLines(false);
mLineGraph.getAxisRight().setDrawGridLines(false);

//to hide right Y and top X border
YAxis rightYAxis = mLineGraph.getAxisRight();
rightYAxis.setEnabled(false);
YAxis leftYAxis = mLineGraph.getAxisLeft();
leftYAxis.setEnabled(false);
XAxis topXAxis = mLineGraph.getXAxis();
topXAxis.setEnabled(false);


XAxis xAxis = mLineGraph.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setEnabled(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

set1.setLineWidth(4f);
set1.setCircleRadius(3f);
set1.setDrawValues(false);
set1.setCircleHoleColor(getResources().getColor(R.color.pie_color_4));
set1.setCircleColor(getResources().getColor(R.color.pie_color_4));

//String setter in x-Axis
mLineGraph.getXAxis().setValueFormatter(new com.github.mikephil.charting.formatter.IndexAxisValueFormatter(xAxisValues));

LineData data = new LineData(dataSets);
mLineGraph.setData(data);
mLineGraph.animateX(2000);
mLineGraph.invalidate();
mLineGraph.getLegend().setEnabled(false);
mLineGraph.getDescription().setEnabled(false);

答案 5 :(得分:1)

chart.xAxis.valueFormatter = IndexAxisValueFormatter(list_of_labels)

答案 6 :(得分:0)

上述方法似乎已被弃用。

 xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
               // return the string va
                return  your_string_basedon_value_as_index;
            }
        });

答案 7 :(得分:0)

在版本3.1.0中,您必须覆盖getAxisLabel(),永远不会调用不赞成使用的方法getFormattedValue(float, AxisBase)。 Kotlin示例:

class MyFormatter : ValueFormatter() {

    override fun getAxisLabel(value: Float, axis: AxisBase): String {
        return "ABC"
    }
}