{"id":527,"date":"2010-03-31T21:21:03","date_gmt":"2010-03-31T15:51:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=527"},"modified":"2010-06-09T15:32:38","modified_gmt":"2010-06-09T10:02:38","slug":"generating-dyanmic-charts-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/generating-dyanmic-charts-in-grails\/","title":{"rendered":"Generating Dynamic charts in Grails"},"content":{"rendered":"<p>In my project we need to generate dynamic Pie charts &amp; Area charts. I had various options to do so.<br \/>\n1. Use Google charts API<br \/>\n2. Use Grails Eastwood Chart plugins.<br \/>\n3. Use jFreeChart API.<\/p>\n<p>In case of Google charts, the application only generates the required URL &amp; get the chart from google server , which is unacceptable if we are developing intranet application.<\/p>\n<p>In case of Grails Eastwood Chart plugin,  it is good when we talk about simple charts (as it generates them quickly with only single line of code), but it does not provide vast functionality &amp; doesn&#8217;t have full control over the chart.<br \/>\nLike , in case of pie chart , it doesn&#8217;t provide Legends.<\/p>\n<p>So, finally i used the JfreeChart API for our project. It is free Java chart library. JFreeChart supports pie charts (2D and 3D), bar charts (horizontal and vertical, regular and stacked), line charts, scatter plots, time series charts, high-low-open-close charts, candlestick plots, Gantt charts, combined plots, thermometers, dials and more.<\/p>\n<p>For generating Charts in Jfree , just download the jfreechart-1.0.10.jar &amp; jcommon-1.0.13.jar  and place them in lib folder.<br \/>\nThe list we are passing contains the labels(keys)  &#038;  their values. Height and width are for size of chart.<\/p>\n<pre lang=\"groovy\">\r\n\r\nvoid drawPieChart(List keys, List values, Integer width, Integer height) {\r\n\r\nDefaultPieDataset pieDataset = new DefaultPieDataset();\r\n\r\nkeys.eachWithIndex {String data, Integer index -&gt;\r\npieDataset.setValue(data, values[index]);\r\n}\r\n\r\nPiePlot3D plot = new PiePlot3D(pieDataset);\r\n\r\nplot.setDirection(Rotation.CLOCKWISE)\r\nplot.setStartAngle(90)\r\nplot.setBackgroundPaint(Color.white)\r\nplot.setLabelGenerator(new CustomLabelGenerator())  \/\/ this class is used for generation of customized labels\r\nplot.setLabelOutlinePaint(Color.WHITE)\r\nplot.setLabelFont(new Font(\"SansSerif\", Font.PLAIN, 10))\r\n\r\nJFreeChart chart = new JFreeChart(plot)\r\nchart.setBorderVisible(false)\r\nchart.setBackgroundPaint(Color.white)\r\nchart.legend.setPosition(RectangleEdge.TOP)\r\n\r\nfinal ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());\r\nfinal File file1 = new File(\"\/\/project\/web-app\/images\/piechart.png\");\r\nChartUtilities.saveChartAsPNG(file1, chart, width, height, info);\r\n}\r\n<\/pre>\n<p>And the class used for generation of custom labels is:-<\/p>\n<pre  lang=\"groovy\" >\r\nstatic class CustomLabelGenerator implements PieSectionLabelGenerator {\r\n\r\n    public String generateSectionLabel(final PieDataset dataset, final Comparable key) {\r\n        String temp = null\r\n        if (dataset != null) {\r\n            temp = dataset.getValue(key).toString() + \"%\"\r\n        }\r\n        return temp\r\n    }\r\n\r\n<\/pre>\n<p>It will generate the labels as percentage(%) values.<\/p>\n<p>This method will generate the pie chart png image at the given path.<\/p>\n<p>Useful links:-<br \/>\n         <a href=\"http:\/\/jfree.org\/\">http:\/\/jfree.org\/<\/a><br \/>\n         <a href=\"http:\/\/grails.org\/plugin\/eastwood-chart\">http:\/\/grails.org\/plugin\/eastwood-chart<\/a><\/p>\n<p>Hope you find this useful.<\/p>\n<p>Vishal<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my project we need to generate dynamic Pie charts &amp; Area charts. I had various options to do so. 1. Use Google charts API 2. Use Grails Eastwood Chart plugins. 3. Use jFreeChart API. In case of Google charts, the application only generates the required URL &amp; get the chart from google server , [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/527"}],"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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}