Advertisement
Guest User

v0.0

a guest
Dec 10th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------ PERSISTS ------//
  2. /**
  3.  * TODO:
  4.  * - Implement Frontpage
  5.  * - Fix double draw call on chart
  6.  */
  7. //Members
  8. var jsondat;    //JSON Data Store
  9. var chart = {}; //Chart Data Store
  10. chart.Dataset = {}; //Dataset collection
  11. var geoloc = {};    //Geolocation Data Store
  12. //Functions
  13. //Chart
  14. function refactor() {
  15.     //update names
  16.     chart.Dataset.people = $('.chrtperssel:checkbox:checked').map(function () {
  17.         return this.name.replace(/_/g,' ');
  18.     }).get();
  19.     //alert(chart.Dataset.people.join(','));
  20.     draw();
  21. }
  22.  
  23. function gmaps_set() {
  24.     //Find height and mass for lat, lng use
  25.     for (var i = 0; i < jsondat.people.length; i++) {
  26.         if (jsondat.people[i].name == geoloc.target) {
  27.             geoloc.tlat = 60;   //Test values, place with algorithm
  28.             geoloc.tlng = 75;
  29.             break;
  30.         }
  31.     }
  32.     //Generate map settings
  33.     var mc = new google.maps.LatLng(geoloc.tlat, geoloc.tlng);
  34.     var opt = {
  35.         center: mc,
  36.         zoom: 10,
  37.         mapTypeId: google.maps.MapTypeId.ROADMAP
  38.     }
  39.     //Render map
  40.     geoloc.map = new google.maps.Map($('#geocvs'), opt);
  41. }
  42.  
  43. function draw() {
  44.     //alert(chart.Dataset.type);
  45.     //Compile Data
  46.     for (var k = 0; k < chart.Dataset.people.length; k++) {
  47.         for (var l = 0; l < jsondat.people.length; l++) {
  48.             if (chart.Dataset.people[k] == jsondat.people[l].name) {
  49.                 chart.Dataset.data[k] = (chart.Dataset.type == 'height') ?
  50.                     jsondat.people[l].height : jsondat.people[l].mass;
  51.                 //console.log(chart.Dataset.people[k] + ':' + chart.Dataset.data[k]);
  52.             }
  53.         }
  54.     }
  55.  
  56.     var bgc = {};   //Background colour data
  57.     var bc = {};    //Border colour data
  58.     var t;          //Generate spectrum colour distribution for bar colours
  59.     for (var i = 0; i < chart.Dataset.people.length; i++) {
  60.         t = i / chart.Dataset.people.length * 255;
  61.         bgc[i] = 'rgba(' + Math.round(255 - t)
  62.             + ', ' + Math.round(255-(2*Math.abs(t-127)))
  63.             + ', ' + Math.round(t) + ', 0.4)';
  64.         t = (i / chart.Dataset.people.length * 200) + 55;
  65.         bc[i] = 'rgba(' + Math.round(255 - t)
  66.             + ', ' + Math.round(255 - (2 * Math.abs(t - 127)))
  67.             + ', ' + Math.round(t) + ', 0.4)';
  68.     }
  69.     //Draw
  70.     chart.inst = new Chart(chart.ctx, {
  71.         type: 'bar',
  72.         data: {
  73.             labels: chart.Dataset.people,
  74.             datasets: [{
  75.                 label: chart.Dataset.type,
  76.                 data: chart.Dataset.data,
  77.                 backgroundColor: bgc,
  78.                 borderColor: bc,
  79.                 borderWidth: 1
  80.             }]
  81.         },
  82.         options: {
  83.             scales: {
  84.                 yAxes: [{
  85.                     ticks: {
  86.                         beginAtZero: true
  87.                     }
  88.                 }]
  89.             }
  90.         }
  91.     });
  92. }
  93. //------ END PERSIST ------//
  94. $(document).ready(function () {
  95.     //------ JSON INIT ------//
  96.     $.ajax({
  97.         type: "GET",
  98.         dataType: "json",
  99.         url: "res/pe3.json",
  100.         success: function (jdat) {
  101.             jsondat = jdat;
  102.             $.each(jsondat.people, function (i, person) {
  103.                 $("#plist").append('<li><a href="#' + person.name.replace(/ /g, '_')
  104.                     + '">' + person.name + '</a></li>');
  105.                 //------ TEXT DISPLAY ------//
  106.                 $('a[href="#' + person.name.replace(/ /g, '_') + '"]').click(function (e) {
  107.                     //--Override default behaviour
  108.                     e.preventDefault();
  109.                     //--Generate page
  110.                     var tgthref = e.target.href.split("#", 2)[1].replace(/_/g, ' '); //get selection
  111.                     //find entry in list
  112.                     var ptr = {};
  113.                     for (var j = 0; j < jsondat.people.length; j++) {
  114.                         if (jsondat.people[j].name == tgthref) {
  115.                             ptr = jsondat.people[j];
  116.                         }
  117.                     }
  118.                     //Pull data from other JSON links in JSON
  119.                     if (ptr.homeworld != '') {  //Check for empty
  120.                         $.ajax({
  121.                             type: "GET",
  122.                             dataType: "json",
  123.                             url: ptr.homeworld + "?format=json",
  124.                             success: function (dat) {
  125.                                 $('#hw').html(dat.name);
  126.                             }
  127.                         });
  128.                     } else {
  129.                         $('#hw').html('');
  130.                     }
  131.  
  132.                     if (typeof ptr.films !== undefined && ptr.films.length > 0) { //empty, null check
  133.                         for (var j = 0; j < ptr.films.length; j++) {
  134.                             $.ajax({
  135.                                 type: "GET",
  136.                                 dataType: "json",
  137.                                 url: ptr.films[i] + "?format=json",
  138.                                 success: function (dat) {
  139.                                     $('#fl').html(dat.title + '<br>');
  140.                                 }
  141.                             });
  142.                         }
  143.                     } else {
  144.                         $('#fl').html('');
  145.                     }
  146.  
  147.                     if (typeof ptr.species !== undefined && ptr.species.length > 0) { //empty, null check
  148.                         for (var j = 0; j < ptr.species.length; j++) {
  149.                             $.ajax({
  150.                                 type: "GET",
  151.                                 dataType: "json",
  152.                                 url: ptr.species[i] + "?format=json",
  153.                                 success: function (dat) {
  154.                                     $('#sp').html(dat.name + '<br>');
  155.                                 }
  156.                             });
  157.                         }
  158.                     } else {
  159.                         $('#sp').html('');
  160.                     }
  161.  
  162.                     if (typeof ptr.vehicles !== undefined && ptr.vehicles.length > 0) { //empty, null check
  163.                         for (var j = 0; j < ptr.vehicles.length; j++) {
  164.                             $.ajax({
  165.                                 type: "GET",
  166.                                 dataType: "json",
  167.                                 url: ptr.vehicles[i] + "?format=json",
  168.                                 success: function (dat) {
  169.                                     $('#vh').html(dat.name + '<br>');
  170.                                 }
  171.                             });
  172.                         }
  173.                     } else {
  174.                         $('#vh').html('');
  175.                     }
  176.  
  177.                     if (typeof ptr.starships !== undefined && ptr.starships.length > 0) { //empty, null check
  178.                         for (var j = 0; j < ptr.starships.length; j++) {
  179.                             $.ajax({
  180.                                 type: "GET",
  181.                                 dataType: "json",
  182.                                 url: ptr.starships[i] + "?format=json",
  183.                                 success: function (dat) {
  184.                                     $('#ss').html(dat.name + '<br>');
  185.                                 }
  186.                             });
  187.                         }
  188.                     } else {
  189.                         $('#ss').html('');
  190.                     }
  191.  
  192.                     //populate table
  193.                     $('#nm').html(ptr.name);
  194.                     $('#ht').html(ptr.height+'m');
  195.                     $('#ms').html(ptr.mass+'kg');
  196.                     $('#hc').html(ptr.hair_color);
  197.                     $('#sc').html(ptr.skin_color);
  198.                     $('#ec').html(ptr.eye_color);
  199.                     $('#by').html(ptr.birth_year);
  200.                     $('#gd').html(ptr.gender);
  201.                     $('#cr').html(ptr.created);
  202.                     $('#et').html(ptr.edited);
  203.                     $('#ur').html('<a href="' + ptr.url + '">Original</a>');
  204.                     //--Switch to page
  205.                     $(':mobile-pagecontainer').pagecontainer('change', '#txtdis', { allowSamePageTransition: true });
  206.                 });
  207.                 //------ END TEXTDIS ------//
  208.                 //------ SELECT INIT ------//
  209.                 $('#chrtpersonsel').append('<input type="checkbox" name="'
  210.                     + person.name.replace(/ /g, '_') + '" id="'
  211.                     + person.name.replace(/ /g, '_') + '" class="chrtperssel"><label for="'
  212.                     + person.name.replace(/ /g, '_') + '">'
  213.                     + person.name + '</label>'
  214.                     );
  215.                 $('#' + person.name.replace(/ /g, '_')).change(function () {
  216.                     refactor();
  217.                 });
  218.                 //------ END SELINIT ------//
  219.                 //------ GEOLOC INIT ------//
  220.                 //Populate options list
  221.                 $('#geosel').append('<option value="'
  222.                     + person.name.replace(/ /g, '_') +'">'
  223.                     + person.name + '</option>'
  224.                     );
  225.                 //------ END GEOLOC ------//
  226.             });
  227.         }
  228.     });
  229.     //------ END JSONINIT ------//
  230.  
  231.     //------ CHART FUNCTIONS ------//
  232.     //Init Chart
  233.     chart.ctx = $('#chrtcvs');
  234.     chart.Dataset.type = 'height'; //Default
  235.     refactor();
  236.     //Events
  237.     $('#chrtdatsel').change(function () {
  238.         chart.Dataset.type = $('#chrtdatsel').val();
  239.         draw();
  240.     });
  241.     //------ END CHRTFUNC ------//
  242.     //------ GEOLOC INIT ------//
  243.     $('#geosel').change(function () {
  244.         geoloc.target = $('#geoloc').val().replace(/_/g, ' ');
  245.         gmaps_set();
  246.     })
  247.     //------ END GEOLOC ------//
  248. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement