Skip to content

Commit 69ad28e

Browse files
Merge pull request #7 from WesleyFaveri/master
Add indicator(needle)
2 parents fa1e473 + 808829f commit 69ad28e

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ yarn add react-native-speedometer-chart
3434
| outerCircleStyle | null | object | no | |
3535
| halfCircleStyle | null | object | no | |
3636
| percentSize | 0.5 | number | no | |
37-
37+
| showIndicator | false | bool | no | Show a needle |
38+
| indicatorColor | #808080 | string | no | `value` color |
3839

3940
## Basic Usage
4041

src/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import { View, Text } from 'react-native';
33
import PropTypes from 'prop-types';
4-
import { getStyles } from './rules';
4+
import { getStyles, getColorPointIndicator } from './rules';
55

6-
const Speedometer = ({ value, totalValue, size, outerColor, innerColor, internalColor, style, innerCircleStyle, outerCircleStyle, halfCircleStyle, showText, text, textStyle, showLabels, labelStyle, showPercent, percentStyle, percentSize }) => {
6+
const Speedometer = ({ value, totalValue, size, outerColor, innerColor, internalColor, style, innerCircleStyle, outerCircleStyle, halfCircleStyle, showText, text, textStyle, showLabels, labelStyle, showPercent, percentStyle, percentSize, showIndicator, indicatorColor }) => {
77
const styles = getStyles(size, percentSize);
88
const degreesValue = (value > totalValue) ? totalValue : value;
99
const percentValue = parseInt(String((value * 100) / totalValue).split('.')[0]);
@@ -13,6 +13,10 @@ const Speedometer = ({ value, totalValue, size, outerColor, innerColor, internal
1313
transform: [{ translateX: size / 4 }, { rotate: `${degrees}deg` }, { translateX: (size / 4 * -1) }],
1414
};
1515

16+
const degressStyleIndicator = {
17+
transform: [{ translateX: (2 + size) / 4 }, { rotate: `${((180 / 100) * degreesValue)}deg` }, { translateX: ( (2 + size) / 4 * -1) }],
18+
};
19+
1620
const percentElement = (showPercent) ? (
1721
<Text style={[{ backgroundColor: innerColor }, percentStyle]} numberOfLines={1}>{percentValue}%</Text>
1822
) : null;
@@ -28,6 +32,12 @@ const Speedometer = ({ value, totalValue, size, outerColor, innerColor, internal
2832
</View>
2933
) : null;
3034

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+
3141
return (
3242
<View style={style}>
3343
<View style={[styles.outerCircle, { backgroundColor: outerColor }, outerCircleStyle]}>
@@ -37,6 +47,7 @@ const Speedometer = ({ value, totalValue, size, outerColor, innerColor, internal
3747
{textElement}
3848
</View>
3949
</View>
50+
{indicadorElement}
4051
{labelsElement}
4152
</View>
4253
);
@@ -64,6 +75,8 @@ Speedometer.propTypes = {
6475
outerCircleStyle: PropTypes.object,
6576
halfCircleStyle: PropTypes.object,
6677
percentSize: PropTypes.number,
78+
showIndicator: PropTypes.bool,
79+
indicatorColor: PropTypes.string,
6780
};
6881

6982
Speedometer.defaultProps = {
@@ -80,6 +93,8 @@ Speedometer.defaultProps = {
8093
showPercent: false,
8194
percentStyle: {},
8295
percentSize: 0.5,
96+
showIndicator: false,
97+
indicatorColor: 'grey'
8398
};
8499

85100
export default Speedometer;

src/rules.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import color from 'color';
2+
13
export const getStyles = (size, percent = 0.5) => ({
2-
outerCircle: {
4+
outerCircle: {
35
justifyContent: 'flex-end',
46
alignItems: 'center',
57
width: size,
@@ -39,4 +41,17 @@ export const getStyles = (size, percent = 0.5) => ({
3941
finalLabel: {
4042
flex: 0,
4143
},
44+
indicator: {
45+
height: 4,
46+
zIndex: 1000,
47+
justifyContent: 'center',
48+
},
49+
pointIndicator: {
50+
borderRadius: 50,
51+
width: 15,
52+
height: 15,
53+
alignSelf: 'flex-end',
54+
},
4255
});
56+
57+
export const getColorPointIndicator = (cor) => color(cor).darken(.2);

0 commit comments

Comments
 (0)