Grails Domain Model - One to Many / Bidirectional Friday, April 17, 2009

One to Many / Bidirectional

I am in the process of constructing a Domain Model for a grails application.



Blogger Extension for OpenOffice

Blogger Extension for OpenOffice

Tried the "Blogger for Word" plugin, but it did not work with the new Blooger version, and Google is not updating it.

http://extensions.services.openoffice.org/download/995








Comments

It did not upload the image, so I needed to manually edit this post and upload the image.





Pareto Chart Service for Grails

Creating a Pareto Chart Service for Grails 









Prerequisites:
  • Install grails
  • JFreeCharts
  • Run: grails create-service ParetoChart
  • Got to “./grails-app/service/” replace the “ParetoChartService.groovy” script attached, or copy and paste contents.
Usage:
  • In your view, just call the service and its single method.
  • E.g., paretoChartService.makeChart()

Notes:
    • Pass in a map of data
      • def data = ["A":4800, "B":2000, "C#":26, "Java":1901, "SQL":263, "Perl":2500, "PHP":1689, "Python":948, "Ruby":100, "Unix Shell":485
    • Need to pass about 5 arguments for
      • Map data, i.e.,  [category: value] 
      • String Title label
      • String y-axis label
      • String x-axis label
      • List of String subtitles   
    paretoChartService.makeChart(data, "My First Chart", "Y Axis", "X Axis", ["Subtitle A", "Subtitle B"] )     
    


    Output:  









    Code:

    import java.text.*  
    import java.awt.Font  
    import org.jfree.chart.labels.*  
    import org.jfree.chart.renderer.category.*  
    import org.jfree.chart.ChartFactory  
    import org.jfree.chart.ChartPanel  
    import org.jfree.chart.JFreeChart  
    import org.jfree.chart.axis.CategoryAxis  
    import org.jfree.chart.axis.NumberAxis  
    import org.jfree.chart.plot.CategoryPlot  
    import org.jfree.chart.plot.DatasetRenderingOrder  
    import org.jfree.chart.plot.PlotOrientation  
    import org.jfree.chart.title.TextTitle  
    import org.jfree.data.DataUtilities  
    import org.jfree.data.DefaultKeyedValues  
    import org.jfree.data.KeyedValues  
    import org.jfree.data.category.CategoryDataset  
    import org.jfree.data.general.DatasetUtilities  
    import org.jfree.ui.ApplicationFrame  
    import org.jfree.ui.RefineryUtilities  
    import org.jfree.util.SortOrder  
    import org.jfree.chart.ChartUtilities
    
    class ParetoChartService {  
        boolean transactional = true  
        
        def makeChart(Map rawData, String titleLabel, String yAxisLabel, String xAxisLabel, List subTitle) {  
            
            DefaultKeyedValues data = new DefaultKeyedValues()  
       
            //Repackage rawRaw input into data package  
            rawData.each{k,v ->  
                data.addValue(k , new Integer(v))  
            }  
       
            data.sortByValues(SortOrder.DESCENDING)  
            KeyedValues cumulative = DataUtilities.getCumulativePercentages(data)  
            CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Languages", data)  
            
            JFreeChart chart = ChartFactory.createBarChart(
                titleLabel,           // chart title
                yAxisLabel,           // domain axis label
                xAxisLabel,           // range axis label 
                dataset,              // data  
                PlotOrientation.VERTICAL,
                true,                 // include legend
                true,
                false
            )
       
            subTitle.each{   
                chart.addSubtitle(new TextTitle(it))  
            }
    
            //Create a plot object for more customizations  
            CategoryPlot plot = chart.getCategoryPlot()
    
            //Create a renderer for our plot, so we can create a label generator  
            CategoryItemRenderer renderer = plot.getRenderer()
       
            //Create a label generator for our bars
            CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0"))
       
            //Give the label generator to the renderer  
            renderer.setItemLabelGenerator(generator)  
            renderer.setItemLabelsVisible(true)  
            renderer.setSeriesItemLabelsVisible(0, true)  
            renderer.setItemLabelFont(new Font("SansSerif", Font.PLAIN, 10))  
       
            //Get handle on domainAxis to change the margins  
            CategoryAxis domainAxis = plot.getDomainAxis()  
       
            //Margin between first bar on the left, and the y-axis line  
            domainAxis.setLowerMargin(0.02)  
       
            //Margin between last bar on the right, and the number-axis line  
            domainAxis.setUpperMargin(0.02)  
        
            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis()  
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits())  
            rangeAxis.setUpperMargin(1.2)
    
            ************ Part II ****************  
            //Start to prepare pareto line  
            LineAndShapeRenderer renderer2 = new LineAndShapeRenderer()  
       
            //Format lables as percentages  
            NumberFormat formatter = NumberFormat.getPercentInstance()  
       
            //Create generator to feed lables to the renderer  
            CategoryItemLabelGenerator generator2 = new StandardCategoryItemLabelGenerator("{2}", formatter)  
       
            //Give the renderer a handle to the label generator  
            renderer2.setItemLabelGenerator(generator2)  
       
            //Customize the labels  
            renderer2.setItemLabelsVisible(true)  
            renderer2.setSeriesItemLabelsVisible(0, true)  
            renderer2.setItemLabelFont(new Font("SansSerif", Font.PLAIN, 10))  
       
            //Create the dataset for the percentage line  
            CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("Cumulative", cumulative)  
       
            //Create a secondary axis on the right side for our percents  
            NumberAxis axis2 = new NumberAxis("Percent")  
        
            //Set a margin so all labels above line are visible  
            axis2.setUpperMargin(0.3)  
        
            //Format the text of lables to read as percentages   
            axis2.setNumberFormatOverride(NumberFormat.getPercentInstance())  
        
            //Set all the new values in the plot object   
            plot.setRangeAxis(1, axis2)  
            plot.setDataset(1, dataset2)  
            plot.setRenderer(1, renderer2)  
            plot.mapDatasetToRangeAxis(1, 1)  
        
            //Render our plot  
            //Render primary dataset behind the second   
            plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD)  
        
            //Write chart to .png image  
            ChartUtilities.writeChartAsPNG(new FileOutputStream("./web-app/images/export.png"),chart, 500 , 250)  
        }  
    }
    

    Grails Domain Model - One to Many / Bidirectional

    One to Many / Bidirectional