package zk.support; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.chart.Charts; import org.zkoss.chart.model.CategoryModel; import org.zkoss.chart.model.DefaultCategoryModel; import org.zkoss.chart.plotOptions.AreaPlotOptions; /** * 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; @Init public void init() { doReplaceModel(); } @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("replaceModel") @NotifyChange({"model"}) public void doReplaceModel() { model = new DefaultCategoryModel(); categoryPrefix = "init"; numSeries = 3; numCategories = 3; retrieveDataFromDB(dataParam); // setColors(); } @Command("updateModel") public void doUpdateModel() { model.clear(); categoryPrefix = "updt"; numSeries++; numCategories++; retrieveDataFromDB(dataParam); setColors(); } private void setColors() { chart.getSeries(0).setColor("#FF0000"); chart.getSeries(1).setColor("#00FF00"); chart.getSeries(2).setColor("#FF00FF"); } 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; } }