Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: BarChart: Added custom rect #490

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions src/animated-path.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react'
import { InteractionManager } from 'react-native'
import PropTypes from 'prop-types'
import { Path } from 'react-native-svg'
import { Path, Rect } from 'react-native-svg'
import * as interpolate from 'd3-interpolate-path'

class AnimatedPath extends Component {
Expand Down Expand Up @@ -80,26 +80,70 @@ class AnimatedPath extends Component {
}

render() {
const { radiusX, radiusY, showPlaceholderView, heightPlaceholderView, showCustomRect } = this.props
if (showCustomRect) {

var commands = this.state.d.split(/(?=[LMC])/);

var pointArrays = commands.map(function (d) {
var pointsArray = d.slice(1, d.length).split(',');
var pairsArray = [];
for (var i = 0; i < pointsArray.length; i += 2) {
pairsArray.push([+pointsArray[i], +pointsArray[i + 1]]);
}
return pairsArray;
});

const width = Math.abs(pointArrays[1][0][0] - pointArrays[0][0][0]);
const height = Math.abs(pointArrays[2][0][1] - pointArrays[1][0][1]);
const x = Math.abs(pointArrays[0][0][0]);
const y = Math.abs(pointArrays[0][0][1]);

return (
<Path
<>
{showPlaceholderView && <Rect
x={x} rx={radiusX} ry={radiusY} y={0} width={width} height={heightPlaceholderView}
fill='#f4f4f4'
/>}
<Rect
x={x} rx={radiusX} ry={radiusY} y={y} width={width} height={height}
ref={(ref) => (this.component = ref)}
{...this.props}
d={this.props.animate ? this.state.d : this.props.d}
/>
</>
)
} else {
return (
<Path
ref={(ref) => (this.component = ref)}
{...this.props}
d={this.props.animate ? this.state.d : this.props.d}
/>
)
}
}
}

AnimatedPath.propTypes = {
animate: PropTypes.bool,
animationDuration: PropTypes.number,
renderPlaceholder: PropTypes.func,
radiusX: PropTypes.number,
radiusY: PropTypes.number,
showPlaceholderView: PropTypes.bool,
heightPlaceholderView: PropTypes.number,
showCustomRect: PropTypes.bool,
...Path.propTypes,
}

AnimatedPath.defaultProps = {
animate: false,
animationDuration: 300,
radiusX: 10,
radiusY: 10,
showPlaceholderView: true,
showCustomRect: true,
heightPlaceholderView: 220,
renderPlaceholder: () => null,
}

Expand Down
13 changes: 11 additions & 2 deletions src/bar-chart/bar-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class BarChart extends PureComponent {
}

render() {
const { data, animate, animationDuration, style, numberOfTicks, svg, horizontal, children } = this.props
const { data, animate, animationDuration, style, numberOfTicks, svg, horizontal, children, radiusX, radiusY, heightPlaceholderView, showPlaceholderView, showCustomRect } = this.props

const { height, width } = this.state

Expand Down Expand Up @@ -173,6 +173,11 @@ class BarChart extends PureComponent {

return (
<Path
radiusX={radiusX}
radiusY={radiusY}
showPlaceholderView={showPlaceholderView}
heightPlaceholderView={heightPlaceholderView}
showCustomRect={showCustomRect}
key={index}
{...svg}
{...barSvg}
Expand Down Expand Up @@ -212,10 +217,14 @@ BarChart.propTypes = {
gridMin: PropTypes.number,
gridMax: PropTypes.number,
svg: PropTypes.object,

yMin: PropTypes.any,
yMax: PropTypes.any,
clamp: PropTypes.bool,
radiusX: PropTypes.number,
radiusY: PropTypes.number,
showPlaceholderView: PropTypes.bool,
heightPlaceholderView: PropTypes.number,
showCustomRect: PropTypes.bool,
}

BarChart.defaultProps = {
Expand Down