Printer not working for chart objects

UI Components for JSF
Post Reply
cacophonic7
Posts: 5
Joined: 09 May 2012, 23:11

09 May 2012, 23:21

Hello all,
I apologize in advance if this has been answered somewhere else before, but I have been unable to find an answer on my own. I am having trouble using the printer object to print chart components. I worked up a simple line chart demo based off the showcase code, and added a CommandButton and printer behavior to print the LineChart component. However, the print out only contains the legend and value ticks along the chart axes. None of the actual data series lines, points, nor the grid backing of the chart show up in the printout.

Is this a known bug, or am I just missing something? If it is a known bug, will it be fixed? Is there a work around for printing chart data?

Thank you, and my code follows:

Here is my chart.xhtml

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml"  
    xmlns:h="http://java.sun.com/jsf/html"  
    xmlns:f="http://java.sun.com/jsf/core"  
    xmlns:p="http://primefaces.org/ui"> 
  <h:head>
    <title>Primefaces Chart test</title>
  </h:head>
  
  <h:body>
	  <f:loadBundle basename="com.dashboard.MessageBundle" var="bundle"/>
   	  <f:view contentType="text/html">
   	    <h:commandButton value="Print" type="button" icon="ui-icon-print">  
        	<p:printer target="linear" />  
    	</h:commandButton>
     
		<p:lineChart id="linear" value="#{chartBean.model}" legendPosition="e" title="Linear Chart" minY="0" maxY="10" style="height:300px"/>   	
	</f:view>
  </h:body>
</html>
Here is my faces-config.xml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 <managed-bean>
  <managed-bean-name>chartBean</managed-bean-name>
  <managed-bean-class>com.dashboard.controller.LineChartControllerBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/chart.xhtml</from-view-id>
 </navigation-rule>
</faces-config>
Here are the contents of LineChartControllerBean.java class:

Code: Select all

package com.dashboard.controller;

import java.util.ArrayList;
import java.util.List;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.LineChartSeries;

public class LineChartControllerBean extends ChartControllerBean {

   protected List<LineChartSeries> seriesList;
   
   public LineChartControllerBean() {
      model = new CartesianChartModel();
      initializeSeriesList();
      initializeModel();
   }
   
   public List<LineChartSeries> getSeriesList() {
      if(seriesList == null) {
         seriesList = new ArrayList<LineChartSeries>();
      }
      
      return seriesList;
   }

   public void setSeriesList(List<LineChartSeries> seriesList) {
      this.seriesList = seriesList;
   }
   
   public void addSeries(LineChartSeries series) {
      getSeriesList().add(series);
   }
   
   public void removeSeries(int index) {
      getSeriesList().remove(index);
   }
   
   private void initializeSeriesList() {
      LineChartSeries series1 = new LineChartSeries();  
      series1.setLabel("Series 1");

      series1.set(1, 2);  
      series1.set(2, 1);  
      series1.set(3, 3);  
      series1.set(4, 6);  
      series1.set(5, 8);  

      addSeries(series1);
      
      LineChartSeries series2 = new LineChartSeries();  
      series2.setLabel("Series 2");  
      series2.setMarkerStyle("diamond");  

      series2.set(1, 6);  
      series2.set(2, 3);  
      series2.set(3, 2);  
      series2.set(4, 7);  
      series2.set(5, 9); 
      
      addSeries(series2);
   }
   
   private void initializeModel() {
      for(LineChartSeries series : getSeriesList()) {
         ((CartesianChartModel)model).addSeries(series);
      }      
  }
}
Lastly, here is ChartControllerBean.java, which LineChartControllerBean.java extends:

Code: Select all

package com.dashboard.controller;

import org.primefaces.model.chart.ChartModel;

public abstract class ChartControllerBean {

   protected ChartModel model;
   
   public ChartControllerBean() {
   }
   
   public ChartModel getModel()
   {
      return model;
   }

   public void setModel(ChartModel model)
   {
      this.model = model;
   }
   
   public String toString() {
      StringBuilder str = new StringBuilder();
      str.append(this.getClass());
      return str.toString();
   }
}

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

10 May 2012, 09:37

Primefaces charts are created by jqPlot. This JavaScript library is using HTML5 cancas, which could not be printed using the primefaces printer tag. The new version of jqPlot has the ability to export charts to images. But i do not know, if upgrade in PF is planned or not.

What you could do is using JFreeChart and put created images on your page, then you can print them as well.
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

jrobertsz66
Posts: 90
Joined: 11 Jun 2012, 12:52

25 Jul 2015, 03:17

I just posted the answer of how to do this here:

http://stackoverflow.com/questions/2530 ... ith-pchart
PrimeFaces 5.2 /GlassFish 4.1

Hamsterbau
Posts: 409
Joined: 28 Dec 2011, 17:44

27 Jul 2015, 20:32

After 2 years there is a solution ... for which version of Primefaces?
Looking at the JavaScript it looks like PF4 or newer, but i can be wrong.

Nowadays it is "simply" possible to write canvas and SVG objects directly as "streams" in HTML-Anchor-Elements using href attribute. But HTML5 was not so widely used 2012 and IE8 was still alive :lol:

MfG

Hamsterbau

PS: ... Browsers \o/
Primefaces 8.0.7 (PF Extensions 8.0)
JSF: Mojarra 2.3.2 (Spec 2.3) - running on WildFly 22

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 31 guests