我不明白如何更改"react-native-svg-charts“的x轴和y轴标签

kiayqfof  于 2023-02-09  发布在  React
关注(0)|答案(2)|浏览(128)

无法理解如何更改"react-native-svg-charts"的x轴和y轴标签。看不到标签

import React,{useState} from 'react';
import {View, StyleSheet, Text, Button, Dimensions, TouchableOpacity} from 'react-native';
// import {LineChart} from "react-native-chart-kit";
import { LineChart, XAxis, YAxis } from 'react-native-svg-charts';

let {height, width} = Dimensions.get("window");//(Below) make it as width as the screen

const GraphComponent_1 = (props) => {
    const {pinnedMeasurements, Labelss} = props;
        const data = [ 50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24 ]
        const xLabels = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
        const yLabels = [ 'Poor', 'Fair', 'Good', 'Very Good', 'Excellent' ]
        
        return (
          <View style={{ height: 200, padding: 20 }}>
            <LineChart
              style={{ flex: 1 }}
              data={data}
              svg={{ stroke: 'rgb(134, 65, 244)' }}
              contentInset={{ top: 20, bottom: 20 }}
            >
              <XAxis
                style={{ marginTop: 10 }}
                data={data}
                formatLabel={(value, index) => xLabels[index]}
                style={{
                    axisLabel: {
                        color: 'red', // changes x axis text color to red
                    },
                }}
              />
              <YAxis
                style={{ marginRight: 10 }}
                data={data}
                contentInset={{ top: 20, bottom: 20 }}
                min={-60}
                max={100}
                numberOfTicks={yLabels.length}
                
                formatLabel={(value, index) => yLabels[index]}
              />

            </LineChart>
          </View>
        )
      }
      
const styles = StyleSheet.create({

});

我做的最好的是这个(下面)与这个错误(下面):

eiee3dmh

eiee3dmh1#

发生错误的原因是index超出范围。xLabels的长度为12,而实际data的长度为15
您可以为x轴添加额外的标签,也可以将数据减少到12。
Y轴也有同样的问题。我看到你想用一些标签将它们"分组"。你可以将numberOfTicks属性与yLabels.length一起使用。然后在格式化标签时,你可以使用index

<YAxis
  style={{ marginRight: 10 }}
  data={data}
  contentInset={{ top: 20, bottom: 20 }}
  numberOfTicks={yLabels.length}
  formatLabel={(value, index) => yLabels[index]}
/>
    • 编辑**

我在YAxisminmax中找到了一些东西。如果提供了它们,错误就消失了。

<YAxis
  style={{ marginRight: 10 }}
  data={data}
  contentInset={{ top: 20, bottom: 20 }}
  min={-60}
  max={100}
  numberOfTicks={yLabels.length}
  formatLabel={(value, index) => yLabels[index]}
/>

对于布局:

<View style={{ height: 200, margin: 20, marginTop: 60, flexDirection: "row" }}>
  <YAxis
    style={{ marginRight: 10 }}
    svg={{
      fill: "grey",
      fontSize: 10,
    }}
    contentInset={{ top: 20, bottom: 20 }}
  />
  <View
    style={{
      flex: 1,
    }}
  >
    <LineChart
      style={{ flex: 1 }}
      svg={{ stroke: "rgb(134, 65, 244)" }}
      contentInset={{ top: 20, bottom: 20 }}
    />
    <XAxis
      contentInset={{ left: 10, right: 10 }}
      svg={{
        fill: "grey",
        fontSize: 10,
      }}
    />
  </View>
</View>
ef1yzkbh

ef1yzkbh2#

错误已解决

import React,{useState} from 'react';
import {View, StyleSheet, Text, Button, Dimensions, TouchableOpacity} from 'react-native';
// import {LineChart} from "react-native-chart-kit";
import { LineChart, XAxis, YAxis } from 'react-native-svg-charts';

let {height, width} = Dimensions.get("window");//(Below) make it as width as the screen

const GraphComponent_1 = (props) => {
    const {pinnedMeasurements, Labelss} = props;
        const data = [ 50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24 ]
        const xLabels = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
        const yLabels = [ 'Poor', 'Fair', 'Good', 'Very Good', 'Excellent' ]
        
        return (
            <View style={{ height: 200, margin: 20, marginTop: 60, flexDirection: "row" }}>
                <YAxis
                    style={{ marginRight: 10 }}
                    svg={{
                    fill: "grey",
                    fontSize: 10,
                    }}
                    contentInset={{ top: 20, bottom: 20 }}
                    data={data}
                min={-60}
                max={100}
                numberOfTicks={yLabels.length}
                formatLabel={(value, index) => yLabels[index]}
                />
            <View style={{flex: 1,}}>
              <LineChart
              data={data}
                style={{ flex: 1 }}
                svg={{ stroke: "rgb(134, 65, 244)" }}
                contentInset={{ top: 20, bottom: 20 }}
              />
              <XAxis
              data={xLabels}
                contentInset={{ left: 10, right: 10 }}
                svg={{
                  fill: "grey",
                  fontSize: 10,
                }}
                numberOfTicks={xLabels.length}
                formatLabel={(value, index) => xLabels[index]}
              />
            </View>
          </View>
        )
      }
      
const styles = StyleSheet.create({

});
// formatLabel={(value) => `${value}ºC`}

export default GraphComponent_1;

相关问题