首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react原生图表工具包中将Y轴值从数字更改为字符串

如何在react原生图表工具包中将Y轴值从数字更改为字符串
EN

Stack Overflow用户
提问于 2020-09-15 23:44:15
回答 1查看 1.6K关注 0票数 1

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

EN

回答 1

Stack Overflow用户

发布于 2020-09-24 22:58:52

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

代码语言:javascript
复制
/*
 * 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*

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63905403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档