Compare commits
25 Commits
08.04.26-1
...
d9303dd78a
| Author | SHA1 | Date | |
|---|---|---|---|
| d9303dd78a | |||
| 5ebe7d0449 | |||
| 577a8652ec | |||
| 4db6dd24df | |||
| d227208578 | |||
| 9c006f94fb | |||
| d0a5315ca2 | |||
| 7b3e3f8829 | |||
| 3fd7ef15b4 | |||
| 1b9efaa239 | |||
| 96cdac60eb | |||
| 82901f623b | |||
| 8ac87e9edb | |||
| e48826b765 | |||
| 7800a92dae | |||
| 90ebe64393 | |||
| 35274a963b | |||
| 7305393f6c | |||
| c13bb289ed | |||
| c9dbe7dc3b | |||
| a56c59456b | |||
| c8ee9b0a37 | |||
| dc2ea03224 | |||
| 519ce77813 | |||
| 110784a530 |
@@ -18,5 +18,5 @@ Les fichiers de tests se trouvent dans le dossier ``tests``.
|
||||
Actuellement, nous avons terminé la complétion/nettoyage par Moyenne/Régression Linéaire.
|
||||
Nous allons donc voir pour l'interface graphique.
|
||||
|
||||
Le fichier qui permet de tester l'ouverture du fichier Test2 pour l'évaluation est le fichier ``ecoparasite.LectureEvaltest2``.
|
||||
Ce fichier a été réalisé par Sébastien BOUSQUET.
|
||||
Le fichier qui permet de tester l'ouverture du fichier Test3 pour l'évaluation est le fichier ``ecoparasite.LectureEval``.
|
||||
Ce fichier a été réalisé par Benjamin THOREL.
|
||||
102
UML/classes.puml
102
UML/classes.puml
@@ -3,6 +3,7 @@
|
||||
namespace ecoparasite {
|
||||
|
||||
class Application {
|
||||
+ {static} main
|
||||
}
|
||||
|
||||
namespace ecoparasite.input {
|
||||
@@ -119,34 +120,109 @@ namespace ecoparasite {
|
||||
|
||||
namespace ecoparasite.completion {
|
||||
class Completion {
|
||||
+ {static} completeColumnsMoyenne
|
||||
+ {static} completeColumnsLinear
|
||||
+ {static} completeColumnsMoyenne()
|
||||
+ {static} completeColumnsLinear()
|
||||
}
|
||||
}
|
||||
|
||||
namespace ecoparasite.nettoyage {
|
||||
class Nettoyage {
|
||||
+ {static} nettoieColumnsMoyenne
|
||||
+ {static} nettoieColumnsLinear
|
||||
+ {static} nettoieColumns()
|
||||
}
|
||||
}
|
||||
|
||||
namespace ecoparasite.unknown {
|
||||
|
||||
note top of ecoparasite.unknown : Ce paquet est temporaire pour des classes / interfaces qui devront avoir plus de déclinaisons.
|
||||
|
||||
class DataCleaner {
|
||||
+ DataCleaner()
|
||||
+ String toString()
|
||||
namespace ecoparasite.representation {
|
||||
class ValeursXY {
|
||||
- double x
|
||||
- double y
|
||||
+ {static} HashSet<ValeursXY> convertToXY()
|
||||
}
|
||||
}
|
||||
|
||||
interface DataCompletion {
|
||||
+ void exception()
|
||||
namespace ecoparasite.svg {
|
||||
class SVGFactory {
|
||||
+ {static} createSVG()
|
||||
+ {static} createSVGCode()
|
||||
+ {static} createFile()
|
||||
}
|
||||
|
||||
class Coordonnees {
|
||||
- double x
|
||||
- double y
|
||||
}
|
||||
|
||||
class SVGResizing {
|
||||
- double minX
|
||||
- double maxX
|
||||
- double minY
|
||||
- double maxY
|
||||
+ resize()
|
||||
}
|
||||
|
||||
class SVGBuilder {
|
||||
- ArrayList<Double> pointsX
|
||||
- ArrayList<Double> pointsY
|
||||
- double minPointsX
|
||||
- double minPointsY
|
||||
- double maxPointsX
|
||||
- double maxPointsY
|
||||
+ SVGBuilder()
|
||||
+ getResizer()
|
||||
+ buildAll()
|
||||
+ buildAxes()
|
||||
+ buildXTicks()
|
||||
+ buildYTicks()
|
||||
+ buildPoints()
|
||||
+ buildRegression()
|
||||
- getBeginAxeX()
|
||||
- getBeginAxeY()
|
||||
+ {static} calcPointsAxes()
|
||||
+ {static} niceStep()
|
||||
+ {static} roundMin()
|
||||
+ {static} roundMax()
|
||||
}
|
||||
|
||||
exception IncorrectAxesPointsException {
|
||||
}
|
||||
|
||||
SVGBuilder o--> SVGResizing : - resizer
|
||||
|
||||
}
|
||||
|
||||
namespace ecoparasite.svg.elements {
|
||||
|
||||
class ElementsFactory {
|
||||
+ {static} SVGAxes()
|
||||
}
|
||||
|
||||
abstract class Element {
|
||||
+ {abstract} toSVG()
|
||||
}
|
||||
|
||||
Element o--> ecoparasite.svg.Coordonnees : # coordonnees
|
||||
|
||||
class Circle extends Element {
|
||||
- int rayon
|
||||
- String color
|
||||
}
|
||||
|
||||
class Line extends Element {
|
||||
- int lineWidth
|
||||
- String color
|
||||
}
|
||||
|
||||
class Text extends Element {
|
||||
- String text
|
||||
- String color
|
||||
- int size
|
||||
}
|
||||
|
||||
Line o--> ecoparasite.svg.Coordonnees : # coordonneesB
|
||||
}
|
||||
|
||||
}
|
||||
Exception <|-- InputFileException
|
||||
Exception <|-- RawDataOverflow
|
||||
Exception <|-- IncorrectAxesPointsException
|
||||
|
||||
@enduml
|
||||
@@ -1,7 +1,70 @@
|
||||
package ecoparasite;
|
||||
|
||||
import ecoparasite.completion.Completion;
|
||||
import ecoparasite.input.InputFactory;
|
||||
import ecoparasite.input.InputFileException;
|
||||
import ecoparasite.input.RawData;
|
||||
import ecoparasite.input.RawDataOverflow;
|
||||
import ecoparasite.nettoyage.Nettoyage;
|
||||
import ecoparasite.poisson.MackerelSerra;
|
||||
import ecoparasite.poisson.Poisson;
|
||||
import ecoparasite.representation.ValeursXY;
|
||||
import ecoparasite.svg.IncorrectAxesPointsException;
|
||||
import ecoparasite.svg.SVGBuilder;
|
||||
import ecoparasite.svg.SVGFactory;
|
||||
import ecoparasite.svg.elements.Element;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World");
|
||||
|
||||
public static void main(String[] args) throws InputFileException, RawDataOverflow {
|
||||
|
||||
RawData rawMackerel = InputFactory.readData("test2.csv", ",");
|
||||
|
||||
HashSet<Poisson> mackerelSet = MackerelSerra.parse(rawMackerel);
|
||||
|
||||
System.out.println( "Avant nettoyage et complétion" );
|
||||
System.out.println( mackerelSet );
|
||||
|
||||
Function<Poisson,Double> getLength = Poisson::getLength;
|
||||
Function<Poisson,Double> getInfes = Poisson::getInfestation;
|
||||
BiConsumer<Poisson,Double> setInfes = Poisson::setInfestation;
|
||||
|
||||
mackerelSet = Nettoyage.nettoieColumns( mackerelSet, Poisson::getLength, Poisson::setLength, false );
|
||||
mackerelSet = Completion.completeColumnsMoyenne( mackerelSet, Poisson::getLength, Poisson::setLength );
|
||||
|
||||
System.out.println( "Après Nettoyage et complétion de la longueur du poisson." );
|
||||
System.out.println( mackerelSet );
|
||||
|
||||
mackerelSet = Nettoyage.nettoieColumns( mackerelSet, getInfes, setInfes, false );
|
||||
mackerelSet = Completion.completeColumnsLinear( mackerelSet, getLength, getInfes, setInfes );
|
||||
|
||||
System.out.println( "Après Nettoyage et complétion du taux d'infestation des poissons" );
|
||||
System.out.println( mackerelSet );
|
||||
|
||||
// Conversion des valeurs.
|
||||
HashSet<ValeursXY> mackerelXY = ValeursXY.convertToXY( mackerelSet, getLength, getInfes );
|
||||
HashMap<String, ArrayList<Double>> axes = SVGBuilder.calcPointAxes( mackerelXY );
|
||||
System.out.println( axes );
|
||||
|
||||
SVGBuilder axesInstance;
|
||||
try {
|
||||
axesInstance = new SVGBuilder(axes);
|
||||
} catch (IncorrectAxesPointsException e) {
|
||||
System.out.println( "Mauvais format communiqué" );
|
||||
return;
|
||||
}
|
||||
|
||||
// Sauvegarde pour plus tard.
|
||||
double[] ABCoef = Completion.getLinearCoef(mackerelSet, getLength, getInfes);
|
||||
|
||||
ArrayList<Element> SVGElements = axesInstance.buildAll( "Length (mm)", "Taux d'infestation", mackerelXY, ABCoef[0], ABCoef[1] );
|
||||
SVGFactory.createSVG( SVGElements );
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
package ecoparasite;
|
||||
|
||||
import ecoparasite.completion.Completion;
|
||||
import ecoparasite.input.InputFactory;
|
||||
import ecoparasite.input.InputFileException;
|
||||
import ecoparasite.input.RawData;
|
||||
import ecoparasite.input.RawDataOverflow;
|
||||
import ecoparasite.nettoyage.Nettoyage;
|
||||
import ecoparasite.poisson.Poisson;
|
||||
import ecoparasite.population.Population;
|
||||
import ecoparasite.population.PopulationArgInterval;
|
||||
import ecoparasite.population.PopulationArgs;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class LectureEval {
|
||||
|
||||
public static HashSet<Population> parseEval( RawData popRaw ){
|
||||
|
||||
HashSet<Population> popEspece = new HashSet<>();
|
||||
|
||||
int index = 1;
|
||||
try {
|
||||
while(true){
|
||||
HashMap<String,String> fields = popRaw.getEntry(index);
|
||||
|
||||
String espece = fields.get("Espèce");
|
||||
|
||||
Population population = new Population(espece);
|
||||
|
||||
if( population.getTotal() == null ){
|
||||
population.setTotal( new PopulationArgs() );
|
||||
}
|
||||
for( String k: fields.keySet() ){
|
||||
if( k.equals("Espèce") )
|
||||
continue;
|
||||
|
||||
LectureEval.applyValueForPopEval( population.getTotal(), k, fields.get(k) );
|
||||
}
|
||||
|
||||
popEspece.add(population);
|
||||
index++;
|
||||
}
|
||||
} catch (RawDataOverflow e) {
|
||||
// Fin de la liste.
|
||||
}
|
||||
|
||||
return popEspece;
|
||||
}
|
||||
|
||||
public static void applyValueForPopEval( PopulationArgs popArgs, String column, String value ){
|
||||
|
||||
if( value == null || value == "" ) // On n'ajoute pas les valeurs nulles.
|
||||
return;
|
||||
|
||||
switch (column){
|
||||
case "zone":
|
||||
popArgs.setZone(value);
|
||||
break;
|
||||
case "N":
|
||||
popArgs.setNumber( Integer.parseInt(value) );
|
||||
break;
|
||||
case "Prevalence":
|
||||
popArgs.setPrevalence(PopulationArgInterval.fromString(value));
|
||||
break;
|
||||
case "LT mm":
|
||||
popArgs.setLength(PopulationArgInterval.fromString(value));
|
||||
break;
|
||||
case "Masse g":
|
||||
popArgs.setWidth(PopulationArgInterval.fromString(value));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RawDataOverflow {
|
||||
|
||||
RawData popRaw;
|
||||
try {
|
||||
popRaw = InputFactory.readData("test3.csv", "," );
|
||||
} catch(InputFileException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<Population> pop = parseEval(popRaw);
|
||||
|
||||
// System.out.println( popRaw.getEntry(1) );
|
||||
|
||||
for( Population p: pop){
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
// Complétion de la masse.
|
||||
Function<Population,Double> getWeight = population -> {
|
||||
return population.getTotal().getWidth() != null ? population.getTotal().getWidth().transformToDouble() : null;
|
||||
};
|
||||
BiConsumer<Population,Double> setWeight = (population, aDouble) -> {
|
||||
population.getTotal().setWidth(new PopulationArgInterval(aDouble,aDouble));
|
||||
};
|
||||
|
||||
// Complétion de la masse.
|
||||
pop = Completion.completeColumnsMoyenne(pop, getWeight, setWeight);
|
||||
System.out.println("---");
|
||||
for( Population p: pop){
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
// Nettoyage de la masse.
|
||||
pop = Nettoyage.nettoieColumnsMoyenne(pop, getWeight, setWeight, false);
|
||||
System.out.println("---");
|
||||
for( Population p: pop){
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -139,4 +139,25 @@ public class Completion {
|
||||
return meanY - valueA * meanX;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de retourner les coefficients de la regression linéaire.
|
||||
* @param list La liste de données de type T.
|
||||
* @param getX Le Getter des valeurs X.
|
||||
* @param getY Le Getter des valeurs Y.
|
||||
* @return Les coefficients a et b, [0] = a, [1] = b
|
||||
* @param <T> Le type de données cobaye.
|
||||
* @param <V> Le type de valeurs de retour numérique.
|
||||
*/
|
||||
public static <T,V extends Number> double[] getLinearCoef( HashSet<T> list, Function<T,V> getX, Function<T,V> getY ){
|
||||
|
||||
double meanX = calculateMean(list, getX);
|
||||
double meanY = calculateMean(list, getY);
|
||||
|
||||
double a = calculateLinearA(list,getX,getY,meanX,meanY);
|
||||
double b = calculateLinearB(meanX,meanY,a);
|
||||
|
||||
return new double[]{a,b};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,43 @@ import java.util.function.Function;
|
||||
*/
|
||||
public class Nettoyage {
|
||||
|
||||
/**
|
||||
* Permet de remplacer les valeurs abérrantes d'un paramètre d'un HashSet par null.
|
||||
* Exemple d'utilisation : T = Poisson, V = Double, getValue = Poisson::getInfestation, setValue = Poisson::setInfestation.
|
||||
*
|
||||
* @param list La liste de données cobaye.
|
||||
* @param getValue La fonction (Getter) qui permet d'obtenir la valeur que l'on veut vérifier
|
||||
* @param setValue La fonction (Setter) qui permet de remplacer la valeur si null.
|
||||
* @param allowNegative Savoir si une valeur négative est forcément aberrant.
|
||||
* @return Le HashSet avec les valeurs remplacés.
|
||||
* @param <T> Le type de données cobaye. Exemple : Poisson, Population
|
||||
* @param <V> Le type de la donnée à vérifier, doit être un Wrapper Number. Exemple : Double.
|
||||
*/
|
||||
public static <T,V extends Number> HashSet<T> nettoieColumns(HashSet<T> list, Function<T,V> getValue, BiConsumer<T,V> setValue, boolean allowNegative ){
|
||||
|
||||
ArrayList<Double> array = new ArrayList<>();
|
||||
for ( T item : list) {
|
||||
if (getValue.apply(item)!= null){ //Test des valeurs null pour les Tests Unitaires. Je ne devrais pas en avoir.
|
||||
array.add(getValue.apply(item).doubleValue());
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(array);
|
||||
|
||||
int quartIndex = array.size()/4;
|
||||
Double firstQuart = array.get(quartIndex);
|
||||
Double thirdQuart = array.get(quartIndex *3);
|
||||
Double IQR = thirdQuart - firstQuart;
|
||||
|
||||
for(T item : list){
|
||||
if( getValue.apply(item) == null || getValue.apply(item).doubleValue() < firstQuart - (IQR * 1.5) || getValue.apply(item).doubleValue() > thirdQuart + (IQR * 1.5) || ( !allowNegative && getValue.apply(item).doubleValue() < 0 ) ){
|
||||
setValue.accept( item, null);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de remplacer les valeurs abérrantes d'un paramètre d'un HashSet par la moyenne des autres valeurs (non nulles).
|
||||
* Exemple d'utilisation : T = Poisson, V = Double, getValue = Poisson::getInfestation, setValue = Poisson::setInfestation.
|
||||
@@ -26,6 +63,7 @@ public class Nettoyage {
|
||||
* @param <T> Le type de données cobaye. Exemple : Poisson, Population
|
||||
* @param <V> Le type de la donnée à vérifier, doit être un Wrapper Number. Exemple : Double.
|
||||
*/
|
||||
/*
|
||||
public static <T,V extends Number> HashSet<T> nettoieColumnsMoyenne(HashSet<T> list, Function<T,V> getValue, BiConsumer<T,V> setValue, boolean allowNegative ){
|
||||
|
||||
Double mean = Completion.calculateMean(list, getValue);
|
||||
@@ -52,6 +90,7 @@ public class Nettoyage {
|
||||
|
||||
return list;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Polymorphisme de la fonction précédente. Autorise les valeurs abérrantes à être négative.
|
||||
@@ -62,10 +101,10 @@ public class Nettoyage {
|
||||
* @param <T>
|
||||
* @param <V>
|
||||
*
|
||||
* @see Nettoyage::nettoieColumnsMoyenne
|
||||
* @see Nettoyage::nettoieColumns
|
||||
*/
|
||||
public static <T,V extends Number> HashSet<T> nettoieColumnsMoyenne(HashSet<T> list, Function<T,V> getValue, BiConsumer<T,V> setValue){
|
||||
return nettoieColumnsMoyenne(list, getValue, setValue, true);
|
||||
public static <T,V extends Number> HashSet<T> nettoieColumns(HashSet<T> list, Function<T,V> getValue, BiConsumer<T,V> setValue){
|
||||
return nettoieColumns(list, getValue, setValue, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,6 +120,7 @@ public class Nettoyage {
|
||||
* @param <T> Le type de données cobaye. Exemple : Poisson, Population
|
||||
* @param <V> Le type de la donnée à vérifier, doit être un Wrapper Number. Exemple : Double.
|
||||
*/
|
||||
/*
|
||||
public static <T,V extends Number> HashSet<T> nettoieColumnsLinear(HashSet<T> list, Function<T,V> getX, Function<T,V> getY, BiConsumer<T,V> setY, boolean allowNegative ){
|
||||
|
||||
double meanX = Completion.calculateMean(list, getX);
|
||||
@@ -112,6 +152,7 @@ public class Nettoyage {
|
||||
|
||||
return list;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Polymorphisme de la fonction nettoyage de colonne linéaire avec par défaut, l'autorisation des valeurs négatives.
|
||||
@@ -123,7 +164,9 @@ public class Nettoyage {
|
||||
* @param <T>
|
||||
* @param <V>
|
||||
*/
|
||||
/*
|
||||
public static <T,V extends Number> HashSet<T> nettoieColumnsLinear(HashSet<T> list, Function<T,V> getX, Function<T,V> getY, BiConsumer<T,V> setY){
|
||||
return nettoieColumnsLinear(list, getX, getY, setY, true);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class Poisson{
|
||||
|
||||
/**
|
||||
* Setter de l'attribut length
|
||||
* @param length le Double de la nouvelle valeur de length
|
||||
* @param length le Double de la nouvelle valeur de la length
|
||||
*/
|
||||
public void setLength(Double length) {
|
||||
this.length = length;
|
||||
@@ -102,4 +102,6 @@ public class Poisson{
|
||||
String result = "[ %5s : %4f mm, %4f g, %4f taux d'infestation ]";
|
||||
return String.format(result, this.getClass().getSimpleName(), this.getLength(), this.getWeight(), this.getInfestation() );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -181,7 +181,10 @@ public class PopulationArgs {
|
||||
|
||||
public void setZone(String zone) { this.zone = zone; }
|
||||
|
||||
|
||||
/**
|
||||
* Avoir sous forme de string les données de nos arguments de population.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String toString(){
|
||||
return String.format( "Année: %d, N: %d, Length: %f, Width: %f, Prevalence: %f, IC: %f, Intensity: %f, Abondance: %f, Zone: %s",
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package ecoparasite.representation;
|
||||
|
||||
public class ValeurXY {
|
||||
}
|
||||
57
src/ecoparasite/representation/ValeursXY.java
Normal file
57
src/ecoparasite/representation/ValeursXY.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package ecoparasite.representation;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Représente un tuple de Valeurs X/Y pour nos données.
|
||||
* Ce type de données est utilisé lors de la conversion entre nos données et la partie SVG.
|
||||
*/
|
||||
public class ValeursXY {
|
||||
|
||||
private double x;
|
||||
private double y;
|
||||
|
||||
/**
|
||||
* Constructeur
|
||||
* @param x Notre valeur correspondant à l'axe X.
|
||||
* @param y Notre valeur correspondant à l'axe Y.
|
||||
*/
|
||||
public ValeursXY(double x, double y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
public void setX(double x) {
|
||||
this.x = x;
|
||||
}
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
public void setY(double y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de convertir un type de données en valeursXY.
|
||||
* @param list Notre liste de données de type T.
|
||||
* @param getX Notre getter pour notre valeur symbolisant X.
|
||||
* @param getY Notre getter pour notre valeur symbolisant Y.
|
||||
* @return Un HashSet de valeursXY.
|
||||
* @param <T> Le type de données qui vont être converties.
|
||||
* @param <V> Le type de données retournées comme un nombre.
|
||||
*/
|
||||
public static <T,V extends Number> HashSet<ValeursXY> convertToXY(HashSet<T> list, Function<T,V> getX, Function<T,V> getY){
|
||||
HashSet<ValeursXY> xy = new HashSet<ValeursXY>();
|
||||
for(T item : list){
|
||||
if(getX.apply(item) != null && getY.apply(item) != null){
|
||||
xy.add( new ValeursXY(getX.apply(item).doubleValue(), getY.apply(item).doubleValue()));
|
||||
}
|
||||
}
|
||||
return xy;
|
||||
}
|
||||
|
||||
}
|
||||
30
src/ecoparasite/svg/Coordonnees.java
Normal file
30
src/ecoparasite/svg/Coordonnees.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package ecoparasite.svg;
|
||||
|
||||
/**
|
||||
* Classe qui permet de stocker les coordonnées utilisées pour le positionnement sur le fichier SVG.
|
||||
*/
|
||||
public class Coordonnees {
|
||||
|
||||
private double x;
|
||||
private double y;
|
||||
|
||||
public Coordonnees(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
public void setX(double x) {
|
||||
this.x = x;
|
||||
}
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
public void setY(double y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
7
src/ecoparasite/svg/IncorrectAxesPointsException.java
Normal file
7
src/ecoparasite/svg/IncorrectAxesPointsException.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package ecoparasite.svg;
|
||||
|
||||
/**
|
||||
* Exception déclenchée lorsque le format des Axes est incorrect par rapport à ce que la fonction attend.
|
||||
*/
|
||||
public class IncorrectAxesPointsException extends Exception{
|
||||
}
|
||||
396
src/ecoparasite/svg/SVGBuilder.java
Normal file
396
src/ecoparasite/svg/SVGBuilder.java
Normal file
@@ -0,0 +1,396 @@
|
||||
package ecoparasite.svg;
|
||||
|
||||
import ecoparasite.representation.ValeursXY;
|
||||
import ecoparasite.svg.elements.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* Classe qui permet de construire les éléments présents sur le SVG.
|
||||
* Axes, Points, Regression, Ticks.
|
||||
*/
|
||||
public class SVGBuilder {
|
||||
|
||||
final int SIZE_TICK_TEXT = ElementsFactory.AXES_TEXT_SIZE - 3;
|
||||
|
||||
private ArrayList<Double> pointsX;
|
||||
private ArrayList<Double> pointsY;
|
||||
private Double offsetX;
|
||||
private Double offsetY;
|
||||
private SVGResizing resizer;
|
||||
|
||||
private Double minPointsX;
|
||||
private Double minPointsY;
|
||||
private Double maxPointsX;
|
||||
private Double maxPointsY;
|
||||
|
||||
/**
|
||||
* Constructeur.
|
||||
* Initialise les différentes variables tirées de axesPoints.
|
||||
* @param axesPoints Paramètre tiré de la fonction calcPointsAxes.
|
||||
* @throws IncorrectAxesPointsException Si le format de axesPoints est incorrect.
|
||||
*
|
||||
* @see SVGBuilder::calcPointsAxes
|
||||
*/
|
||||
public SVGBuilder(HashMap<String, ArrayList<Double>> axesPoints ) throws IncorrectAxesPointsException {
|
||||
|
||||
if( axesPoints.get("AxeX") == null || axesPoints.get("AxeY") == null || axesPoints.get("OffsetX") == null || axesPoints.get("OffsetY") == null ){
|
||||
throw new IncorrectAxesPointsException();
|
||||
}
|
||||
|
||||
this.pointsX = axesPoints.get("AxeX");
|
||||
this.pointsY = axesPoints.get("AxeY");
|
||||
this.offsetX = axesPoints.get("OffsetX").getFirst();
|
||||
this.offsetY = axesPoints.get("OffsetY").getFirst();
|
||||
|
||||
this.minPointsX = this.pointsX.getFirst();
|
||||
this.minPointsY = this.pointsY.getFirst();
|
||||
this.maxPointsX = this.pointsX.getLast();
|
||||
this.maxPointsY = this.pointsY.getLast();
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Double> getPointsX() {
|
||||
return pointsX;
|
||||
}
|
||||
|
||||
public ArrayList<Double> getPointsY() {
|
||||
return pointsY;
|
||||
}
|
||||
|
||||
public Double getOffsetX() {
|
||||
return offsetX;
|
||||
}
|
||||
|
||||
public Double getOffsetY() {
|
||||
return offsetY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet d'obtenir un resizer ou bien le crée s'il n'existe pas déjà.
|
||||
* @return
|
||||
*/
|
||||
public SVGResizing getResizer() {
|
||||
if( this.resizer == null ){
|
||||
this.resizer = new SVGResizing( this.minPointsX, this.minPointsY, this.maxPointsX, this.maxPointsY );
|
||||
}
|
||||
return resizer;
|
||||
}
|
||||
|
||||
public void setResizer(SVGResizing resizer) {
|
||||
this.resizer = resizer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de tout construire d'un seul coup.
|
||||
* @param XLabel Le nom de l'axe X.
|
||||
* @param YLabel Le nom de l'axe Y.
|
||||
* @param points Les points pour le nuage de points.
|
||||
* @param A Le coefficient A de la regression
|
||||
* @param B Le coefficient B de la regression
|
||||
* @return Une liste d'éléments pour la génération du SVG.
|
||||
*/
|
||||
public ArrayList<Element> buildAll(String XLabel, String YLabel, HashSet<ValeursXY> points, double A, double B){
|
||||
|
||||
ArrayList<Element> elements = new ArrayList<>();
|
||||
|
||||
elements.addAll(buildAxes(XLabel, YLabel));
|
||||
elements.addAll(buildXTicks());
|
||||
elements.addAll(buildYTicks());
|
||||
elements.addAll(buildPoints(points));
|
||||
elements.addAll(buildRegression(A,B));
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de construire les axes au format SVG.
|
||||
* @param XLabel Le nom de l'axe X.
|
||||
* @param YLabel Le nom de l'axe Y.
|
||||
* @return La liste d'éléments SVG pour les axes.
|
||||
*/
|
||||
public ArrayList<Element> buildAxes(String XLabel, String YLabel){
|
||||
|
||||
// Offsets/Configuration.
|
||||
final int OFFSET_TEXT_AXISX_X = -20;
|
||||
final int OFFSET_TEXT_AXISX_Y = -10;
|
||||
final int OFFSET_TEXT_AXISY_X = +5;
|
||||
final int OFFSET_TEXT_AXISY_Y = +10;
|
||||
|
||||
ArrayList<Element> elements = new ArrayList<>();
|
||||
|
||||
double beginAxeX = getBeginAxeX();
|
||||
double beginAxeY = getBeginAxeY();
|
||||
|
||||
Coordonnees bottom = getResizer().resize( beginAxeX, minPointsY );
|
||||
Coordonnees top = getResizer().resize( beginAxeX, maxPointsY );
|
||||
Coordonnees left = getResizer().resize( minPointsX, beginAxeY );
|
||||
Coordonnees right = getResizer().resize( maxPointsX, beginAxeY );
|
||||
|
||||
// Axes
|
||||
elements.add( new Line( bottom, top, ElementsFactory.COLOR_WHITE, 2 ) );
|
||||
elements.add( new Line( left, right, ElementsFactory.COLOR_WHITE, 2 ) );
|
||||
|
||||
// Labels.
|
||||
elements.add( new Text(
|
||||
new Coordonnees( right.getX() + OFFSET_TEXT_AXISX_X, right.getY() + OFFSET_TEXT_AXISX_Y ),
|
||||
XLabel, ElementsFactory.COLOR_WHITE, ElementsFactory.AXES_TEXT_SIZE
|
||||
) );
|
||||
elements.add( new Text(
|
||||
new Coordonnees( top.getX() + OFFSET_TEXT_AXISY_X, top.getY() + OFFSET_TEXT_AXISY_Y ),
|
||||
YLabel, ElementsFactory.COLOR_WHITE, ElementsFactory.AXES_TEXT_SIZE
|
||||
));
|
||||
|
||||
return elements;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de construire les batons de l'axe X.
|
||||
* Utilise this.pointsX
|
||||
*
|
||||
* @return La liste d'éléments des points X.
|
||||
*/
|
||||
public ArrayList<Element> buildXTicks(){
|
||||
|
||||
// Offsets/Configuration.
|
||||
final int OFFSET_TICK = -5;
|
||||
final int OFFSET_TEXT_X = -10;
|
||||
final int OFFSET_TEXT_Y = +15;
|
||||
|
||||
ArrayList<Element> elements = new ArrayList<>();
|
||||
|
||||
double beginAxeY = getBeginAxeY();
|
||||
for( Double X : this.pointsX ){
|
||||
Coordonnees coords = getResizer().resize( X, beginAxeY );
|
||||
|
||||
elements.add(new Line(
|
||||
coords,
|
||||
new Coordonnees( coords.getX(), coords.getY() + OFFSET_TICK ),
|
||||
ElementsFactory.COLOR_WHITE, 1
|
||||
));
|
||||
elements.add(new Text(
|
||||
new Coordonnees( coords.getX() + OFFSET_TEXT_X, coords.getY() + OFFSET_TEXT_Y ),
|
||||
X.toString(),
|
||||
ElementsFactory.COLOR_WHITE,
|
||||
SIZE_TICK_TEXT
|
||||
));
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de construire les batons de l'axe Y.
|
||||
* Utilise this.pointsY
|
||||
*
|
||||
* @return La liste d'éléments des points Y.
|
||||
*/
|
||||
public ArrayList<Element> buildYTicks(){
|
||||
|
||||
final int OFFSET_TICK = +5;
|
||||
final int OFFSET_TEXT_X = -35;
|
||||
final int OFFSET_TEXT_Y = +5;
|
||||
|
||||
ArrayList<Element> elements = new ArrayList<>();
|
||||
|
||||
double beginAxeX = getBeginAxeX();
|
||||
for( Double Y : this.pointsY ){
|
||||
Coordonnees coords = getResizer().resize( beginAxeX, Y );
|
||||
|
||||
elements.add(new Line(
|
||||
new Coordonnees(coords.getX() + OFFSET_TICK, coords.getY() ),
|
||||
coords,
|
||||
ElementsFactory.COLOR_WHITE, 1
|
||||
));
|
||||
elements.add(new Text(
|
||||
new Coordonnees( coords.getX() + OFFSET_TEXT_X, coords.getY() + OFFSET_TEXT_Y ),
|
||||
Y.toString(),
|
||||
ElementsFactory.COLOR_WHITE,
|
||||
SIZE_TICK_TEXT
|
||||
));
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construire le nuage de points sur le SVG.
|
||||
* @param points La liste des points.
|
||||
* @return La liste des éléments pour le nuage de points SVG.
|
||||
*/
|
||||
public ArrayList<Element> buildPoints( HashSet<ValeursXY> points ){
|
||||
|
||||
ArrayList<Element> elements = new ArrayList<>();
|
||||
for( ValeursXY point : points ){
|
||||
Coordonnees coords = getResizer().resize( point.getX(), point.getY() );
|
||||
elements.add(new Circle(coords,3,ElementsFactory.COLOR_BLUE) );
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construire la droite de regression linéaire.
|
||||
* @param A Coefficient a
|
||||
* @param B Coefficient B
|
||||
* @return La liste des éléments pour la droite.
|
||||
*/
|
||||
public ArrayList<Element> buildRegression( double A, double B ){
|
||||
ArrayList<Element> elements = new ArrayList<>();
|
||||
|
||||
double y1 = A * minPointsX + B;
|
||||
double y2 = A * maxPointsX + B;
|
||||
|
||||
Coordonnees coords1 = getResizer().resize(minPointsX, y1);
|
||||
Coordonnees coords2 = getResizer().resize(maxPointsX, y2);
|
||||
|
||||
elements.add( new Line( coords1, coords2, ElementsFactory.COLOR_RED, 2 ) );
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de savoir à quel point l'axe X doit débuter.
|
||||
* @return Le double de début de l'axe X.
|
||||
*/
|
||||
private double getBeginAxeX(){
|
||||
return ( minPointsX > 0 ) ? minPointsX : ( maxPointsX < 0 ? maxPointsX : 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de savoir à quel point l'axe Y doit débuter.
|
||||
* @return Le double du début de l'axe Y.
|
||||
*/
|
||||
private double getBeginAxeY(){
|
||||
return ( minPointsY > 0 ) ? minPointsY : ( maxPointsY < 0 ? maxPointsY : 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de renvoyer des valeurs "clean" pour l'affichage des axes
|
||||
* @param h Contient les Coordonnées de chacun des points de nos données
|
||||
* @return une HashMap de String et de Hashset de Double.
|
||||
* Avec la String "AxeX", un Hashset de Double contenant les valeurs des gradations de l'axe X
|
||||
* Avec la String "AxeY", un Hashset de Double contenant les valeurs des gragations de l'axe Y
|
||||
* Avec la String "OffsetX", un Hashset de Double contenant uniquement la valeur de l'offset des points par rapport à l'axe X
|
||||
* Avec la String "OffsetY", un Hashset de Double contenant uniquement la valeur de l'offset des points par rapport à l'axe Y
|
||||
*/
|
||||
public static HashMap< String ,ArrayList<Double>> calcPointAxes(HashSet<ValeursXY> h){
|
||||
|
||||
HashMap< String, ArrayList<Double> > map = new HashMap<>();
|
||||
|
||||
//Définition des min et max
|
||||
double max_x = Double.MIN_VALUE;
|
||||
double min_x = Double.MAX_VALUE;
|
||||
double max_y = Double.MIN_VALUE;
|
||||
double min_y = Double.MAX_VALUE;
|
||||
|
||||
//Trouvé les min et max
|
||||
for (ValeursXY var : h) {
|
||||
|
||||
if (max_x < var.getX()){
|
||||
max_x = var.getX();
|
||||
}
|
||||
if (min_x > var.getX()){
|
||||
min_x = var.getX();
|
||||
}
|
||||
|
||||
if (max_y < var.getY()){
|
||||
max_y = var.getY();
|
||||
}
|
||||
if (min_y > var.getY()){
|
||||
min_y = var.getY();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
double range_x = max_x-min_x;
|
||||
double range_y = max_y-min_y;
|
||||
|
||||
int target = 10; // Ideal Number of Gradation
|
||||
|
||||
double step_x = niceStep(range_x,target);
|
||||
double step_y = niceStep(range_y,target);
|
||||
|
||||
double nicemin_x = roundMin(min_x,step_x);
|
||||
double nicemax_x = roundMax(max_x,step_x);
|
||||
double nicemin_y = roundMin(min_y,step_y);
|
||||
double nicemax_y = roundMax(max_y,step_y);
|
||||
|
||||
// Compléter un Hashset de Double pour X et pour Y et Offset X et Y. TODO
|
||||
ArrayList<Double> axeX = new ArrayList<>();
|
||||
ArrayList<Double> axeY = new ArrayList<>();
|
||||
ArrayList<Double> OffsetX = new ArrayList<>();
|
||||
ArrayList<Double> OffsetY = new ArrayList<>();
|
||||
|
||||
Double ix = nicemin_x;
|
||||
while ( ix <= nicemax_x ) {
|
||||
axeX.add(ix);
|
||||
ix+=step_x;
|
||||
};
|
||||
map.put("AxeX", axeX);
|
||||
|
||||
Double iy = nicemin_y;
|
||||
while ( iy <= nicemax_y ) {
|
||||
axeY.add(iy);
|
||||
iy+=step_y;
|
||||
}
|
||||
map.put("AxeY",axeY);
|
||||
|
||||
double offsetX = min_x - nicemin_x;
|
||||
double offsetY = min_y - nicemin_y;
|
||||
|
||||
ArrayList<Double> offsetXHash = new ArrayList<>();
|
||||
offsetXHash.add(offsetX);
|
||||
ArrayList<Double> offsetYHash = new ArrayList<>();
|
||||
offsetYHash.add(offsetY);
|
||||
|
||||
map.put("OffsetX", offsetXHash);
|
||||
map.put("OffsetY", offsetYHash);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction de calcul d'un step rond
|
||||
* Cette fonction est basé sur une idée demandée à ChatGPT
|
||||
* @param range écart entre la plus petite et la plus grande valeur
|
||||
* @param targetTicks nombre de gradation ideal
|
||||
* @return
|
||||
*/
|
||||
public static double niceStep(double range, int targetTicks) {
|
||||
|
||||
double rawStep = range / targetTicks;
|
||||
|
||||
double exponent = Math.floor(Math.log10(rawStep));
|
||||
double fraction = rawStep / Math.pow(10, exponent);
|
||||
|
||||
double niceFraction;
|
||||
|
||||
if (fraction < 1.5)
|
||||
niceFraction = 1;
|
||||
else if (fraction < 3)
|
||||
niceFraction = 2;
|
||||
else if (fraction < 7)
|
||||
niceFraction = 5;
|
||||
else
|
||||
niceFraction = 10;
|
||||
|
||||
return niceFraction * Math.pow(10, exponent);
|
||||
}
|
||||
|
||||
/**
|
||||
* retourne une valeur arrondi "joli" adapter à un graphique
|
||||
* @param value
|
||||
* @param step
|
||||
* @return
|
||||
*/
|
||||
public static double roundMin(double value, double step) {
|
||||
return Math.floor(value / step) * step;
|
||||
}
|
||||
|
||||
public static double roundMax(double value, double step) {
|
||||
return Math.ceil(value / step) * step;
|
||||
}
|
||||
}
|
||||
108
src/ecoparasite/svg/SVGFactory.java
Normal file
108
src/ecoparasite/svg/SVGFactory.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package ecoparasite.svg;
|
||||
|
||||
import ecoparasite.representation.ValeursXY;
|
||||
import ecoparasite.svg.elements.Element;
|
||||
import ecoparasite.svg.elements.ElementsFactory;
|
||||
|
||||
import javax.lang.model.util.ElementScanner14;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SVGFactory {
|
||||
|
||||
static final private String EXPORT_PATH = "export/";
|
||||
static final private String EXTENSION = ".svg";
|
||||
|
||||
/**
|
||||
* Permet la création du fichier SVG
|
||||
* @param mesElements un array des elements à ajouter dans le svg
|
||||
* @return True si la création est un succès, False sinon
|
||||
*/
|
||||
public static boolean createSVG(ArrayList<Element> mesElements){
|
||||
|
||||
String code = createSVGCode(mesElements);
|
||||
|
||||
try {
|
||||
createFile(code);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet la création du fichier SVG (Polymorphisme pour ajouter un nom de fichier)
|
||||
* @param mesElements un Array des elements à ajouter dans le SVG
|
||||
* @param filename une String représentant le nom du fichier choisi
|
||||
* @return True si la création est un succès, False sinon
|
||||
*/
|
||||
public static boolean createSVG(ArrayList<Element> mesElements, String filename) {
|
||||
|
||||
String code = createSVGCode(mesElements);
|
||||
|
||||
try {
|
||||
createFile(code,filename);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction basique de transformation des éléments en code SVG
|
||||
* @param mesElements un array contenant les éléments à mettre dans le svg
|
||||
* @return une String contenant la totalité du code SVG de notre graphique
|
||||
*/
|
||||
public static String createSVGCode(ArrayList<Element> mesElements){
|
||||
|
||||
String code = "<svg height=\"" + ElementsFactory.SVG_SIZE + "\" width=\"" + ElementsFactory.SVG_SIZE + "\" >";
|
||||
|
||||
for (Element e : mesElements){
|
||||
|
||||
code += e.toSVG();
|
||||
|
||||
}
|
||||
|
||||
code += "</svg>";
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* fonction qui créer le fichier, ici avec une ID random comme nom de fichier
|
||||
* @param data une String contenant le contenue du fichier désiré (ici pour le SVG)
|
||||
* @throws IOException Déclenché par un échec de la création du fichier
|
||||
*/
|
||||
public static void createFile(String data) throws IOException {
|
||||
String id = UUID.randomUUID().toString();
|
||||
createFile(data,id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet la création du fichier
|
||||
* @param data une String contenant le contenue du fichier désiré
|
||||
* @param filename une String contenant le nom du fichier voulu
|
||||
* @throws IOException Déclenché par un échec de la création du fichier
|
||||
*/
|
||||
public static void createFile(String data, String filename) throws IOException {
|
||||
|
||||
// create a FileWriter object with the file name
|
||||
FileWriter writer = new FileWriter(EXPORT_PATH + filename + EXTENSION);
|
||||
|
||||
// write the string to the file
|
||||
writer.write(data);
|
||||
|
||||
// close the writer
|
||||
writer.close();
|
||||
|
||||
System.out.println("Successfully wrote text to file.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package ecoparasite.svg.SVGFactory;
|
||||
|
||||
import ecoparasite.representation.ValeurXY;
|
||||
import ecoparasite.svg.elements.Element;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Classe Définissant de manière static les méthodes pour créer un fichier SVG de nos graphiques
|
||||
*/
|
||||
public class SVGFactory {
|
||||
|
||||
/**
|
||||
* Chemin d'export et Extension du fichier
|
||||
*/
|
||||
static final private String EXPORT_PATH = "export/";
|
||||
static final private String EXTENSION = ".svg";
|
||||
|
||||
/**
|
||||
* Fonction createSVG permettant de créer le fichier SVG dans le dossier du chemin d'export.
|
||||
* Le nom du fichier est defini automatiquement.
|
||||
* @param mesElements tableau composé des différents éléments de notre graphique
|
||||
* @return True si la fonction a réussi à créer le fichier, False sinon
|
||||
*/
|
||||
public static boolean createSVG(HashSet<Element> mesElements){
|
||||
|
||||
String code = createSVGCode(mesElements);
|
||||
|
||||
try {
|
||||
createFile(code);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Fonction createSVG permettant de créer le fichier SVG dans le dossier du chemin d'export.
|
||||
* @param mesElements tableau composé des différents éléments de notre graphique
|
||||
* @param filename String contenant le nom du fichier voulu
|
||||
* @return True si la fonction a réussi à créer le fichier, False sinon
|
||||
*/
|
||||
public static boolean createSVG(HashSet<Element> mesElements, String filename) {
|
||||
|
||||
String code = createSVGCode(mesElements);
|
||||
|
||||
try {
|
||||
createFile(code,filename);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet la création du code de notre SVG
|
||||
* @param mesElements tableau d'élement de notre graphiques
|
||||
* @return une String contenant le code complet de notre SVG
|
||||
*/
|
||||
public static String createSVGCode(HashSet<Element> mesElements){
|
||||
|
||||
String code = "<svg height=\"800\" width=\"800\" >";
|
||||
|
||||
for (Element e : mesElements){
|
||||
|
||||
code += e.toSVG();
|
||||
|
||||
}
|
||||
|
||||
code += "</svg>";
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de lancer la création du fichier. Cette version ne prend pas de filename mais à la place créer un UUID random pour.
|
||||
* @param data String contenant le code de notre SVG
|
||||
* @throws IOException C'est une création de fichier, il peut y avoir des erreurs
|
||||
*/
|
||||
public static void createFile(String data) throws IOException {
|
||||
String id = UUID.randomUUID().toString();
|
||||
createFile(data,id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de créer un fichier SVG contenant notre graphique
|
||||
* @param data String contenant le code de notre SVG
|
||||
* @param filename String contenant le nom de notre fichier
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void createFile(String data, String filename) throws IOException {
|
||||
|
||||
// create a FileWriter object with the file name
|
||||
FileWriter writer = new FileWriter(EXPORT_PATH + filename + EXTENSION);
|
||||
|
||||
// write the string to the file
|
||||
writer.write(data);
|
||||
|
||||
// close the writer
|
||||
writer.close();
|
||||
|
||||
System.out.println("Successfully wrote text to file.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
75
src/ecoparasite/svg/SVGResizing.java
Normal file
75
src/ecoparasite/svg/SVGResizing.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package ecoparasite.svg;
|
||||
|
||||
import ecoparasite.representation.ValeursXY;
|
||||
import ecoparasite.svg.elements.ElementsFactory;
|
||||
|
||||
/**
|
||||
* Classe qui permet de convertir des doubles en dimension SVG.
|
||||
*/
|
||||
public class SVGResizing {
|
||||
|
||||
|
||||
private double minX;
|
||||
private double maxX;
|
||||
private double minY;
|
||||
private double maxY;
|
||||
|
||||
/**
|
||||
* Constructeur.
|
||||
* Définit les minimum et maximum X et Y.
|
||||
* @param minXGrad
|
||||
* @param minYGrad
|
||||
* @param maxXGrad
|
||||
* @param maxYGrad
|
||||
*/
|
||||
public SVGResizing(
|
||||
double minXGrad,
|
||||
double minYGrad,
|
||||
double maxXGrad,
|
||||
double maxYGrad
|
||||
){
|
||||
this.minX = minXGrad;
|
||||
this.maxX = maxXGrad;
|
||||
this.minY = minYGrad;
|
||||
this.maxY = maxYGrad;
|
||||
}
|
||||
|
||||
public double getMinX() {
|
||||
return minX;
|
||||
}
|
||||
|
||||
public double getMaxX() {
|
||||
return maxX;
|
||||
}
|
||||
|
||||
public double getMinY() {
|
||||
return minY;
|
||||
}
|
||||
|
||||
public double getMaxY() {
|
||||
return maxY;
|
||||
}
|
||||
|
||||
public Coordonnees resize(ValeursXY vxy ){
|
||||
return this.resize( vxy.getX(), vxy.getY() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de redimensionner quelconque valeur X Y (Provenant de ValeursXY par exemple) en Coordonnées SVG.
|
||||
* @param X
|
||||
* @param Y
|
||||
* @return La coordonnée SVG équivalente.
|
||||
*/
|
||||
public Coordonnees resize( double X, double Y ){
|
||||
double surface = ElementsFactory.SVG_SIZE - 2 * ElementsFactory.SVG_OFFSET;
|
||||
|
||||
double convX = ( X - minX ) / ( maxX - minX );
|
||||
double convY = ( Y - minY ) / ( maxY - minY );
|
||||
|
||||
double SVG_X = ElementsFactory.SVG_OFFSET + convX * surface;
|
||||
double SVG_Y = ElementsFactory.SVG_SIZE - ElementsFactory.SVG_OFFSET - convY * surface;
|
||||
|
||||
return new Coordonnees(SVG_X, SVG_Y);
|
||||
}
|
||||
|
||||
}
|
||||
64
src/ecoparasite/svg/elements/Circle.java
Normal file
64
src/ecoparasite/svg/elements/Circle.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package ecoparasite.svg.elements;
|
||||
|
||||
import ecoparasite.svg.Coordonnees;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Element Cercle SVG.
|
||||
*/
|
||||
public class Circle extends Element {
|
||||
|
||||
private int rayon;
|
||||
private String color;
|
||||
|
||||
/**
|
||||
* Constructeur.
|
||||
* @param coordonnees
|
||||
* @param rayon
|
||||
* @param color - Couleur provenant de ElementsFactory.
|
||||
*/
|
||||
public Circle(Coordonnees coordonnees, int rayon, String color) {
|
||||
super(coordonnees);
|
||||
this.rayon = rayon;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Circle(Coordonnees coordonnees, int rayon) {
|
||||
super(coordonnees);
|
||||
this.rayon = rayon;
|
||||
this.color = ElementsFactory.COLOR_RED;
|
||||
}
|
||||
|
||||
public int getRayon() {
|
||||
return rayon;
|
||||
}
|
||||
|
||||
public void setRayon(int rayon) {
|
||||
this.rayon = rayon;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée la string SVG.
|
||||
* @return La string SVG.
|
||||
*/
|
||||
@Override
|
||||
public String toSVG() {
|
||||
StringBuilder svg = new StringBuilder();
|
||||
svg.append("<circle ");
|
||||
|
||||
String str = String.format(Locale.US, "r=\"%d\" cx=\"%f\" cy=\"%f\" fill=\"%s\"", this.rayon, this.coordonnees.getX(), this.coordonnees.getY(), this.color);
|
||||
svg.append( str );
|
||||
svg.append(" />");
|
||||
|
||||
return svg.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,39 @@
|
||||
package ecoparasite.svg.elements;
|
||||
|
||||
public class Element {
|
||||
import ecoparasite.svg.Coordonnees;
|
||||
|
||||
public Element() {
|
||||
System.out.println("Un Element... Non implémenter encore");
|
||||
/**
|
||||
* Classe abstraite qui permet de définir des éléments SVG.
|
||||
*/
|
||||
abstract public class Element {
|
||||
|
||||
protected Coordonnees coordonnees;
|
||||
|
||||
/**
|
||||
* Constructeur par défaut.
|
||||
* Inscrit juste les coordonnées.
|
||||
* @param coordonnees Les coordonnées.
|
||||
*/
|
||||
public Element(Coordonnees coordonnees) {
|
||||
this.coordonnees = coordonnees;
|
||||
}
|
||||
|
||||
public String toSVG() {
|
||||
return "une string";
|
||||
public Coordonnees getCoordonnees() {
|
||||
return coordonnees;
|
||||
}
|
||||
|
||||
public void setCoordonnees(Coordonnees coordonnees) {
|
||||
this.coordonnees = coordonnees;
|
||||
}
|
||||
|
||||
public void setCoordonnees(int x, int y) {
|
||||
this.coordonnees = new Coordonnees(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Méthode abstraite qui va permettre de transformer notre élément en SVG.
|
||||
* @return La String SVG
|
||||
*/
|
||||
abstract public String toSVG();
|
||||
|
||||
}
|
||||
|
||||
83
src/ecoparasite/svg/elements/ElementsFactory.java
Normal file
83
src/ecoparasite/svg/elements/ElementsFactory.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package ecoparasite.svg.elements;
|
||||
|
||||
import ecoparasite.svg.Coordonnees;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* Contient divers éléments utiles pour les fichiers SVG.
|
||||
*/
|
||||
public class ElementsFactory {
|
||||
|
||||
/**
|
||||
* Taille du fichier SVG.
|
||||
* SVG_SIZE * SVG_SIZE
|
||||
*/
|
||||
final public static int SVG_SIZE = 800;
|
||||
|
||||
/**
|
||||
* Le décalage de cadre du fichier SVG.
|
||||
*/
|
||||
final public static int SVG_OFFSET = 50;
|
||||
|
||||
/**
|
||||
* La taille du texte pour les axes.
|
||||
*/
|
||||
final public static int AXES_TEXT_SIZE = 10;
|
||||
|
||||
// Définition de couleurs.
|
||||
|
||||
final public static String COLOR_WHITE = "white";
|
||||
final public static String COLOR_RED = "red";
|
||||
final public static String COLOR_BLUE = "blue";
|
||||
final public static String COLOR_BLACK = "black";
|
||||
|
||||
/*
|
||||
public static ArrayList<Element> SVGAxes(String HName, String VName ){
|
||||
|
||||
final int begin = SVG_OFFSET + AXES_TEXT_SIZE + (AXES_TEXT_SIZE / 2);
|
||||
final int end = SVG_SIZE - SVG_OFFSET - AXES_TEXT_SIZE - (AXES_TEXT_SIZE / 2);
|
||||
|
||||
Element V = new Line(
|
||||
new Coordonnees( begin, begin ),
|
||||
new Coordonnees( begin, SVG_SIZE - SVG_OFFSET ),
|
||||
COLOR_BLACK,
|
||||
2
|
||||
);
|
||||
|
||||
Element H = new Line(
|
||||
new Coordonnees( begin, SVG_SIZE - SVG_OFFSET ),
|
||||
new Coordonnees( end, SVG_SIZE - SVG_OFFSET ),
|
||||
COLOR_BLACK,
|
||||
2
|
||||
);
|
||||
|
||||
Element VLabel = new Text(
|
||||
new Coordonnees( SVG_OFFSET, SVG_OFFSET ),
|
||||
VName,
|
||||
COLOR_BLACK,
|
||||
AXES_TEXT_SIZE
|
||||
);
|
||||
Element HLabel = new Text(
|
||||
new Coordonnees( SVG_SIZE - SVG_OFFSET - AXES_TEXT_SIZE, SVG_SIZE - SVG_OFFSET - AXES_TEXT_SIZE ),
|
||||
HName,
|
||||
COLOR_BLACK,
|
||||
AXES_TEXT_SIZE
|
||||
);
|
||||
|
||||
ArrayList<Element> SVGAxes = new ArrayList<>();
|
||||
SVGAxes.add(V);
|
||||
SVGAxes.add(H);
|
||||
SVGAxes.add(VLabel);
|
||||
SVGAxes.add(HLabel);
|
||||
return SVGAxes;
|
||||
|
||||
}
|
||||
|
||||
public static ArrayList<Element> SVGAxes(){
|
||||
return SVGAxes( "None", "None" );
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
92
src/ecoparasite/svg/elements/Line.java
Normal file
92
src/ecoparasite/svg/elements/Line.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package ecoparasite.svg.elements;
|
||||
|
||||
import ecoparasite.svg.Coordonnees;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Permet de construire une droite/ligne en SVG.
|
||||
*/
|
||||
public class Line extends Element {
|
||||
|
||||
private Coordonnees coordonneesB;
|
||||
private String color;
|
||||
private int lineWidth;
|
||||
|
||||
/**
|
||||
* Constructeur
|
||||
* @param coordonneesA Coordonnées de départ
|
||||
* @param coordonneesB Coordonnées d'arrivé.
|
||||
* @param color Couleur provenant de ElementsFactory
|
||||
* @param lineWidth Epaisseur de la ligne.
|
||||
*/
|
||||
public Line(Coordonnees coordonneesA, Coordonnees coordonneesB, String color, int lineWidth) {
|
||||
super(coordonneesA);
|
||||
this.coordonneesB = coordonneesB;
|
||||
this.color = color;
|
||||
this.lineWidth = lineWidth;
|
||||
}
|
||||
|
||||
public Line(Coordonnees coordonneesA, Coordonnees coordonneesB) {
|
||||
super(coordonneesA);
|
||||
this.coordonneesB = coordonneesB;
|
||||
this.color = ElementsFactory.COLOR_RED;
|
||||
this.lineWidth = 1;
|
||||
}
|
||||
|
||||
public Coordonnees getCoordonneesA() {
|
||||
return coordonnees;
|
||||
}
|
||||
|
||||
public void setCoordonneesA(Coordonnees coordonnees) {
|
||||
this.coordonnees = coordonnees;
|
||||
}
|
||||
|
||||
public Coordonnees getCoordonneesB() {
|
||||
return coordonneesB;
|
||||
}
|
||||
|
||||
public void setCoordonneesB(Coordonnees coordonnees) {
|
||||
this.coordonneesB = coordonnees;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public int getLineWidth() {
|
||||
return lineWidth;
|
||||
}
|
||||
|
||||
public void setLineWidth(int lineWidth) {
|
||||
this.lineWidth = lineWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère la string SVG de l'élément.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String toSVG() {
|
||||
StringBuilder svg = new StringBuilder();
|
||||
svg.append("<line ");
|
||||
|
||||
String params = String.format( Locale.US,"x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke:%s;stroke-width:%d\"",
|
||||
this.coordonnees.getX(),
|
||||
this.coordonnees.getY(),
|
||||
this.coordonneesB.getX(),
|
||||
this.coordonneesB.getY(),
|
||||
this.color,
|
||||
this.lineWidth
|
||||
);
|
||||
svg.append(params);
|
||||
svg.append( "/>" );
|
||||
|
||||
return svg.toString();
|
||||
}
|
||||
|
||||
}
|
||||
78
src/ecoparasite/svg/elements/Text.java
Normal file
78
src/ecoparasite/svg/elements/Text.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package ecoparasite.svg.elements;
|
||||
|
||||
import ecoparasite.svg.Coordonnees;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Élement qui permet de générer un texte sur le SVG.
|
||||
*/
|
||||
public class Text extends Element {
|
||||
|
||||
private String text;
|
||||
private String color;
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Constructeur
|
||||
* @param coordonnees Les coordonnées du texte
|
||||
* @param text
|
||||
* @param color Provenant de ElementsFactory
|
||||
* @param size La taille du texte
|
||||
*/
|
||||
public Text(Coordonnees coordonnees, String text, String color, int size) {
|
||||
super(coordonnees);
|
||||
this.text = text;
|
||||
this.color = color;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public Text(Coordonnees coordonnees, String text) {
|
||||
super(coordonnees);
|
||||
this.text = text;
|
||||
this.color = ElementsFactory.COLOR_BLACK;
|
||||
this.size = ElementsFactory.AXES_TEXT_SIZE;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère la string SVG.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String toSVG() {
|
||||
StringBuilder svg = new StringBuilder();
|
||||
svg.append("<text ");
|
||||
|
||||
String params = String.format(Locale.US,"x=\"%f\" y=\"%f\" fill=\"%s\" font-size=\"%s\"", coordonnees.getX(), coordonnees.getY(), color, size);
|
||||
svg.append(params);
|
||||
svg.append(" >");
|
||||
svg.append( this.text );
|
||||
svg.append("</text>");
|
||||
return svg.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import ecoparasite.input.InputFactory;
|
||||
import ecoparasite.input.InputFileException;
|
||||
import ecoparasite.input.RawData;
|
||||
import ecoparasite.input.RawDataOverflow;
|
||||
import ecoparasite.nettoyage.Nettoyage;
|
||||
import ecoparasite.poisson.Mackerel;
|
||||
import ecoparasite.poisson.Poisson;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -44,6 +45,7 @@ class CompletionTest {
|
||||
Function<Poisson,Double> getInfes = Poisson::getInfestation;
|
||||
BiConsumer<Poisson,Double> setInfes = Poisson::setInfestation;
|
||||
|
||||
testp = Nettoyage.nettoieColumns(testp,getInfes,setInfes,false);
|
||||
testp = Completion.completeColumnsLinear(testp,getLength,getInfes,setInfes);
|
||||
System.out.println(testp);
|
||||
}
|
||||
|
||||
@@ -50,11 +50,7 @@ class NettoyageTest {
|
||||
Function<Poisson,Double> getInfes = Poisson::getInfestation;
|
||||
BiConsumer<Poisson,Double> setInfes = Poisson::setInfestation;
|
||||
|
||||
testp = Completion.completeColumnsLinear( testp, getLength, getInfes, setInfes );
|
||||
|
||||
System.out.println(testp);
|
||||
|
||||
testp = Nettoyage.nettoieColumnsLinear( testp, getLength, getInfes, setInfes, false );
|
||||
testp = Nettoyage.nettoieColumns( testp, getInfes, setInfes, false );
|
||||
|
||||
System.out.println(testp);
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package ecoparasite.poisson;
|
||||
|
||||
import ecoparasite.input.InputFactory;
|
||||
import ecoparasite.input.InputFileException;
|
||||
import ecoparasite.input.RawData;
|
||||
import ecoparasite.input.RawDataOverflow;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MackerelSerraTest {
|
||||
|
||||
@Test
|
||||
void parse() throws InputFileException, RawDataOverflow {
|
||||
RawData test = InputFactory.readData("test2.csv", ",");
|
||||
|
||||
HashSet<Poisson> testp = MackerelSerra.parse(test);
|
||||
|
||||
System.out.println(testp);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package ecoparasite.svg.SVGFactory;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SVGFactoryTest {
|
||||
|
||||
@Test
|
||||
void createSVGCode() {
|
||||
|
||||
//String ret = SVGFactory.createSVGCode();
|
||||
|
||||
//System.out.println(ret);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateFile() {
|
||||
|
||||
//String ret = SVGFactory.createSVGCode();
|
||||
|
||||
//SVGFactory.createFile(ret);
|
||||
|
||||
}
|
||||
}
|
||||
15
tests/ecoparasite/svg/SVGFactoryTest.java
Normal file
15
tests/ecoparasite/svg/SVGFactoryTest.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package ecoparasite.svg;
|
||||
|
||||
import ecoparasite.svg.elements.ElementsFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SVGFactoryTest {
|
||||
|
||||
@Test
|
||||
public void generateSVGAxes(){
|
||||
// SVGFactory.createSVG( ElementsFactory.SVGAxes() );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user