android - 如何使用库提供的辅助函数?

标签 android reactjs react-native react-hooks

enter image description here

我正在使用react-native-animated-charts来渲染图表。但是,我在读取可移动图表点的 x 和 y 值时遇到困难。文档提到 useChartData 辅助函数可以访问此信息。但是,我不确定如何使用(甚至在哪里使用或初始化此功能)。

编辑:我添加了下面的代码

import React, {useEffect, useState, useRef} from 'react';
import {Text, Dimensions, View, TouchableHighlight, StyleSheet} from 'react-native';
import {
  ChartDot,
  ChartPath,
  ChartPathProvider,
  ChartYLabel,
  ChartXLabel,
  useChartData,
  monotoneCubicInterpolation,
} from '@rainbow-me/animated-charts';
import {runOnJS} from 'react-native-reanimated';
import Card2View from '..//Card2View/Card2View';


import styles from './styles';

export const {width: SIZE, height} = Dimensions.get('window');
const TABLE_ITEM_OFFSET = 10;
const TABLE_ITEM_MARGIN = TABLE_ITEM_OFFSET * 2;
const SCREEN_WIDTH = SIZE < height ? SIZE : height;

export const data = [
  {x: 1453075200, y: 1.47},
  {x: 1453161600, y: 1.37},
  {x: 1453248000, y: 1.53},
  {x: 1453334400, y: 1.54},
  {x: 1453420800, y: 1.52},
  {x: 1453507200, y: 2.03},
  {x: 1453593600, y: 2.1},
  {x: 1453680000, y: 2.5},
  {x: 1453766400, y: 2.3},
  {x: 1453852800, y: 2.42},
  {x: 1453939200, y: 2.55},
  {x: 1454025600, y: 2.41},
  {x: 1454112000, y: 2.43},
  {x: 1454198400, y: 2.2},
];



const points = monotoneCubicInterpolation({data, range: 40});
const LineChartView1 = ({priceData}) => {

  const [activeChart, setActiveChart] = useState(0)
  const lineChartTables = ['1D', '1W', '1M', '3M', '1Y', 'ALL'];

  const output = useChartData()

  console.log(output);

  const getX = value => {
    'worklet';
    // console.log(runOnJS(useChartData("state")));
    if (value === '') {
      return '';
    }
    return `$ ${value.toLocaleString('en-US', {
      currency: 'USD',
    })}`;
  };
  
  const getY = value => {
    'worklet';
    // console.log(runOnJS(useChartData("state")));
    if (value === '') {
      return '';
    }
    const date = new Date(Number(value * 1000));
    const s = date.getSeconds();
    const m = date.getMinutes();
    const h = date.getHours();
    const d = date.getDate();
    const n = date.getMonth();
    const y = date.getFullYear();
    return `${y}-${n}-${d} ${h}:${m}:${s}`;
  };

 

  renderTable = (item, index) => (
    <TouchableHighlight
        onPress={() => setActiveChart(index)}
        underlayColor="rgba(73,182,77,1,0.9)"
        key={index}
        style={
          activeChart == index
            ? {
                justifyContent: 'center',
                backgroundColor: '#617180',
                borderRadius: 5,
                flex: 1,
                alignItems: 'center',
                margin: TABLE_ITEM_OFFSET,
                width:
                  (SCREEN_WIDTH - TABLE_ITEM_MARGIN) / lineChartTables.length -
                  TABLE_ITEM_OFFSET,
                height:
                  (SCREEN_WIDTH - TABLE_ITEM_MARGIN) / lineChartTables.length -
                  TABLE_ITEM_OFFSET,
                maxWidth: 50,
                maxHeight: 50
              }
            : {
                justifyContent: 'center',
                backgroundColor: 'white',
                borderRadius: 5,
                flex: 1,
                alignItems: 'center',
                margin: TABLE_ITEM_OFFSET,
                width:
                  (SCREEN_WIDTH - TABLE_ITEM_MARGIN) / lineChartTables.length -
                  TABLE_ITEM_OFFSET,
                height:
                  (SCREEN_WIDTH - TABLE_ITEM_MARGIN) / lineChartTables.length -
                  TABLE_ITEM_OFFSET,
                maxWidth: 50,
                maxHeight: 50
              }
        }
      >
        <Text style={activeChart == index ? chart.activeChartTxt : chart.chartTxt}>
          {item}
        </Text>
      </TouchableHighlight>
    );
      
  return(
    
    <View>
      <Card2View item={{title:priceData.symbol, text:priceData.lastUpdatedPrice, money:`Rs. ${priceData.lastUpdatedPrice}`, procent:`${(priceData.percentageChange).toFixed(2)}`}} />
    <View
    style={{backgroundColor: 'white'}}>
      
    <ChartPathProvider
      data={{
        points,
        smoothingStrategy: 'bezier',
      }}
      >
        
      <ChartPath height={SIZE / 2} stroke="black" strokeWidth="2" selectedOpacity="0.3" width={SIZE} />
      <ChartDot
        style={{
          backgroundColor: 'black',
        }}
        size={15}
      />
      {/* <ChartYLabel format={getX} style={{backgroundColor: 'white', color: 'black'}}/>
      <ChartXLabel format={getY} style={{backgroundColor: 'white', color: 'black'}}/> */}
    </ChartPathProvider>


    <View style={{ flexDirection: 'row', justifyContent: 'space-around', flex: 1 }}>
        {lineChartTables.map((data, index) => renderTable(data, index))}
    </View>
  </View>
  </View>
  )
  
}

export default LineChartView1;

const chart = StyleSheet.create({
  chartTxt: {
    fontSize: 14,
    color: 'black'
  },
  activeChartTxt: {
    fontSize: 14,
    color: 'white',
    fontWeight: 'bold'
  }
});

最佳答案

您需要调用 ChartPathProvider 内的 Hook 。示例:

const ChildComponent = () => {
    const values = useChartData();
    return <Text>{values.greatestX}</Text>
}
const ParentComponent = ({ points }) => (
   <ChartPathProvider 
       data={{
           points,
           smoothingStrategy: 'bezier',
       }}
   >
       {/* other chart components */}
       <ChildComponent />
   </ChartPathProvider>
)

在您的示例中,您在定义提供程序之前(即在 return 语句之前)调用 Hook 。

关于android - 如何使用库提供的辅助函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67431154/

相关文章:

android - 在 Android 应用中拥有自己的广告

java - removeView( View )在 ViewGroup 的子类中不起作用

javascript - React.createElement 将 props 传递给子组件

java - Android:LayoutParams 不接受浮点值

javascript - 如何从键盘编辑时间选择器的值?

javascript - 未处理的 JS 异常 : Cannot create styled-component for component: [object Object]

ios - React Native WebView (iOS) 中的 SVG 加载问题

react-native - 如何在 ubuntu 19.04 中完全卸载 brew 并重新安装 brew

react-native - 使用 React Native 在 iOS 模拟器上无法识别的字体系列

java - 如何计算包含一组元素的矩形