Nouvelles classes pour rendre le graph plus joli

pull/51/head
Thomas Chazot 1 year ago
parent 5667dc7ee6
commit 515a5e1407

@ -1,7 +1,7 @@
import React, { useEffect } from "react";
import { DataSet, Network} from "vis-network/standalone/esm/vis-network";
import EdgesCreator from "../source/EdgesCreator";
import GraphCreator from "../source/GraphCreator";
import GraphCreator from "../source/Graph/GraphCreator";
import IndiceChooser from "../source/IndiceChooser";
import NetworkGenerator from "../source/NetworkGenerator";
import Stub from "../source/Stub";
@ -41,13 +41,6 @@ const MyGraphComponent = () => {
// Configuration des options du Graphe
const initialOptions = {
nodes: {
shape: 'circle',
size: 30,
font: {
size: 20
},
},
layout: {
improvedLayout: true,
hierarchical: {
@ -70,8 +63,6 @@ const MyGraphComponent = () => {
const network = new Network(container, networkData, initialOptions);
// Gérer le changement entre la physique et le déplacement manuel
let physicsEnabled = true;
network.on("dragging", (params) => {
if (params.nodes.length > 0) {
// Un nœud a été cliqué

@ -17,7 +17,7 @@ import Indice from '../source/Indices/Indice';
import SportIndice from '../source/Indices/SportIndice';
import Stub from '../source/Stub';
import NetworkGenerator from '../source/NetworkGenerator';
import GraphCreator from '../source/GraphCreator';
import GraphCreator from '../source/Graph/GraphCreator';
import { FormattedMessage } from 'react-intl';

@ -3,9 +3,7 @@ enum Color {
NOIR,
BLOND,
ROUX,
CHATAIN,
BRUN,
}
export default Color

@ -50,19 +50,23 @@ class EdgesCreator{
return
}
if (p != personToCreateEdge && p != choosenPerson && !personToCreateEdge.getFriends().includes(p)){
let testEdgeWork = 0
let testEdgeWork1 = 0
let testEdgeWork2 = 0
const p1 = cloneDeep(p);
const personEdge = cloneDeep(personToCreateEdge);
p1.addFriend(personEdge)
personEdge.addFriend(p1)
indices.forEach((indice) => {
const tester = IndiceTesterFactory.Create(indice)
if (tester.Works(p1) || tester.Works(personEdge) || map.get(p.getId()) === p.getFriends().length){
testEdgeWork ++
if (tester.Works(p1) || map.get(p.getId()) === p.getFriends().length){
testEdgeWork1 ++
}
if (tester.Works(personEdge) || map.get(p.getId()) === p.getFriends().length){
testEdgeWork2 ++
}
});
if (testEdgeWork < indices.length){
if (testEdgeWork1 < indices.length && testEdgeWork2 < indices.length){
p.addFriend(personToCreateEdge)
personToCreateEdge.addFriend(p)
}
@ -72,6 +76,13 @@ class EdgesCreator{
CreateAllEdges(personNetwork: PersonNetwork, choosenPerson: Person, indices: Indice[]){
const test = new Map<number, number>()
const tabEdgesSize: number[] = []
for (let i = 0; i<personNetwork.getPersons().length / 4; i++){
tabEdgesSize.push(1)
tabEdgesSize.push(2)
tabEdgesSize.push(3)
tabEdgesSize.push(4)
}
indices.forEach(indice => {
if (indice instanceof EdgesIndice){
this.CreateWorkingEdge(personNetwork, choosenPerson, indice, indices)
@ -79,7 +90,8 @@ class EdgesCreator{
});
personNetwork.getPersons().forEach((p) => {
if (p != choosenPerson){
test.set(p.getId(), Math.floor(Math.random() * 5) + p.getFriends().length + 1)
const rand = Math.floor(Math.random() * 4)
test.set(p.getId(), tabEdgesSize[rand] + p.getFriends().length)
}
});
personNetwork.getPersons().forEach((p) => {

@ -0,0 +1,118 @@
import Color from "./Color";
import Sport from "./Sport";
function ColorToString(color: Color, lang: string): string{
switch(color){
case Color.BLANC:
switch(lang){
case "en":
return "White"
default:
return "Blanc"
}
case Color.NOIR:
switch(lang){
case "en":
return "Black"
default:
return "Noir"
}
case Color.BLOND:
switch(lang){
case "en":
return "Blond"
default:
return "Blond"
}
case Color.ROUX:
switch(lang){
case "en":
return "Redhead"
default:
return "Roux"
}
case Color.BRUN:
switch(lang){
case "en":
return "Brown"
default:
return "Brun"
}
}
}
function ColorToHexa(color: Color): string{
switch(color){
case Color.BLANC:
return "#FFFFFF"
case Color.NOIR:
return "#000000"
case Color.BLOND:
return "#E2BC74"
case Color.ROUX:
return "#FF8B00"
case Color.BRUN:
return "#5B3C11"
}
}
function ColorToColorFont(color: Color): string{
switch(color){
case Color.BLANC:
return "#000000"
case Color.NOIR:
return "#FFFFFF"
case Color.BLOND:
return "#000000"
case Color.ROUX:
return "#000000"
case Color.BRUN:
return "#FFFFFF"
}
}
function SportToString(sport: Sport, lang: string): string{
switch(sport){
case Sport.FOOT:
switch(lang){
case "en":
return "Football"
default:
return "Football"
}
case Sport.RUGBY:
switch(lang){
case "en":
return "Rugby"
default:
return "Rugby"
}
case Sport.BASKET:
switch(lang){
case "en":
return "Basket"
default:
return "Basket"
}
case Sport.TENNIS:
switch(lang){
case "en":
return "Tennis"
default:
return "Tennis"
}
case Sport.CURLING:
switch(lang){
case "en":
return "Curling"
default:
return "Curling"
}
case Sport.AUCUN:
return ""
}
}
export {ColorToString, SportToString, ColorToHexa, ColorToColorFont}

@ -0,0 +1,14 @@
class Font{
public color: string
public size: number
public align: string
constructor(color: string, size: number, align: string){
this.color=color
this.size=size
this.align=align
}
}
export default Font

@ -0,0 +1,39 @@
import Edge from "./Edge";
import { ColorToColorFont, ColorToHexa, SportToString } from "../EnumExtender";
import GraphPerson from "./GraphPerson";
import NodePerson from "./NodePerson";
import PersonNetwork from "../PersonsNetwork";
import Font from "./Font";
class GraphCreator{
static CreateGraph(network: PersonNetwork): GraphPerson{
const nodesPerson : NodePerson[] = []
const edges: Edge[] = []
network.getPersons().forEach((p) =>{
let label = p.getName() + "\n" + p.getAge() + "\n"
for (let i = 0; i<p.getSports().length; i++){
if (i==p.getSports().length -1 || p.getSports().length == 1){
label += SportToString(p.getSports()[i], "fr")
}
else{
label += SportToString(p.getSports()[i], "fr") + "\n"
}
}
const nodePerson = new NodePerson(p.getId(), label, p.getColor(), new Font(ColorToColorFont(p.getColor()), 14, 'center'), 'ellipse')
nodesPerson.push(nodePerson)
p.getFriends().forEach((f) =>{
if(edges.some(edge => (edge.from === f.getId() && edge.to === p.getId()) || (edge.from === p.getId() && edge.to === f.getId()))){
return;
}
edges.push(new Edge(p.getId(), f.getId()))
});
});
return new GraphPerson(edges, nodesPerson)
}
}
export default GraphCreator

@ -0,0 +1,21 @@
import Color from "../Color"
import { ColorToColorFont, ColorToHexa } from "../EnumExtender"
import Font from "./Font"
class NodePerson{
public id: number
public label: string
public color: string
public font: Font
public shape: string
constructor(id: number, label: string, color: Color, font: Font, shape: string){
this.id=id
this.label=label
this.color=ColorToHexa(color)
this.font=font
this.shape = shape
}
}
export default NodePerson

@ -1,26 +0,0 @@
import Edge from "./Edge";
import GraphPerson from "./GraphPerson";
import NodePerson from "./NodePerson";
import PersonNetwork from "./PersonsNetwork";
class GraphCreator{
static CreateGraph(network: PersonNetwork): GraphPerson{
const nodesPerson : NodePerson[] = []
const edges: Edge[] = []
network.getPersons().forEach((p) =>{
const nodePerson = new NodePerson(p.getId(), p.getName())
nodesPerson.push(nodePerson)
p.getFriends().forEach((f) =>{
if(edges.some(edge => (edge.from === f.getId() && edge.to === p.getId()) || (edge.from === p.getId() && edge.to === f.getId()))){
return;
}
edges.push(new Edge(p.getId(), f.getId()))
});
});
return new GraphPerson(edges, nodesPerson)
}
}
export default GraphCreator

@ -11,7 +11,7 @@ class AgeIndice extends Indice {
}
// Implémentation de la méthode abstraite
ToString(): string {
ToString(lang: string): string {
return "La personne a entre " + this.minimum + " et " + this.maximum + " ans"
}

@ -1,7 +1,9 @@
import Color from "../Color";
import { ColorToString } from "../EnumExtender";
import EdgesIndice from "./EdgesIndice";
class ColorEdgesIndice extends EdgesIndice {
private neighborsColors: Color[];
constructor(id: number, neighborsColors: Color[]) {
@ -14,8 +16,17 @@ class ColorEdgesIndice extends EdgesIndice {
}
// Implémentation de la méthode abstraite
ToString(): string {
return "La personne a au moins " + this.neighborsColors + " amis";
ToString(lang: string): string {
let string = "La personne a au moins un ami avec les cheveux";
for (let i = 0; i<this.neighborsColors.length; i++){
if (i==this.neighborsColors.length - 1 || this.neighborsColors.length == 1){
string = string + " " + ColorToString(this.neighborsColors[i], lang)
}
else{
string = string + " ou " + ColorToString(this.neighborsColors[i], lang)
}
}
return string
}
}

@ -1,4 +1,5 @@
import Color from "../Color";
import { ColorToString } from "../EnumExtender";
import Indice from "./Indice";
class ColorIndice extends Indice {
@ -10,11 +11,19 @@ class ColorIndice extends Indice {
}
// Implémentation de la méthode abstraite
ToString(): string {
return "La personne a entre "
ToString(lang: string): string {
let string = "La personne a au moins un ami avec les cheveux";
for (let i = 0; i<this.colors.length; i++){
if (i==this.colors.length - 1 || this.colors.length == 1){
string = string + " " + ColorToString(this.colors[i], lang)
}
else{
string = string + " ou " + ColorToString(this.colors[i], lang)
}
}
return string
}
getColors(): Color[]{
return this.colors
}

@ -15,7 +15,9 @@ abstract class Indice {
}
// Méthode abstraite pour être implémentée par les classes dérivées
abstract ToString(): string;
abstract ToString(lang: string): string;
}
export default Indice

@ -13,7 +13,7 @@ class NbEdgesIndice extends EdgesIndice {
}
// Implémentation de la méthode abstraite
ToString(): string {
ToString(lang: string): string {
return "La personne a au moins " + this.nbNeighbors + " amis";
}
}

@ -1,3 +1,4 @@
import { SportToString } from "../EnumExtender";
import Sport from "../Sport";
import Indice from "./Indice";
@ -10,8 +11,17 @@ class SportIndice extends Indice {
}
// Implémentation de la méthode abstraite
ToString(): string {
return "La personne a entre "
ToString(lang: string): string {
let string = "La personne pratique au moins un de ces sports: ";
for (let i = 0; i<this.sports.length; i++){
if (i==this.sports.length - 1 || this.sports.length == 1){
string = string + " " + SportToString(this.sports[i], lang)
}
else{
string = string + " ou " + SportToString(this.sports[i], lang)
}
}
return string
}

@ -1,11 +0,0 @@
class NodePerson{
public id: number
public label: string
constructor(id: number, label: string){
this.id=id
this.label=label
}
}
export default NodePerson
Loading…
Cancel
Save