import java.awt.*; import java.util.Enumeration; public class TMNode{ protected float size = 0; protected Rectangle area = new Rectangle(); private String title = ""; private String last = ""; private String first = ""; private String position = ""; private float age = 0; private float projection = 0; private Vector children = new Vector(); private color filling = color(255,255,255); // filling of this node private color colorTitle = color(0,0,0); // color of the title private TMNode parent=null; private boolean highlighted=false; TMNode(){ } TMNode(float s){ size=s; } TMNode(float s,String t){ size=s; title=t; last=title; } TMNode(float s,String l,String f,float a, String pos,float proj){ size=s; last=l; first=f; age=a; position=pos; title=l; projection=proj; } public Enumeration children(){ return children.elements(); } public void setPosition(String pos){ position=pos; } public boolean isLeaf(){ return false; } public float getSize(){ return size; } public void setSize(float s){ size=s; } public Rectangle getArea(){ return area; } public void setArea(Rectangle r){ area=r; } public color getFilling(){ if(highlighted)return getHighlightFilling(); return filling; } public void setFilling(color c){ filling=c; } public color getColorTitle(){ color c=getFilling(); if(brightness(c)<180)return #ffffff; return #000000; } public void setColorTitle(color c){ colorTitle=c; } public float computeSize() { if(children.size()>0){ size = 0.0f; TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); size += child.computeSize(); } } return size; } public void addChild(TMNode child){ children.add(child); child.setParent(this); } public String getTitle(){ if(title.equals("")){ return String.valueOf(size); }else{ if(area.height>=55){ return title; }else{ if(area.width>5 && last.length()>int(area.width/5)){ return last.substring(0,int(area.width/5)-1); }else{ return last; } } } } public void setTitle(String t){ title=t; } public void clearHighlight(boolean mode){ highlighted=false; if(!mode)title=last; } public void clearHighlights(boolean mode) { clearHighlight(mode); if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); child.clearHighlights(mode); } } } public color getHighlightFilling(){ color retColor=filling; if(retColor<#888888)return color(red(retColor)+48,green(retColor)+48,blue(retColor)+48); return color(red(retColor)-48,green(retColor)-48,blue(retColor)-48); } public TMNode highlight(int x, int y,int level) { TMNode retNode=null; DecimalFormat formatter = new DecimalFormat("##.##"); if(contains(x,y)){ if(level==-1){ highlighted=true; if(!first.equals("")){ title=first + "\n"+ last +"\n$" + formatter.format(size); }else{ title=last +"\n$" + formatter.format(size); } return this; }else{ if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); TMNode testNode=child.highlight(x,y,level-1); if(testNode!=null)return testNode; } } } } return null; } public void setHighlightTitles() { DecimalFormat formatter = new DecimalFormat("##.##"); if(!first.equals("")){ title=first + "\n"+ last +"\n$" + formatter.format(size); }else{ title=last +"\n$" + formatter.format(size); } if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); child.setHighlightTitles(); } } } public String getTooltip(){ String tooltip=""; DecimalFormat formatter = new DecimalFormat("##.##"); if(!first.equals("")){ tooltip=first + "\n"+ last +"\n$" + formatter.format(size); }else{ tooltip=last +"\n$" + formatter.format(size); } return tooltip; } public String getTooltip(int x, int y,int lvl) { return this.getChildNode(x,y,lvl--).getTooltip(); } public color getTooltipColor(int x, int y,int lvl) { return this.getChildNode(x,y,lvl--).getFilling(); } public TMNode getChildNode(int x, int y) { if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); if(child.contains(x,y))return child; } } return this; } public TMNode getChildNode(int x, int y,int lvl) { if(lvl==0)return this; if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); if(child.contains(x,y)){ return child.getChildNode(x,y,lvl-1); } } } return this; } protected boolean contains(int x, int y){ return ((x>area.x && x<(area.x+area.width)) && (y>area.y && y<(area.y+area.height))); } public void setParent(TMNode par){ parent=par; } public TMNode getParent(){ if(parent!=null)return parent; return this; } public String getLast(){ return last; } public TMNode find(String search) { if(last.equals(search))return this; if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); TMNode found= child.find(search); if(found!=null)return found; } } return null; } public void setFilling(String teamname){ filling=#ffffff;colorTitle=#000000; if(teamname.equals("Yankees")){filling=#132448;colorTitle=#cdcdce;} if(teamname.equals("Blue Jays")){filling=#003090;colorTitle=#0090c0;} if(teamname.equals("Red Sox")){filling=#bd3039;colorTitle=#0d2b56;} if(teamname.equals("Orioles")){filling=#d15809;colorTitle=#000000;} if(teamname.equals("Devil Rays")){filling=#006d3e;colorTitle=#303060;} if(teamname.equals("Twins")){filling=#042462;colorTitle=#bc0033;} if(teamname.equals("White Sox")){filling=#000000;colorTitle=#606060;} if(teamname.equals("Royals")){filling=#181ea5;colorTitle=#000000;} if(teamname.equals("Indians")){filling=#c00000;colorTitle=#c0c0c0;} if(teamname.equals("Tigers")){filling=#003060;colorTitle=#f06000;} if(teamname.equals("Athletics")){filling=#003831;colorTitle=#f3ea21;} if(teamname.equals("Angels")){filling=#ba0021;colorTitle=#00234b;} if(teamname.equals("Mariners")){filling=#0c2c56;colorTitle=#005c5c;} if(teamname.equals("Rangers")){filling=#003278;colorTitle=#000000;} if(teamname.equals("Cubs")){filling=#0e3386;colorTitle=#d82427;} if(teamname.equals("Cardinals")){filling=#c41e3a;colorTitle=#f0f000;} if(teamname.equals("Reds")){filling=#c6011f;colorTitle=#000000;} if(teamname.equals("Brewers")){filling=#0a2351;colorTitle=#b6922e;} if(teamname.equals("Pirates")){filling=#000000;colorTitle=#e1b81e;} if(teamname.equals("Astros")){filling=#95322c;colorTitle=#c7c393;} if(teamname.equals("Mets")){filling=#003581;colorTitle=#ff5731;} if(teamname.equals("Phillies")){filling=#e81828;colorTitle=#284898;} if(teamname.equals("Nationals")){filling=#ba122b;colorTitle=#11225b;} if(teamname.equals("Braves")){filling=#0f437c;colorTitle=#af0039;} if(teamname.equals("Marlins")){filling=#000000;colorTitle=#00a8b0;} if(teamname.equals("Dodgers")){filling=#0f3e6f;colorTitle=#87a0c3;} if(teamname.equals("Giants")){filling=#000000;colorTitle=#fd5a1e;} if(teamname.equals("Diamondbacks")){filling=#6335a0;colorTitle=#00646a;} if(teamname.equals("Rockies")){filling=#000000;colorTitle=#303060;} if(teamname.equals("Padres")){filling=#000060;colorTitle=#559dc5;} if(position.equals("P")){ color temp=filling; filling=colorTitle; colorTitle=temp; } } public void setFillingScheme(int scheme) { if(position!="" && position!="Team"){ if(scheme==0|| ((scheme==1) && position.equals("P")) || ((scheme==2) && position.equals("C")) || ((scheme==3) && position.equals("1B")) || ((scheme==4) && position.equals("2B")) || ((scheme==5) && position.equals("SS")) || ((scheme==6) && position.equals("3B")) || ((scheme==7) && position.equals("OF")) ){ setFilling(this.getParent().getLast());//this will be team name for Player parent Nodes }else{ filling=#ffffff;colorTitle=#000000; if(scheme==8){ //age greying float greyfactor=255-((age-20)*10); filling=color(greyfactor,greyfactor,greyfactor); } if(scheme==9){ filling=#000000; if(projection>0){ float projfactor=0; if(position.equals("P")){ float spread=.9*(maxERA-minERA)/2; projfactor=255-(255*(projection-avgERA)/spread); }else{ float spread=.9*(maxOPS-minOPS)/2; projfactor=255+(255*((projection-avgOPS)/spread)); } if(projfactor>255){ filling=color(projfactor-255,0,0); }else{ filling=color(0,0,255-projfactor); } } } } } if(children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); child.setFillingScheme(scheme); } } } protected void drawNodes(int level,PFont font,boolean bTextOnly,boolean bFillOnly) { color borderColor = colorTitle; if(!bTextOnly){ fill(getFilling()); rect(area.x, area.y, area.width, area.height); stroke(borderColor); rect(area.x, area.y, area.width, area.height); } if(!bFillOnly){ int fontsize=128; float fontmultiplier=0.0; fontmultiplier=(1.0f*area.width*area.height)/(width*height); fontsize=10+int(fontmultiplier*fontsize); textFont(font,fontsize); textAlign(CENTER); fill(getColorTitle()); String nodeTitle=getTitle(); if(level==-1 && nodeTitle!=null ){ int texty=area.y+(area.height/2); if(nodeTitle.split("\n").length>2)texty=area.y+(area.height/3); if(area.width*area.height>100)text(nodeTitle, area.x+(area.width/2), texty); } } if(level>-1 && children.size()>0){ TMNode child = null; for (Enumeration e = children(); e.hasMoreElements(); ) { child = (TMNode) e.nextElement(); child.drawNodes(level-1,font,bTextOnly,bFillOnly); } } } }