Files
ecoparasite-data-analyse/src/ecoparasite/svg/SVGFactory.java

173 lines
4.4 KiB
Java
Raw Normal View History

2026-04-08 14:47:42 +02:00
package ecoparasite.svg;
2026-04-08 16:00:42 +02:00
import ecoparasite.representation.ValeursXY;
2026-04-08 14:47:42 +02:00
import ecoparasite.svg.elements.Element;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
2026-04-08 16:00:42 +02:00
import java.util.HashMap;
import java.util.HashSet;
2026-04-08 14:47:42 +02:00
import java.util.UUID;
public class SVGFactory {
static final private String EXPORT_PATH = "export/";
static final private String EXTENSION = ".svg";
public static boolean createSVG(ArrayList<Element> mesElements){
String code = createSVGCode(mesElements);
try {
createFile(code);
} catch (Exception e) {
return false;
}
return true;
}
public static boolean createSVG(ArrayList<Element> mesElements, String filename) {
String code = createSVGCode(mesElements);
try {
createFile(code,filename);
} catch (Exception e) {
return false;
}
return true;
}
public static String createSVGCode(ArrayList<Element> mesElements){
String code = "<svg height=\"800\" width=\"800\" >";
for (Element e : mesElements){
code += e.toSVG();
}
code += "</svg>";
return code;
}
public static void createFile(String data) throws IOException {
String id = UUID.randomUUID().toString();
createFile(data,id);
}
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.");
}
2026-04-08 16:00:42 +02:00
public static HashMap< String ,HashSet<ValeursXY>> PointAXES(HashSet<ValeursXY> h){
//Définition initial
HashSet<ValeursXY> x = new HashSet<>();
HashSet<ValeursXY> y = new HashSet<>();
HashSet<ValeursXY> offset = new HashSet<>();
HashMap< String, HashSet<ValeursXY> > 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();
} else if (min_x > var.getX()){
min_x = var.getX();
}
if (max_y < var.getY()){
max_y = var.getY();
} else if (min_y > var.getY()){
min_y = var.getY();
}
}
2026-04-20 16:39:00 +02:00
double range_x = max_x-min_x;
double range_y = max_y-min_y;
2026-04-08 16:00:42 +02:00
2026-04-20 16:39:00 +02:00
int target = 10; // Ideal Number of Gradation
2026-04-08 16:00:42 +02:00
2026-04-20 16:39:00 +02:00
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);
HashSet<ValeursXY> axe_x = new HashSet<>();
HashSet<ValeursXY> axe_y = new HashSet<>();
2026-04-08 16:00:42 +02:00
return null;
}
2026-04-20 16:39:00 +02:00
/**
* 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);
}
2026-04-08 16:00:42 +02:00
/**
* 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;
}
2026-04-08 14:47:42 +02:00
}