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.

TreeMap.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { hierarchy as d3Hierarchy, treemap as d3Treemap } from 'd3-hierarchy';
  21. import * as React from 'react';
  22. import { formatMeasure, localizeMetric } from '../../helpers/measures';
  23. import { Location } from '../../helpers/urls';
  24. import './TreeMap.css';
  25. import TreeMapRect from './TreeMapRect';
  26. export interface TreeMapItem {
  27. color?: string;
  28. gradient?: string;
  29. icon?: React.ReactNode;
  30. key: string;
  31. label: string;
  32. link?: string | Location;
  33. measureValue?: string;
  34. metric?: { key: string; type: string };
  35. size: number;
  36. tooltip?: React.ReactNode;
  37. }
  38. interface HierarchicalTreemapItem extends TreeMapItem {
  39. children?: TreeMapItem[];
  40. }
  41. interface Props {
  42. height: number;
  43. items: TreeMapItem[];
  44. onRectangleClick?: (item: string) => void;
  45. width: number;
  46. }
  47. export default class TreeMap extends React.PureComponent<Props> {
  48. mostCommitPrefix = (labels: string[]) => {
  49. const sortedLabels = labels.slice(0).sort();
  50. const firstLabel = sortedLabels[0];
  51. const firstLabelLength = firstLabel.length;
  52. const lastLabel = sortedLabels[sortedLabels.length - 1];
  53. let i = 0;
  54. while (i < firstLabelLength && firstLabel.charAt(i) === lastLabel.charAt(i)) {
  55. i++;
  56. }
  57. const prefix = firstLabel.substr(0, i);
  58. const prefixTokens = prefix.split(/[\s\\/]/);
  59. const lastPrefixPart = prefixTokens[prefixTokens.length - 1];
  60. return prefix.substr(0, prefix.length - lastPrefixPart.length);
  61. };
  62. render() {
  63. const { items, height, width } = this.props;
  64. const hierarchy = d3Hierarchy({ children: items } as HierarchicalTreemapItem)
  65. .sum((d) => d.size)
  66. .sort((a, b) => (b.value || 0) - (a.value || 0));
  67. const treemap = d3Treemap<TreeMapItem>().round(true).size([width, height]);
  68. const nodes = treemap(hierarchy).leaves();
  69. const prefix = this.mostCommitPrefix(items.map((item) => item.label));
  70. const halfWidth = width / 2;
  71. return (
  72. <div className="sonar-d3">
  73. <div className="treemap-container" style={{ width, height }}>
  74. {nodes.map((node) => (
  75. <TreeMapRect
  76. fill={node.data.color}
  77. gradient={node.data.gradient}
  78. height={node.y1 - node.y0}
  79. icon={node.data.icon}
  80. itemKey={node.data.key}
  81. key={node.data.key}
  82. label={node.data.label}
  83. link={node.data.link}
  84. onClick={this.props.onRectangleClick}
  85. placement={node.x0 === 0 || node.x1 < halfWidth ? 'right' : 'left'}
  86. prefix={prefix}
  87. value={
  88. node.data.metric && (
  89. <>
  90. {formatMeasure(node.data.measureValue, node.data.metric.type)}
  91. <span className="little-spacer-left">
  92. {localizeMetric(node.data.metric.key)}
  93. </span>
  94. </>
  95. )
  96. }
  97. tooltip={node.data.tooltip}
  98. width={node.x1 - node.x0}
  99. x={node.x0}
  100. y={node.y0}
  101. />
  102. ))}
  103. </div>
  104. </div>
  105. );
  106. }
  107. }