Question

I like the achartengine charting library for Android, but I keep getting this problem. I want the activity to redraw a chart for different data sets from my db (going back different amounts of time) on a button click. I have three buttons in the activity along with the chart (below the listview that contains the chart). Everything works except some extra garbage characters are drawn behind a button after it is pressed. It doesn't happen on the first press of a button, but on the press of a button (doesn't matter which) the garbage appears.

My Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/chart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#000000"/>

    <Button
        android:id="@+id/oneMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="25dp"
        android:text="@string/OneMonth" />

    <Button
        android:id="@+id/twoMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/TwoMonth" />

    <Button
        android:id="@+id/threeMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="25dp"
        android:text="@string/ThreeMonth" />

</RelativeLayout>

My activity:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Plot extends Activity{

 /** Called when the activity is first created. */
    private XYMultipleSeriesDataset mDataset;
    private XYMultipleSeriesRenderer mRenderer;
    List<double[]> values = new ArrayList<double[]>();
    private GraphicalView mChartView;
    private TimeSeries time_series;
    private final String RANGE = "range";
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.plot);

        layout = (LinearLayout)findViewById(R.id.chart);

        final Button set30 = (Button)findViewById(R.id.oneMonth);
        final Button set60 = (Button)findViewById(R.id.twoMonth);
        final Button set90 = (Button)findViewById(R.id.threeMonth);

        set30.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                setUpChart(-30);

            }});

        set60.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                setUpChart(-60);

            }});



        set90.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                setUpChart(-90);

            }});

        }



    @Override
    protected void onResume() {

        super.onResume();
         setUpChart(-30);
    }

    private void fillData(int x) {
        DataBaseHelper helper = new DataBaseHelper(this.getApplicationContext());
        helper.open(DataBaseHelper.READABLE);

        Cursor cursor = helper.getDaysSoManyDaysInThePast(x);

        if(cursor.getCount() != 0){
            DateFormat formatter ; 
             Date date = null; 
              formatter = new SimpleDateFormat(DataBaseHelper.DATE_FORMAT);
             String s;

             int dateCol = cursor.getColumnIndex("date");
             int caffCol = cursor.getColumnIndex("sum(mgCaffeine)");

            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){

                s = cursor.getString(dateCol);
                try {
                    date = (Date)formatter.parse(s);

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }   
                int caff = cursor.getInt(caffCol);
                time_series.add(date, caff);            
            }


        }else{
            Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
        }

       helper.close();
    }

        private void setUpChart(int x){

            mDataset = new XYMultipleSeriesDataset();
            mRenderer = new XYMultipleSeriesRenderer();
            mRenderer.setAxisTitleTextSize(16);
            mRenderer.setChartTitleTextSize(20);
            mRenderer.setLabelsTextSize(15);
            mRenderer.setLegendTextSize(20);
            mRenderer.setPointSize(3f);

            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(Color.GREEN);
            r.setPointStyle(PointStyle.CIRCLE);
            r.setFillPoints(true);
            mRenderer.addSeriesRenderer(r);
            mRenderer.setClickEnabled(true);
            mRenderer.setSelectableBuffer(20);
            mRenderer.setPanEnabled(true);

            time_series = new TimeSeries("test");
            mDataset.addSeries(time_series);

            fillData(x);

            mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,
                DataBaseHelper.DATE_FORMAT);



        layout.removeAllViews();
        layout.addView(mChartView);

    }
}

Here's a picture The garbage is behind the bottom part of the button. Any help would be appreciated.

Was it helpful?

Solution

I found a way to fix it --- enclose the buttons in a relativelayout and give the linealayout that has the chart a bottom margin of 50dp.

OTHER TIPS

You can try to do the following call.

mRenderer.setInScroll(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top