我正在使用achartengine-1.1.0.jar。我的代码生成折线图,大多数时候都有效(见下文)
但有时会出现一个空白图表,其中y轴和x轴可见(从图像中可以看出)。这种情况相当随机。
我尝试过以各种方式复制问题,但仍无法找到根本原因。直到现在它似乎完全随机。
以下是负责图表的代码段:
String seriesTitle = "Alpha";
String seriesTitleB = "Beta";
String seriesTitleD = "Delta";
// create a new series of data
XYSeries series = new XYSeries(seriesTitle);
XYSeries seriesB = new XYSeries(seriesTitleB);
XYSeries seriesD = new XYSeries(seriesTitleD);
float x =1;
int c =0;
do{
String s= alpha.getItems().get(c).toString();
series.add((double)x, Double.parseDouble(s.substring(1, s.length()-1)));
s= beta.getItems().get(c).toString();
seriesB.add((double)x, Double.parseDouble(s.substring(1, s.length()-1)));
s= delta.getItems().get(c).toString();
seriesD.add((double)x, Double.parseDouble(s.substring(1, s.length()-1)));
x++;
c++;
}while(alpha.size() > c);
mDataset.addSeries(series);
mDataset.addSeries(seriesB);
mDataset.addSeries(seriesD);
// create a new renderer for the new series
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.argb(255, 238 , 89, 125));
// create a new renderer for the new series
XYSeriesRenderer rendererB = new XYSeriesRenderer();
rendererB.setColor(Color.argb(255, 29, 158, 225));
XYSeriesRenderer rendererD = new XYSeriesRenderer();
rendererD.setColor(Color.argb(255, 15, 122, 131));
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(rendererB);
mRenderer.addSeriesRenderer(rendererD);
mChartView.repaint();
知道可能导致这种不稳定行为的原因吗?
编辑:@keshav这是我在我的视图中添加mChartView的地方
@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chartProgress);
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
// enable the chart click events
mRenderer.setSelectableBuffer(10);
mChartView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// handle the click event on the chart
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(ProgressChart.this, "No chart element", Toast.LENGTH_SHORT).show();
} else {
// display information of the clicked point
Toast.makeText(
ProgressChart.this,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y="
+ seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
boolean enabled = mDataset.getSeriesCount() > 0;
} else {
mChartView.repaint();
}
}
编辑:以前我没有提到过两个线性布局。一个布局包含图表(首先隐藏)。第二个布局包含日期选择器。当我触摸到日期选择器中的空位时,问题就出现了。线性布局然后当我按下按钮来改变线性布局时,空白图表就会发生。如果我只触摸日期选择器而不是空白空间,那么图表就可以了。