Advertisement
materialblock

ChartHealth.js

Nov 17th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect, useRef, PureComponent } from "react";
  2. import { useTheme } from "@material-ui/core/styles";
  3. import { LineChart,Line,XAxis,YAxis,Label,ResponsiveContainer,Tooltip,} from "recharts";
  4. import Title from "../interface/TittleNav";
  5. class CustomizedLabel extends PureComponent {
  6.   render() {
  7.     const { x, y, stroke, value } = this.props;
  8.  
  9.     return (
  10.       <text x={x} y={y} dy={-4} fill={stroke} fontSize={10} textAnchor="middle">
  11.         {value}
  12.       </text>
  13.     );
  14.   }
  15. }
  16. export default function Chart() {
  17.   const theme = useTheme();
  18.   const ws = useRef(null);
  19.  
  20.   const [barData, setBarData] = useState({});
  21.  
  22.   useEffect(() => {
  23.     ws.current = new WebSocket("wss://elepsio.herokuapp.com/");
  24.     ws.current.onopen = () => console.log("ws opened");
  25.     ws.current.onclose = () => console.log("ws closed");
  26.  
  27.     return () => {
  28.       ws.current.close();
  29.     };
  30.   }, []);
  31.   useEffect(() => {
  32.     if (!ws.current) return;
  33.  
  34.     ws.current.onmessage = function (event) {
  35.       setBarData({ data: [...barData.data, JSON.parse(event.data)] });
  36.     };
  37.   }, [barData]);
  38.  
  39.   console.log(barData);
  40.  
  41.   return (
  42.     <React.Fragment>
  43.       <Title>График</Title>
  44.       <ResponsiveContainer>
  45.         <LineChart
  46.           isAnimationActive="false"
  47.           data={barData.data}
  48.           margin={{
  49.             top: 16,
  50.             right: 16,
  51.             bottom: 0,
  52.             left: 24,
  53.           }}
  54.         >
  55.           <XAxis dataKey="time" stroke={theme.palette.text.secondary} />
  56.           <YAxis stroke={theme.palette.text.secondary}>
  57.             <Label
  58.               angle={270}
  59.               position="left"
  60.               style={{ textAnchor: "middle", fill: theme.palette.text.primary }}
  61.             >
  62.               Пульс
  63.             </Label>
  64.           </YAxis>
  65.           <Line
  66.             isAnimationActive={false}
  67.             type="monotone"
  68.             dataKey="pulse"
  69.             stroke={theme.palette.primary.main}
  70.             dot={false}
  71.             label={<CustomizedLabel />}
  72.           />
  73.         </LineChart>
  74.       </ResponsiveContainer>
  75.     </React.Fragment>
  76.   );
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement