使用 GraphView 库 android 根据 Json 数据绘制图形

Plotting Graph from Json Data using GraphView Library android

你好,我写了一个 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 ], ]

但现在我无法应用图形视图库来绘制 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();

}

}

尝试这样的事情

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

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

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

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