visit
Data will talk to people if you can present it elegantly.
Is there a react-native library with which you can-
react-native-gifted-charts caters to all the above needs.
npm install react-native-gifted-charts react-native-linear-gradient react-native-svg
barData = [{value: 15}, {value: 30}, {value: 26}, {value: 40}];
import { BarChart } from "react-native-gifted-charts";
const App = () => {
const barData = [{value: 15}, {value: 30}, {value: 26}, {value: 40}];
return <BarChart data={barData}/>;
};
Just add frontColor=‘#color-code’
to the <BarChart>
component.
Not just color, you can configure a whole list of properties, simply by using props like these-
The data for this chart is-
[500, 745, 320, 600, 256, 300]
This scares me because if I use the frontColor
prop, it changes the color of each bar. How do I change the color of some specific bars? 😱
Soon I figured out that I could pass the color along with the value in the data array. Also, there’s a label (weekday) under each bar, so I need to pass the label
too.
const data = [
{value: 250, label: 'M'},
{value: 500, label: 'T', frontColor: '#177AD5'},
{value: 745, label: 'W', frontColor: '#177AD5'},
{value: 320, label: 'T'},
{value: 600, label: 'F', frontColor: '#177AD5'},
{value: 256, label: 'S'},
{value: 300, label: 'S'}
];
<BarChart>
component.
One day I saw something like this-
[
[10, 20],
[10, 11, 15],
[14, 18],
[7, 11, 10]
]
<BarChart>
component is an array of objects.stacks
.stacks
key is an array of objects, each object representing a section of the stack.The actual code for the above chart is-
import { BarChart } from "react-native-gifted-charts";
const App = () => {
const stackData = [
{
stacks: [
{value: 10, color: 'orange'},
{value: 20, color: '#4ABFF4', marginBottom: 2},
],
label: 'Jan',
},
{
stacks: [
{value: 10, color: '#4ABFF4'},
{value: 11, color: 'orange', marginBottom: 2},
{value: 15, color: '#28B2B3', marginBottom: 2},
],
label: 'Mar',
},
{
stacks: [
{value: 14, color: 'orange'},
{value: 18, color: '#4ABFF4', marginBottom: 2},
],
label: 'Feb',
},
{
stacks: [
{value: 7, color: '#4ABFF4'},
{value: 11, color: 'orange', marginBottom: 2},
{value: 10, color: '#28B2B3', marginBottom: 2},
],
label: 'Mar',
},
];
return(
<View>
<BarChart
width={340}
rotateLabel
barWidth={12}
spacing={40}
noOfSections={4}
barBorderRadius={6}
stackData={stackData}
/>
</View>
);
};
Since the data is the same, we can use the same code, just replace the <BarChart>
component with <LineChart>
import { LineChart } from "react-native-gifted-charts";
const App = () => {
const data = [{value: 15}, {value: 30}, {value: 26}, {value: 40}];
return <LineChart data={data}/>;
};
Pro tip: You can convert any line chart into area chart by just passing the areaChart prop to the <LineChart/> component.
import { PieChart } from "react-native-gifted-charts";
const App = () => {
const data = [{value: 15}, {value: 30}, {value: 26}, {value: 40}];
return <PieChart data={data}/>;
};
Only the <BarChart>
component is replaced with <PieChart>
and it produces this-