import React, { Component } from 'react'
import { FlatList } from 'react-native'
import range from 'date-range'
import Svg,{
G,
Rect,
Text,
Circle,
Line
} from 'react-native-svg'
import { LocalDate } from 'js-joda'
import { getCycleDay, getOrCreateCycleDay, cycleDaysSortedByDate } from '../db'
const right = 600
const top = 10
const bottom = 350
const columnWidth = 30
const middle = columnWidth / 2
const dateRow = {
height: 30,
width: right
}
const temperatureScale = {
low: 33,
high: 40
}
const cycleDaysToShow = 40
const dotRadius = 4
const curveColor = 'darkblue'
export default class CycleChart extends Component {
constructor(props) {
super(props)
this.state = {
columns: makeColumnInfo(cycleDaysToShow)
}
this.recalculateChartInfo = (function(Chart) {
return function() {
Chart.setState({columns: makeColumnInfo(cycleDaysToShow)})
}
})(this)
cycleDaysSortedByDate.addListener(this.recalculateChartInfo)
}
componentWillUnmount() {
cycleDaysSortedByDate.removeListener(this.recalculateChartInfo)
}
passDateToDayView(dateString) {
const cycleDay = getOrCreateCycleDay(dateString)
this.props.navigation.navigate('cycleDay', { cycleDay })
}
makeDayColumn({ label, cycleDay, y }, index) {
return (
this.passDateToDayView(label)}>
{label.split('-')[2]}
{cycleDay && cycleDay.bleeding ? : null}
{y ? this.drawDotAndLine(y, index) : null}
)
}
drawDotAndLine(currY, index) {
let lineToRight
let lineToLeft
const cols = this.state.columns
function makeLine(otherColY, x) {
const middleY = ((otherColY - currY) / 2) + currY
const rightTarget = [x, middleY]
return
}
const thereIsADotToTheRight = index > 0 && cols[index - 1].y
const thereIsADotToTheLeft = index < cols.length - 1 && cols[index + 1].y
if (thereIsADotToTheRight) {
lineToRight = makeLine(cols[index - 1].y, columnWidth)
}
if (thereIsADotToTheLeft) {
lineToLeft = makeLine(cols[index + 1].y, 0)
}
return (
{lineToRight}
{lineToLeft}
)
}
render() {
return (
{
return (
)
}}
keyExtractor={item => item.label}
>
)
}
}
function makeColumnInfo(n) {
const xAxisDates = getPreviousDays(n).map(jsDate => {
return LocalDate.of(
jsDate.getFullYear(),
jsDate.getMonth() + 1,
jsDate.getDate()
).toString()
})
return xAxisDates.map(datestring => {
const cycleDay = getCycleDay(datestring)
const temp = cycleDay && cycleDay.temperature && cycleDay.temperature.value
return {
label: datestring,
cycleDay,
y: temp ? normalizeToScale(temp) : null
}
})
}
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
}