Some stylistic changes to pie charts and to the disk space widget

main
Antonio De Lucreziis 2 years ago
parent 50e1fd4253
commit c54bf850fa

@ -3,11 +3,12 @@ import { hashCode } from '../../util.jsx'
let pieChartPiecesSeed = 934635493 let pieChartPiecesSeed = 934635493
export const PieChart = ({ parts, labels, total }) => { export const PieChart = ({ parts, labels, total, totalLabel }) => {
const [seed] = useState(() => pieChartPiecesSeed++) const [seed] = useState(() => pieChartPiecesSeed++)
parts = parts || [1] parts ??= [1]
labels = labels || parts labels ??= parts
totalLabel ??= ''
const canvasRef = useRef() const canvasRef = useRef()
@ -33,7 +34,7 @@ export const PieChart = ({ parts, labels, total }) => {
g.scale(2, 2) g.scale(2, 2)
g.translate(width / 2, height / 2) g.translate(width / 2, height / 2)
g.font = `16px 'Open Sans'` g.font = `bold 16px 'Open Sans'`
g.textAlign = 'center' g.textAlign = 'center'
g.textBaseline = 'middle' g.textBaseline = 'middle'
@ -65,15 +66,31 @@ export const PieChart = ({ parts, labels, total }) => {
g.lineTo(0, 0) g.lineTo(0, 0)
g.stroke() g.stroke()
g.fillStyle = '#333' const { width: w } = g.measureText(label)
const textRadius = Math.sqrt(w ** 2 + 16 ** 2) / 2
g.fillStyle = `hsl(${((hashCode(`${i} ${seed}`) % 0xff) * 360) / 0xff}, 80%, 15%)`
g.fillText( g.fillText(
label, label,
Math.cos(acc + angle / 2 - 0.5 * Math.PI) * width * 0.5 * 0.9125, Math.cos(acc + angle / 2 - 0.5 * Math.PI) *
Math.sin(acc + angle / 2 - 0.5 * Math.PI) * width * 0.5 * 0.9125 (width * 0.5 * 0.7 - textRadius * 0.8),
Math.sin(acc + angle / 2 - 0.5 * Math.PI) *
(width * 0.5 * 0.7 - textRadius * 0.8)
) )
acc += angle acc += angle
}) })
if (totalLabel && Math.abs(2 * Math.PI - acc) > Math.PI / 32) {
const totalLabelAngle = acc + (2 * Math.PI - acc) / 2
g.fillStyle = '#333'
g.fillText(
totalLabel,
Math.cos(totalLabelAngle - 0.5 * Math.PI) * width * 0.5 * 0.5,
Math.sin(totalLabelAngle - 0.5 * Math.PI) * width * 0.5 * 0.5
)
}
} }
}, [canvasRef, parts, labels]) }, [canvasRef, parts, labels])

@ -44,7 +44,17 @@ export const DiskUsage = ({ disk, diskLabel }) => {
{err ? ( {err ? (
<p>Errore "{err}"</p> <p>Errore "{err}"</p>
) : ( ) : (
<PieChart parts={[used]} labels={[byteSizeToString(used)]} total={total} /> <>
<PieChart
parts={[used]}
labels={[byteSizeToString(used)]}
total={total}
totalLabel={''}
/>
<p class="center">
Spazio totale <strong>{byteSizeToString(total)}</strong>
</p>
</>
)} )}
</div> </div>
</> </>

@ -526,6 +526,10 @@ p {
& + p { & + p {
margin-top: 0.5rem; margin-top: 0.5rem;
} }
&.center {
text-align: center;
}
} }
pre { pre {

Loading…
Cancel
Save