generated from graasp/graasp-app-starter-ts-vite
-
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.
feat: export data to CSV and PNG (#124)
* feat: export data to csv * feat: download charts as PNG * feat: improve responsivity on small devices
- Loading branch information
Showing
9 changed files
with
221 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { useSimulation } from '@/context/SimulationContext'; | ||
import { HouseComponent } from '@/types/houseComponent'; | ||
|
||
export const useHouseCost = (): { wallCost: number } => { | ||
const { | ||
house: { getByType }, | ||
} = useSimulation(); | ||
|
||
const wallCost = useMemo( | ||
() => | ||
getByType(HouseComponent.Wall).reduce( | ||
(totCost, houseComponent) => | ||
totCost + | ||
houseComponent.actualArea * | ||
houseComponent.buildingMaterials.reduce( | ||
(componentCost, material) => | ||
componentCost + material.price * material.thickness, | ||
0, | ||
), | ||
0, | ||
), | ||
[getByType], | ||
); | ||
|
||
return { wallCost }; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { Button } from '@mui/material'; | ||
|
||
import { FileSpreadsheet } from 'lucide-react'; | ||
import Papa from 'papaparse'; | ||
|
||
import { useSimulation } from '@/context/SimulationContext'; | ||
import { useHouseCost } from '@/hooks/useHouseCost'; | ||
|
||
export const ExportCSVButton = (): JSX.Element => { | ||
const { t } = useTranslation('SIMULATION_GRAPHICS', { | ||
keyPrefix: 'EXPORT_CSV', | ||
}); | ||
|
||
const { | ||
simulation: { | ||
days: { simulationDays }, | ||
}, | ||
temperatures: { indoor, outdoor }, | ||
electricity: { pricekWh }, | ||
} = useSimulation(); | ||
|
||
const { wallCost } = useHouseCost(); | ||
|
||
const handleClick = (): void => { | ||
const data = simulationDays.map((d) => ({ | ||
'date (UTC)': d.date.toUTCString(), | ||
'indoor (°C)': indoor, | ||
'outdoor (°C)': outdoor.value, | ||
'heat loss (W)': d.heatLoss.global, | ||
'total heat loss (W)': d.totalHeatLoss, | ||
'price kWh (CHF)': pricekWh, | ||
'total electricity cost (CHF)': d.totalElectricityCost, | ||
'wall cost (CHF)': wallCost, | ||
})); | ||
|
||
const csv = Papa.unparse(data); | ||
const csvData = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); | ||
const csvURL = URL.createObjectURL(csvData); | ||
|
||
const link = document.createElement('a'); | ||
link.href = csvURL; | ||
link.download = `Insulation_Simulator_Data${new Date().getTime()}.csv`; | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
}; | ||
|
||
return ( | ||
<Button | ||
startIcon={<FileSpreadsheet />} | ||
variant="outlined" | ||
onClick={handleClick} | ||
> | ||
{t('DOWNLOAD_BTN_LABEL')} | ||
</Button> | ||
); | ||
}; |
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.