Skip to content

Commit

Permalink
Fix Donut Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurswag authored and Monsieur-Swag committed Feb 24, 2024
1 parent bc5dc67 commit 4d290d9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
32 changes: 18 additions & 14 deletions backend/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,16 @@ def aggregate_risks_per_field(
count = (
RiskScenario.objects.filter(id__in=object_ids_view)
.filter(residual_level=i)
.filter(risk_assessment__risk_matrix__name=["name"])
# .filter(risk_assessment__risk_matrix__name=["name"])
.count()
)
) # What the second filter does ? Is this usefull ?
else:
count = (
RiskScenario.objects.filter(id__in=object_ids_view)
.filter(current_level=i)
.filter(risk_assessment__risk_matrix__name=["name"])
# .filter(risk_assessment__risk_matrix__name=["name"])
.count()
)
) # What the second filter does ? Is this usefull ?

if "count" not in values[m["risk"][i][field]]:
values[m["risk"][i][field]]["count"] = count
Expand All @@ -528,22 +528,26 @@ def aggregate_risks_per_field(


def risks_count_per_level(user: User, risk_assessments: list | None = None):
current_level = list()
residual_level = list()
current_level = []
residual_level = []

for r in aggregate_risks_per_field(
for level_name, level_data in aggregate_risks_per_field(
user, "name", risk_assessments=risk_assessments
).items():
current_level.append(
{"name": r[0], "value": r[1]["count"], "color": r[1]["color"]}
)
current_level.append({
"name": level_name,
"value": level_data["count"],
"color": level_data["color"]
})

for r in aggregate_risks_per_field(
for level_name, level_data in aggregate_risks_per_field(
user, "name", residual=True, risk_assessments=risk_assessments
).items():
residual_level.append(
{"name": r[0], "value": r[1]["count"], "color": r[1]["color"]}
)
residual_level.append({
"name": level_name,
"value": level_data["count"],
"color":level_data["color"]
})

return {"current": current_level, "residual": residual_level}

Expand Down
2 changes: 1 addition & 1 deletion backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def impact(self, request, pk):
return Response(choices)

@action(detail=False, name="Get risk count per level")
def count_per_level(self, request): # _per_level
def count_per_level(self, request):
return Response({"results": risks_count_per_level(request.user)})


Expand Down
9 changes: 7 additions & 2 deletions frontend/src/lib/components/Chart/DonutChart.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<script lang="ts">
import { onMount } from 'svelte';
export let name: string;
export let s_label: string;
export let values: any[]; // Set the types for these variables later on
export let colors: string[] = [];
let chart_element: HTMLElement | null = null;
$: chart_id = `${name}_div`;
// let chart_element: HTMLElement | null = null;
onMount(async () => {
const echarts = await import('echarts');
const chart_element = document.getElementById(chart_id);
let chart = echarts.init(chart_element, null, { renderer: 'svg' });
// specify chart configuration item and data
Expand Down Expand Up @@ -69,4 +74,4 @@
});
</script>

<div class="bg-white w-auto h-full" bind:this={chart_element} />
<div id="{chart_id}" class="bg-white w-auto h-full"/> <!-- bind:this={chart_element} -->
2 changes: 2 additions & 0 deletions frontend/src/routes/(app)/analytics/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@
<span class="text-sm font-semibold">Current risk level per risk scenario</span>

<DonutChart
name="current_risk"
s_label={cur_rsk_label}
values={risk_level.current}
colors={RISK_COLOR_PALETTE}
Expand All @@ -297,6 +298,7 @@
<span class="text-sm font-semibold">Residual risk level per risk scenario</span>

<DonutChart
name="residual_risk"
s_label={rsd_rsk_label}
values={risk_level.residual}
colors={RISK_COLOR_PALETTE}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/routes/(app)/analytics/composer/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<div class="items-center h-96">
<DonutChart
name="current_risk_level"
s_label="Current risk level per risk scenario"
values={data.current_level}
colors={RISK_COLOR_PALETTE}
Expand All @@ -52,6 +53,7 @@
<div class="p-2 text-sm font-semibold">Residual risk level per risk scenario</div>
<div class="items-center h-96">
<DonutChart
name="residual_risk_level"
s_label="Residual risk level per risk scenario"
values={data.residual_level}
colors={RISK_COLOR_PALETTE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
</div>
<div class="w-full">
<DonutChart
name="compliance_assessments"
s_label="compliance_assessments"
values={compliance_assessment_donut_values.values}
/>
Expand Down

0 comments on commit 4d290d9

Please sign in to comment.