All files / src/pages/flowsheet-page/pinch-analysis PinchGraph.tsx

5.4% Statements 2/37
0% Branches 0/17
0% Functions 0/10
6.45% Lines 2/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142                            33x     33x                                                                                                                                                                                                                                                        
import { Card, CardHeader, CardTitle } from "@/ahuora-design-system/ui/card";
import { PinchGraphRead } from "@/api/apiStore.gen";
import { useState } from "react";
import {
  Label,
  Legend,
  Line,
  LineChart,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";
 
const lineColours = ["#FF4500", "#8884d8", "#FFFF00", "#D3D3D3"];
 
 
const CustomTooltip = ({ active, payload, label }) => {
  Iif (active && payload && payload.length) {
    return (
      <div className="bg-zinc-700 p-2">
        <p className="text-xs">{`x: ${payload[0].payload.x}`}</p>
        <p className="text-xs">{`y: ${payload[0].value}`}</p>
      </div>
    );
  }
  return null;
};
 
function getXValueRage(segments) {
    let maxX = 0;
    let minX = 0;
    segments.forEach(segment => {
        const segmentMaxX = Math.max(...segment.data_points.map(point => point.x));
        const segmentMinX = Math.min(...segment.data_points.map(point => point.x));
        Iif (segmentMaxX > maxX) {
            maxX = segmentMaxX;
        }
        Iif (segmentMinX < minX) {
            minX = segmentMinX
        }
    });
    return [minX, maxX];
}
 
function getXAxisDomain(minX, maxX) {
    const numPoints = 4;
    const roundedMaxX = Math.ceil((maxX.toPrecision(Math.ceil(maxX).toString().length))/100)*100;
    const roundedMinX = Math.floor((minX.toPrecision(Math.ceil(minX).toString().length))/100)*100;
    const returnArray: Number[] = [];
    Iif (roundedMinX<0){returnArray.push(0)}
    const slice = (roundedMaxX+ Math.abs(roundedMinX)) / numPoints;
    for (let i = 0; i <= numPoints; i++) {
        returnArray.push(roundedMinX+(slice * i));
    }
 
    return returnArray.sort((a,b)=>a - b)
}
 
interface PinchGraphProps {
  graph: PinchGraphRead;
  width?: string | number;
  height?: string | number;
  dot?: boolean;
  name?: string;
}
 
export default function PinchGraph({ graph, width = "95%", height = 200, dot = false, name = "No Title" }: PinchGraphProps) {
    const [showLegend, setShowLegend] = useState(false);
    const [minXValue, maxXValue] = getXValueRage(graph.segments);
    const xTicks = getXAxisDomain(minXValue,maxXValue);
    const segments = graph.segments || [];
    const sortedSegments = [...segments].sort((a, b) => b.colour! - a.colour!);
 
  return (
    <Card className="bg-muted/5 border-muted/80 pt-0">
      <CardHeader>
        <CardTitle className="font-semibold">
          {graph.name ? graph.name : `${name} ${graph.type}`}
        </CardTitle>
      </CardHeader>
      <ResponsiveContainer width={width} height={height}>
        <LineChart margin={{ left: 5, bottom: 15, right: 25 }}>
          <XAxis
            type="number"
            label={
              <Label
                value={"ΔH (kW)"}
                offset={-12}
                position={"insideBottom"}
                className="font-light text-sm"
              />
            }
            dataKey="x"
            scale="linear"
            ticks={xTicks}
            allowDecimals={false}
            style={{ fontSize: "0.7rem" }}
          />
          <YAxis
            type="number"
            label={
              <Label
                angle={270}
                value={"T (°C)"}
                position={"insideLeft"}
                className="font-light text-sm"
              />
            }
            dataKey="y"
            style={{ fontSize: "0.7rem" }}
          />
          <Tooltip
            content={
              <CustomTooltip
                active={undefined}
                payload={undefined}
                label={undefined}
              />
            }
          />
          {showLegend && <Legend />}
          {sortedSegments.map((segment, index) => (
            <Line
              key={index}
              // key={`${segment.title}-${index}`}
              type="linear"
              data={segment.data_points}
              dataKey="y"
              name={segment.title}
              stroke={lineColours[segment.colour!]}
              dot={dot}
              activeDot={{ r: 2 }}
              connectNulls={false}
            />
          ))}
        </LineChart>
      </ResponsiveContainer>
    </Card>
  );
}