説明なし

Datos.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import React, {useState} from 'react';
  2. import { View, Text, Pressable, StyleSheet, Alert} from 'react-native';
  3. import DropDownPicker from 'react-native-dropdown-picker';
  4. import { Table, TableWrapper, Row, Rows, Col, Cols, Cell } from 'react-native-table-component';
  5. import { parse } from 'json2csv'
  6. import {styles} from "./styles"
  7. var allData
  8. var allTitle
  9. var table = ""
  10. var pueblo = ""
  11. //where data will be stored
  12. var information
  13. function Picker() {
  14. //table to pick from
  15. const [openTable, setOpenTable] = useState(false);
  16. const [valueTable, setValueTable] = useState(null);
  17. const [table, setTable] = useState([
  18. {label:'DP02PR', value: 'DP02PR'},
  19. {label:'DP03', value: 'DP03'},
  20. {label:'DP04', value: 'DP04'},
  21. {label:'DP05', value: 'DP05'},
  22. ]);
  23. //cambiar pueblo
  24. //pueblo
  25. const [openPueblo, setOpenPueblo] = useState(false);
  26. const [valuePueblo, setValuePueblo] = useState(null);
  27. const [pueblo, setPueblo] = useState([
  28. {label: 'Adjuntas', value: '001'},
  29. {label: 'Aguada', value: '003'},
  30. {label: 'Aguadillas', value: '005'},
  31. {label: 'Aguas Buenas', value: '007'},
  32. {label: 'Aibonito', value: '009'},
  33. {label: 'Añasco', value: '011'},
  34. {label: 'Arecibo', value: '013'},
  35. {label: 'Arroyo', value: '015'},
  36. {label: 'Barceloneta', value: '017'},
  37. {label: 'Barranquitas', value: '019'},
  38. {label: 'Bayamón', value: '021'},
  39. {label: 'Cabo Rojo', value: '023'},
  40. {label: 'Caguas', value: '025'},
  41. {label: 'Camuy', value: '027'},
  42. {label: 'Canóvanas', value: '029'},
  43. {label: 'Carolina', value: '031'},
  44. {label: 'Cataño', value: '033'},
  45. {label: 'Cayey', value: '035'},
  46. {label: 'Ceiba', value: '037'},
  47. {label: 'Ciales', value: '039'},
  48. {label: 'Cidra', value: '041'},
  49. {label: 'Coamo', value: '043'},
  50. {label: 'Comerío', value: '045'},
  51. {label: 'Corozal', value: '047'},
  52. {label: 'Culebra', value: '049'},
  53. {label: 'Dorado', value: '051'},
  54. {label: 'Fajardo', value: '053'},
  55. {label: 'Florida', value: '054'},
  56. {label: 'Guánica', value: '055'},
  57. {label: 'Guayama', value: '057'},
  58. {label: 'Guayanilla', value: '059'},
  59. {label: 'Guaynabo', value: '061'},
  60. {label: 'Gurabo', value: '063'},
  61. {label: 'Hatillo', value: '065'},
  62. {label: 'Hormigueros', value: '067'},
  63. {label: 'Humacao', value: '069'},
  64. {label: 'Isabela', value: '071'},
  65. {label: 'Jayuya', value: '073'},
  66. {label: 'Juana Díaz', value: '075'},
  67. {label: 'Juncos', value: '077'},
  68. {label: 'Lajas', value: '079'},
  69. {label: 'Lares', value: '081'},
  70. {label: 'Las Marías', value: '083'},
  71. {label: 'Las Piedras', value: '085'},
  72. {label: 'Loíza', value: '087'},
  73. {label: 'Luquillo', value: '089'},
  74. {label: 'Manatí', value: '091'},
  75. {label: 'Maricao', value: '093'},
  76. {label: 'Maunabo', value: '095'},
  77. {label: 'Mayagüez', value: '097'},
  78. {label: 'Moca', value: '099'},
  79. {label: 'Morovis', value: '101'},
  80. {label: 'Naguabo', value: '103'},
  81. {label: 'Naranjito', value: '105'},
  82. {label: 'Orocovis', value: '107'},
  83. {label: 'Patillas', value: '109'},
  84. {label: 'Peñuelas', value: '111'},
  85. {label: 'Ponce', value: '113'},
  86. {label: 'Quebradillas', value: '115'},
  87. {label: 'Rincón', value: '117'},
  88. {label: 'Río Grande', value: '119'},
  89. {label: 'Sabana Grande', value: '121'},
  90. {label: 'Salinas', value: '123'},
  91. {label: 'San Germán', value: '125'},
  92. {label: 'San Juan', value: '127'},
  93. {label: 'San Lorenzo', value: '129'},
  94. {label: 'San Sebastián', value: '131'},
  95. {label: 'Santa Isabel', value: '133'},
  96. {label: 'Toa Alta', value: '135'},
  97. {label: 'Toa Baja', value: '137'},
  98. {label: 'Trujillo Alto', value: '139'},
  99. {label: 'Utuado', value: '141'},
  100. {label: 'Vega Alta', value: '143'},
  101. {label: 'Vega Baja', value: '145'},
  102. {label: 'Vieques', value: '147'},
  103. {label: 'Villalba', value: '149'},
  104. {label: 'Yabucoa', value: '151'},
  105. {label: 'Yauco', value: '153'}
  106. ]);
  107. return (
  108. <View>
  109. <DropDownPicker
  110. open={openTable}
  111. value={valueTable}
  112. items={table}
  113. setOpen={setOpenTable}
  114. setValue={setValueTable}
  115. setItems={setTable}
  116. onChangeValue = {(valueTable) => {
  117. updateTable(valueTable);
  118. }}
  119. onOpen = {() => {setOpenPueblo(false)}}
  120. zIndex={3000}
  121. zIndexInverse={1000}
  122. placeholder="Tabla de Datos"
  123. />
  124. <DropDownPicker
  125. searchable={true}
  126. open={openPueblo}
  127. value={valuePueblo}
  128. items={pueblo}
  129. setOpen={setOpenPueblo}
  130. setValue={setValuePueblo}
  131. setItems={setPueblo}
  132. onChangeValue = {(valuePueblo) => {
  133. updatePueblo(valuePueblo);
  134. }}
  135. onOpen = {() => {setOpenTable(false)}}
  136. zIndex={1000}
  137. zIndexInverse={3000}
  138. placeholder="Pueblo"
  139. searchPlaceholder="Busqueda..."
  140. />
  141. </View>
  142. );
  143. }
  144. function updateTable(value){
  145. table = value
  146. }
  147. function updatePueblo(value){
  148. pueblo = value
  149. }
  150. //calls data query
  151. function loadDoc(query) {
  152. console.log("load")
  153. var xhttp = new XMLHttpRequest();
  154. xhttp.onreadystatechange = function() {
  155. if (this.readyState == 4) {
  156. information = this.responseText;
  157. if(this.status == 200){console.log("received response");organize()}
  158. }
  159. }
  160. xhttp.open("GET", query, true);
  161. xhttp.send(null);
  162. }
  163. function fetchData() {
  164. var query = "https://api.census.gov/data/2020/acs/acs5/profile?get=group" + "(" + table + ")&for=county:" + pueblo + "&in=state:72"
  165. if(pueblo && table)
  166. {loadDoc(query)}
  167. else
  168. {Alert.alert("Please fill every parameter before calling!")}
  169. }
  170. function DataTable() {
  171. let tableHead = ['Variable', 'Estimate', 'MoE']
  172. let tableTitle = allTitle
  173. let tableData = allData
  174. return (
  175. <View style={styles.tablecontainer}>
  176. <Table borderStyle={{borderWidth: 1}}>
  177. <Row data={tableHead} flexArr={[1, 2, 1, 1]} style={styles.head} textStyle={styles.tabletext}/>
  178. <TableWrapper style={styles.wrapper}>
  179. <Col data={tableTitle} style={styles.title} heightArr={[28,28]} textStyle={styles.tabletext}/>
  180. <Rows data={tableData} flexArr={[2, 1, 1]} style={styles.row} textStyle={styles.tabletext}/>
  181. </TableWrapper>
  182. </Table>
  183. </View>
  184. )
  185. }
  186. //organize data make it pretty
  187. //add loading thing while this organizes all the data !!!
  188. function organize(){
  189. let bigData = JSON.parse(information)
  190. bigData = parse(bigData)
  191. var variables = []
  192. var estimates = []
  193. var moe = []
  194. let divData = bigData.split(",")
  195. let num = Math.floor(divData.length/3) + 1
  196. for(let i = num; i < Math.floor(divData.length/3*2); i++){
  197. variables.push(divData[i])
  198. estimates.push(divData[i+num])
  199. moe.push(divData[i+num*1.5])
  200. }
  201. //console.log(variables)
  202. //console.log(estimates)
  203. //console.log(moe)
  204. let tableContents = []
  205. for(let i = 0; i < estimates.length; i++)
  206. {
  207. tableContents.push([estimates[i],moe[i]])
  208. }
  209. allTitle = variables
  210. allData = tableContents
  211. information = <DataTable />
  212. /*
  213. var stringDiv = JSON.stringify(information)
  214. stringDiv = stringDiv.replace('[[', '');
  215. stringDiv = stringDiv.replace(']]', '');
  216. stringDiv = stringDiv.split(',')
  217. //create smaller array variables
  218. let variables = []
  219. let value = []
  220. let est = []
  221. //theres a better way to do this but im not fixing it rn
  222. for(let i = 0; i<stringDiv.length; i++){
  223. if(i<stringDiv.length/3)
  224. {variables.push(stringDiv[i])}
  225. else if(i<stringDiv.length/3*2)
  226. {value.push(stringDiv[i])}
  227. else
  228. {est.push(stringDiv[i])}
  229. }
  230. //anyways this to format it
  231. console.log("vars:",variables)
  232. console.log("val:",value)
  233. console.log("est:",est)
  234. */
  235. }
  236. function json_2_csv(information){
  237. const csv = parse(obj);
  238. console.log(csv);
  239. }
  240. //fix
  241. DropDownPicker.setListMode("SCROLLVIEW")
  242. //data parsing goes here
  243. function getInfo(){
  244. if(!information)
  245. {Alert.alert("Please try again later! Error retriving data")}
  246. else
  247. {return information}
  248. }
  249. //data display
  250. class DataButton extends React.Component{
  251. state = {
  252. updated: 1,
  253. notUpdated: 0
  254. }
  255. pullData = () => {fetchData(); this.setState({current: 1})}
  256. //the reason you need to press the button twice is bc the http request doesnt finish in time
  257. //for information to be updated before its rerendered
  258. componentDidUpdate(){
  259. if(this.state.current == 1){
  260. this.setState({current: 0})
  261. }
  262. }
  263. state = {current: 0}
  264. render() {
  265. return(
  266. <View style = {styles.container}>
  267. <Pressable style={styles.buttons} onPress = {this.pullData}>
  268. <Text style={styles.text}> Fetch Data</Text>
  269. </Pressable>
  270. <Text style={styles.sub}>{information}</Text>
  271. </View>
  272. );
  273. }
  274. }
  275. export { Picker, updateTable, updatePueblo,loadDoc,fetchData,DataButton};