Android GraphView库 - 添加了GraphView.getSecondScale()的系列.addSeries(系列)无法删除

时间:2015-07-26 13:11:36

标签: android android-graphview

我的图表中有三个系列。系列1和2使用左侧刻度,系列3使用第二刻度通过添加

CurrentPlot.getSecondScale().addSeries(sensor3Series);

用户可以选择查看所有三个系列或仅查看其中一些系列。

可以使用

删除系列1和2
currentPlot.removeSeries(sensor1Series);
currentPlot.removeSeries(sensor2Series);

但系列3无法删除。 我试过了

currentPlot.removeAllSeries();
currentPlot.removeSeries(sensor3Series);

两者都不会使用第二个刻度删除系列。

请建议如何删除

代码提取:

初始化(简化):

private void initChart(boolean isContinuous) {

        // find the temperature levels plot in the layout
        currentPlot = (GraphView) findViewById(R.id.graph);
        // setup and format sensor 1 data series
        sensor1Series = new LineGraphSeries<>();
        // setup and format sensor 2 data series
        sensor2Series = new LineGraphSeries<>();
        // setup and format sensor 3 data series
        sensor3Series = new LineGraphSeries<>();

        currentPlot.getGridLabelRenderer().setNumVerticalLabels(10);
        currentPlot.getGridLabelRenderer().setNumHorizontalLabels(5);

        sensor1Series.setColor(Color.YELLOW);
        sensor2Series.setColor(Color.RED);
        sensor3Series.setColor(Color.GREEN);
        currentPlot.getGridLabelRenderer().setVerticalAxisTitle("Watt");

        // SKIPPED code to add data to the 3 series with appendData()

        currentPlot.setTitle(dayToShow);

        minMaxVal = getMinMax();

        // SKIPPED set min and max bounds of both scales to manual and set the values

        // SKIPPED add setOnDataPointTapListeners to all three series with setOnDataPointTapListener()

        currentPlot.addSeries(sensor2Series);
        currentPlot.getSecondScale().addSeries(sensor3Series);
        currentPlot.addSeries(sensor1Series);
    }

在onClick中删除所选系列(通过CheckBox)的代码:

    case R.id.cb_solar:
        CheckBox cbSolar = (CheckBox)findViewById(R.id.cb_solar);
        if (cbSolar.isChecked()) {
            currentPlot.addSeries(sensor1Series);
            showSeries1 = true;
        } else {
            currentPlot.removeSeries(sensor1Series);
            showSeries1 = false;
        }
        break;
    case R.id.cb_cons:
        CheckBox cbCons = (CheckBox)findViewById(R.id.cb_cons);
        if (cbCons.isChecked()) {
            currentPlot.addSeries(sensor2Series);
            showSeries2 = true;
        } else {
            currentPlot.removeSeries(sensor2Series);
            showSeries2 = false;
        }
        break;
    case R.id.cb_light:
        CheckBox cbLight = (CheckBox)findViewById(R.id.cb_light);
        if (cbLight.isChecked()) {
            currentPlot.getSecondScale().addSeries(sensor3Series);
            showSeries3 = true;
        } else {
            currentPlot.removeAllSeries();
                    //removeSeries(sensor3Series);
            showSeries3 = false;
        }
        break;

编辑: 解决问题 - 将系列的颜色设置为Color.TRANSPARENT

case R.id.cb_light:
                CheckBox cbLight = (CheckBox)findViewById(R.id.cb_light);
                if (cbLight.isChecked()) {
                    currentPlot.getSecondScale().addSeries(sensor3Series);
                    sensor3Series.setColor(Color.GREEN);
                    showSeries3 = true;
                } else {
                    currentPlot.removeSeries(sensor3Series);
                    sensor3Series.setColor(Color.TRANSPARENT);
                    showSeries3 = false;
                }
                break;

这使得第三个系列不可见,但右侧的第二个刻度仍然可见。

1 个答案:

答案 0 :(得分:-1)

尝试:

graph.getSecondScale().getSeries().clear();