-
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.
- Loading branch information
Showing
6 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/components/diapers/widgets/dailychart/ChartDaily.js
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,116 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
// libs | ||
import { GoogleCharts } from 'google-charts'; | ||
|
||
// Styles | ||
import styles from "./chartDaily.scss"; | ||
import { useEffect } from 'react'; | ||
|
||
const ChartDaily = props => { | ||
let { diapers, maxDays = 7 } = props; | ||
let timeout = null; | ||
let data = []; | ||
|
||
const prepareData = () => { | ||
let date = ''; | ||
let numPee = 0; | ||
let numPoo = 0; | ||
let numUndef = 0; | ||
|
||
diapers.forEach(diaper => { | ||
if (data.length < maxDays) { | ||
let tempDate = new Date(diaper.time).toLocaleDateString(); | ||
// check if we have reached the next day | ||
if (tempDate !== date) { | ||
// add last day to data array if not null | ||
if (date !== '') { | ||
data.push([date, numUndef, numPee, numPoo]); | ||
} | ||
date = tempDate; | ||
numPee = 0; | ||
numPoo = 0; | ||
numUndef = 0; | ||
} | ||
// increment Pee and Poo | ||
numPee += diaper.pee ? 1 : 0; | ||
numPoo += diaper.poo ? 1 : 0; | ||
numUndef += !diaper.poo && !diaper.pee ? 1 : 0; | ||
} | ||
}); | ||
if (data.length < maxDays) { | ||
data.push([date, numUndef, numPee, numPoo]); | ||
} | ||
} | ||
|
||
function prepareChart() { | ||
var dataTable = new GoogleCharts.api.visualization.DataTable(); | ||
dataTable.addColumn("string", "Date"); | ||
dataTable.addColumn("number", "Not set"); | ||
dataTable.addColumn("number", "Pee"); | ||
dataTable.addColumn("number", "Poo"); | ||
|
||
data.reverse(); | ||
dataTable.addRows(data); | ||
|
||
var options = { | ||
title: "Diapers per Day", | ||
isStacked: true, | ||
colors: ['grey', styles.yellow, styles.brown], | ||
}; | ||
|
||
var chart = new GoogleCharts.api.visualization.ColumnChart( | ||
document.getElementById("diaperschart") | ||
); | ||
|
||
chart.draw(dataTable, options); | ||
} | ||
|
||
const drawChart = () => { | ||
if (diapers && diapers.length > 0) { | ||
data = []; | ||
prepareData(); | ||
GoogleCharts.load(prepareChart, { packages: ["corechart", "bar"] }); | ||
} | ||
} | ||
|
||
if (diapers && diapers.length > 0) { | ||
prepareData(); | ||
GoogleCharts.load(prepareChart, { packages: ["corechart", "bar"] }); | ||
} | ||
|
||
const _handleWindowResize = () => { | ||
window.clearTimeout(timeout); | ||
timeout = window.setTimeout(() => { | ||
drawChart(); | ||
}, 1000); | ||
} | ||
|
||
const ChartContainer = () => { | ||
return ( | ||
<div id={"diaperschart"}></div> | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener("resize", _handleWindowResize); | ||
|
||
return (() => { | ||
window.removeEventListener("resize", _handleWindowResize); | ||
}) | ||
}, [diapers]); | ||
|
||
return ( | ||
<article className={props.className}> | ||
<span className="card_icon fas fa-chart-line fa-3x"></span> | ||
<ChartContainer><p>Still gathering Data...</p></ChartContainer> | ||
</article> | ||
); | ||
} | ||
|
||
ChartDaily.propTypes = { | ||
|
||
} | ||
|
||
export default ChartDaily |
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,7 @@ | ||
@import '../../../../assets/scss/colors'; | ||
|
||
:export { | ||
timelineSingleColor: $highlight; | ||
yellow: $yellow; | ||
brown: $brown; | ||
} |
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