Moves out chart (data modelling) helpers to a separate file
This commit is contained in:
@@ -19,7 +19,7 @@ import config from '../../config'
|
|||||||
import cycleModule from '../../lib/cycle'
|
import cycleModule from '../../lib/cycle'
|
||||||
import { getCycleDay } from '../../db'
|
import { getCycleDay } from '../../db'
|
||||||
import DotAndLine from './dot-and-line'
|
import DotAndLine from './dot-and-line'
|
||||||
import { normalizeToScale } from './y-axis'
|
import { normalizeToScale } from '../helpers/chart'
|
||||||
|
|
||||||
const label = styles.column.label
|
const label = styles.column.label
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getCycleStatusForDay } from '../../lib/sympto-adapter'
|
import { getCycleStatusForDay } from '../../lib/sympto-adapter'
|
||||||
import { normalizeToScale } from './y-axis'
|
import { normalizeToScale } from '../helpers/chart'
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const cycle = {
|
const cycle = {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { View } from 'react-native'
|
|||||||
|
|
||||||
import Tick from './tick'
|
import Tick from './tick'
|
||||||
|
|
||||||
import { getTickList } from './y-axis'
|
import { getTickList } from '../helpers/chart'
|
||||||
|
|
||||||
import styles from './styles'
|
import styles from './styles'
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ import React from 'react'
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { View } from 'react-native'
|
import { View } from 'react-native'
|
||||||
|
|
||||||
import config from '../../config'
|
import { getTickPositions } from '../helpers/chart'
|
||||||
import { scaleObservable, unitObservable } from '../../local-storage'
|
|
||||||
|
|
||||||
import SymptomIcon from './symptom-icon'
|
import SymptomIcon from './symptom-icon'
|
||||||
import TickList from './tick-list'
|
import TickList from './tick-list'
|
||||||
@@ -11,44 +10,6 @@ import ChartLegend from './chart-legend'
|
|||||||
|
|
||||||
import styles from './styles'
|
import styles from './styles'
|
||||||
|
|
||||||
export function getTickList(columnHeight) {
|
|
||||||
|
|
||||||
const units = unitObservable.value
|
|
||||||
const scaleMax = scaleObservable.value.max
|
|
||||||
|
|
||||||
return getTickPositions(columnHeight).map((tickPosition, i) => {
|
|
||||||
|
|
||||||
const tick = scaleMax - i * units
|
|
||||||
let isBold, label, shouldShowLabel
|
|
||||||
|
|
||||||
if (Number.isInteger(tick)) {
|
|
||||||
isBold = true
|
|
||||||
label = tick.toString() + '.0'
|
|
||||||
} else {
|
|
||||||
isBold = false
|
|
||||||
label = tick.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
// when temp range <= 3, units === 0.1 we show temp values with step 0.2
|
|
||||||
// when temp range > 3, units === 0.5 we show temp values with step 0.5
|
|
||||||
|
|
||||||
if (units === 0.1) {
|
|
||||||
// show label with step 0.2
|
|
||||||
shouldShowLabel = !(tick * 10 % 2)
|
|
||||||
} else {
|
|
||||||
// show label with step 0.5
|
|
||||||
shouldShowLabel = !(tick * 10 % 5)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
position: tickPosition,
|
|
||||||
label,
|
|
||||||
isBold,
|
|
||||||
shouldShowLabel,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function makeHorizontalGrid(columnHeight, symptomRowHeight) {
|
export function makeHorizontalGrid(columnHeight, symptomRowHeight) {
|
||||||
return getTickPositions(columnHeight).map(tick => {
|
return getTickPositions(columnHeight).map(tick => {
|
||||||
return (
|
return (
|
||||||
@@ -61,33 +22,6 @@ export function makeHorizontalGrid(columnHeight, symptomRowHeight) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTickPositions(columnHeight) {
|
|
||||||
const units = unitObservable.value
|
|
||||||
const scaleMin = scaleObservable.value.min
|
|
||||||
const scaleMax = scaleObservable.value.max
|
|
||||||
const numberOfTicks = (scaleMax - scaleMin) * (1 / units) + 1
|
|
||||||
const tickDistance = 1 / (numberOfTicks - 1)
|
|
||||||
const tickPositions = []
|
|
||||||
for (let i = 0; i < numberOfTicks; i++) {
|
|
||||||
const position = getAbsoluteValue(tickDistance * i, columnHeight)
|
|
||||||
tickPositions.push(position)
|
|
||||||
}
|
|
||||||
return tickPositions
|
|
||||||
}
|
|
||||||
|
|
||||||
export function normalizeToScale(temp, columnHeight) {
|
|
||||||
const scale = scaleObservable.value
|
|
||||||
const valueRelativeToScale = (scale.max - temp) / (scale.max - scale.min)
|
|
||||||
return getAbsoluteValue(valueRelativeToScale, columnHeight)
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAbsoluteValue(relative, columnHeight) {
|
|
||||||
// we add some height to have some breathing room
|
|
||||||
const verticalPadding = columnHeight * config.temperatureScale.verticalPadding
|
|
||||||
const scaleHeight = columnHeight - 2 * verticalPadding
|
|
||||||
return scaleHeight * relative + verticalPadding
|
|
||||||
}
|
|
||||||
|
|
||||||
const YAxis = ({ height, symptomsToDisplay, symptomsSectionHeight }) => {
|
const YAxis = ({ height, symptomsToDisplay, symptomsSectionHeight }) => {
|
||||||
const symptomIconHeight = symptomsSectionHeight / symptomsToDisplay.length
|
const symptomIconHeight = symptomsSectionHeight / symptomsToDisplay.length
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import { scaleObservable, unitObservable } from '../../local-storage'
|
||||||
|
import config from '../../config'
|
||||||
|
|
||||||
|
export function normalizeToScale(temp, columnHeight) {
|
||||||
|
const scale = scaleObservable.value
|
||||||
|
const valueRelativeToScale = (scale.max - temp) / (scale.max - scale.min)
|
||||||
|
return getAbsoluteValue(valueRelativeToScale, columnHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAbsoluteValue(relative, columnHeight) {
|
||||||
|
// we add some height to have some breathing room
|
||||||
|
const verticalPadding = columnHeight * config.temperatureScale.verticalPadding
|
||||||
|
const scaleHeight = columnHeight - 2 * verticalPadding
|
||||||
|
return scaleHeight * relative + verticalPadding
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getTickPositions(columnHeight) {
|
||||||
|
const units = unitObservable.value
|
||||||
|
const scaleMin = scaleObservable.value.min
|
||||||
|
const scaleMax = scaleObservable.value.max
|
||||||
|
const numberOfTicks = (scaleMax - scaleMin) * (1 / units) + 1
|
||||||
|
const tickDistance = 1 / (numberOfTicks - 1)
|
||||||
|
const tickPositions = []
|
||||||
|
for (let i = 0; i < numberOfTicks; i++) {
|
||||||
|
const position = getAbsoluteValue(tickDistance * i, columnHeight)
|
||||||
|
tickPositions.push(position)
|
||||||
|
}
|
||||||
|
return tickPositions
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getTickList(columnHeight) {
|
||||||
|
|
||||||
|
const units = unitObservable.value
|
||||||
|
const scaleMax = scaleObservable.value.max
|
||||||
|
|
||||||
|
return getTickPositions(columnHeight).map((tickPosition, i) => {
|
||||||
|
|
||||||
|
const tick = scaleMax - i * units
|
||||||
|
let isBold, label, shouldShowLabel
|
||||||
|
|
||||||
|
if (Number.isInteger(tick)) {
|
||||||
|
isBold = true
|
||||||
|
label = tick.toString() + '.0'
|
||||||
|
} else {
|
||||||
|
isBold = false
|
||||||
|
label = tick.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// when temp range <= 3, units === 0.1 we show temp values with step 0.2
|
||||||
|
// when temp range > 3, units === 0.5 we show temp values with step 0.5
|
||||||
|
|
||||||
|
if (units === 0.1) {
|
||||||
|
// show label with step 0.2
|
||||||
|
shouldShowLabel = !(tick * 10 % 2)
|
||||||
|
} else {
|
||||||
|
// show label with step 0.5
|
||||||
|
shouldShowLabel = !(tick * 10 % 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
position: tickPosition,
|
||||||
|
label,
|
||||||
|
isBold,
|
||||||
|
shouldShowLabel,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user