Q:

Dynamically updated charts with Achartengine

I will like to have a chart in my application that the user can update when inserting data in an edit text. That is to say I want that when a user inserts data in the edit text and sent them with the send button the chart updates and shows the new data.
I tried inserting the new data into the arraylist in which data are saved and calling the repaint function but it doesn't work and I can't figure why.
This is my code.
Can someone help me, please?
  public class ChartActivity extends Activity {
        private GraphicalView mChartView;

        XYMultipleSeriesRenderer renderer;
        List<double[]> x= new ArrayList<double[]>();
        List<double[]> values = new ArrayList<double[]>();
        String[] titles = new String[] { "spens1" };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.llayout);
            LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout2);

            x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
            // }

            values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13,
                            10 });
            int[] colors = new int[] { Color.BLUE };
            PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };

            renderer = buildRenderer(colors, styles);
            int length = renderer.getSeriesRendererCount();
            for (int i = 0; i < length; i++) {
                ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
                        .setFillPoints(true);
            }
            setChartSettings(renderer, "Ranges", "time",
                    "distance", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY);
            renderer.setXLabels(12);
            renderer.setYLabels(10);
            renderer.setShowGrid(true);
            renderer.setXLabelsAlign(Align.RIGHT);
            renderer.setYLabelsAlign(Align.RIGHT);
            renderer.setZoomButtonsVisible(true);
            renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
            renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });

            if (mChartView == null) {
                Log.d("Oncreate ", "if (mChartView == null)");
                mChartView = ChartFactory.getLineChartView(this, mDataset(titles,
                        x, values), renderer);
                layout.addView(mChartView, new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

            } else {
                mChartView.repaint();
                Log.d("Oncreate ", "if (mChartView != null)");
            }
        }

        private final Handler mHandler = new Handler();
        public void sendMessageChart(View view) {

            mHandler.post(mUpdateUITimerTask);
        }

        private final Runnable mUpdateUITimerTask = new Runnable() {
             public void run() {

                EditText editText = (EditText) findViewById(R.id.edit_message_chart);
                 String message = editText.getText().toString();
                 double new_y = Double.parseDouble(message);
                 double[] x_val = new double[]{x.size()+1};
                 x.add(x.size()+1,x_val);
                double[] y_val = new double[]{new_y};
                 values.add(values.size()+1,y_val);
                mChartView = .repaint();

             }

        };

        private void setChartSettings(XYMultipleSeriesRenderer renderer3,
                String title, String xTitle, String yTitle, double xMin,
                double xMax, double yMin, double yMax, int axesColor,
                int labelsColor) {
            // TODO Auto-generated method stub

            renderer3.setChartTitle(title);
            renderer3.setXTitle(xTitle);
            renderer3.setYTitle(yTitle);
            renderer3.setXAxisMin(xMin);
            renderer3.setXAxisMax(xMax);
            renderer3.setYAxisMin(yMin);
            renderer3.setYAxisMax(yMax);
            renderer3.setAxesColor(axesColor);
            renderer3.setLabelsColor(labelsColor);

        }

        private XYMultipleSeriesRenderer buildRenderer(int[] colors,
                PointStyle[] styles) {
            // TODO Auto-generated method stub
            XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
            setRenderer(renderer, colors, styles);
            return renderer;
        }

        private void setRenderer(XYMultipleSeriesRenderer renderer2, int[] colors,
                PointStyle[] styles) {
            // TODO Auto-generated method stub
            renderer2.setAxisTitleTextSize(16);
            renderer2.setChartTitleTextSize(20);
            renderer2.setLabelsTextSize(15);
            renderer2.setLegendTextSize(15);
            renderer2.setPointSize(5f);
            renderer2.setMargins(new int[] { 20, 30, 15, 20 });
            int length = colors.length;
            for (int i = 0; i < length; i++) {
                XYSeriesRenderer r = new XYSeriesRenderer();
                r.setColor(colors[i]);
                r.setPointStyle(styles[i]);
                renderer2.addSeriesRenderer(r);
            }
        }

        private XYMultipleSeriesDataset mDataset(String[] titles,
                List<double[]> xValues, List<double[]> yValues) {
            // TODO Auto-generated method stub
            XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
            addXYSeries(dataset1, titles, xValues, yValues, 0);
            return dataset1;
        }

        private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
                List<double[]> xValues, List<double[]> yValues, int scale) {
            // TODO Auto-generated method stub

            int length = titles.length;
            for (int i = 0; i < length; i++) {
                XYSeries series = new XYSeries(titles[i], scale);
                double[] xV = xValues.get(i);
                double[] yV = yValues.get(i);
                int seriesLength = xV.length;
                for (int k = 0; k < seriesLength; k++) {
                    series.add(xV[k], yV[k]);
                }
                dataset.addSeries(series);
            }

        }

    }

A:

The new data needs to be added to the XYSeries, not to a random ArrayList. See this code that does exactly what you need.

