发布于 2020-09-24 22:58:52
您可以使用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轴标签,则需要将LineChart的segments属性设置为3。
如果你想在Javascript中学习更多关于yield的知识,你可以阅读这个:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*。
https://stackoverflow.com/questions/63905403
复制相似问题