Make chart scrollable
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import { ScrollView } from 'react-native'
|
import { ScrollView } from 'react-native'
|
||||||
import { bleedingDaysSortedByDate, temperatureDaysSortedByDate, getOrCreateCycleDay } from './db'
|
|
||||||
import range from 'date-range'
|
import range from 'date-range'
|
||||||
import Svg,{
|
import Svg,{
|
||||||
G,
|
G,
|
||||||
@@ -10,8 +9,9 @@ import Svg,{
|
|||||||
Circle
|
Circle
|
||||||
} from 'react-native-svg'
|
} from 'react-native-svg'
|
||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
|
import { bleedingDaysSortedByDate, temperatureDaysSortedByDate, getOrCreateCycleDay } from './db'
|
||||||
|
|
||||||
const right = 350
|
const right = 600
|
||||||
const top = 10
|
const top = 10
|
||||||
const bottom = 350
|
const bottom = 350
|
||||||
const columnWidth = 30
|
const columnWidth = 30
|
||||||
@@ -20,7 +20,6 @@ const dateRow = {
|
|||||||
width: right
|
width: right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getPreviousDays(n) {
|
function getPreviousDays(n) {
|
||||||
const today = new Date()
|
const today = new Date()
|
||||||
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0)
|
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0)
|
||||||
@@ -28,6 +27,7 @@ function getPreviousDays(n) {
|
|||||||
|
|
||||||
return range(twoWeeksAgo, today).reverse()
|
return range(twoWeeksAgo, today).reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
const xAxisDates = getPreviousDays(14).map(jsDate => {
|
const xAxisDates = getPreviousDays(14).map(jsDate => {
|
||||||
return LocalDate.of(
|
return LocalDate.of(
|
||||||
jsDate.getFullYear(),
|
jsDate.getFullYear(),
|
||||||
@@ -35,6 +35,7 @@ const xAxisDates = getPreviousDays(14).map(jsDate => {
|
|||||||
jsDate.getDate()
|
jsDate.getDate()
|
||||||
).toString()
|
).toString()
|
||||||
})
|
})
|
||||||
|
|
||||||
const xAxisDatesWithRightOffset = xAxisDates.map((datestring, columnIndex) => {
|
const xAxisDatesWithRightOffset = xAxisDates.map((datestring, columnIndex) => {
|
||||||
const rightOffset = right - (columnWidth * (columnIndex + 1))
|
const rightOffset = right - (columnWidth * (columnIndex + 1))
|
||||||
return {
|
return {
|
||||||
@@ -57,9 +58,9 @@ function normalizeToScale(temp) {
|
|||||||
low: 33,
|
low: 33,
|
||||||
high: 40
|
high: 40
|
||||||
}
|
}
|
||||||
const tempInScaleDecs = (scale.high - temp) / (scale.high - scale.low)
|
const valueRelativeToScale = (scale.high - temp) / (scale.high - scale.low)
|
||||||
const scaleHeight = bottom - top
|
const scaleHeight = bottom - top
|
||||||
return scaleHeight * tempInScaleDecs
|
return scaleHeight * valueRelativeToScale
|
||||||
}
|
}
|
||||||
|
|
||||||
function placeBleedingSymbolsOnColumns() {
|
function placeBleedingSymbolsOnColumns() {
|
||||||
@@ -107,16 +108,30 @@ export default class SvgExample extends Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.scrollContainer.scrollToEnd()
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<ScrollView horizontal={true}>
|
<ScrollView
|
||||||
|
ref={(scroll) => {
|
||||||
|
if (scroll) this.scrollContainer = scroll
|
||||||
|
}}
|
||||||
|
horizontal={true}>
|
||||||
|
|
||||||
<Svg
|
<Svg
|
||||||
height="350"
|
height="350"
|
||||||
width="2000"
|
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()}
|
||||||
>
|
>
|
||||||
{ xAxisDatesWithRightOffset.map(this.makeDayColumn.bind(this)) }
|
{xAxisDatesWithRightOffset.map(this.makeDayColumn.bind(this))}
|
||||||
|
|
||||||
{ placeBleedingSymbolsOnColumns() }
|
{placeBleedingSymbolsOnColumns()}
|
||||||
|
|
||||||
<Polyline
|
<Polyline
|
||||||
points={determineCurvePoints()}
|
points={determineCurvePoints()}
|
||||||
|
|||||||
Reference in New Issue
Block a user