react-native-slide-charts
uses react-native-svg
, d3
, and react-native-gesture-handler
to create highly customizable interactive charts that animate smoothly via Direct Manipulation
.
$ npm install @worker722/react-native-slide-charts --save
or
$ yarn add @worker722/react-native-slide-charts
All peerDependencies
are included with Expo so installation of peerDependencies
isn't required.
@worker722/react-native-slide-charts
depends on two peer dependencies with native modules that must be installed alongside it. Follow the installation instructions for both iOS and Android both packages.
Package | Minimum Version | Maximum Version |
---|---|---|
react-native-svg |
7.0.0 | 11.x |
react-native-gesture-handler |
1.1.0 | 1.x |
Make sure the version of the native module packages chosen works with the react-native
version of the project. Manually linking the projects may be required depending on the version and platform.
@worker722/react-native-slide-charts
exports two types of charts, SlideAreaChart
and SlideBarChart
along with the type definitions for the Charts, Props, and Enums.
import {
SlideAreaChart,
SlideBarChart,
SlideBarChartProps,
SlideAreaChartProps,
YAxisProps,
XAxisProps,
XAxisLabelAlignment,
YAxisLabelAlignment,
CursorProps,
ToolTipProps,
ToolTipTextRenderersInput,
GradientProps,
} from '@worker722/react-native-slide-charts'
To get started you can render an empty chart:
<SlideAreaChart />
But it will be more visually useful for configuring to use some test data.
<SlideBarChart
data={[
{ x: 0, y: 0 },
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 3 },
{ x: 4, y: 4 },
]}
/>
The Props, Types, Enums, and Defaults are defined below:
Prop | Type | Default | Note |
|
|
Determines if the indicator for the chart should be visible always or just when being touched. |
|
|
|
Animates the charts on mounting and between prop updates. |
|
|
|
Height of the area below the chart for the X-Axis markers and label to render in. |
|
|
|
Width of the area left of the chart for the Y-Axis markers and label to render in. |
|
(x: number
| Date)
=> void |
|
Callback function that provides the current Cursor position |
|
(y: number)
=> void |
|
Callback function that provides the current Cursor position |
|
|
|
Pushes the rendered height of the data within the chart down to make room for the |
|
Array<{
x: number
| Date,
y: number
}> |
|
Data that will be displayed on the chart. This must be an array of objects with |
|
|
|
Color to fill the bars of the bar chart or area of the area chart. Takes precedence over |
|
|
|
Height of the entire chart component. |
|
() => void |
|
If provided the chart will not be interactive and instead can be pressed. |
|
|
|
Bottom padding as it can not be applied via |
|
|
|
Left padding as it can not be applied via style to the chart component. |
|
|
|
Right padding as it can not be applied via style to the chart component. |
|
|
|
Top padding as it can not be applied via style to the chart component. |
|
(props:
GradientProps)
=> JSX.Element
| null |
Function that takes |
||
|
|
Ensure touch is passed to |
|
|
|
Terminates touch outside the chart component. |
|
(opacity: number)
=> void |
|
If |
|
|
{backgroundColor:
'#fff'} |
Style of chart component. |
|
|
|
On some slower Android devices there may be too many calls across the bridge that it can cause some lagging of the indicator movement, this limits the calls to the queue on Android with little noticeable change in the interaction. |
|
|
Props for rendering the |
||
|
Dimensions
.get('window')
.width |
Width of the entire chart. |
|
|
Props for rendering the |
||
[number, number]
| [Date, Date] |
Value of |
The range for the |
|
'time' | 'linear' |
|
Determines the applied |
|
|
Props for rendering the YAxis. |
||
[number, number] |
Max and Min value of |
The range for the |
Prop | Type | Default | Note |
|
|
Sets the bar color for the selected bar. Takes precedence over |
|
|
|
Sets the space that should be rendered between each bar on the bar chart. |
|
|
|
Sets the width of each bar, takes precedence over |
|
|
|
Prevents |
|
(props:
GradientProps)
=> JSX.Element
| null |
Function that takes |
||
(bar: number)
=> void |
|
This callback is fired when the bar selection has changed via touch. This can be used to fire off haptic feedback to the user, or for other side effects. If using expo |
Prop | Type | Default | Note |
{cursorMarkerHeight: 24,
cursorMarkerWidth: 24,
cursorWidth: 2} |
Props for |
||
shape.CurveFactory
| shape
.CurveFactoryLineOnly |
shape.curveMonotoneX |
Type of curve to use for the area chart from |
|
|
|
Color for the line designating the data charted. |
|
|
|
Stroke width of the line designating the data charted. |
Prop | Type | Default | Note |
|
|
Label for the axis. |
|
TSpanProps &
{ color: string } |
{fontSize: 13,
color: '#777'} |
Styling for axisLabel, extends |
|
|
|
Space above the label to offset it from either the top of the component (Y-Axis) or the bottom of the chart (X-Axis). |
Prop | Type | Default | Note |
|
|
If |
|
|
Position for the axisLabel below the chart. |
||
string[] |
|
Array of labels placed below the chart, these will line up with the bars if there are an adequate amount, or space evenly otherwise. |
|
|
|
If |
|
|
|
If |
|
|
|
The axis marker labels are top justified below the chart, this is the space between the marker and the chart. |
|
|
|
The first and last marker are always shown then the rest are laid out left to right; if the marker before the final one would layout closer than |
|
|
|
The right hand marker of the chart can be set to a special marker. |
|
|
|
The left hand marker of the chart can be set to a special marker. |
Prop | Type | Default | Note |
|
|
Color for average line. |
|
|
|
Number of decimal places to show in the average marker if |
|
TSpanProps &
{ color:
string } |
{
fontSize:
13,
color:
'#77777780'
} |
Styling for marker, extends |
|
|
Position for the axis label left of the chart. |
||
TSpanProps &
{ color:
string } |
{fontSize:
13,
color:
'#222'} |
Styling for markers, extends |
|
|
|
Extends bottom line of chart to left edge. |
|
|
|
Hides the markers on the Y-Axis without hiding the tick lines. |
|
|
|
Color for horizontal lines, overrides |
|
|
|
Stroke width for horizontal lines. |
|
|
|
Spacing for the Y-Axis ticks, this is determined using |
|
|
|
If there are ticks with labels and an |
|
|
|
Show marker next to the average of the data. |
|
|
|
Spacing between the markers and the chart to the left of the chart i.e. right of the markers. |
|
|
|
Show marker next to bottom line, often |
|
|
|
Number of ticks the chart should have, spaced via |
|
(props:
GradientProps
& { count:
number }) =>
JSX.Element
| null |
Function that takes |
||
(props:
GradientProps)
=> JSX.Element
| null |
Function that takes |
||
|
|
Rotates the Y-Axis label vertically. |
|
|
|
Shows the average line. |
|
|
|
Shows the base line of the chart. |
|
|
|
Color for vertical lines, overrides |
|
|
|
Stroke width of the vertial side lines. |
Prop | Type | Default | Note |
|
|
Border color for default cursor. |
|
|
|
Internal color for default cursor. |
|
|
|
Display line to bottom of chart below cursor. |
|
|
|
Height of the cursor, you need to adjust this is you use a custom cursor or want to change the default cursor size. |
|
|
|
Width of the cursor, you need to adjust this is you use a custom cursor or want to change the default cursor size. |
|
|
|
Width of the cursor line. |
|
|
|
Display the cursor. |
|
(props:
CursorProps & { ref:
React.RefObject<any>
}) =>
React.ReactNode | null |
|
Function that returns a custom cursor marker with ref assigned to outer most |
Prop | Type | Default | Note |
|
|
Background color of tool tip. |
|
|
|
Border radius of tool tip corners. |
|
|
|
Display tool tip if |
|
|
|
Display tool tip triangle on bottom of tool tip. |
|
|
|
Default font size of tool tip text. |
|
|
|
Height of the tool tip, you should set this if there are minimal changes to the height across the data as the measurements for setting this can cause jitteriness on Android at times. |
|
|
|
Set the tool tip triangle centered instead of moving to adjust for the position within the chart. |
|
|
Array of text styles that match up with toolTipTextRenderers array for styling. |
||
Array<(
toolTipTextRenderersInput:
ToolTipTextRenderersInput
) => { text: string }
> |
|
Array of functions that take |
|
|
|
Width of the tool tip, you should set this if there are minimal changes to the width across the data as the measurements for setting this can cause jitteriness on Android at times. |
Key | Type | Note |
ScaleTime<number, number> |
ScaleLinear<number, number> |
The scaleX function used to draw the chart, combined with |
|
ScaleLinear<number, number> |
The scaleY function used to draw the chart, combined with |
|
|
The |
|
|
Current |
|
|
Current |
Key | Type | Note |
|
The |
enum XAxisLabelAlignment {
right = 'right',
left = 'left',
center = 'center',
}
enum YAxisLabelAlignment {
top = 'top',
bottom = 'bottom',
middle = 'middle',
aboveTicks = 'aboveTicks',
}
const defaultAreaChartFillGradient = (props: GradientProps) => {
return (
<LinearGradient x1='50%' y1='0%' x2='50%' y2='100%' {...props}>
<Stop stopColor='#0081EB' offset='0%' stopOpacity='0.2' />
<Stop stopColor='#FFFFFF' offset='100%' stopOpacity='0.2' />
</LinearGradient>
)
}
const defaultBarChartFillGradient = (props: GradientProps) => {
return (
<LinearGradient x1='50%' y1='0%' x2='50%' y2='100%' {...props}>
<Stop stopColor='#0081EB' offset='0%' stopOpacity='0.4' />
<Stop stopColor='#0081EB' offset='100%' stopOpacity='0.4' />
</LinearGradient>
)
}
const defaultBarChartSelectedFillGradient = (props: GradientProps) => {
return (
<LinearGradient x1='50%' y1='0%' x2='50%' y2='100%' {...props}>
<Stop stopColor='#0081EB' offset='0%' stopOpacity='1' />
<Stop stopColor='#0081EB' offset='100%' stopOpacity='1' />
</LinearGradient>
)
}
export const verticalLineGradient = (props: GradientProps) => (
<Defs key={'verticalLineGradient'}>
<LinearGradient x1='50%' y1='0%' x2='50%' y2='100%' {...props}>
<Stop stopColor='#ffffff' offset='0%' stopOpacity='1' />
<Stop stopColor='#dadada' offset='30%' stopOpacity='1' />
<Stop stopColor='#dadada' offset='100%' stopOpacity='1' />
</LinearGradient>
</Defs>
)
export const horizontalLineGradient = (
props: GradientProps & { count: number }
) => (
<Defs key={props.id}>
<LinearGradient x1='0%' y1='0%' x2='100%' y2='0%' id={props.id}>
<Stop stopColor='#dadada' offset='0%' stopOpacity='1' />
<Stop stopColor='#dadada' offset='50%' stopOpacity='1' />
<Stop stopColor='#dadada' offset='100%' stopOpacity='1' />
</LinearGradient>
</Defs>
)