2

Hi I have written a json parser to pull some data in an array like this

"records": [ [ "2015-01-11 04:50", 1.5 ], [ "2015-01-11 05:00", 1.6 ], [ "2015-01-11 05:10", 1.7 ], [ "2015-01-11 05:20", 1.6 ], [ "2015-01-11 05:30", 1.5 ], [ "2015-01-11 05:40", 1.7 ], [ "2015-01-11 05:50", 1.8 ], [ "2015-01-11 06:00", 1.8 ], [ "2015-01-11 06:10", 1.9 ], [ "2015-01-11 06:20", 1.8 ], [ "2015-01-11 06:30", 1.6 ], [ "2015-01-11 06:40", 1.4 ], [ "2015-01-11 06:50", 1.6 ], [ "2015-01-11 07:00", 1.4 ], [ "2015-01-11 07:10", 1.4 ], ]

But now I can't get to apply the Graph view library to plot the x,y and show the data as line graph in the app. Kindly advice what I am missing in my code below. Help me save a lot of time because I tried the GraphViewSeries exampleSeries data, It works but i don't want to hard code the gragh data. As its supplied from a REST web service, so just want to plot the data from using the Json parser.

Here is my code;

public class GraphFragment extends Fragment implements OnClickListener {
Context context;
String label_y [];
ArrayList<DataAssetGraph> alAssetGraph = new ArrayList<DataAssetGraph>();
TextView tvNoItemsG;
LinearLayout llGraphView;
Button btnOneDayG, btnOneWeekG, btnOneMonthG;
String startDate;
String assetId;
ProgressDialog dialog;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = null;
    view = inflater.inflate(R.layout.fragment_chart, null);
    initView(view);
    tvNoItemsG.setVisibility(View.VISIBLE);
    /*if (getArguments() != null) {

        alAssetGraph = (ArrayList<DataAssetGraph>) getArguments()
                .getSerializable(WebElement.RECORDS);
        if (alAssetGraph != null && alAssetGraph.size() > 0) {
            label_y = getHorizontalLabels(alAssetGraph);
            initGraphView();
        } else {
            llGraphView.setVisibility(View.GONE);
            tvNoItemsG.setVisibility(View.VISIBLE);

        }
    }*/
    return view;
}

private void initView(View view) {
    tvNoItemsG = (TextView) view.findViewById(R.id.tvNoItemsG);
    llGraphView = (LinearLayout) view.findViewById(R.id.llGraphView);
    btnOneDayG = (Button) view.findViewById(R.id.btnOneDayG);
    btnOneWeekG = (Button) view.findViewById(R.id.btnOneWeekG);
    btnOneMonthG = (Button) view.findViewById(R.id.btnOneMonthG);
    btnOneDayG.setSelected(true);
    btnOneDayG.setOnClickListener(this);
    btnOneMonthG.setOnClickListener(this);
    btnOneWeekG.setOnClickListener(this);
}

private void initGraphView() {

    /*GraphViewSeries exampleSeries = new GraphViewSeries(
            new GraphViewData[] { new GraphViewData(1, 2.0d),
                    new GraphViewData(2, 1.5d), new GraphViewData(3, 2.5d),
                    new GraphViewData(4, 1.0d) });*/

    GraphView graphView = new LineGraphView(getActivity(), "");

    // graphView.addSeries(exampleSeries);

    if (label_y != null && label_y.length > 0) {
        graphView.setHorizontalLabels(label_y);
    }
    /*
     * else { GraphViewSeries exampleSeries = new GraphViewSeries(new
     * GraphViewData[]{new GraphViewData(1, 2.0d), new GraphViewData(2,
     * 1.5d),new GraphViewData(3, 2.5d),new GraphViewData(4, 1.0d)});
     * graphView.addSeries(exampleSeries); }
     */
    llGraphView.addView(graphView);
}

private GraphViewData[] getArrayOfPoints() {
    GraphViewData[] mArray = new GraphViewData[10];
    return mArray;
}

private String[] getHorizontalLabels(ArrayList<DataAssetGraph> arrayList) {
    int size = arrayList.size();
    String[] array_labels = new String[size];

    for (int i = 0; i < size; i++) {
        array_labels[i] = arrayList.get(i).x_cord;
    }
    return array_labels;

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnOneDayG:
        btnOneWeekG.setSelected(false);
        btnOneMonthG.setSelected(false);
        if (!btnOneDayG.isSelected()) {
            btnOneDayG.setSelected(true);
            String end_date = GlobalData.getDateAfterOneDay();
            getGraphHistory(end_date);
        }
        break;

    case R.id.btnOneWeekG:
        btnOneDayG.setSelected(false);
        btnOneMonthG.setSelected(false);
        if (!btnOneWeekG.isSelected()) {
            btnOneWeekG.setSelected(true);
            String end_date = GlobalData.getDateAfterOneWeek();
            getGraphHistory(end_date);
        }
        break;
    case R.id.btnOneMonthG:
        btnOneWeekG.setSelected(false);
        btnOneDayG.setSelected(false);
        if (!btnOneMonthG.isSelected()) {
            btnOneMonthG.setSelected(true);
            String end_date = GlobalData.getDateAfterOneMonth();
            getGraphHistory(end_date);
        }
        break;

    default:
        break;
    }
}

private void getGraphHistory(String end_date) {
    dialog = new ProgressDialog(getActivity());
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setMessage("Retrieving graph...");
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
    startDate = GlobalData.getCurrentDate();
    end_date =  GlobalData.getDateAfterOneDay();
    assetId = ComponentActivity.assetId;
    new LoadAssetGraphTask(getActivity(), assetId, end_date, startDate ) {
        @Override
        protected void onPostExecute(ArrayList<DataAssetGraph> result) {
            super.onPostExecute(result);
            if (dialog.isShowing())
                dialog.dismiss();
            if (result != null && result.size() > 0) {
                label_y = getHorizontalLabels(result);
                initGraphView();
            } else {
                llGraphView.setVisibility(View.GONE);
                tvNoItemsG.setVisibility(View.VISIBLE);

            }
        };
    }.execute();

}

}

Cockpit Aliens
  • 411
  • 6
  • 18

2 Answers2

1

try something like this

GraphView.GraphViewData[] recordGraphData=new GraphView.GraphViewData[records.size()];
for(int i=0; i<records.size();i++)
{
recordGraphData[i]=new GraphView.GraphViewData(i,records.get(i).get(1));
}

graphView.addSeries(new GraphViewSeries(recordsGraphData));
Shaz
  • 302
  • 3
  • 13
  • I tried this but doesn't work either. Any other suggestions of point me to where exactly in my code I should modify with your suggestions. – Cockpit Aliens Jan 12 '15 at 21:30
  • before ur adding graphView to llGraphView – Shaz Jan 12 '15 at 21:31
  • Shaz i tried the above but still getting error on this line see the code i recordGraphData[i]=new GraphView.GraphViewData(i,records.get(i).get(1)); see the 3 classes involved and kindly assist – Cockpit Aliens Jan 16 '15 at 11:16
  • @CockpitAliens can you also paste relevant error log? – Shaz Jan 16 '15 at 17:28
  • Hi Shaz please check the error log posted and kindly help me. Thanks – Cockpit Aliens Jan 19 '15 at 06:54
  • @CockpitAliens I cannot see any error log posted, please verify? – Shaz Jan 19 '15 at 23:22
  • Maybe the administrator removed it. But the error log simple point to this line recordGraphData[i]. I'm sure I'm not passing the extras from the json parser properly I guess. – Cockpit Aliens Jan 20 '15 at 04:18
  • @CockpitAliens try to follow above answer and refactor your code accordingly. most probably it should help to solve your problem. – Shaz Jan 20 '15 at 04:51
  • Ok if I have ArrayList graphData objects x_cord and y_cord implementing serialiseble and I parse json from another class how would I create recordGraphData with the extras from the json parser – Cockpit Aliens Jan 20 '15 at 05:07
  • 01-20 08:56:29.549: E/AndroidRuntime(22814): FATAL EXCEPTION: main 01-20 08:56:29.549: E/AndroidRuntime(22814): Process: com.app .beyond, PID: 22814 01-20 08:56:29.549: E/AndroidRuntime(22814): java.lang.NullPointerException 01-20 08:56:29.549: E/AndroidRuntime(22814): at com.app .beyond.fragment.GraphFragment.initGraphView(GraphFragment.java:83) 01-20 08:56:29.549: E/AndroidRuntime(22814): at com.applord.app.beyond.fragment.GraphFragment.onCreateView(GraphFragment.java:54) – Cockpit Aliens Jan 20 '15 at 06:58
0

there are examples of adding data to the graph in runtime. take a look at the GraphView-Demos project.

https://play.google.com/store/apps/details?id=com.jjoe64.graphview_demos

and documentation http://www.android-graphview.org/documentation/realtime-updates

appsthatmatter
  • 6,347
  • 3
  • 36
  • 40