PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.63k stars 9.02k forks source link

Array Out Of Bounds Exception when creating Bar Chart #2319

Open tvajjhala opened 8 years ago

tvajjhala commented 8 years ago

Here is the error: FATAL EXCEPTION: main Process: com.example.tvajjhal.sposv2, PID: 16564 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.github.mikephil.charting.renderer.XAxisRendererHorizontalBarChart.drawLabels(XAxisRendererHorizontalBarChart.java:127) at com.github.mikephil.charting.renderer.XAxisRendererHorizontalBarChart.renderAxisLabels(XAxisRendererHorizontalBarChart.java:78) at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:267) at android.view.View.draw(View.java:17469) at android.view.View.updateDisplayListIfDirty(View.java:16464) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3905) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3885) at android.view.View.updateDisplayListIfDirty(View.java:16424) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:325) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:331) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:366) at android.view.ViewRootImpl.draw(ViewRootImpl.java:3140) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2939) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2522) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7414) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) at android.view.Choreographer.doCallbacks(Choreographer.java:695) at android.view.Choreographer.doFrame(Choreographer.java:631) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) If needed I can upload my code, but this issue has really been plaguing me for a while

ifeomaro commented 8 years ago

@tvajjhala include your code.

tvajjhala commented 8 years ago

`package com.example.tvajjhal.spos_graph;

import android.graphics.Color; import android.os.Environment; import android.os.Handler; import android.os.Looper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.AbsListView; import android.widget.FrameLayout;

import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.HorizontalBarChart; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.utils.ColorTemplate;

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.security.Signature; import java.util.ArrayList; import java.util.Arrays; import java.util.List;

public class MainActivity extends AppCompatActivity { FrameLayout graphLayout; HorizontalBarChart mChart;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    graphLayout = (FrameLayout) findViewById(R.id.graphlayout);
    mChart = new HorizontalBarChart(this);
    graphLayout.addView(mChart, new AbsListView.LayoutParams
            (AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
    mChart.setDescription("");
    mChart.setNoDataTextDescription("No Data Found");
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(false);
    mChart.setDrawGridBackground(false);
    mChart.setPinchZoom(false);
    mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    mChart.setBackgroundColor(Color.TRANSPARENT);
    mChart.setAutoScaleMinMaxEnabled(true);
    mChart.getXAxis().setLabelsToSkip(0);
    PleaseWork();
    //MyRunnable myRunnable = new MyRunnable(10);
    //handler= new Handler(Looper.getMainLooper());
 /*   Thread t = new Thread(this);
    t.start();*/
    //BARDATA = new BarData(BarEntryLabels, Bardataset);
    //mChart.setData(BARDATA);
    //mChart.animateY(3000);
}

/* public void AddValuesToBARENTRY() {

     BARENTRY.add(new BarEntry(20f, 0));
     BARENTRY.add(new BarEntry(4f, 1));
     BARENTRY.add(new BarEntry(6f, 2));
     BARENTRY.add(new BarEntry(8f, 3));
     BARENTRY.add(new BarEntry(7f, 4));
     BARENTRY.add(new BarEntry(3f, 5));

 }

 public void AddValuesToBarEntryLabels() {

     BarEntryLabels.add("January");
     BarEntryLabels.add("February");
     BarEntryLabels.add("March");
     BarEntryLabels.add("April");
     BarEntryLabels.add("May");
     BarEntryLabels.add("June");

 }*/
private void PleaseWork() {
    Thread t = new Thread() { //new thread
        public void run() {
            Boolean b = true;
            try {
                do {
                    sleep(1000);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                          //  Looper.prepare();
                            ArrayList<BarEntry> BARENTRY;
                            BarData BARDATA;
                            ArrayList<String> BarEntryLabels;
                            BarDataSet Bardataset;

                            //   Looper.prepare();

                            File sdcard = Environment.getExternalStorageDirectory();
                            File file = new File(sdcard, "/Download/myfile.txt");
                            int offset = 15;
                            BARENTRY = new ArrayList<>();
                            BarEntryLabels = new ArrayList<String>();
                            Bardataset = new BarDataSet(BARENTRY, "Messages");
                            Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

                            try {
                                BufferedReader in = new BufferedReader(new FileReader(file));
                                Log.d("Status:", "File Opened");
                                String str;
                                str = in.readLine();
                                List<String> XLabelArray = new ArrayList<>();
                                List<String> YValueArray = new ArrayList<>();
                                while ((str = in.readLine()) != null) {
                                    String[] FullLine = str.split(",");
                                    String[] OnlySpos = Arrays.copyOfRange(FullLine, offset, FullLine.length);
                                    int SignalSets = OnlySpos.length / 5;
                                    for (int index = 0; index < SignalSets; index++) {
                                        String XLabel = OnlySpos[index * 5] + "," + OnlySpos[index * 5 + 1] + "," + OnlySpos[index * 5 + 2];
                                        String YValue = OnlySpos[index * 5 + 3];
                                        XLabelArray.add(XLabel);
                                        YValueArray.add(YValue);
                                        Log.d(XLabel, YValue);
                                    }
                                    for (int index = 0; index < SignalSets; index++) {
                                        BarEntryLabels.add(XLabelArray.get(index));
                                        BARENTRY.add(new BarEntry(java.lang.Math.abs(Float.parseFloat(YValueArray.get(index))), index + 1));
                                        //Log.d("Graph:", "Yes");
                                    }
                                    // mChart.invalidate();
                                    BARDATA = new BarData(BarEntryLabels, Bardataset);
                                    Thread.sleep(1000);

                                    mChart.setData(BARDATA);

// mChart.invalidate(); BarEntryLabels.clear(); BARENTRY.clear(); // mChart.clearValues(); in.close();

                                }

                            } catch (IOException e) {
                                Log.d("Error:", "File Read Error");

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            mChart.notifyDataSetChanged();
                            mChart.invalidate();

                        }
                    });

                }
                while (b == true);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }
    };
    t.start();
}

public void run2() {

}

} `

tvajjhala commented 8 years ago

any suggestions?