使用 VictoryChart 响应原生 flex 布局,图表适合父容器

Rat*_*hma 2 layout flexbox react-native react-native-android victory-charts

我有以下渲染功能:

render() {
    return (
        <View style={styles.container}>
            <View style={styles.header}>
                <Text> Header
                </Text>
            </View>
            <View style={styles.services}>
                <Text>Services</Text>
            </View>
            <View style={styles.chart}>

                <VictoryChart>
                    <VictoryLine
                        style={{
                            data: {stroke: "#c43a31"},
                            parent: {border: "1px solid #ccc"}
                        }}
                        data={[
                            {x: 1, y: 2},
                            {x: 2, y: 3},
                            {x: 3, y: 5},
                            {x: 4, y: 4},
                            {x: 5, y: 7}
                        ]}
                    />
                </VictoryChart>    
            </View>
        </View>
    );
}
Run Code Online (Sandbox Code Playgroud)

我在页面底部有某种图表,但我无法强制图表填充其父级,它的专用部分有点溢出,我该如何实现?(我尝试使用flexWrap但没有帮助!)

这是它现在的样子的预览: 在此处输入图片说明

这是我的这个简单设计的样式表:

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    },
    header: {
        flex: 1,
        backgroundColor: '#e7fe52'
    },

    services: {
        flex: 4,
        backgroundColor: '#20fe23'
    },

    chart: {
        flex: 2,
        flexWrap: 'wrap',
        backgroundColor: '#4fccb0'
    }
});
Run Code Online (Sandbox Code Playgroud)

nee*_*eep 6

VictoryChart 似乎有一个默认的高度 300,它不适应它的父高度。因此,解决此问题的一种方法是从图表样式中删除 flex: 2 属性并添加手动计算的维度。像这样的东西:

const chartHeight = Dimensions.get("window").height * 0.3;
const chartWidth = Dimensions.get("window").width;

<VictoryChart height={chartHeight} width={chartWidth} >
    <VictoryLine
        style={{
            data: {stroke: "#c43a31"},
            parent: {border: "1px solid #ccc"}
        }}
        data={[
            {x: 1, y: 2},
            {x: 2, y: 3},
            {x: 3, y: 5},
            {x: 4, y: 4},
            {x: 5, y: 7}
        ]}
    />
</VictoryChart>

// styles  
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    },
    header: {
        flex: 1,
        backgroundColor: '#e7fe52'
    },
    services: {
        flex: 4,
        backgroundColor: '#20fe23'
    },
    chart: {
        //remove flex from here
        backgroundColor: '#4fccb0'
    }
});
Run Code Online (Sandbox Code Playgroud)

虽然这并不完美,但它可以工作,但我会为图表设置一个最小高度。我的意思是如果可用空间太小,它看起来不会很好。在这种情况下,您可以将屏幕包裹在 ScrollView 中。