Skip to content

Commit

Permalink
Merge pull request #29 from Vizzuality/habitat-percentage-scale
Browse files Browse the repository at this point in the history
Fix habitat widget bar fill to be relative to total area + changing scale to 100%
  • Loading branch information
SARodrigues authored Oct 31, 2023
2 parents f6199da + bee33c6 commit 2ffac9b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions frontend/src/components/charts/horizontal-bar-chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { format } from 'd3-format';
import { cn } from '@/lib/classnames';

const DEFAULT_BAR_COLOR = '#1E1E1E';
const DEFAULT_MAX_PERCENTAGE = 55;
const DEFAULT_MAX_PERCENTAGE = 100;
const PROTECTION_TARGET = 30;

type HorizontalBarChartProps = {
Expand All @@ -30,7 +30,11 @@ const HorizontalBarChart: React.FC<HorizontalBarChartProps> = ({ className, data
}, [totalArea, protectedArea]);

const barFillPercentage = useMemo(() => {
return Math.round((protectedArea * DEFAULT_MAX_PERCENTAGE) / totalArea);
const totalPercentage = (protectedArea * 100) / totalArea;
const relativeToMaxPercentage = (totalPercentage * 100) / DEFAULT_MAX_PERCENTAGE;

// Prevent overflowing if the bar fill exceeds the set max percentage
return relativeToMaxPercentage > DEFAULT_MAX_PERCENTAGE ? 100 : relativeToMaxPercentage;
}, [protectedArea, totalArea]);

const formattedArea = useMemo(() => {
Expand Down

0 comments on commit 2ffac9b

Please sign in to comment.