Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="fr">
- <head>
- <meta charset="utf-8">
- <title>Bretagne - hydrométrie</title>
- <script src="https://www.chartjs.org/dist/2.8.0/Chart.min.js"></script>
- <!-- <script src="https://www.chartjs.org/samples/latest/utils.js"></script> -->
- <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>
- <!-- <script src="papaparse.min.js"></script> -->
- <!-- <script src="wfsGrapheur.js"></script> -->
- <style>
- canvas{
- -moz-user-select: none;
- -webkit-user-select: none;
- -ms-user-select: none;
- }
- </style>
- </head>
- <body>
- <div id="hideLEgend" style="width:100%;">
- <!-- <button id="indicateurs">Indicateurs</button> -->
- <!-- <div id="indic" style="display:none;"></div> -->
- <canvas id="canvas" ></canvas>
- </div>
- <script>
- //pour récupérer les paramètres
- <!-- function processUser() -->
- <!-- { -->
- <!-- var parameters = location.search.substring(1).split("&"); -->
- <!-- var temp = parameters[0].split("="); -->
- <!-- l1 = unescape(temp[1]); -->
- <!-- temp = parameters[1].split("="); -->
- <!-- p1 = unescape(temp[1]); -->
- <!-- temp = parameters[2].split("="); -->
- <!-- l2 = unescape(temp[1]); -->
- <!-- temp = parameters[3].split("="); -->
- <!-- p2 = unescape(temp[1]); -->
- <!-- listeId=[l1,l2]; -->
- <!-- listeTitre=[p1,p2]; -->
- <!-- }; -->
- var listeOption=[];
- var date,moyenne_interannuelle,valeur_faible,valeur_tres_faible,valeur_forte,debit_donnee_provisoire_m3,debit_donnee_validee_m3;
- date={
- nom:"date",
- label:"date",
- draw:false,
- axeX:true,
- indiceCSV:1,
- liste:[]
- };
- moyenne_interannuelle={
- nom:"moyenne_interannuelle",
- label:"Valeur médiane interannuelle",
- draw:true,
- axeX:false,
- color:'rgb(0,128,0)',
- borderWidth:1,
- indiceCSV:2,
- liste:[]
- };
- valeur_faible={
- nom:"valeur_faible",
- label:"Valeur faible (VCN3 quinquennal)",
- draw:true,
- axeX:false,
- color:'rgb(235,152,78)',
- borderWidth:1,
- indiceCSV:3,
- liste:[]
- };
- valeur_tres_faible={
- nom:"valeur_tres_faible",
- label:"Valeur très faible (VCN3 décennal)",
- draw:true,
- axeX:false,
- color:'rgb(255,0,0)',
- borderWidth:1,
- indiceCSV:4,
- liste:[]
- };
- valeur_forte={
- nom:"valeur_forte",
- label:"Valeur forte (QMJ maximum quinquennal)",
- draw:true,
- axeX:false,
- color:'rgb(0,255,255)',
- borderWidth:1,
- indiceCSV:5,
- liste:[]
- };
- debit_donnee_provisoire_m3={
- nom:"debit_donnee_provisoire_m3",
- label:"Débit moyen journalier provisoire",
- draw:true,
- axeX:false,
- color:'rgb(0, 0, 255)',
- borderWidth:1,
- indiceCSV:6,
- liste:[]
- };
- debit_donnee_validee_m3={
- nom:"debit_donnee_validee_m3",
- label:"Débit moyen journalier validé",
- draw:true,
- axeX:false,
- color:'rgb(0, 0, 200)',
- borderWidth:3,
- indiceCSV:7,
- liste:[]
- };
- <!--------------------------------------------------------------->
- //ici récupérer la liste éventuelle des cql_filter, ainsi que les titles
- //ensuite pour chaque élément de la liste faire un option
- //lancer wfsGrapheur pour chaque option, à placer dans la page
- <!-- processUser(); -->
- <!-- for (var i=0;i<2;i++) { -->
- var option={
- url:"https://geobretagne.fr/geoserver-test/geobretagne/wfs?",
- typeName:"hydrometrie_qmj_historique",
- cql_filter:"code_hydro="+'J7010610',
- title:'Airon à Landivy (DREAL Normandie)',
- listeParam:[date,debit_donnee_validee_m3,debit_donnee_provisoire_m3,moyenne_interannuelle,valeur_forte,valeur_faible,valeur_tres_faible],
- logarithme:true,
- source:"DREAL Bretagne"
- };
- listeOption.push(option);
- <!-- } -->
- wfsGrapheur(option);
- <!-- function pausecomp(millis) -->
- <!-- { -->
- <!-- var date = new Date(); -->
- <!-- var curDate = null; -->
- <!-- do { curDate = new Date(); } -->
- <!-- while(curDate-date < millis); -->
- <!-- } -->
- <!-- pausecomp(2000); -->
- <!-- console.log("coucou après 2 secondes"); -->
- //wfsGrapheur(listeOption[1],'canvas1');
- //afficher / masquer quasi tous les champs (titre légende axes)
- //pb si on veut individualiser les actions
- //essayer aussi de déporter ces actions dans le js, plutôt que dans le template
- // pour afficher/masquer la légende
- document.getElementById('hideLEgend').addEventListener('click', habillage);
- // pour afficher/masquer les indicateurs
- <!-- document.getElementById('indicateurs').addEventListener('click', indicateur); -->
- //déclaration des variables
- var data=[],
- listeDate=[],
- listeLabels=[],
- MONTHS,
- config,
- listeDatas=[],
- url,
- source,
- maxValue,
- titre,
- axeY,
- bool;
- function wfsGrapheur(option){
- //construction de l'url
- url=option.url+"service=wfs&version=2.0.0&request=getFeature&"+
- "typeName="+option.typeName+"&cql_filter="+option.cql_filter+
- "&outputFormat=CSV"+"&propertyName=";
- //on ajoute à l'url la liste des propertyName
- for (var i=0;i<option.listeParam.length;i++){
- url=url+option.listeParam[i].nom+",";
- }
- console.log(url,"url");
- //on rempli les champs source du graphe avec l'année en cours et titre
- source="source: "+option.source+" "+new Date().getFullYear();
- titre=option.title;
- papaparse(option);
- bool=option.logarithme;
- log(bool);
- }
- //passage de l'échelle au log
- function log(booleen){
- if (booleen==true){
- axeY=[{
- display: true,
- type: 'logarithmic',
- ticks: {
- min: 0,
- max: maxValue+1,//si on mets une valeur fixe on pourra peut être mieux comparer les graphes entre eux
- callback: function(value, index, values) {//needed to change the scientific notation results from using logarithmic scale
- return Number(value.toString());//pass tick values as a string into Number function
- }
- },
- scaleLabel: {
- display: true,
- fontSize:16,
- fontStyle:'bold',
- labelString: 'Débit en m³/s - échelle log'
- }
- }];
- axeYsimplifie=[{
- display: true,
- type: 'logarithmic',
- ticks: {
- min: 0,
- max: maxValue+1,//si on mets une valeur fixe on pourra peut être mieux comparer les graphes entre eux
- callback: function(value, index, values) {//needed to change the scientific notation results from using logarithmic scale
- return Number(value.toString());//pass tick values as a string into Number function
- }
- },
- scaleLabel: {
- display: false,
- fontSize:16,
- fontStyle:'bold',
- labelString: 'Débit en m³/s - échelle log'
- }
- }];
- //axe qui s'affiche au chargement
- axeYInit=[{
- display: true,
- type: 'logarithmic',
- ticks: {
- min: 0,
- max: maxValue+1,//si on mets une valeur fixe on pourra peut être mieux comparer les graphes entre eux
- callback: function(value, index, values) {//needed to change the scientific notation results from using logarithmic scale
- return Number(value.toString());//pass tick values as a string into Number function
- }
- },
- scaleLabel: {
- display: false,
- fontSize:16,
- fontStyle:'bold',
- labelString: 'Débit en m³/s - échelle log'
- }
- }];
- }
- else {
- axeY=[{
- display: true,
- scaleLabel: {
- display: true,
- fontSize:16,
- fontStyle:'bold',
- labelString: 'Débit en m³/s '
- }
- }];
- axeYsimplifie=[{
- display: true,
- scaleLabel: {
- display: false,
- fontSize:16,
- fontStyle:'bold',
- labelString: 'Débit en m³/s '
- }
- }];
- //axe qui s'affiche au chargement
- axeYInit=[{
- display: true,
- scaleLabel: {
- display: false,
- fontSize:16,
- fontStyle:'bold',
- labelString: 'Débit en m³/s '
- }
- }];
- }
- return [axeY,axeYsimplifie];
- }
- //définition de la configuration du graphique
- function initialisation(){
- MONTHS = listeDate;
- config = {
- type: 'line',
- data: {
- labels: listeLabels,
- datasets: listeDatas
- },
- options: {
- legend: {
- display:false,
- labels:{
- usePointStyle: true
- },
- // par défaut, onClick supprime les courbes. Est-ce un comportement à conserver?
- //pour enlever le comportement ajout d'une fonction vide toto
- onClick:function toto(){}
- },
- responsive: true,
- title: {
- display: false,
- fontSize:16,
- fontStyle:'bold',
- text: titre
- },
- tooltips: {
- mode: 'nearest',//'index' pour la liste des valeurs pour un x
- intersect: false,
- },
- hover: {
- mode: 'nearest',
- intersect: true
- },
- scales: {
- xAxes: [{
- display: false
- }],
- yAxes: axeYInit
- }
- }
- };
- }
- //fonction qui affiche/cache les différents habillages du graphique
- function habillage() {
- var nom = window.location.href;
- //window.open(nom,"nom_fenetre","options_fenetre");
- var axeY=myLine.options.scales.yAxes;
- myLine.options.legend.display = !myLine.options.legend.display;
- myLine.options.title.display = !myLine.options.title.display;
- var axeVisible=[{
- display: true,
- scaleLabel: {
- display: true,
- fontSize:16,
- fontStyle:'bold',
- labelString: source
- }
- }];
- var axeCache= [{
- display: false
- }];
- if(myLine.options.legend.display){
- myLine.options.scales.xAxes=axeVisible;
- myLine.options.scales.yAxes=log(option.logarithme)[0];
- document.getElementById('hideLEgend').style.width="80%";
- }else{
- myLine.options.scales.xAxes=axeCache;
- myLine.options.scales.yAxes=log(option.logarithme)[1];
- document.getElementById('hideLEgend').style.width="100%";
- }
- myLine.update();
- }
- //fonction qui affiche/cache les indicateurs
- function indicateur() {
- if (document.getElementById('indic').style.display == "none"){
- document.getElementById('indic').style.display = "block";
- }else{
- document.getElementById('indic').style.display = "none";
- }
- }
- //charge la courbe dans la div 'canvas'
- function drawChart() {
- var ctx = document.getElementById('canvas').getContext('2d');
- window.myLine = new Chart(ctx, config);
- }
- function papaparse(option){
- Papa.parse(url, {
- download: true,
- complete: function(results) {
- data=results.data;
- // on rempli toutes les listes
- for (var j =0;j<option.listeParam.length;j++){
- option.listeParam[j].liste=[];
- for (var i = 1; i < data.length-1; i++) {
- option.listeParam[j].liste.push(data[i][option.listeParam[j].indiceCSV]);
- }
- }
- // en cas d'absence de valeur dans une des listes, on remplace par null pour éviter d'afficher les 0 dans le graphique
- for (var k =0;k<option.listeParam.length;k++){
- for (var l=0;l<option.listeParam[k].liste.length;l++){
- if (option.listeParam[k].liste[l]===""){
- option.listeParam[k].liste[l]=null;
- }
- }
- }
- // pour chaque courbe, on ajoute un dataset
- maxValue=0;
- for (var m=0;m<option.listeParam.length;m++) {
- if (option.listeParam[m].draw){
- var obj={
- pointStyle:'line',
- label:option.listeParam[m].label,
- backgroundColor: 'rgb(255, 255, 255)',//option.listeParam[m].color,
- borderColor: option.listeParam[m].color,
- borderWidth:option.listeParam[m].borderWidth,
- pointBorderWidth:0.5,
- pointRadius:0.5,
- data: option.listeParam[m].liste,
- fill: false,
- };
- listeDatas.push(obj);
- //on cherche la valeur maximale dans le graphique pour fixer la valeur max de l'échelle log
- if (option.logarithme){
- for(var n=0;n<option.listeParam[m].liste.length;n++){
- if (option.listeParam[m].liste[n] != 'null'){
- maxValue=Math.max(maxValue,option.listeParam[m].liste[n]);
- }
- }
- }
- }
- if (option.listeParam[m].axeX){
- listeDate=option.listeParam[m].liste;
- }
- }
- console.log(listeDatas);
- //listeDate, pour simplifier l'affichage de l'axe X
- for (var a=0;a<listeDate.length;a++){
- var an,mois,moisLabel;
- an=listeDate[a].substring(2,4);
- mois=listeDate[a].substring(5,7);
- jour=listeDate[a].substring(8,10);
- // switch(mois) {
- // case '01':
- // moisLabel='Janv';
- // break;
- // case '02':
- // moisLabel='Févr';
- // break;
- // case '03':
- // moisLabel='Mars';
- // break;
- // case '04':
- // moisLabel='Avr';
- // break;
- // case '05':
- // moisLabel='Mai';
- // break;
- // case '06':
- // moisLabel='Juin';
- // break;
- // case '07':
- // moisLabel='Juill';
- // break;
- // case '08':
- // moisLabel='Août';
- // break
- // case '09':
- // moisLabel='Sept';
- // break;
- // case '10':
- // moisLabel='Oct';
- // break;
- // case '11':
- // moisLabel='Nov';
- // break;
- // case '12':
- // moisLabel='Déc';
- // break;
- // default:
- // break;
- // }
- // listeLabels.push(moisLabel+"-"+an);
- listeLabels.push(jour+"/"+mois+"/"+an);
- }
- initialisation();
- drawChart();
- // indicateurs(option,true);
- }
- });
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement