Top Related Projects
a react native charts wrapper (support android & iOS)
victory components for react native
πReact Native Chart Kit: Line Chart, Bezier Line Chart, Progress Ring, Bar chart, Pie chart, Contribution graph (heatmap)
[NOT MAINTAINED] :bar_chart: Add line, area, pie, and bar charts to your React Native app
π Beautiful, high-performance Graphs and Charts for React Native built with Skia
Quick Overview
The react-native-svg-charts
library is a powerful and flexible charting library for React Native applications. It provides a set of customizable and highly performant chart components that can be used to create a wide range of data visualizations, including line charts, bar charts, pie charts, and more.
Pros
- Extensive Chart Types: The library offers a wide range of chart types, including line charts, bar charts, pie charts, scatter plots, and more, allowing developers to create a variety of data visualizations.
- Customization: The library provides a high degree of customization, allowing developers to adjust the appearance and behavior of the charts to fit their specific needs.
- Performance: The library is built on top of the
react-native-svg
library, which provides a highly performant and scalable way to render SVG graphics in React Native applications. - Accessibility: The library includes support for accessibility features, making it easier to create charts that are accessible to users with disabilities.
Cons
- Learning Curve: The library has a relatively steep learning curve, especially for developers who are new to React Native and data visualization.
- Limited Documentation: The library's documentation could be more comprehensive, which may make it more difficult for new users to get started.
- Dependency on
react-native-svg
: The library is dependent on thereact-native-svg
library, which may add additional complexity to the setup process. - Limited Community Support: The library has a relatively small community compared to some other React Native libraries, which may make it more difficult to find help and resources.
Code Examples
Here are a few examples of how to use the react-native-svg-charts
library:
Line Chart:
import React from 'react';
import { LineChart } from 'react-native-svg-charts';
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80];
const LineChartExample = () => {
return (
<LineChart
style={{ height: 200 }}
data={data}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
/>
);
};
export default LineChartExample;
Bar Chart:
import React from 'react';
import { BarChart } from 'react-native-svg-charts';
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80];
const BarChartExample = () => {
return (
<BarChart
style={{ height: 200 }}
data={data}
svg={{ fill: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
/>
);
};
export default BarChartExample;
Pie Chart:
import React from 'react';
import { PieChart } from 'react-native-svg-charts';
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80];
const PieChartExample = () => {
return (
<PieChart
style={{ height: 200 }}
data={data}
spacing={0}
outerRadius={'95%'}
/>
);
};
export default PieChartExample;
Getting Started
To get started with the react-native-svg-charts
library, follow these steps:
- Install the library and its dependencies:
npm install react-native-svg-charts react-native-svg
- Import the desired chart components and use them in your React Native application:
import React from 'react';
import { LineChart } from 'react-native-svg-charts';
const data = [50, 10, 40,
Competitor Comparisons
a react native charts wrapper (support android & iOS)
Pros of react-native-charts-wrapper
- More chart types and customization options
- Based on popular MPAndroidChart and Charts libraries for native performance
- Extensive documentation and examples
Cons of react-native-charts-wrapper
- Larger package size due to native dependencies
- Steeper learning curve for complex customizations
- Less frequent updates and maintenance
Code Comparison
react-native-charts-wrapper:
<LineChart
data={{
dataSets: [{
values: [{y: 1}, {y: 2}, {y: 3}],
label: 'Dataset 1',
}],
}}
xAxis={{valueFormatter: ['Jan', 'Feb', 'Mar']}}
/>
react-native-svg-charts:
<LineChart
data={[1, 2, 3]}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
/>
react-native-svg-charts offers a simpler API for basic charts, while react-native-charts-wrapper provides more advanced features at the cost of complexity. The choice between them depends on the specific project requirements and the level of customization needed.
victory components for react native
Pros of victory-native
- More comprehensive and feature-rich charting library
- Better documentation and examples
- Consistent API across web and mobile platforms
Cons of victory-native
- Larger package size and potentially higher performance overhead
- Steeper learning curve due to more complex API
- Less focused on React Native-specific optimizations
Code Comparison
react-native-svg-charts:
import { LineChart } from 'react-native-svg-charts'
<LineChart
data={[50, 10, 40, 95, 85]}
svg={{ stroke: 'rgb(134, 65, 244)' }}
/>
victory-native:
import { VictoryLine } from 'victory-native'
<VictoryLine
data={[
{ x: 1, y: 50 },
{ x: 2, y: 10 },
{ x: 3, y: 40 },
{ x: 4, y: 95 },
{ x: 5, y: 85 }
]}
style={{ data: { stroke: 'rgb(134, 65, 244)' } }}
/>
victory-native offers a more flexible and powerful API, allowing for more customization and complex chart types. However, react-native-svg-charts provides a simpler and more straightforward approach for basic charting needs in React Native applications. The choice between the two libraries depends on the specific requirements of your project, such as the complexity of charts needed and the importance of cross-platform consistency.
πReact Native Chart Kit: Line Chart, Bezier Line Chart, Progress Ring, Bar chart, Pie chart, Contribution graph (heatmap)
Pros of react-native-chart-kit
- More chart types available (e.g., Progress Ring, Contribution Graph)
- Easier to customize with built-in styling options
- Better documentation with more examples and use cases
Cons of react-native-chart-kit
- Less flexible for advanced customizations
- Larger package size due to more built-in features
- Some users report performance issues with large datasets
Code Comparison
react-native-chart-kit:
import { LineChart } from "react-native-chart-kit";
<LineChart
data={{
labels: ["January", "February", "March", "April", "May", "June"],
datasets: [{ data: [20, 45, 28, 80, 99, 43] }]
}}
width={Dimensions.get("window").width}
height={220}
chartConfig={{
backgroundColor: "#e26a00",
backgroundGradientFrom: "#fb8c00",
backgroundGradientTo: "#ffa726",
decimalPlaces: 2,
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: { borderRadius: 16 }
}}
/>
react-native-svg-charts:
import { LineChart } from 'react-native-svg-charts'
<LineChart
style={{ height: 200 }}
data={[20, 45, 28, 80, 99, 43]}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
/>
[NOT MAINTAINED] :bar_chart: Add line, area, pie, and bar charts to your React Native app
Pros of react-native-chart
- Simpler API for basic chart types
- Includes built-in animations
- Smaller package size
Cons of react-native-chart
- Less customization options
- Limited chart types available
- Less active maintenance and updates
Code Comparison
react-native-chart:
<LineChart
data={{
labels: ["January", "February", "March", "April", "May", "June"],
datasets: [{
data: [20, 45, 28, 80, 99, 43]
}]
}}
width={Dimensions.get("window").width}
height={220}
/>
react-native-svg-charts:
<LineChart
style={{ height: 200 }}
data={[20, 45, 28, 80, 99, 43]}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
/>
react-native-svg-charts offers more flexibility in styling and data representation, while react-native-chart provides a simpler API for basic chart creation. The former allows for more advanced customization, while the latter is easier to set up for common chart types. react-native-svg-charts has more active development and a wider range of chart types, making it more suitable for complex data visualization needs.
π Beautiful, high-performance Graphs and Charts for React Native built with Skia
Pros of react-native-graph
- Built with performance in mind, using native components for rendering
- Supports smooth animations and interactions out of the box
- Offers a more modern and actively maintained codebase
Cons of react-native-graph
- Less customization options compared to react-native-svg-charts
- Smaller community and fewer examples available
- Limited chart types (primarily focused on line charts)
Code Comparison
react-native-graph:
import { LineGraph } from 'react-native-graph'
<LineGraph
points={[{ x: 1, y: 5 }, { x: 2, y: 8 }, { x: 3, y: 3 }]}
animated={true}
/>
react-native-svg-charts:
import { LineChart } from 'react-native-svg-charts'
<LineChart
data={[5, 8, 3]}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
/>
react-native-graph focuses on simplicity and performance, while react-native-svg-charts offers more flexibility in chart customization. The former uses a points array with x and y values, while the latter accepts a simple data array. react-native-graph includes animation by default, whereas react-native-svg-charts requires additional configuration for animations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-native-svg-charts
Welcome to react-native-svg-charts!
react-native-svg-charts
provides SVG Charts support to React Native on iOS and Android, and a compatibility layer for the web.
Looking for maintainers! I alone don't have the time to maintain this library anymore. Preferably looking for somebody who uses this library in their proffesional work (how I originally got the time to maintain).
version 5 is now available!
A much improved decorator system has been introduced, allowing for greater flexibility and less complexity. See releases for more information.
In order to not bloat this README too much we've moved some examples over to
react-native-svg-charts-examples
.
There we will try to showcase the really cool things you can do with this library.
This README will try to keep things as simple as possible so that everybody can get up and running as fast as possible.
Prerequisites
This library uses react-native-svg to render its graphs. Therefore this library needs to be installed AND linked into your project to work.
Other than the above dependency this library uses pure javascript and supports both iOS and Android
Getting Started
Make sure to follow the installation mentioned in the Prerequisites
Then you can install the library using either yarn:
yarn add react-native-svg-charts
or npm:
npm install --save react-native-svg-charts
Now you're good to go!
Motivation
Creating beautiful graphs in React Native shouldn't be hard or require a ton of knowledge. We use react-native-svg in order to render our SVG's and to provide you with great extensibility. We utilize the very popular d3 library to create our SVG paths and to calculate the coordinates.
We built this library to be as extensible as possible while still providing you with the most common charts and data visualization tools out of the box.
We're very proud of our "decorator" support. All charts can be extended with "decorators", a component that somehow styles or enhances your chart.
Simply pass in a react-native-svg
compliant component as a child to the graph and it will be called with all the necessary information to layout your decorator.
See each chart for information on what data the decorator will be called with.
Feedback and PR's are more than welcome Γ°ΒΒΒ
Running
If you want to test out the library you can clone this repository and run it. We suggest that you test out the storybooks that we've implemented. Most of the charts are implemented with knobs so that you can tweak most properties and see their behavior live.
Clone the repo and run the following:
yarn
# for iOS
(cd ios && pod install)
react-native run-ios
# for Android
react-native run-android
yarn storybook
# and then reload your device
Common Props
Property | Default | Description |
---|---|---|
data | required | An array of arbitrary data - use prop xAccessor /yAccessor to tell the chart about the data structure |
yAccessor | ({ item }) => item | A function that takes each entry of data (named "item") as well as the index and returns the y-value of that entry |
xAccessor | ({ index }) => index | Same as yAccessor but returns the x-value of that entry |
yScale | d3Scale.scaleLinear | A function that determines the scale of said axis (only tested with scaleLinear, scaleTime & scaleBand ) |
xScale | d3Scale.scaleLinear | Same as yScale but for the x axis |
svg | {} | an object containing all the props that should be passed down to the underlying react-native-svg component. See available props |
animate | false | PropTypes.bool |
animationDuration | 300 | PropTypes.number |
style | undefined | Supports all ViewStyleProps |
curve | d3.curveLinear | A function like this |
contentInset | { top: 0, left: 0, right: 0, bottom: 0 } | An object that specifies how much fake "margin" to use inside of the SVG canvas. This is particularly helpful on Android where overflow: "visible" isn't supported and might cause clipping. Note: important to have same contentInset on axis's and chart |
numberOfTicks | 10 | We use d3-array to evenly distribute the grid and dataPoints on the yAxis. This prop specifies how many "ticks" we should try to render. Note: important that this prop is the same on both the chart and on the yAxis |
showGrid | true | Whether or not to show the grid lines |
yMin | undefined | Alter how the chart bounds are calculated |
yMax | undefined | Alter how the chart bounds are calculated |
xMin | undefined | Alter how the chart bounds are calculated |
xMax | undefined | Alter how the chart bounds are calculated |
children | undefined | One or many react-native-svg components that will be used to enhance your chart |
Common arguments to children
Property | Description |
---|---|
x | a function that normally accepts the index of a data point an returns its 'x' location on the canvas |
y | a function that normally accepts the value of a data point an returns its 'y' location on the canvas |
width | the width of the canvas in pixels |
height | the height of the canvas in pixels |
data | the same data array provided to the chart, use this to map over your data points if you want decorators on each point |
ticks | if numberOfTicks has been provided to the chart this array will include the calculated tick values (useful for grids) |
Components
This library currently provides the following components
Also see
AreaChart
Example
import React from 'react'
import { AreaChart, Grid } from 'react-native-svg-charts'
import * as shape from 'd3-shape'
class AreaChartExample extends React.PureComponent {
render() {
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80]
return (
<AreaChart
style={{ height: 200 }}
data={data}
contentInset={{ top: 30, bottom: 30 }}
curve={shape.curveNatural}
svg={{ fill: 'rgba(134, 65, 244, 0.8)' }}
>
<Grid />
</AreaChart>
)
}
}
Props
See Common Props
Property | Default | Description |
---|---|---|
start | 0 | The value of which the area should start (will always end on the data point) |
Arguments to children
Supports all Common arguments to children
StackedAreaChart
Very similar to an area chart but with multiple sets of data stacked together.
We suggest that you read up on d3 stacks in order to better understand this chart and its props
See Area stack chart with Y axis to see how to use a YAxis with this component
Use the svgs
prop to pass in react-native-svg
compliant props to each area.
Example
import React from 'react'
import { StackedAreaChart } from 'react-native-svg-charts'
import * as shape from 'd3-shape'
class StackedAreaExample extends React.PureComponent {
render() {
const data = [
{
month: new Date(2015, 0, 1),
apples: 3840,
bananas: 1920,
cherries: 960,
dates: 400,
},
{
month: new Date(2015, 1, 1),
apples: 1600,
bananas: 1440,
cherries: 960,
dates: 400,
},
{
month: new Date(2015, 2, 1),
apples: 640,
bananas: 960,
cherries: 3640,
dates: 400,
},
{
month: new Date(2015, 3, 1),
apples: 3320,
bananas: 480,
cherries: 640,
dates: 400,
},
]
const colors = ['#8800cc', '#aa00ff', '#cc66ff', '#eeccff']
const keys = ['apples', 'bananas', 'cherries', 'dates']
const svgs = [
{ onPress: () => console.log('apples') },
{ onPress: () => console.log('bananas') },
{ onPress: () => console.log('cherries') },
{ onPress: () => console.log('dates') },
]
return (
<StackedAreaChart
style={{ height: 200, paddingVertical: 16 }}
data={data}
keys={keys}
colors={colors}
curve={shape.curveNatural}
showGrid={false}
svgs={svgs}
/>
)
}
}
Props
Property | Default | Description |
---|---|---|
data | required | An array of the data entries |
keys | required | This array should contain the object keys of interest (see above example) |
colors | required | An array of equal size as keys with the color for each key |
order | d3.stackOrderNone | The order in which to sort the areas |
offset | d3.stackOffsetNone | A function to determine the offset of the areas |
Also see Common Props
Arguments to children
Property | Description |
---|---|
x | a function that normally accepts the index of a data points an returns its 'x' location on the canvas |
y | a function that normally accepts the value of a data points an returns its 'y' location on the canvas |
width | the width of the canvas in pixels |
height | the height of the canvas in pixels |
~~the same data array provided to the chart, use this to map over your data points if you want decorators on each point~ | |
ticks | if numberOfTicks has been provided to the chart this array will include the calculated tick values (useful for grids) |
This chart does not call a child with the data
argument. This is due to the fact that a lot of calculations go into
creating the stacked chart, meaning that the original data
prop doesn't provide especially valuable information
when trying to layout decorators. It does however call with the rest of the common arguments
BarChart
Example
import React from 'react'
import { BarChart, Grid } from 'react-native-svg-charts'
class BarChartExample extends React.PureComponent {
render() {
const fill = 'rgb(134, 65, 244)'
const data = [50, 10, 40, 95, -4, -24, null, 85, undefined, 0, 35, 53, -53, 24, 50, -20, -80]
return (
<BarChart style={{ height: 200 }} data={data} svg={{ fill }} contentInset={{ top: 30, bottom: 30 }}>
<Grid />
</BarChart>
)
}
}
Props
Also see Common Props
Property | Default | Description |
---|---|---|
data | required | The data prop in a barChart can look exactly like in a Line- or AreaChart, i.e an array of just numbers or complex objects. It can however also be an array with several data sets. A data object can contain a svg property which allows you two override styles on that specific object. See the examples repo |
horizontal | false | Boolean whether or not the bars should be horizontal |
svg | {} | Default svg props for all bars. Supports all svg props an svg path normally supports. This styles will be overriden if there are specific styles for a given data object |
spacingInner | 0.05 | Spacing between the bars (or groups of bars) |
spacingOuter | 0.05 | Spacing outside of the bars (or groups of bars). Percentage of one bars width |
contentInset | { top: 0, left: 0, right: 0, bottom: 0 } | PropTypes.shape |
Arguments to children
Property | Description |
---|---|
bandwidth | the width of a band (a.k.a bar) |
Also supports all Common arguments to children
StackedBarChart
The same as the StackedAreaChart except with bars (and different svgs
prop).
We suggest that you read up on d3 stacks in order to better understand this chart and its props
The svgs
prop here is not based on keys, but rather entries, as the user might want to specify different props
for each entry in each bar. Therefore each key entry can contain a complex object that contains e.g an svg
prop. See this example for inspiration
Example
import React from 'react'
import { StackedBarChart } from 'react-native-svg-charts'
class StackedBarChartExample extends React.PureComponent {
render() {
const data = [
{
month: new Date(2015, 0, 1),
apples: 3840,
bananas: 1920,
cherries: 960,
dates: 400,
oranges: 400,
},
{
month: new Date(2015, 1, 1),
apples: 1600,
bananas: 1440,
cherries: 960,
dates: 400,
},
{
month: new Date(2015, 2, 1),
apples: 640,
bananas: 960,
cherries: 3640,
dates: 400,
},
{
month: new Date(2015, 3, 1),
apples: 3320,
bananas: 480,
cherries: 640,
dates: 400,
},
]
const colors = ['#7b4173', '#a55194', '#ce6dbd', '#de9ed6']
const keys = ['apples', 'bananas', 'cherries', 'dates']
return (
<StackedBarChart
style={{ height: 200 }}
keys={keys}
colors={colors}
data={data}
showGrid={false}
contentInset={{ top: 30, bottom: 30 }}
/>
)
}
}
Props
Property | Default | Description |
---|---|---|
data | required | An array of the data entries: each value can be a number or a complex object with custom svg props for example |
keys | required | This array should contain the object keys of interest (see above example) |
colors | required | An array of equal size as keys with the color for each key |
valueAccessor | ({ item, key }) => item[key] | Very similar to the yAccessor of the other charts, usually needed when using complex objects as values |
horizontal | false | Boolean whether or not the bars should be horizontal |
order | d3.stackOrderNone | The order in which to sort the areas |
offset | d3.stackOffsetNone | A function to determine the offset of the areas |
Also see Common Props
Arguments to children
Property | Description |
---|---|
x | a function that normally accepts the index of a data points an returns its 'x' location on the canvas |
y | a function that normally accepts the value of a data points an returns its 'y' location on the canvas |
width | the width of the canvas in pixels |
height | the height of the canvas in pixels |
ticks | if numberOfTicks has been provided to the chart this array will include the calculated tick values (useful for grids) |
This chart does not call a child with the data
argument. This is due to the fact that a lot of calculations go into
creating the stacked chart, meaning that the original data
prop doesn't provide especially valuable information
when trying to layout decorators. It does however call with the rest of the common arguments
LineChart
Example
import React from 'react'
import { LineChart, Grid } from 'react-native-svg-charts'
class LineChartExample extends React.PureComponent {
render() {
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80]
return (
<LineChart
style={{ height: 200 }}
data={data}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={{ top: 20, bottom: 20 }}
>
<Grid />
</LineChart>
)
}
}
Props
See Common Props
Arguments to children
Supports all Common arguments to children
PieChart
The PieChart is a really nice component with great support for custom behavior. See more examples in the examples repo
Example
import React from 'react'
import { PieChart } from 'react-native-svg-charts'
class PieChartExample extends React.PureComponent {
render() {
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80]
const randomColor = () => ('#' + ((Math.random() * 0xffffff) << 0).toString(16) + '000000').slice(0, 7)
const pieData = data
.filter((value) => value > 0)
.map((value, index) => ({
value,
svg: {
fill: randomColor(),
onPress: () => console.log('press', index),
},
key: `pie-${index}`,
}))
return <PieChart style={{ height: 200 }} data={pieData} />
}
}
Props
Property | Default | Description |
---|---|---|
data | required | Very similar to the data prop of our other charts, the only exception is that the PieChart only accepts complex objects (not just numbers). An item can also contain the arc property which allows you two override settings on that specific arc. See examples repo |
valueAccessor | ({ item }) => item.value | Very similar to the yAccessor of the other charts |
outerRadius | "100%" | The outer radius, use this to tweak how close your pie is to the edge of it's container. Takes either percentages or absolute numbers (pixels) |
innerRadius | "50%" | The inner radius, use this to create a donut. Takes either percentages or absolute numbers (pixels) |
labelRadius | undefined | The radius of the circle that will help you layout your labels. Takes either percentages or absolute numbers (pixels) |
padAngle | The angle between the slices | |
startAngle | 0 | The start angle in radians of the entire pie |
endAngle | Math.PI * 2 | The end angle in radians of the entire pie |
sort | (a,b) => b.value - a.value | Like any normal sort function it expects either 0, a positive or negative return value. The arguments are each an object from the dataPoints array |
Arguments to children
Property | Description |
---|---|
width | the width of the canvas in pixels |
height | the height of the canvas in pixels |
slices | an array of the pie chart slices. See source code and examples for what it includes |
data | the same data array provided to the chart, use this to map over your data points if you want decorators on each point |
ProgressCircle
Example
import React from 'react'
import { ProgressCircle } from 'react-native-svg-charts'
class ProgressCircleExample extends React.PureComponent {
render() {
return <ProgressCircle style={{ height: 200 }} progress={0.7} progressColor={'rgb(134, 65, 244)'} />
}
}
Props
Property | Default | Description |
---|---|---|
progress | required | PropTypes.number.isRequired |
progressColor | 'black' | PropTypes.any |
backgroundColor | '#ECECEC' | PropTypes.any |
startAngle | 0 | PropTypes.number |
endAngle | Math.PI * 2 | PropTypes.number |
strokeWidth | 5 | PropTypes.number |
cornerRadius | 45 | PropTypes.number |
Arguments to children
Property | Description |
---|---|
width | the width of the canvas in pixels |
height | the height of the canvas in pixels |
data | the same data array provided to the chart, use this to map over your data points if you want decorators on each point |
YAxis
A helper component to layout your Y-axis labels on the same coordinates as your chart.
It's very important that the component has the exact same view bounds (preferably wrapped in the same parent view) as the chart it's supposed to match.
If the chart has property contentInset
set it's very important that the YAxis has the same vertical contentInset.
Example
import React from 'react'
import { LineChart, YAxis, Grid } from 'react-native-svg-charts'
import { View } from 'react-native'
class YAxisExample extends React.PureComponent {
render() {
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80]
const contentInset = { top: 20, bottom: 20 }
return (
<View style={{ height: 200, flexDirection: 'row' }}>
<YAxis
data={data}
contentInset={contentInset}
svg={{
fill: 'grey',
fontSize: 10,
}}
numberOfTicks={10}
formatLabel={(value) => `${value}ΓΒΊC`}
/>
<LineChart
style={{ flex: 1, marginLeft: 16 }}
data={data}
svg={{ stroke: 'rgb(134, 65, 244)' }}
contentInset={contentInset}
>
<Grid />
</LineChart>
</View>
)
}
}
Props
(see Common Props)
Property | Default | Description |
---|---|---|
scale | d3Scale.scaleLinear | Should be the same as passed into the charts yScale , or d3Scale.scaleBand if used in conjunction with a horizontal BarChart |
svg | {} | supports all svg props an svg text normally supports |
spacingInner | 0.05 | Spacing between the labels. Only applicable if scale=d3Scale.scaleBand and should then be equal to spacingInner prop on the actual BarChart |
spacingOuter | 0.05 | Spacing outside of the labels. Only applicable if scale=d3Scale.scaleBand and should then be equal to spacingOuter prop on the actual BarChart |
formatLabel | value => {} | A utility function to format the text before it is displayed, e.g `value => "$" + value |
contentInset | { top: 0, bottom: 0 } | Used to sync layout with chart (if same prop used there) |
min | undefined | Used to sync layout with chart (if gridMin is used there) |
max | undefined | Used to sync layout with chart (if gridMax is used there) |
Arguments to children
No arguments
XAxis
A helper component to layout your X-axis labels on the same coordinates as your chart.
It's very important that the component has the exact same view bounds (preferably wrapped in the same parent view) as the chart it's supposed to match.
If the chart has property contentInset
set it's very important that the XAxis has the same horizontal contentInset.
The XAxis also supports the xAccessor
prop, if it's not supplied it will assume that you're only interested in the index of the data set.
Example
import React from 'react'
import { LineChart, XAxis, Grid } from 'react-native-svg-charts'
import { View } from 'react-native'
class XAxisExample extends React.PureComponent {
render() {
const data = [50, 10, 40, 95, -4, -24, 85, 91, 35, 53, -53, 24, 50, -20, -80]
return (
<View style={{ height: 200, padding: 20 }}>
<LineChart
style={{ flex: 1 }}
data={data}
gridMin={0}
contentInset={{ top: 10, bottom: 10 }}
svg={{ stroke: 'rgb(134, 65, 244)' }}
>
<Grid />
</LineChart>
<XAxis
style={{ marginHorizontal: -10 }}
data={data}
formatLabel={(value, index) => index}
contentInset={{ left: 10, right: 10 }}
svg={{ fontSize: 10, fill: 'black' }}
/>
</View>
)
}
}
Props
Property | Default | Description |
---|---|---|
data | required | An array of values or objects to render on the xAxis. Should preferably have the same length as the chart's dataPoints. If a complex object is used instead of a simple value, a xAccessor prop is required to calculate the axis' extent. A data object can contain a svg property which allows you to override styles on that specific object |
scale | d3Scale.scaleLinear | Should be the same as passed into the charts xScale |
spacingInner | 0.05 | Spacing between the labels. Only applicable if scale=d3Scale.scaleBand and should then be equal to spacingInner prop on the actual BarChart |
spacingOuter | 0.05 | Spacing between the labels. Only applicable if scale=d3Scale.scaleBand and should then be equal to spacingOuter prop on the actual BarChart |
svg | {} | Default svg props for all labels. Supports all svg props an svg text normally supports. This styles will be overriden if there are specific styles for a given data object |
formatLabel | value => value | A utility function to format the text before it is displayed, e.g value => "day" + value . Passes back the value provided by the xAccessor |
contentInset | { left: 0, right: 0 } | Used to sync layout with chart (if same prop used there) |
Arguments to children
No arguments
Children
New for version 5.0.
Each chart (and axes) component now accepts React children. Important note is that all children must be a react-native-svg
component
on order for it to be rendered by the chart. This API deprecates the old one with extras
and decorators
.
Everything that should be rendered above or below the chart should now be supplied as a child to said chart.
This allows you to declare the order in which your decorators should be rendered. If you want anything rendered below the chart,
simply add the prop belowChart={true}
. There's a ton of examples in the examples repo, go and have a look.
Grid
This library provides a helper component for drawing grids. Simply place it as child to the chart of your choice and (if necessary) set its direction.
Props
Property | Default | Description |
---|---|---|
svg | {} | an object containing all the props that should be passed down to the underlying react-native-svg component. See available props |
direction | Grid.Direction.HORIZONTAL | The direction of the grid lines. |
belowChart | true | whether or not to render below the chart |
Examples
There are tons of examples over at react-native-svg-chart-examples
License
Top Related Projects
a react native charts wrapper (support android & iOS)
victory components for react native
πReact Native Chart Kit: Line Chart, Bezier Line Chart, Progress Ring, Bar chart, Pie chart, Contribution graph (heatmap)
[NOT MAINTAINED] :bar_chart: Add line, area, pie, and bar charts to your React Native app
π Beautiful, high-performance Graphs and Charts for React Native built with Skia
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot