JSF Chart creator printing problem
JSF Chart creator printing problem
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...
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...
Not sure this will help or not. But can give a try
On graph.java
On your jsf /jsp page
On graph.java
Code: Select all
byte[] getImageByte(JFreeChart chart)
{
BufferedImage img = chart.createBufferedImage(500, 300);
byte[] imgByte = CharUtilities.encodeAsPNG(img);
return imgByte;
}
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
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..
<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..
On the JSP page
This will return as a image file.
So you can do <img src="xxx.jsp"/>
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();
%>
So you can do <img src="xxx.jsp"/>
Strafing for excellence
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;
}
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;
}
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.
There, you created a JFreeChart instance which you can latter pass to the function.
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);
Strafing for excellence