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:
const csv = `name,age,email
John,32,jonn@gmail.
Ruth,51,ruth@gmail.`Defineix el tipus Person per les nostres dades CSV:
interface Person name: string
age: number
email: string
}Amb aquesta interfície pots llegir les dades CSV:
const persons = Papa.<Person> header: true,
dynamicTyping: true
}).Al 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:
interface Person name: string
age: number
email: string
}
const csv = `name,age,email
John,32,jonn@gmail.
Ruth,51,ruth@gmail.`
const persons = Papa.<Person> header: true,
dynamicTyping: true
}).
return <ul>
</ul>
)
}Enlloc de mostrar una llista, mostra tots els resultats en una taula:
{% sol %}
return <div className='container m-5'>
<div className='row align-items-center'>
<div className='col-6'>
<h3 className='text-center'>Data</h3>
<table className='table table-striped mt-5 text-center'>
<tr><th>Name</th><th>Age</th><th>Email</th></tr>
</table>
</div>
</div>
</div>
){% endsol %}
Mou tot el codi a un fitxer FromString.tsx.
Modificar el fitxer App.tsx amb aquest codi:
return <FromString/>
}Fitxer remot
Pots treballar amb fitxer remots has de passar una URL i un funció “callback”:
const = useState<any>
// 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
const url = "https://www.ine.es/jaxi/files/tpx/csv_bdsc/43494.csv"
Papa. // El fitxer utilitza ; com a delimitador enlloc de ,
delimiter: ";",
download: true,
complete: ;
}
}
)
}, )
return )
}
// Funció que crea una taula amb el contingut del fitxer CSV
return <table className='table table-striped'>
</tr>)}
</table>
}Gestiona l’error amb un missatge:
{% sol %}
const = useState<>
// 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
const url = "https://www.ine.es/jaxi/files/tpx/csv_bdsc/43494.csvXXX"
Papa. // El fitxer utilitza ; com a delimitador enlloc de ,
delimiter: ";",
download: true,
complete:
;
} ;
}
}
}
)
}, )
return <div className="container">
<h3 className="text-center">Data</h3>
<p className="border border-2 border-info rounded p-2 text-secondary"></p>
<ViewCSV csv= />
</div>
)
}
// Funció que crea una taula amb el contingut del fitxer CSV
return <table className='table table-striped'>
</tr>)}
</table>
}{% endsol %}
Seguir explicat: https://www.papaparse.com/
Plot
https://github.com/javascriptdata/danfojs
;
;
;
const =
// 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
const url = "https://www.ine.es/jaxi/files/tpx/csv_bdsc/43494.csv";
Papa. // El fitxer utilitza ; com a delimitador enlloc de ,
delimiter: ";",
download: true,
complete:
;
} ;
}
}
}
)
}, )
const graphData = x: result...,
y:
.
.
.,
text: result...,
type: 'bar'
}]
// debugger
const layout = 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