2

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

在此处输入图像描述

4

1 回答 1

1

您可以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 轴标签,则需要将segmentsprop设置LineChart为 3。

如果您想了解更多关于yieldJavascript 的信息,可以阅读以下内容:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield* 。

于 2020-09-24T14:58:51.910 回答