我想更改ListSeries的数量,以便在点击按钮时绘制图表。
我正在使用Vaadin 8.0.1和Vaadin图表4.0.0和Java 8.
Class包含图表的实现。
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()返回不可修改的List。但它从列表中删除系列时抛出异常(预计由于不可修改的List)。
任何帮助都将不胜感激。我整天都在谷歌但没有运气。
答案 0 :(得分: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();
}
});