11"use client" ;
22
3+ import { BarChart3 , TrendingDown , TrendingUp } from "lucide-react" ;
34import { useState } from "react" ;
4- import { Area , AreaChart , CartesianGrid , XAxis , YAxis , ResponsiveContainer } from "recharts" ;
5- import { Card , CardContent , CardDescription , CardHeader , CardTitle } from "./ui/card" ;
6- import { Button } from "./ui/button" ;
5+ import { Area , AreaChart , CartesianGrid , XAxis , YAxis } from "recharts" ;
6+ import { Portfolio } from "./portfolio-selector" ;
77import { Badge } from "./ui/badge" ;
8- import {
9- Select ,
10- SelectContent ,
11- SelectItem ,
12- SelectTrigger ,
13- SelectValue ,
14- } from "./ui/select" ;
8+ import { Card , CardContent , CardDescription , CardHeader , CardTitle } from "./ui/card" ;
159import {
1610 ChartConfig ,
1711 ChartContainer ,
18- ChartTooltip ,
19- ChartTooltipContent ,
2012 ChartLegend ,
2113 ChartLegendContent ,
14+ ChartTooltip ,
15+ ChartTooltipContent ,
2216} from "./ui/chart" ;
2317import { Checkbox } from "./ui/checkbox" ;
24- import { TrendingUp , TrendingDown , BarChart3 } from "lucide-react" ;
25- import { Portfolio } from "./portfolio-selector" ;
18+ import {
19+ Select ,
20+ SelectContent ,
21+ SelectItem ,
22+ SelectTrigger ,
23+ SelectValue ,
24+ } from "./ui/select" ;
2625
2726interface ComparisonData {
2827 date : string ;
@@ -49,6 +48,37 @@ const chartColors = [
4948 "hsl(var(--chart-5))" ,
5049] ;
5150
51+ // Function to get color based on portfolio value (red for lower, blue for higher)
52+ const getColorByValue = ( portfolios : Portfolio [ ] , selectedPortfolios : string [ ] ) => {
53+ const selectedPortfolioData = selectedPortfolios
54+ . map ( id => portfolios . find ( p => p . id === id ) )
55+ . filter ( Boolean ) as Portfolio [ ] ;
56+
57+ if ( selectedPortfolioData . length === 0 ) return { } ;
58+
59+ // Sort portfolios by total value
60+ const sortedPortfolios = [ ...selectedPortfolioData ] . sort ( ( a , b ) => a . totalValue - b . totalValue ) ;
61+
62+ const colorMap : { [ key : string ] : string } = { } ;
63+
64+ selectedPortfolioData . forEach ( portfolio => {
65+ const index = sortedPortfolios . findIndex ( p => p . id === portfolio . id ) ;
66+ const ratio = index / ( sortedPortfolios . length - 1 ) ;
67+
68+ // Interpolate between red (lower values) and blue (higher values)
69+ if ( sortedPortfolios . length === 1 ) {
70+ colorMap [ portfolio . id ] = "#3b82f6" ; // Default blue for single portfolio
71+ } else {
72+ // Red to Blue gradient: red (0, 0) -> blue (0, 0, 255)
73+ const red = Math . round ( 220 * ( 1 - ratio ) ) ; // Start with red, fade to 0
74+ const blue = Math . round ( 59 + 196 * ratio ) ; // Start with some blue, increase to 255
75+ colorMap [ portfolio . id ] = `rgb(${ red } , 0, ${ blue } )` ;
76+ }
77+ } ) ;
78+
79+ return colorMap ;
80+ } ;
81+
5282export function PortfolioComparison ( { portfolios, className } : PortfolioComparisonProps ) {
5383 const [ selectedPortfolios , setSelectedPortfolios ] = useState < string [ ] > (
5484 portfolios . slice ( 0 , 2 ) . map ( p => p . id )
@@ -94,12 +124,15 @@ export function PortfolioComparison({ portfolios, className }: PortfolioComparis
94124 }
95125 } ;
96126
97- const chartConfig : ChartConfig = selectedPortfolios . reduce ( ( config , portfolioId , index ) => {
127+ // Get colors based on portfolio values
128+ const valueBasedColors = getColorByValue ( portfolios , selectedPortfolios ) ;
129+
130+ const chartConfig : ChartConfig = selectedPortfolios . reduce ( ( config , portfolioId ) => {
98131 const portfolio = portfolios . find ( p => p . id === portfolioId ) ;
99132 if ( portfolio ) {
100133 config [ portfolio . name ] = {
101134 label : portfolio . name ,
102- color : chartColors [ index % chartColors . length ] ,
135+ color : valueBasedColors [ portfolioId ] || "#3b82f6" , // Default blue if no color found
103136 } ;
104137 }
105138 return config ;
@@ -186,9 +219,10 @@ export function PortfolioComparison({ portfolios, className }: PortfolioComparis
186219 < ChartContainer config = { chartConfig } className = "h-[400px]" >
187220 < AreaChart data = { comparisonData } >
188221 < defs >
189- { selectedPortfolios . map ( ( portfolioId , index ) => {
222+ { selectedPortfolios . map ( ( portfolioId ) => {
190223 const portfolio = portfolios . find ( p => p . id === portfolioId ) ;
191224 if ( ! portfolio ) return null ;
225+ const portfolioColor = valueBasedColors [ portfolioId ] || "#3b82f6" ;
192226 return (
193227 < linearGradient
194228 key = { portfolio . name }
@@ -200,12 +234,12 @@ export function PortfolioComparison({ portfolios, className }: PortfolioComparis
200234 >
201235 < stop
202236 offset = "5%"
203- stopColor = { chartColors [ index % chartColors . length ] }
237+ stopColor = { portfolioColor }
204238 stopOpacity = { 0.3 }
205239 />
206240 < stop
207241 offset = "95%"
208- stopColor = { chartColors [ index % chartColors . length ] }
242+ stopColor = { portfolioColor }
209243 stopOpacity = { 0.1 }
210244 />
211245 </ linearGradient >
@@ -240,15 +274,16 @@ export function PortfolioComparison({ portfolios, className }: PortfolioComparis
240274 />
241275 }
242276 />
243- { selectedPortfolios . map ( ( portfolioId , index ) => {
277+ { selectedPortfolios . map ( ( portfolioId ) => {
244278 const portfolio = portfolios . find ( p => p . id === portfolioId ) ;
245279 if ( ! portfolio ) return null ;
280+ const portfolioColor = valueBasedColors [ portfolioId ] || "#3b82f6" ;
246281 return (
247282 < Area
248283 key = { portfolio . name }
249284 type = "monotone"
250285 dataKey = { portfolio . name }
251- stroke = { chartColors [ index % chartColors . length ] }
286+ stroke = { portfolioColor }
252287 fill = { `url(#fill${ portfolio . name . replace ( / \s + / g, '' ) } )` }
253288 strokeWidth = { 2 }
254289 />
0 commit comments