使用Achartengine的动态折线图不起作用

时间:2017-04-24 18:24:06

标签: android achartengine linechart

我正在使用Achartengine制作动态折线图,这是在处理静态数据但不处理动态数据。实际,计划和月份是动态的。我想根据实际,计划和月份的值创建折线图。我正在使用下面的代码。

        private void drawChart() {

        XYSeries actualSeries = new XYSeries("Actual");
        // Creating an  XYSeries for Expense<br />
        XYSeries planSeries = new XYSeries("Plan");
        // Adding data to Income and Expense Series
        for (int i = 0; i <actualList.size; i++) {
            actualSeries.add(i,Double.parseDouble(actualList.get(i)));
            planSeries.add(i,Double.parseDouble(planList.get(i)));

        }
        // Creating a dataset to hold each series
        XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
        // Adding Income Series to the dataset
        xyMultipleSeriesDataset.addSeries(actualSeries);
        // Adding Expense Series to dataset
        xyMultipleSeriesDataset.addSeries(planSeries);

        XYSeriesRenderer actualRenderer = new XYSeriesRenderer();
        actualRenderer.setColor(Color.GREEN);
        actualRenderer.setPointStyle(PointStyle.CIRCLE);
        actualRenderer.setFillPoints(true);
        actualRenderer.setLineWidth(2);
        actualRenderer.setDisplayChartValues(true);
        // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer planRenderer = new XYSeriesRenderer();
        planRenderer.setColor(Color.RED);
        planRenderer.setPointStyle(PointStyle.CIRCLE);
        planRenderer.setFillPoints(true);
        planRenderer.setLineWidth(2);
        planRenderer.setDisplayChartValues(true);
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setApplyBackgroundColor(true);
        multiRenderer.setBackgroundColor(Color.WHITE);
        multiRenderer.setMarginsColor(Color.WHITE);
        multiRenderer.setLabelsColor(Color.RED);
        multiRenderer.setPanEnabled(false, false);
       /* multiRenderer.setYLabels(5);
        multiRenderer.setXLabels(1);
       */ multiRenderer.setXLabelsColor(Color.BLACK);
        multiRenderer.setYLabelsColor(0, Color.BLACK);
        //*  multiRenderer.setShowGridX(true);
        //multiRenderer.setShowGridY(true)
        multiRenderer.setGridColor(Color.BLACK);
        multiRenderer.setChartTitle("Plan vs Actual Curve");
        multiRenderer.setXTitle("Months");
        multiRenderer.setYTitle("plan/actual");
        multiRenderer.setZoomButtonsVisible(false);
        for (int i = 0; i < monthList.size(); i++) {
            multiRenderer.addXTextLabel(i + 1, monthList.get(i).toString());
        }

        multiRenderer.addSeriesRenderer(actualRenderer);
        multiRenderer.addSeriesRenderer(planRenderer);

        // Getting a reference to LinearLayout of the MainActivity Layout<br />
        LinearLayout chartContainer = (LinearLayout)  mView.findViewById(R.id.chart_container);
        // Creating a Line Chart<br />
        GraphicalView chart = ChartFactory.getLineChartView(getActivity(),   xyMultipleSeriesDataset, multiRenderer);
        chartContainer.addView(chart);
        if (chart != null) {
            chartContainer.removeView(chart);

            chart.repaint();

        }

    } 

Thanks in advance

1 个答案:

答案 0 :(得分:0)

我在做了一些研究后自己解决了我的问题。问题是UI更新问题。一旦图表被绘制,那么对于新数据,我们必须删除该视图,并应根据数据创建新的图表视图.Below是工作代码。

private void drawChart() {

    // Creating an  XYSeries for Income
    XYSeries actualSeries = new XYSeries("Actual");
    // Creating an  XYSeries for Expense<br />
    XYSeries planSeries = new XYSeries("Plan");
    XYSeries cupOneSeries = new XYSeries("Cup1");
    XYSeries cupTwoSeries = new XYSeries("Cup2");
    XYSeries cupThreeSeries = new XYSeries("Cup3");
    // Adding data to Income and Expense Series
    Log.e("x axis value in curve", "" + xAxisValueList.size());
    Log.e("size of Actual", actualList.size() + "");
    Log.e("size of plan", planList.size() + "");
    for (int i = 0; i < actualList.size(); i++) {
        actualSeries.add(i, Math.round(Double.parseDouble(actualList.get(i))));
        planSeries.add(i, Math.round(Double.parseDouble(planList.get(i))));
        cupOneSeries.add(i, Math.round(Double.parseDouble(cupOneList.get(i))));
        cupTwoSeries.add(i, Math.round(Double.parseDouble(cupTwoList.get(i))));
        cupThreeSeries.add(i, Math.round(Double.parseDouble(cupThreeList.get(i))));

    }
    // Creating a dataset to hold each series
    XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
    // Adding Income Series to the dataset
    xyMultipleSeriesDataset.addSeries(actualSeries);
    // Adding Expense Series to dataset
    xyMultipleSeriesDataset.addSeries(planSeries);

    xyMultipleSeriesDataset.addSeries(cupOneSeries);
    xyMultipleSeriesDataset.addSeries(cupTwoSeries);
    xyMultipleSeriesDataset.addSeries(cupThreeSeries);

    XYSeriesRenderer actualRenderer = new XYSeriesRenderer();
    actualRenderer.setColor(getActivity().getResources().getColor(R.color.logo_blue));
    actualRenderer.setPointStyle(PointStyle.CIRCLE);
    actualRenderer.setFillPoints(true);
    actualRenderer.setLineWidth(2);
    actualRenderer.setDisplayChartValues(true);
    // Creating XYSeriesRenderer to customize expenseSeries
    XYSeriesRenderer planRenderer = new XYSeriesRenderer();
    planRenderer.setColor(getActivity().getResources().getColor(R.color.logo_orange));
    planRenderer.setPointStyle(PointStyle.CIRCLE);
    planRenderer.setFillPoints(true);
    planRenderer.setLineWidth(2);
    planRenderer.setDisplayChartValues(true);

    XYSeriesRenderer cupOneRenderer = new XYSeriesRenderer();
    cupOneRenderer.setColor(getActivity().getResources().getColor(R.color.black));
    cupOneRenderer.setPointStyle(PointStyle.CIRCLE);
    cupOneRenderer.setFillPoints(true);
    cupOneRenderer.setLineWidth(2);
    cupOneRenderer.setDisplayChartValues(true);

    XYSeriesRenderer cupTwoRenderer = new XYSeriesRenderer();
    cupTwoRenderer.setColor(getActivity().getResources().getColor(R.color.green));
    cupTwoRenderer.setPointStyle(PointStyle.CIRCLE);
    cupTwoRenderer.setFillPoints(true);
    cupTwoRenderer.setLineWidth(2);
    cupTwoRenderer.setDisplayChartValues(true);

    XYSeriesRenderer cupThreeRenderer = new XYSeriesRenderer();
    cupThreeRenderer.setColor(Color.parseColor("#0D8ECF"));
    cupThreeRenderer.setPointStyle(PointStyle.CIRCLE);
    cupThreeRenderer.setFillPoints(true);
    cupThreeRenderer.setLineWidth(2);
    cupThreeRenderer.setDisplayChartValues(true);

    // Creating a XYMultipleSeriesRenderer to customize the whole chart<br />
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setApplyBackgroundColor(true);
    multiRenderer.setBackgroundColor(Color.WHITE);
    multiRenderer.setMarginsColor(Color.WHITE);
    multiRenderer.setLabelsColor(Color.RED);
    multiRenderer.setPanEnabled(true,false);
   /* multiRenderer.setLabelsTextSize(getActivity().getResources().getDimensionPixelSize(R.dimen.sp_9));
    multiRenderer.setLegendTextSize(getActivity().getResources().getDimensionPixelSize(R.dimen.sp_9));
   */ multiRenderer.setChartValuesTextSize(getActivity().getResources().getDimensionPixelSize(R.dimen.sp_9));
    multiRenderer.setXLabelsColor(Color.BLACK);
    multiRenderer.setYLabelsColor(0, Color.BLACK);
     multiRenderer.setShowGridX(true);
    multiRenderer.setShowGridY(true);

    multiRenderer.setGridColor(Color.BLACK);
    multiRenderer.setChartTitle("Plan vs Actual Curve");
    multiRenderer.setXTitle("Months");
    multiRenderer.setYTitle("plan/actual");
    multiRenderer.setZoomButtonsVisible(false);
    multiRenderer.setZoomEnabled(true);
    for (int i = 0; i < monthList.size(); i++) {
        multiRenderer.addXTextLabel(i, monthList.get(i).toString());
    }
    multiRenderer.addSeriesRenderer(actualRenderer);
    multiRenderer.addSeriesRenderer(planRenderer);
    multiRenderer.addSeriesRenderer(cupOneRenderer);
    multiRenderer.addSeriesRenderer(cupTwoRenderer);
    multiRenderer.addSeriesRenderer(cupThreeRenderer);
    Log.e("size of actual log",actualList.size()+"");
    LinearLayout chartContainer = (LinearLayout) mView.findViewById(R.id.chart_container);
    if (monthList.size() > 0) {
        // Getting a reference to LinearLayout of the MainActivity Layout<br />

        chartContainer.removeAllViews();
        //drawing bar chart<br />
        chart = ChartFactory.getLineChartView(getActivity(), xyMultipleSeriesDataset, multiRenderer);
        //adding the view to the linearlayout<br />
        chartContainer.addView(chart);

    } else {
        chartContainer.removeAllViews();
        CustomToast.initToast(getActivity(),"No data available for S-curve");
    }


}