Draw curve as FlatList instead of monolithic SVG
This commit is contained in:
+71
-166
@@ -1,20 +1,21 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import { ScrollView } from 'react-native'
|
import { FlatList } from 'react-native'
|
||||||
import range from 'date-range'
|
import range from 'date-range'
|
||||||
import Svg,{
|
import Svg,{
|
||||||
G,
|
G,
|
||||||
Polyline,
|
|
||||||
Rect,
|
Rect,
|
||||||
Text,
|
Text,
|
||||||
Circle
|
Circle,
|
||||||
|
Line
|
||||||
} from 'react-native-svg'
|
} from 'react-native-svg'
|
||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
import { bleedingDaysSortedByDate, temperatureDaysSortedByDate, getOrCreateCycleDay } from '../db'
|
import { getCycleDay, getOrCreateCycleDay, cycleDaysSortedByDate } from '../db'
|
||||||
|
|
||||||
const right = 600
|
const right = 600
|
||||||
const top = 10
|
const top = 10
|
||||||
const bottom = 350
|
const bottom = 350
|
||||||
const columnWidth = 30
|
const columnWidth = 30
|
||||||
|
const middle = columnWidth / 2
|
||||||
const dateRow = {
|
const dateRow = {
|
||||||
height: 30,
|
height: 30,
|
||||||
width: right
|
width: right
|
||||||
@@ -30,36 +31,21 @@ const curveColor = 'darkblue'
|
|||||||
export default class CycleChart extends Component {
|
export default class CycleChart extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.xAxisTicks = makeXAxisTicks(cycleDaysToShow)
|
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
curveCoordinates: this.makeCurveCoordinates(),
|
columns: makeColumnInfo(cycleDaysToShow)
|
||||||
bleedIconCoordinates: this.makeBleedIconCoordinates()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setStateWithNewCurveCoordinates = (function (chartComponent) {
|
this.recalculateChartInfo = (function(Chart) {
|
||||||
return function () {
|
return function() {
|
||||||
chartComponent.setState({
|
Chart.setState({columns: makeColumnInfo(cycleDaysToShow)})
|
||||||
curveCoordinates: chartComponent.makeCurveCoordinates()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})(this)
|
})(this)
|
||||||
|
|
||||||
this.setStateWithNewBleedIconCoordinates = (function (chartComponent) {
|
cycleDaysSortedByDate.addListener(this.recalculateChartInfo)
|
||||||
return function () {
|
|
||||||
chartComponent.setState({
|
|
||||||
bleedIconCoordinates: chartComponent.makeBleedIconCoordinates()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})(this)
|
|
||||||
|
|
||||||
temperatureDaysSortedByDate.addListener(this.setStateWithNewCurveCoordinates)
|
|
||||||
bleedingDaysSortedByDate.addListener(this.setStateWithNewBleedIconCoordinates)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
temperatureDaysSortedByDate.removeListener(this.setStateWithNewCurveCoordinates)
|
cycleDaysSortedByDate.removeListener(this.recalculateChartInfo)
|
||||||
temperatureDaysSortedByDate.removeListener(this.setStateWithNewBleedIconCoordinates)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
passDateToDayView(dateString) {
|
passDateToDayView(dateString) {
|
||||||
@@ -67,11 +53,11 @@ export default class CycleChart extends Component {
|
|||||||
this.props.navigation.navigate('cycleDay', { cycleDay })
|
this.props.navigation.navigate('cycleDay', { cycleDay })
|
||||||
}
|
}
|
||||||
|
|
||||||
makeDayColumn(columnInfo) {
|
makeDayColumn({ label, cycleDay, y }, index) {
|
||||||
return (
|
return (
|
||||||
<G key={columnInfo.label}>
|
<G key={label} onPress={() => this.passDateToDayView(label)}>
|
||||||
<Rect
|
<Rect
|
||||||
x={columnInfo.rightOffset}
|
x={0}
|
||||||
y={top}
|
y={top}
|
||||||
width={columnWidth}
|
width={columnWidth}
|
||||||
height={bottom - top - dateRow.height}
|
height={bottom - top - dateRow.height}
|
||||||
@@ -82,100 +68,78 @@ export default class CycleChart extends Component {
|
|||||||
<Text
|
<Text
|
||||||
stroke="grey"
|
stroke="grey"
|
||||||
fontSize="10"
|
fontSize="10"
|
||||||
x={columnInfo.rightOffset}
|
x={0}
|
||||||
y={bottom - top - dateRow.height}
|
y={bottom - top - dateRow.height}
|
||||||
>{columnInfo.label.split('-')[2]}</Text>
|
>{label.split('-')[2]}</Text>
|
||||||
|
|
||||||
|
{cycleDay && cycleDay.bleeding ? <Circle cx={middle} cy="50" r="7" fill="red" /> : null}
|
||||||
|
|
||||||
|
{y ? this.drawDotAndLine(y, index) : null}
|
||||||
</G>
|
</G>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
makeColumnGrid(xAxisTicks) {
|
drawDotAndLine(currY, index) {
|
||||||
return xAxisTicks.map(this.makeDayColumn.bind(this))
|
let lineToRight
|
||||||
}
|
let lineToLeft
|
||||||
|
const cols = this.state.columns
|
||||||
|
|
||||||
placeTouchHandlerRectangles() {
|
function makeLine(otherColY, x) {
|
||||||
return this.xAxisTicks.map(columnInfo => {
|
const middleY = ((otherColY - currY) / 2) + currY
|
||||||
return (
|
const rightTarget = [x, middleY]
|
||||||
<Rect
|
return <Line
|
||||||
key={columnInfo.label}
|
x1={middle}
|
||||||
x={columnInfo.rightOffset}
|
y1={currY}
|
||||||
y={top}
|
x2={rightTarget[0]}
|
||||||
width={columnWidth}
|
y2={rightTarget[1]}
|
||||||
height={bottom - top - dateRow.height}
|
stroke={'lightseagreen'}
|
||||||
fillOpacity={0}
|
strokeWidth={2}
|
||||||
onPress={() => this.passDateToDayView(columnInfo.label)}
|
|
||||||
/>
|
/>
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
placeBleedingSymbolsOnColumns() {
|
const thereIsADotToTheRight = index > 0 && cols[index - 1].y
|
||||||
return this.state.bleedIconCoordinates.map(x => {
|
const thereIsADotToTheLeft = index < cols.length - 1 && cols[index + 1].y
|
||||||
return (<Circle key={x} cx={x} cy="50" r="7" fill="red" />)
|
|
||||||
})
|
if (thereIsADotToTheRight) {
|
||||||
|
lineToRight = makeLine(cols[index - 1].y, columnWidth)
|
||||||
|
}
|
||||||
|
if (thereIsADotToTheLeft) {
|
||||||
|
lineToLeft = makeLine(cols[index + 1].y, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
makeCurveCoordinates() {
|
return (<G>
|
||||||
return temperatureDaysSortedByDate
|
<Circle
|
||||||
.filter(cycleDayIsNotInTheFuture())
|
cx={middle}
|
||||||
.reduce(separateIntoContinousChunks, [[]])
|
cy={currY}
|
||||||
.map(makeCurveCoordinatesForChunk.bind(this))
|
r={dotRadius}
|
||||||
}
|
fill={curveColor}
|
||||||
|
/>
|
||||||
makeBleedIconCoordinates() {
|
{lineToRight}
|
||||||
return bleedingDaysSortedByDate
|
{lineToLeft}
|
||||||
.filter(cycleDayIsNotInTheFuture())
|
</G>)
|
||||||
.map(day => {
|
|
||||||
const match = this.xAxisTicks.find(tick => {
|
|
||||||
return tick.label === day.date
|
|
||||||
})
|
|
||||||
return match.rightOffset + columnWidth / 2
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
makeTemperatureCurves() {
|
|
||||||
return this.state.curveCoordinates.map(makeCurveFromPoints)
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
this.scrollContainer.scrollToEnd()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<ScrollView
|
<FlatList
|
||||||
ref={(scroll) => {
|
horizontal={true}
|
||||||
if (scroll) this.scrollContainer = scroll
|
inverted={true}
|
||||||
}}
|
data={this.state.columns}
|
||||||
horizontal={true}>
|
renderItem={({item, index}) => {
|
||||||
|
return (
|
||||||
<Svg
|
<Svg width={columnWidth} height={bottom}>
|
||||||
height="350"
|
{this.makeDayColumn(item, index)}
|
||||||
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() }
|
|
||||||
|
|
||||||
{ this.makeTemperatureCurves() }
|
|
||||||
|
|
||||||
{/* we place a trasnparent rectangle over every day column */}
|
|
||||||
{/* so that all elements including the line and circles are clickable */}
|
|
||||||
{ this.placeTouchHandlerRectangles() }
|
|
||||||
|
|
||||||
</Svg>
|
</Svg>
|
||||||
</ScrollView>
|
)
|
||||||
|
}}
|
||||||
|
keyExtractor={item => item.label}
|
||||||
|
>
|
||||||
|
</FlatList>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeXAxisTicks(n) {
|
function makeColumnInfo(n) {
|
||||||
const xAxisDates = getPreviousDays(n).map(jsDate => {
|
const xAxisDates = getPreviousDays(n).map(jsDate => {
|
||||||
return LocalDate.of(
|
return LocalDate.of(
|
||||||
jsDate.getFullYear(),
|
jsDate.getFullYear(),
|
||||||
@@ -184,11 +148,13 @@ function makeXAxisTicks(n) {
|
|||||||
).toString()
|
).toString()
|
||||||
})
|
})
|
||||||
|
|
||||||
return xAxisDates.map((datestring, columnIndex) => {
|
return xAxisDates.map(datestring => {
|
||||||
const rightOffset = right - (columnWidth * (columnIndex + 1))
|
const cycleDay = getCycleDay(datestring)
|
||||||
|
const temp = cycleDay && cycleDay.temperature && cycleDay.temperature.value
|
||||||
return {
|
return {
|
||||||
label: datestring,
|
label: datestring,
|
||||||
rightOffset
|
cycleDay,
|
||||||
|
y: temp ? normalizeToScale(temp) : null
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -206,64 +172,3 @@ function normalizeToScale(temp) {
|
|||||||
const scaleHeight = bottom - top
|
const scaleHeight = bottom - top
|
||||||
return scaleHeight * valueRelativeToScale
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function separateIntoContinousChunks(curveChunks, curr) {
|
|
||||||
const lastChunk = curveChunks[curveChunks.length - 1]
|
|
||||||
const lastSeenCycleDate = lastChunk.length && lastChunk[lastChunk.length - 1]
|
|
||||||
|
|
||||||
if (!lastSeenCycleDate) {
|
|
||||||
lastChunk.push(curr)
|
|
||||||
return curveChunks
|
|
||||||
}
|
|
||||||
|
|
||||||
const lastSeenLocalDate = LocalDate.parse(lastSeenCycleDate.date)
|
|
||||||
const currLocalDate = LocalDate.parse(curr.date)
|
|
||||||
if (lastSeenLocalDate.compareTo(currLocalDate) === 1) {
|
|
||||||
lastChunk.push(curr)
|
|
||||||
} else {
|
|
||||||
curveChunks.push([curr])
|
|
||||||
}
|
|
||||||
|
|
||||||
return curveChunks
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCurveCoordinatesForChunk(chunk) {
|
|
||||||
return chunk
|
|
||||||
.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]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCurveFromPoints(curveChunkPoints, i) {
|
|
||||||
const pointsInPolyLineFormat = curveChunkPoints
|
|
||||||
.map(xYPair => xYPair.join())
|
|
||||||
.join(' ')
|
|
||||||
|
|
||||||
return (
|
|
||||||
<G key={i}>
|
|
||||||
<Polyline
|
|
||||||
points={pointsInPolyLineFormat}
|
|
||||||
fill="none"
|
|
||||||
stroke={curveColor}
|
|
||||||
strokeWidth="2"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
/>
|
|
||||||
{ makeDots(curveChunkPoints) }
|
|
||||||
</G>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeDots(points) {
|
|
||||||
return points.map(([x, y], i) => <Circle cx={x} cy={y} r={dotRadius} fill={curveColor} key={i} />)
|
|
||||||
}
|
|
||||||
@@ -53,7 +53,7 @@ function saveTemperature(cycleDay, temperature) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const getCycleDaysSortedByDateView = () => db.objects('CycleDay').sorted('date', true)
|
const cycleDaysSortedByDate = db.objects('CycleDay').sorted('date', true)
|
||||||
|
|
||||||
function saveBleeding(cycleDay, bleeding) {
|
function saveBleeding(cycleDay, bleeding) {
|
||||||
db.write(() => {
|
db.write(() => {
|
||||||
@@ -73,6 +73,10 @@ function getOrCreateCycleDay(localDate) {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCycleDay(localDate) {
|
||||||
|
return db.objectForPrimaryKey('CycleDay', localDate)
|
||||||
|
}
|
||||||
|
|
||||||
function deleteAll() {
|
function deleteAll() {
|
||||||
db.write(() => {
|
db.write(() => {
|
||||||
db.deleteAll()
|
db.deleteAll()
|
||||||
@@ -95,7 +99,8 @@ export {
|
|||||||
getOrCreateCycleDay,
|
getOrCreateCycleDay,
|
||||||
bleedingDaysSortedByDate,
|
bleedingDaysSortedByDate,
|
||||||
temperatureDaysSortedByDate,
|
temperatureDaysSortedByDate,
|
||||||
getCycleDaysSortedByDateView,
|
cycleDaysSortedByDate,
|
||||||
deleteAll,
|
deleteAll,
|
||||||
getPreviousTemperature
|
getPreviousTemperature,
|
||||||
|
getCycleDay
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user