Skip to content

Commit

Permalink
Merge pull request #25 from UMassCDS/21-add-unit-information-to-legend
Browse files Browse the repository at this point in the history
Add units to legend
  • Loading branch information
pmandler-umass authored Oct 2, 2024
2 parents b874cf4 + 19afd43 commit 17dae15
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
21 changes: 18 additions & 3 deletions src/components/Legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { useEffect, useState } from 'react';
import { Grid } from '@mantine/core';
import { changeLegend, DataTypes} from '../hooks/dataUrl';
import '../styles/Legend.css';

// Interface for the Legend
interface LegendProps {
Expand All @@ -17,18 +16,28 @@ function Legend(props: LegendProps) {
const [lowLabel, setLowLabel] = useState<number>(0);
const [midLabel, setMidLabel] = useState<number>(50);
const [highLabel, setHighLabel] = useState<number>(100);

// Fetches the JSON of values from the backend
const getJSON = async (url: string) => {
const response = await fetch(url);
if (!response.ok) {
// TODO PAM how should it handle errors?
// check if response worked (no 404 errors etc...)
throw new Error(response.statusText);
}
const d = response.json(); // get JSON from the response
return d; // returns a promise, which resolves to this data value
};

function getUnits(data_type: DataTypes) {
if (data_type === DataTypes.ABUNDANCE) {
return "Birds/km^2";
} else if (data_type === DataTypes.MOVEMENT) {
return "Birds/km/week";
}
return "";
};


// Every time the dataType or speciesType is changed by the user, the legend updates
useEffect(() => {
const u = changeLegend(dataType, speciesType);
Expand All @@ -43,6 +52,9 @@ function Legend(props: LegendProps) {
setLowLabel(d[0].value);
setHighLabel(d[d.length-1].value);
setMidLabel(Math.floor((d[d.length-1].value-d[0].value)/2));
})
.catch((error) => {
alert(error);
});
}, [dataType, speciesType]);

Expand All @@ -55,7 +67,8 @@ function Legend(props: LegendProps) {
style={{
display: 'inline-block',
width: '14px',
height: '200px',
height: '180px',
margin: '10px',
background: `linear-gradient( 0deg, ${data} )`,
}}
/>
Expand All @@ -68,6 +81,8 @@ function Legend(props: LegendProps) {
<div>{lowLabel}</div>
</Grid.Col>
</Grid>
<div style={{textAlign:"center"}}>{getUnits(dataType)}</div>

</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/dataUrl.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const baseUrl = 'https://avianinfluenza.s3.us-east-2.amazonaws.com/';
export enum DataTypes {ABUNDANCE = 'abundance', MOVEMENT = 'netmovement'};// avianinfluenza.s3.us-east-2.amazonaws.com/abundance/mean/scale_abundance_mean.json
export enum DataTypes {ABUNDANCE = 'abundance', MOVEMENT = 'netmovement'};

/* Determine the url containing data to display the legend scale.
The function takes in the data type and the species type. */
Expand Down
7 changes: 0 additions & 7 deletions src/styles/Legend.css

This file was deleted.

0 comments on commit 17dae15

Please sign in to comment.