This commit is contained in:
Julia Friesel
2018-06-19 16:17:36 +02:00
parent cb21496e2f
commit e85e1c0de9
+74 -64
View File
@@ -19,65 +19,16 @@ const dateRow = {
height: 30, height: 30,
width: right width: right
} }
const temperatureScale = {
function getPreviousDays(n) { low: 33,
const today = new Date() high: 40
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()
} }
const cycleDaysToShow = 40
const xAxisDates = getPreviousDays(14).map(jsDate => { export default class CycleChart extends Component {
return LocalDate.of(
jsDate.getFullYear(),
jsDate.getMonth() + 1,
jsDate.getDate()
).toString()
})
const xAxisDatesWithRightOffset = xAxisDates.map((datestring, columnIndex) => {
const rightOffset = right - (columnWidth * (columnIndex + 1))
return {
label: datestring,
rightOffset
}
})
function determineCurvePoints() {
return temperatureDaysSortedByDate.map(cycleDay => {
const match = xAxisDatesWithRightOffset.find(tick => tick.label === cycleDay.date)
const x = match.rightOffset + columnWidth / 2
const y = normalizeToScale(cycleDay.temperature.value)
return [x,y].join()
}).join(' ')
}
function normalizeToScale(temp) {
const scale = {
low: 33,
high: 40
}
const valueRelativeToScale = (scale.high - temp) / (scale.high - scale.low)
const scaleHeight = bottom - top
return scaleHeight * valueRelativeToScale
}
function placeBleedingSymbolsOnColumns() {
return bleedingDaysSortedByDate.map(day => {
// TODO handle no bleeding days, same for curve
// TODO exclude future bleeding days (??)
const match = xAxisDatesWithRightOffset.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"/>)
})
}
export default class SvgExample extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.xAxisTicks = makeXAxisTicks(cycleDaysToShow)
} }
passDateToDayView(dateString) { passDateToDayView(dateString) {
@@ -85,33 +36,59 @@ export default class SvgExample extends Component {
this.props.navigation.navigate('cycleDay', { cycleDay }) this.props.navigation.navigate('cycleDay', { cycleDay })
} }
makeDayColumn(labelInfo) { makeDayColumn(columnInfo) {
return ( return (
<G key={labelInfo.label}> <G key={columnInfo.label}>
<Rect <Rect
x={labelInfo.rightOffset} x={columnInfo.rightOffset}
y={top} y={top}
width={columnWidth} width={columnWidth}
height={bottom - top - dateRow.height} height={bottom - top - dateRow.height}
fill="lightgrey" fill="lightgrey"
strokeWidth="1" strokeWidth="1"
stroke="grey" stroke="grey"
onPress={() => this.passDateToDayView(labelInfo.label)} onPress={() => this.passDateToDayView(columnInfo.label)}
/> />
<Text <Text
stroke="grey" stroke="grey"
fontSize="10" fontSize="10"
x={labelInfo.rightOffset} x={columnInfo.rightOffset}
y={bottom - top - dateRow.height} y={bottom - top - dateRow.height}
>{labelInfo.label.split('-')[2]}</Text> >{columnInfo.label.split('-')[2]}</Text>
</G> </G>
) )
} }
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 (<Circle key={day.date} cx={x} cy="50" r="7" fill="red" />)
})
}
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() { componentDidMount() {
this.scrollContainer.scrollToEnd() this.scrollContainer.scrollToEnd()
} }
render() { render() {
return ( return (
<ScrollView <ScrollView
@@ -129,12 +106,13 @@ export default class SvgExample extends Component {
// we scroll to the very left because we want to show the most recent data // we scroll to the very left because we want to show the most recent data
onLayout={() => this.scrollContainer.scrollToEnd()} onLayout={() => this.scrollContainer.scrollToEnd()}
> >
{xAxisDatesWithRightOffset.map(this.makeDayColumn.bind(this))}
{placeBleedingSymbolsOnColumns()} {this.makeColumnGrid(this.xAxisTicks)}
{this.placeBleedingSymbolsOnColumns()}
<Polyline <Polyline
points={determineCurvePoints()} points={ this.determineCurvePoints() }
fill="none" fill="none"
stroke="darkblue" stroke="darkblue"
strokeWidth="2" strokeWidth="2"
@@ -144,4 +122,36 @@ export default class SvgExample extends Component {
</ScrollView> </ScrollView>
) )
} }
}
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
} }