react-native-charts-wrapper
Version:
A react-native charts support both android and ios.
117 lines (102 loc) • 2.45 kB
JavaScript
import React from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View, processColor
} from 'react-native';
import {BarChart} from 'react-native-charts-wrapper';
class StackedBarChartScreen extends React.Component {
constructor() {
super();
this.state = {
legend: {
enabled: true,
textSize: 14,
form: "SQUARE",
formSize: 14,
xEntrySpace: 10,
yEntrySpace: 5,
wordWrapEnabled: true
},
data: {
dataSets: [{
values: [5, 40, 77, 81, 43],
label: 'Company A',
config: {
drawValues: false,
colors: [processColor('red')],
}
}, {
values: [40, 5, 50, 23, 79],
label: 'Company B',
config: {
drawValues: false,
colors: [processColor('blue')],
}
}, {
values: [10, 55, 35, 90, 82],
label: 'Company C',
config: {
drawValues: false,
colors: [processColor('green')],
}
}],
config: {
barWidth: 0.2,
group: {
fromX: 0,
groupSpace: 0.1,
barSpace: 0.1,
},
}
},
xAxis: {
valueFormatter: ['1990', '1991', '1992', '1993', '1994'],
granularityEnabled: true,
granularity: 1,
axisMaximum: 5,
axisMinimum: 0,
centerAxisLabels: true
},
};
}
handleSelect(event) {
let entry = event.nativeEvent
if (entry == null) {
this.setState({...this.state, selectedEntry: null})
} else {
this.setState({...this.state, selectedEntry: JSON.stringify(entry)})
}
}
render() {
return (
<View style={{flex: 1}}>
<View style={{height:80}}>
<Text> selected entry</Text>
<Text> {this.state.selectedEntry}</Text>
</View>
<View style={styles.container}>
<BarChart
style={styles.chart}
xAxis={this.state.xAxis}
data={this.state.data}
legend={this.state.legend}
drawValueAboveBar={false}
onSelect={this.handleSelect.bind(this)}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF'
},
chart: {
flex: 1
}
});
export default StackedBarChartScreen;