Resultater fra norsk eliteserie i fotball
Tippeligaen
Poengfangst hjemme og borte, runde for runde
Velg en sesong: selector from setupSeasonSelector() goes here
Velg et lag:
selector from setupTeamSelector(year) goes here
Sammenlign med:
selector from setupTeamSelector2(year) goes here
Det er fire involverte processing-filer:
_nff2.pde
/* Receiving data from javascript and draw animated development of a season in Tippeligaen. Data is year and a string with one line pr team containing teamname and commaseparated accumulated points round by round. Two values pr round: accumulated homepoints and accumulated awaypoints. sample string (far too short): "VIF#0,0,1,0,1,3\nViking#0,0,3,0,1,3" See function setupSeason below. */ // current season Season curSeason=null; // currentRound int curRound=0; // hilited team int curTeam=-1; // team to compare to int curTeam2=-1; // drawing limits int W=500; int H=400; // size of a marker float circleR=5.0; // the biggest value we find for the season, home and away float maxXValue=0; float maxYValue=0; // texting PFont veryBigFont; PFont bigFont; PFont smallFont; boolean restart=false; void setup(){ size(W,H); background(255, 255, 255); frameRate(5); veryBigFont=createFont("Arial Bold",70); bigFont=createFont("Arial Bold",30); smallFont=createFont("Arial Black",12); } void draw(){ if(restart){ background(255, 255, 255); restart=false; } // will not show anything unless we have ordred a season if(curSeason!=null) curSeason.drawMe(curRound++,curTeam,curTeam2); else{ pushStyle(); stroke(0); fill(0); textFont(bigFont); textAlign(CENTER); text("Last opp en sesong",W/2,100); popStyle(); } } //------------------------------- // from javascript // receive data for a season and set it up void setupSeason(int year, String T){ maxXValue=0; maxYValue=0; curSeason=new Season(year,T); curRound=0; curTeam=-1; curteam2=-1; restart=true; } // mark a hilited team void hiliteTeam(String T){ if(curSeason!=null){ curTeam=curSeason.getTeamIndex(T); curRound=0; restart=true; } } // mark a compare team void hiliteTeam2(String T){ if(curSeason!=null){ curTeam2=curSeason.getTeamIndex(T); curRound=0; restart=true; } }
og
_Season.pde
/* A season hold all teams for the season and which year it is about */ class Season{ int year; Team[] teams; Season(int year,String data){ this.year=year; // data is a multiline text with one line for each team String[] T=data.trim().split("\n"); //one team for each line teams=new Team[T.length]; for (int ix=0;ix < T.length;ix++) teams[ix]=new Team(T[ix].trim()); } // locate a team in the teamlist, -1 when not found int getTeamIndex(String T){ for(int ix=0;ix < teams.length; ix++) if(teams[ix].name.indexOf(T)!=-1) return ix; return -1; } // draw up to and including round // hilite teamIx void drawMe(int round,int hilitIx,int compareIx){ // axis stroke(0); line(0,H-1,W,H-1); line(0,0,0,H-1); pushStyle(); textFont(bigFont); fill(120); textAlign(LEFT); text(year,40,50); popStyle(); fill(0); textAlign(RIGHT); text("Hjemme",W-1,H-15); textAlign(LEFT); text("Borte",10,10); // if we should compare a team to a given team we // draw only the two if((compareIx!=-1)&&(hilitIx!=-1)){ teams[compareIx].drawMe(round,false); teams[hilitIx].drawMe(round,true); return; } // we draw all and hilite if wanted for(int ix=0;ix < teams.length;ix++) if(ix!=hilitIx) teams[ix].drawMe(round,false); if(hilitIx !=-1) teams[hilitIx].drawMe(round,true); } }
og
_Team.pde
/* a team ha a name and a history the histoy is accumulated points for each round */ class Team{ String name; Point[] history; Team(String S){ // vif#hp,ap,hp,ap,hp,ap // get the name String T[]=S.split("#"); name=T[0].trim(); // the rest is history, two values pr round String[]V=T[1].split(","); history=new Point[int(V.length/2)]; int hix=0; for(int vix=0; vix < V.length; vix+=2){ history[hix]=new Point(float(V[vix].trim()),float(V[vix+1].trim())); hix++; } } void drawMe(int round,boolean hilite){ pushStyle(); // dont draw non existent data if(round >= history.length){ return; } // name it if hilited stroke(100); if(hilite){ fill(200,100,0); stroke(200,100,0); textFont(bigFont); textAlign(LEFT); text(name,40,80); } // draw all up to and including round, with connecting lines float gap=circleR; for(int ix=1;ix < round;ix++) { float x1=map(history[ix-1].x,0,maxXValue,gap,W-gap); float y1=map(history[ix-1].y,0,maxYValue,H-gap,gap); float x2=map(history[ix].x,0,maxXValue,gap,W-gap); float y2=map(history[ix].y,0,maxYValue,H-gap,gap); ellipse(x1,y1,circleR,circleR); line(x1,y1,x2,y2); ellipse(x2,y2,circleR,circleR); } popStyle(); } }
Javascriptkoden er slik:
_produce.js
// all seasons as loaded var seasons; // when we have loaded the page function initAll(){ loadSeasons(); setupSeasonSelector(); var selector=document.getElementById("year"); selector.value="2015" selector.onchange=function(){markYear()}; setTimeout(markYear,100); } // load all data, all seasons function loadSeasons(){ // local data with var assignment alldata=<JSON>: seasons=alldata.seasons; // otherwise, do syncron ajax } // look for a season, by year function getSeason(year){ for(var six=0;six<seasons.length;six++) if(seasons[six].year==year){ return seasons[six]; } return null; } // locate a team in the seasonStatus we // are building in produceProString function findTeam(teamName,resultlist){ for(var ix=0;ix < resultlist.length;ix++) if (resultlist[ix].name==teamName) return resultlist[ix]; return null; } // produce the string we are to send to processing // together with the year function produceProString(year){ //"Viking,5,5,3,4,1,2 #Rosenborg,0,3,5,6,7,8" // name,hp,ap,hp,ap#name,hp,ap,hp,ap // two values (accumulated homepoints,accumulated awaypoints) for each round // assume seasons are loaded var season= getSeason(year); if(!season){ console.log("ERROR, NO SEASON",year); return; } var matches=season.matches; // we build in this array, with an element pr round // only one match pr team pr round var seasonStatus=[]; // set up initial team-object list for(var ix=0;ix < matches.length;ix++){ var theTeam=findTeam(matches[ix].hteam,seasonStatus); if(theTeam==null){ var newTeam=[{ "name":matches[ix].hteam, "homepoints":0, "awaypoints":0, "proString":"" }]; seasonStatus=seasonStatus.concat(newTeam); } } // walk throug matches and update prostring in seasonStatus for(var mix=0; mix < matches.length; mix++){ var match=matches[mix]; var hteam=findTeam(match.hteam,seasonStatus); var ateam=findTeam(match.ateam,seasonStatus); if(match.hscore == match.ascore){ hteam.homepoints+=1; ateam.awaypoints+=1 }else if(match.hscore > match.ascore) hteam.homepoints+=3; else ateam.awaypoints+=3; // and update string hteam.proString+=hteam.homepoints+","+hteam.awaypoints+","; ateam.proString+=ateam.homepoints+","+ateam.awaypoints+","; } // format it and return, make round 0 so all starts at origo var result=""; for(var ix=0;ix < seasonStatus.length;ix++){ var ps=seasonStatus[ix].proString; ps=ps.substring(0,ps.length-1); // loose a , result=result+seasonStatus[ix].name+"# 0,0,+"+ps+"\n"; } return result; } // new year, update teamselectors and sen data to processing function markYear(){ var year=parseInt(document.getElementById("year").value); // update teamselector (hilited) setupTeamSelector(year); var selector=document.getElementById("team"); selector.onchange=function(){setTeam()}; // update teamselector (compare to) setupTeamSelector2(year); var selector=document.getElementById("team2"); selector.onchange=function(){setTeam2()}; // send year data setYear(year); } // --------- talk to the sketch ---------- // all data for a year function setYear(year){ // identify processing var pjs=Processing.getInstanceById("draw1"); var T=produceProString(year); pjs.setupSeason(year,T); } // hilited team function setTeam(){ var pjs=Processing.getInstanceById("draw1"); var Team=document.getElementById("team").value; pjs.hiliteTeam(Team); } // team to compare to function setTeam2(){ var pjs=Processing.getInstanceById("draw1"); var Team=document.getElementById("team2").value; pjs.hiliteTeam2(Team); }
og litt kode for å konfigurere websiden med selectorer:
_makehtml.js
// prepareing selector elements according to // selected data function setupSeasonSelector(){ var T='<select style="font-size:14px;" name="year" id="year" >'; for(var six=0;six<seasons.length;six++){ year=seasons[six].year; T+='<option value="'+year+'">'+year+'</option>'; } T+='</select>'; document.getElementById("seasonselector").innerHTML=T; } // hilited team function setupTeamSelector(year){ var T='<select style="font-size:14px;" name="team" id="team" >'; var season=getSeason(year) var teams =["ingen"]; var matches=season.matches; // set up initial team-object list for(var ix=0;ix < matches.length;ix++){ var t=matches[ix].hteam; var found=false; for(var tix=0; (tix < teams.length) &&(!found); tix++) found=teams[tix]==t; if(!found) teams=teams.concat([t]); } for(var tix=0;tix < teams.length;tix++){ T+='<option value="'+teams[tix]+'">'+teams[tix]+'</option>'; } T+='</select>'; document.getElementById("teamselector").innerHTML=T; } // compare to - team function setupTeamSelector2(year){ var T='<select style="font-size:14px;" name="team2" id="team2" >'; var season=getSeason(year) var teams =["alle"]; var matches=season.matches; // set up initial team-object list for(var ix=0;ix < matches.length;ix++){ var t=matches[ix].hteam; var found=false; for(var tix=0; (tix < teams.length) &&(!found); tix++) found=teams[tix]==t; if(!found) teams=teams.concat([t]); } for(var tix=0;tix < teams.length;tix++){ T+='<option value="'+teams[tix]+'">'+teams[tix]+'</option>'; } T+='</select>'; document.getElementById("teamselector2").innerHTML=T; }