Skip to content

Commit

Permalink
feat(#53): created line component
Browse files Browse the repository at this point in the history
  • Loading branch information
sametcodes committed Mar 27, 2023
1 parent e1293ad commit da88685
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/@dsvgui/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./metrics";
export * from "./article";
export * from "./line";
114 changes: 114 additions & 0 deletions lib/@dsvgui/components/line/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Document } from "@/lib/@dsvgui";
import { getTextWidth } from "../../utils/index";

type ILine = {
title: string;
subtitle: string;
total: string;
points: number[];
};

export const Line: React.FC<ILine> = ({ title, subtitle, total, points }) => {
function createDynamicSvgPath({
height,
width,
ratio,
}: {
height: number;
width: number;
ratio: number;
}): string {
const max_value = Math.max(...points);
const x_gap = (width - 20) / points.length;
let x = 10;
let path =
"M " + x + " " + (height - 5 - (points[0] / max_value) * height * ratio);
for (let i = 0; i <= points.length; i++) {
x += x_gap;
path +=
" L " +
x +
" " +
(height - 5 - (points[i] / max_value) * height * ratio);
}

return path;
}

const total_text_width =
getTextWidth(subtitle, { fontSize: 16 }) +
getTextWidth(total, { fontSize: 24 });
const height = 110;
const width = total_text_width + 100;
const path_value = createDynamicSvgPath({ height, width, ratio: 0.5 });

return (
<Document w={width} h={height} padding={10}>
<path
d={path_value}
stroke="url(#paint0_linear_135_79)"
stroke-width="3"
stroke-linecap="round"
stroke-linejoin="round"
/>
<text
fill="#000000"
xmlSpace="preserve"
fontFamily="Roboto"
fontSize="16"
fontWeight="bolder"
letterSpacing="0.5px"
>
<tspan x="26" y="32.4688">
{title}
</tspan>
</text>
<text
fill="#555555"
xmlSpace="preserve"
fontFamily="Roboto"
fontSize="14"
fontWeight={500}
letterSpacing="0.5px"
>
<tspan x="26" y="50.4688">
{subtitle}
</tspan>
</text>
<text
fill="black"
xmlSpace="preserve"
fontFamily="Roboto"
fontSize="24"
fontWeight="bold"
letterSpacing="0.5px"
>
<tspan
x={width - 26 - getTextWidth(total, { fontSize: 24 })}
y="42.7031"
>
{total}
</tspan>
</text>
<defs>
<linearGradient
id="paint0_linear_135_79"
x1="317"
y1="38.6358"
x2="29.4084"
y2="155.088"
gradientUnits="userSpaceOnUse"
>
<stop offset="0" stop-color="#ffffff" />
<stop offset="0.141455" stop-color="#FF12E7" />
<stop offset="0.201455" stop-color="#FF5ecc" />
<stop offset="0.363128" stop-color="#FF9eeE" />
<stop offset="0.581288" stop-color="#FF0AAC" />
<stop offset="0.729526" stop-color="#FF046E" />
<stop offset="0.943128" stop-color="#FF9eeE" />
<stop offset="1" stop-color="#ffffff" />
</linearGradient>
</defs>
</Document>
);
};

0 comments on commit da88685

Please sign in to comment.