JSF Chart creator printing problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
subash_ks
Posts: 10
Joined: Thu Aug 21, 2008 9:38 am

JSF Chart creator printing problem

Post by subash_ks » Thu Aug 21, 2008 9:45 am

I want to display my JSF chart creator graph in another pop up window but
its not displaying the image while we open the new window.I have embebde the graph inside <div> and while clicking on the print button I have used the following code..

function printDiv()
{
var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,";
disp_setting+="scrollbars=yes,width=650, height=500, left=100, top=25,status=yes";
var content_value = document.getElementById('drivingBehaviour:historyFrom:graphAssetDetail');
alert(content_value.innerHTML);
var docprint=window.open("","",disp_setting);

docprint.document.open();
docprint.document.write('<html><head><title>Fleet Management Server</title>');
docprint.document.write('</head><body>');
docprint.document.write('<center>');
docprint.document.write(content_value.innerHTML);
docprint.document.write('</center></body></html>');
docprint.document.close();
docprint.focus();
}


The problem is i am not getting the graph image in the new window..Its a web based application the graph is comming properly in my window with values from our data base.While taking the print preview in the browser the graph is not shown..Please help me...

kbng11
Posts: 11
Joined: Wed Aug 20, 2008 8:49 am
Location: Kuala Lumpur, Malaysia

Post by kbng11 » Thu Aug 21, 2008 10:09 am

Not sure this will help or not. But can give a try

On graph.java

Code: Select all

byte[] getImageByte(JFreeChart chart)
{
          BufferedImage img = chart.createBufferedImage(500, 300);
          byte[] imgByte = CharUtilities.encodeAsPNG(img);
          return imgByte;
}
On your jsf /jsp page

Code: Select all

graph g = new graph();
byte[] imgData = g.getImageByte();
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData); o.flush(); o.close();
Strafing for excellence

subash_ks
Posts: 10
Joined: Thu Aug 21, 2008 9:38 am

Post by subash_ks » Thu Aug 21, 2008 10:36 am

Thanks for the reply let me try it....

subash_ks
Posts: 10
Joined: Thu Aug 21, 2008 9:38 am

Post by subash_ks » Thu Aug 21, 2008 11:02 am

In which file i have to write this code..I have a JSF page and a bean for data source..This is my JSF code for chart.

<c:chart id="chart2" datasource="#{LineCharts.sourceProvider.categoryDataset}"
type="line" title ="Driving Behaviour" is3d="false" orientation="vertical"
usemap="#stackedbarmap" generateMap="stackedbarmap" ongeneratedimagemapclick="stackedChartClick"
legend="true" legendFontSize="11" output="png">
<c:chartAxis domain="true" verticalTickLabels="true" tickMarks="true" />
</c:chart>


This is my java code for creating Data set..

public DefaultCategoryDataset getCategoryDataset() {
DefaultCategoryDataset categoryDataSet;
try {
fetchDetailsGraph();
} catch (Exception e1) {
log.error("Exception in fetchDetailsGraph",e1);
}

categoryDataSet = new DefaultCategoryDataset();
try{
ArrayList temp=new ArrayList();
Double speed;
String str;
for(int i=0;i<graph.size();i++)
{
temp.clone();
temp=graph.get(i);
speed = new Double((String)temp.get(2)).doubleValue();
str=(String)temp.get(1);

if(str.length()!=22)
str=str.substring(0,19);
// create the dataset...
categoryDataSet.addValue(speed, (String)temp.get(0), str);
}

}
catch(Exception e)
{
log.error("Exception in getCategoryDataset",e);
}
return categoryDataSet;
}



In fire fox print preview is coming properly but in IE its not coming..Please help me..I am unale to take printouts in IE nothing is coming..

kbng11
Posts: 11
Joined: Wed Aug 20, 2008 8:49 am
Location: Kuala Lumpur, Malaysia

Post by kbng11 » Thu Aug 21, 2008 11:40 am

On the JSP page

Code: Select all

<%@ page language="java"
    import="java.util.ArrayList"
    import="java.io.OutputStream"
    import="com.graph"
%>
<%
    graph g = new graph();
    byte[] imgData = g.getImageByte();
    response.setContentType("image/gif");
    OutputStream o = response.getOutputStream();
    o.write(imgData); o.flush(); o.close();        
%>
This will return as a image file.
So you can do <img src="xxx.jsp"/>
Strafing for excellence

subash_ks
Posts: 10
Joined: Thu Aug 21, 2008 9:38 am

Post by subash_ks » Thu Aug 21, 2008 1:45 pm

could you please provide the bean code also.Thanks in advance

subash_ks
Posts: 10
Joined: Thu Aug 21, 2008 9:38 am

Post by subash_ks » Thu Aug 21, 2008 2:06 pm

I have only a DefaultCategoryDataset object (creating line chart).

String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
String category1 = "A";
String category2 = "B";
String category3 = "C";
String category4 = "D";
String category5 = "E";

// create the dataset...
categoryDataSet = new DefaultCategoryDataset();
categoryDataSet.addValue(1.0, series1, category1);
categoryDataSet.addValue(4.0, series1, category2);


.....................................
..............................
return categoryDataSet;



How can we convert categoryDataSet to JFreeChart object for using the method

byte[] getImageByte(JFreeChart chart)
{
BufferedImage img = chart.createBufferedImage(500, 300);
byte[] imgByte = CharUtilities.encodeAsPNG(img);
return imgByte;
}

kbng11
Posts: 11
Joined: Wed Aug 20, 2008 8:49 am
Location: Kuala Lumpur, Malaysia

Post by kbng11 » Fri Aug 22, 2008 3:14 am

As you can see, I am not using any bean. So you have to figure out yourself how to implement bean.

As for the DataSet. I will provide a simple example of a LineGraph, alter it to suit your categoryDataSet.

Code: Select all

DateAxis xAxis = new DateAxis("Date");
NumberAxis yAxis = new NumberAxis("Amount");
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
XYDataset dataset = getDataSet();
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart(plot);
There, you created a JFreeChart instance which you can latter pass to the function.
Strafing for excellence

Locked