From b99fa45c8c2f0d57087e7dc9fd0698e7b642ab45 Mon Sep 17 00:00:00 2001 From: jaieds <87969327+jaieds@users.noreply.github.com> Date: Thu, 10 Apr 2025 19:33:16 +0600 Subject: [PATCH 1/4] imprv (LineChart): Prop to customize y-axis colors - Added an option to change y-axis colors. --- .../line-chart/line-chart.stories.tsx | 10 ++++++ src/components/line-chart/line-chart.tsx | 35 +++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/components/line-chart/line-chart.stories.tsx b/src/components/line-chart/line-chart.stories.tsx index c270c89e..69edcc79 100644 --- a/src/components/line-chart/line-chart.stories.tsx +++ b/src/components/line-chart/line-chart.stories.tsx @@ -63,6 +63,9 @@ export const LineChartSimple: Story = { xAxisDataKey: 'month', xAxisFontSize: 'sm', withDots: false, + lineChartWrapperProps: { + margin: { top: 5, right: 15, bottom: 5, left: 15 }, + }, }, }; @@ -79,6 +82,9 @@ export const LineChartWithDots: Story = { xAxisDataKey: 'month', xAxisFontSize: 'sm', withDots: true, + lineChartWrapperProps: { + margin: { top: 5, right: 15, bottom: 5, left: 15 }, + }, }, }; @@ -95,6 +101,9 @@ export const LineChartMultiple: Story = { xAxisDataKey: 'month', xAxisFontSize: 'sm', withDots: false, + lineChartWrapperProps: { + margin: { top: 5, right: 15, bottom: 5, left: 15 }, + }, }, }; @@ -117,5 +126,6 @@ export const BiaxialLineChart: Story = { lineChartWrapperProps: { margin: { top: 5, right: 5, bottom: 5, left: 5 }, }, + yAxisFontColor: [ '#3b82f6', '#10B981' ], }, }; diff --git a/src/components/line-chart/line-chart.tsx b/src/components/line-chart/line-chart.tsx index ba160144..36b944ec 100644 --- a/src/components/line-chart/line-chart.tsx +++ b/src/components/line-chart/line-chart.tsx @@ -11,6 +11,11 @@ import ChartTooltipContent from './chart-tooltip-content'; import Label from '../label'; import type { CategoricalChartProps } from 'recharts/types/chart/generateCategoricalChart'; +// Default color constants +const DEFAULT_FONT_COLOR = '#6B7280'; +const DEFAULT_GRID_COLOR = '#E5E7EB'; +const DEFAULT_LINE_COLORS = [ { stroke: '#2563EB' }, { stroke: '#38BDF8' } ]; + interface DataItem { [key: string]: number | string; } @@ -61,8 +66,12 @@ interface LineChartProps { /** Font color for the labels on the x-axis. */ xAxisFontColor?: string; - /** Font color for the labels on the y-axis. */ - yAxisFontColor?: string; + /** + * Font color for the labels on the y-axis. + * When biaxial is true, you can provide an array of two colors [leftAxisColor, rightAxisColor]. + * If a single color is provided, it will be used for both axes. + */ + yAxisFontColor?: string | string[]; /** Width of the chart container. */ chartWidth?: number | string; @@ -114,19 +123,17 @@ const LineChart = ( { xAxisDataKey, yAxisDataKey, xAxisFontSize = 'sm', // sm, md, lg - xAxisFontColor = '#6B7280', - yAxisFontColor = '#6B7280', + xAxisFontColor = DEFAULT_FONT_COLOR, + yAxisFontColor = DEFAULT_FONT_COLOR, chartWidth = 350, chartHeight = 200, withDots = false, lineChartWrapperProps, strokeDasharray = '3 3', - gridColor = '#E5E7EB', + gridColor = DEFAULT_GRID_COLOR, biaxial = false, }: LineChartProps ) => { - const defaultColors = [ { stroke: '#2563EB' }, { stroke: '#38BDF8' } ]; - - const appliedColors = colors.length > 0 ? colors : defaultColors; + const appliedColors = colors.length > 0 ? colors : DEFAULT_LINE_COLORS; const fontSizeMap = { sm: '12px', @@ -136,6 +143,14 @@ const LineChart = ( { const fontSizeVariant = fontSizeMap[ xAxisFontSize ] || fontSizeMap.sm; + // Handle Y-axis colors for biaxial chart + const getYAxisFontColor = ( index = 0 ) => { + if ( Array.isArray( yAxisFontColor ) ) { + return yAxisFontColor[ index ] || yAxisFontColor[ 0 ] || DEFAULT_FONT_COLOR; + } + return yAxisFontColor; + }; + if ( ! data || data.length === 0 ) { return (