2

我正在尝试使用此选项,但它没有效果,当我查看 react-native-charts-wrapper 代码时,我认为它可能不正确。在文件 ChartBaseManager.java 中,setCommonAxisConfig 函数只为 xAxis 调用,而从未为 yAxis 调用。我是 react native 和 java 的新手,所以我不理解所有代码。也许这是在 y 轴上没有工作 yOffset 的原因,或者我做错了什么?

我正在使用折线图,我的配置是:

    this.state = {
      data: {
        dataSets: [
          {
            values: [getDefaultPoint()],
            label: "Teste",
            config: {
              drawCircleHole: false,
              drawCircles: false,
              drawValues: false,
              lineWidth: 2,
              barSpace: 0.2,
              shadowWidth: 0.5,
              shadowColor: processColor("white"),
              shadowColorSameAsCandle: true,
              neutralColor: processColor("white"),
              decreasingColor: processColor("red"),
              decreasingPaintStyle: "fill",
              increasingColor: processColor("green"),
              increasingPaintStyle: "fill",

              color: processColor("#50E3C2"),
              drawFilled: true,
              fillGradient: {
                colors: [processColor("#3F4969"), processColor("#313347")],
                positions: [0, 0.5],
                angle: 90,
                orientation: "LEFT_RIGHT"
              },
              fillAlpha: 200
            }
          }
        ]
      },
      legend: { enabled: false },
      marker: {
        digits: this.props.markerDigits,
        enabled: true,
        backgroundTint: processColor("blue"),
        markerColor: processColor("#F0C0FF8C"),
        textColor: processColor("white"),
        textSize: Math.round(14 * EStyleSheet.value("$scale"))
      },
      xAxis: {
        drawGridLines: false,
        drawAxisLine: true,
        drawLabels: true,
        position: "BOTTOM",
        textColor: processColor("white"),
        valueFormatter: "date",
        valueFormatterPattern: "HH:mm",
        avoidFirstLastClipping: true,
        textSize: 12 * EStyleSheet.value("$scale"),
        axisLineColor: processColor("#50E3C2"),
        gridColor: processColor(EStyleSheet.value("$chartGridLineColor")),
        granularity: 1,
        granularityEnabled: true,
        yOffset: 5
      },
      yAxis: {
        right: {
          enabled: false
        },
        left: {
          enabled: true,
          valueFormatter: this.props.yValueFormatterPattern,
          textColor: processColor("white"),
          drawGridLines: true,
          gridLineWidth: 1,
          drawAxisLine: false,
          drawLabels: true,
          labelCount: 4,
          labelCountForce: true,
          yOffset: -5,
          position: "INSIDE_CHART",
          textSize: 10 
        }
      },
      chart: {
        drawGridBackground: false,
        autoScaleMinMaxEnabled: true,
        touchEnabled: this.props.touchEnabled,
        dragEnabled: true,
        scaleEnabled: true,
        scaleXEnabled: true,
        scaleYEnabled: true,
        pinchZoom: true,
        doubleTapToZoomEnabled: true,
        dragDecelerationEnabled: true,
        dragDecelerationFrictionCoef: 0.99,
        keepPositionOnRotation: false,
        viewPortOffsets: {
          left: this.props.viewPortLeftOffset,
          top: this.props.viewPortTopOffset,
          right: this.props.viewPortRightOffset,
          bottom: this.props.viewPortBottomOffset
        }
      }
    };

...

    <LineChart
    style={styles.chart}
    data={this.state.data}
    chartDescription={{ text: "" }}
    legend={this.state.legend}
    marker={this.state.marker}
    xAxis={this.state.xAxis}
    yAxis={this.state.yAxis}
    {...this.state.chart}
  />

但是,如果我在 xAxis 配置中放置“yOffset:-5”,则偏移量将应用于标签。

在 yAxis 中使用此选项时有人遇到同样的问题吗?

4

1 回答 1

0

我尝试将配置移出状态但得到相同的结果,因此我创建了一个新应用程序并使用 react-native-charts-wrapper 示例的 LineChartGradientScreen.js 的代码并使用以下选项配置 yAxis。

无论我在 yOffset 中输入什么值,标签始终保持在同一个位置

在此处输入图像描述

我想将标签放在网格线上方。

我的示例应用程序代码:

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  processColor,
} from 'react-native';

import {LineChart} from 'react-native-charts-wrapper';

const greenBlue = 'rgb(26, 182, 151)';
const petrel = 'rgb(59, 145, 153)';

const App: () => React$Node = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <View style={styles.body}>
          <LineChart
            style={styles.chart}
            data={{
              dataSets: [
                {
                  values: [
                    {
                      y: 65,
                      x: 0,
                      marker: '65 kg',
                    },
                    {
                      y: 77,
                      x: 1,
                      marker: '77 kg',
                    },
                    {
                      y: 76,
                      x: 2,
                      marker: '76 kg',
                    },
                    {
                      y: 74,
                      x: 3,
                      marker: '74 kg',
                    },
                    {
                      y: 76,
                      x: 4,
                      marker: '76 kg',
                    },
                    {
                      y: 65,
                      x: 5,
                      marker: 'Today: 65 kg',
                    },
                  ],
                  label: '',
                  config: {
                    mode: 'CUBIC_BEZIER',
                    drawValues: false,
                    lineWidth: 2,
                    drawCircles: true,
                    circleColor: processColor(petrel),
                    drawCircleHole: false,
                    circleRadius: 5,
                    highlightColor: processColor('transparent'),
                    color: processColor(petrel),
                    drawFilled: true,
                    fillGradient: {
                      colors: [processColor(petrel), processColor(greenBlue)],
                      positions: [0, 0.5],
                      angle: 90,
                      orientation: 'TOP_BOTTOM',
                    },
                    fillAlpha: 1000,
                    valueTextSize: 15,
                  },
                },

                {
                  values: [
                    {
                      y: 35,
                      x: 0,
                      marker: '35 kg',
                    },
                    {
                      y: 47,
                      x: 1,
                      marker: '47 kg',
                    },
                    {
                      y: 46,
                      x: 2,
                      marker: '46 kg',
                    },
                    {
                      y: 44,
                      x: 3,
                      marker: '44 kg',
                    },
                    {
                      y: 46,
                      x: 4,
                      marker: '46 kg',
                    },
                    {
                      y: 35,
                      x: 5,
                      marker: 'Today: 35 kg',
                    },
                  ],
                  label: '',
                  config: {
                    mode: 'CUBIC_BEZIER',
                    drawValues: false,
                    lineWidth: 2,
                    drawCircles: true,
                    circleColor: processColor(petrel),
                    drawCircleHole: false,
                    circleRadius: 5,
                    highlightColor: processColor('transparent'),
                    color: processColor(petrel),
                    drawFilled: true,
                    fillGradient: {
                      colors: [processColor('red'), processColor('yellow')],
                      positions: [0, 0.5],
                      angle: 90,
                      orientation: 'TOP_BOTTOM',
                    },
                    fillAlpha: 1000,
                    valueTextSize: 15,
                  },
                },
              ],
            }}
            chartDescription={{text: ''}}
            legend={{
              enabled: false,
            }}
            marker={{
              enabled: true,
              markerColor: processColor('white'),
              textColor: processColor('black'),
            }}
            xAxis={{
              enabled: true,
              granularity: 1,
              drawLabels: true,
              position: 'BOTTOM',
              drawAxisLine: true,
              drawGridLines: false,
              fontFamily: 'HelveticaNeue-Medium',
              fontWeight: 'bold',
              textSize: 12,
              textColor: processColor('gray'),
              valueFormatter: ['M', 'T', 'W', 'T', 'F', 'S'],
            }}
            yAxis={{
              left: {
                enabled: true,
                textColor: processColor('white'),
                drawGridLines: true,
                gridLineWidth: 1,
                drawAxisLine: false,
                drawLabels: true,
                yOffset: -5,
                position: 'INSIDE_CHART',
                textSize: 10,
                gridColor: processColor('white'),
              },
              right: {
                enabled: false,
              },
            }}
            autoScaleMinMaxEnabled={true}
            animation={{
              durationX: 0,
              durationY: 1500,
              easingY: 'EaseInOutQuart',
            }}
            drawGridBackground={false}
            drawBorders={false}
            touchEnabled={true}
            dragEnabled={false}
            scaleEnabled={false}
            scaleXEnabled={false}
            scaleYEnabled={false}
            pinchZoom={false}
            doubleTapToZoomEnabled={false}
            dragDecelerationEnabled={true}
            dragDecelerationFrictionCoef={0.99}
            keepPositionOnRotation={false}
          />
        </View>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: 'black',
  },

  body: {
    backgroundColor: 'black',
  },
  chart: {
    height: 250,
  },
});

export default App;
于 2020-12-14T11:09:15.910 回答