Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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