Skip to content

Commit

Permalink
Crosshairs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamwills committed Aug 17, 2016
1 parent fb40188 commit 8aceccb
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 47 deletions.
Binary file modified R/inst/java/brunel-core-2.0.jar
Binary file not shown.
Binary file modified R/inst/java/brunel-data-2.0.jar
Binary file not shown.
18 changes: 16 additions & 2 deletions core/src/main/java/org/brunel/build/d3/D3Interaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class D3Interaction {

private static final boolean[] ZOOM_ALL = new boolean[]{true, true};
private static final boolean[] ZOOM_NONE = new boolean[]{false, false};
public static final String DEFAULT_SNAP_DISTANCE = "geom.inner_radius/4";

private final ChartStructure structure; // Chart Structure
private final D3ScaleBuilder scales; // Scales for the chart
Expand Down Expand Up @@ -172,6 +173,11 @@ public void addHandlers(List<Param> interactions) {
if (eventName.equals("mouseover") || eventName.equals("mousemove"))
addFunctionDefinition("mouseout",
"BrunelD3.select(null, this, element, updateAll)", elementEvents);

// And we want a click on the main space to select nothing
if (eventName.equals("click"))
addFunctionDefinition("click", "BrunelD3.select(null, this, element, updateAll)", overlayEvents);

}
} else if (type == Interaction.call) {
// One of call, call:func, call:func:mouseXXX, call:func:snap, call:func:snap:ZZ
Expand All @@ -187,14 +193,22 @@ public void addHandlers(List<Param> interactions) {
addFunctionDefinition(eventName, functionName + "(d, this, element)", elementEvents);
if (eventName.equals("mouseover") || eventName.equals("mousemove"))
addFunctionDefinition("mouseout", functionName + "(null, this, element)", elementEvents);

// And we want a click on the main space to select nothing
if (eventName.equals("click"))
addFunctionDefinition("click", functionName + "(null, c.target, element, 'xy')", overlayEvents);

}
}
}

if (!overlayEvents.isEmpty()) {
// Start each set of overlay commands with a command to find the closest item
for (List<String> e : overlayEvents.values()) {
e.add(0, "var c = BrunelD3.closest(merged, '" + snapInfo[0] + "', " + snapInfo[1] + " )");
if (snapInfo == null)
e.add(0, "var c = BrunelD3.closest(merged, 'xy', " + DEFAULT_SNAP_DISTANCE + " )");
else
e.add(0, "var c = BrunelD3.closest(merged, '" + snapInfo[0] + "', " + snapInfo[1] + " )");
}

out.add("chart.select('rect.overlay')").at(60).comment("Attach handlers to the overlay");
Expand Down Expand Up @@ -260,7 +274,7 @@ private String[] findSnapInfo(Param param) {
}

if (snapFunction != null)
return new String[]{snapFunction, "geom.inner_radius/4"}; // Default to a quarter the space allowed on screen
return new String[]{snapFunction, DEFAULT_SNAP_DISTANCE}; // Default to a quarter the space allowed on screen
else
return null; // No snap
}
Expand Down
33 changes: 25 additions & 8 deletions core/src/main/resources/javascript/Brunel.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}

.brunel .brunel .axis * {
shape-rendering:crispEdges
shape-rendering: crispEdges
}

.brunel .grid {
Expand Down Expand Up @@ -132,27 +132,26 @@
}

.brunel .overlap {
visibility:hidden;
visibility: hidden;
}

.brunel .header {
fill: #0b9bdb;
font-size: 18px;
font-weight:bold;
font-weight: bold;
stroke: none;
pointer-events: none;
}

.brunel .footer {
fill: #888888;
font-size: 12px;
font-weight:normal;
label-location:right;
font-weight: normal;
label-location: right;
stroke: none;
pointer-events: none;
}


.brunel .labels text {
text-shadow: 1px 1px 5px white, -1px -1px 5px white, -1px 1px 5px white, 1px -1px 5px white
}
Expand All @@ -171,7 +170,7 @@
.brunel path.element.guide {
stroke-width: 1px;
stroke: lightGray;
stroke-dasharray:10 5
stroke-dasharray: 10 5
}

.brunel .element.bar, .brunel .element.polygon, .brunel .element.area, .brunel .element.line.filled, .brunel .element.path.filled {
Expand Down Expand Up @@ -238,7 +237,7 @@

.brunel .mark.L4 {
stroke: #a0a0a0;
fill:white;
fill: white;
}

.brunel .label.L1 {
Expand Down Expand Up @@ -355,6 +354,24 @@ div.brunel.tooltip.below:after {
pointer-events: all;
}

.brunel .crosshairs * {
pointer-events: none;
}

.brunel .crosshairs line {
stroke: red;
stroke-dasharray: 8 4;
}

.brunel .crosshairs circle {
stroke: red;
fill: none;
}

.brunel .crosshairs text {
fill: red;
}

.ui-slider {
width: 200px;
}
Expand Down
33 changes: 24 additions & 9 deletions core/src/main/resources/javascript/BrunelD3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1487,19 +1487,14 @@ var BrunelD3 = (function () {
if (g.empty()) {
g = group.append("g").attr("class", "crosshairs");

function style(line, colName, dashing) {
line.style(colName, "red").style("pointer-events", "none").style("stroke-dasharray", dashing || "none");
}

// 4 lines, a central circle, and text tags
for (i = 0; i < 4; i++)
style(g.append("line").attr("class", "dim" + Math.floor(i / 2) + " part" + i), "stroke", "8 4");
style(g.append("circle").style("fill", "none"), "stroke");
style(g.append("text").attr("class", "dim0").attr("dy", "-0.3em"), "fill");
style(g.append("text").attr("class", "dim1").attr("dy", "-0.3em"), "fill");
g.append("line").attr("class", "dim" + Math.floor(i / 2) + " part" + i);
g.append("circle").style("fill", "none");
g.append("text").attr("class", "dim0").attr("dy", "-0.3em");
g.append("text").attr("class", "dim1").attr("dy", "-0.3em");
}


if (!item) {
g.style("visibility", "hidden");
return;
Expand All @@ -1519,6 +1514,26 @@ var BrunelD3 = (function () {
py = box.y + box.height / 2;
}

//Add an inverse to categorical scales
function addInvertFunction(scale) {
scale.invert = function (v) {
// Binary search for closest point
var values = scale.domain(), low = 0, high = values.length - 1, t;
while (high - low > 1) {
t = Math.floor((low + high) / 2);
if (scale(values[t]) > v)
high = t;
else
low = t;
}
// return whichever is the nearest of low and high (which are on either side of the value)
return v - scale(values[low]) < scale(values[high]) - v ? values[low] : values[high];
}
}

if (!scales.x.invert) addInvertFunction(scales.x);
if (!scales.y.invert) addInvertFunction(scales.y);

var x = scales.x.invert(px),
y = scales.y.invert(py),
x1 = scales.x.range()[0],
Expand Down
2 changes: 1 addition & 1 deletion data/src/main/java/org/brunel/data/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static String format(Object o, boolean useGrouping) {
"if (d == 0) return '0';",
"if (Math.abs(d) <= 1e-6 || Math.abs(d) >= 1e8) return $.formatScientific(d);",
"if (Math.abs((d - Math.round(d)) / d) < 1e-9) return $.formatInt(Math.round(d), useGrouping);",
"return $.formatFixed(d, decimalPlaces || 6, useGrouping);"
"return $.formatFixed(d, decimalPlaces == null ? 6 : decimalPlaces, useGrouping);"
})
public static String formatNumeric(double d, Number decimalPlaces, boolean useGrouping) {
if (d == 0) return "0";
Expand Down
1 change: 1 addition & 0 deletions data/src/main/java/org/brunel/data/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public String format(Object v) {
}
if (isProperty("list"))
return ((ItemsList) v).toString((DateFormat) property("dateFormat"));

return v.toString();
}

Expand Down
1 change: 1 addition & 0 deletions data/src/test/java/org/brunel/data/TestField.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public void testFieldNumericFormatting() {
assertEquals("123,456", f.valueFormatted(0));
assertEquals("234,567", f.valueFormatted(1));
assertEquals("345,678", f.valueFormatted(2));
assertEquals("8", f.format(7.9));

f = Fields.makeColumnField("a", "label", new Object[]{0.0});
f = Data.toNumeric(f);
Expand Down
Binary file modified lib/translator-0.4.jar
Binary file not shown.
Loading

0 comments on commit 8aceccb

Please sign in to comment.