ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  chartdirector and jsf
Posted by riadhhwajdii on Aug-24-2009 16:38
hi
i am developping a jsf application
now i would like to add chartdirector to my application
i saw the documentation but all was done in the jsp page: i would like to create the chart in my bean and call it in the jsp
can anyone help me?

  Re: chartdirector and jsf
Posted by Peter Kwan on Aug-24-2009 22:45
Hi riadhhwajdii,

There is an example in the following thread. May be you can use it as reference.

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&pattern=JSF&thread=1239105242#N1239810952

Basically, your chart in bean only needs to have one or two properties as output, which is the URL of the created chart image, and the image map (if you are using image map). An example is like:


class MyChart
{
   public MyChart()
   {
        //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()
    {
        return _url;
    }

    public String getImageMap()
    {
        return _imageMap;
    }
}


Then in your JSF web page, you may use above bean for your image and image map.


<HTML>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <body bgcolor="white">
    <f:view>
         <h:graphicImage url='#{MyChart.url}' usemap="#map1" style="border:0" />
         <h:outputText escape="false" value="#{MyChart.imageMap}" />
    </f:view>
    </body>
</HTML>


Hope this can help.

Regards
Peter Kwan

  Re: chartdirector and jsf
Posted by p_t on Apr-14-2011 06:56
Mr. Kwan,

You give a fairly good example, but when I was trying it out, I noticed that you placed a 'request' inside the c.makeSession() method. What kind of object/value is 'request'?

//output the chart
        String chart1URL = c.makeSession(request, "chart1");

Thanks,
p_t

  Re: chartdirector and jsf
Posted by Peter Kwan on Apr-14-2011 16:09
Hi p_t,

Sorry for the confusion. The request and response objects are:

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

I actually copied the code from another post, but I forgot to copy the above two lines. The original code comes from:

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_general&pattern=JSF&thread=1285061534

Hope this can help.

Regards
Peter Kwan

  Re: chartdirector and jsf
Posted by p_t on Apr-14-2011 22:05
Great support. Quick response. Thanks for the help. :)