6 Replies Latest reply on Nov 18, 2011 9:19 PM by itays100

    JfreeChart with seam (ToolTip problem)

    itays100 Newbie

      I am trying to generate chart from seam application and I used the code below from the JFreeChart war file.




      public static String generateXYChart(String section, HttpSession session, PrintWriter pw) {
          String filename = null;
          try {
              //  Retrieve list of WebHits
              WebHitDataSet whDataSet = new WebHitDataSet();
              ArrayList list = whDataSet.getDataByHitDate(section);
      
              //  Throw a custom NoDataException if there is no data
              if (list.size() == 0) {
                  System.out.println("No data has been found");
                  throw new NoDataException();
              }
      
              //  Create and populate an XYSeries Collection
              XYSeries dataSeries = new XYSeries("Hits");
              Iterator iter = list.listIterator();
              while (iter.hasNext()) {
                  WebHit wh = (WebHit)iter.next();
                  dataSeries.add(wh.getHitDate().getTime(),wh.getHitCount());
              }
              XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
      
              //  Create tooltip and URL generators
              SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
      
              StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
                      StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                      sdf, NumberFormat.getInstance());
              TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(
                      sdf, "pie_chart.jsp", "series", "hitDate");
      
              //  Create the chart object
              ValueAxis timeAxis = new DateAxis("");
              NumberAxis valueAxis = new NumberAxis("");
              valueAxis.setAutoRangeIncludesZero(false);  // override default
              StandardXYItemRenderer renderer = new StandardXYItemRenderer(
                      StandardXYItemRenderer.LINES + StandardXYItemRenderer.SHAPES,
                      ttg, urlg);
              renderer.setShapesFilled(true);
              XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, renderer);
              JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
              chart.setBackgroundPaint(java.awt.Color.white);
      
              //  Write the chart image to the temporary directory
              ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
              filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
      
              //  Write the image map to the PrintWriter
              ChartUtilities.writeImageMap(pw, filename, info);
              pw.flush();
      
          } catch (NoDataException e) {
              System.out.println(e.toString());
              filename = "public_nodata_500x300.png";
          } catch (Exception e) {
              System.out.println("Exception - " + e.toString());
              e.printStackTrace(System.out);
              filename = "public_error_500x300.png";
          }
          return filename;
      }
      



      I have this code in my seam entity:


      @Entity
      @Table(name = "myobject", catalog = "my_object")
      public class MyObject implements java.io.Serializable {
      
      .....
      
      @Transient
              public String getChartFileName() {
                      PrintWriter pw = new PrintWriter(System.out);
                      ExternalContext extCtxt = FacesContext.getCurrentInstance().getExternalContext();
                      javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest)extCtxt.getRequest();
              String reqUrl = request.getRequestURL().toString();
              this.fileName = WebHitChart.generateXYChart("Service", request.getSession(), pw);
                      System.out.println("filename - " + fileName);
                      String graphURL = reqUrl + "/servlet/DisplayChart?filename=" + fileName;
                      return graphURL;
              }
      



      And I have this in my .xhtml:


      <img  id="myChartId" src="#{myobject.chartFileName}" usemap="##{myobject.chartShortName}">
      



      I able to see the chart on the page but the tooltip is not shown on mouseover like the example.
      The map tag is not generated as I check the generated html.


      I should be able to see something like this:



      <MAP NAME="jfreechart-3299886008362407185.png">
      
      <AREA SHAPE="POLY" COORDS="92,242,98,242,98,248,92,248,92,242,92,242" title="Hits: (01-Aug-2002, 235,059)" href="pie_chart.jsp?series=Hits&hitDate=01-Aug-2002">
      
      <AREA SHAPE="POLY" COORDS="106,234,112,234,112,240,106,240,106,234,106,234" title="Hits: (02-Aug-2002, 240,635)" href="pie_chart.jsp?series=Hits&hitDate=02-Aug-2002">
      
      <AREA SHAPE="POLY" COORDS="148,223,154,223,154,229,148,229,148,223,148,223" title="Hits: (05-Aug-2002, 248,903)" href="pie_chart.jsp?series=Hits&hitDate=05-Aug-2002">
      
      





      I'm not sure if it seam related or not but hopefully someone here had the same situation before.
      Thanks.

        • 1. Re: JfreeChart with seam (ToolTip problem)
          Martin Frey Newbie

          Hi
          At the time when i had to implement a some graphs in the application i support i did use the seam graph tag. I think this one supports the tooltip already. But it didnt support the creation of an imagemap for the jfreechart for executing hyperlinks on a click on a bar inside the chart. Take a look at the seam pdf extension. Probably its already enough for you. Dont just bind your data to an img tag. Else you need to bind the map data to an imagemap tag yourself. 


          What i had to do was creating my own taghandler that will create not just the img tag but also an imagemap if the chart has such an urlmap. This is less complex that you would expect.

          • 2. Re: JfreeChart with seam (ToolTip problem)
            itays100 Newbie

            Hi Martin thank for reply.


            I have tried also seam graph


            <s:graphicImage value="#{fund.chart}" usemap="#{fund.chart}" ismap="true"/>  
            
            But in order to make it work I had to change the method to return byte[]
            



            I am getting the same result a chart with no tooltip and map not generated.
            I'm not sure what did you mean by creation of an imagemap for the jfreechart for executing hyperlinks


            How did the tooltip work without the map tag ? I think you are missing something or you just
            haven't used the tooltip.
            I need the tooltip show details from the map (that should be generated).


            I found something weird in the code above as well
            When I writeImageMap the chart is not used in this method.


            ChartUtilities.writeImageMap(pw, filename, info);



            JFreeChart should have support for generating a chart with tooltip. I've seen this working from a war that I download from here
            My Link
            Just scroll down and you a link to download.

            • 3. Re: JfreeChart with seam (ToolTip problem)
              Martin Frey Newbie

              I gave you the wrong tag name sorry.


              Take a look at the seam pdf extension and the p:chart tag. I extended this tag to print also an imagemap for navigation and im not sure if the tooltips are coming because of that. Ill have to take a look on my code

              • 4. Re: JfreeChart with seam (ToolTip problem)
                itays100 Newbie

                Hi Martin,


                Seam pdf package include support for display chart on html as well. You are right.
                But the results are exactly the same.


                The only different is that you can configure the chart on the tag itself instead of using the JFreeChart constructor directly.


                I understand that you have/had your own implementation for the tag but JFreeChart has already support for tooltips and I would like use it in my seam application.

                • 5. Re: JfreeChart with seam (ToolTip problem)
                  Martin Frey Newbie

                  Hey Itay


                  I did not have to implement the image map creation myself. I took a look at the code and the tooltips are actually also comming from the imagemap. Only that i use it for both, tooltips and actions (links).


                  What i basically did is this:
                  - Extend org.jboss.seam.pdf.ui.UIAnyChart
                  - override the encodeEnd method
                    - add usemap attribute to the img html tag
                    - use org.jfree.chart.imagemap.ImageMapUtilities.getImageMap(useMap, imageInfo) to write the image map html tag
                  - create a facelet taglib for it

                  • 6. Re: JfreeChart with seam (ToolTip problem)
                    itays100 Newbie

                    Can you post sample code including the facelet taglib ? thanks.