我的目标是创建一个图表,根据用户操作隐藏和显示系列。我想通过首先清除图形和数据集以及渲染器,然后重新插入新值来实现这一点。就像Arraylists的.clear()
方法一样,我想知道是否为XYMultipleSeriesRenderer构建了某种类似的功能。我注意到XYMultipleSeriesDataset缺乏类似的功能,但我能够解决这个问题(我想我还不确定)
for(int i = 0; i < dataset.getSeriesCount(); i++) {
dataset.removeSeries(i);
}
幸运的是,XYMultipleSeriesRenderer似乎有一个getrenderercount类型的方法,但不是一个取ints
的删除方法。我只看到一个删除函数,它不会ints
,只是实际的系列渲染器名称。
如果有帮助,可以使用以下代码:
private void excecuteGraph() {
// add series to the complete dataset
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
switch (graphCase ){
case -1:
dataset.addSeries(series0);
dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
// create the individual renderers, customize settings for each
XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setColor(Color.BLUE);
renderer1.setLineWidth(3);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.DKGRAY);
renderer2.setLineWidth(3);
XYSeriesRenderer renderer3 = new XYSeriesRenderer();
renderer3.setColor(Color.RED);
renderer3.setLineWidth(3);
XYSeriesRenderer renderer4 = new XYSeriesRenderer();
renderer4.setColor(Color.WHITE);
renderer4.setLineWidth(3);
// add renderers to the complete multirenderer, customize settings for it
mrenderer.addSeriesRenderer(renderer1);
mrenderer.addSeriesRenderer(renderer2);
mrenderer.addSeriesRenderer(renderer3);
mrenderer.addSeriesRenderer(renderer4);
break;
case 0:
for(int i = 0; i < dataset.getSeriesCount(); i++) {
dataset.removeSeries(i);
}
dataset.addSeries(series0);
XYSeriesRenderer renderer00 = new XYSeriesRenderer();
renderer00.setColor(Color.BLUE);
renderer00.setLineWidth(3);
mrenderer.addSeriesRenderer(renderer00);
break;
case 1:
for(int i = 0; i < dataset.getSeriesCount(); i++) {
dataset.removeSeries(i);
Log.d("testingGraph", dataset.toString());
}
dataset.addSeries(series1);
XYSeriesRenderer renderer10 = new XYSeriesRenderer();
renderer10.setColor(Color.DKGRAY);
renderer10.setLineWidth(3);
mrenderer.addSeriesRenderer(renderer10);
break;
case 2:
for(int i = 0; i < dataset.getSeriesCount(); i++) {
dataset.removeSeries(i);
}
dataset.addSeries(series2);
XYSeriesRenderer renderer20 = new XYSeriesRenderer();
renderer20.setColor(Color.RED);
renderer20.setLineWidth(3);
mrenderer.addSeriesRenderer(renderer20);
case 3:
for(int i = 0; i < dataset.getSeriesCount(); i++) {
dataset.removeSeries(i);
}
dataset.addSeries(series3);
XYSeriesRenderer renderer30 = new XYSeriesRenderer();
renderer30.setColor(Color.WHITE);
renderer30.setLineWidth(3);
mrenderer.addSeriesRenderer(renderer30);
}
mrenderer.setYTitle("Accuracy (%)");
mrenderer.setYAxisMin(0);
mrenderer.setYAxisMax(100);
mrenderer.setApplyBackgroundColor(true);
mrenderer.setBackgroundColor(Color.TRANSPARENT);
// Get the chart view result, add it to the linear layout
GraphicalView mChartView = ChartFactory.getTimeChartView(this, dataset,
mrenderer, "MM/dd/yyyy");
LinearLayout graphLayout = (LinearLayout) findViewById(R.id.layout_graph);
graphLayout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
if (mChartView !=null) {
mChartView.repaint();
}
}
答案 0 :(得分:5)
我不确定我完全理解你的问题,但我可以给你一些线索。
删除XYMultipleSeriesRenderer
中的所有渲染器:
renderer.removeAllRenderers();
删除XYMultipleSeriesDataset
中的所有系列:
int size = dataset.getSeriesCount();
for (int i = 0; i < size; i++) {
// always remove the first element because once you remove one element,
// the size of the list becomes size - 1 and so on
dataset.removeSeries(0);
}
但是,如果你想要隐藏一个系列,那么就删除那个系列,而不是全部。