Advertisement
Guest User

Untitled

a guest
Jun 6th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var buildGraphs = {
  2.  
  3. init: function(){
  4.  
  5.         self = this;
  6.        
  7.         $.ajax({
  8.           url: "pollscript.php",
  9.           cache: false
  10.         }).done(function(data) {
  11.        
  12.           console.log(data);
  13.           console.log("hi");
  14.             var statsJSON = jQuery.parseJSON(data);
  15.             console.log(statsJSON);
  16.             self.statsJSON = statsJSON;
  17.             self.hardscores = self.methods.buildScoreArr(self.data.categories.cathard);
  18.             self.softscores = self.methods.buildScoreArr(self.data.categories.catsoft);
  19.             self.expscores = self.methods.buildScoreArr(self.data.categories.catexp);
  20.             self.totscores = self.methods.totalScore();
  21.             self.hardsimple = self.methods.simpleScore(self.hardscores);
  22.             self.softsimple = self.methods.simpleScore(self.softscores);
  23.             self.expsimple = self.methods.simpleScore(self.expscores);
  24.             self.totsimple = self.methods.simpleScore(self.totscores);
  25.             self.hardbars = self.methods.graphSizes(self.hardsimple);
  26.             self.softbars = self.methods.graphSizes(self.softsimple);
  27.             self.expbars = self.methods.graphSizes(self.expsimple);
  28.             self.totbars = self.methods.graphSizes(self.totsimple);
  29.             console.log(buildGraphs.hardbars);
  30.             console.log(buildGraphs.softbars);
  31.             console.log(buildGraphs.expbars);
  32.             console.log(buildGraphs.totbars);
  33.             self.methods.addTotMarkup();
  34.             self.methods.addCatMarkup();
  35.  
  36.         });
  37.            
  38.     },
  39.  
  40. data: {
  41.  
  42.         categories: {
  43.        
  44.             cathard: ["consoleunit","motioncap","otherinter"],
  45.             catsoft: ["games","ng_apps","media_int","marketplace"],
  46.             catexp: ["onlineplay","service","interface"]
  47.            
  48.         },
  49.        
  50.         barsizeratio: 2.66,
  51.        
  52.         barlength: 216
  53.  
  54.     },
  55.    
  56. methods: {
  57.        
  58.         buildScoreArr: function(arr){
  59.        
  60.             usearray = arr;
  61.             jsonObj = buildGraphs.statsJSON;
  62.             thiscatscore = [0,0,0,0];
  63.            
  64.             for (i=0; i<usearray.length; i++){
  65.                
  66.                 for (ii=0; ii<4; ii++){
  67.                
  68.                     thiscatscore[ii] += jsonObj[usearray[i]][ii];
  69.                    
  70.                 }
  71.                
  72.             }
  73.            
  74.             for (i=0;i<4;i++){
  75.            
  76.                 thiscatscore[i] = thiscatscore[i]/usearray.length;
  77.                 result = Math.round(thiscatscore[i]);
  78.                 thiscatscore[i] = result;
  79.                
  80.             }
  81.            
  82.             return thiscatscore;
  83.  
  84.         },
  85.        
  86.         totalScore: function(){
  87.        
  88.             totalscore = [0,0,0,0];
  89.            
  90.             for (i=0;i<4;i++){
  91.            
  92.                 totalscore[i] += buildGraphs.hardscores[i];
  93.                 totalscore[i] += buildGraphs.softscores[i];
  94.                 totalscore[i] += buildGraphs.expscores[i];
  95.                 totalscore[i] = Math.round(totalscore[i]/3);
  96.            
  97.             }
  98.            
  99.             return totalscore;
  100.        
  101.         },
  102.        
  103.         simpleScore: function(arr){
  104.        
  105.             usearray = arr;
  106.             thisScoreSimple = [0,0];
  107.        
  108.             thisScoreSimple[0] = Math.round((usearray[0]+usearray[2])/2);
  109.             thisScoreSimple[1] = 100-thisScoreSimple[0];
  110.             console.log(thisScoreSimple);
  111.             return thisScoreSimple;
  112.            
  113.         },
  114.        
  115.         graphSizes: function(arr){
  116.        
  117.             usearray = arr;
  118.             thisbarsizeshort = [0,0];
  119.             thisbarsizeshort[0] = Math.round((usearray[0]/100)*buildGraphs.data.barlength);
  120.             thisbarsizeshort[1] = buildGraphs.data.barlength-thisbarsizeshort[0];
  121.             return thisbarsizeshort;
  122.        
  123.         },
  124.        
  125.         addCatMarkup: function(){
  126.        
  127.             $wrapCats = $("<div></div>").attr("id","wrapCats");
  128.             $paraHard = $("<p></p>").text("Hardware").addClass("catTitle");
  129.             $paraSoft = $("<p></p>").text("Software").addClass("catTitle");
  130.             $paraExp = $("<p></p>").text("Experience").addClass("catTitle");
  131.                
  132.             $barWrapperCat = $("<div></div>").addClass("barWrapperCat");
  133.             $barPS3 = $("<div></div>").addClass("scorebar0").addClass("ps3").css("width",buildGraphs.hardbars[1]).text("PS3 (" + buildGraphs.hardsimple[1] + "%)");
  134.             $barXBOX = $("<div></div>").addClass("scorebar1").addClass("xbox").css("width",buildGraphs.hardbars[0]).text("XBOX (" + buildGraphs.hardsimple[0] + "%)");
  135.             $barWrapperCat.append($paraHard).append($barPS3).append($barXBOX);
  136.            
  137.             $wrapCats.append($barWrapperCat);
  138.        
  139.             $barWrapperCat = $("<div></div>").addClass("barWrapperCat");
  140.             $barPS3 = $("<div></div>").addClass("scorebar0").addClass("ps3").css("width",buildGraphs.softbars[1]).text("PS3 (" + buildGraphs.softsimple[1] + "%)");
  141.             $barXBOX = $("<div></div>").addClass("scorebar1").addClass("xbox").css("width",buildGraphs.softbars[0]).text("XBOX (" + buildGraphs.softsimple[0] + "%)");
  142.             $barWrapperCat.append($paraSoft).append($barPS3).append($barXBOX);
  143.        
  144.             $wrapCats.append($barWrapperCat);
  145.            
  146.             $barWrapperCat = $("<div></div>").addClass("barWrapperCat");
  147.             $barPS3 = $("<div></div>").addClass("scorebar0").addClass("ps3").css("width",buildGraphs.expbars[1]).text("PS3 (" + buildGraphs.expsimple[1] + "%)");
  148.             $barXBOX = $("<div></div>").addClass("scorebar1").addClass("xbox").css("width",buildGraphs.expbars[0]).text("XBOX (" + buildGraphs.expsimple[0] + "%)");
  149.             $barWrapperCat.append($paraExp).append($barPS3).append($barXBOX);
  150.            
  151.             $wrapCats.append($barWrapperCat);
  152.            
  153.             $("#graphWrapper").append($wrapCats);
  154.            
  155.         },
  156.        
  157.         addTotMarkup: function(){
  158.        
  159.             totWidth0 = Math.round((buildGraphs.totsimple[0]/100)*778);
  160.             totWidth1 = 778-totWidth0;
  161.             $paraTot = $("<p></p>").text("Overall Score").addClass("catTitle");
  162.            
  163.             $barWrapperTot = $("<div></div>").attr("id","wrapTot");
  164.             $barPS3 = $("<div></div>").addClass("scorebar0").addClass("ps3").css("width",totWidth1).text("PS3 (" + buildGraphs.totsimple[1] + "%)");
  165.             $barXBOX = $("<div></div>").addClass("scorebar1").addClass("xbox").css("width",totWidth0).text("XBOX (" + buildGraphs.totsimple[0] + "%)");
  166.             $barWrapperTot.append($paraTot).append($barPS3).append($barXBOX);
  167.             $("#graphWrapper").append($barWrapperTot);
  168.            
  169.         }
  170.  
  171.     }
  172.  
  173. }
  174.  
  175. $(document).ready(function(){
  176.  
  177. buildGraphs.init();
  178.  
  179. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement