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.


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


          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(),
          // Manager is a internal Seam comppnent (@In Manager manager;)
          manager.encodeConversationId(documentUrl, null));
          // Don't process the reposne

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


          • 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 key="authorised" seriesPaint="series2">
                                                    <p:data key="Temperatur" value="-7"/>
                                            <p:color id="series1" color="orange"/>
                                            <p:color id="series2" color="red"/>

            May be you got a better way to do that.

            thanks in advance

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


              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.


              • 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)
                     public JFreeChart getQueriesChart() {


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

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


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


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

                    Here's an example:

                    public class ChartManager implements Serializable {
                         private EntityManager entityManager;
                         @Factory(value = QUERIES_CHART)
                         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.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
                              Font f = renderer.getBaseItemLabelFont();
                              Font f2 = new Font(f.getName(), f.getStyle(), 10);
                              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.

                        public class ChartManager implements Serializable {
                          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