{"id":12842,"date":"2014-04-09T02:17:19","date_gmt":"2014-04-08T20:47:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12842"},"modified":"2014-04-13T18:43:15","modified_gmt":"2014-04-13T13:13:15","slug":"customizing-fusion-chart-for-various-type-via-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/customizing-fusion-chart-for-various-type-via-grails\/","title":{"rendered":"Customizing Fusion Chart For Various Type Via Grails"},"content":{"rendered":"<p>Hi everyone,<\/p>\n<p>Recently in my Grails project I had to implement various fusion charts simultaneously on a single page. When I started implementing multiple fusion charts on a single page, the code became quite messy and complex. It became difficult even for me to understand and handle so many charts. This resulted in a lot of spaghetti code which quickly became unreadable and I needed to think of another way to render the charts in a much more efficient way and readable way. I followed the given steps in order to do so:<\/p>\n<p>1. First of all create a POGO to carry all the required information to render the fusion chart. For example:<\/p>\n<p>[code]<\/p>\n<p>class FbAnalyticsGraphVO {<br \/>\n String graphName<br \/>\n String graphDivName<br \/>\n String title<br \/>\n String swfUrl<br \/>\n String dataFormat<br \/>\n String height<br \/>\n String width<br \/>\n String dataSource<\/p>\n<p> \/\/ You can add more fields to further customize your VO and chart.<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>2. Now create a template to render an individual chart. For example &#8216;_customFusionChart.gsp&#8217;:<\/p>\n<p>[code]<br \/>\n&lt;div id=&quot;${fbAnalyticsGraphVO.graphDivName}&quot;<br \/>\n     style=&quot;height: ${FbAnalyticsGraphVO.height}; width: ${FbAnalyticsGraphVO.width};&quot;&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;\/\/ &lt;![CDATA[<br \/>\n$(function () {<br \/>\n if (FusionCharts(&#8216;${FbAnalyticsGraphVO.graphName}&#8217;)) {<br \/>\n   FusionCharts(&#8216;${FbAnalyticsGraphVO.graphName}&#8217;).dispose()<br \/>\n }<\/p>\n<p> jQuery(&#8216;#&#8217; + &#8216;${FbAnalyticsGraphVO.graphDivName}&#8217;).insertFusionCharts({<br \/>\n     swfUrl: &quot;${FbAnalyticsGraphVO.swfUrl}&quot;,<br \/>\n     dataSource: &#8216;${FbAnalyticsGraphVO.fusionChartDataJson}&#8217;,<br \/>\n     renderer: &#8216;JavaScript&#8217;,<br \/>\n     dataFormat: &quot;${FbAnalyticsGraphVO.dataFormat}&quot;,<br \/>\n     height: &quot;${FbAnalyticsGraphVO.height}&quot;,<br \/>\n     width: &quot;${FbAnalyticsGraphVO.width}&quot;,<br \/>\n     id: &#8216;${FbAnalyticsGraphVO.graphName}&#8217;<br \/>\n });<br \/>\n});<br \/>\n\/\/ ]]&gt;&lt;\/script&gt;<br \/>\n[\/code]<\/p>\n<p>3. Now in your controller, create an action named <strong><em>renderVariousFusionCharts()<\/em><\/strong> which creates and instance of <strong><em>FbAnalyticsGraphVO<\/em><\/strong> for each chart you want to render and add all chart instances into a list as follows:<\/p>\n<p>[code]<\/p>\n<p>def renderVariousFusionCharts() {<br \/>\n   List fbAnalyticsGraphVOs = []<br \/>\n   FbAnalyticsGraphVO fusionChart1 = new FbAnalyticsGraphVO(<br \/>\n        type: &quot;chartType1&quot;,<br \/>\n        swfUrl: &#8216;MSArea&#8217;,<br \/>\n        title: &quot;rendering chart type 1&quot;,<br \/>\n        graphName: &#8216;chartType1Graph&#8217;,<br \/>\n        graphDivName: &quot;chart1GraphDiv&quot;,<br \/>\n        dataFormat: &quot;json&quot;,<br \/>\n        height: &#8216;100%&#8217;,<br \/>\n        width: &#8216;100%&#8217;,<br \/>\n        dataSource: &#8216;..\/demo\/jsonDataForChartType1&#8217;<br \/>\n   )<br \/>\n fusionChartList.add(fusionChart1)<\/p>\n<p> \/\/Similarly you can prepare FbAnalyticsGraphVO for other charts.<\/p>\n<p> FbAnalyticsGraphVO fusionChart2 = new FbAnalyticsGraphVO(&#8230;)<br \/>\n fusionChartList.add(fusionChart2)<br \/>\n .<br \/>\n .<br \/>\n .<br \/>\n FbAnalyticsGraphVO fusionChart5 = new FbAnalyticsGraphVO(&#8230;)<br \/>\n fusionChartList.add(fusionChart5)<\/p>\n<p> render(view: &#8216;\/demo\/variousFusionCharts&#8217;, model: [fbAnalyticsGraphVOs: fbAnalyticsGraphVOs])<br \/>\n}<\/p>\n<p>[\/code]<\/p>\n<p>This following code segment can then be used to render all the various fusion charts we want using the data sent from the controller:<\/p>\n<p>[code]<br \/>\n&lt;%@ page import=&quot;net.thoughtbuzz.omniog.enums.ArticleSourceType&quot; contentType=&quot;text\/html;charset=UTF-8&quot; %&gt;<br \/>\n&lt;html&gt;<\/p>\n<p>&lt;head&gt;<br \/>\n &lt;meta name=&quot;layout&quot; content=&quot;mainLayout&quot;\/&gt;<br \/>\n &lt;title&gt;&lt;g:message code=&quot;omniog.menu.dashboard&quot;\/&gt;&lt;\/title&gt;<br \/>\n &lt;g:javascript src=&quot;..\/fusionCharts\/js\/FusionCharts.js&quot;\/&gt;<br \/>\n &lt;g:javascript src=&quot;..\/fusionCharts\/js\/FusionCharts.HC.js&quot;\/&gt;<br \/>\n &lt;g:javascript src=&quot;..\/fusionCharts\/js\/FusionCharts.HC.Charts.js&quot;\/&gt;<br \/>\n &lt;g:javascript src=&quot;..\/fusionCharts\/js\/FusionCharts.jqueryplugin.js&quot;\/&gt;<br \/>\n &lt;g:javascript src=&quot;..\/fusionCharts\/js\/FusionChartsExportComponent.js&quot;\/&gt;<br \/>\n&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<br \/>\n &lt;div&gt;<br \/>\n  &lt;g:each test=&quot;${fbAnalyticsGraphVOs}&quot; var=&quot;fbAnalyticsGraphVO&quot;&gt;<br \/>\n   &lt;span&gt;<br \/>\n    &lt;h4&gt;${fbAnalyticsGraphVO.title}&lt;\/h4&gt;<br \/>\n    &lt;g:render template=&quot;customFusionChart&quot; model=&quot;[fbAnalyticsGraphVO: fbAnalyticsGraphVO]&quot;\/&gt;<br \/>\n   &lt;\/span&gt;<br \/>\n  &lt;\/g:each&gt;<br \/>\n &lt;\/div&gt;<br \/>\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<br \/>\n[\/code]<\/p>\n<p>Hope this helps anyone who is looking for a better way to render multiple Fusion charts on a single page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, Recently in my Grails project I had to implement various fusion charts simultaneously on a single page. When I started implementing multiple fusion charts on a single page, the code became quite messy and complex. It became difficult even for me to understand and handle so many charts. This resulted in a lot [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12842"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12842"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12842\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}