adding date on xaxis and value on Yaxis in MP android Line chart?

时间:2017-12-18 07:23:12

标签: android graph mpandroidchart

I am using Library MPAndroid,compile 'com.github.PhilJay:MPAndroidChart:v3.0.0-beta1'

I have to pass dates in x-axis and value on y-axis in MPAndroid Line chart, when I am passing values in x-axis or yaxis, the app gets crashed showing ArrayIndexOutOfBound Exception, with array size as -2.

How, can I acheive this? I have used xAxis value formatter as well. Please, help.Below is my code

{ mChart.setOnChartGestureListener(this); mChart.setOnChartValueSelectedListener(this); mChart.setDrawGridBackground(false);

    mChart.setNoDataTextDescription("You need to provide data for the chart.");
    mChart.setTouchEnabled(false);
    mChart.getAxisLeft().setDrawGridLines(false);
    mChart.getXAxis().setDrawGridLines(false);
    mChart.setDragEnabled(false);
    mChart.setScaleEnabled(false);
    mChart.getAxisLeft().setDrawLimitLinesBehindData(true);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setLabelRotationAngle(270);
    xAxis.setAxisLineWidth(3);
    xAxis.setAxisMaxValue(310f);
    xAxis.setTextSize(13f);
    xAxis.setTextColor(Color.WHITE);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    leftAxis.setAxisMaxValue(330f);
    leftAxis.setAxisMinValue(50f);
    leftAxis.setAxisLineWidth(3);
    leftAxis.setDrawAxisLine(false);
    leftAxis.setDrawGridLines(true);
    leftAxis.setTextSize(13f);
    leftAxis.setGranularity(1f);
    leftAxis.setTextColor(Color.WHITE);
    leftAxis.setDrawZeroLine(false);
    leftAxis.setDrawLimitLinesBehindData(false);

    mChart.getAxisRight().setEnabled(false);
    mChart.animateX(2500, Easing.EasingOption.EaseInOutQuart);

    mChart.invalidate();

}

      private ArrayList<String> setXAxisValues() {
    return typesList;
}

private ArrayList<Entry> setYAxisValues() {
    float[] EndTime = new float [glucoseReportReading.length];
    for (int i=0; i<glucoseReportReading.length;i++)
    {
        float number = Float.parseFloat(glucoseReportReading[i]);
        float rounded = (int) Math.round(number * 1000) / 1000f;
        EndTime[i] = rounded;
    }

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

    for(int i=0;i<glucoseReadingArrayList.size();i++){
        yVals.add(new Entry(EndTime[i],i));
    }

    return yVals;
}

private void setData() {
    GraphModelClass graphModelClass = new GraphModelClass();
    ArrayList<Entry> yVals1 = setYAxisValues();
    ArrayList<String> xVals1 = setXAxisValues();
    graphModelClass.setyVals(yVals1);
    graphModelClass.setxVals(xVals1);

    ArrayList<String> xVals = graphModelClass.getxVals();//setXAxisValues();
    ArrayList<Entry> yVals = graphModelClass.getyVals();//setYAxisValues();

    LineDataSet set1;

    set1 = new LineDataSet(yVals, "DataSet 1");
    set1.setFillColor(Color.TRANSPARENT);
    set1.setColor(Color.RED);
    set1.setCircleColor(Color.BLUE);
    set1.setLineWidth(2f);
    set1.setCircleRadius(4f);
    set1.setDrawCircleHole(true);
    set1.setValueTextSize(10f);
    set1.setDrawFilled(true);
   /* ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
    dataSets.add(set1);*/
    LineData data = new LineData(xVals,set1);
    mChart.setData(data);}

2 个答案:

答案 0 :(得分:0)

Using an <input type="text"> is correct. The Problem you are facing is that the Library wants to draw labels before and after your chart, but can't find values in your array.

You can solve this by checking the indizes and retourning empty labels.

AxisValueFormatter

If you don't want empty spaces before and after your chart you can further use

@Override
public String getFormattedValue(float value, AxisBase axis) {
    if (value < 0 || value > glucoseReportTime.size() - 1)
        return "";
    return glucoseReportTime[(int) value];
}

答案 1 :(得分:0)

替换此行:

for(int i=0;i<glucoseReadingArrayList.size();i++)

使用:

for(int i=0;i<typesList.size();i++)