我喜欢 Android 的 achartengine 图表库,但我一直遇到这个问题。我希望活动在单击按钮时从我的数据库中重绘不同数据集的图表(返回不同的时间量)。我在活动中有三个按钮以及图表(在包含图表的列表视图下方)。一切正常,除了在按下按钮后会在按钮后面绘制一些额外的垃圾字符。它不会在第一次按下按钮时发生,而是在按下按钮时(不管是哪个按钮)都会出现垃圾。
我的布局:
<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>
我的活动:
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);
}
}
这是一张图片垃圾在按钮底部的后面。任何帮助,将不胜感激。