2017-08-17 67 views
0

我想要更改ListSeries的數量以在按鈕單擊上繪製圖表。從vaadin圖表中刪除系列4.0.0

我使用Vaadin 8.0.1與Vaadin圖4.0.0和Java 8

類包含實施圖表。

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

上面的代碼不會拋出編譯錯誤爲configuration.getSeries()返回不可修改的列表。但它拋出異常,同時從列表中刪除系列(預計由於不可修改的列表)。

任何幫助將不勝感激。我有谷歌整天但沒有運氣。

回答

0

但是,從圖表中刪除系列很簡單。只需從圖表配置中獲取現有系列,並確定您應該刪除哪一個系列。獲取新列表或陣列上的擴展名並將其設置回圖表配置。

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

    } 
});