Generating Dynamic charts in Grails

31 / Mar / 2010 by Vishal Sahu 7 comments

In my project we need to generate dynamic Pie charts & 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 & get the chart from google server , which is unacceptable if we are developing intranet application.

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 & doesn’t have full control over the chart.
Like , in case of pie chart , it doesn’t provide Legends.

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.

For generating Charts in Jfree , just download the jfreechart-1.0.10.jar & jcommon-1.0.13.jar and place them in lib folder.
The list we are passing contains the labels(keys) & their values. Height and width are for size of chart.


void drawPieChart(List keys, List values, Integer width, Integer height) {

DefaultPieDataset pieDataset = new DefaultPieDataset();

keys.eachWithIndex {String data, Integer index ->
pieDataset.setValue(data, values[index]);
}

PiePlot3D plot = new PiePlot3D(pieDataset);

plot.setDirection(Rotation.CLOCKWISE)
plot.setStartAngle(90)
plot.setBackgroundPaint(Color.white)
plot.setLabelGenerator(new CustomLabelGenerator())  // this class is used for generation of customized labels
plot.setLabelOutlinePaint(Color.WHITE)
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 10))

JFreeChart chart = new JFreeChart(plot)
chart.setBorderVisible(false)
chart.setBackgroundPaint(Color.white)
chart.legend.setPosition(RectangleEdge.TOP)

final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
final File file1 = new File("//project/web-app/images/piechart.png");
ChartUtilities.saveChartAsPNG(file1, chart, width, height, info);
}

And the class used for generation of custom labels is:-

static class CustomLabelGenerator implements PieSectionLabelGenerator {

    public String generateSectionLabel(final PieDataset dataset, final Comparable key) {
        String temp = null
        if (dataset != null) {
            temp = dataset.getValue(key).toString() + "%"
        }
        return temp
    }

It will generate the labels as percentage(%) values.

This method will generate the pie chart png image at the given path.

Useful links:-
http://jfree.org/
http://grails.org/plugin/eastwood-chart

Hope you find this useful.

Vishal
vishal@intelligrape.com

Tag -

FOUND THIS USEFUL? SHARE IT

comments (7)

  1. Roxanne

    I read a lot of interesting posts here. Probably you spend
    a lot of time writing, i know how to save you a lot of work,
    there is an online tool that creates high quality, google friendly articles
    in seconds, just type in google – laranitas free content
    source

    Reply
  2. Sigma Infosolutions

    Hi ,

    My Problem is that I am using grails Weceem CMS Plugin .
    I want to combine Jsecurity plugin with this weceem cms plugin.
    how to do it ?

    Please help me to find the solution.

    Regards
    DINESH T

    Reply
    1. Falapio

      HI Basem, If you browse thourgh various posts and comments on this blog you will soon realize that my approach to Elliott Waves is different from most others. I tend to use EWP to help me in my trades. The emphasis is not so much on being accurate with wave counts because Elliott Wave counts are always a work-in-progress. One should learn how to use the wave principle to trade effectively, to keep risks under control, and take advantage of moves that turn out to be right. So I retain the ability to change my counts to suit my trading objective. Also you will do well to learn that a majority of wave counts will be proved wrong sooner or later (including mine). Success is measured by how you deal with the markets in the interim!

      Reply
  3. Roshan Shrestha

    If you are not against flash, then Open Flash Chart produces much more beautiful charts than JFreeCharts, and the server only has to send XML or JSON data to the browser. There is even a Grails plugin for that

    Reply

Leave a comment -