11import React from 'react' ;
22import { View , Text } from 'react-native' ;
33import PropTypes from 'prop-types' ;
4- import { getStyles , getColorPointIndicator } from './rules' ;
4+ import { getStyles } from './rules' ;
5+
6+ const Speedometer = ( props ) => {
7+ const { value, totalValue, style, innerCircleStyle, outerCircleStyle, halfCircleStyle, showText, text, textStyle, showLabels, labelStyle, showPercent, percentStyle } = props ;
58
6- const Speedometer = ( { value, totalValue, size, outerColor, innerColor, internalColor, style, innerCircleStyle, outerCircleStyle, halfCircleStyle, showText, text, textStyle, showLabels, labelStyle, showPercent, percentStyle, percentSize, showIndicator, indicatorColor } ) => {
7- const styles = getStyles ( size , percentSize ) ;
8- const degreesValue = ( value > totalValue ) ? totalValue : value ;
99 const percentValue = parseInt ( String ( ( value * 100 ) / totalValue ) . split ( '.' ) [ 0 ] ) ;
10+ const degreesValue = ( value > totalValue ) ? totalValue : value ;
1011 const degrees = ( ( degreesValue * 180 ) / ( ( totalValue === 0 ) ? 1 : totalValue ) ) - 90 ;
11- const degressStyle = {
12- backgroundColor : internalColor ,
13- transform : [ { translateX : size / 4 } , { rotate : `${ degrees } deg` } , { translateX : ( size / 4 * - 1 ) } ] ,
14- } ;
1512
16- const degressStyleIndicator = {
17- transform : [ { translateX : ( 2 + size ) / 4 } , { rotate : `${ ( ( 180 / 100 ) * degreesValue ) } deg` } , { translateX : ( ( 2 + size ) / 4 * - 1 ) } ] ,
18- } ;
13+ const styles = getStyles ( props , degrees ) ;
1914
2015 const percentElement = ( showPercent ) ? (
21- < Text style = { [ { backgroundColor : innerColor } , percentStyle ] } numberOfLines = { 1 } > { percentValue } %</ Text >
16+ < Text style = { [ styles . percentText , percentStyle ] } numberOfLines = { 1 } > { percentValue } %</ Text >
2217 ) : null ;
2318
2419 const textElement = ( ( showText ) && ( text ) ) ? (
25- < Text style = { [ { backgroundColor : innerColor } , textStyle ] } numberOfLines = { 1 } > { text } </ Text >
20+ < Text style = { [ styles . text , textStyle ] } numberOfLines = { 1 } > { text } </ Text >
2621 ) : null ;
2722
2823 const labelsElement = ( showLabels ) ? (
29- < View style = { [ styles . labelsView , { width : size } ] } >
24+ < View style = { styles . labelsView } >
3025 < Text style = { [ styles . initialLabel , labelStyle ] } numberOfLines = { 1 } > 0</ Text >
3126 < Text style = { [ styles . finalLabel , labelStyle ] } numberOfLines = { 1 } > { totalValue } </ Text >
3227 </ View >
3328 ) : null ;
3429
35- const indicadorElement = ( ( ! showText ) && ( ! showPercent ) && ( showIndicator ) && ( totalValue ) ) ? (
36- < View style = { [ degressStyleIndicator , styles . indicator , { width : 2 + size / 2 , backgroundColor : indicatorColor } ] } >
37- < View style = { [ styles . pointIndicator , { backgroundColor : getColorPointIndicator ( indicatorColor ) } ] } />
38- </ View >
39- ) : null ;
40-
4130 return (
4231 < View style = { style } >
43- < View style = { [ styles . outerCircle , { backgroundColor : outerColor } , outerCircleStyle ] } >
44- < View style = { [ styles . halfCircle , degressStyle , halfCircleStyle ] } />
45- < View style = { [ styles . innerCircle , { backgroundColor : innerColor } , innerCircleStyle ] } >
32+ < View style = { [ styles . outerCircle , outerCircleStyle ] } >
33+ < View style = { [ styles . halfCircle , halfCircleStyle ] } />
34+ < View style = { [ styles . innerCircle , innerCircleStyle ] } >
4635 { percentElement }
4736 { textElement }
4837 </ View >
4938 </ View >
50- { indicadorElement }
5139 { labelsElement }
5240 </ View >
5341 ) ;
@@ -60,23 +48,42 @@ Speedometer.propTypes = {
6048 outerColor : PropTypes . string ,
6149 innerColor : PropTypes . string ,
6250 internalColor : PropTypes . string ,
63- style : PropTypes . object ,
51+ style : PropTypes . oneOfType ( [
52+ PropTypes . object ,
53+ PropTypes . arrayOf ( PropTypes . object ) ,
54+ ] ) ,
6455 showText : PropTypes . bool ,
6556 text : PropTypes . oneOfType ( [
6657 PropTypes . string ,
6758 PropTypes . number ,
6859 ] ) ,
69- textStyle : PropTypes . object ,
60+ textStyle : PropTypes . oneOfType ( [
61+ PropTypes . object ,
62+ PropTypes . arrayOf ( PropTypes . object ) ,
63+ ] ) ,
7064 showLabels : PropTypes . bool ,
71- labelStyle : PropTypes . object ,
65+ labelStyle : PropTypes . oneOfType ( [
66+ PropTypes . object ,
67+ PropTypes . arrayOf ( PropTypes . object ) ,
68+ ] ) ,
7269 showPercent : PropTypes . bool ,
73- percentStyle : PropTypes . object ,
74- innerCircleStyle : PropTypes . object ,
75- outerCircleStyle : PropTypes . object ,
76- halfCircleStyle : PropTypes . object ,
70+ percentStyle : PropTypes . oneOfType ( [
71+ PropTypes . object ,
72+ PropTypes . arrayOf ( PropTypes . object ) ,
73+ ] ) ,
74+ innerCircleStyle : PropTypes . oneOfType ( [
75+ PropTypes . object ,
76+ PropTypes . arrayOf ( PropTypes . object ) ,
77+ ] ) ,
78+ outerCircleStyle : PropTypes . oneOfType ( [
79+ PropTypes . object ,
80+ PropTypes . arrayOf ( PropTypes . object ) ,
81+ ] ) ,
82+ halfCircleStyle : PropTypes . oneOfType ( [
83+ PropTypes . object ,
84+ PropTypes . arrayOf ( PropTypes . object ) ,
85+ ] ) ,
7786 percentSize : PropTypes . number ,
78- showIndicator : PropTypes . bool ,
79- indicatorColor : PropTypes . string ,
8087} ;
8188
8289Speedometer . defaultProps = {
@@ -92,9 +99,10 @@ Speedometer.defaultProps = {
9299 labelStyle : { } ,
93100 showPercent : false ,
94101 percentStyle : { } ,
102+ innerCircleStyle : { } ,
103+ outerCircleStyle : { } ,
104+ halfCircleStyle : { } ,
95105 percentSize : 0.5 ,
96- showIndicator : false ,
97- indicatorColor : 'grey'
98106} ;
99107
100108export default Speedometer ;
0 commit comments