Author: chatellier Date: 2010-11-03 13:45:45 +0000 (Wed, 03 Nov 2010) New Revision: 154 Log: Selection de l'espece sur laquelle porte le graphique. Changement du type de graphique en ligne (xy). Ajoute de la deuxieme s?\195?\169rie taille sur le graphique des captures. Added: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/SpecyComboModel.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java trunk/coser-business/src/main/resources/i18n/coser-business-en_GB.properties 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 trunk/coser-ui/src/main/resources/i18n/coser-ui-en_GB.properties trunk/coser-ui/src/main/resources/i18n/coser-ui-fr_FR.properties 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-03 11:27:39 UTC (rev 153) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ChartService.java 2010-11-03 13:45:45 UTC (rev 154) @@ -25,9 +25,11 @@ package fr.ifremer.coser.services; -import java.util.ArrayList; +import static org.nuiton.i18n.I18n._; + +import java.text.NumberFormat; +import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; @@ -38,17 +40,21 @@ import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; +import org.jfree.chart.labels.CategoryToolTipGenerator; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; +import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 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.chart.renderer.category.LineAndShapeRenderer; import org.jfree.data.category.DefaultCategoryDataset; import fr.ifremer.coser.CoserBusinessConfig; +import fr.ifremer.coser.CoserConstants.Category; import fr.ifremer.coser.bean.Control; import fr.ifremer.coser.bean.Project; import fr.ifremer.coser.data.Catch; +import fr.ifremer.coser.data.Length; /** * Validation service. @@ -69,34 +75,37 @@ this.config = config; } - public JFreeChart getCatchGraph(Project project, Control control) { - + /** + * Retourne le graph de comparaison entre le nombre de capture et + * le nombre dans les tailles. + * + * @param project projet + * @param control control + * @param specyName sepcy name + * @return chart + */ + public JFreeChart getCompareCatchLengthGraph(Project project, Control control, String specyName) { + + // look for data (data summed over catch) + SortedMap<String, Double> catchForYears = new TreeMap<String, Double>(); 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])) { + 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 (dataForYears.containsKey(year)) { - Double oldValue = dataForYears.get(year); + if (catchForYears.containsKey(year)) { + Double oldValue = catchForYears.get(year); Double newValue = oldValue + nombreDouble; - dataForYears.put(year, newValue); + catchForYears.put(year, newValue); } else { - dataForYears.put(year, nombreDouble); + catchForYears.put(year, nombreDouble); } } catch (NumberFormatException ex) { @@ -107,43 +116,72 @@ } } - List<SortedMap<String, Double>> results = new ArrayList<SortedMap<String,Double>>(); - results.add(dataForYears); + // look for data (data summed over length) + SortedMap<String, Double> lengthForYears = new TreeMap<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); + } + } + catch (NumberFormatException ex) { + if (log.isWarnEnabled()) { + log.warn("Can't parse " + nombreValue + " as double"); + } + } + } + } + + 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; } - protected JFreeChart displayGraph(List<SortedMap<String, Double>> results) { - + protected JFreeChart displayGraph(Map<String, SortedMap<String, Double>> resultSeries) { + // 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()); + 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("Année"); + + CategoryAxis categoryAxis = new CategoryAxis(_("coser.business.common.year")); categoryAxis.setCategoryMargin(0); // label horizontaux //categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); - ValueAxis valueAxis = new NumberAxis("Nombre"); + ValueAxis valueAxis = new NumberAxis(_("coser.business.common.number")); valueAxis.setUpperMargin(0.1); - //XYSplineRenderer renderer = new XYSplineRenderer(); - //AreaRenderer renderer = new AreaRenderer(); - //StackedBarRenderer renderer = new StackedBarRenderer(); - - CategoryItemRenderer renderer = new AreaRenderer(); + CategoryItemRenderer renderer = new LineAndShapeRenderer(); StandardCategoryItemLabelGenerator itemLabelGenerator = new StandardCategoryItemLabelGenerator(); renderer.setBaseItemLabelGenerator(itemLabelGenerator); renderer.setBaseItemLabelsVisible(true); + + CategoryToolTipGenerator cttg = new StandardCategoryToolTipGenerator("{0}: {2}", NumberFormat.getInstance()); + renderer.setBaseToolTipGenerator(cttg); CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer); plot.setOrientation(PlotOrientation.VERTICAL); - JFreeChart chart = new JFreeChart("Captures", JFreeChart.DEFAULT_TITLE_FONT, - plot, true); + JFreeChart chart = new JFreeChart(_("coser.business.chart.compareCatchLengthNumberTitle"), + JFreeChart.DEFAULT_TITLE_FONT, plot, true); return chart; } Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-03 11:27:39 UTC (rev 153) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-03 13:45:45 UTC (rev 154) @@ -30,6 +30,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -801,37 +802,63 @@ List<Integer> result = new ArrayList<Integer>(years); return result; } - + /** + * Get species name in project. + * + * Used in control ui (graph). + * + * @param container data container + * @return species + */ + public List<String> getProjectSpecies(AbstractDataContainer container) { + List<String> result = new ArrayList<String>(); + + Iterator<String[]> itTuple = container.getCatch().iterator(); + itTuple.next(); // skip header + while (itTuple.hasNext()) { + String[] tuple = itTuple.next(); + String species = tuple[Catch.INDEX_SPECIES]; + if (!result.contains(species)) { + result.add(species); + } + } + + Collections.sort(result); + return result; + } + + /** * Get species name in project with data in [{@code beginYear}-{@code endYear}]. * * Used in selection ui. * - * @param selection selection - * @param beginYear begin year - * @param endYear end year - * @return zones + * @param container data container + * @param beginYear begin year (can be null) + * @param endYear end year (can be null) + * @return species */ - public List<Specy> getProjectSpecies(Selection selection, Integer beginYear, Integer endYear) { + public List<Specy> getProjectSpecies(AbstractDataContainer container, Integer beginYear, Integer endYear) { SortedMap<String, Specy> result = new TreeMap<String, Specy>(); - Iterator<String[]> itTuple = selection.getCatch().iterator(); + Iterator<String[]> itTuple = container.getCatch().iterator(); itTuple.next(); // skip header while (itTuple.hasNext()) { String[] tuple = itTuple.next(); + String species = tuple[Catch.INDEX_SPECIES]; + // "Campagne","Annee","Trait","Espece","Nombre","Poids" String annee = tuple[Catch.INDEX_YEAR]; try { int intAnnee = Integer.parseInt(annee); if (intAnnee >= beginYear && intAnnee <= endYear) { - String species = tuple[Catch.INDEX_SPECIES]; if (!result.containsKey(species)) { Specy specy = new Specy(); specy.setName(species); result.put(species, specy); - } + } } } catch (NumberFormatException ex) { Modified: trunk/coser-business/src/main/resources/i18n/coser-business-en_GB.properties =================================================================== --- trunk/coser-business/src/main/resources/i18n/coser-business-en_GB.properties 2010-11-03 11:27:39 UTC (rev 153) +++ trunk/coser-business/src/main/resources/i18n/coser-business-en_GB.properties 2010-11-03 13:45:45 UTC (rev 154) @@ -31,6 +31,9 @@ coser.business.category.length=Length coser.business.category.reftax.species= coser.business.category.strata=Stata +coser.business.chart.compareCatchLengthNumberTitle= +coser.business.common.number= +coser.business.common.year= coser.business.control.error.duplicatedLine= coser.business.control.error.minObservationCount= coser.business.control.error.minObservationCountDetail= 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-03 11:27:39 UTC (rev 153) +++ trunk/coser-business/src/main/resources/i18n/coser-business-fr_FR.properties 2010-11-03 13:45:45 UTC (rev 154) @@ -31,6 +31,9 @@ coser.business.category.length=Tailles coser.business.category.reftax.species=Reftax (esp\u00E8ce) coser.business.category.strata=Strates +coser.business.chart.compareCatchLengthNumberTitle=Comparaison des nombre dans Capture et Taille +coser.business.common.number=Nombre +coser.business.common.year=Ann\u00E9e coser.business.control.error.duplicatedLine=Ligne en doublon coser.business.control.error.minObservationCount=Nombre minimal d'observation non atteint coser.business.control.error.minObservationCountDetail=Nombre minimal d'observation non atteint (%s) \: %.2f 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-03 11:27:39 UTC (rev 153) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/services/ChartServiceTest.java 2010-11-03 13:45:45 UTC (rev 154) @@ -25,9 +25,7 @@ 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; @@ -47,7 +45,6 @@ * Last update : $Date$ * By : $Author$ */ - at Ignore public class ChartServiceTest extends CoserTestAbstract { protected ProjectService projectService; @@ -62,7 +59,7 @@ @Test public void testCatchChart() throws CoserBusinessException { Project project = createTestProject(projectService); - JFreeChart chart = chartService.getCatchGraph(project, project.getControl()); + JFreeChart chart = chartService.getCompareCatchLengthGraph(project, project.getControl(), "COSER_SPECIES1"); JDialog f = new JDialog(); f.setModal(true); 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-03 11:27:39 UTC (rev 153) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlGraphDialog.jaxx 2010-11-03 13:45:45 UTC (rev 154) @@ -24,8 +24,22 @@ --> <JDialog title="coser.ui.control.graphtitle"> - <JScrollPane> - <JPanel id="controlGraphPanel" /> - </JScrollPane> + <ControlHandler id="handler" javaBean="null" /> + <Table> + <row> + <cell> + <JLabel text="coser.ui.control.graph.specy"/> + </cell> + <cell fill="horizontal"> + <SpecyComboModel id="specyComboModel" /> + <JComboBox model="{specyComboModel}" onActionPerformed="getHandler().updateGraphSpecy(this)" /> + </cell> + </row> + <row fill="both" weightx="1" weighty="1" columns="2"> + <cell> + <JPanel id="controlGraphPanel" /> + </cell> + </row> + </Table> </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-11-03 11:27:39 UTC (rev 153) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlHandler.java 2010-11-03 13:45:45 UTC (rev 154) @@ -527,17 +527,35 @@ * @param view view */ public void displayGraph(ControlView view) { - ChartService chartService = view.getContextValue(ChartService.class); + ProjectService projectService = view.getContextValue(ProjectService.class); Project project = view.getContextValue(Project.class); - JFreeChart chart = chartService.getCatchGraph(project, project.getControl()); + List<String> species = projectService.getProjectSpecies(project.getControl()); + ControlGraphDialog dialog = new ControlGraphDialog(view); - dialog.getControlGraphPanel().add(new ChartPanel(chart)); + dialog.setHandler(this); + dialog.getSpecyComboModel().setSpecy(species); + updateGraphSpecy(dialog); dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setVisible(true); } /** + * Met a jour le graphique lorsque la selection de l'espece change. + * + * @param view view + */ + public void updateGraphSpecy(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); + view.getControlGraphPanel().removeAll(); + view.getControlGraphPanel().add(new ChartPanel(chart)); + view.getControlGraphPanel().validate(); + } + + /** * Valide les modifications faites sur le bean actuellement edité. * * @param view view Added: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/SpecyComboModel.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/SpecyComboModel.java (rev 0) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/SpecyComboModel.java 2010-11-03 13:45:45 UTC (rev 154) @@ -0,0 +1,97 @@ +/* + * #%L + * Coser :: UI + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 Ifremer, Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +package fr.ifremer.coser.ui.control; + +import java.util.List; + +import javax.swing.AbstractListModel; +import javax.swing.ComboBoxModel; + +/** + * Model de selection d'especu utilise sur la dialog de graph pour + * changer l'espece sur laquelle le graphique porte. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class SpecyComboModel extends AbstractListModel implements ComboBoxModel { + + /** serialVersionUID. */ + private static final long serialVersionUID = -4769109927915812519L; + + protected List<String> species; + + protected Object selectedItem; + + public void setSpecy(List<String> species) { + this.species = species; + selectedItem = species.get(0); + fireContentsChanged(this, 0, species.size()); + } + + /* + * @see javax.swing.ListModel#getSize() + */ + @Override + public int getSize() { + int result = 0; + if (species != null) { + result = species.size(); + } + return result; + } + + /* + * @see javax.swing.ListModel#getElementAt(int) + */ + @Override + public Object getElementAt(int index) { + return species.get(index); + } + + public int getIndexOf(Object o) { + return species.indexOf(o); + } + + /* + * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object) + */ + @Override + public void setSelectedItem(Object anItem) { + this.selectedItem = anItem; + } + + /* + * @see javax.swing.ComboBoxModel#getSelectedItem() + */ + @Override + public Object getSelectedItem() { + return selectedItem; + } +} Property changes on: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/SpecyComboModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/coser-ui/src/main/resources/i18n/coser-ui-en_GB.properties =================================================================== --- trunk/coser-ui/src/main/resources/i18n/coser-ui-en_GB.properties 2010-11-03 11:27:39 UTC (rev 153) +++ trunk/coser-ui/src/main/resources/i18n/coser-ui-en_GB.properties 2010-11-03 13:45:45 UTC (rev 154) @@ -23,6 +23,7 @@ coser.ui.control.global.done=\u2026 coser.ui.control.global.level=Type coser.ui.control.global.message=Message +coser.ui.control.graph.specy=Specy \: coser.ui.control.graphtitle=Graph coser.ui.control.replace.find=Find \: coser.ui.control.replace.inField=In field \: Modified: trunk/coser-ui/src/main/resources/i18n/coser-ui-fr_FR.properties =================================================================== --- trunk/coser-ui/src/main/resources/i18n/coser-ui-fr_FR.properties 2010-11-03 11:27:39 UTC (rev 153) +++ trunk/coser-ui/src/main/resources/i18n/coser-ui-fr_FR.properties 2010-11-03 13:45:45 UTC (rev 154) @@ -23,6 +23,7 @@ coser.ui.control.global.done=\u2026 coser.ui.control.global.level=Type coser.ui.control.global.message=Message +coser.ui.control.graph.specy=Esp\u00E8ces \: coser.ui.control.graphtitle=Graphique coser.ui.control.replace.find=Chercher \: coser.ui.control.replace.inField=Dans le champ \: