Merge pull request #2140 from OpenRefine/issue-2117-scatterplot

Fix scatterplot drawing command
This commit is contained in:
Antonin Delpeuch 2019-09-13 13:50:48 +01:00 committed by GitHub
commit e935730c6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 138 additions and 4 deletions

View File

@ -104,7 +104,7 @@ public class ScatterplotFacet implements Facet {
protected int rotation;
@JsonIgnore
protected double l;
protected double l = 1.;
@JsonProperty(DOT)
protected double dot;

View File

@ -47,6 +47,7 @@ import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.browsing.Engine;
import com.google.refine.browsing.FilteredRows;
@ -101,9 +102,9 @@ public class GetScatterplotCommand extends Command {
public int size = 100;
@JsonProperty(ScatterplotFacet.DOT)
double dot = 100;
@JsonProperty(ScatterplotFacet.DIM_X)
@JsonIgnore
public int dim_x = ScatterplotFacet.LIN;
@JsonProperty(ScatterplotFacet.DIM_Y)
@JsonIgnore
public int dim_y = ScatterplotFacet.LIN;
@JsonProperty(ScatterplotFacet.ROTATION)
public int rotation = ScatterplotFacet.NO_ROTATION;
@ -119,6 +120,36 @@ public class GetScatterplotCommand extends Command {
public String columnName_y = "";
@JsonProperty(ScatterplotFacet.Y_EXPRESSION)
public String expression_y = "value";
@JsonProperty(ScatterplotFacet.DIM_X)
public String getDimX() {
return dim_x == ScatterplotFacet.LIN ? "lin" : "log";
}
@JsonProperty(ScatterplotFacet.DIM_Y)
public String getDimY() {
return dim_y == ScatterplotFacet.LIN ? "lin" : "log";
}
@JsonProperty(ScatterplotFacet.DIM_X)
public void setDimX(String dim) {
dim_x = dim.equals("lin") ? ScatterplotFacet.LIN : ScatterplotFacet.LOG;
}
@JsonProperty(ScatterplotFacet.DIM_Y)
public void setDimY(String dim) {
dim_y = dim.equals("lin") ? ScatterplotFacet.LIN : ScatterplotFacet.LOG;
}
// rotation can be set to "none" (a JSON string) in which case it should be ignored
@JsonProperty(ScatterplotFacet.ROTATION)
public void setRotation(Object rotation) {
try {
this.rotation = Integer.parseInt(rotation.toString());
} catch(NumberFormatException e) {
;
}
}
}
public void draw(OutputStream output, Project project, Engine engine, PlotterConfig o) throws IOException {

View File

@ -26,6 +26,9 @@
******************************************************************************/
package com.google.refine.browsing.facets;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import org.testng.annotations.Test;
@ -34,6 +37,7 @@ import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.refine.RefineTest;
import com.google.refine.browsing.Engine;
import com.google.refine.browsing.RowFilter;
import com.google.refine.browsing.facets.ScatterplotFacet;
import com.google.refine.browsing.facets.ScatterplotFacet.ScatterplotFacetConfig;
import com.google.refine.model.Cell;
@ -102,7 +106,11 @@ public class ScatterplotFacetTests extends RefineTest {
ScatterplotFacet facet = config.apply(project);
facet.computeChoices(project, engine.getAllFilteredRows());
TestUtils.isSerializedTo(facet, facetJson);
RowFilter filter = facet.getRowFilter(project);
assertTrue(filter.filterRow(project, 0, project.rows.get(0)));
assertFalse(filter.filterRow(project, 1, project.rows.get(1)));
assertTrue(filter.filterRow(project, 3, project.rows.get(3)));
}
}

View File

@ -0,0 +1,95 @@
package com.google.refine.commands.browsing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.Assert;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.refine.browsing.facets.ScatterplotFacet;
import com.google.refine.commands.Command;
import com.google.refine.commands.browsing.GetScatterplotCommand;
import com.google.refine.util.ParsingUtilities;
public class ScatterplotDrawCommandTests {
protected HttpServletRequest request = null;
protected HttpServletResponse response = null;
protected StringWriter writer = null;
protected Command command = null;
@BeforeMethod
public void setUp() {
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
command = new GetScatterplotCommand();
writer = new StringWriter();
try {
when(response.getWriter()).thenReturn(new PrintWriter(writer));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String configJson = "{"
+ "\"name\":\"a (x) vs. b (y)\","
+ "\"cx\":\"a\","
+ "\"cy\":\"b\","
+ "\"l\":150,"
+ "\"ex\":\"value\","
+ "\"ey\":\"value\","
+ "\"dot\":0.8,"
+ "\"dim_x\":\"log\","
+ "\"dim_y\":\"lin\","
+ "\"type\":\"scatterplot\","
+ "\"from_x\":1,"
+ "\"to_x\":2,"
+ "\"from_y\":3,"
+ "\"to_y\":4,"
+ "\"color\":\"ff6a00\""
+ "}";
public static String configJsonWithNone = "{"
+ "\"name\":\"b (x) vs. y (y)\","
+ "\"cx\":\"b\","
+ "\"cy\":\"y\","
+ "\"l\":150,"
+ "\"ex\":\"value\","
+ "\"ey\":\"value\","
+ "\"dot\":1.4,"
+ "\"dim_x\":\"lin\","
+ "\"dim_y\":\"lin\","
+ "\"r\":\"none\","
+ "\"type\":\"scatterplot\","
+ "\"from_x\":0,"
+ "\"to_x\":0,"
+ "\"from_y\":0,"
+ "\"to_y\":0,"
+ "\"color\":\"ff6a00\"}";
@Test
public void testParseConfig() throws JsonParseException, JsonMappingException, IOException {
GetScatterplotCommand.PlotterConfig config = ParsingUtilities.mapper.readValue(configJson, GetScatterplotCommand.PlotterConfig.class);
Assert.assertEquals("a", config.columnName_x);
Assert.assertEquals("b", config.columnName_y);
Assert.assertEquals(ScatterplotFacet.LOG, config.dim_x);
Assert.assertEquals(ScatterplotFacet.LIN, config.dim_y);
}
@Test
public void testParseConfigWithNone() throws JsonParseException, JsonMappingException, IOException {
GetScatterplotCommand.PlotterConfig config = ParsingUtilities.mapper.readValue(configJsonWithNone, GetScatterplotCommand.PlotterConfig.class);
Assert.assertEquals(0, config.rotation);
}
}