package demo.bar;
import org.zkoss.chart.Charts;
import org.zkoss.chart.Legend;
import org.zkoss.chart.StackLabels;
import org.zkoss.chart.Theme;
import org.zkoss.chart.YAxis;
import org.zkoss.chart.plotOptions.ColumnPlotOptions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;
public class ColumnStackedComposer extends SelectorComposer<Window> {
@Wire
Charts chart;
public void doAfterCompose(Window comp) throws Exception {
super.doAfterCompose(comp);
chart.setModel(ColumnStackedData.getCategoryModel());
YAxis yAxis = chart.getYAxis();
yAxis.setMin(0);
yAxis.getTitle().setText("Total fruit consumption");
StackLabels stackLabels = yAxis.getStackLabels();
stackLabels.setEnabled(true);
String style = "fontWeight: bold;";
if (!isThemeStyleSet("textColor")) {
style += "color: gray;";
}
stackLabels.setStyle(style);
Legend legend = chart.getLegend();
legend.setAlign("right");
legend.setX(-30);
legend.setVerticalAlign("top");
legend.setY(25);
legend.setFloating(true);
if (!isThemeStyleSet("background2")) {
legend.setBackgroundColor("white");
}
legend.setBorderColor("#CCC");
legend.setBorderWidth(1);
legend.setShadow(false);
chart.getTooltip().setHeaderFormat("<b>{point.x}</b><br/>");
chart.getTooltip().setPointFormat("{series.name}: {point.y}<br/>Total: {point.stackTotal}");
ColumnPlotOptions plotOptions = chart.getPlotOptions().getColumn();
plotOptions.setStacking("normal");
plotOptions.getDataLabels().setEnabled(true);
if (!isThemeStyleSet("dataLabelsColor")) {
plotOptions.getDataLabels().setColor("white");
}
}
private boolean isThemeStyleSet(String style) {
Theme theme = chart.getTheme();
return theme != null && theme.toString().contains(style);
}
}
package demo.bar;
import org.zkoss.chart.model.CategoryModel;
import org.zkoss.chart.model.DefaultCategoryModel;
public class ColumnStackedData {
private static CategoryModel model;
static {
model = new DefaultCategoryModel();
model.setValue("John", "Apples", 5);
model.setValue("John", "Oranges", 3);
model.setValue("John", "Pears", 4);
model.setValue("John", "Grapes", 7);
model.setValue("John", "Bananas", 2);
model.setValue("Jane", "Apples", 2);
model.setValue("Jane", "Oranges", 2);
model.setValue("Jane", "Pears", 3);
model.setValue("Jane", "Grapes", 2);
model.setValue("Jane", "Bananas", 1);
model.setValue("Joe", "Apples", 3);
model.setValue("Joe", "Oranges", 4);
model.setValue("Joe", "Pears", 4);
model.setValue("Joe", "Grapes", 2);
model.setValue("Joe", "Bananas", 5);
}
public static CategoryModel getCategoryModel() {
return model;
}
}