8 Replies Latest reply on Nov 16, 2010 7:22 AM by Martin Frey

    jfree chart with seam

    Franck Olivier Newbie

      how to get stacked bar chart in seam. I need to display negative and positive value on a bar chart. Please help me get an issue.


      thanks.

        • 1. Re: jfree chart with seam
          Leo van den berg Master

          Hi,


          creating the chart is basically the same as with any object producing piece of code. You only need to get it the correct way in Seam.


          A small code snapshot which you can use for any byte array result.
          It needs additional work, but it is basically the same for any byte producing component such as JFreeChart or PDF generation.


          // Her comes you code to generate a byte[] (YOURBYTEARRA)
          ....
          // Then make sure it gets to the user.
          
          DocumentData data = new ByteArrayDocumentData("WHATEVER", new DocumentData.DocumentType("YOURTYPE", "YOURMIMETYEP"), YOURBYTEARRAY);
          
          DocumentStore documentStore = DocumentStore.instance();
          String docId = documentStore.newId();
          documentStore.saveData(docId, data);
          
          String documentUrl = documentStore.preferredUrlForContent(data
                                   .getBaseName(), data.getDocumentType().getExtension(),
                                   docId);
          
          // Manager is a internal Seam comppnent (@In Manager manager;)
          facesContext.getExternalContext().redirect(
          manager.encodeConversationId(documentUrl, null));
          // Don't process the reposne
          facesContext.responseComplete();
          



          Hopefully this gets you started. Read the Seam documentation, because it contains all the additional information you need.


          Leo

          • 2. Re: jfree chart with seam
            Franck Olivier Newbie
            Hi Leo,

            First thanks for reply. I just need bar chart to display temperatur values from a database on a browser and i'm searching a way to get positive and negative values on the same bar chart. Here a small facelet code:

            < p:barchart borderVisible="false"  legend="false" plotForegroundAlpha="0.9" orientation="horizontal" height="180" width="250" domainGridlinesVisible="true" plotBackgroundPaint="white">
                                            <p:series key="actual" seriesPaint="series1">
                                                    <p:data key="Temperatur" value="-4"/>
                                            </p:series>
                                            <p:series key="authorised" seriesPaint="series2">
                                                    <p:data key="Temperatur" value="-7"/>
                                            </p:series>
                                            <p:color id="series1" color="orange"/>
                                            <p:color id="series2" color="red"/>
            </p:barchart>

            May be you got a better way to do that.

            thanks in advance

            Olivier
            • 3. Re: jfree chart with seam
              Leo van den berg Master

              Hi,


              can't help you with this issue, because after different try-and-error runs I found that - at the moment - the additional viewing components are pretty restricted, badly documented and it has no editing tools, so I skipped the whole lot and use the different API's directly. Not as fancy as the facelet code, but a lot less headache.


              Leo

              • 4. Re: jfree chart with seam
                Martin Frey Newbie

                Hi Olivier.
                I used the <p:chart> tag to do something similar. The advantage is that in the backing bean you have full access to the jfreechart api. I even extended the seam chart component to allow the usage of the imagemap. Like this it's easy to introduce interactivity.




                <l:mappedChart chart="#{queryChart}" width="600" height="300"
                                    useMap="queryChartPopupMap" style="margin:10px" />
                




                where the queryChart returns a JFreechart object:




                @Factory(value = QUERIES_CHART, scope = ScopeType.EVENT)
                     @Transactional
                     public JFreeChart getQueriesChart() {
                     ...
                }



                Martin

                • 5. Re: jfree chart with seam
                  Franck Olivier Newbie

                  Hi Martin. Thanks for your post. I'm trying it with the


                  <p:chart>




                  , and i'm interesting to know, how you program the backing bean. Can you please post a code snippet? 


                  Olivier


                  • 6. Re: jfree chart with seam
                    Martin Frey Newbie

                    Sure.
                    Here's an example:




                    @Name("chartManager")
                    @Scope(ScopeType.CONVERSATION)
                    public class ChartManager implements Serializable {
                         @In
                         private EntityManager entityManager;
                    
                         @Factory(value = QUERIES_CHART)
                         @Transactional
                         public JFreeChart getQueriesChart() {
                              List<Object[]> results = entityManager.createNamedQuery("object.groupedByStatus").getResultList();
                    
                              DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                              for (Object[] result : results) {
                                   dataset.setValue((Long) result[1], "Personal", (PublishStatus) result[0]);
                              }
                    
                              JFreeChart chart = ChartFactory.createBarChart("Objects by Status", "Status", "Count", dataset, PlotOrientation.HORIZONTAL, true,
                                        false, true);
                              CategoryItemRenderer renderer = ((CategoryPlot) chart.getPlot()).getRenderer();
                              renderer.setBaseItemLabelsVisible(true);
                              renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
                              Font f = renderer.getBaseItemLabelFont();
                              Font f2 = new Font(f.getName(), f.getStyle(), 10);
                              renderer.setBaseItemLabelFont(f2);
                              return chart;
                    
                         }
                    
                    }



                    • 7. Re: jfree chart with seam
                      Franck Olivier Newbie

                      Thanks Martin. I've tried your code but my browser could not find the method getQueriesChart(). Can you please post the view to the above backing bean? Anyway i with you help, i understand it better :).    

                      • 8. Re: jfree chart with seam
                        Martin Frey Newbie

                        I just realized that the @Factory uses a constant ;) just give it a name you like.




                        @Name("chartManager")
                        @Scope(ScopeType.CONVERSATION)
                        public class ChartManager implements Serializable {
                          @Factory(value="queriesChart")
                          public JFreeChart getQueriesChart() {
                          }
                        }





                        And then either of these entries should work.


                        <p:chart chart="#{queriesChart}" width="600" height="300" />
                         // queriesChart cached conversation entry
                        
                        <p:chart chart="#{chartManager.queriesChart}" width="600" height="300" />
                         // chartmanager cached in conversation but method getQueriesChart() called
                        
                        <p:chart chart="#{chartManager.getQueriesChart()}" width="600" height="300" />
                         // chartmanager cached in conversation but method called