3

I'm trying to use this option, but it has no effect, and when I take a look at react-native-charts-wrapper code, I think maybe it is not correct. In file ChartBaseManager.java, the setCommonAxisConfig function is only called for xAxis, but never called for yAxis. I'm a newbie at react native and java, so I don't understand all the code. Maybe is this the reason for not working yOffset in y axis or I'm doing something wrong?

I'm using Line chart and my configuration is:

    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}
  />

But if I place "yOffset: -5" in xAxis configuration, the offset is applied to labels.

Anyone has the same problem using this option in yAxis?

  • What chart are you attempting to use these props with? Could you include your entire implementation of the chart please – Taylor Johnson Dec 10 '20 at 20:58
  • Sorry @TaylorJohnson, I updated the question with all the information – Nuno Magalhães Dec 11 '20 at 07:38
  • in [RNBarLineChartViewBase.swift](https://github.com/wuxudong/react-native-charts-wrapper/blob/f2c99f96220f6aa8b329218a7ec8b2c0abb560d9/ios/ReactNativeCharts/RNBarLineChartViewBase.swift#L24) you can see that `setCommonAxisConfig` is called for both the `left` and `right` YAxis. Does this work in iOS, but not android? – Taylor Johnson Dec 13 '20 at 21:03
  • Hmm, I also see it in the [BarLineChartBaseManager.java](https://github.com/wuxudong/react-native-charts-wrapper/blob/f2c99f96220f6aa8b329218a7ec8b2c0abb560d9/android/src/main/java/com/github/wuxudong/rncharts/charts/BarLineChartBaseManager.java#L56) – Taylor Johnson Dec 13 '20 at 21:06
  • Is it possible you're overwriting your state somewhere and removing the axis config objects? Could try exploring moving those configs out of state if they're meant to be static – Taylor Johnson Dec 13 '20 at 21:07
  • @TaylorJohnson, see my answer below – Nuno Magalhães Dec 14 '20 at 11:24
  • I'm currently using a [custom branch](https://github.com/Taylor123/react-native-charts-wrapper/tree/release/custom), and i'm not sure how much it has diverged from the package. That `yOffset` functionality works for me, but I have not looked at the diff between this and the package in a while and to use that you would just run `npm install` or `yarn install` `Taylor123/react-native-charts-wrapper#release/custom` – Taylor Johnson Dec 14 '20 at 20:01
  • @TaylorJohnson, I compared the files between your custom branch and react-native-charts-wrapper master and I belive that is missing 2 files and some code in RNBarLineChartViewBase.swift and BarLineChartBaseManager.java. I made the changes and tested and it works as expected, could you take a look to see if these changes can affect other functionalitys? I made the changes in this [branch](https://github.com/nmagalhaes/react-native-charts-wrapper/tree/y-Label-Fix) . If its ok, can I make a PR on react-native-charts-wrapper to correct this functionality, or would you rather be you? – Nuno Magalhães Dec 16 '20 at 18:20
  • Feel free to make the PR! Just happy if the code gets put to use :) – Taylor Johnson Dec 17 '20 at 16:42

1 Answers1

1

I tried moving configs out of state but get the same result, so I created a new app and used the code of LineChartGradientScreen.js of react-native-charts-wrapper examples and configured yAxis with options below.

No matter what value I put in yOffset, the labels always stay in same place

enter image description here

I want to put the labels above grid lines.

My example app code:

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;