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.

TreeMapRect.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 * as classNames from 'classnames';
  21. import { scaleLinear } from 'd3-scale';
  22. import * as React from 'react';
  23. import { Link } from 'react-router';
  24. import { Location } from '../../helpers/urls';
  25. import Tooltip, { Placement } from '../controls/Tooltip';
  26. import LinkIcon from '../icons/LinkIcon';
  27. const SIZE_SCALE = scaleLinear().domain([3, 15]).range([11, 18]).clamp(true);
  28. interface Props {
  29. fill?: string;
  30. gradient?: string;
  31. height: number;
  32. icon?: React.ReactNode;
  33. itemKey: string;
  34. label: string;
  35. link?: string | Location;
  36. onClick?: (item: string) => void;
  37. placement?: Placement;
  38. prefix: string;
  39. tooltip?: React.ReactNode;
  40. value?: React.ReactNode;
  41. width: number;
  42. x: number;
  43. y: number;
  44. }
  45. const TEXT_VISIBLE_AT_WIDTH = 80;
  46. const TEXT_VISIBLE_AT_HEIGHT = 50;
  47. const ICON_VISIBLE_AT_WIDTH = 60;
  48. const ICON_VISIBLE_AT_HEIGHT = 30;
  49. export default class TreeMapRect extends React.PureComponent<Props> {
  50. handleLinkClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
  51. event.stopPropagation();
  52. };
  53. handleRectClick = () => {
  54. if (this.props.onClick) {
  55. this.props.onClick(this.props.itemKey);
  56. }
  57. };
  58. renderLink = () => {
  59. const { link, height, width } = this.props;
  60. const hasMinSize = width >= 24 && height >= 24 && (width >= 48 || height >= 50);
  61. if (!hasMinSize || link == null) {
  62. return null;
  63. }
  64. return (
  65. <Link className="treemap-link" onClick={this.handleLinkClick} to={link}>
  66. <LinkIcon />
  67. </Link>
  68. );
  69. };
  70. renderCell = () => {
  71. const cellStyles = {
  72. left: this.props.x,
  73. top: this.props.y,
  74. width: this.props.width,
  75. height: this.props.height,
  76. backgroundColor: this.props.fill,
  77. backgroundImage: this.props.gradient,
  78. backgroundSize: '12px 12px',
  79. fontSize: SIZE_SCALE(this.props.width / this.props.label.length),
  80. lineHeight: `${this.props.height}px`,
  81. cursor: this.props.onClick != null ? 'pointer' : 'default',
  82. };
  83. const isTextVisible =
  84. this.props.width >= TEXT_VISIBLE_AT_WIDTH && this.props.height >= TEXT_VISIBLE_AT_HEIGHT;
  85. const isIconVisible =
  86. this.props.width >= ICON_VISIBLE_AT_WIDTH && this.props.height >= ICON_VISIBLE_AT_HEIGHT;
  87. return (
  88. <div
  89. className="treemap-cell"
  90. onClick={this.handleRectClick}
  91. role="treeitem"
  92. style={cellStyles}
  93. tabIndex={0}>
  94. {isTextVisible && (
  95. <div className="treemap-inner" style={{ maxWidth: this.props.width }}>
  96. {this.props.prefix || this.props.value ? (
  97. <div className="treemap-text">
  98. <div>
  99. {isIconVisible && (
  100. <span className={classNames('treemap-icon', { 'spacer-right': isTextVisible })}>
  101. {this.props.icon}
  102. </span>
  103. )}
  104. {this.props.prefix && (
  105. <>
  106. {this.props.prefix}
  107. <br />
  108. </>
  109. )}
  110. {this.props.label.substr(this.props.prefix.length)}
  111. </div>
  112. <div className="treemap-text-suffix little-spacer-top">{this.props.value}</div>
  113. </div>
  114. ) : (
  115. <div className="treemap-text">{this.props.label}</div>
  116. )}
  117. </div>
  118. )}
  119. {this.renderLink()}
  120. </div>
  121. );
  122. };
  123. render() {
  124. const { placement, tooltip } = this.props;
  125. return (
  126. <Tooltip overlay={tooltip || undefined} placement={placement || 'left'}>
  127. {this.renderCell()}
  128. </Tooltip>
  129. );
  130. }
  131. }