Open arlyxiao opened 1 year ago
import { Group } from '@visx/group';
import { scalePoint } from '@visx/scale';
import { AxisLeft, AxisBottom } from '@visx/axis';
import { GridRows, GridColumns } from '@visx/grid';
import styles from './styles.module.css';
const background = '#2a2829';
const strokeColor = '#e0e0e0';
const dashlineColor = '#b8c5ea';
const minX = '130';
const maxX = '330';
const minmaxY = '-10';
const maDomains = ['0mA', '1mA', '2mA', '3mA', '4mA', '5mA', '6mA', '7mA'];
const levelDomains = ['L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8'];
const defaultMargin = { top: 40, right: 30, bottom: 50, left: 40 };
function getScaleHeightValue(
heightMax: number,
domainMax: number,
value: number,
) {
const result = heightMax - (heightMax / domainMax) * value;
console.log(`scale value: ${result}`);
return result;
}
function getScaleWidthValue(
heightMax: number,
domainMax: number,
value: number,
) {
const result = (heightMax / domainMax) * value;
console.log(`scale value: ${result}`);
return result;
}
export type ChartProps = {
width: number;
height: number;
margin?: { top: number; right: number; bottom: number; left: number };
};
export function AxisChart({
width,
height,
margin = defaultMargin,
}: ChartProps) {
if (width < 10) return null;
const widthMax = width - margin.left - margin.right;
const heightMax = height - margin.top - margin.bottom;
const maScale = scalePoint<string>({
domain: maDomains,
range: [0, widthMax],
});
const levelScale = scalePoint<string>({
domain: levelDomains,
range: [heightMax, 0],
});
console.log(`widthMax: ${widthMax}`);
console.log(`heightMax: ${heightMax}`);
return (
<svg width={width} height={height}>
<rect
x={0}
y={0}
width={width}
height={height}
fill={background}
rx={14}
/>
<Group left={margin.left} top={margin.top}>
<GridRows
scale={levelScale}
width={widthMax}
height={heightMax}
stroke={strokeColor}
/>
<GridColumns
scale={maScale}
width={widthMax}
height={heightMax}
stroke={strokeColor}
/>
<line
x1={widthMax}
x2={widthMax}
y1={0}
y2={heightMax}
stroke={strokeColor}
/>
<line
x1={minX}
x2={minX}
y1={0}
y2={heightMax}
stroke={dashlineColor}
strokeDasharray="4 2"
/>
<line
x1={maxX}
x2={maxX}
y1={0}
y2={heightMax}
stroke={dashlineColor}
strokeDasharray="4 2"
/>
<AxisBottom
top={heightMax}
scale={maScale}
hideTicks={true}
stroke={strokeColor}
tickLabelProps={{
fill: strokeColor,
fontSize: 11,
textAnchor: 'middle',
}}
/>
<AxisLeft
scale={levelScale}
hideTicks={true}
stroke={strokeColor}
tickLabelProps={{
dx: '-15px',
fill: strokeColor,
fontSize: 11,
textAnchor: 'middle',
}}
/>
<circle
cx={getScaleWidthValue(widthMax, 7, 2)}
cy={getScaleHeightValue(heightMax, 7, 3.2)}
r="8"
stroke={strokeColor}
strokeWidth="2"
fill="none"
/>
<text
x={minX}
y={minmaxY}
className={styles.minmax}
stroke={strokeColor}
>
Min
</text>
<text
x={maxX}
y={minmaxY}
className={styles.minmax}
stroke={strokeColor}
>
Max
</text>
</Group>
</svg>
);
}
x/y axis