Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-svg-charts

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-svg-charts

Customizable charts (Line, Bar, Area, Pie, Circle, Waterfall, Progress) for React Native

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
41K
decreased by-16.52%
Maintainers
1
Weekly downloads
 
Created
Source

react-native-svg-charts

npm version npm

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.

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. The Line-, Bar-, Area- and Waterfall -charts can all be extended with "accessories" and "extras". The renderDecorator prop is called on each passed dataPoint and allow you to simply add things such as points or other decorators to your charts. The extras and renderExtra prop is used to further decorate your charts with e.g intersections and projections, see the examples for more info.

Common Props

PropertyDefaultDescription
dataPointsrequiredAn array of integers - the data you want plotted, e.g [1,2,3,4]. This prop is different for PieChart and BarChart
strokeColor'black'color of the stroke
strokeWidth1width of the stroke
fillColor'none'color of the fill
dashArray[ 5, 5 ]see this but pass in as array
renderGradient() => {}function that renders the gradient. Example
animatetruePropTypes.bool
animationDuration300PropTypes.number
styleundefinedSupports all ViewStyleProps
curved3.curveCardinalA 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
numberOfTicks10We 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
showGridtrueWhether or not to show the grid lines
gridMinundefinedNormally the graph tries to draw from edge to edge within the view bounds. Using this prop will allow the grid to reach further than the actual dataPoints. Example
gridMaxundefinedThe same as "gridMin" but will instead increase the grids maximum value

Components

This library currently provides the following components

YAxis

Line chart

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

See example

Props

(see Common Props)

PropertyDefaultDescription
labelStyleundefinedSupports all TextStyleProps
formatLabelvalue => {}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)

XAxis

Line chart

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 YAxis has the same horizontal contentInset. The XAxis has a special property chartType that should match the type of the chart in order to layout the labels correctly

Example

See example

Props
PropertyDefaultDescription
valuesrequiredAn array of values to render on the xAxis. Should preferably have the same length as the chart's dataPoints.
chartTypeXAxis.Type.LINEShould state what chart type it is rendered next to. Important because of slightly different calculations. One of [ XAxis.Type.LINE, XAxis.Type.BAR ]
spacing0.05Only applicable if chartType=XAxis.Type.BAR and should then be equal to spacing prop on the actual BarChart.
labelStyleundefinedSupports all TextStyleProps
formatLabelvalue => {}A utility function to format the text before it is displayed, e.g `value => "day" + value
contentInset{ left: 0, right: 0 }Used to sync layout with chart (if same prop used there)

AreaChart

Area chart

Example
import { AreaChart } from 'react-native-svg-charts'

const chart = () => (
    <AreaChart
        style={{ height: 200 }}
        dataPoints={data.map(data => data.value)}
        showPoints={false}
        strokeColor={'white'}
        strokeWidth={2}
        contentInset={{ bottom: 10, left: 15, top: 10, right: 15 }}
    />
)
Props

See Common Props

BarChart

Bar chart

Example
import { BarChart } from 'react-native-svg-charts'

const foo = () => (
    <BarChart
        style={{ height: 200 }}
        dataPoints={[
            {
                ...colors,
                values: [5,3,2,3]
            },
            {
                ...colors,
                values: [-5,2,5,-3]
            }
        ]}
        spacing={0.3}
    />
)

Props

Also see Common Props

PropertyDefaultDescription
dataPointsrequiredSlightly different than other charts since we allow for grouping of bars. This array should contain at least one object with the following shape {fillColor: 'string', fillColorNegative: 'string', strokeColorPositive: 'string', strokeColorNegative: '', values: []}
spacing0.05Spacing between the bars (or groups of bars). Percentage of one bars width. Default = 5% of bar width
contentInset{ top: 0, left: 0, right: 0, bottom: 0 }PropTypes.shape

LineChart

Line chart

Example
import { LineChartfrom 'react-native-svg-charts'

const foo = () => (
    <LineChart
        style={{ height: 200 }}
        dataPoints={data}
        dashArray={[ 5, 5 ]}
        shadowColor={'rgba(34, 182, 176, 0.2)'}
        contentInset={{ bottom: 10, left: 15, right: 15, top: 10 }}
    />
)
Props

See Common Props

PieChart

Pie chart

Example
import { PieChart } from 'react-native-svg-charts'

const foo = () => (
    <PieChart
        style={{height: 200}
        dataPoints={[
            {
                key: 'foo',
                color: 'blue',
                value: 40,
            },
            {
                key: 'bar',
                color: 'green',
                value: 60,
            },
        ]}
        innerRadius={0.7}
        labelSpacing={40}
        renderLabel={item => (
            <TouchableOpacity onPress={() => console.log('clicked!', item)}>
                <Image source={Label}/>
            </TouchableOpacity>
        )}
    />
)
Props
PropertyDefaultDescription
dataPointsrequiredSlightly different because we allow for custom coloring of slices. The array should contain objects of the following shape: `{key: 'string
innerRadius0.5The inner radius, use this to create a donut
padAngleThe angle between the slices
renderLabel() => {}PropTypes.func
labelSpacing0PropTypes.number

ProgressGauge

Progress gauge

Example
importProgressCirclefrom 'react-native-svg-charts'

const foo = () => (
    <ProgressCircle
        style={{ height: 200 }}
        progress={0.4}
    />
)
Props
PropertyDefaultDescription
progressrequiredPropTypes.number.isRequired
progressColor'black'PropTypes.any
startAngle-Math.PI * 0.8PropTypes.number
endAngleMath.PI * 0.8PropTypes.number

WaterfallChart

Waterfall chart

Example
import { WaterfallChart } from 'react-native-svg-charts'

const foo = () => (
    <WaterfallChart
        style={{ height: 200 }}
        dataPoints={[ 0, -10, 50, 30, 60, 70, 30, 30}
        dashArray={[ 3, 5 ]}
    />
)

Props
PropertyDefaultDescription
dataPointsrequireddataPoints: PropTypes.arrayOf(PropTypes.shape({
fillColor: PropTypes.string,
strokeColor: PropTypes.string,
strokeColorNegative: PropTypes.string,
fillColorNegative: PropTypes.string,
values: PropTypes.arrayOf(PropTypes.number).isRequired,
})).isRequired,
spacing0.05PropTypes.number
strokeColor'black'PropTypes.string
strokeWidth1PropTypes.number
fillColor'none'PropTypes.string
renderGradient() => {}PropTypes.func (see this for more info)
animatetruePropTypes.bool
animationDuration300PropTypes.number
styleundefinedPropTypes.any
curvefooPropTypes.func
contentInset{ top: 0, left: 0, right: 0, bottom: 0 }PropTypes.shape
numberOfTicks9PropTypes.number
showGridtruePropTypes.bool
gridMinundefinedPropTypes.number
gridMaxundefinedPropTypes.number
intersections[ ]PropTypes.arrayOf(PropTypes.number)
renderIntersection() => {}PropTypes.func

Other Examples

Chart with axis

Gradient

Decorator

Extras

gridMin/Max

Keywords

FAQs

Package last updated on 31 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc