Skip to content

Commit

Permalink
fix: charts components
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasrosa committed Nov 25, 2023
1 parent 95494ed commit 6e92ee5
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/components/common/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Props {
export function Card({ title, subtitle, children, ...props }: Props) {
return (
<CardTremor {...props}>
<Title className="justify-between">
<Title className="justify-between mb-2">
<span data-testid="title">{title}</span>
<span data-testid="subtitle" className="text-gray-500 text-sm">
{subtitle}
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function Title({ children, ...props }: TitleProps) {
return (
<TitleTremor
className={cn(
'border-b text-gray-600 border-b-gray-400 flex flex-col pb-2',
'border-b text-gray-600 border-b-gray-400 flex flex-col',
'sm:flex-row sm:justify-between',
props.className,
)}
Expand Down
7 changes: 5 additions & 2 deletions src/components/pages/home/history-chart/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toCurrency } from '@/helpers/currency'
import { LineChart } from '@tremor/react'
import { AreaChart } from '@tremor/react'

interface Props {
data: any[]
Expand All @@ -12,12 +12,15 @@ export function Chart({ data = [], categories = [] }: Props) {
}

return (
<LineChart
<AreaChart
data={data}
index="date"
categories={categories}
valueFormatter={toCurrency}
yAxisWidth={85}
startEndOnly={true}
autoMinValue={true}
curveType="step"
colors={[
'red',
'blue',
Expand Down
27 changes: 24 additions & 3 deletions src/components/pages/home/history-chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ const convertData = (
return mergeArrayOfObjects(rowData)
})

return data.reverse()
return data.reverse().filter((row) => {
return Object.values(row).every((value) => {
if (String(value).includes('/')) {
return true
}

return value !== 0
})
})
}

const getCategories = (tickers: TickerData[]) => {
Expand All @@ -61,20 +69,33 @@ export function HistoryCard({ report, title, subtitle, metric }: Props) {
if (isLoading || !tickersData) {
return (
<Card title={title} subtitle={subtitle}>
Carregando...
<p className="text-gray-500 text-sm text-center mt-6">Carregando...</p>
</Card>
)
}

if (error) {
return (
<Card title={title} subtitle={subtitle}>
Erro...
<p className="text-gray-500 text-sm text-center mt-6">
Ocorreu um erro ao carregar os dados!
</p>
</Card>
)
}

if (tickersData.length === 0) {
return (
<Card title={title} subtitle={subtitle}>
<p className="text-gray-500 text-sm text-center mt-6">
Adicione os ativos na cateira para visualizar
</p>
</Card>
)
}

const data = convertData(dateList, tickersData, report, metric)

const categories = getCategories(tickersData)

return (
Expand Down
47 changes: 29 additions & 18 deletions src/components/pages/home/tickers-table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,34 @@ export function Table() {
const { tickers } = usePortfolios()

return (
<TremorTable>
<TableHead>
<TableRow data-testid="header-portfolio">
<HeaderCell title="" />
<HeaderCell title="Papel" helper={<HelperTicker />} />
<HeaderCell title="Valor Atual" helper={<HelperPrice />} />
<HeaderCell title="PV/P" helper={<HelperPVP />} />
<HeaderCell title="DY 12M" helper={<HelperDY />} />
<HeaderCell title="Últ. Rend." helper={<HelperLastDividend />} />
<HeaderCell title="Últ. Rend. 12M" helper={<HelperSumDividends />} />
</TableRow>
</TableHead>
<TableBody>
{tickers.map((t) => (
<Row key={t.ticker} ticker={t} />
))}
</TableBody>
</TremorTable>
<>
<TremorTable>
<TableHead>
<TableRow data-testid="header-portfolio">
<HeaderCell title="" />
<HeaderCell title="Papel" helper={<HelperTicker />} />
<HeaderCell title="Valor Atual" helper={<HelperPrice />} />
<HeaderCell title="PV/P" helper={<HelperPVP />} />
<HeaderCell title="DY 12M" helper={<HelperDY />} />
<HeaderCell title="Últ. Rend." helper={<HelperLastDividend />} />
<HeaderCell
title="Últ. Rend. 12M"
helper={<HelperSumDividends />}
/>
</TableRow>
</TableHead>
<TableBody>
{tickers.map((t) => (
<Row key={t.ticker} ticker={t} />
))}
</TableBody>
</TremorTable>

{tickers.length === 0 && (
<p className="text-gray-500 text-sm text-center mt-6">
Adicione os ativos na cateira para visualizar
</p>
)}
</>
)
}
7 changes: 5 additions & 2 deletions src/components/pages/simulator/chart/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toCurrency } from '@/helpers/currency'
import { SimulatorResult } from '@/helpers/simulatorCalculate'
import { LineChart } from '@tremor/react'
import { AreaChart } from '@tremor/react'

interface Props {
results: SimulatorResult[]
Expand All @@ -18,12 +18,15 @@ export function Chart({ results = [] }: Props) {
}))

return (
<LineChart
<AreaChart
data={data}
index="Mês"
categories={['Total Investido', 'Total Acumulado']}
valueFormatter={toCurrency}
yAxisWidth={105}
startEndOnly={true}
autoMinValue={true}
curveType="natural"
colors={['blue', 'green']}
/>
)
Expand Down
22 changes: 6 additions & 16 deletions src/services/TickerService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ describe('TickerService', () => {
expect(result).toBeDefined()
expect(result.ticker).toBe(ticker)
expect(result.pvp).toBe(1.05)
expect(result.dy12).toBe(8.21)
expect(result.dy12).toBe(8.5)
expect(result.price).toBe(107)
expect(result.dividend12).toBe(8.79)
expect(result.dividend12).toBe(9.1)
expect(result.lastDividend).toBe(0.74)
expect(result.dividendsHistory.length).toBe(12)
expect(result.pricesHistory.length).toBeTruthy()
Expand Down Expand Up @@ -45,25 +45,15 @@ describe('TickerService', () => {
expect(result.dividendsHistory.length).toBe(12)

expect(result.dividendsHistory[0]).toEqual({
kind: 'cash',
currency: 'brl',
isin_code: 'BRXPLGCTF002',
label: 'Rendimento',
amount: 0.78,
approved_in: '2023-10-31',
traded_until: '2023-10-31',
payment_date: '2023-11-16',
date: 'Nov/23',
timestamp: 1700103600000,
})

expect(result.dividendsHistory[11]).toEqual({
kind: 'cash',
currency: 'brl',
isin_code: 'BRXPLGCTF002',
label: 'Rendimento',
amount: 0.74,
approved_in: '2023-03-31',
traded_until: '2023-03-31',
payment_date: '2023-04-17',
date: 'Dec/22',
timestamp: 1670986800000,
})
})

Expand Down
5 changes: 2 additions & 3 deletions src/services/TickerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class TickerService {
throw new Error(dividends.message)
}

dividends
return dividends
.filter((result: any) => {
const tickerCode = `BR${symbol.substring(0, 4)}CTF`
return (
Expand All @@ -121,8 +121,7 @@ export class TickerService {

return { timestamp, amount, date }
})

return dividends.splice(0, 12)
.splice(0, 12)
}

private async fetchPricesHistory(symbol: string) {
Expand Down
8 changes: 4 additions & 4 deletions src/storage/tickersInitialState.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Ticker } from '@/@types/TickersTypes'
// import { config } from '@/config'
import { config } from '@/config'

// const portfolioId = '5a31ee47-6b2f-4586-8fb9-983010149ec7'
const portfolioId = '5a31ee47-6b2f-4586-8fb9-983010149ec7'

export const tickersInitialState: Ticker[] = [
// { ...config.defaults.ticker, portfolioId, ticker: 'XPLG11' },
// { ...config.defaults.ticker, portfolioId, ticker: 'XPML11' },
{ ...config.defaults.ticker, portfolioId, ticker: 'XPLG11' },
{ ...config.defaults.ticker, portfolioId, ticker: 'XPML11' },
// { ...config.defaults.ticker, portfolioId, ticker: 'SARE11' },
]

0 comments on commit 6e92ee5

Please sign in to comment.