CustomTooltip.jsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/Typography';
  19. import {styles} 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. // unit contains the units for the bytePlotter.
  35. const unit = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
  36. // simplifyBytes returns the simplified version of the given value followed by the unit.
  37. const simplifyBytes = (x: number) => {
  38. let i = 0;
  39. for (; x > 1024 && i < 8; i++) {
  40. x /= 1024;
  41. }
  42. return x.toFixed(2).toString().concat(' ', unit[i], 'B');
  43. };
  44. // bytePlotter renders a tooltip, which displays the payload as a byte value.
  45. export const bytePlotter = <T>(text: string, mapper: (T => T) = multiplier(1)) => (payload: T) => {
  46. const p = mapper(payload);
  47. if (typeof p !== 'number') {
  48. return null;
  49. }
  50. return (
  51. <Typography type='caption' color='inherit'>
  52. <span style={styles.light}>{text}</span> {simplifyBytes(p)}
  53. </Typography>
  54. );
  55. };
  56. // bytePlotter renders a tooltip, which displays the payload as a byte value followed by '/s'.
  57. export const bytePerSecPlotter = <T>(text: string, mapper: (T => T) = multiplier(1)) => (payload: T) => {
  58. const p = mapper(payload);
  59. if (typeof p !== 'number') {
  60. return null;
  61. }
  62. return (
  63. <Typography type='caption' color='inherit'>
  64. <span style={styles.light}>{text}</span> {simplifyBytes(p)}/s
  65. </Typography>
  66. );
  67. };
  68. export type Props = {
  69. active: boolean,
  70. payload: Object,
  71. tooltip: <T>(text: string, mapper?: T => T) => (payload: mixed) => null | React$Element<any>,
  72. };
  73. // CustomTooltip takes a tooltip function, and uses it to plot the active value of the chart.
  74. class CustomTooltip extends Component<Props> {
  75. render() {
  76. const {active, payload, tooltip} = this.props;
  77. if (!active || typeof tooltip !== 'function' || !Array.isArray(payload) || payload.length < 1) {
  78. return null;
  79. }
  80. return tooltip(payload[0].value);
  81. }
  82. }
  83. export default CustomTooltip;