开发使用achartengine的Android应用程序。我遇到的问题是每当我使用新数据刷新饼图时,图例显示的值都超出了应有的值。
初始加载屏幕:
刷新数据后的屏幕:
这是我的代码:
public class DevicesOfflineActivity extends BaseActionBarActivity
{
private static int[] COLORS = new int[] { 0xFF008000, 0xFFB3B300, Color.RED };
private static String[] NAME_LIST = new String[] { "Online", "Stale", "Offline" };
private List<Integer> devicesOnline;
private int online;
private int stale;
private int offline;
private CategorySeries mSeries = new CategorySeries("");
private DefaultRenderer mRenderer = new DefaultRenderer();
private GraphicalView mChartView;
private void setGraph()
{
mRenderer.setChartTitleTextSize(20);
mRenderer.setShowLabels(false);
mRenderer.setLegendTextSize(30);
mRenderer.setShowTickMarks(true);
mRenderer.setMargins(new int[] { 20, 30, 15, 15 });
mRenderer.setStartAngle(90);
int[] vals = new int[] {online, stale, offline};
for (int i = 0; i < vals.length; i++)
{
mSeries.add(NAME_LIST[i] + " " + vals[i], vals[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
mRenderer.addSeriesRenderer(renderer);
}
if (mChartView != null)
{
mChartView.repaint();
}
}
}
我尝试将mRenderer = new DefaultRenderer();
和mSeries = new CategorySeries("");
添加到setGraph()
的开头,但我的应用程序崩溃了。
我最接近的解决方案是:
for (int i = 0; i < vals.length; i++)
{
if (mSeries.getItemCount() <= i) mSeries.add(NAME_LIST[i] + " " + vals[i], vals[i]);
else mSeries.set(i, NAME_LIST[i] + " " + vals[i], vals[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
if (mRenderer.getSeriesRendererCount() <= i) mRenderer.addSeriesRenderer(renderer);
else mRenderer.addSeriesRenderer(i, renderer);
}
但是当我这样做时,我的传奇是灰色的:
此外,mRenderer.getSeriesRendererCount()
每次刷新时都会增加3,mSeries.getItemCount()
保持为3.我无法找到替换mRenderer中现有项目的方法。
任何熟悉achartengine的人都有很好的解决方案吗?
编辑:
我想我已经取得了一些进展。我得到的项目数量保持在3,但现在传说全是红色。
代码:
for (int i = 0; i < vals.length; i++)
{
if (mSeries.getItemCount() <= i) mSeries.add(NAME_LIST[i] + " " + vals[i], vals[i]);
else mSeries.set(i, NAME_LIST[i] + " " + vals[i], vals[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
if (mRenderer.getSeriesRendererCount() <= i) mRenderer.addSeriesRenderer(renderer);
else
{
mRenderer.removeSeriesRenderer(mRenderer.getSeriesRendererAt(i));
mRenderer.addSeriesRenderer(i, renderer);
}
}
图像:
答案 0 :(得分:2)
好的,我明白了。
mSeries.clear();
for (int i = 0; i < vals.length; i++)
{
mSeries.add(NAME_LIST[i] + " " + vals[i], vals[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
if (mRenderer.getSeriesRendererCount() <= i) mRenderer.addSeriesRenderer(renderer);
else
{
mRenderer.removeSeriesRenderer(mRenderer.getSeriesRendererAt(i));
mRenderer.addSeriesRenderer(i, renderer);
}
}