View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.echarts;
23  
24  import java.io.IOException;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  
30  import org.primefaces.renderkit.CoreRenderer;
31  import org.primefaces.util.FacetUtils;
32  import org.primefaces.util.FastStringWriter;
33  import org.primefaces.util.WidgetBuilder;
34  
35  public class EChartRenderer extends CoreRenderer {
36  
37      @Override
38      public void decode(FacesContext context, UIComponent component) {
39          super.decodeBehaviors(context, component);
40      }
41  
42      @Override
43      public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
44          EChart chart = (EChart) component;
45  
46          encodeMarkup(context, chart);
47          encodeScript(context, chart);
48      }
49  
50      protected void encodeMarkup(FacesContext context, EChart chart) throws IOException {
51          ResponseWriter writer = context.getResponseWriter();
52          String clientId = chart.getClientId(context);
53          String style = chart.getStyle();
54          String styleClass = getStyleClassBuilder(context).add("ui-chart").add(chart.getStyleClass()).build();
55  
56          writer.startElement("div", null);
57          writer.writeAttribute("id", clientId, null);
58          writer.writeAttribute("class", styleClass, "styleClass");
59  
60          if (style != null) {
61              writer.writeAttribute("style", style, "style");
62          }
63          else {
64              writer.writeAttribute("style", "width: 600px; height: 400px;", "style");
65          }
66          writer.endElement("div");
67      }
68  
69      protected void encodeScript(FacesContext context, EChart chart) throws IOException {
70          WidgetBuilder wb = getWidgetBuilder(context);
71          wb.init("ExtEChart", chart)
72                      .attr("theme", chart.getTheme(), "default")
73                      .nativeAttr("config", renderConfig(context, chart))
74                      .nativeAttr("extender", chart.getExtender());
75  
76          encodeClientBehaviors(context, chart);
77  
78          wb.finish();
79      }
80  
81      /**
82       * Allow value to be a property or a facet of raw JSON.
83       */
84      protected String renderConfig(FacesContext context, EChart chart) throws IOException {
85          UIComponent facet = chart.getFacet("value");
86          if (FacetUtils.shouldRenderFacet(facet)) {
87              // swap writers
88              ResponseWriter originalWriter = context.getResponseWriter();
89              FastStringWriter fsw = new FastStringWriter();
90              ResponseWriter clonedWriter = originalWriter.cloneWithWriter(fsw);
91              context.setResponseWriter(clonedWriter);
92  
93              // encode the component
94              facet.encodeAll(context);
95  
96              // restore the original writer
97              context.setResponseWriter(originalWriter);
98              return fsw.toString();
99          }
100         else {
101             return chart.getValue();
102         }
103     }
104 
105 }