import React, { Component } from 'react' import { ScrollView } from 'react-native' import range from 'date-range' import Svg,{ G, Polyline, Rect, Text, Circle } from 'react-native-svg' import { LocalDate } from 'js-joda' import { bleedingDaysSortedByDate, temperatureDaysSortedByDate, getOrCreateCycleDay } from './db' const right = 600 const top = 10 const bottom = 350 const columnWidth = 30 const dateRow = { height: 30, width: right } const temperatureScale = { low: 33, high: 40 } const cycleDaysToShow = 40 export default class CycleChart extends Component { constructor(props) { super(props) this.xAxisTicks = makeXAxisTicks(cycleDaysToShow) } passDateToDayView(dateString) { const cycleDay = getOrCreateCycleDay(dateString) this.props.navigation.navigate('cycleDay', { cycleDay }) } makeDayColumn(columnInfo) { return ( this.passDateToDayView(columnInfo.label)} /> {columnInfo.label.split('-')[2]} ) } makeColumnGrid(xAxisTicks) { return xAxisTicks.map(this.makeDayColumn.bind(this)) } placeBleedingSymbolsOnColumns() { return bleedingDaysSortedByDate.map(day => { // TODO handle no bleeding days, same for curve // TODO exclude future bleeding days (??) const match = this.xAxisTicks.find(tick => { return tick.label === day.date }) const x = match.rightOffset + columnWidth / 2 return () }) } determineCurvePoints() { return temperatureDaysSortedByDate.map(cycleDay => { const match = this.xAxisTicks.find(tick => tick.label === cycleDay.date) const x = match.rightOffset + columnWidth / 2 const y = normalizeToScale(cycleDay.temperature.value) return [x, y].join() }).join(' ') } componentDidMount() { this.scrollContainer.scrollToEnd() } render() { return ( { if (scroll) this.scrollContainer = scroll }} horizontal={true}> this.scrollContainer.scrollToEnd()} > {this.makeColumnGrid(this.xAxisTicks)} {this.placeBleedingSymbolsOnColumns()} ) } } function makeXAxisTicks(n) { const xAxisDates = getPreviousDays(n).map(jsDate => { return LocalDate.of( jsDate.getFullYear(), jsDate.getMonth() + 1, jsDate.getDate() ).toString() }) return xAxisDates.map((datestring, columnIndex) => { const rightOffset = right - (columnWidth * (columnIndex + 1)) return { label: datestring, rightOffset } }) } function getPreviousDays(n) { const today = new Date() today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0) const twoWeeksAgo = new Date(today - (range.DAY * n)) return range(twoWeeksAgo, today).reverse() } function normalizeToScale(temp) { const valueRelativeToScale = (temperatureScale.high - temp) / (temperatureScale.high - temperatureScale.low) const scaleHeight = bottom - top return scaleHeight * valueRelativeToScale }