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.

CoverageRating.tsx 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 * as React from 'react';
  21. import { colors } from '../../app/theme';
  22. import DonutChart from '../../components/charts/DonutChart';
  23. const SIZE_TO_WIDTH_MAPPING = { small: 16, normal: 24, big: 40, huge: 60 };
  24. const SIZE_TO_THICKNESS_MAPPING = { small: 2, normal: 3, big: 3, huge: 4 };
  25. export interface CoverageRatingProps {
  26. muted?: boolean;
  27. size?: 'small' | 'normal' | 'big' | 'huge';
  28. value: number | string | null | undefined;
  29. }
  30. export default function CoverageRating({
  31. muted = false,
  32. size = 'normal',
  33. value
  34. }: CoverageRatingProps) {
  35. let data = [{ value: 100, fill: '#ccc ' }];
  36. let padAngle = 0;
  37. if (value != null) {
  38. const numberValue = Number(value);
  39. data = [
  40. { value: numberValue, fill: muted ? colors.gray71 : colors.green },
  41. { value: 100 - numberValue, fill: muted ? colors.barBackgroundColor : colors.lineCoverageRed }
  42. ];
  43. if (numberValue !== 0 && numberValue < 100) {
  44. padAngle = 0.1; // Same for all sizes, because it scales automatically
  45. }
  46. }
  47. const width = SIZE_TO_WIDTH_MAPPING[size];
  48. const thickness = SIZE_TO_THICKNESS_MAPPING[size];
  49. return (
  50. <DonutChart
  51. data={data}
  52. height={width}
  53. padAngle={padAngle}
  54. thickness={thickness}
  55. width={width}
  56. />
  57. );
  58. }