I'm trying to print a few chart objects generated inside a <p:repeat></p:repeat>
tag.
I know that the regular <p:printer>
tag does not work with primefaces charts due to the use of HTML5 Canvas...es? Canvii?
Either way, I grabbed this JS function from another SO post, but I can't quite get it to work the way I want.
This is my panel with charts:
<script type="text/javascript">
function exportChart(component) {
//export image
$('#output').empty().append(PF(component).exportAsImage());
//show the dialog
PF('dlg').show();
}
</script>
<p:dialog widgetVar="dlg" showEffect="fade" modal="false" header="Druckbares Diagramm als Bild" resizable="true">
<p:outputPanel id="output" layout="block" style="width:1000px;height:300px"/>
<p:commandButton onClick="javascript:PF('dlg').hide()" value = "Schließen"/>
</p:dialog>
<p:panel id="charts">
<p:repeat value="#{AttributeChartsController.kacModel.chartWrappers}" var="wrapper" style="height: #{wrapper.drawHeight}" >
<p:chart type="bar" model="#{wrapper.chart}" style="height: #{wrapper.drawHeight}" widgetVar="#{wrapper.widgetVarIdStr}"/>
<p:commandLink id="lnk#{wrapper.widgetVarIdStr}" onclick="print(#{wrapper.widgetVarIdStr})">Print</p:commandLink>
</p:repeat>
</p:panel>
I set my widgetVar
to widgetVarIdStr
which is generated for each chart like this, kind of:
//these are values retrieved from a database
long widgetVarId = 0;
for(Entry<String, HashMap<String, String>> entry : keyAttribs.entrySet()) {
this.chartWrappers.add(createChartWrapper(entry.getKey(), entry.getValue()));
widgetVarId++;
}
private ChartWrapper createChartWrapper(String attribName, HashMap<String, String> keyValue) {
//... create Chart, ChartSeries, enter values, etc
wrapper.setWidgetVarId(widgetVarId);
return wrapper;
}
The result is this, however:
The chart works, the print button is there and has the right value, but the widgetVar doesn't seem to get set properly within the
<p:repeat>
element. The widgetVar
property is also nowhere to be found in the final generated source of the page, is that normal?
Here is my chartWrapper, there's probably a more elegant way to do what I want to do here but I'll stick with it:
public class ChartWrapper {
private HorizontalBarChartModel chart;
private long drawHeight;
private long widgetVarId;
public HorizontalBarChartModel getChart() {
return chart;
}
public void setChart(HorizontalBarChartModel chart) {
this.chart = chart;
}
public long getDrawHeight() {
return this.drawHeight();
}
public void setDrawHeight(long drawHeight) {
this.drawHeight = drawHeight;
}
public String getDrawHeightStr() {
return drawHeight+"px";
}
public long getWidgetVarId() {
return widgetVarId;
}
public void setWidgetVarId(long widgetVarId) {
this.widgetVarId = widgetVarId;
}
public String getWidgetVarIdStr() {
return "attrChart"+widgetVarId;
}
}