<?xml version="1.0" encoding="UTF-8"?>
<window apply="demo.heatandtree.TreemapLargeComposer">
<charts id="chart" type="treemap"
title="Global Mortality Rate 2012, per 100 000 population"
subtitle="Click points to drill down. Source: <a href="http://apps.who.int/gho/data/node.main.12?lang=en">WHO</a>."/>
</window>
package demo.heatandtree;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zkoss.chart.Charts;
import org.zkoss.chart.Level;
import org.zkoss.chart.Point;
import org.zkoss.chart.Series;
import org.zkoss.chart.demo.WebResourceLoader;
import org.zkoss.chart.plotOptions.TreemapPlotOptions;
import org.zkoss.json.JSONObject;
import org.zkoss.json.JSONValue;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;
public class TreemapLargeComposer extends SelectorComposer<Window> {
@Wire
Charts chart;
public void doAfterCompose(Window comp) throws Exception {
super.doAfterCompose(comp);
chart.getTitle().setAlign("left");
chart.getSubtitle().setAlign("left");
Series series = chart.getSeries();
series.setName("Regions");
series.setType("treemap");
TreemapPlotOptions treemapPlotOptions = new TreemapPlotOptions();
treemapPlotOptions.setLayoutAlgorithm("squarified");
treemapPlotOptions.setAllowTraversingTree(true);
treemapPlotOptions.setAnimationLimit(1000);
treemapPlotOptions.getDataLabels().setEnabled(false);
Level level = new Level();
level.setLevel(1);
level.getDataLabels().setEnabled(true);
level.setBorderWidth(3);
level.setLevelIsConstant(false);
Level level1 = new Level();
level1.setLevel(1);
level1.getDataLabels().setStyle("fontSize: 14px");
treemapPlotOptions.setLevels(level, level1);
series.setPlotOptions(treemapPlotOptions);
series.getAccessibility().setExposeAsGroupOnly(true);
series.setData(getData());
}
private Map<String, String> causes = new HashMap<String, String>();
{
causes.put("Communicable & other Group I", "Communicable diseases");
causes.put("Noncommunicable diseases", "Non-communicable diseases");
causes.put("Injuries", "Injuries");
}
private static JSONObject data;
static {
data = (JSONObject) JSONValue.parse(WebResourceLoader.readData("world-mortality.json"));
}
private Point[] getData() {
List<Point> points = new ArrayList<Point>();
int regionId = 0;
for (Object regionName : data.keySet()) {
JSONObject region = (JSONObject) data.get(regionName);
double regionSum = 0.0;
Point regionPt = new Point();
regionPt.setId("id_" + regionId);
regionPt.setName((String) regionName);
regionPt.setColor(chart.getColors().get(regionId));
int countryId = 0;
for (Object countryName : region.keySet()) {
JSONObject country = (JSONObject) region.get(countryName);
Point countryPt = new Point();
countryPt.setId(regionPt.getId() + "_" + countryId);
countryPt.setName((String) countryName);
countryPt.setParent(regionPt.getId());
points.add(countryPt);
int causeId = 0;
for (Object causeName : country.keySet()) {
Number cause = Optional.ofNullable((Number) country.get(causeName)).orElse(0);
Point causePt = new Point();
causePt.setId(countryPt.getId() + "_" + causeId);
causePt.setName(causes.get(causeName));
causePt.setParent(countryPt.getId());
causePt.setValue(Math.round(cause.doubleValue()));
regionSum += causePt.getValue().doubleValue();
points.add(causePt);
causeId += 1;
}
countryId += 1;
}
regionPt.setValue(Math.round(regionSum / countryId));
points.add(regionPt);
regionId += 1;
}
return points.toArray(new Point[0]);
}
}