diff --git a/components/chart/chart.js b/components/chart/chart.js index dcc5be84225d321b88d278e47e1296cbf2dc8300..c08e0dcfc867b942559eed90cb2d340b575b3f23 100644 --- a/components/chart/chart.js +++ b/components/chart/chart.js @@ -4,9 +4,8 @@ import range from 'date-range' import { LocalDate } from 'js-joda' import { yAxis, normalizeToScale } from './y-axis' import DayColumn from './day-column' -import { getCycleDay, cycleDaysSortedByDate } from '../../db' +import { getCycleDay, cycleDaysSortedByDate, getAmountOfCycleDays } from '../../db' import styles from './styles' -import config from './config' const yAxisView = <View {...styles.yAxis}>{yAxis.labels}</View> @@ -14,7 +13,7 @@ export default class CycleChart extends Component { constructor(props) { super(props) this.state = { - columns: makeColumnInfo(config.xAxisRangeInDays) + columns: makeColumnInfo(), } this.renderColumn = ({item, index}) => { return ( @@ -29,7 +28,7 @@ export default class CycleChart extends Component { this.reCalculateChartInfo = (function(Chart) { return function() { - Chart.setState({columns: makeColumnInfo(config.xAxisRangeInDays)}) + Chart.setState({columns: makeColumnInfo()}) } })(this) @@ -60,8 +59,16 @@ export default class CycleChart extends Component { } } -function makeColumnInfo(n) { - const xAxisDates = getPreviousDays(n).map(jsDate => { +function makeColumnInfo() { + let amountOfCycleDays = getAmountOfCycleDays() + // if there's not much data yet, we want to show at least 30 days on the chart + if (amountOfCycleDays < 30) { + amountOfCycleDays = 30 + } else { + // we don't want the chart to end abruptly before the first data day + amountOfCycleDays += 5 + } + const xAxisDates = getTodayAndPreviousDays(amountOfCycleDays).map(jsDate => { return LocalDate.of( jsDate.getFullYear(), jsDate.getMonth() + 1, @@ -85,7 +92,7 @@ function makeColumnInfo(n) { }) } -function getPreviousDays(n) { +function getTodayAndPreviousDays(n) { const today = new Date() today.setHours(0) today.setMinutes(0) diff --git a/components/chart/config.js b/components/chart/config.js index 39d8a51bc178c6f21a48b85e2262abfd4a4714ff..a83eafc697bdbed7218602aac679e219754718e0 100644 --- a/components/chart/config.js +++ b/components/chart/config.js @@ -4,8 +4,7 @@ const config = { temperatureScale: { low: 35, high: 38 - }, - xAxisRangeInDays: 1000 + } } const margin = 3 diff --git a/components/chart/day-column.js b/components/chart/day-column.js index 419042dea806bfacb55b114e7e545e8b3807268f..f356e7c91f065c0951400784f179dcf6f74609a2 100644 --- a/components/chart/day-column.js +++ b/components/chart/day-column.js @@ -57,7 +57,6 @@ export default class DayColumn extends Component { } if (typeof mucus === 'number') { - console.log('ever') const mucusIcon = ( <View position='absolute' diff --git a/db/index.js b/db/index.js index d3d49e87931d5290c9a88c9d37fdde71e2a891d3..55b34b4710fdc7a80cf73ef7b8d22fb9e6ee5ecf 100644 --- a/db/index.js +++ b/db/index.js @@ -1,5 +1,5 @@ import Realm from 'realm' -import { LocalDate } from 'js-joda' +import { LocalDate, ChronoUnit } from 'js-joda' import { cycleWithTempAndNoMucusShift, cycleWithFhm, @@ -193,6 +193,15 @@ function getColumnNamesForCsv() { } } +function getAmountOfCycleDays() { + const amountOfCycleDays = cycleDaysSortedByDate.length + if (!amountOfCycleDays) return 0 + const earliest = cycleDaysSortedByDate[amountOfCycleDays - 1] + const today = LocalDate.now() + const earliestAsLocalDate = LocalDate.parse(earliest.date) + return earliestAsLocalDate.until(today, ChronoUnit.DAYS) +} + export { saveSymptom, getOrCreateCycleDay, @@ -203,5 +212,6 @@ export { deleteAll, getPreviousTemperature, getCycleDay, - getColumnNamesForCsv + getColumnNamesForCsv, + getAmountOfCycleDays }