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.

LineChart.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 { extent, max } from 'd3-array';
  21. import { scaleLinear, ScaleLinear } from 'd3-scale';
  22. import { area as d3Area, curveBasis, line as d3Line } from 'd3-shape';
  23. import * as React from 'react';
  24. import { AutoSizer } from 'react-virtualized/dist/commonjs/AutoSizer';
  25. import './LineChart.css';
  26. interface DataPoint {
  27. x: number;
  28. y?: number;
  29. }
  30. interface Props {
  31. backdropConstraints?: [number, number];
  32. data: DataPoint[];
  33. displayBackdrop?: boolean;
  34. displayPoints?: boolean;
  35. displayVerticalGrid?: boolean;
  36. domain?: [number, number];
  37. height: number;
  38. padding?: [number, number, number, number];
  39. width?: number;
  40. xTicks?: {}[];
  41. xValues?: {}[];
  42. }
  43. export default class LineChart extends React.PureComponent<Props> {
  44. renderBackdrop(xScale: ScaleLinear<number, number>, yScale: ScaleLinear<number, number>) {
  45. const { displayBackdrop = true } = this.props;
  46. if (!displayBackdrop) {
  47. return null;
  48. }
  49. const area = d3Area<DataPoint>()
  50. .x((d) => xScale(d.x))
  51. .y0(yScale.range()[0])
  52. .y1((d) => yScale(d.y || 0))
  53. .defined((d) => d.y != null)
  54. .curve(curveBasis);
  55. let { data } = this.props;
  56. if (this.props.backdropConstraints) {
  57. const c = this.props.backdropConstraints;
  58. data = data.filter((d) => c[0] <= d.x && d.x <= c[1]);
  59. }
  60. return <path className="line-chart-backdrop" d={area(data) as string} />;
  61. }
  62. renderPoints(xScale: ScaleLinear<number, number>, yScale: ScaleLinear<number, number>) {
  63. const { displayPoints = true } = this.props;
  64. if (!displayPoints) {
  65. return null;
  66. }
  67. const points = this.props.data
  68. .filter((point) => point.y != null)
  69. .map((point, index) => {
  70. const x = xScale(point.x);
  71. const y = yScale(point.y || 0);
  72. return <circle className="line-chart-point" cx={x} cy={y} key={index} r="3" />;
  73. });
  74. return <g>{points}</g>;
  75. }
  76. renderVerticalGrid(xScale: ScaleLinear<number, number>, yScale: ScaleLinear<number, number>) {
  77. const { displayVerticalGrid = true } = this.props;
  78. if (!displayVerticalGrid) {
  79. return null;
  80. }
  81. const lines = this.props.data.map((point, index) => {
  82. const x = xScale(point.x);
  83. const y1 = yScale.range()[0];
  84. const y2 = yScale(point.y || 0);
  85. return <line className="line-chart-grid" key={index} x1={x} x2={x} y1={y1} y2={y2} />;
  86. });
  87. return <g>{lines}</g>;
  88. }
  89. renderXTicks(xScale: ScaleLinear<number, number>, yScale: ScaleLinear<number, number>) {
  90. const { xTicks = [] } = this.props;
  91. if (!xTicks.length) {
  92. return null;
  93. }
  94. const ticks = xTicks.map((tick, index) => {
  95. const point = this.props.data[index];
  96. const x = xScale(point.x);
  97. const y = yScale.range()[0];
  98. return (
  99. <text className="line-chart-tick" dy="1.5em" key={index} x={x} y={y}>
  100. {tick}
  101. </text>
  102. );
  103. });
  104. return <g>{ticks}</g>;
  105. }
  106. renderXValues(xScale: ScaleLinear<number, number>, yScale: ScaleLinear<number, number>) {
  107. const { xValues = [] } = this.props;
  108. if (!xValues.length) {
  109. return null;
  110. }
  111. const ticks = xValues.map((value, index) => {
  112. const point = this.props.data[index];
  113. const x = xScale(point.x);
  114. const y = yScale(point.y || 0);
  115. return (
  116. <text className="line-chart-tick" dy="-1em" key={index} x={x} y={y}>
  117. {value}
  118. </text>
  119. );
  120. });
  121. return <g>{ticks}</g>;
  122. }
  123. renderLine(xScale: ScaleLinear<number, number>, yScale: ScaleLinear<number, number>) {
  124. const p = d3Line<DataPoint>()
  125. .x((d) => xScale(d.x))
  126. .y((d) => yScale(d.y || 0))
  127. .defined((d) => d.y != null)
  128. .curve(curveBasis);
  129. return <path className="line-chart-path" d={p(this.props.data) as string} />;
  130. }
  131. renderChart = (width: number) => {
  132. const { height, padding = [10, 10, 10, 10] } = this.props;
  133. if (!width || !height) {
  134. return <div />;
  135. }
  136. const availableWidth = width - padding[1] - padding[3];
  137. const availableHeight = height - padding[0] - padding[2];
  138. const xScale = scaleLinear()
  139. .domain(extent(this.props.data, (d) => d.x) as [number, number])
  140. .range([0, availableWidth]);
  141. const yScale = scaleLinear().range([availableHeight, 0]);
  142. if (this.props.domain) {
  143. yScale.domain(this.props.domain);
  144. } else {
  145. const maxY = max(this.props.data, (d) => d.y) as number;
  146. yScale.domain([0, maxY]);
  147. }
  148. return (
  149. <svg className="line-chart" height={height} width={width}>
  150. <g transform={`translate(${padding[3]}, ${padding[0]})`}>
  151. {this.renderVerticalGrid(xScale, yScale)}
  152. {this.renderBackdrop(xScale, yScale)}
  153. {this.renderLine(xScale, yScale)}
  154. {this.renderPoints(xScale, yScale)}
  155. {this.renderXTicks(xScale, yScale)}
  156. {this.renderXValues(xScale, yScale)}
  157. </g>
  158. </svg>
  159. );
  160. };
  161. render() {
  162. return this.props.width !== undefined ? (
  163. this.renderChart(this.props.width)
  164. ) : (
  165. <AutoSizer disableHeight={true}>{(size) => this.renderChart(size.width)}</AutoSizer>
  166. );
  167. }
  168. }