|
Chartdirector in JSF2 |
Posted by Stevod on Jun-12-2013 04:56 |
|
Hi,
I have previously used Chartdirector very successfuly in ASP.Net and I am now planning to use the same in
JSF 2.
In order to prove the concept, based on a few of the posts in this forum, I have put together a very simple demo
using Netbeans 7.3, listed below.
However, when I run the project to open the index.html file, the url field in the h:graphicImage is blank, and
there is a Null Pointer exception in the Glassfish server log before the start of the Charts constructor in the
bean.
Can you provide any pointers to what I may be doing wrong?
Thanks,
Stevod
index.html
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<f:view>
<h:graphicImage url="#{Charts.url}" style="border:0" />
</f:view>
</h:body>
</html>
---------------------------------
JAVA BEAN
package bean;
import ChartDirector.XYChart;
import javax.ejb.Stateless;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Stateless
public class Charts {
public Charts()
{
HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse)
FacesContext.getCurrentInstance().getExternalContext().getResponse();
//The followings are cut and paste from the Simple Bar Chart sample code
//that comes with ChartDirector.
//The data for the bar chart
double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};
//Create a XYChart object of size 250 x 250 pixels
XYChart c = new XYChart(250, 250);
//Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200);
//Add a bar chart layer using the given data
c.addBarLayer(data);
//Set the labels on the x axis.
c.xAxis().setLabels(labels);
//output the chart
String chart1URL = c.makeSession(request, "chart1");
//include tool tip for the chart
String imageMap1 = c.getHTMLImageMap("", "", "title='{xLabel}: US${value}K'");
//set to member variables
_url = response.encodeURL("getchart.jsp?"+chart1URL);
_imageMap = "<map name='map1'>" + imageMap1 + "</map>";
}
private String _url;
private String _imageMap;
public String getUrl()
{
HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse)
FacesContext.getCurrentInstance().getExternalContext().getResponse();
//The followings are cut and paste from the Simple Bar Chart sample code
//that comes with ChartDirector.
//The data for the bar chart
double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};
//Create a XYChart object of size 250 x 250 pixels
XYChart c = new XYChart(250, 250);
//Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200);
//Add a bar chart layer using the given data
c.addBarLayer(data);
//Set the labels on the x axis.
c.xAxis().setLabels(labels);
//output the chart
String chart1URL = c.makeSession(request, "chart1");
//include tool tip for the chart
String imageMap1 = c.getHTMLImageMap("", "", "title='{xLabel}: US${value}K'");
//set to member variables
_url = response.encodeURL("getchart.jsp?"+chart1URL);
_imageMap = "<map name='map1'>" + imageMap1 + "</map>";
return _url;
}
public String getImageMap()
{
return _imageMap;
}
} |
Re: Chartdirector in JSF2 |
Posted by Stevod on Jun-12-2013 20:38 |
|
Hi,
I have got Chartdirector working in a difference JSF test project, after lots of detective
work (which is always more satisfying anyway). Therefore, there is no need to respond to
this post. |
Re: Chartdirector in JSF2 |
Posted by Skyfox on Jan-10-2014 18:23 |
|
Hi Stevod,
I'm so interesting how you are work Chartdirector in JSF2. I follow same steps in this post:
http://www.chartdir.com/forum/download_thread.php?
bn=chartdir_support&thread=1239105242#N1239810952
I couldn't make it works. Could you give any guide lines?
Thanks for your example. |
Re: Chartdirector in JSF2 |
Posted by Peter Kwan on Jan-10-2014 22:42 |
|
Hi Skyfox,
I think the code is all correct. If there is any issue, it may be the CDI annotation the code
is using.
A simple way to make the code work is to use managed beans annotation instead of CDI
annotation (use "import javax.faces.bean.ManagedBean;", then changed the annotation
from @Stateless to @ManagedBean). If you prefer to use CDI annotation, you may try the
@Named and @RequestScope annotation (instead of the @Stateless annotation).
I have attached the files that I have tried, and they work normally.
Note that from my experience, with some development tools (such as NetBeans with
GlassFish), occasionally you may have to shutdown the IDE and the web server and restart
it again, otherwise the life cycle of the beans may not be managed correctly.
Regards
Peter Kwan
|
|