javascript - 用 react-native-chart-kit 图表填充父容器(React Native,Expo)

标签 javascript reactjs react-native expo react-native-chart-kit

我正在使用 react-native-chart-kit 在 React Native Expo 应用程序中渲染图表。文档中提供的示例工作正常

问题:在我的应用中,需要图表来填充父容器,但图表似乎必须具有 height Prop 作为固定值。

<LineChart
    height={221}
    ...
/>

正在尝试设置 height100%或删除定义将导致与 NaN 相关的错误

Invariant Violation: [.....,"y1":"<>","x2":"375","y2":"<>"}] is not usable as a native method argument

这个问题有解决办法吗?谢谢!

基于文档的工作示例

export default class Chart extends Component {

    render() {
        const data = {
            datasets: [{
                data: [
                    Math.random() * 100,
                    Math.random() * 100,
                    Math.random() * 100,
                    Math.random() * 100,
                    Math.random() * 100,
                    Math.random() * 100
                ]
            }]
        }

        return (
            <LineChart 
                data={data}
                width={Dimensions.get('window').width}
                height={221}
                yAxisLabel={'$'}
                chartConfig={{
                    backgroundColor: '#e26a00',
                    backgroundGradientFrom: '#fb8c00',
                    backgroundGradientTo: '#ffa726',
                    decimalPlaces: 2, // optional, defaults to 2dp
                    color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
                    style: {
                        borderRadius: 16
                    }
                }}
                bezier
                style={{
                    marginVertical: 8,
                    borderRadius: 16
                }}
            />
        )
    }

}                   

@hong 开发

react-native-chart-kit组件 <LineChart />可以是另一个高度可变的组件的子组件。所以<LineChart />不一定具有 Dimensions.get('window').height; 给出的高度.例如,

render() {
    <View>
        <Header />
        { this.props.foo ? this.renderFoo() : null }
        <View>
            <LineChart 
                ...
            />
        </View>
    </View>
}

最佳答案

height 的类型是number。因此,字符串值 是不允许的。如果你想替换你正在尝试做的事情,你可以使用它。

const screenheight = Dimensions.get('window').height;
<LineChart
    height={screenheight}
    ...
/>

引用this document,可以看到不设置高度就看不到图表。因为宽高是设置值,所以必须无条件记下这个值。

关于javascript - 用 react-native-chart-kit 图表填充父容器(React Native,Expo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650101/

相关文章:

javascript - .map 中的条件 if 语句

reactjs - 如果初始化没有延迟,回流触发器将无法工作

android - React-Native:显示加载屏幕直到加载 webview

javascript - 如何在 React 中克隆子项

javascript - html textarea 如何在文本区域内的内容上应用样式

javascript - Angular 拼接功能删除另一条记录而不是实际选择的记录

node.js - 使用自签名证书将 API 调用到我的服务器时证书被撤销

javascript - 如何在提交表单后查找在PHP中添加的Div的数量

Javascript 和 CSS : how to get the value of the style sheet file?

javascript - 如何在 React Native 中更改原生 Picker fontSize 和 fontFamily