Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 57b1c01

Browse files
committed
DEV: Varying sized doughnuts
1 parent e0f4c75 commit 57b1c01

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

assets/javascripts/discourse/components/doughnut-chart.gjs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
import Component from "@glimmer/component";
2+
import { tracked } from "@glimmer/tracking";
23
import Chart from "admin/components/chart";
34

45
export default class DoughnutChart extends Component {
6+
@tracked canvasSize = null;
7+
8+
calculateRadius(postCount) {
9+
const minPosts = 0;
10+
const maxPosts = 100;
11+
const minRadius = 30;
12+
const maxRadius = 100;
13+
const clampedPostCount = Math.min(Math.max(postCount, minPosts), maxPosts);
14+
return (
15+
minRadius +
16+
((clampedPostCount - minPosts) / (maxPosts - minPosts)) *
17+
(maxRadius - minRadius)
18+
);
19+
}
20+
521
get config() {
622
const totalScore = this.args.totalScore || "";
23+
// const radius = this.calculateRadius(this.args.totalScore)
24+
// Temporary for tesitng:
25+
const radius = this.calculateRadius(Math.floor(Math.random() * (100 + 1)));
26+
27+
const paddingTop = 30;
28+
const paddingBottom = 0;
29+
const canvasSize = 2 * radius + paddingTop + paddingBottom;
30+
this.canvasSize = canvasSize;
731

832
return {
933
type: "doughnut",
@@ -13,14 +37,25 @@ export default class DoughnutChart extends Component {
1337
{
1438
data: this.args.data,
1539
backgroundColor: this.args.colors,
40+
cutout: "70%",
41+
radius,
1642
},
1743
],
1844
},
1945
options: {
2046
responsive: true,
47+
maintainAspectRatio: false,
48+
layout: {
49+
padding: {
50+
top: paddingTop,
51+
left: 0,
52+
right: 0,
53+
bottom: 0,
54+
},
55+
},
2156
plugins: {
2257
legend: {
23-
position: this.args.legendPosition || "bottom",
58+
display: false,
2459
},
2560
},
2661
},
@@ -55,13 +90,26 @@ export default class DoughnutChart extends Component {
5590
ctx.save();
5691
},
5792
},
93+
{
94+
id: "resizeCanvas",
95+
afterDraw: function (chart) {
96+
const size = `${canvasSize}px`;
97+
chart.canvas.style.width = size;
98+
chart.canvas.style.height = size;
99+
100+
chart.resize();
101+
},
102+
},
58103
],
59104
};
60105
}
61106

62107
<template>
63108
{{#if this.config}}
64-
<h3 class="doughnut-chart-title">{{@doughnutTitle}}</h3>
109+
<h3
110+
class="doughnut-chart-title"
111+
style="max-width: {{this.canvasSize}}px"
112+
>{{@doughnutTitle}}</h3>
65113
<Chart @chartConfig={{this.config}} class="admin-report-doughnut" />
66114
{{/if}}
67115
</template>

assets/stylesheets/modules/sentiment/common/dashboard.scss

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
flex: 1;
4040
}
4141

42-
.control:nth-of-type(n + 6) {
43-
flex-basis: 49%;
44-
align-self: flex-end;
45-
}
46-
4742
// Hides tag selector when showing subcategories selector
4843
.control:nth-of-type(6):nth-last-of-type(3) {
4944
display: none;
@@ -59,7 +54,6 @@
5954
display: flex;
6055
flex-flow: row wrap;
6156
order: 2;
62-
gap: 1rem;
6357
align-items: flex-start;
6458
max-height: 100vh;
6559
}
@@ -70,29 +64,29 @@
7064
flex: 2;
7165
display: flex;
7266
flex-flow: row wrap;
73-
gap: 3rem;
7467

7568
.admin-report-doughnut {
76-
max-width: 300px;
77-
max-height: 300px;
7869
padding: 0.25rem;
7970
}
8071

8172
&__chart-wrapper {
82-
width: auto;
73+
height: fit-content;
74+
position: relative;
75+
transition: transform 0.25s ease, box-shadow 0.25s ease;
76+
border-radius: var(--d-border-radius);
77+
8378
.doughnut-chart-title {
8479
@include ellipsis;
85-
margin: 0 auto;
86-
text-align: center;
87-
margin-bottom: 1rem;
88-
max-width: 300px;
80+
position: absolute;
81+
top: 3%;
82+
left: 50%;
83+
transform: translateX(-50%);
84+
font-size: var(--font-0);
85+
z-index: 999;
8986
}
90-
transition: transform 0.25s ease, box-shadow 0.25s ease;
91-
border-radius: var(--d-border-radius);
9287

9388
&:hover {
9489
transform: translateY(-1rem);
95-
box-shadow: var(--shadow-card);
9690
cursor: pointer;
9791
}
9892
}
@@ -125,7 +119,7 @@
125119
@include report-container-box();
126120
flex: 1 1 300px;
127121
min-width: 300px;
128-
122+
margin-left: 1rem;
129123
display: flex;
130124
flex-flow: column nowrap;
131125
overflow-y: auto;

0 commit comments

Comments
 (0)