이윤영

Modify rendering

1 import React, { Component } from 'react'; 1 import React, { Component } from 'react';
2 import { View, Text, StyleSheet,ScrollView, RefreshControl } from 'react-native'; 2 import { View, Text, StyleSheet,ScrollView, RefreshControl } from 'react-native';
3 -import { BarChart, Grid } from 'react-native-svg-charts'
4 import HistoryGraph from '../component/HistoryGraph' 3 import HistoryGraph from '../component/HistoryGraph'
5 import SegmentedControlTab from "react-native-segmented-control-tab"; 4 import SegmentedControlTab from "react-native-segmented-control-tab";
6 -import {add_data, get_week_data, get_today_data, get_month_data, get_year_data} from'../component/DBhelper.js'; 5 +import {add_data, get_week_data, get_month_data, get_year_data, add_temp_data} from'../component/DBhelper.js';
7 -var list1 = get_week_data(); 6 +
8 -var list2 = get_month_data();
9 -var list3 = get_year_data();
10 //var today = new Date(); 7 //var today = new Date();
11 //today.setHours(today.getHours()+9); 8 //today.setHours(today.getHours()+9);
12 -var week = ['SUN','MON','TUE', 'WED', 'THU', 'FRI', 'SAT'];
13 -
14 -const data1 = [
15 - { label: week[list1[1][0]], value: list1[0][0] },
16 - { label: week[list1[1][1]], value: list1[0][1] },
17 - { label: week[list1[1][2]], value: list1[0][2] },
18 - { label: week[list1[1][3]], value: list1[0][3] },
19 - { label: week[list1[1][4]], value: list1[0][4] },
20 - { label: week[list1[1][5]], value: list1[0][5] },
21 - { label: week[list1[1][6]], value: list1[0][6] }
22 -]
23 -const data2 = [
24 - { label: '1', value: list2[0] },
25 - { label: '7', value: list2[1] },
26 - { label: '14', value: list2[2]},
27 - { label: '21', value: list2[3] },
28 - { label: '28', value: list2[4] }
29 -]
30 -const data3 = [
31 - { label: 'Jan', value: list3[0] },
32 - { label: 'Feb', value: list3[1] },
33 - { label: 'Mar', value: list3[2] },
34 - { label: 'Apr', value: list3[3] },
35 - { label: 'May', value: list3[4] },
36 - { label: 'Jun', value: list3[5] },
37 - { label: 'Jul', value: list3[6] },
38 - { label: 'Aug', value: list3[7] },
39 - { label: 'Sep', value: list3[8] },
40 - { label: 'Oct', value: list3[9] },
41 - { label: 'Nov', value: list3[10] },
42 - { label: 'Dec', value: list3[11] }
43 -]
44 -
45 -const data4 = [
46 - { label: 'C', value: 500 },
47 - { label: 'H', value: 312 },
48 - { label: 'A', value: 424 },
49 - { label: 'N', value: 745 },
50 - { label: 'G', value: 89 },
51 - { label: 'E', value: 434 }
52 -]
53 9
54 function total(list){ 10 function total(list){
55 - var total=0; 11 + sum = 0;
56 for(var i =0; i< list.length; i++) 12 for(var i =0; i< list.length; i++)
57 - total += list[i] 13 + sum += list[i].value;
58 - return total 14 + return sum
59 } 15 }
60 16
61 -const kg1 = "총 " + total(list1[0]) + "kg"
62 -const kg2 = "총 " + total(list2) + "kg"
63 -const kg3 = "총 " + total(list3) + "kg"
64 -
65 export default class HomeTab extends Component { 17 export default class HomeTab extends Component {
66 constructor(){ 18 constructor(){
67 super(); 19 super();
68 this.state = { 20 this.state = {
69 selectedIndex: 0, 21 selectedIndex: 0,
70 - data: data1, 22 + data: get_week_data(),
71 - title: "총 " + total(list1[0]) + "kg", 23 + title: "총 " + total(get_week_data())/1000 + "kg",
72 spane: "", 24 spane: "",
73 refreshing: false 25 refreshing: false
74 } 26 }
...@@ -76,22 +28,33 @@ export default class HomeTab extends Component { ...@@ -76,22 +28,33 @@ export default class HomeTab extends Component {
76 28
77 _onRefresh = () => { 29 _onRefresh = () => {
78 this.setState({refresing: true}); 30 this.setState({refresing: true});
79 - // this.setState({data: data4})
80 this.get_diff_data(); 31 this.get_diff_data();
81 this.setState({refreshing: false}); 32 this.setState({refreshing: false});
82 } 33 }
83 34
35 + componentDidMount() {
36 + const { navigation } = this.props;
37 + this.focusListener = navigation.addListener('didFocus', () => {
38 + this.handleIndexChange(this.state.selectedIndex);
39 + });
40 + }
41 +
42 + componentWillUnmount() {
43 + // Remove the event listener
44 + this.focusListener.remove();
45 + }
46 +
84 handleIndexChange = index => { 47 handleIndexChange = index => {
85 this.setState({selectedIndex: index}); 48 this.setState({selectedIndex: index});
86 switch(index){ 49 switch(index){
87 case 0: 50 case 0:
88 - this.setState({data: data1, title: kg1}); 51 + this.setState({data: get_week_data(), title: "총 " + total(get_week_data())/1000 + "kg"});
89 break; 52 break;
90 case 1: 53 case 1:
91 - this.setState({data: data2, title: kg2}); 54 + this.setState({data: get_month_data(), title: "총 " + total(get_month_data())/1000 + "kg"});
92 break; 55 break;
93 case 2: 56 case 2:
94 - this.setState({data: data3, title: kg3}); 57 + this.setState({data: get_year_data(), title: "총 " + total(get_year_data())/1000 + "kg"});
95 break; 58 break;
96 } 59 }
97 }; 60 };
...@@ -111,24 +74,17 @@ export default class HomeTab extends Component { ...@@ -111,24 +74,17 @@ export default class HomeTab extends Component {
111 .then((response) => response.json()) 74 .then((response) => response.json())
112 .then((responseJson)=> { 75 .then((responseJson)=> {
113 console.log(responseJson) 76 console.log(responseJson)
114 - add_data(responseJson) 77 + //add_data(responseJson)
78 + add_temp_data();
115 },function(){ 79 },function(){
116 }) 80 })
81 + .then(()=>{this.handleIndexChange(this.state.selectedIndex)})
117 .catch((error) => { 82 .catch((error) => {
118 console.error(error); 83 console.error(error);
119 }) 84 })
120 } 85 }
121 86
122 render() { 87 render() {
123 - // const fill = 'rgb(134, 65, 244)'
124 - // const data = [50, 10, 40, 95, -4, -24, null, 85, undefined, 0, 35, 53, -53, 24, 50, -20, -80]
125 -
126 - // return (
127 - // <BarChart style={{ height: 200 }} data={data} svg={{ fill }} contentInset={{ top: 30, bottom: 30 }}>
128 - // <Grid />
129 - // </BarChart>
130 - // );
131 -
132 return( 88 return(
133 <ScrollView style = {style.container} 89 <ScrollView style = {style.container}
134 refreshControl={ 90 refreshControl={
...@@ -147,15 +103,18 @@ export default class HomeTab extends Component { ...@@ -147,15 +103,18 @@ export default class HomeTab extends Component {
147 <View style={style.header}> 103 <View style={style.header}>
148 <Text style={style.tabName}>History </Text> 104 <Text style={style.tabName}>History </Text>
149 </View> 105 </View>
150 - <View style={{flex : 8, justifyContent:'center'}}> 106 +
151 - <Text style ={style.date_text}>{this.state.title}</Text> 107 + <View style={{justifyContent:'center', padding: 10}}>
152 - </View> 108 + <SegmentedControlTab
153 - <SegmentedControlTab 109 + values={["Week", "Month", "Year"]}
154 - values={["Week", "Month", "Year"]} 110 + selectedIndex={this.state.selectedIndex}
155 - selectedIndex={this.state.selectedIndex} 111 + onTabPress={this.handleIndexChange}
156 - onTabPress={this.handleIndexChange} 112 + />
157 - /> 113 + </View>
158 - <HistoryGraph data={this.state.data} round={100} unit="kg"/> 114 + <View style={{flex : 8, justifyContent:'center', padding: 10}}>
115 + <Text style ={style.date_text}>{this.state.title}</Text>
116 + </View>
117 + <HistoryGraph data={this.state.data} round={1000} unit="g"/>
159 </View> 118 </View>
160 </ScrollView> 119 </ScrollView>
161 ) 120 )
......
...@@ -2,7 +2,7 @@ import React, { Component } from 'react'; ...@@ -2,7 +2,7 @@ import React, { Component } from 'react';
2 import { View, Text, StyleSheet, SafeAreaView, ScrollView, Image } from 'react-native'; 2 import { View, Text, StyleSheet, SafeAreaView, ScrollView, Image } from 'react-native';
3 import Circle from '../component/circle_component' 3 import Circle from '../component/circle_component'
4 import {Table, TableWrapper, Row,Rows} from 'react-native-table-component' 4 import {Table, TableWrapper, Row,Rows} from 'react-native-table-component'
5 -import {get_now_weight, get_today_data, add_data} from'../component/DBhelper.js'; 5 +import {get_now_weight, get_today_data, add_data,add_temp_data} from'../component/DBhelper.js';
6 6
7 var date = new Date(); 7 var date = new Date();
8 date.setHours(date.getHours()+9); 8 date.setHours(date.getHours()+9);
...@@ -21,6 +21,21 @@ export default class HomeTab extends Component { ...@@ -21,6 +21,21 @@ export default class HomeTab extends Component {
21 table_contents : get_today_data() 21 table_contents : get_today_data()
22 } 22 }
23 } 23 }
24 + componentDidMount() {
25 + const { navigation } = this.props;
26 + this.focusListener = navigation.addListener('didFocus', () => {
27 + this.setState({
28 + current_weight : get_now_weight(),
29 + table_contents : get_today_data()
30 + });
31 + });
32 + }
33 +
34 + componentWillUnmount() {
35 + // Remove the event listener
36 + this.focusListener.remove();
37 + }
38 +
24 get_diff_data(){ 39 get_diff_data(){
25 return fetch('http://testloadbalancer-1847561458.ap-northeast-2.elb.amazonaws.com/app/get_diff_data/',{ 40 return fetch('http://testloadbalancer-1847561458.ap-northeast-2.elb.amazonaws.com/app/get_diff_data/',{
26 method:'POST', 41 method:'POST',
...@@ -30,19 +45,25 @@ export default class HomeTab extends Component { ...@@ -30,19 +45,25 @@ export default class HomeTab extends Component {
30 }, 45 },
31 body:JSON.stringify({ 46 body:JSON.stringify({
32 ras_id : "pi1", 47 ras_id : "pi1",
33 - date : "1576339901541", 48 + date : "1576648647000",
34 }), 49 }),
35 }) 50 })
36 .then((response) => response.json()) 51 .then((response) => response.json())
37 .then((responseJson)=> { 52 .then((responseJson)=> {
38 console.log(responseJson) 53 console.log(responseJson)
39 - add_data(responseJson); 54 + //add_data(responseJson);
55 + add_temp_data();
40 },function(){ 56 },function(){
41 }) 57 })
58 + .then(()=>{this.setState({
59 + current_weight : get_now_weight(),
60 + table_contents : get_today_data()
61 + })})
42 .catch((error) => { 62 .catch((error) => {
43 console.error(error); 63 console.error(error);
44 }) 64 })
45 } 65 }
66 +
46 render() { 67 render() {
47 return ( 68 return (
48 <View style={style.container}> 69 <View style={style.container}>
......
...@@ -40,8 +40,8 @@ export default class SettingTab extends Component { ...@@ -40,8 +40,8 @@ export default class SettingTab extends Component {
40 delete_id(){ 40 delete_id(){
41 this.setState({ID: ''}) 41 this.setState({ID: ''})
42 //save_app_data() 42 //save_app_data()
43 - //delete_all_data() 43 + delete_all_data()
44 - deleteId() 44 + //deleteId()
45 this.props.navigation.navigate('Login'); 45 this.props.navigation.navigate('Login');
46 } 46 }
47 render() { 47 render() {
......
...@@ -58,8 +58,8 @@ export default class Login extends Component { ...@@ -58,8 +58,8 @@ export default class Login extends Component {
58 /> 58 />
59 <LoginButton 59 <LoginButton
60 //onPress={()=>{this.getdata}} 60 //onPress={()=>{this.getdata}}
61 - onPress={() =>{ this.props.navigation.navigate('MainScreen', { ID: this.state.ID });}} 61 + //onPress={() =>{ this.props.navigation.navigate('MainScreen', { ID: this.state.ID });}}
62 - //onPress={() =>{ this.getdata().then(()=>{this.props.navigation.navigate('MainScreen', { ID: this.state.ID })}) }} 62 + onPress={() =>{ this.getdata().then(()=>{this.props.navigation.navigate('MainScreen', { ID: this.state.ID })}) }}
63 /> 63 />
64 </View> 64 </View>
65 </View> 65 </View>
......
...@@ -33,7 +33,7 @@ const AppTabNavigator = createMaterialTopTabNavigator({ ...@@ -33,7 +33,7 @@ const AppTabNavigator = createMaterialTopTabNavigator({
33 } 33 }
34 }); 34 });
35 35
36 -const AppTabContainet = createAppContainer(AppTabNavigator); 36 +const AppTabContainer = createAppContainer(AppTabNavigator);
37 37
38 export default class MainScreen extends Component { 38 export default class MainScreen extends Component {
39 39
...@@ -44,11 +44,12 @@ export default class MainScreen extends Component { ...@@ -44,11 +44,12 @@ export default class MainScreen extends Component {
44 }; 44 };
45 } 45 }
46 static navigationOptions = { 46 static navigationOptions = {
47 - title: 'FW IOT' } 47 + title: 'FW IOT'
48 + }
48 49
49 render() { 50 render() {
50 registerID(this.state.ID); 51 registerID(this.state.ID);
51 - return <AppTabContainet/>; // AppTabContainet 컴포넌트를 리턴한다. 52 + return <AppTabContainer/>; // AppTabContainet 컴포넌트를 리턴한다.
52 } 53 }
53 } 54 }
54 55
......
This diff is collapsed. Click to expand it.
...@@ -3,12 +3,11 @@ import { View, Text, StyleSheet } from 'react-native'; ...@@ -3,12 +3,11 @@ import { View, Text, StyleSheet } from 'react-native';
3 import { Svg, G, Line, Rect } from 'react-native-svg' 3 import { Svg, G, Line, Rect } from 'react-native-svg'
4 import { Text as SvgText } from 'react-native-svg' 4 import { Text as SvgText } from 'react-native-svg'
5 import * as d3 from 'd3' 5 import * as d3 from 'd3'
6 -// import Showkg from './ShowKg'
7 6
8 const GRAPH_MARGIN = 20 7 const GRAPH_MARGIN = 20
9 const GRAPH_BAR_WIDTH = 10 8 const GRAPH_BAR_WIDTH = 10
10 const colors = { 9 const colors = {
11 - axis: '#E4E4E4', 10 + axis: '#CED4DA',
12 bars: '#0066cc', 11 bars: '#0066cc',
13 bardefult: '#CED4DA' 12 bardefult: '#CED4DA'
14 } 13 }
...@@ -19,23 +18,26 @@ export default class HistoryGraph extends PureComponent { ...@@ -19,23 +18,26 @@ export default class HistoryGraph extends PureComponent {
19 super(props); 18 super(props);
20 this.handleClick = this.handleClick.bind(this); 19 this.handleClick = this.handleClick.bind(this);
21 this.state = { 20 this.state = {
22 - kg: this.props.data[this.props.data.length - 1].label, 21 + kg: this.props.data[this.props.data.length - 1].value,
22 + span: this.props.data[this.props.data.length -1].label,
23 index: this.props.data.length-1 23 index: this.props.data.length-1
24 } 24 }
25 } 25 }
26 26
27 - handleClick = inkg => { 27 + handleClick = item => {
28 - this.setState({kg: inkg}); 28 + this.setState({span: item.label, kg: item.value});
29 } 29 }
30 30
31 componentDidUpdate(prevProps, prevState){ 31 componentDidUpdate(prevProps, prevState){
32 if (this.props.data !== prevProps.data) { 32 if (this.props.data !== prevProps.data) {
33 this.setState({ 33 this.setState({
34 ...this.state, 34 ...this.state,
35 - kg : this.props.data[this.props.data.length - 1].label, 35 + kg : this.props.data[this.props.data.length - 1].value,
36 + span: this.props.data[this.props.data.length -1].label,
36 index: this.props.data.length-1 37 index: this.props.data.length-1
37 }) 38 })
38 - } } 39 + }
40 + }
39 41
40 render() { 42 render() {
41 // Dimensions 43 // Dimensions
...@@ -88,7 +90,7 @@ export default class HistoryGraph extends PureComponent { ...@@ -88,7 +90,7 @@ export default class HistoryGraph extends PureComponent {
88 y2={y(topValue) * -1} 90 y2={y(topValue) * -1}
89 stroke={colors.axis} 91 stroke={colors.axis}
90 strokeDasharray={[3, 3]} 92 strokeDasharray={[3, 3]}
91 - strokeWidth="0.5" 93 + strokeWidth="1"
92 /> 94 />
93 95
94 {/* middle axis */} 96 {/* middle axis */}
...@@ -99,7 +101,7 @@ export default class HistoryGraph extends PureComponent { ...@@ -99,7 +101,7 @@ export default class HistoryGraph extends PureComponent {
99 y2={y(middleValue) * -1} 101 y2={y(middleValue) * -1}
100 stroke={colors.axis} 102 stroke={colors.axis}
101 strokeDasharray={[3, 3]} 103 strokeDasharray={[3, 3]}
102 - strokeWidth="0.5" 104 + strokeWidth="1"
103 /> 105 />
104 106
105 {/* bottom axis */} 107 {/* bottom axis */}
...@@ -109,7 +111,7 @@ export default class HistoryGraph extends PureComponent { ...@@ -109,7 +111,7 @@ export default class HistoryGraph extends PureComponent {
109 x2={graphWidth} 111 x2={graphWidth}
110 y2="2" 112 y2="2"
111 stroke={colors.axis} 113 stroke={colors.axis}
112 - strokeWidth="0.5" 114 + strokeWidth="1"
113 /> 115 />
114 116
115 {/* bars */} 117 {/* bars */}
...@@ -121,8 +123,8 @@ export default class HistoryGraph extends PureComponent { ...@@ -121,8 +123,8 @@ export default class HistoryGraph extends PureComponent {
121 rx={2.5} 123 rx={2.5}
122 width={GRAPH_BAR_WIDTH} 124 width={GRAPH_BAR_WIDTH}
123 height={y(item.value)} 125 height={y(item.value)}
124 - fill = {this.state.kg == item.label ? colors.bars : colors.bardefult} 126 + fill = {this.state.span == item.label ? colors.bars : colors.bardefult}
125 - onPress={()=>this.handleClick(item.label)} 127 + onPress={()=>this.handleClick(item)}
126 /> 128 />
127 ))} 129 ))}
128 130
...@@ -138,7 +140,10 @@ export default class HistoryGraph extends PureComponent { ...@@ -138,7 +140,10 @@ export default class HistoryGraph extends PureComponent {
138 </G> 140 </G>
139 </Svg> 141 </Svg>
140 <View style={style.container}> 142 <View style={style.container}>
141 - <Text>{this.state.kg}</Text> 143 + <Text>{this.state.span}</Text>
144 + <View>
145 + <Text>{this.state.kg/1000}kg</Text>
146 + </View>
142 </View> 147 </View>
143 </View> 148 </View>
144 ) 149 )
......