From a54e48b17ed7cadbb71112570dbf52cc065cd9bd Mon Sep 17 00:00:00 2001 From: bl00dymarie Date: Wed, 3 Apr 2024 20:39:59 +0200 Subject: [PATCH] Feature: Combine dynamic month label with moving month label in x axis Co-authored-by: @livgm --- components/chart/chart-legend.js | 9 ++++++--- components/chart/chart.js | 32 +++++++++++++++++++++++++++++--- components/chart/y-axis.js | 4 +++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/components/chart/chart-legend.js b/components/chart/chart-legend.js index 9c59875..d21b507 100644 --- a/components/chart/chart-legend.js +++ b/components/chart/chart-legend.js @@ -1,21 +1,23 @@ import React from 'react' import PropTypes from 'prop-types' import { StyleSheet, View } from 'react-native' +import moment from 'moment' import AppText from '../common/app-text' import { Sizes, Typography } from '../../styles' import { CHART_YAXIS_WIDTH } from '../../config' -import { shared as labels } from '../../i18n/en/labels' -const ChartLegend = ({ height }) => { +const ChartLegend = ({ height, currentDate }) => { + const displayedMonth = moment(currentDate).format('MMM') + return ( # - {labels.date} + {displayedMonth} ) @@ -23,6 +25,7 @@ const ChartLegend = ({ height }) => { ChartLegend.propTypes = { height: PropTypes.number.isRequired, + currentDate: PropTypes.string, } const styles = StyleSheet.create({ diff --git a/components/chart/chart.js b/components/chart/chart.js index 5cc54b3..47757d4 100644 --- a/components/chart/chart.js +++ b/components/chart/chart.js @@ -32,6 +32,7 @@ import { CHART_GRID_LINE_HORIZONTAL_WIDTH, CHART_SYMPTOM_HEIGHT_RATIO, CHART_XAXIS_HEIGHT_RATIO, + CHART_YAXIS_WIDTH, SYMPTOMS, } from '../../config' import { Spacing } from '../../styles' @@ -97,8 +98,7 @@ const CycleChart = ({ navigate, setDate }) => { const shouldShowNoDataWarning = isTemperatureEnabled && chartSymptoms.indexOf('temperature') <= -1 - const { width, height } = Dimensions.get('window') - const numberOfColumnsToRender = Math.round(width / CHART_COLUMN_WIDTH) + const { height } = Dimensions.get('window') const xAxisHeight = height * 0.7 * CHART_XAXIS_HEIGHT_RATIO const remainingHeight = height * 0.7 - xAxisHeight @@ -117,6 +117,29 @@ const CycleChart = ({ navigate, setDate }) => { const columns = makeColumnInfo() + // Monitor scrolling to show proper month abbreviation in symptom chart + + const [currentScrollPosition, setCurrentScrollPosition] = useState(0) + + const handleScroll = (event) => { + const currentPosition = event.nativeEvent.contentOffset.x + setCurrentScrollPosition(currentPosition) + } + + const [numberOfColumnsToRender, setNumberOfColumnsToRender] = useState(0) + const onLayout = (event) => { + const { width } = event.nativeEvent.layout + setNumberOfColumnsToRender( + Math.round((width - CHART_YAXIS_WIDTH) / CHART_COLUMN_WIDTH) + ) + } + + const getcomputedDateInView = () => { + const columnIndex = Math.floor(currentScrollPosition / CHART_COLUMN_WIDTH) + // detect oldest visible day to render its month dynamically + return columns[columnIndex + numberOfColumnsToRender] + } + const renderColumn = ({ item }) => { return ( { contentContainerStyle={styles.pageContainer} scrollViewStyle={styles.page} > - + {shouldShowHint && } {shouldShowNoDataWarning && } @@ -154,12 +177,15 @@ const CycleChart = ({ navigate, setDate }) => { symptomsSectionHeight={symptomRowHeight} shouldShowTemperatureColumn={shouldShowTemperatureColumn} xAxisHeight={xAxisHeight} + computedDate={getcomputedDateInView()} /> {shouldShowTemperatureColumn && ( diff --git a/components/chart/y-axis.js b/components/chart/y-axis.js index c71f236..c96855b 100644 --- a/components/chart/y-axis.js +++ b/components/chart/y-axis.js @@ -14,13 +14,14 @@ const YAxis = ({ symptomsSectionHeight, shouldShowTemperatureColumn, xAxisHeight, + computedDate, }) => { const symptomIconHeight = symptomsSectionHeight / symptomsToDisplay.length return ( {shouldShowTemperatureColumn && } - + {symptomsToDisplay.map((symptom) => (