-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Vizzuality/SKY30-29-fe-implement-the-mari…
…ne-conservation-coverage-widget [SKY30-29][SKY30-52] Marine Conservation Coverage widget
- Loading branch information
Showing
13 changed files
with
1,099 additions
and
494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
frontend/src/components/charts/conservation-chart/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { | ||
ComposedChart, | ||
Bar, | ||
XAxis, | ||
YAxis, | ||
CartesianGrid, | ||
ResponsiveContainer, | ||
Cell, | ||
// Tooltip, | ||
ReferenceLine, | ||
Line, | ||
} from 'recharts'; | ||
|
||
import { cn } from '@/lib/utils'; | ||
|
||
import ChartLegend from './legend'; | ||
|
||
// import ChartTooltip from './tooltip'; | ||
|
||
type ConservationChartProps = { | ||
className?: string; | ||
data: { | ||
year?: number; | ||
percentage: number; | ||
protectedArea: number; | ||
totalArea: number; | ||
active?: boolean; | ||
future?: boolean; | ||
}[]; | ||
}; | ||
|
||
const ConservationChart: React.FC<ConservationChartProps> = ({ className, data }) => { | ||
const firstYearData = data[0]; | ||
const lastYearData = data[data?.length - 1]; | ||
const activeYearData = data.find(({ active }) => active); | ||
const xAxisTicks = [firstYearData.year, activeYearData.year, lastYearData.year]; | ||
|
||
const historicalLineData = [ | ||
{ year: firstYearData.year, percentage: firstYearData.percentage }, | ||
{ year: activeYearData.year + 0.5, percentage: activeYearData.percentage }, | ||
]; | ||
|
||
const projectedPercentage = useMemo(() => { | ||
const numHistoricalYears = activeYearData.year - firstYearData.year; | ||
const numProjectedYears = lastYearData.year - activeYearData.year; | ||
|
||
const projectedPercentageChange = | ||
((activeYearData.percentage - firstYearData.percentage) / numHistoricalYears) * | ||
numProjectedYears; | ||
|
||
return activeYearData.percentage + projectedPercentageChange; | ||
}, [ | ||
activeYearData.percentage, | ||
activeYearData.year, | ||
firstYearData.percentage, | ||
firstYearData.year, | ||
lastYearData.year, | ||
]); | ||
|
||
const projectedLineData = [ | ||
{ year: activeYearData.year + 0.5, percentage: activeYearData.percentage }, | ||
{ year: lastYearData.year, percentage: projectedPercentage }, | ||
]; | ||
|
||
return ( | ||
<div className={cn(className, 'text-xs text-black')}> | ||
<ResponsiveContainer> | ||
<ComposedChart data={data}> | ||
<CartesianGrid vertical={false} strokeDasharray="3 3" /> | ||
<ReferenceLine | ||
xAxisId={1} | ||
y={30} | ||
label={{ position: 'insideBottomLeft', value: '30x30 Target', fill: '#FD8E28' }} | ||
stroke="#FD8E28" | ||
strokeDasharray="3 3" | ||
/> | ||
<ReferenceLine | ||
xAxisId={1} | ||
x={firstYearData.year - 0.4} | ||
label={{ position: 'insideTopLeft', value: 'Historical', fill: '#000' }} | ||
stroke="#000" | ||
/> | ||
<ReferenceLine | ||
xAxisId={1} | ||
x={activeYearData.year + 0.4} | ||
label={{ position: 'insideTopLeft', value: 'Future Projection', fill: '#000' }} | ||
stroke="#000" | ||
/> | ||
<XAxis | ||
xAxisId={1} | ||
type="number" | ||
dataKey="year" | ||
ticks={xAxisTicks} | ||
domain={[firstYearData.year - 0.4, lastYearData.year]} | ||
/> | ||
<XAxis | ||
xAxisId={2} | ||
type="number" | ||
dataKey="year" | ||
hide={true} | ||
domain={[firstYearData.year, lastYearData.year]} | ||
/> | ||
<YAxis | ||
domain={[0, 55]} | ||
ticks={[0, 15, 30, 45, 55]} | ||
tickFormatter={(value) => `${value}%`} | ||
/> | ||
{/* | ||
// TODO: Investigate tooltip | ||
// Tooltip does not play nice when the Line charts are used (no payload) | ||
<Tooltip content={<ChartTooltip />} /> | ||
*/} | ||
<Line | ||
xAxisId={2} | ||
type="monotone" | ||
data={historicalLineData} | ||
strokeWidth={2} | ||
dataKey="percentage" | ||
stroke="#4879FF" | ||
dot={false} | ||
/> | ||
<Line | ||
xAxisId={2} | ||
type="monotone" | ||
data={projectedLineData} | ||
strokeWidth={2} | ||
strokeDasharray="4 4" | ||
dataKey="percentage" | ||
stroke="#4879FF" | ||
dot={false} | ||
/> | ||
<Bar dataKey="percentage" xAxisId={1}> | ||
{data.map((entry, index) => ( | ||
<Cell | ||
stroke="black" | ||
fill={entry?.active ? '#4879FF' : 'transparent'} | ||
key={`cell-${index}`} | ||
/> | ||
))} | ||
</Bar> | ||
</ComposedChart> | ||
</ResponsiveContainer> | ||
<ChartLegend /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ConservationChart; |
16 changes: 16 additions & 0 deletions
16
frontend/src/components/charts/conservation-chart/legend/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const ChartLegend = () => { | ||
return ( | ||
<div className="ml-8 mt-2 flex justify-between gap-3"> | ||
<span className="inline-flex items-center gap-3"> | ||
<span className="block h-[2px] w-10 border-b-2 border-blue"></span> | ||
<span>Historical Trend</span> | ||
</span> | ||
<span className="inline-flex items-center gap-3"> | ||
<span className="block h-[2px] w-10 border-b-2 border-dashed border-blue"></span> | ||
<span>Future Projection</span> | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ChartLegend; |
35 changes: 35 additions & 0 deletions
35
frontend/src/components/charts/conservation-chart/tooltip/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useMemo } from 'react'; | ||
|
||
import { format } from 'd3-format'; | ||
|
||
const ChartTooltip = ({ active, payload }) => { | ||
const { percentage, year, protectedArea, totalArea, future } = payload[0]?.payload || {}; | ||
|
||
const formattedAreas = useMemo(() => { | ||
return { | ||
protected: format(',.2r')(protectedArea), | ||
total: format(',.2r')(totalArea), | ||
}; | ||
}, [protectedArea, totalArea]); | ||
|
||
if (!active || !payload?.length) return null; | ||
|
||
return ( | ||
<div className="flex flex-col gap-px rounded-md border border-black bg-white p-4 text-sm"> | ||
<span>Year: {year}</span> | ||
{!future && ( | ||
<> | ||
<span>Protection percentage: {percentage}%</span> | ||
<span> | ||
Protected area: {formattedAreas.protected} km<sup>2</sup> | ||
</span> | ||
<span> | ||
Total area: {formattedAreas.total} km<sup>2</sup> | ||
</span> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ChartTooltip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.