我喜欢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);
}
}
这是一张图片垃圾在按钮的底部后面。任何帮助将不胜感激。
答案 0 :(得分:0)
我找到了一种方法来修复它 - 将按钮包含在relativelayout中,并给出linealayout,其中图表的底部边距为50dp。
答案 1 :(得分:0)
您可以尝试进行以下调用。
mRenderer.setInScroll(true);