CustomTooltip.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @flow
  2. // Copyright 2018 The go-ethereum Authors
  3. // This file is part of the go-ethereum library.
  4. //
  5. // The go-ethereum library is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // The go-ethereum library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  17. import React, {Component} from 'react';
  18. import Typography from '@material-ui/core/Typography';
  19. import {styles, simplifyBytes} from '../common';
  20. // multiplier multiplies a number by another.
  21. export const multiplier = <T>(by: number = 1) => (x: number) => x * by;
  22. // percentPlotter renders a tooltip, which displays the value of the payload followed by a percent sign.
  23. export const percentPlotter = <T>(text: string, mapper: (T => T) = multiplier(1)) => (payload: T) => {
  24. const p = mapper(payload);
  25. if (typeof p !== 'number') {
  26. return null;
  27. }
  28. return (
  29. <Typography type='caption' color='inherit'>
  30. <span style={styles.light}>{text}</span> {p.toFixed(2)} %
  31. </Typography>
  32. );
  33. };
  34. // bytePlotter renders a tooltip, which displays the payload as a byte value.
  35. export const bytePlotter = <T>(text: string, mapper: (T => T) = multiplier(1)) => (payload: T) => {
  36. const p = mapper(payload);
  37. if (typeof p !== 'number') {
  38. return null;
  39. }
  40. return (
  41. <Typography type='caption' color='inherit'>
  42. <span style={styles.light}>{text}</span> {simplifyBytes(p)}
  43. </Typography>
  44. );
  45. };
  46. // bytePlotter renders a tooltip, which displays the payload as a byte value followed by '/s'.
  47. export const bytePerSecPlotter = <T>(text: string, mapper: (T => T) = multiplier(1)) => (payload: T) => {
  48. const p = mapper(payload);
  49. if (typeof p !== 'number') {
  50. return null;
  51. }
  52. return (
  53. <Typography type='caption' color='inherit'>
  54. <span style={styles.light}>{text}</span>
  55. {simplifyBytes(p)}/s
  56. </Typography>
  57. );
  58. };
  59. export type Props = {
  60. active: boolean,
  61. payload: Object,
  62. tooltip: <T>(text: string, mapper?: T => T) => (payload: mixed) => null | React$Element<any>,
  63. };
  64. // CustomTooltip takes a tooltip function, and uses it to plot the active value of the chart.
  65. class CustomTooltip extends Component<Props> {
  66. render() {
  67. const {active, payload, tooltip} = this.props;
  68. if (!active || typeof tooltip !== 'function' || !Array.isArray(payload) || payload.length < 1) {
  69. return null;
  70. }
  71. return tooltip(payload[0].value);
  72. }
  73. }
  74. export default CustomTooltip;