@Override
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
Components.generateIdIfNotSpecified(component);
ResponseWriter writer = facesContext.getResponseWriter();
Chart chart = (Chart) component;
ChartView view = chart.getChartView();
if (!chart.isRendered() || view == null)
return;
writer.startElement("div", chart);
writeIdAttribute(facesContext, chart);
Rendering.writeComponentClassAttribute(writer, chart);
writer.writeAttribute("style", "width: " + chart.getWidth() + "px;" + " height:" + chart.getHeight() + "px;", "style");
String actionFiledId = chart.getClientId(facesContext) + MapRenderUtilities.ACTION_FIELD_SUFFIX;
Rendering.writeNewLine(writer);
Rendering.renderHiddenField(writer, actionFiledId, null);
Rendering.writeNewLine(writer);
BufferedImage image = chart.make();
final byte[] imageAsByteArray = Rendering.encodeAsPNG(image);
final JfcRenderHints renderHints = chart.getRenderHints();
final ChartRenderingInfo renderingInfo = renderHints.getRenderingInfo();
String mapId = renderHints.getMapId(chart);
String map = MapRenderUtilities.getImageMapExt(chart, mapId, renderingInfo,
new StandardToolTipTagFragmentGenerator(), new StandardURLTagFragmentGenerator());
renderHints.setMap(map);
if (view.getChartPopup() != null) {
encodeChartPopup(facesContext, chart, view, renderingInfo);
}
chart.setImageBytes(imageAsByteArray);
final Integer oldEntityIndex = chart.getEntityIndex();
chart.setEntityIndex(-1);
DynamicImage dynamicImage = new DynamicImage();
ValueExpression ve = new ByteArrayValueExpression(imageAsByteArray);
dynamicImage.setValueExpression("data", ve);
dynamicImage.setId("img");
dynamicImage.setParent(chart);
dynamicImage.setMapId(mapId);
dynamicImage.setMap(map);
dynamicImage.getAttributes().put(DynamicImageRenderer.DEFAULT_STYLE_ATTR, "o_chart");
dynamicImage.setWidth(chart.getWidth());
dynamicImage.setHeight(chart.getHeight());
copyAttributes(dynamicImage, chart, "onclick", "ondblclick", "onmousedown", "onmouseup",
"onmousemove", "onmouseover", "onmouseout");
dynamicImage.setImageType(ImageType.PNG);
dynamicImage.encodeAll(facesContext);
chart.setEntityIndex(oldEntityIndex);
if (map != null) {
Resources.renderJSLinkIfNeeded(facesContext, Resources.utilJsURL(facesContext));
Resources.renderJSLinkIfNeeded(facesContext, Resources.internalURL(facesContext, "chart/chart.js"));
}
encodeScripts(facesContext, chart, dynamicImage);