We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
We need to use method equals() when comparing String contents: [-X
https://processing.org/reference/String_equals_.html
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
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
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: