Author: chatellier Date: 2010-11-18 17:33:16 +0000 (Thu, 18 Nov 2010) New Revision: 238 Log: Generation des graphe de toutes les especes d'un seul coup Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java trunk/coser-business/src/main/resources/i18n/coser-business_fr_FR.properties trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.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 Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java 2010-11-18 15:06:25 UTC (rev 237) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java 2010-11-18 17:33:16 UTC (rev 238) @@ -31,8 +31,9 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.SortedMap; +import java.util.SortedSet; import java.util.TreeMap; +import java.util.TreeSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -46,6 +47,7 @@ import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.chart.renderer.category.LineAndShapeRenderer; +import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import fr.ifremer.coser.CoserBusinessConfig; @@ -75,92 +77,142 @@ } /** - * Retourne le graph de comparaison entre le nombre de capture et - * le nombre dans les tailles. + * Retourne les graphiques de comparaison entre le nombre de capture et + * le nombre dans les tailles pour toutes les especes. * - * @param project projet + * @param project project * @param control control - * @param specyName sepcy name * @return chart */ - public JFreeChart getCompareCatchLengthGraph(Project project, Control control, String specyName) { + public Map<String, JFreeChart> getCompareCatchLengthGraph(Project project, Control control) { + int minYear = Integer.MAX_VALUE; + int maxYear = Integer.MIN_VALUE; + // look for data (data summed over catch) - SortedMap<String, Double> catchForYears = new TreeMap<String, Double>(); + Map<String, Map<String, Double>> catchForSpeciesYears = new HashMap<String, Map<String, Double>>(); Iterator<String[]> itCatchData = control.getCatch().iterator(); itCatchData.next(); // header while (itCatchData.hasNext()) { String[] tuple = itCatchData.next(); + String species = tuple[Catch.INDEX_SPECIES]; - if (specyName.equals(tuple[Catch.INDEX_SPECIES])) { - String year = tuple[Catch.INDEX_YEAR]; - String nombreValue = tuple[Catch.INDEX_NUMBER]; - try { - Double nombreDouble = Double.valueOf(nombreValue); - - if (catchForYears.containsKey(year)) { - Double oldValue = catchForYears.get(year); - Double newValue = oldValue + nombreDouble; - catchForYears.put(year, newValue); - } - else { - catchForYears.put(year, nombreDouble); - } + Map<String, Double> speciesCatchForYears = catchForSpeciesYears.get(species); + if (speciesCatchForYears == null) { + speciesCatchForYears = new HashMap<String, Double>(); + catchForSpeciesYears.put(species, speciesCatchForYears); + } + + String year = tuple[Catch.INDEX_YEAR]; + try { + int intYear = Integer.parseInt(year); + minYear = Math.min(minYear, intYear); + maxYear = Math.max(maxYear, intYear); + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't parse " + year + "as int"); } - catch (NumberFormatException ex) { - if (log.isWarnEnabled()) { - log.warn("Can't parse " + nombreValue + " as double"); - } + } + String nombreValue = tuple[Catch.INDEX_NUMBER]; + try { + Double nombreDouble = Double.valueOf(nombreValue); + + if (speciesCatchForYears.containsKey(year)) { + Double oldValue = speciesCatchForYears.get(year); + Double newValue = oldValue + nombreDouble; + speciesCatchForYears.put(year, newValue); } + else { + speciesCatchForYears.put(year, nombreDouble); + } } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't parse " + nombreValue + " as double"); + } + } } // look for data (data summed over length) - SortedMap<String, Double> lengthForYears = new TreeMap<String, Double>(); + Map<String, Map<String, Double>> lengthForSpeciesYears = new HashMap<String, Map<String, Double>>(); Iterator<String[]> itLengthData = control.getLength().iterator(); itLengthData.next(); // header while (itLengthData.hasNext()) { String[] tuple = itLengthData.next(); - if (specyName.equals(tuple[Length.INDEX_SPECIES])) { - String year = tuple[Length.INDEX_YEAR]; - String nombreValue = tuple[Length.INDEX_NUMBER]; - try { - Double nombreDouble = Double.valueOf(nombreValue); - - if (lengthForYears.containsKey(year)) { - Double oldValue = lengthForYears.get(year); - Double newValue = oldValue + nombreDouble; - lengthForYears.put(year, newValue); - } - else { - lengthForYears.put(year, nombreDouble); - } + String species = tuple[Length.INDEX_SPECIES]; + + Map<String, Double> speciesLengthForYears = lengthForSpeciesYears.get(species); + if (speciesLengthForYears == null) { + speciesLengthForYears = new HashMap<String, Double>(); + lengthForSpeciesYears.put(species, speciesLengthForYears); + } + + String year = tuple[Length.INDEX_YEAR]; + String nombreValue = tuple[Length.INDEX_NUMBER]; + try { + Double nombreDouble = Double.valueOf(nombreValue); + + if (speciesLengthForYears.containsKey(year)) { + Double oldValue = speciesLengthForYears.get(year); + Double newValue = oldValue + nombreDouble; + speciesLengthForYears.put(year, newValue); } - catch (NumberFormatException ex) { - if (log.isWarnEnabled()) { - log.warn("Can't parse " + nombreValue + " as double"); + else { + speciesLengthForYears.put(year, nombreDouble); + } + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't parse " + nombreValue + " as double"); + } + } + } + + // generation des graphes pour chaques especes + Map<String, JFreeChart> charts = new TreeMap<String, JFreeChart>(); + for (Map.Entry<String, Map<String, Double>> catchEntries : catchForSpeciesYears.entrySet()) { + String species = catchEntries.getKey(); + Map<String, Double> catchNumbers = catchEntries.getValue(); + Map<String, Double> lengthNumbers = lengthForSpeciesYears.get(species); + + + //SortedMap<String, Double> catchForYears = new TreeMap<String, Double>(); + //SortedMap<String, Double> lengthForYears = new TreeMap<String, Double>(); + DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + for (int year = minYear ; year <= maxYear ; ++year) { + Double catchNumber = catchNumbers.get(String.valueOf(year)); + if (catchNumber == null) { + catchNumber = 0.0; + } + dataset.setValue(catchNumber, _(Category.CATCH.getTranslationKey()), (Integer)year); + + if (lengthNumbers != null) { + Double lengthNumber = lengthNumbers.get(String.valueOf(year)); + if (lengthNumber == null) { + lengthNumber = 0.0; } + dataset.setValue(lengthNumber, _(Category.LENGTH.getTranslationKey()), (Integer)year); } } + + JFreeChart chart = displayGraph(dataset, _("coser.business.chart.compareCatchLengthNumberTitle", species)); + charts.put(species, chart); } - - Map<String, SortedMap<String, Double>> results = new HashMap<String, SortedMap<String,Double>>(); - results.put(_(Category.CATCH.getTranslationKey()), catchForYears); - results.put(_(Category.LENGTH.getTranslationKey()), lengthForYears); - JFreeChart chart = displayGraph(results); - return chart; + + return charts; } - protected JFreeChart displayGraph(Map<String, SortedMap<String, Double>> resultSeries) { + protected JFreeChart displayGraph(CategoryDataset categoryDataSet, String title) { // create a chart with the dataset - DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + //DefaultCategoryDataset dataset = new DefaultCategoryDataset(); - for (Map.Entry<String, SortedMap<String, Double>> resultSerie : resultSeries.entrySet()) { + /*for (Map.Entry<String, SortedMap<String, Double>> resultSerie : resultSeries.entrySet()) { for (Map.Entry<String, Double> data : resultSerie.getValue().entrySet()) { dataset.setValue(data.getValue(), resultSerie.getKey(), data.getKey()); } - } + }*/ CategoryAxis categoryAxis = new CategoryAxis(_("coser.business.common.year")); categoryAxis.setCategoryMargin(0); @@ -179,9 +231,9 @@ CategoryToolTipGenerator cttg = new StandardCategoryToolTipGenerator("{0}: {2}", NumberFormat.getInstance()); renderer.setBaseToolTipGenerator(cttg); - CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer); + CategoryPlot plot = new CategoryPlot(categoryDataSet, categoryAxis, valueAxis, renderer); plot.setOrientation(PlotOrientation.VERTICAL); - JFreeChart chart = new JFreeChart(_("coser.business.chart.compareCatchLengthNumberTitle"), + JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); return chart; Modified: trunk/coser-business/src/main/resources/i18n/coser-business_fr_FR.properties =================================================================== --- trunk/coser-business/src/main/resources/i18n/coser-business_fr_FR.properties 2010-11-18 15:06:25 UTC (rev 237) +++ trunk/coser-business/src/main/resources/i18n/coser-business_fr_FR.properties 2010-11-18 17:33:16 UTC (rev 238) @@ -31,7 +31,7 @@ coser.business.category.reftax.species=Reftax (esp\u00E8ce) coser.business.category.strata=Strates coser.business.category.typeEspece=Code type des esp\u00E8ces -coser.business.chart.compareCatchLengthNumberTitle=Comparaison des nombre dans Capture et Taille +coser.business.chart.compareCatchLengthNumberTitle=Comparaison des nombres dans Capture et Taille (%s) coser.business.common.number=Nombre coser.business.common.year=Ann\u00E9e coser.business.control.error.diffCatchLength=Diff\u00E9rence entre les captures et taille Modified: trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java =================================================================== --- trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java 2010-11-18 15:06:25 UTC (rev 237) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java 2010-11-18 17:33:16 UTC (rev 238) @@ -25,6 +25,8 @@ package fr.ifremer.coser.services; +import java.util.Map; + import javax.swing.JDialog; import org.jfree.chart.ChartPanel; @@ -60,8 +62,8 @@ @Test public void testCatchChart() throws CoserBusinessException { Project project = createTestProject(projectService); - JFreeChart chart = chartService.getCompareCatchLengthGraph(project, project.getControl(), "COSER_SPECIES1"); - + Map<String, JFreeChart> charts = chartService.getCompareCatchLengthGraph(project, project.getControl()); + JFreeChart chart = charts.get("COSER_SPECIES1"); JDialog f = new JDialog(); f.setModal(true); f.add(new ChartPanel(chart)); 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-11-18 15:06:25 UTC (rev 237) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlGraphDialog.jaxx 2010-11-18 17:33:16 UTC (rev 238) @@ -38,7 +38,7 @@ </row> <row fill="both" weightx="1" weighty="1" columns="2"> <cell> - <JPanel id="controlGraphPanel" /> + <JPanel id="controlGraphPanel" layout="{new BorderLayout()}"/> </cell> </row> </Table> 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-11-18 15:06:25 UTC (rev 237) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlHandler.java 2010-11-18 17:33:16 UTC (rev 238) @@ -25,6 +25,7 @@ import static org.nuiton.i18n.I18n._; +import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.Insets; import java.awt.Point; @@ -33,9 +34,10 @@ import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.beans.Introspector; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.UUID; import javax.swing.JLabel; @@ -705,14 +707,17 @@ * @param view view */ public void displayCompareNumberCatchGraph(ControlView view) { - ProjectService projectService = view.getContextValue(ProjectService.class); Project project = view.getContextValue(Project.class); - List<String> species = projectService.getProjectSpecies(project.getControl()); + ChartService chartService = view.getContextValue(ChartService.class); + Map<String, JFreeChart> charts = chartService.getCompareCatchLengthGraph(project, project.getControl()); + List<String> species = new ArrayList<String>(charts.keySet()); + ControlGraphDialog dialog = new ControlGraphDialog(view); dialog.setHandler(this); + dialog.setContextValue(charts); dialog.getSpecyComboModel().setSpecy(species); - updateCompareNumberCatchGraph(dialog); + //updateCompareNumberCatchGraph(dialog); dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setVisible(true); @@ -734,12 +739,14 @@ * @param view view */ public void updateCompareNumberCatchGraph(ControlGraphDialog view) { - ChartService chartService = view.getContextValue(ChartService.class); - Project project = view.getContextValue(Project.class); String specyName = (String)view.getSpecyComboModel().getSelectedItem(); - JFreeChart chart = chartService.getCompareCatchLengthGraph(project, project.getControl(), specyName); + Map<String, JFreeChart> charts = view.getContextValue(Map.class); + JFreeChart chart = charts.get(specyName); view.getControlGraphPanel().removeAll(); - view.getControlGraphPanel().add(new ChartPanel(chart)); + ChartPanel chartPanel = new ChartPanel(chart); + //chartPanel.setMinimumDrawWidth(1000); + //chartPanel.setMinimumDrawHeight(800); + view.getControlGraphPanel().add(chartPanel, BorderLayout.CENTER); view.getControlGraphPanel().validate(); }