Skip to content
GitLab
Projets
Groupes
Sujets
Extraits de code
/
Aide
Aide
Support
Forum de la communauté
Raccourcis clavier
?
Proposer une rétroaction
Contribuer à GitLab
Connexion
Activer/désactiver la navigation
Menu
Nathan Marye
web-3-api-platform
Comparer les révisions
2975e34ca025a9c9c8d4648000842b4d5a340431...db4cabccaf7ab1baecb5d3d3289d3651162a7949
Commits (2)
lines for donut divisions added
· bcc04b5d
Hajar RAHMOUNI
a écrit
jan. 07, 2024
bcc04b5d
Merge branch 'donut-adjustment' into 'main'
· db4cabcc
Hajar RAHMOUNI
a écrit
jan. 07, 2024
lines for donut divisions added See merge request
!26
db4cabcc
Masquer les modifications d'espaces
En ligne
Côte à côte
pwa/app/components/donut-chart/donut.jsx
Voir le fichier @
db4cabcc
...
@@ -3,14 +3,15 @@ import * as d3 from "d3";
...
@@ -3,14 +3,15 @@ import * as d3 from "d3";
export
default
function
DonutChart
({
data
})
{
export
default
function
DonutChart
({
data
})
{
const
ref
=
useRef
();
const
ref
=
useRef
();
const
svgWidth
=
6
00
;
const
svgWidth
=
11
00
;
const
svgHeight
=
6
00
;
const
svgHeight
=
10
00
;
const
width
=
400
;
const
width
=
400
;
const
height
=
400
;
const
height
=
400
;
const
radius
=
Math
.
min
(
width
,
height
)
/
2
;
const
radius
=
Math
.
min
(
width
,
height
)
/
2
;
const
innerRadius
=
radius
*
0.6
;
const
innerRadius
=
radius
*
0.6
;
const
outerRadius
=
radius
*
0.9
;
const
outerRadius
=
radius
*
0.9
;
const
hoverOuterRadius
=
outerRadius
+
20
;
const
hoverOuterRadius
=
outerRadius
+
20
;
const
minPercentage
=
1
;
// le seuil pour regrouper les petites divisions
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
!
data
||
data
.
length
===
0
)
{
if
(
!
data
||
data
.
length
===
0
)
{
...
@@ -18,9 +19,7 @@ export default function DonutChart({ data }) {
...
@@ -18,9 +19,7 @@ export default function DonutChart({ data }) {
}
}
const
totalOccurrences
=
data
.
reduce
((
acc
,
item
)
=>
acc
+
item
.
occurrences
,
0
);
const
totalOccurrences
=
data
.
reduce
((
acc
,
item
)
=>
acc
+
item
.
occurrences
,
0
);
const
minPercentage
=
1
;
let
otherData
=
{
region
:
"
Autres
"
,
occurrences
:
0
,
details
:
[]
};
let
otherData
=
{
region
:
"
Autres
"
,
occurrences
:
0
,
details
:
[]
};
let
newData
=
data
.
filter
(
d
=>
{
let
newData
=
data
.
filter
(
d
=>
{
let
percentage
=
(
d
.
occurrences
/
totalOccurrences
)
*
100
;
let
percentage
=
(
d
.
occurrences
/
totalOccurrences
)
*
100
;
if
(
percentage
<
minPercentage
)
{
if
(
percentage
<
minPercentage
)
{
...
@@ -38,6 +37,7 @@ export default function DonutChart({ data }) {
...
@@ -38,6 +37,7 @@ export default function DonutChart({ data }) {
const
pieData
=
d3
.
pie
().
value
(
d
=>
d
.
occurrences
)(
newData
);
const
pieData
=
d3
.
pie
().
value
(
d
=>
d
.
occurrences
)(
newData
);
const
arc
=
d3
.
arc
().
innerRadius
(
innerRadius
).
outerRadius
(
outerRadius
);
const
arc
=
d3
.
arc
().
innerRadius
(
innerRadius
).
outerRadius
(
outerRadius
);
const
hoverArc
=
d3
.
arc
().
innerRadius
(
innerRadius
).
outerRadius
(
hoverOuterRadius
);
const
hoverArc
=
d3
.
arc
().
innerRadius
(
innerRadius
).
outerRadius
(
hoverOuterRadius
);
const
labelArc
=
d3
.
arc
().
innerRadius
(
outerRadius
+
30
).
outerRadius
(
outerRadius
+
30
);
const
svg
=
d3
.
select
(
ref
.
current
)
const
svg
=
d3
.
select
(
ref
.
current
)
.
attr
(
"
width
"
,
svgWidth
)
.
attr
(
"
width
"
,
svgWidth
)
...
@@ -54,37 +54,45 @@ export default function DonutChart({ data }) {
...
@@ -54,37 +54,45 @@ export default function DonutChart({ data }) {
.
on
(
"
mouseover
"
,
function
(
event
,
d
)
{
.
on
(
"
mouseover
"
,
function
(
event
,
d
)
{
d3
.
select
(
this
).
transition
().
attr
(
"
d
"
,
hoverArc
);
d3
.
select
(
this
).
transition
().
attr
(
"
d
"
,
hoverArc
);
if
(
d
.
data
.
region
===
"
Autres
"
)
{
let
text
=
(
d
.
data
.
region
===
"
Autres
"
)
?
svg
.
append
(
"
text
"
)
otherData
.
details
.
join
(
"
,
"
)
:
.
attr
(
"
class
"
,
"
details-text
"
)
`
${
d
.
data
.
region
}
:
${((
d
.
data
.
occurrences
/
totalOccurrences
)
*
100
).
toFixed
(
2
)}
%`
;
.
attr
(
"
x
"
,
0
)
.
attr
(
"
y
"
,
0
)
.
attr
(
"
text-anchor
"
,
"
middle
"
)
.
selectAll
(
"
tspan
"
)
.
data
(
otherData
.
details
)
.
enter
()
.
append
(
"
tspan
"
)
.
attr
(
"
x
"
,
0
)
.
attr
(
"
dy
"
,
"
1.2em
"
)
.
text
(
detail
=>
detail
);
}
else
{
const
percentage
=
((
d
.
data
.
occurrences
/
totalOccurrences
)
*
100
).
toFixed
(
2
);
const
text
=
`
${
d
.
data
.
region
}
:
${
percentage
}
%`
;
svg
.
append
(
"
text
"
)
const
centroid
=
hoverArc
.
centroid
(
d
);
.
attr
(
"
class
"
,
"
tooltip
"
)
svg
.
append
(
"
text
"
)
.
attr
(
"
x
"
,
0
)
.
attr
(
"
class
"
,
"
tooltip
"
)
.
attr
(
"
y
"
,
20
)
.
attr
(
"
x
"
,
centroid
[
0
]
)
.
attr
(
"
text-anchor
"
,
"
middle
"
)
.
attr
(
"
y
"
,
centroid
[
1
]
)
.
text
(
text
);
.
attr
(
"
text-anchor
"
,
"
middle
"
)
}
.
text
(
text
);
})
})
.
on
(
"
mouseout
"
,
function
()
{
.
on
(
"
mouseout
"
,
function
()
{
d3
.
select
(
this
).
transition
().
attr
(
"
d
"
,
arc
);
d3
.
select
(
this
).
transition
().
attr
(
"
d
"
,
arc
);
svg
.
selectAll
(
"
.details-text
"
).
remove
();
svg
.
selectAll
(
"
.tooltip
"
).
remove
();
svg
.
selectAll
(
"
.tooltip
"
).
remove
();
});
});
// l'ajout des lignes et textes pour chaque division sauf pour autres
svg
.
selectAll
(
"
.label-line
"
)
.
data
(
pieData
.
filter
(
d
=>
d
.
data
.
region
!==
"
Autres
"
))
.
enter
()
.
append
(
"
line
"
)
.
attr
(
"
x1
"
,
d
=>
labelArc
.
centroid
(
d
)[
0
])
.
attr
(
"
y1
"
,
d
=>
labelArc
.
centroid
(
d
)[
1
])
.
attr
(
"
x2
"
,
d
=>
labelArc
.
centroid
(
d
)[
0
]
*
2
)
.
attr
(
"
y2
"
,
d
=>
labelArc
.
centroid
(
d
)[
1
]
*
2
)
.
attr
(
"
stroke
"
,
"
gray
"
);
svg
.
selectAll
(
"
.label-text
"
)
.
data
(
pieData
.
filter
(
d
=>
d
.
data
.
region
!==
"
Autres
"
))
.
enter
()
.
append
(
"
text
"
)
.
attr
(
"
transform
"
,
d
=>
{
const
[
x
,
y
]
=
labelArc
.
centroid
(
d
);
return
`translate(
${
x
*
2
}
,
${
y
*
2
}
)`
;
})
.
attr
(
"
text-anchor
"
,
"
middle
"
)
.
text
(
d
=>
`
${
d
.
data
.
region
}
:
${((
d
.
data
.
occurrences
/
totalOccurrences
)
*
100
).
toFixed
(
2
)}
%`
);
},
[
data
]);
},
[
data
]);
return
<
svg
ref
=
{
ref
}
></
svg
>;
return
<
svg
ref
=
{
ref
}
></
svg
>;
...
...
pwa/app/style/style.css
Voir le fichier @
db4cabcc
...
@@ -19,8 +19,7 @@
...
@@ -19,8 +19,7 @@
margin
:
20px
;
margin
:
20px
;
}
}
.donut-container
{
.donut-container
{
margin
:
10px
;
width
:
80rem
;
width
:
90%
;
}
}
.form-container
{
.form-container
{
display
:
flex
;
display
:
flex
;
...
...