package zk.support; import org.zkoss.bind.BindUtils; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.GlobalCommand; import org.zkoss.bind.annotation.Init; import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.chart.Charts; import org.zkoss.chart.plotOptions.AreaPlotOptions; import org.zkoss.zul.CategoryModel; import org.zkoss.zul.SimpleCategoryModel; /** * Date: 17.03.14 */ public class ChartsTestVM { private CategoryModel model = null; private String dataParam = "series"; private char seriesPrefix = 'A'; private String categoryPrefix = "init"; private int numSeries = 3; private int numCategories = 3; private Charts chart; private boolean updateColors; @Init public void init() { categoryPrefix = "init"; replaceModel(); } @Command("configurePlot") public void doConfigurePlot(@BindingParam("areaPlotOptions") AreaPlotOptions plotOptions, @BindingParam("chart") Charts chart) { this.chart = chart; chart.getXAxis().setTickmarkPlacement("on"); chart.getYAxis().setTitle("Incidents"); chart.getYAxis().getLabels().setFormat("{value}"); chart.getTooltip().setShared(true); plotOptions.setStacking("normal"); plotOptions.setLineColor("#B80000"); plotOptions.setLineWidth(1); plotOptions.getMarker().setLineWidth(1); plotOptions.getMarker().setLineColor("#666666"); setColors(); } @Command("replaceModelWorkaround") //@NotifyChange({"model"}) public void doReplaceModelWorkaround() { categoryPrefix = "refix"; replaceModel(); chart.setModel(model); // need to set the model before changing the colors setColors(); } @Command("replaceModel") @NotifyChange({"model"}) public void doReplaceModel() { categoryPrefix = "repl"; replaceModel(); setColors(); } private void replaceModel() { model = new SimpleCategoryModel(); retrieveDataFromDB(dataParam); } @Command("updateModel") public void doUpdateModel() { model.clear(); categoryPrefix = "updt"; numSeries++; numCategories++; retrieveDataFromDB(dataParam); setColors(); } @GlobalCommand("setColors") public void setColors() { chart.getSeries(0).setColor("#FF0000"); chart.getSeries(1).setColor("#00FF00"); } private void retrieveDataFromDB(String dataParam) { //simulates model population int numCategories = (int)(Math.random() * 5) + 3; int numSeries = (int)(Math.random() * 5) + 3; numCategories = 20; numSeries = 2; for(int seriesIndex = 0; seriesIndex < numSeries; seriesIndex++) { for(int index = 0; index < numCategories; index++) { double value; // if(seriesIndex == 0 && (index < 5 || index > 15)) { // make the other series "incomplete" if(seriesIndex == 1 && (index < 5 || index > 15)) { // create an incomplete series continue; // value = 0.0; } else { //random values // value = Double.valueOf(Math.random() * 100); //deterministic values value = Math.sin(index / 6.0 + seriesIndex) * 30 + 55; } model.setValue(seriesPrefix + dataParam + " " + (seriesIndex + 1), categoryPrefix + "/" + index, value); } } //seriesPrefix++; } public CategoryModel getModel() { return model; } }