Skip to content
Snippets Groups Projects
chart.js 7.3 KiB
Newer Older
import React, { Component } from 'react'
import { View, FlatList } from 'react-native'
import range from 'date-range'
import { LocalDate } from 'js-joda'
tina's avatar
tina committed
import Svg, { G } from 'react-native-svg'
import { makeYAxisLabels, makeHorizontalGrid } from './y-axis'
import nfpLines from './nfp-lines'
import DayColumn from './day-column'
import { getCycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
import styles from './styles'
import { scaleObservable } from '../../local-storage'
import config from '../../config'
import { AppText } from '../app-text'
tina's avatar
tina committed
import { shared as labels } from '../labels'
tina's avatar
tina committed
import BleedingIcon from '../../assets/bleeding'
import CervixIcon from '../../assets/cervix'
import DesireIcon from '../../assets/desire'
import MucusIcon from '../../assets/mucus'
import NoteIcon from '../../assets/note'
import PainIcon from '../../assets/pain'
import SexIcon from '../../assets/sex'
Julia Friesel's avatar
Julia Friesel committed
export default class CycleChart extends Component {
    this.cycleDaysSortedByDate = getCycleDaysSortedByDate()
    this.getFhmAndLtlInfo = nfpLines()
  }

  renderColumn = ({ item, index }) => {
    return (
      <DayColumn
        dateString={item}
        index={index}
        navigate={this.props.navigate}
        symptomHeight={this.symptomHeight}
        columnHeight={this.columnHeight}
        chartHeight={this.state.chartHeight}
        symptomRowSymptoms={this.symptomRowSymptoms}
        chartSymptoms={this.chartSymptoms}
        getFhmAndLtlInfo={this.getFhmAndLtlInfo}
      />
    )
  onLayout = ({ nativeEvent }) => {
    if (this.state.chartHeight) return
    const height = nativeEvent.layout.height
    this.setState({ chartHeight: height })
    this.reCalculateChartInfo = () => {
      // how many symptoms need to be displayed on the chart's upper symptom row?
      this.symptomRowSymptoms = [
        'bleeding',
        'mucus',
        'cervix',
        'sex',
        'desire',
        'pain',
        'note'
      ].filter((symptomName) => {
        return this.cycleDaysSortedByDate.some(cycleDay => {
          return cycleDay[symptomName]
        })
      })

      this.xAxisHeight = this.state.chartHeight * config.xAxisHeightPercentage
      const remainingHeight = this.state.chartHeight - this.xAxisHeight
      this.symptomHeight = config.symptomHeightPercentage * remainingHeight
tina's avatar
tina committed
      this.symptomRowHeight = this.symptomRowSymptoms.length *
        this.symptomHeight
      this.columnHeight = remainingHeight - this.symptomRowHeight

      this.chartSymptoms = [...this.symptomRowSymptoms]
      if (this.cycleDaysSortedByDate.some(day => day.temperature)) {
        this.chartSymptoms.push('temperature')
      const columnData = this.makeColumnInfo()
      this.setState({ columns: columnData })
    this.cycleDaysSortedByDate.addListener(this.reCalculateChartInfo)
    this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false)
  }

  componentWillUnmount() {
    this.cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo)
    this.removeObvListener()
    let amountOfCycleDays = getAmountOfCycleDays()
    // if there's not much data yet, we want to show at least 30 days on the chart
    if (amountOfCycleDays < 30) {
      amountOfCycleDays = 30
    } else {
      // we don't want the chart to end abruptly before the first data day
      amountOfCycleDays += 5
    }
    const jsDates = getTodayAndPreviousDays(amountOfCycleDays)
      return LocalDate.of(
        jsDate.getFullYear(),
        jsDate.getMonth() + 1,
        jsDate.getDate()
      ).toString()
    })
  }

  render() {
    return (
      <View
        onLayout={this.onLayout}
        style={{ flexDirection: 'row', flex: 1 }}
      >
Julia Friesel's avatar
Julia Friesel committed
        {!this.state.chartLoaded &&
          <View style={{width: '100%', justifyContent: 'center', alignItems: 'center'}}>
tina's avatar
tina committed
            <AppText>{labels.loading}</AppText>
Julia Friesel's avatar
Julia Friesel committed
          </View>
        }

        {this.state.chartHeight && this.state.chartLoaded &&
tina's avatar
tina committed
            <View style={[styles.yAxis, {height: this.symptomRowHeight}]}>
              {this.symptomRowSymptoms.map(symptomName => {
tina's avatar
tina committed
                return <View
                  style={{ alignItems: 'center', justifyContent: 'center' }}
                  key={symptomName}
                  width={styles.yAxis.width}
tina's avatar
tina committed
                  height={this.symptomRowHeight /
                    this.symptomRowSymptoms.length}
tina's avatar
tina committed
                >
                  <Svg
tina's avatar
tina committed
                    width={styles.yAxis.width * 0.8}
                    height={this.symptomRowHeight /
                      this.symptomRowSymptoms.length * 0.8}
tina's avatar
tina committed
                    viewBox={symptomIcons[symptomName].viewBox}
                  >
                    <G fill={symptomIcons[symptomName].color}>
                      {symptomIcons[symptomName].icon}
                    </G>
                  </Svg>
tina's avatar
tina committed
              })}
tina's avatar
tina committed
            <View style={[styles.yAxis, {height: this.columnHeight}]}>
              {makeYAxisLabels(this.columnHeight)}
            </View>
tina's avatar
tina committed
            <View style={[styles.yAxis, {height: this.xAxisHeight}]}>
tina's avatar
tina committed
              <AppText style = {[
                styles.column.label.number,
                styles.yAxisLabels.cycleDayLabel
              ]}>
tina's avatar
tina committed
                {labels.cycleDayWithLinebreak}
              </AppText>
tina's avatar
tina committed
              <AppText style={[
                styles.column.label.date,
                styles.yAxisLabels.dateLabel
              ]}>
tina's avatar
tina committed
                {labels.date}
              </AppText>
        {this.state.chartHeight && this.state.chartLoaded &&
          makeHorizontalGrid(this.columnHeight, this.symptomRowHeight)
        }

        {this.state.chartHeight &&
          <FlatList
Julia Friesel's avatar
Julia Friesel committed
            horizontal={true}
            inverted={true}
            showsHorizontalScrollIndicator={false}
            data={this.state.columns}
            renderItem={this.renderColumn}
Julia Friesel's avatar
Julia Friesel committed
            initialNumToRender={15}
            maxToRenderPerBatch={5}
Julia Friesel's avatar
Julia Friesel committed
            onLayout={() => this.setState({chartLoaded: true})}
Julia Friesel's avatar
Julia Friesel committed
}

function getTodayAndPreviousDays(n) {
Julia Friesel's avatar
Julia Friesel committed
  const today = new Date()
  today.setHours(0)
  today.setMinutes(0)
  today.setSeconds(0)
  today.setMilliseconds(0)
Julia Friesel's avatar
Julia Friesel committed
  const earlierDate = new Date(today - (range.DAY * n))
Julia Friesel's avatar
Julia Friesel committed

Julia Friesel's avatar
Julia Friesel committed
  return range(earlierDate, today).reverse()
tina's avatar
tina committed
const symptomIcons = {
  'bleeding': {
    'viewBox': '10 10 320 400',
    'color': styles.iconShades.bleeding[3],
    'icon': <BleedingIcon strokeWidth={'5'}/>
  },
  'mucus': {
    'viewBox': '10 10 320 400',
    'color': styles.iconShades.mucus[4],
    'icon': <MucusIcon/>
  },
  'cervix': {
    'viewBox': '10 10 320 440',
    'color': styles.iconShades.cervix[3],
    'icon': <CervixIcon/>
  },
  'desire': {
    'viewBox': '10 10 320 380',
    'color': styles.iconShades.desire[2],
    'icon': <DesireIcon/>
  },
  'sex': {
    'viewBox': '10 10 320 400',
    'color': styles.iconShades.sex[2],
    'icon': <SexIcon/>
  },
  'pain': {
    'viewBox': '10 10 300 400',
    'color': styles.iconShades.pain[0],
    'icon': <PainIcon/>
  },
  'note': {
    'viewBox': '10 10 270 400',
    'color': styles.iconShades.note[0],
    'icon': <NoteIcon/>
  },