Color an object based on strings in a table

edited March 2015 in How To...

I would like to assign the colors of dots in a scatter plot based on the strings from a column in a table. I've simplified the code in the example below. I want to color the dot red if the string from column "educ" is equal to "Type A" and color it green if it is equal to any other value.

The code I've used which does not work is:

color dot;

void draw()
{
  background(255);

  int numRows = dataTable.getRowCount();

  for (int row=0; row<numRows; row++)
  {
    float X = dataTable.getFloat(row, "X");
    float Y   = dataTable.getFloat(row, "Y");

    float xPos        = map(discIncome, 0, 2000, 0, width);
    float yPos        = map(consumed, 0, 2000, height, 0);

    String colorData = dataTable.getString(row,"educ");

    if (colorData == "Type A") 
    {
      color dot = color(255,0,0);
    }
    else
    {
      color dot = color(0,255,0);
    }

      fill(dot);
      ellipse(xPos, yPos, 5, 5);
  }
}

Any help would be appreciated.

Answers

  • Hi GoToLoop,

    Sorry about the formatting and I appreciate your response. I've changed the code but the dot colors still won't change according to the string in the table.

    It now looks like

    color dot;
    
    void draw()
    {
      background(255);
    
      int numRows = dataTable.getRowCount();
    
      for (int row=0; row<numRows; row++)
      {
        float X = dataTable.getFloat(row, "X");
        float Y   = dataTable.getFloat(row, "Y");
    
        float xPos        = map(discIncome, 0, 2000, 0, width);
        float yPos        = map(consumed, 0, 2000, height, 0);
    
        String colorData = dataTable.getString(row,"educ");
        String str1 = "Type A";
    
        if (str1.equals(colorData) == true) 
        {
          color dot = color(255,0,0);
        }
        else
        {
          color dot = color(0,255,0);
        }
    
          fill(dot);
          ellipse(xPos, yPos, 5, 5);
      }
    }
    
  • edited March 2015 Answer ✓

    Ru sure that both "educ" & "Type A" are correct character by character?
    Whether there are any whitespaces before &/or after? :-?

    Here's a more simplified example which relies on TableRow view of the original Table instance.
    Pay close attention whether the acquired values x, y & type are fully correct: :-B

    // forum.processing.org/two/discussion/9759/
    // color-an-object-based-on-strings-in-a-table
    
    for (TableRow tr : dataTable.rows()) {
      float x = tr.getFloat("X"), y = tr.getFloat("Y");
      String type = tr.getString("educ");
    
      fill("Type A".equals(type)? #FF0000 : #00FF00);
      ellipse(x, y, 5, 5);
    
      println("X:", x, "Y:", y, "Type:", type);
    }
    
  • edited March 2015
    if (str1.equals(colorData) == true)
    {
      color dot = color(255,0,0);
    }
    else
    {
      color dot = color(0,255,0);
    }
    

    is wrong, because you declare a dot variable in each part of this comparison, each shadowing the global dot variable.

    Remove the global variable, which has no use, and declare dot outside of the condition:

    color dot = color(0,255,0);
    if (str1.equals(colorData) == true)
    {
      dot = color(255,0,0);
    }
    
Sign In or Register to comment.