Newer
Older
import React, { Component } from 'react'
import { ScrollView } from 'react-native'
import range from 'date-range'
import Svg,{
G,
Polyline,
Rect,
Text,
} from 'react-native-svg'
import { LocalDate } from 'js-joda'
import { bleedingDaysSortedByDate, temperatureDaysSortedByDate, getOrCreateCycleDay } from './db'
const top = 10
const bottom = 350
const columnWidth = 30
const dateRow = {
height: 30,
width: right
}
constructor(props) {
super(props)
}
passDateToDayView(dateString) {
const cycleDay = getOrCreateCycleDay(dateString)
this.props.navigation.navigate('cycleDay', { cycleDay })
}
return (
<Rect
y={top}
width={columnWidth}
height={bottom - top - dateRow.height}
fill="lightgrey"
strokeWidth="1"
stroke="grey"
/>
<Text
fontSize="10"
y={bottom - top - dateRow.height}
</G>
)
}
makeColumnGrid(xAxisTicks) {
return xAxisTicks.map(this.makeDayColumn.bind(this))
}
placeBleedingSymbolsOnColumns() {
return bleedingDaysSortedByDate
.filter(cycleDayIsNotInTheFuture())
.map(day => {
const match = this.xAxisTicks.find(tick => {
return tick.label === day.date
})
const x = match.rightOffset + columnWidth / 2
return (<Circle key={day.date} cx={x} cy="50" r="7" fill="red" />)
return temperatureDaysSortedByDate
.filter(cycleDayIsNotInTheFuture())
.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()
}
<ScrollView
ref={(scroll) => {
if (scroll) this.scrollContainer = scroll
}}
horizontal={true}>
width={right}
// the svg is not complete on 'componentDidMount' = why?
// not sure if this is the right event, for now a hack
// because there is no 'onLoad' attribute
// we scroll to the very left because we want to show the most recent data
onLayout={() => this.scrollContainer.scrollToEnd()}
{this.makeColumnGrid(this.xAxisTicks)}
{this.placeBleedingSymbolsOnColumns()}
/>
</Svg>
</ScrollView>
)
}
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}
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
}
function cycleDayIsNotInTheFuture() {
const today = LocalDate.now()
return function (cycleDay) {
const cycleDayLocalDate = LocalDate.parse(cycleDay.date)
return cycleDayLocalDate.isBefore(today) || cycleDayLocalDate.isEqual(today)
}