CSV
Moltes dades que necessitem estan en format CSV
Introducció
En aquest enllaç tens el projecte de suport d’aquesta activitat: https://gitlab.com/xtec/ts/csv
Entorn de treball
Crea un projecte react:
PapaParse
PapaParse és un parser de dades CSV.
Afegeix una dependència amb papaparse
Afegeix aquest import al fitxer index.ts:
Imagina’t que tenim aquestes dades CSV:
John,32,jonn@gmail.com
Ruth,51,ruth@gmail.com`Defineix el tipus Person per les nostres dades CSV:
name: string
age: number
email: string
}Amb aquesta interfície pots llegir les dades CSV:
header: true,
dynamicTyping: true
}).dataAl indicar que la primera fila és una capçalera (“header”), cada fila s’organitza pel nom del camp enlloc d’utilitzar un index.
Per defecte tot es converteix al tipus string.
Amb dynamicTyping: true fas que els números es converteixin en number i els booleans en boolean.
I ja pots mostrar tots els noms en una llista:
name: string
age: number
email: string
}
John,32,jonn@gmail.com
Ruth,51,ruth@gmail.com`
header: true,
dynamicTyping: true
}).data
return
</ul>
)
}Enlloc de mostrar una llista, mostra tots els resultats en una taula:
{% sol %}
return
</table>
</div>
</div>
</div>
){% endsol %}
Mou tot el codi a un fitxer FromString.tsx.
Modificar el fitxer App.tsx amb aquest codi:
return
}Fitxer remot
Pots treballar amb fitxer remots has de passar una URL i un funció “callback”:
// https://datos.gob.es/es/catalogo/ea0010587-numero-de-estudiantes-de-educacion-superior-que-han-recibido-becas-o-ayudas-segun-el-tipo-de-beca-o-ayuda-recibida-identificador-api-t13-p460-2019-l0-02014-px1
url, // El fitxer utilitza ; com a delimitador enlloc de ,
delimiter: ";",
download: true,
complete: results.data;
}
}
)
},)
return
)
}
// Funció que crea una taula amb el contingut del fitxer CSV
return
</tr>)}
</table>
}Gestiona l’error amb un missatge:
{% sol %}
// https://datos.gob.es/es/catalogo/ea0010587-numero-de-estudiantes-de-educacion-superior-que-han-recibido-becas-o-ayudas-segun-el-tipo-de-beca-o-ayuda-recibida-identificador-api-t13-p460-2019-l0-02014-px1
url, // El fitxer utilitza ; com a delimitador enlloc de ,
delimiter: ";",
download: true,
complete:
if results.errors.length != 0 ;
} else ;
}
}
}
)
},)
return
</div>
)
}
// Funció que crea una taula amb el contingut del fitxer CSV
return
</tr>)}
</table>
}{% endsol %}
Seguir explicat: https://www.papaparse.com/
Plot
https://github.com/javascriptdata/danfojs
;
;
;
// https://datos.gob.es/es/catalogo/ea0010587-numero-de-estudiantes-de-educacion-superior-que-han-recibido-becas-o-ayudas-segun-el-tipo-de-beca-o-ayuda-recibida-identificador-api-t13-p460-2019-l0-02014-px1
;
url, // El fitxer utilitza ; com a delimitador enlloc de ,
delimiter: ";",
download: true,
complete:
ifresults.errors.length > 0 ;
}else ;
}
}
}
)
},)
result.data
x: 1, result.data.length - 1row,
y: 1, result.data.length - 1
row
/\./gi, ''
num,
text: 1, result.data.length - 1row,
type: 'bar'
}]
// debugger
title: "Num of students that have recived a scholarship",
}
return Plot data= layout= /;
}TODO
-
<https://www.ine.es/dyngs/INEbase/listaoperaciones.htm >
-
https://typescript.tv/hands-on/parsing-csv-files-in-typescript-with-papa-parse/
-
https://www.basedash.com/blog/how-to-use-papaparse-with-typescript