0

I want to change number of ListSeries to draw chart on button click.

I am using Vaadin 8.0.1 with Vaadin chart 4.0.0 and Java 8.

Class contains implementation of chart.

package v8.v8;


import java.util.List;

import com.vaadin.addon.charts.Chart;
import com.vaadin.addon.charts.model.ChartType;
import com.vaadin.addon.charts.model.Configuration;
import com.vaadin.addon.charts.model.HorizontalAlign;
import com.vaadin.addon.charts.model.LayoutDirection;
import com.vaadin.addon.charts.model.Legend;
import com.vaadin.addon.charts.model.ListSeries;
import com.vaadin.addon.charts.model.PlotOptionsColumn;
import com.vaadin.addon.charts.model.Series;
import com.vaadin.addon.charts.model.Tooltip;
import com.vaadin.addon.charts.model.VerticalAlign;
import com.vaadin.addon.charts.model.XAxis;
import com.vaadin.addon.charts.model.YAxis;
import com.vaadin.addon.charts.model.style.SolidColor;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class GetChartUI {


    protected Component getChart() {
        VerticalLayout vLayout = new VerticalLayout();
        Chart chart = new Chart(ChartType.COLUMN);


        Configuration conf = chart.getConfiguration();

        conf.setTitle("Total fruit consumption, grouped by gender");
        conf.setSubTitle("Source: WorldClimate.com");

        XAxis x = new XAxis();
        x.setCategories("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
        conf.addxAxis(x);

        YAxis y = new YAxis();
        y.setMin(0);
        y.setTitle("Rainfall (mm)");
        conf.addyAxis(y);

        Legend legend = new Legend();
        legend.setLayout(LayoutDirection.VERTICAL);
        legend.setBackgroundColor(new SolidColor("#FFFFFF"));
        legend.setAlign(HorizontalAlign.LEFT);
        legend.setVerticalAlign(VerticalAlign.TOP);
        legend.setX(100);
        legend.setY(70);
        legend.setFloating(true);
        legend.setShadow(true);
        conf.setLegend(legend);

        Tooltip tooltip = new Tooltip();
        tooltip.setFormatter("this.x +': '+ this.y +' mm'");
        conf.setTooltip(tooltip);

        PlotOptionsColumn plot = new PlotOptionsColumn();
        plot.setPointPadding(0.2);
        plot.setBorderWidth(0);

        conf.addSeries(new ListSeries("Tokyo", 49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6,
                54.4));
        conf.addSeries(
                new ListSeries("New York", 83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3));
        conf.addSeries(
                new ListSeries("London", 48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2));
        conf.addSeries(
                new ListSeries("Berlin", 42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1));


        chart.drawChart(conf);
        Button button = new Button("Remove series",new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                // TODO Auto-generated method stub
                Configuration configuration =  chart.getConfiguration();
                List<Series> sc = configuration.getSeries();

                for(Series scq :sc){
                    //Here i want to remove ListSeries contain name called London 
                    if (scq.getName().equals("London"))
                        configuration.getSeries().remove(scq);
                }
                chart.drawChart();
            }
        });
        vLayout.addComponents(chart,button);

        return vLayout;
    }
}

Above code does not throwing compilation error as configuration.getSeries() return unmodifiable List. But it throwing exception while removing series from list(It is expected due to unmodifiable List).

Any help will be appreciate. I have google whole day but no luck.

Uttam Kasundara
  • 237
  • 3
  • 13

1 Answers1

0

However, it simple to remove series from chart. Just get existing series from chart configuration and identify which one you should have to remove. Get reaming on new list or array and set them back to chart configuration.

Button button = new Button("Remove series", new ClickListener() {

    @Override
    public void buttonClick(ClickEvent event) {
        // TODO Auto-generated method stub
        Configuration configuration = chart.getConfiguration();

        List<Series> sc = configuration.getSeries();

        Series[] aList = new Series[sc.size()];
        int i = 0;
        for (Series scq : sc) {
            if (!scq.getName().equals("London"))
                aList[i++] = scq;
        }

        chart.getConfiguration().setSeries(aList);
        chart.drawChart();

    }
});
Uttam Kasundara
  • 237
  • 3
  • 13