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

}

}

2 Answers 2

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));
Sign up to request clarification or add additional context in comments.

10 Comments

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.
before ur adding graphView to llGraphView
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
@CockpitAliens can you also paste relevant error log?
Hi Shaz please check the error log posted and kindly help me. Thanks
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.