Loading...
Logo
Processing Forum
This is my first Processing project, so please feel free to point out any additional inaccuracies in the code

This is the issue. 

I've been able to draw a map using imported PShape SVG and grab children from it. I've been able to draw a UI element that cycles through a series of 'selected Days.' But I can't get the current selected day on the UI element to reflect a color change in the Map. 

I'm using this sketch as a reference for what I've done:  http://www.openprocessing.org/sketch/16277

In her example she's importing a csv data sheet to control the size of her specific selected items on the map. I don't think i need to do that, because I'm only selecting the child SVG group and changing the color based on the corrosponding selected number in the UI method. 

There are a few variable defined globally that aren't being used because there were part of my experiment in trying to change the sources sketch into my own.... 

here is a link to the .svg file if you want to include in in a data directory:  http://www.smithvisual.com/medellin/

Copy code
  1.     //Graphic breakdown of multi day routes throughout the city of Medellin, Colombia. 
  2.     //Routes are hand-drawn for aesthetic effect, and so are also loaded as a SVG file. 
  3.     // Improvements
  4.     //control row in numCol dynamically instead of adjusting value every time a new day is added. 
  5.     
  6.     
  7.     PFont font;
  8.     PShape days;
  9.     PShape[] routes;
  10.     int numDays; //count number of days
  11.     String[] daysColor;
  12.     
  13.     String[] rowLabel;
  14.     float[][] metric;
  15.     float[] metricColor;  //defines color of selected route
  16.     float[] metricTotal;
  17.     
  18.     HashMap hm_metric = new HashMap();
  19.     
  20.     //Some variables for graph
  21.     int selDay = 1;
  22.     int numCols = 7;
  23.     
  24.     int marginX = 5;
  25.     int firstDay = 1;
  26.     
  27.     
  28.     void setup(){
  29.      size(550, 700);
  30.      
  31.      font = createFont("Arial",36); 
  32.      //load the map
  33.      days = loadShape("days.svg");
  34.      parseMap();
  35.      metricTotal = new float[numCols];
  36.     
  37.      smooth();
  38.     
  39.     }
  40.     
  41.     void draw() {
  42.       background(220);
  43.       drawRef();
  44.       drawMap();
  45.       drawDays();
  46.     
  47.     }
  48.     
  49.     void parseMap() {
  50.       numDays = days.getChildCount(); //count number of Child routes
  51.       routes = new PShape[numDays];
  52.       //dayCodes = new String[numDays];
  53.       days.disableStyle(); //turn off styles for whole map
  54.       
  55.       //make route object
  56.       for(int i = 0; i < numDays; i++){
  57.         routes[i] = days.getChild(i);
  58.       }
  59.     }
  60.     
  61.     void drawMap() {
  62.       for (int i = 0; i< numDays; i++){
  63.         routes[i].disableStyle();
  64.         fill(100,99);
  65.         stroke(0);
  66.         strokeWeight(.25);
  67.         shape(routes[i]);
  68.       }
  69.     }
  70.     void drawDays(){
  71.       textFont(font, 14);
  72.       fill(0);
  73.       textAlign(CENTER);
  74.       float dayX = marginX + (((width-2*marginX)*selDay)/(numCols-1));
  75.       text(selDay+firstDay-1, dayX, 650);
  76.       strokeWeight(1);
  77.       stroke(0);
  78.       line(dayX, 620, dayX, 590); 
  79.       //draw the line chart of total visitors
  80.       stroke(0);
  81.       noFill();
  82.       strokeWeight(2);
  83.       beginShape();
  84.       for (int i=1; i<numCols-1; i++){
  85.         vertex(marginX + (i*((width-2*marginX)/(numCols-1.01))),
  86.               height-70-((metricTotal[i])));
  87.       }
  88.       
  89.     }
  90.     void keyPressed(){
  91.       int displayDay = selDay + firstDay;
  92.       
  93.       if (keyCode == RIGHT){
  94.         if (selDay == numCols-2) selDay = 1;
  95.         else if (selDay < numCols-2) selDay = selDay+1;  //when moving right don't go further than num years
  96.       }
  97.       else if (keyCode == LEFT){                //when moving left don't go further than first year
  98.         if (selDay == 1) selDay = numCols-2;
  99.         else if (selDay > 1) selDay = selDay -1;
  100.       }
  101.     }
  102.     void drawRef(){
  103.         //write the references information
  104.       noStroke();
  105.       textFont(font,12);
  106.       fill(100);
  107.       textAlign(LEFT);
  108.       text("Use Left and Right Arrow Keys to cycle through Days", 20, height-15);
  109.       textFont(font,10);
  110.     }
  111.     
I was thinking about using an if statement in void draw, but don't really know what to include.  


Thanks for any help you can give me. Very much appreciated. I'll be sure to post the sketch once I'm finished. This forum has already been a massive help. 

Replies(3)

So. After a few days and some help from some friends I figured out the issue. Again. this is my first processing sketch, so please feel free to point out any ineffeciencies or inaccuracies in the code.

I'm posting this incase anyone else has a similar issue. Basically, what solved my problem was assigning a int variable called 'selectedRoute' to the shape call for the drawRoutes function. then associating that with the key press commands. That part was figured out by a friend of mine, Still pretty green at all of this 

Copy code

  1. //Graphic breakdown of multi day routes throughout the city of Medellin, Colombia. 
  2. //Routes are hand-drawn for aesthetic effect, and so are also loaded as a SVG file. 
  3. // Improvements
  4. //control row in numCol dynamically instead of adjusting value every time a new day is added. 

  5. PFont font;
  6. PShape days;
  7. PShape[] routes;
  8. int numDays; //count number of days

  9. String[] dayName;


  10. int selectedRoute= 0;

  11. //Some variables for graph
  12. int selDay = 1;
  13. int numCols = 13;
  14. int displayDay;


  15. int marginX = 5;
  16. int firstDay = 1;


  17. void setup() {
  18.   size(550, 700);
  19.   font = createFont("Arial",36); 
  20.   //load the map
  21.   days = loadShape("days.svg");

  22.   parseMap();

  23.   smooth();
  24. }

  25. void draw() {
  26.   background(20);
  27.   drawRef();
  28.   drawMap();
  29.   drawRoutes();
  30.   drawUI();
  31. }

  32. void parseMap() {
  33.   numDays = days.getChildCount(); //count number of Child routes
  34.   routes = new PShape[numDays];
  35.   dayName = new String [numDays];

  36.   //make route object
  37.   for(int i = 0; i < numDays; i++) {
  38.     routes[i] = days.getChild(i);
  39.     dayName[i] = routes[i].getName();
  40.   }
  41. }



  42. void drawRoutes() {
  43.   for (int i = 0; i< numDays; i++) {
  44.     //disable style to allow for creation in processing
  45.     routes[i].disableStyle();
  46.     stroke(220);
  47.     strokeWeight(.25);
  48.     noFill();
  49.     shape(routes[selectedRoute]);
  50.   }
  51. }
  52. void drawMap() {
  53.   days = loadShape("days.svg");
  54.   shape(days, 0, 0);
  55. }
  56. void drawUI() {
  57.   textFont(font, 14);
  58.   fill(200);
  59.   textAlign(CENTER);
  60.   float dayX = marginX + (((width-2*marginX)*selDay)/(numCols-1));
  61.   text(selDay+firstDay-1, dayX, 650);
  62.   strokeWeight(2);
  63.   stroke(200);
  64.   line(dayX, 631, dayX, 630); 
  65.   //draw the line chart of total visitors
  66.   stroke(200);
  67.   noFill();
  68.   beginShape();
  69. }
  70. void keyPressed() {
  71.   displayDay = selDay + firstDay;

  72.   if (keyCode == RIGHT) {
  73.     if (selDay == numCols-2) selDay = 1;
  74.     else if (selDay < numCols-2) selDay = selDay+1;  //when moving right don't go further than num years
  75.     selectedRoute= (selDay-1)%numDays;
  76.   }
  77.   else if (keyCode == LEFT) {                //when moving left don't go further than first year
  78.     if (selDay == 1) selDay = numCols-2;
  79.     else if (selDay > 1) selDay = selDay -1;
  80.     selectedRoute= (selDay-1)%numDays;
  81.   }
  82. }
  83. void drawRef() {
  84.   //write the references information
  85.   noStroke();
  86.   textFont(font,10);
  87.   fill(200);
  88.   textAlign(LEFT);
  89.   text("Use Left and Right Arrow Keys to cycle through Days", 20, height-15);
  90.   textFont(font,10);
  91. }

OK, for inefficiency, we have a classical here: you load (and parse) days.svg on each frame, in drawMap(). Which isn't useful since you already loaded it in setup(), anyway.
Finally got around to fixing my code for the efficiency issue you outlined. Thanks for the advice PHI. 

this is the snippet of code from void draw map that now relies on void setup for the initialized shape.

Copy code
  1. void drawMap() {
  2.   for (int i = 0; i< numDays; i++) {
  3.     shape(routes[i]);  
  4.   }
  5. }
Pretty easy once I thought about it for a minute.....Took me a while to really understand how these conditionals work with arrays :) Still kind of swatting in the dark....

Hope this helps someone.