/* Please note that Much of this code was adapted from Ben Fry's * Treemap example in chapter 7 of his book: * Visualizing Data, First Edition by Ben Fry. Copyright 2008 Ben Fry * ISBN: 9780596514556 */ /* ** ======================================================== ** MAT259 Visualizaing Data | Winter 2011 | George Legrady ** ======================================================= ** Project 2 : Top 100 Most Checked Out Items from 2005 to 2010 ** ============================================================== ** Nichole Stockman ** ================ ** ** ** ===================================================================== ** For information on how the data was parsed, how the application works ** and some key notes and observations, please see the README file.` ** Thank you! ** ===================================================================== */ class Item extends SimpleMapItem { FolderItem parent; String name; int type; int level; color c; float hue; float brightness; float textPadding = 8; float boxLeft, boxTop; float boxRight, boxBottom; Item(FolderItem parent, String name, int type, int size, int level, int order) { this.parent = parent; this.name = name.trim(); this.type = type; this.size = size; this.order = order; this.level = level; } void updateColors() { if (parent != null) { hue = map(order, 0, parent.getItemCount(), 0, 360); } brightness = 80; colorMode(HSB, 360, 100, 100); if (parent == zoomItem) { c = color(hue, 80, 80); } else if (parent != null) { if(type == cd){ brightness = 80; }else if (type == dvd){ brightness = 55; }else{ brightness = 30; } c = color(parent.hue, 80, brightness); } colorMode(RGB, 255); } void calcBox() { boxLeft = zoomBounds.spanX(x, sideOffset, treemapWidth); boxRight = zoomBounds.spanX(x+w, sideOffset, treemapWidth); boxTop = zoomBounds.spanY(y, topOffset, treemapHeight); boxBottom = zoomBounds.spanY(y+h, topOffset, treemapHeight); } public void draw() { calcBox(); fill(c); rect(boxLeft, boxTop, boxRight, boxBottom); if(mouseInside()){ rolloverItem = this; } if (textFits()) { drawTitle(); } else{ drawShortTitle(); } } void drawInfoBox(){ float x = mouseX + 25; float y = mouseY + 50; int borderThickness = 5; int textOffset = borderThickness + 5; int textSpacing = 3; float textHeight = textAscent() + textDescent(); String typeString = null; if(type == 0){ typeString = "CD"; }else if (type == 1){ typeString = "DVD"; }else if (type == 2){ typeString = "BOOK"; }else if (type == 3){ typeString = "Folder"; } float width = max(textWidth("Title: ") + textWidth(name) + 2*textOffset, textWidth("Type: ") + textWidth(typeString) + 2*textOffset, textWidth("TotalCheckouts: " + (int)(size)) + 2*textOffset); float height = textAscent() + textDescent() + 2*textOffset + 2*(textHeight + textSpacing); if(x + width > treemapWidth + sideOffset){ x = treemapWidth + sideOffset - width; } if(y + height > treemapHeight + topOffset){ y = treemapHeight + topOffset - height; } //border fill(100); quad(x, y, x + width, y, x + width, y + height, x, y + height); //body fill(220); quad(x + borderThickness, y + borderThickness, x + width - borderThickness, y + borderThickness, x + width - borderThickness, y + height - borderThickness, x + borderThickness, y + height - borderThickness); textAlign(LEFT, TOP); fill(50); text("Title: " + name, x + textOffset, y + textOffset); text("Type: " + typeString, x + textOffset, y + textOffset + (textHeight + textSpacing)); text("Total Checkouts: " + (int)(size), x + textOffset, y + textOffset + 2*(textHeight + textSpacing)); } boolean textFits() { float wide = textWidth(name) + textPadding*2; float high = textAscent() + textDescent() + textPadding*2; return (boxRight - boxLeft > wide) && (boxBottom - boxTop > high); } void drawTitle() { fill(255, 200); float middleX = (boxLeft + boxRight) / 2; float middleY = (boxTop + boxBottom) / 2; if (middleX > 0 && middleX < width && middleY > 0 && middleY < height) { if (boxLeft + textWidth(name) + textPadding*2 > width) { textAlign(RIGHT); text(name, width - textPadding, boxBottom - textPadding); } else { textAlign(LEFT); text(name, boxLeft + textPadding, boxBottom - textPadding); } } } void drawShortTitle(){ String shortName = name; for(int i = 0; i < name.length(); i ++){ shortName = name.substring(0, i) + "..."; if(i > 0 && textWidth(shortName) + 2*textPadding > (boxRight - boxLeft)){ shortName = name.substring(0, i-1) + "..."; break; } } textAlign(LEFT, BOTTOM); fill(255); text(shortName, boxLeft + textPadding, boxBottom - textPadding); } boolean mouseInside() { return (mouseX > boxLeft && mouseX < boxRight && mouseY > boxTop && mouseY < boxBottom); } boolean mousePressed() { if (mouseInside()) { if (mouseButton == LEFT) { parent.zoomIn(); return true; } else if (mouseButton == RIGHT) { if (parent == zoomItem) { parent.zoomOut(); } else { parent.hideContents(); } return true; } } return false; } }