gpt4 book ai didi

react-native - 如何在 React Native 图表工具包中将 Y 轴值从数字更改为字符串

转载 作者:行者123 更新时间:2023-12-05 04:58:31 32 4
gpt4 key购买 nike

我正在使用 react-native-chart-kit显示并尝试更改 y 轴(请参见下面的屏幕截图)。我尝试用字符串数组填充 yLabels 属性。但这没有用。任何帮助将不胜感激!

enter image description here

最佳答案

您可以为此使用 formatYLabel 属性 ( https://github.com/indiespirit/react-native-chart-kit#line-chart ):

/*
* Generator helper function that allows us to
* cleanly return a label from an array
* of labels for each segment of the y-axis
*/
function* yLabel() {
yield* ['None', 'Low', 'Med', 'High'];
}

function App() {
// Instantiate the iterator
const yLabelIterator = yLabel();

return (
<View>
<LineChart
data={{
labels: ['Mo', 'Tu', 'We', 'Fr', 'Sa', 'Su'],
datasets: [
{
data: [3, 2, 1, 4, 4],
},
{
data: [5, 1, 3, 2],
},
],
}}
segments={3}
width={320}
height={200}
formatYLabel={() => yLabelIterator.next().value}
chartConfig={{
backgroundColor: 'white',
backgroundGradientFrom: 'grey',
backgroundGradientTo: 'grey',
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
}}
/>
</View>
);
}

export default App;

如果你想显示 4 个 y 轴标签,你需要将 LineChartsegments 属性设置为 3。

如果您想了解更多有关 Javascript 中的 yield 的信息,您可以阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield* .

关于react-native - 如何在 React Native 图表工具包中将 Y 轴值从数字更改为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63905403/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com