问题
I am hoping I can find some help here, I've been researching and trying over and again but can't seem to find a solution. I am using processing 2.0.3.
I have an SVG file that is a map of US states. I have an excel sheet saved as a .csv with data for each state. I've been trying to find a way to click on the state on the displayed SVG file, and have it display some sort of graph from the data in the excel sheet.
My questions are: How do I make the SVG file clickable, and how do I link it to the data? The solution I've seen around the internet is to create an invisible shape layer with all the states having a slightly different color, detecting that color on a click of the mouse will determine the color and read it from the data. I've attempted to recreate the examples I found but they are not making sense and I feel like I am at a wall. If anyone has any help or suggestions it would help a ton! Thanks in advance!
Code so far, most of it adapted from http://forum.processing.org/one/topic/need-help-with-mouse-over-text-box-display-on-us-map.html. Its not working... :/
PShape states;
PShape buff;
Table crime;
PGraphics pg;
int rowCount;
int start = 100;
int yrButton = 0;
float crimeButton = 0;
boolean mouseShowText = false;
String[] textToShow = {"HI","RI","CT","MA","ME","NH","VT","NY","NJ","FL","NC","OH","IN","IA",
"CO","NV","PA","DE","MD","MI","WA","CA","OR","IL","MN","WI","DC","NM","VA","AK","GA","AL",
"TN","AZ","UT","ND","SD","NE","MS","MO","AR","OK","KS","LA"};
void setup(){
size(1000,700);
crime = loadTable("crime.csv");
states = loadShape("states.svg");
rowCount = crime.getRowCount();
pg = createGraphics(700,500);
pg.beginDraw();
pg.background(0);
makeHiddenMap(textToShow);
pg.endDraw();
noLoop();
}
void draw(){
background(222,250,248);
fill(200);
rect(0,0,575,400);
shape(states,0,0,700,500);
}
void makeHiddenMap(String[] states){
pg.stroke(255);
for(int i=0;i<states.length;i++){
PShape buff = states.getChild(states[i]);
buff.disableStyle();
pg.fill(count+10,0,0);
pg.noStroke();
pg.shape(buff,0,0);
textToShow[count] = states[i] + winText;
count++;
}
}
void showMouseOver(){
String textMouseOver=textToShow[numberOfState];
if (!textMouseOver.equals("")){
float posX=textX;
float posY=textY;
if (posX+textWidth(textMouseOver)+10 > width){
posX=width-textWidth(textMouseOver)-12;
}
fill(255,255,44);
rect(posX+14,posY+4,textWidth(textMouseOver)+5,20);
fill(0);
text(textMouseOver,posX+16,posY+4+16);
}
}
来源:https://stackoverflow.com/questions/19894346/using-processing-creating-interactivity-with-an-svg