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.

ColorGradientLegend.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 { ScaleLinear, ScaleOrdinal } from 'd3-scale';
  21. import * as React from 'react';
  22. import { ThemeConsumer } from '../theme';
  23. import './ColorGradientLegend.css';
  24. interface Props {
  25. className?: string;
  26. colorScale:
  27. | ScaleOrdinal<string, string> // used for LEVEL type
  28. | ScaleLinear<string, string | number>; // used for RATING or PERCENT type
  29. height: number;
  30. padding?: [number, number, number, number];
  31. showColorNA?: boolean;
  32. width: number;
  33. }
  34. const NA_SPACING = 4;
  35. const NA_GRADIENT_LINE_INCREMENTS = [0, 8, 16, 24];
  36. export default function ColorGradientLegend({
  37. className,
  38. colorScale,
  39. padding = [12, 24, 0, 0],
  40. height,
  41. showColorNA = false,
  42. width,
  43. }: Props) {
  44. const colorRange: Array<string | number> = colorScale.range();
  45. const colorDomain: Array<string | number> = colorScale.domain();
  46. const lastColorIdx = colorRange.length - 1;
  47. const lastDomainIdx = colorDomain.length - 1;
  48. const widthNoPadding = width - padding[1];
  49. const rectHeight = height - padding[0];
  50. return (
  51. <ThemeConsumer>
  52. {({ colors }) => (
  53. <svg className={className} height={height} width={width}>
  54. <defs>
  55. <linearGradient id="gradient-legend">
  56. {colorRange.map((color, idx) => (
  57. <stop key={idx} offset={idx / lastColorIdx} stopColor={String(color)} />
  58. ))}
  59. </linearGradient>
  60. <pattern
  61. id="stripes"
  62. width="30"
  63. height="30"
  64. patternTransform="rotate(45 0 0)"
  65. patternUnits="userSpaceOnUse">
  66. {NA_GRADIENT_LINE_INCREMENTS.map((i) => (
  67. <React.Fragment key={i}>
  68. <line
  69. x1={i}
  70. y1="0"
  71. x2={i}
  72. y2="30"
  73. style={{ stroke: colors.gray71, strokeWidth: NA_SPACING }}
  74. />
  75. <line
  76. x1={i + NA_SPACING}
  77. y1="0"
  78. x2={i + NA_SPACING}
  79. y2="30"
  80. style={{ stroke: colors.gray60, strokeWidth: NA_SPACING }}
  81. />
  82. </React.Fragment>
  83. ))}
  84. </pattern>
  85. </defs>
  86. <g transform={`translate(${padding[3]}, ${padding[0]})`}>
  87. <rect
  88. fill="url(#gradient-legend)"
  89. height={rectHeight}
  90. width={widthNoPadding}
  91. x={0}
  92. y={0}
  93. />
  94. {colorDomain.map((d, idx) => (
  95. <text
  96. className="gradient-legend-text"
  97. dy="-2px"
  98. key={idx}
  99. x={widthNoPadding * (idx / lastDomainIdx)}
  100. y={0}>
  101. {d}
  102. </text>
  103. ))}
  104. </g>
  105. {showColorNA && (
  106. <g transform={`translate(${widthNoPadding}, ${padding[0]})`}>
  107. <rect
  108. fill="url(#stripes)"
  109. height={rectHeight}
  110. width={padding[1] - NA_SPACING}
  111. x={NA_SPACING}
  112. y={0}
  113. />
  114. <text
  115. className="gradient-legend-na"
  116. dy="-2px"
  117. x={NA_SPACING + (padding[1] - NA_SPACING) / 2}
  118. y={0}>
  119. N/A
  120. </text>
  121. </g>
  122. )}
  123. </svg>
  124. )}
  125. </ThemeConsumer>
  126. );
  127. }