2

我的目标是创建一个图表,该图表将根据用户操作隐藏和显示一系列。我想通过首先清除图形和数据集以及渲染器然后重新插入新值来做到这一点。很像.clear()Arraylists 的方法,我想知道是否为 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();
    }

}
4

1 回答 1

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);
}

但是,如果您想隐藏一个系列,则只需删除该系列而不是全部。

于 2012-10-28T13:56:22.637 回答