在android中使用AchartEngine动态绘制线图时的问题

时间:2015-02-13 07:09:20

标签: android achartengine

这是我的课程的样子

import org.achartengine.ChartFactory;
    import org.achartengine.GraphicalView;
    import org.achartengine.model.XYMultipleSeriesDataset;
    import org.achartengine.model.XYSeries;
    import org.achartengine.renderer.XYMultipleSeriesRenderer;
    import org.achartengine.renderer.XYSeriesRenderer;
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.Paint.Align;

    public class ChartController {
        private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
        private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        public GraphicalView mChartView;
        private Context context;
        public boolean initialised;

        public static double xMin = 0;
        public static double xMax = 50;
        public static double yMin = 0;
        public static double yMax = 100;

        private static XYSeries series_1;

        public GraphicalView getmChartView() {
            return mChartView;
        }

        public PPSChartController(Context context) {
            super();
            initialised = false;
            this.context = context;

        }

        public void init(int argbColor, String name, XYSeries _series_1) {

            if (!FragmentDistance.Possible && ChartRecorder.distanceCovered > 0) {

                xMax = 50 * ChartRecorder.result;

            }
            else if (FragmentDistance.Possible) {
                xMax = ChartRecorder.distance;

            }
            series_1 = _series_1;
            mDataset.addSeries(0, series_1);

            int[] margins = { 0, 0, 0, 0 };

            mRenderer.setApplyBackgroundColor(true);
            mRenderer.setBackgroundColor(Color.TRANSPARENT);
            mRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
            mRenderer.setLegendTextSize(50);
            mRenderer.setYLabelsAlign(Align.LEFT);
            mRenderer.setZoomButtonsVisible(false);
            mRenderer.setPanEnabled(false, false);
            mRenderer.setAntialiasing(true);
            mRenderer.setFitLegend(false);
            mRenderer.setShowLegend(false);
            mRenderer.setMargins(margins);
            mRenderer.setYAxisMin(0);
            mRenderer.setXAxisMin(0);
            mRenderer.setXAxisMax(xMax);
            mRenderer.setYAxisMax(yMax + 10);
            mRenderer.setShowGridY(false);
            mRenderer.setShowGridX(true);
            mRenderer.setGridColor(Color.GRAY);
            mRenderer.setInScroll(true);

            XYSeriesRenderer rendererSignal = new XYSeriesRenderer();
            rendererSignal.setColor(Color.CYAN);
            rendererSignal.setLineWidth(4);

            mRenderer.addSeriesRenderer(0, rendererSignal);

            mChartView = ChartFactory.getLineChartView(context, mDataset, mRenderer);
            mChartView.setClickable(true);
            initialised = true;

        }

        public void refresh() {
            mChartView.repaint();
        }

        public void clear() {
            series_1.clear();
        }

    }

ChartRecorder类

import org.achartengine.model.XYSeries;

import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;


public class ChartRecorder {

    public ChartController chartController;
    public static XYSeries sig_socCurve;
    public static int distance;
    int count = 0;
    public static int result;
    public static int distanceCovered;
    int soc;

    public void initChart() {
        if (chartController == null || !chartController.initialised) {
            chartController = new ChartController(ActivityMain.appContext);
            chartController.init(1, "PPS", sig_socCurve);
        }

    }

    public void resetChart() {
        if (chartController != null) {
            chartController.clear();
        }
    }

    public ChartRecorder() {

        chartController = new ChartController(ActivityMain.appContext);
        sig_socCurve = new XYSeries("SOCCurve");
    }

    public void handleSignal() {

        if (SomeCondition)) {

        }
        if (someCondition2)) {

            distance = 100;


            **if (someCondition) {
                if (againSomeCondition) {
                    result = someResult;
                    // here i am calling the init() method from ChartController 
                    chartController.init(1, "PPS", sig_socCurve);
                }
            } else if (someCondition) {
            // here i am calling the init() method from ChartController
                chartController.init(1, "PPS", sig_socCurve);
                count++;
            }**

            sig_socCurve.add(distanceCovered, soc);

        }

        if (ppsRecord != null) {
            // this calling every time so ther not a issue with repaint or refresh
            ppsRecord.chartController.refresh();
        }

    }

}

这是片段:

Here i have a fragment class 
 where based on the some  condition i am calling handleSignal() 
 for example


in OnCreateView() method i am calling 
initChart()  from ChartRecorder class

 if(someCondition) {
 handleSignal() from ChartRecorder class

 }

当我在ChartController类

中评论以下代码时,动态图表正在工作
**if (someCondition) {
                if (againSomeCondition) {
                    result = someResult;
                    // here i am calling the init() method from ChartController 
                    chartController.init(1, "PPS", sig_socCurve);
                }
            } else if (someCondition) {
            // here i am calling the init() method from ChartController
                chartController.init(1, "PPS", sig_socCurve);
                count++;
            }**

为什么它不发生任何线索......请提出一些想法

1 个答案:

答案 0 :(得分:0)

是的我得到了问题的解决方案

这里在方法init()里面的ChartController.java类我设置了xaxis值

 if (!FragmentDistance.Possible && ChartRecorder.distanceCovered > 0) {

                xMax = 50 * ChartRecorder.result;

            }
            else if (FragmentDistance.Possible) {
                xMax = ChartRecorder.distance;

            }

同样的事情,当我在refresh()方法内部工作时,它工作。但不知道为什么它工作在{(1}}我不在init()设置之后调用repaint()