使用GraphView Library android从Json数据绘制图形

时间:2015-01-12 20:37:50

标签: android json plot coordinates android-graphview

您好我已经编写了一个json解析器来提取像这样的数组中的一些数据

"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 ], ]

但现在我无法应用Graph视图库绘制x,y并在应用程序中将数据显示为折线图。请在下面的代码中建议我缺少的内容。帮助我节省了大量时间,因为我尝试了GraphViewSeries exampleSeries数据,它工作但我不想硬编码gragh数据。由于它是从REST Web服务提供的,所以只想绘制使用Json解析器的数据。

这是我的代码;

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 个答案:

答案 0 :(得分:1)

尝试这样的事情

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

答案 1 :(得分:0)

有一些在运行时向图表添加数据的示例。看一下GraphView-Demos项目。

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

和文档 http://www.android-graphview.org/documentation/realtime-updates