3

I'm using a custom render on the dots to print the abbreviated value above the dot instead of using a y axis legend. However, the top-most value gets cut off by the upper boundary of the chart's container. I tried adding padding top to both the chart component itself, as well as the chart props. Adding paddingTop to the chart component props does add padding but it's only to the outer container. Adding any styles to the style prop in the chartConfig prop seems to have no effect.

const chartConfig: ChartConfig = {
  backgroundGradientFrom: colors.purpBgGradLeft,
  backgroundGradientTo: colors.purpBgGradRight,
  color: (opacity = 1) => `rgba(${colors.chartLineRGB}, ${opacity})`,
  labelColor: (opacity = 1) => `rgba(${colors.chartLineRGB}, ${opacity})`,
  propsForDots: {
    r: '5',
    strokeWidth: '1',
    stroke: `rgba(${colors.chartLineRGB}, 1)`,
  },
};
<View
  style={{ backgroundColor: colors.purpBgGradLeft }}
  onLayout={handleLayout}>
  <LineChart
    data={chartData}
    width={chartContainerWidth + CHART_WIDTH_OFFSET}
    height={CHART_HEIGHT}
    chartConfig={chartConfig}
    style={{ marginLeft: MARGIN_OFFSET }}
    renderDotContent={({ x, y, index, indexData }) => (
      <Svg key={index}>
        <SvgText
          x={x}
          y={y - DOT_TEXT_OFFSET}
          fill={colors.textLight}
          fontSize={DOT_TEXT_SIZE}
          textAnchor="middle">
          {formatStat(indexData, 0)}
        </SvgText>
      </Svg>
    )}
    withHorizontalLabels={false}
  />
</View>

enter image description here

Any help would be greatly appreciated! I created an Expo Snack for your convenience!

https://snack.expo.dev/@devlylabs/chart-example-with-dot-labels

Thanks in advance for any help!

1 Answer 1

1

If you're willing to forgo the gradient background color you can change into the following:

const chartConfig: ChartConfig = {
  backgroundGradientFrom: colors.purpBgGradLeft,
  backgroundGradientTo: colors.purpBgGradLeft,
  ...
}

Inside View

style={{ backgroundColor: colors.purpBgGradLeft}}

Inside LineChart

style={{ marginLeft: MARGIN_OFFSET, paddingTop: 30, paddingBottom: 20 }}
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up getting rid of the gradient background anyway because it wasn't stretching fully to the right side. This solution works great. Good thing the top figure is able to break out of the SVG canvas and not get cut off. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.