Added length function and support for creating custom numeric (range slider) facets.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@48 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-05 19:43:43 +00:00
parent 16dda46a61
commit 3e30ab2a33
4 changed files with 88 additions and 31 deletions

View File

@ -29,6 +29,7 @@ public class RangeFacet implements Facet {
protected double _from; protected double _from;
protected double _to; protected double _to;
protected boolean _selected;
public RangeFacet() { public RangeFacet() {
} }
@ -79,36 +80,49 @@ public class RangeFacet implements Facet {
_mode = o.getString("mode"); _mode = o.getString("mode");
if ("min".equals(_mode)) { if ("min".equals(_mode)) {
_from = o.getDouble("from"); if (o.has("from")) {
_from = o.getDouble("from");
_selected = true;
}
} else if ("max".equals(_mode)) { } else if ("max".equals(_mode)) {
_to = o.getDouble("to"); if (o.has("to")) {
_to = o.getDouble("to");
_selected = true;
}
} else { } else {
_from = o.getDouble("from"); if (o.has("from") && o.has("to")) {
_to = o.getDouble("to"); _from = o.getDouble("from");
_to = o.getDouble("to");
_selected = true;
}
} }
} }
@Override @Override
public RowFilter getRowFilter() { public RowFilter getRowFilter() {
if ("min".equals(_mode)) { if (_selected) {
return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { if ("min".equals(_mode)) {
protected boolean checkValue(double d) { return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) {
return d >= _from; protected boolean checkValue(double d) {
return d >= _from;
};
}; };
}; } else if ("max".equals(_mode)) {
} else if ("max".equals(_mode)) { return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) {
return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { protected boolean checkValue(double d) {
protected boolean checkValue(double d) { return d <= _to;
return d <= _to; };
}; };
}; } else {
return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) {
protected boolean checkValue(double d) {
return d >= _from && d <= _to;
};
};
}
} else { } else {
return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { return null;
protected boolean checkValue(double d) { }
return d >= _from && d <= _to;
};
};
}
} }
@Override @Override
@ -127,6 +141,14 @@ public class RangeFacet implements Facet {
_step = index.getStep(); _step = index.getStep();
_baseBins = index.getBins(); _baseBins = index.getBins();
if (_selected) {
_from = Math.max(_from, _min);
_to = Math.min(_to, _max);
} else {
_from = _min;
_to = _max;
}
ExpressionNumericRowBinner binner = ExpressionNumericRowBinner binner =
new ExpressionNumericRowBinner(_eval, _cellIndex, index); new ExpressionNumericRowBinner(_eval, _cellIndex, index);

View File

@ -17,6 +17,7 @@ import com.metaweb.gridworks.expr.functions.IsBlank;
import com.metaweb.gridworks.expr.functions.IsNotBlank; import com.metaweb.gridworks.expr.functions.IsNotBlank;
import com.metaweb.gridworks.expr.functions.IsNotNull; import com.metaweb.gridworks.expr.functions.IsNotNull;
import com.metaweb.gridworks.expr.functions.IsNull; import com.metaweb.gridworks.expr.functions.IsNull;
import com.metaweb.gridworks.expr.functions.Length;
import com.metaweb.gridworks.expr.functions.Not; import com.metaweb.gridworks.expr.functions.Not;
import com.metaweb.gridworks.expr.functions.Or; import com.metaweb.gridworks.expr.functions.Or;
import com.metaweb.gridworks.expr.functions.Replace; import com.metaweb.gridworks.expr.functions.Replace;
@ -44,6 +45,7 @@ public class Parser {
functionTable.put("substring", new Slice()); functionTable.put("substring", new Slice());
functionTable.put("replace", new Replace()); functionTable.put("replace", new Replace());
functionTable.put("split", new Split()); functionTable.put("split", new Split());
functionTable.put("length", new Length());
functionTable.put("and", new And()); functionTable.put("and", new And());
functionTable.put("or", new Or()); functionTable.put("or", new Or());

View File

@ -0,0 +1,27 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class Length implements Function {
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object v = args[0];
if (v != null) {
if (v.getClass().isArray()) {
Object[] a = (Object[]) v;
return a.length;
} else {
String s = (v instanceof String ? (String) v : v.toString());
return s.length();
}
}
}
return null;
}
}

View File

@ -370,7 +370,11 @@ DataTableView.prototype._createMenuForColumnHeader = function(column, index, elm
}, },
{ {
label: "Custom Text Facet ...", label: "Custom Text Facet ...",
click: function() { self._doFilterByExpressionPrompt(column); } click: function() { self._doFilterByExpressionPrompt(column, "value", "list"); }
},
{
label: "Custom Numeric Facet ...",
click: function() { self._doFilterByExpressionPrompt(column, "value", "range"); }
}, },
{}, {},
{ {
@ -637,21 +641,23 @@ DataTableView.prototype._createMenuForColumnHeader = function(column, index, elm
], elmt, { width: "120px", horizontal: false }); ], elmt, { width: "120px", horizontal: false });
}; };
DataTableView.prototype._doFilterByExpressionPrompt = function(column, expression) { DataTableView.prototype._doFilterByExpressionPrompt = function(column, expression, type) {
var self = this; var self = this;
DataTableView.promptExpressionOnVisibleRows( DataTableView.promptExpressionOnVisibleRows(
column, column,
"Custom Filter on " + column.headerLabel, "Custom Filter on " + column.headerLabel,
"value", expression,
function(expression) { function(expression) {
ui.browsingEngine.addFacet( var config = {
"list", "name" : column.headerLabel + ": " + expression,
{ "cellIndex" : column.cellIndex,
"name" : column.headerLabel + ": " + expression, "expression" : expression
"cellIndex" : column.cellIndex, };
"expression" : expression if (type == "range") {
} config.mode = "range";
); }
ui.browsingEngine.addFacet(type, config);
} }
); );
}; };