FloatTable data; float dataMin, dataMax; float plotX1, plotY1; float plotX2, plotY2; float labelX, labelY; int rowCount; int columnCount; int currentColumn = 0; int yearMin, yearMax; int[] years; int yearInterval = 1; int volumeInterval = 250000; PFont plotFont; float barWidth = 5; //float traslado; //float ttt; float plotY1temp; float value2009; void setup() { size(720, 590); data = new FloatTable("sub-nom.tsv"); rowCount = data.getRowCount(); columnCount = data.getColumnCount(); years = int(data.getRowNames()); yearMin = years[0]; yearMax = years[years.length - 1]; dataMin = 0; dataMax = ceil(data.getTableMax() / volumeInterval) * volumeInterval; // Corners of the plotted time series plotX1 = 310; plotX2 = width - 30; labelX = 50; plotY1 = 38; plotY2 = height - 50; labelY = height - 25; float plotW = plotX2 - plotX1; plotFont = createFont("SansSerif", 20); textFont(plotFont); float plotY1temp = plotY1; smooth(); } void draw() { float plotY1temp = 0; background(235); drawTitle(); drawAxisLabels(); noStroke(); fill(255); rectMode(CORNERS); rect(plotX1,plotY1-barWidth,plotX2,plotY2); drawVolumeLabels(); for (int i = 0; i < columnCount; i++) { drawTitleSide(i); drawDataBars(i); } drawLeyenda(); for (int i = 0; i < columnCount; i++) { drawDataHighlight(i); } } void drawTitleSide(int col) { fill(0); textSize(9); textAlign(RIGHT); String title = data.getColumnName(col); float plotY1temp = 19*col; //rect(x,y+plotY1temp+barWidth/2,plotX1,plotY1temp+y-barWidth/2); text(title, plotX1-5, plotY1+barWidth+3+plotY1temp); /*pushMatrix(); translate(0,barWidth*4);*/ } void drawTitle() { fill(0); textSize(15); textAlign(LEFT); String title = "Subvenciones nominativas. Área de las Artes. Ayuntamiento de Madrid 2009-2011"; text(title, 15, plotY1 -12); } void drawLeyenda() { rectMode(CORNER); fill(160,194,234); rect(plotX2-100,plotY2-50,10,10); fill(52,132,234); rect(plotX2-100,plotY2-35,10,10); fill(255,0,0); rect(plotX2-100,plotY2-20,10,10); fill(0); text(2009,plotX2-65,plotY2-41); text(2010,plotX2-65,plotY2-26); text(2011,plotX2-65,plotY2-11); } void drawAxisLabels() { fill(0); textSize(12); textLeading(15); textAlign(RIGHT); //text("Beneficiarios\n\n", plotX1/2+15, plotY1-13); textAlign(CENTER); text("Euros", (plotX1+plotX2)/2, labelY+12); textSize(9); fill(100); textAlign(LEFT); text("kulturometer.org Fuente: Proyecto de presupuestos Ayto. Madrid. 2011", 5, height-8); } void drawYearLabels() { fill(0); textSize(10); textAlign(CENTER); // Use thin, gray lines to draw the grid stroke(255); strokeWeight(1); for (int row = 0; row < rowCount; row++) { if (years[row] % yearInterval == 0) { // float x = map(years[row], yearMin, yearMax, plotX1+plotX1/rowCount, plotX2/rowCount); float x = map(years[row], yearMin, yearMax, plotY2/4, plotY1); text(years[row], plotX1-10, x); //line(x, plotY1, x, plotY2); } } } int volumeIntervalMinor = 5; // Add this above setup() void drawVolumeLabels() { fill(0); textSize(10); textAlign(CENTER); stroke(128); strokeWeight(1); for (float v = dataMin; v <= dataMax; v += volumeIntervalMinor) { if (v % volumeIntervalMinor == 0) { // If a tick mark float y = map(v, dataMin, dataMax, plotX1, plotX2); if (v % volumeInterval == 0) { // If a major tick mark float textOffset = textAscent()/2; // Center vertically if (v == dataMin) { textOffset = 0; // Align by the bottom } else if (v == dataMax) { textOffset = textAscent(); // Align by the top } String cantidad = nfc(floor(v)); text(cantidad, y + textOffset-5, plotY2 + 14); stroke (10); line(y,plotY2 + 4, y, plotY2+1); // Draw major tick stroke(230); line(y,plotY1, y, plotY2); // Draw major tick } else { //line(plotX1 - 2, y, plotX1, y); // Draw minor tick } } } } void drawDataBars(int col) { noStroke(); rectMode(CORNERS); //float traslado = ttt; for (int row = 0; row < rowCount; row++) { if (data.isValid(row, col)) { float value = data.getFloat(row, col); float altobarras = barWidth*2; float y = map(years[row], yearMin, yearMax,plotY1,plotY1+altobarras); float x = map(value, dataMin, dataMax, plotX1, plotX2); float posy = y-barWidth/2; float plotY1temp = 19*col; if (row == 2) { //colorMode(HSB,360,100,100); fill(255,0,0); //colorMode(RGB); } else if (row == 1) { //colorMode(HSB,360,100,100); fill(52,132,234); //colorMode(RGB); } else if ( row ==0) { //colorMode(HSB,360,100,100); fill(160,194,234); //colorMode(RGB); } else { fill(255); } //println("segundo traslado"+traslado); rect(plotX1,plotY1temp+y-barWidth/2,x,y+plotY1temp+barWidth/2); //rect(56,56,traslado,traslado); /* if (mouseY > y+plotY1temp-barWidth/2 && mouseY < plotY1temp+y+barWidth/2) { if (mouseX < x && mouseX > plotX1) { textAlign(LEFT); fill(0); String valueF = nfc(value,0); text(valueF+" euros",x+12,y+plotY1temp+barWidth/2); } }*/ } } } void keyPressed() { if (key == '[') { currentColumn--; if (currentColumn < 0) { currentColumn = columnCount - 1; } } else if (key == ']') { currentColumn++; if (currentColumn == columnCount) { currentColumn = 0; } } } void drawDataHighlight(int col) { noStroke(); rectMode(CORNERS); //float traslado = ttt; for (int row = 0; row < rowCount; row++) { if (data.isValid(row, col)) { float value = data.getFloat(row, col); float value2009 = data.getFloat(0, col); /* Intento de hacer el % en función del año anterior TODO if (row==0) { float value2009 = data.getFloat(2, col); println ("valor 2009 "+value2009); } else { float value2009 = data.getFloat(row, col); println ("valor 2009 2 "+value2009); }*/ float altobarras = barWidth*2.2; float y = map(years[row], yearMin, yearMax,plotY1,plotY1+altobarras); float x = map(value, dataMin, dataMax, plotX1, plotX2); float posy = y-barWidth/2; float plotY1temp = 19*col; if (mouseY > y+plotY1temp-barWidth/2 && mouseY < plotY1temp+y+barWidth/2) { if (mouseX < x && mouseX > plotX1) { textAlign(LEFT); fill(0); String valueF = nfc(value,0); String title = data.getColumnName(col); String txtV = valueF+" € para "+ years[row] +"\n"+ title+"\n"+ceil(value/value2009*100)+ "% respecto año 2009 "; float txtVW= textWidth(txtV); textLeading(12); textSize(11); stroke(130); line(mouseX,mouseY,mouseX+24,mouseY+12); //noFill(); fill(0); noStroke(); rect(plotX1,plotY1temp+y-barWidth/2,x,y+plotY1temp+barWidth/2); rectMode(CORNER); fill(255,180); rect(mouseX+24, mouseY+12,txtVW+8,53); fill(25); text(txtV,mouseX+26,mouseY+24); //text(txtV,x+12,y+plotY1temp+barWidth/2); /*stroke(0,100); strokeWeight(10); point(x, y); float valueA = value/1000; String valueI = nfc(valueA,2); String txtmillones = valueI+ "xxx" + years[row] + ")\n"+ ceil(value/value*100)+ "%"; String millones = valueI+ "millones"; float millw= textWidth(millones); strokeWeight(0); rectMode(CORNER); fill(255,180); rect(x-13, y-52,millw+8,38); triangle(x-3,y-14,x+3,y-14,x,y); fill(0); textSize(10); textAlign(LEFT); textLeading(12); text(txtmillones, x-9, y-42); text(nf(value/1000, 0, 2) + " millones\n (Año " + years[row] + ")\n"+ ceil(value/value*100)+ "%", x-9, y-32); textAlign(LEFT);*/ } } } } }