| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import { useEffect, useRef, useState } from "react";
- import { Gauge } from "@ant-design/plots";
- import { get } from "../../../request";
- import { StatisticCard } from "@ant-design/pro-components";
- interface IApiResponse {
- ok: boolean;
- message: string;
- data: number;
- }
- const ApiGaugeWidget = () => {
- const min = 0;
- const max = 1;
- const [percent, setPercent] = useState<number>(0);
- const [delay, setDelay] = useState<number>(0);
- const maxAxis = 5000; //最大量程-毫秒
- useEffect(() => {
- const timer = setInterval(() => {
- get<IApiResponse>("/v2/api/10?item=average").then((json) => {
- setPercent(json.data / maxAxis);
- setDelay(json.data);
- });
- }, 1000 * 5);
- return () => {
- clearInterval(timer);
- };
- }, []);
- const graphRef: any = useRef(null);
- const config = {
- percent: percent,
- range: {
- ticks: [min, max],
- color: ["l(0) 0:#30BF78 0.5:#FAAD14 1:#F4664A"],
- },
- indicator: {
- pointer: {
- style: {
- stroke: "#D0D0D0",
- },
- },
- pin: {
- style: {
- stroke: "#D0D0D0",
- },
- },
- },
- axis: {
- label: {
- formatter(v: any) {
- return Number(v) * maxAxis;
- },
- },
- subTickLine: {
- count: 3,
- },
- },
- };
- return (
- <StatisticCard
- style={{ width: 400 }}
- statistic={{
- title: "平均相应时间",
- value: delay,
- suffix: "/ ms",
- }}
- chart={
- <Gauge
- ref={graphRef}
- {...config}
- onReady={(chart) => {
- graphRef.current = chart;
- }}
- />
- }
- />
- );
- };
- export default ApiGaugeWidget;
|