React-native-chart-kit: Background Color Issue in LineChart

Created on 27 Jul 2019  路  12Comments  路  Source: indiespirit/react-native-chart-kit

I'm going to keep this brief and just include the important details.

I added this chart kit to my react native project and began to test it out. I needed a line chart so I naturally used the "LineChart." However, I ran into a few issues.

First

I can't seem to change the background color for the life of me. The gradient color works perfectly fine, but "backgroundColor" in the chartConfig, style, or chartConfig style, does not have any effect. If no gradient background is specified then the background is just black.

Second

Transparent backgrounds of any sort (I could only change the background through gradient) does not work, and also results in black background. I tried 'transparent' and 'rgba' yet neither of those work.

Component

import React from 'react';
import {
    Dimensions,
    StyleSheet,
    Text,
    View
} from 'react-native';

import { LineChart } from 'react-native-chart-kit';

const styles = StyleSheet.create({
    outerContainer: {
        elevation: 4,
        backgroundColor: '#fff',
        borderRadius: 10,
        marginTop: 25
    },
});
const chartConfig = {
    backgroundColor: '#e26a00',
    color: (opacity = 1) => `#2699FB`,
    strokeWidth: 2,
}

const data = {
    labels: ['January', 'February', 'March', 'April', 'May', 'June'],
    datasets: [{
        data: [20, 45, 28, 80, 99, 43],
        color: (opacity = 1) => `rgba(134, 65, 244, ${opacity})`, // optional
        strokeWidth: 2 // optional
    }]
}
export default class TideChart extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <View style={styles.outerContainer}>
                <View>
                    <LineChart
                        data={data}
                        height={220}
                        width={Dimensions.get('window').width - 20}
                        chartConfig={chartConfig}
                        style={{ marginBottom: 25, marginTop: 25 }}
                        bezier
                    />
                </View>
            </View>
        );
    }
}

Most of this is "right out of the box" but I tried a few things to try and fix. I wrapped it in a view and tried to get something to change.

I'm hoping this is just an error on my part.

Most helpful comment

backgroundGradientFromOpacity: 0,
backgroundGradientToOpacity: 0,

this works works me, i think if chart have FROM options that minds he have TO options))

All 12 comments

same issue + 1

what if you set backgroundGradientFrom and backgroundGradientTo to the same color? would that work the same way as backgroundColor? What if you set both of them to be transparent?

what if you set backgroundGradientFrom and backgroundGradientTo to the same color? would that work the same way as backgroundColor? What if you set both of them to be transparent?

I tried but it shows black background then :(

+1

same here

set your backgroundGradientFromOpacity: 0
the background will turn transparent

For me backgroundColor is not working instead backgroundGradientFrom works like backGroundColor function

for me after i upgrade the library to 5.4.0 the gradient disappeared and backgroundGradientFrom works like backgroundColor

The same issue here with a transparent background. 馃槧

backgroundGradientFromOpacity: 0,
backgroundGradientToOpacity: 0,

this works works me, i think if chart have FROM options that minds he have TO options))

backgroundGradientFromOpacity: 0,
backgroundGradientToOpacity: 0,

this works works me, i think if chart have FROM options that minds he have TO options))

Yes but at that time sometime label colors coming black and sometime white. even I gave labelColor as white. Please help me.

how to achieve black color for lines?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LillyBrainy picture LillyBrainy  路  4Comments

mcama picture mcama  路  5Comments

azui007 picture azui007  路  6Comments

Danjavia picture Danjavia  路  5Comments

dahudson88 picture dahudson88  路  5Comments