Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.65 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.     <meta charset="utf-8" />
  6.     <title></title>
  7.     <style type="text/css">
  8.         canvas {
  9.             display: block;
  10.             border: 1px solid black;
  11.             margin: auto;
  12.         }
  13.     </style>
  14.     <script type="text/javascript">
  15.  
  16.         // vezi http://ec.europa.eu/eurostat/web/json-and-unicode-web-services/getting-started/rest-request pentru format
  17.         const baseUrl = 'http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/';
  18.  
  19.         const tari = ["BG", "CZ", "HR", "HU", "PL", "RO", "SK"];
  20.         const ani = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017];
  21.         const indicatori = [
  22.             { nume: 'INT', param: 'tin00028?ind_type=IND_TOTAL&filterNonGeo=1&precision=1&unit=PC_IND&indic_is=I_ILT12' },
  23.            { nume: 'POP', param: 'tps00001?indic_de=JAN&filterNonGeo=1&precision=1' },
  24.            { nume: 'PIB', param: 'tec00114?indic_de=JAN&filterNonGeo=1&precision=1' }
  25.        ];
  26.  
  27.         //http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/
  28.         //tps00001?precision=1
  29.         //&geo=BG&geo=CZ
  30.        //&time=2009&time=2013&time=2015
  31.  
  32.        function getUrl(numeIndicator) {
  33.            let url = baseUrl;
  34.             url += indicatori.find(item => item.nume === numeIndicator).param;
  35.             url += '&' + tari.map(item => `geo=${item}`).join('&');
  36.             url += '&' + ani.map(item => `time=${item}`).join('&');
  37.             return url;
  38.         }
  39.  
  40.         //console.log(getUrl('POP'));
  41.  
  42.         async function getDate() {
  43.             let rezultat = [];
  44.             for (let indicator of indicatori) {
  45.                 let raspuns = await fetch(getUrl(indicator.nume));
  46.                 let date = await raspuns.json();
  47.  
  48.                 let index = 0;
  49.                 for (let tara of tari) {
  50.                     for (let an of ani) {
  51.                         rezultat.push({
  52.                             'tara': tara,
  53.                             'an': an,
  54.                             'indicator': indicator.nume,
  55.                             'valoare': date.value[index] === undefined ? 0 : date.value[index]
  56.                         });
  57.                         index++;
  58.                     }
  59.                 }
  60.             }
  61.            
  62.             //rezultat = date.value;
  63.             return rezultat;
  64.         }
  65.  
  66.         let date = null;
  67.         document.addEventListener('DOMContentLoaded', app);
  68.         async function app() {
  69.             date = await getDate();
  70.  
  71.             date = date.map(item => {
  72.  
  73.                 const valori = date
  74.                     .filter(x => x.indicator === item.indicator)
  75.                     .map(x => x.valoare);
  76.                 const min = Math.min(...valori);
  77.                 const max = Math.max(...valori);
  78.  
  79.                 //item.valoareNormalizata = 0.13;
  80.                 item.valoareNormalizata = (item.valoare - min) / (max - min);
  81.                 return item;
  82.             })
  83.  
  84.             let an = 2008;
  85.             const canvas = document.querySelector('canvas');
  86.             const W = canvas.width, H = canvas.height;
  87.             const context = canvas.getContext('2d');
  88.  
  89.             function desenareGrafic(an, indX, indY, indR) {
  90.                 for (let tara of tari) {
  91.                     let x = date.find(item => item.an === an
  92.                         && item.tara === tara
  93.                        && item.indicator === indX).valoareNormalizata * W;
  94.  
  95.                     let y = H - date.find(item => item.an === an
  96.                         && item.tara === tara
  97.                        && item.indicator === indY).valoareNormalizata * H;
  98.  
  99.                     let r = 10 + date.find(item => item.an === an
  100.                         && item.tara === tara
  101.                        && item.indicator === indR).valoareNormalizata * 50;
  102.  
  103.                     context.beginPath();
  104.                     context.moveTo(x + r, y);
  105.                     context.arc(x, y, r, 0, 2 * Math.PI);
  106.                     context.fillStyle = `hsla(${tari.findIndex(x => x === tara) * 50}, 100%, 50%, 0.7)`;
  107.                     context.strokeStyle = 'black';
  108.                     context.fill();
  109.                     context.stroke();
  110.  
  111.                     context.fillStyle = 'black';
  112.                     context.fillText(tara, x, y);
  113.                 }
  114.             }
  115.            
  116.             desenareGrafic(an, 'PIB', 'INT', 'POP');
  117.  
  118.             //console.table(date);
  119.         }
  120.  
  121.     </script>
  122. </head>
  123. <body>
  124.     <p>
  125.         <span>2008</span>
  126.         <input type="button" value=">" />
  127.     </p>
  128.     <canvas width="600" height="400"></canvas>
  129. </body>
  130. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement