ApiGauge.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { useEffect, useRef, useState } from "react";
  2. import { Gauge } from "@ant-design/plots";
  3. import { get } from "../../../request";
  4. import { StatisticCard } from "@ant-design/pro-components";
  5. interface IApiResponse {
  6. ok: boolean;
  7. message: string;
  8. data: number;
  9. }
  10. const ApiGaugeWidget = () => {
  11. const min = 0;
  12. const max = 1;
  13. const [percent, setPercent] = useState<number>(0);
  14. const [delay, setDelay] = useState<number>(0);
  15. const maxAxis = 5000; //最大量程-毫秒
  16. useEffect(() => {
  17. const timer = setInterval(() => {
  18. get<IApiResponse>("/v2/api/10?item=average").then((json) => {
  19. setPercent(json.data / maxAxis);
  20. setDelay(json.data);
  21. });
  22. }, 1000 * 5);
  23. return () => {
  24. clearInterval(timer);
  25. };
  26. }, []);
  27. const graphRef: any = useRef(null);
  28. const config = {
  29. percent: percent,
  30. range: {
  31. ticks: [min, max],
  32. color: ["l(0) 0:#30BF78 0.5:#FAAD14 1:#F4664A"],
  33. },
  34. indicator: {
  35. pointer: {
  36. style: {
  37. stroke: "#D0D0D0",
  38. },
  39. },
  40. pin: {
  41. style: {
  42. stroke: "#D0D0D0",
  43. },
  44. },
  45. },
  46. axis: {
  47. label: {
  48. formatter(v: any) {
  49. return Number(v) * maxAxis;
  50. },
  51. },
  52. subTickLine: {
  53. count: 3,
  54. },
  55. },
  56. };
  57. return (
  58. <StatisticCard
  59. style={{ width: 400 }}
  60. statistic={{
  61. title: "平均相应时间",
  62. value: delay,
  63. suffix: "/ ms",
  64. }}
  65. chart={
  66. <Gauge
  67. ref={graphRef}
  68. {...config}
  69. onReady={(chart) => {
  70. graphRef.current = chart;
  71. }}
  72. />
  73. }
  74. />
  75. );
  76. };
  77. export default ApiGaugeWidget;