Skip to content

Commit

Permalink
Linting and final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
v3gaaa committed Oct 7, 2024
1 parent 3075ca1 commit 3788b5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
20 changes: 18 additions & 2 deletions src/Line/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ interface LineProps {
dataKey: string;
stroke: string;
strokeDasharray?: string;
type?: 'basis' | 'basisClosed' | 'basisOpen' | 'bumpX' | 'bumpY' | 'bump' | 'linear' | 'linearClosed' | 'natural' | 'monotoneX' | 'monotoneY' | 'monotone' | 'step' | 'stepBefore' | 'stepAfter';
type?:
| 'basis'
| 'basisClosed'
| 'basisOpen'
| 'bumpX'
| 'bumpY'
| 'bump'
| 'linear'
| 'linearClosed'
| 'natural'
| 'monotoneX'
| 'monotoneY'
| 'monotone'
| 'step'
| 'stepBefore'
| 'stepAfter';
chartWidth: number;
chartHeight: number;
onMouseOver?: (event: React.MouseEvent, entry: { name: string; [key: string]: any }) => void;
Expand All @@ -31,7 +46,8 @@ const Line: React.FC<LineProps> = ({
const xScale = (index: number) => (index + 0.5) * (chartWidth / data.length);
const yScale = (value: number) => chartHeight - (value / maxValue) * chartHeight;

const lineGenerator = d3Shape.line()
const lineGenerator = d3Shape
.line()
.x((d, index) => xScale(index))
.y((d) => yScale((d as any)[dataKey]))
.curve(d3Shape[`curve${type.charAt(0).toUpperCase() + type.slice(1)}`] || d3Shape.curveLinear);
Expand Down
46 changes: 14 additions & 32 deletions src/LineChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@ interface LineChartProps {
children: ReactNode;
}


// Función para encontrar el valor mínimo
const findMinValue = (data: Array<{ [key: string]: any }>): number => {
return Math.floor(
Math.min(
...data.map(d =>
Math.min(...Object.values(d).map(v => (typeof v === 'number' ? v : Infinity))),
),
),
const findMinValue = (data: Array<{ [key: string]: any }>): number =>
Math.floor(
Math.min(...data.map((d) => Math.min(...Object.values(d).map((v) => (typeof v === 'number' ? v : Infinity))))),
);
};

// Función para redondear el valor máximo
const roundMaxValue = (data: Array<{ [key: string]: any }>): { maxValue: number; minValue: number } => {
const minValue = findMinValue(data);
const maxValue = Math.max(
...data.map(d =>
Math.max(...Object.values(d).map(v => (typeof v === 'number' ? v : -Infinity))),
),
...data.map((d) => Math.max(...Object.values(d).map((v) => (typeof v === 'number' ? v : -Infinity)))),
);

const magnitude = Math.pow(10, Math.floor(Math.log10(maxValue)));
Expand All @@ -49,7 +39,6 @@ const roundMaxValue = (data: Array<{ [key: string]: any }>): { maxValue: number;
finalMaxValue = 10 * magnitude;
}

// Si el valor mínimo es negativo, ajustar para que sean simétricos
const finalMinValue = minValue < 0 ? -finalMaxValue : 0;

return {
Expand Down Expand Up @@ -88,24 +77,16 @@ const LineChart: React.FC<LineChartProps> = ({
}
}, [data, margin.left]);

const xAxis = Children.toArray(children).find(
(child) => React.isValidElement(child) && child.type === XAxis
);
const yAxis = Children.toArray(children).find(
(child) => React.isValidElement(child) && child.type === YAxis
);
const xAxis = Children.toArray(children).find((child) => React.isValidElement(child) && child.type === XAxis);
const yAxis = Children.toArray(children).find((child) => React.isValidElement(child) && child.type === YAxis);
const grid = Children.toArray(children).find(
(child) => React.isValidElement(child) && child.type === CartesianGrid
);
const tooltip = Children.toArray(children).find(
(child) => React.isValidElement(child) && child.type === Tooltip
);
const legend = Children.toArray(children).find(
(child) => React.isValidElement(child) && child.type === Legend
(child) => React.isValidElement(child) && child.type === CartesianGrid,
);
const tooltip = Children.toArray(children).find((child) => React.isValidElement(child) && child.type === Tooltip);
const legend = Children.toArray(children).find((child) => React.isValidElement(child) && child.type === Legend);

const lineComponents = Children.toArray(children).filter(
(child) => React.isValidElement(child) && child.type === Line
(child) => React.isValidElement(child) && child.type === Line,
);

const legendItems = lineComponents.map((child) => {
Expand Down Expand Up @@ -162,12 +143,13 @@ const LineChart: React.FC<LineChartProps> = ({
>
<g transform={`translate(${leftMargin}, ${(margin.top ?? 0) + height * 0.05})`}>
{grid && cloneElement(grid as React.ReactElement, { width: chartWidth, height: chartHeight })}
{xAxis && cloneElement(xAxis as React.ReactElement, { data, width: chartWidth, height: chartHeight })}
{xAxis &&
cloneElement(xAxis as React.ReactElement, { data, width: chartWidth, height: chartHeight })}
{yAxis && cloneElement(yAxis as React.ReactElement, { height: chartHeight, minValue, maxValue })}
{Children.map(children, (child) =>
React.isValidElement(child) && child.type === Line
? cloneElement(child, { data, chartWidth, chartHeight })
: child
: child,
)}
</g>
</svg>
Expand All @@ -177,4 +159,4 @@ const LineChart: React.FC<LineChartProps> = ({
);
};

export default LineChart;
export default LineChart;

0 comments on commit 3788b5b

Please sign in to comment.