이윤영

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
......
...@@ -47,7 +47,7 @@ export function deleteId(){ ...@@ -47,7 +47,7 @@ export function deleteId(){
47 // Get the default Realm with support for our objects 47 // Get the default Realm with support for our objects
48 let realm = new Realm({schema: [Waste, ID]}); 48 let realm = new Realm({schema: [Waste, ID]});
49 export function add_data(responseJson){ 49 export function add_data(responseJson){
50 - for(i = 0; i < responseJson.length; i++){ 50 + for(var i = 0; i < responseJson.length; i++){
51 realm.write(() => { 51 realm.write(() => {
52 // responseJson[0].ras_id.S 52 // responseJson[0].ras_id.S
53 let myWaste = realm.create('Waste', { 53 let myWaste = realm.create('Waste', {
...@@ -62,6 +62,23 @@ export function add_data(responseJson){ ...@@ -62,6 +62,23 @@ export function add_data(responseJson){
62 return responseJson.length; 62 return responseJson.length;
63 } 63 }
64 64
65 +export function add_temp_data(){
66 + realm.write(() => {
67 + let myWaste = realm.create('Waste', {
68 + weight: parseFloat(747),
69 + diff: parseFloat(257),
70 + date: parseFloat(1576691588000),
71 + });
72 + });
73 + realm.write(() => {
74 + let myWaste = realm.create('Waste', {
75 + weight: parseFloat(1466),
76 + diff: parseFloat(468),
77 + date: parseFloat(1576650859000),
78 + });
79 + });
80 +}
81 +
65 export function get_now_weight(){ 82 export function get_now_weight(){
66 let wastes = realm.objects('Waste'); 83 let wastes = realm.objects('Waste');
67 let recent = wastes.max('date'); 84 let recent = wastes.max('date');
...@@ -69,7 +86,7 @@ export function get_now_weight(){ ...@@ -69,7 +86,7 @@ export function get_now_weight(){
69 var recentWeight = wastes.filtered('date = $0', recent); 86 var recentWeight = wastes.filtered('date = $0', recent);
70 console.log("now_weight",recentWeight) 87 console.log("now_weight",recentWeight)
71 if(recentWeight.length != 0) 88 if(recentWeight.length != 0)
72 - return recentWeight[0].weight; 89 + return recentWeight[recentWeight.length-1].weight;
73 else return 0; 90 else return 0;
74 } 91 }
75 92
...@@ -126,9 +143,11 @@ export function get_week_data(){ ...@@ -126,9 +143,11 @@ export function get_week_data(){
126 daybefore5.setDate(today.getDate()-5); 143 daybefore5.setDate(today.getDate()-5);
127 var daybefore6 = new Date(); 144 var daybefore6 = new Date();
128 daybefore6.setDate(today.getDate()-6); 145 daybefore6.setDate(today.getDate()-6);
146 +
129 var day = [daybefore6.getDay(), daybefore5.getDay(),daybefore4.getDay(), 147 var day = [daybefore6.getDay(), daybefore5.getDay(),daybefore4.getDay(),
130 daybefore3.getDay(),daybefore2.getDay(),daybefore1.getDay(), today.getDay()] 148 daybefore3.getDay(),daybefore2.getDay(),daybefore1.getDay(), today.getDay()]
131 today = today.getTime(); 149 today = today.getTime();
150 +
132 daybefore1 = daybefore1.getTime(); 151 daybefore1 = daybefore1.getTime();
133 daybefore2 = daybefore2.getTime(); 152 daybefore2 = daybefore2.getTime();
134 daybefore3 = daybefore3.getTime(); 153 daybefore3 = daybefore3.getTime();
...@@ -136,100 +155,83 @@ export function get_week_data(){ ...@@ -136,100 +155,83 @@ export function get_week_data(){
136 daybefore5 = daybefore5.getTime(); 155 daybefore5 = daybefore5.getTime();
137 daybefore6 = daybefore6.getTime(); 156 daybefore6 = daybefore6.getTime();
138 157
139 - let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore6, daybefore5); 158 + let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore6, daybefore5).sum('diff');
140 - let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore5, daybefore4); 159 + let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore5, daybefore4).sum('diff');
141 - let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore4, daybefore3); 160 + let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore4, daybefore3).sum('diff');
142 - let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore3, daybefore2); 161 + let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore3, daybefore2).sum('diff');
143 - let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore2, daybefore1); 162 + let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore2, daybefore1).sum('diff');
144 - let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore1, today); 163 + let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', daybefore1, today).sum('diff');
145 - let wastes7 = realm.objects('Waste').filtered('date >= $0', today); 164 + let wastes7 = realm.objects('Waste').filtered('date >= $0', today).sum('diff');
146 - var list = []; 165 +
147 - var total1 =0, total2=0, total3=0, total4=0, total5=0, total6=0, total7 = 0;
148 166
149 - for(var i = 0; i< wastes1.length; i++){ 167 + var week = ['SUN','MON','TUE', 'WED', 'THU', 'FRI', 'SAT'];
150 - total1 += wastes1[i].diff; 168 + var data1 = [
151 - } 169 + { label: week[day[0]], value: wastes1 },
152 - for(var i = 0; i< wastes2.length; i++){ 170 + { label: week[day[1]], value: wastes2 },
153 - total2 += wastes2[i].diff; 171 + { label: week[day[2]], value: wastes3 },
154 - } 172 + { label: week[day[3]], value: wastes4 },
155 - for(var i = 0; i< wastes3.length; i++){ 173 + { label: week[day[4]], value: wastes5 },
156 - total3 += wastes3[i].diff; 174 + { label: week[day[5]], value: wastes6 },
157 - } 175 + { label: week[day[6]], value: wastes7 }
158 - for(var i = 0; i< wastes4.length; i++){ 176 + ]
159 - total4 += wastes4[i].diff; 177 + console.log(data1)
160 - } 178 + return data1;
161 - for(var i = 0; i< wastes5.length; i++){ 179 +}
162 - total5 += wastes5[i].diff; 180 +
163 - } 181 +function getM_D(time){
164 - for(var i = 0; i< wastes6.length; i++){ 182 + console.log("time",time)
165 - total6 += wastes6[i].diff; 183 + var day = new Date(time);
166 - } 184 + console.log(day.getDate())
167 - for(var i = 0; i< wastes7.length; i++){ 185 + return day.getMonth()+1+ "/" +day.getDate();
168 - total7 += wastes7[i].diff;
169 - }
170 - list = [[total1, total2, total3, total4, total5, total6, total7],day]
171 - return list;
172 } 186 }
173 187
174 export function get_month_data(){ 188 export function get_month_data(){
189 + var day7 = (24*60*60*1000) * 7;
175 var today = new Date(); 190 var today = new Date();
176 - var sep1 = new Date(today.getFullYear(), today.getMonth()-1, 1); 191 + today.setHours(0,0,0,0);
177 - var sep2 = new Date(today.getFullYear(), today.getMonth()-1, 7); 192 + var day = today.getDay();
178 - var sep3 = new Date(today.getFullYear(), today.getMonth()-1, 14); 193 + var diff = today.getDate() - day + (day == 0 ? -6:1);
179 - var sep4 = new Date(today.getFullYear(), today.getMonth()-1, 21); 194 +
180 - var sep5 = new Date(today.getFullYear(), today.getMonth()-1, 28); 195 + var sep5 = new Date(today.setDate(diff));
181 - var sep6 = new Date(today.getFullYear(), today.getMonth(), 1) 196 + var sep5 = sep5.getTime();
197 + var sep4 = sep5 - day7;
198 + var sep3 = sep4 - day7;
199 + var sep2 = sep3 - day7;
200 + var sep1 = sep2 - day7;
201 + console.log("first day of week: ", sep5, sep4, sep3, sep2, sep1);
182 202
183 - sep1 = sep1.getTime(); 203 + let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep1, sep2).sum('diff');
184 - sep2 = sep2.getTime(); 204 + let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep2, sep3).sum('diff');
185 - sep3 = sep3.getTime(); 205 + let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep3, sep4).sum('diff');
186 - sep4 = sep4.getTime(); 206 + let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep4, sep5).sum('diff');
187 - sep5 = sep5.getTime(); 207 + let wastes5 = realm.objects('Waste').filtered('date >= $0', sep5).sum('diff');
188 - sep6 = sep6.getTime();
189 -
190 - let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep1, sep2);
191 - let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep2, sep3);
192 - let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep3, sep4);
193 - let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep4, sep5);
194 - let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', sep5, sep6);
195 208
196 - var list = []; 209 + var data2 = [
197 - var total1 =0, total2=0, total3=0, total4=0, total5=0; 210 + { label: getM_D(sep1), value: wastes1 },
198 - 211 + { label: getM_D(sep2), value: wastes2 },
199 - for(var i = 0; i< wastes1.length; i++){ 212 + { label: getM_D(sep3), value: wastes3 },
200 - total1 += wastes1[i].diff; 213 + { label: getM_D(sep4), value: wastes4 },
201 - } 214 + { label: getM_D(sep5), value: wastes5 }
202 - for(var i = 0; i< wastes2.length; i++){ 215 + ]
203 - total2 += wastes2[i].diff; 216 + console.log(data2)
204 - } 217 + return data2;
205 - for(var i = 0; i< wastes3.length; i++){
206 - total3 += wastes3[i].diff;
207 - }
208 - for(var i = 0; i< wastes4.length; i++){
209 - total4 += wastes4[i].diff;
210 - }
211 - for(var i = 0; i< wastes5.length; i++){
212 - total5 += wastes5[i].diff;
213 - }
214 - list = [total1, total2, total3, total4, total5]
215 - return list;
216 } 218 }
217 219
218 export function get_year_data(){ 220 export function get_year_data(){
219 var today = new Date(); 221 var today = new Date();
220 - var mon1 = new Date(today.getFullYear()-1, 0, 1); 222 + var mon1 = new Date(today.getFullYear(), 0, 1);
221 - var mon2 = new Date(today.getFullYear()-1, 1, 1); 223 + var mon2 = new Date(today.getFullYear(), 1, 1);
222 - var mon3 = new Date(today.getFullYear()-1, 2, 1); 224 + var mon3 = new Date(today.getFullYear(), 2, 1);
223 - var mon4 = new Date(today.getFullYear()-1, 3, 1); 225 + var mon4 = new Date(today.getFullYear(), 3, 1);
224 - var mon5 = new Date(today.getFullYear()-1, 4, 1); 226 + var mon5 = new Date(today.getFullYear(), 4, 1);
225 - var mon6 = new Date(today.getFullYear()-1, 5, 1); 227 + var mon6 = new Date(today.getFullYear(), 5, 1);
226 - var mon7 = new Date(today.getFullYear()-1, 6, 1); 228 + var mon7 = new Date(today.getFullYear(), 6, 1);
227 - var mon8 = new Date(today.getFullYear()-1, 7, 1); 229 + var mon8 = new Date(today.getFullYear(), 7, 1);
228 - var mon9 = new Date(today.getFullYear()-1, 8, 1); 230 + var mon9 = new Date(today.getFullYear(), 8, 1);
229 - var mon10 = new Date(today.getFullYear()-1, 9, 1); 231 + var mon10 = new Date(today.getFullYear(), 9, 1);
230 - var mon11 = new Date(today.getFullYear()-1, 10, 1); 232 + var mon11 = new Date(today.getFullYear(), 10, 1);
231 - var mon12 = new Date(today.getFullYear()-1, 11, 1); 233 + var mon12 = new Date(today.getFullYear(), 11, 1);
232 - var nYear = new Date(today.getFullYear(), 0, 1); 234 + var nYear = new Date(today.getFullYear()+1, 0, 1);
233 235
234 mon1 = mon1.getTime(); 236 mon1 = mon1.getTime();
235 mon2 = mon2.getTime(); 237 mon2 = mon2.getTime();
...@@ -246,63 +248,35 @@ export function get_year_data(){ ...@@ -246,63 +248,35 @@ export function get_year_data(){
246 nYear = nYear.getTime(); 248 nYear = nYear.getTime();
247 249
248 250
249 - let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon1, mon2); 251 + let wastes1 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon1, mon2).sum('diff');
250 - let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon2, mon3); 252 + let wastes2 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon2, mon3).sum('diff');
251 - let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon3, mon4); 253 + let wastes3 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon3, mon4).sum('diff');
252 - let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon4, mon5); 254 + let wastes4 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon4, mon5).sum('diff');
253 - let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon5, mon6); 255 + let wastes5 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon5, mon6).sum('diff');
254 - let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon6, mon7); 256 + let wastes6 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon6, mon7).sum('diff');
255 - let wastes7 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon7, mon8); 257 + let wastes7 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon7, mon8).sum('diff');
256 - let wastes8 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon8, mon9); 258 + let wastes8 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon8, mon9).sum('diff');
257 - let wastes9 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon9, mon10); 259 + let wastes9 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon9, mon10).sum('diff');
258 - let wastes10 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon10, mon11); 260 + let wastes10 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon10, mon11).sum('diff');
259 - let wastes11 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon11, mon12); 261 + let wastes11 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon11, mon12).sum('diff');
260 - let wastes12 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon12, nYear); 262 + let wastes12 = realm.objects('Waste').filtered('date >= $0 && date <$1', mon12, nYear).sum('diff');
261 263
262 - var list = []; 264 + var data3 = [
263 - var total1 =0, total2=0, total3=0, total4=0, total5=0, 265 + { label: 'Jan', value: wastes1 },
264 - total6 =0, total7=0, total8=0, total9=0, total10=0, 266 + { label: 'Feb', value: wastes2 },
265 - total11 =0, total12=0; 267 + { label: 'Mar', value: wastes3 },
266 - 268 + { label: 'Apr', value: wastes4 },
267 - for(var i = 0; i< wastes1.length; i++){ 269 + { label: 'May', value: wastes5 },
268 - total1 += wastes1[i].diff; 270 + { label: 'Jun', value: wastes6 },
269 - } 271 + { label: 'Jul', value: wastes7 },
270 - for(var i = 0; i< wastes2.length; i++){ 272 + { label: 'Aug', value: wastes8 },
271 - total2 += wastes2[i].diff; 273 + { label: 'Sep', value: wastes9 },
272 - } 274 + { label: 'Oct', value: wastes10 },
273 - for(var i = 0; i< wastes3.length; i++){ 275 + { label: 'Nov', value: wastes11 },
274 - total3 += wastes3[i].diff; 276 + { label: 'Dec', value: wastes12 }
275 - } 277 +]
276 - for(var i = 0; i< wastes4.length; i++){ 278 +console.log(data3)
277 - total4 += wastes4[i].diff; 279 + return data3;
278 - }
279 - for(var i = 0; i< wastes5.length; i++){
280 - total5 += wastes5[i].diff;
281 - }
282 - for(var i = 0; i< wastes6.length; i++){
283 - total6 += wastes6[i].diff;
284 - }
285 - for(var i = 0; i< wastes7.length; i++){
286 - total7 += wastes7[i].diff;
287 - }
288 - for(var i = 0; i< wastes8.length; i++){
289 - total8 += wastes8[i].diff;
290 - }
291 - for(var i = 0; i< wastes9.length; i++){
292 - total9 += wastes9[i].diff;
293 - }
294 - for(var i = 0; i< wastes10.length; i++){
295 - total10 += wastes10[i].diff;
296 - }
297 - for(var i = 0; i< wastes11.length; i++){
298 - total11 += wastes11[i].diff;
299 - }
300 - for(var i = 0; i< wastes12.length; i++){
301 - total12 += wastes12[i].diff;
302 - }
303 -
304 - list = [total1, total2, total3, total4, total5,total6, total7, total8, total9, total10,total11, total12]
305 - return list;
306 } 280 }
307 281
308 export function delete_all_data(today){ 282 export function delete_all_data(today){
...@@ -320,7 +294,8 @@ function get_today_start(){ ...@@ -320,7 +294,8 @@ function get_today_start(){
320 var mm = today.getMonth(); 294 var mm = today.getMonth();
321 var yyyy = today. getFullYear(); 295 var yyyy = today. getFullYear();
322 296
323 - var togettime = new Date(2019,11,13).getTime(); 297 + var togettime = new Date(yyyy,mm,dd).getTime();
324 console.log(togettime) 298 console.log(togettime)
325 return togettime; 299 return togettime;
326 -}
...\ No newline at end of file ...\ No newline at end of file
300 +}
301 +
......
...@@ -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 )
......