2

我希望我能在这里找到一些帮助,我一直在研究和尝试一遍又一遍,但似乎找不到解决方案。我正在使用处理 2.0.3。

我有一个 SVG 文件,它是美国各州的地图。我有一个保存为 .csv 的 Excel 表,其中包含每个州的数据。我一直在尝试找到一种方法来单击显示的 SVG 文件上的状态,并让它从 excel 表中的数据中显示某种图表。

我的问题是:如何使 SVG 文件可点击,以及如何将其链接到数据?我在互联网上看到的解决方案是创建一个不可见的形状层,所有状态的颜色都略有不同,点击鼠标检测颜色将确定颜色并从数据中读取。我试图重新创建我找到的示例,但它们没有意义,我觉得我在墙上。如果有人有任何帮助或建议,那将大有帮助!提前致谢!

到目前为止的代码,大部分改编自http://forum.processing.org/one/topic/need-help-with-mouse-over-text-box-display-on-us-map.html。它不工作......:/

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);
  }
}
4

0 回答 0