Author: chatellier Date: 2010-10-28 16:44:54 +0000 (Thu, 28 Oct 2010) New Revision: 120 Log: Add chart service and first generated chart Added: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/Coser.java trunk/coser-ui/src/main/java/fr/ifremer/coser/CoserConfig.java trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlGraphDialog.jaxx trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlHandler.java Added: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java (rev 0) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java 2010-10-28 16:44:54 UTC (rev 120) @@ -0,0 +1,153 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package fr.ifremer.coser.services; + +import java.awt.image.BufferedImage; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.axis.CategoryAxis; +import org.jfree.chart.axis.NumberAxis; +import org.jfree.chart.axis.ValueAxis; +import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; +import org.jfree.chart.plot.CategoryPlot; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.renderer.category.AreaRenderer; +import org.jfree.chart.renderer.category.CategoryItemRenderer; +import org.jfree.data.category.DefaultCategoryDataset; + +import fr.ifremer.coser.CoserBusinessConfig; +import fr.ifremer.coser.bean.Control; +import fr.ifremer.coser.bean.Project; +import fr.ifremer.coser.data.Catch; + +/** + * Validation service. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class ChartService { + + private static final Log log = LogFactory.getLog(ChartService.class); + + protected CoserBusinessConfig config; + + public ChartService(CoserBusinessConfig config) { + this.config = config; + } + + public JFreeChart getCatchGraph(Project project, Control control) { + + Iterator<String[]> itCatchData = control.getCatch().iterator(); + itCatchData.next(); // header + + // look for data (data summed) + SortedMap<String, Double> dataForYears = new TreeMap<String, Double>(); + String species = null; + while (itCatchData.hasNext()) { + String[] tuple = itCatchData.next(); + + if (species == null) { + species = tuple[Catch.INDEX_SPECIES]; + } + + if (species.equals(tuple[Catch.INDEX_SPECIES])) { + String year = tuple[Catch.INDEX_YEAR]; + String nombreValue = tuple[Catch.INDEX_NUMBER]; + try { + Double nombreDouble = Double.valueOf(nombreValue); + + if (dataForYears.containsKey(year)) { + Double oldValue = dataForYears.get(year); + Double newValue = oldValue + nombreDouble; + dataForYears.put(year, newValue); + } + else { + dataForYears.put(year, nombreDouble); + } + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't parse " + nombreValue + " as double"); + } + } + } + } + + List<SortedMap<String, Double>> results = new ArrayList<SortedMap<String,Double>>(); + results.add(dataForYears); + JFreeChart chart = displayGraph(results); + return chart; + } + + protected JFreeChart displayGraph(List<SortedMap<String, Double>> results) { + + // create a chart with the dataset + DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + + for (Map<String, Double> map : results) { + for (Map.Entry<String, Double> data : map.entrySet()) { + dataset.setValue(data.getValue(), "Capture campagne", data.getKey()); + } + } + + CategoryAxis categoryAxis = new CategoryAxis("Année"); + categoryAxis.setCategoryMargin(0); + // label horizontaux + //categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); + ValueAxis valueAxis = new NumberAxis("Nombre"); + valueAxis.setUpperMargin(0.1); + + //XYSplineRenderer renderer = new XYSplineRenderer(); + //AreaRenderer renderer = new AreaRenderer(); + //StackedBarRenderer renderer = new StackedBarRenderer(); + + CategoryItemRenderer renderer = new AreaRenderer(); + StandardCategoryItemLabelGenerator itemLabelGenerator = new StandardCategoryItemLabelGenerator(); + renderer.setBaseItemLabelGenerator(itemLabelGenerator); + renderer.setBaseItemLabelsVisible(true); + + CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer); + plot.setOrientation(PlotOrientation.VERTICAL); + JFreeChart chart = new JFreeChart("Captures", JFreeChart.DEFAULT_TITLE_FONT, + plot, true); + + return chart; + } +} Property changes on: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java =================================================================== --- trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java (rev 0) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java 2010-10-28 16:44:54 UTC (rev 120) @@ -0,0 +1,73 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package fr.ifremer.coser.services; + +import javax.swing.ImageIcon; +import javax.swing.JDialog; +import javax.swing.JLabel; + +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import fr.ifremer.coser.CoserBusinessException; +import fr.ifremer.coser.bean.Project; + +/** + * CommandService tests + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ + at Ignore +public class ChartServiceTest extends CoserTestAbstract { + + protected ProjectService projectService; + protected ChartService chartService; + + @Before + public void initService() { + projectService = new ProjectService(config); + chartService = new ChartService(config); + } + + @Test + public void testCatchChart() throws CoserBusinessException { + Project project = createTestProject(projectService); + JFreeChart chart = chartService.getCatchGraph(project, project.getControl()); + + JDialog f = new JDialog(); + f.setModal(true); + f.add(new ChartPanel(chart)); + f.pack(); + f.setVisible(true); + } +} Property changes on: trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/Coser.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/Coser.java 2010-10-28 16:44:32 UTC (rev 119) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/Coser.java 2010-10-28 16:44:54 UTC (rev 120) @@ -34,6 +34,7 @@ import org.nuiton.i18n.I18n; import org.nuiton.util.ArgumentsParserException; +import fr.ifremer.coser.services.ChartService; import fr.ifremer.coser.services.ImportService; import fr.ifremer.coser.services.ProjectService; import fr.ifremer.coser.services.ValidationService; @@ -126,6 +127,7 @@ context.setContextValue(new ProjectService(coserConfig)); context.setContextValue(new ImportService(coserConfig)); context.setContextValue(new ValidationService(coserConfig)); + context.setContextValue(new ChartService(coserConfig)); CoserFrame frame = new CoserFrame(context); frame.setLocationRelativeTo(null); frame.setVisible(true); Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/CoserConfig.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/CoserConfig.java 2010-10-28 16:44:32 UTC (rev 119) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/CoserConfig.java 2010-10-28 16:44:54 UTC (rev 120) @@ -27,11 +27,9 @@ import static org.nuiton.i18n.I18n._; -import java.io.File; import java.util.Locale; import org.apache.commons.beanutils.ConvertUtils; -import org.nuiton.util.ApplicationConfig; import org.nuiton.util.LocaleConverter; /** Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlGraphDialog.jaxx =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlGraphDialog.jaxx 2010-10-28 16:44:32 UTC (rev 119) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlGraphDialog.jaxx 2010-10-28 16:44:54 UTC (rev 120) @@ -25,7 +25,7 @@ <JDialog title="coser.ui.control.graphtitle"> <JScrollPane> - <JXImagePanel id="controlGraphPanel" /> + <JPanel id="controlGraphPanel" /> </JScrollPane> </JDialog> Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlHandler.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlHandler.java 2010-10-28 16:44:32 UTC (rev 119) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlHandler.java 2010-10-28 16:44:54 UTC (rev 120) @@ -32,8 +32,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; -import java.io.File; -import java.io.IOException; import java.util.List; import javax.swing.JLabel; @@ -48,7 +46,6 @@ import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.event.ListSelectionEvent; -import javax.swing.table.TableColumnModel; import jaxx.runtime.SwingUtil; import jaxx.runtime.validator.swing.SwingValidator; @@ -57,6 +54,8 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; import fr.ifremer.coser.CoserBusinessException; import fr.ifremer.coser.CoserConstants.Category; @@ -68,6 +67,7 @@ import fr.ifremer.coser.data.Haul; import fr.ifremer.coser.data.Length; import fr.ifremer.coser.data.Strata; +import fr.ifremer.coser.services.ChartService; import fr.ifremer.coser.services.ProjectService; import fr.ifremer.coser.services.ValidationService; @@ -419,13 +419,17 @@ } } + /** + * Display data graph. + * + * @param view view + */ public void displayGraph(ControlView view) { + ChartService chartService = view.getContextValue(ChartService.class); + Project project = view.getContextValue(Project.class); + JFreeChart chart = chartService.getCatchGraph(project, project.getControl()); ControlGraphDialog dialog = new ControlGraphDialog(view); - try { - dialog.getControlGraphPanel().setImage(javax.imageio.ImageIO.read(new File("/home/chatellier/tmp/coser/graph.png"))); - } catch (IOException ex) { - throw new CoserException("Can't init graph", ex); - } + dialog.getControlGraphPanel().add(new ChartPanel(chart)); dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setVisible(true);