You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DonutChart.tsx 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { arc as d3Arc, pie as d3Pie, PieArcDatum } from 'd3-shape';
  21. import * as React from 'react';
  22. interface DataPoint {
  23. fill: string;
  24. value: number;
  25. }
  26. export interface DonutChartProps {
  27. data: DataPoint[];
  28. height: number;
  29. padAngle?: number;
  30. padding?: [number, number, number, number];
  31. thickness: number;
  32. width: number;
  33. }
  34. export default function DonutChart(props: DonutChartProps) {
  35. const { height, padding = [0, 0, 0, 0], width } = props;
  36. const availableWidth = width - padding[1] - padding[3];
  37. const availableHeight = height - padding[0] - padding[2];
  38. const size = Math.min(availableWidth, availableHeight);
  39. const radius = Math.floor(size / 2);
  40. const pie = d3Pie<any, DataPoint>()
  41. .sort(null)
  42. .value((d) => d.value);
  43. if (props.padAngle !== undefined) {
  44. pie.padAngle(props.padAngle);
  45. }
  46. const sectors = pie(props.data).map((d, i) => {
  47. return (
  48. <Sector
  49. data={d}
  50. fill={props.data[i].fill}
  51. key={i}
  52. radius={radius}
  53. thickness={props.thickness}
  54. />
  55. );
  56. });
  57. return (
  58. <svg className="donut-chart" height={height} width={width}>
  59. <g transform={`translate(${padding[3]}, ${padding[0]})`}>
  60. <g transform={`translate(${radius}, ${radius})`}>{sectors}</g>
  61. </g>
  62. </svg>
  63. );
  64. }
  65. interface SectorProps {
  66. data: PieArcDatum<DataPoint>;
  67. fill: string;
  68. radius: number;
  69. thickness: number;
  70. }
  71. function Sector(props: SectorProps) {
  72. const arc = d3Arc<any, PieArcDatum<DataPoint>>()
  73. .outerRadius(props.radius)
  74. .innerRadius(props.radius - props.thickness);
  75. const d = arc(props.data) as string;
  76. return <path d={d} style={{ fill: props.fill }} />;
  77. }