0 Replies Latest reply on Feb 28, 2009 11:17 PM by Nick C

    OpenFlashChart v2 with Seam

    Nick C Newbie
      Hi there,

      I'm trying to use OpenFlashCharts v2 in my Seam application. After looking at various examples, I reckoned that the best way to go about things is to feed a JSON stream to a helper JavaScript which will generate the flash report. The developers helped out by providing a Struts 2 wrapper which looks like the following in the markup:

      <script type="text/javascript">
                     swfobject.embedSWF("<s:url value="/open-flash-chart.swf"/>", "my_chart", "1024", "300", "9.0.0",
                     "expressInstall.swf",
                     {"data-file":"<s:url value="/json-chart/bar.action"/>"});
      </script>

      Bar.action points to a Struts 2 action, which looks like the following:


      `public class Bar {
           protected Title title;
           protected List<Element> elements;
           
           public String execute() {
                title = new Title();
                title.setText("Hej");
                elements = new LinkedList<Element>();
                Element e = new Element();
                Object[] data = new Object[] {1,2,3,4,5,6,7,8,9,10};
                e.setValues(data);
                elements.add(e);
                return Action.SUCCESS;
           }     

           // Getters and Setters follow
           public Title getTitle() {
                return title;
           }
           public void setTitle(Title title) {
                this.title = title;
           }
           public List<Element> getElements() {
                return elements;
           }
           public void setElements(List<Element> elements) {
                this.elements = elements;
           }
      }`


      Basically this action will generate a JSON reponse, based on the struts.xml file provided:
      `     <package name="json-chart" namespace="/json-chart" extends="json-default">
                <action name="bar" class="action.Bar">
                     <result type="json"/>
                </action>
           </package>     `

      My question is, how will I get this done using Seam? Since Seam does not have this JSON converter that Struts is using, I resorted to use the JSON libraries provided at json.org. There, I found a JSONObject class which can be used to serialize a Java class to JSON. I tried to create a Seam component like the following:

      `
      @Name("bar")
      public class Bar {
           protected Title title;
           protected List<Element> elements;
           
           public JSONObject generateJSON() {
                      JSONObject json = new JSONObject();
                title = new Title();
                title.setText("Hej");
                elements = new LinkedList<Element>();
                Element e = new Element();
                Object[] data = new Object[] {1,2,3,4,5,6,7,8,9,10};
                e.setValues(data);
                elements.add(e);
                json.put("Title", title);
                      json.put("Elements", elements);
                      return json;
           }     

           // Getters and Setters follow
           public Title getTitle() {
                return title;
           }
           public void setTitle(Title title) {
                this.title = title;
           }
           public List<Element> getElements() {
                return elements;
           }
           public void setElements(List<Element> elements) {
                this.elements = elements;
           }
      }`

      And the following in my markup:
      `<script type="text/javascript">
                     swfobject.embedSWF("<s:url value="/open-flash-chart.swf"/>", "my_chart", "1024", "300", "9.0.0",
                     "expressInstall.swf",
                     {"data-file":"#{bar.generateJSON()}"});
      </script>`

      However, after running it, no data appeared in my chart. After clicking on "View source", I saw something similar to the following:

      `<script type="text/javascript">
                     swfobject.embedSWF("<s:url value="/open-flash-chart.swf"/>", "my_chart", "1024", "300", "9.0.0",
                     "expressInstall.swf",
                     {"data-file":"{{"Title": "Hej"}{"Elements", [1,2,3,4,5,6,7,8,9,10]}}"});
      </script>`

      The JSON data generated is not the problem because when I copied and pasted the rendered JSON string into a flat file, and changed the markup to target that file:

      "expressInstall.swf",
                     {"data-file":"chartdata.data"});

      the chart actually renders properly. Does anybody have a suggestion on what I should do? Thanks!