Skip to content

Commit a8aff32

Browse files
committed
Fix a problem displaying domain Y coordinates when using a log scale that straddled the origin. Closes #45.
1 parent 62d2836 commit a8aff32

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

toyplot/html.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,40 @@ def default(self, obj):
145145
return x < 0 ? -1 : x > 0 ? 1 : 0;
146146
}
147147
148-
function log_n(x, base)
148+
function _mix(a, b, amount)
149+
{
150+
return ((1.0 - amount) * a) + (amount * b);
151+
}
152+
153+
function _log(x, base)
149154
{
150155
return Math.log(Math.abs(x)) / Math.log(base);
151156
}
152157
153-
function mix(a, b, amount)
158+
function _in_range(a, x, b)
154159
{
155-
return ((1.0 - amount) * a) + (amount * b);
160+
var left = Math.min(a, b);
161+
var right = Math.max(a, b);
162+
return left <= x && x <= right;
156163
}
157164
158165
function to_domain(projection, range)
159166
{
160167
for(var i = 0; i != projection.length; ++i)
161168
{
162169
var segment = projection[i];
163-
if(Math.min(segment.range.bounds.min, segment.range.bounds.max) <= point[0] && point[0] < Math.max(segment.range.bounds.min, segment.range.bounds.max))
170+
if(_in_range(segment.range.bounds.min, range, segment.range.bounds.max))
164171
{
165-
var amount = (range - segment.range.min) / (segment.range.max - segment.range.min);
166172
if(segment.scale == "linear")
167173
{
168-
return mix(segment.domain.min, segment.domain.max, amount)
174+
var amount = (range - segment.range.min) / (segment.range.max - segment.range.min);
175+
return _mix(segment.domain.min, segment.domain.max, amount)
169176
}
170177
else if(segment.scale[0] == "log")
171178
{
179+
var amount = (range - segment.range.min) / (segment.range.max - segment.range.min);
172180
var base = segment.scale[1];
173-
return sign(segment.domain.min) * Math.pow(base, mix(log_n(Math.abs(segment.domain.min), base), log_n(Math.abs(segment.domain.max), base), amount));
181+
return sign(segment.domain.min) * Math.pow(base, _mix(_log(segment.domain.min, base), _log(segment.domain.max, base), amount));
174182
}
175183
}
176184
}
@@ -506,14 +514,21 @@ def render(canvas, fobj=None, animation=False):
506514
xml.SubElement(controls, "script").text = _export_data_tables.substitute(root_id=root.get("id"), data_tables=json.dumps(data_tables))
507515

508516
# Provide interactive mouse coordinates.
517+
def _flip_infinities(value):
518+
if numpy.isinf(value):
519+
return -value
520+
return value
521+
509522
if context._cartesian_axes:
510523
cartesian_axes = dict()
511524
for key, axes in context._cartesian_axes.items():
512525
cartesian_axes[key] = dict()
513-
for axis, projection in [("x", axes._xprojection), ("y", axes._yprojection)]:
514-
cartesian_axes[key][axis] = list()
515-
for segment in projection._segments:
516-
cartesian_axes[key][axis].append({"scale":segment.scale, "domain":{"min":segment.domain.min, "max":segment.domain.max, "bounds":{"min":segment.domain.bounds.min, "max":segment.domain.bounds.max}}, "range":{"min":segment.range.min, "max":segment.range.max, "bounds":{"min":segment.range.bounds.min, "max":segment.range.bounds.max}}})
526+
cartesian_axes[key]["x"] = list()
527+
for segment in axes._xprojection._segments:
528+
cartesian_axes[key]["x"].append({"scale":segment.scale, "domain":{"min":segment.domain.min, "max":segment.domain.max, "bounds":{"min":segment.domain.bounds.min, "max":segment.domain.bounds.max}}, "range":{"min":segment.range.min, "max":segment.range.max, "bounds":{"min":segment.range.bounds.min, "max":segment.range.bounds.max}}})
529+
cartesian_axes[key]["y"] = list()
530+
for segment in axes._yprojection._segments:
531+
cartesian_axes[key]["y"].append({"scale":segment.scale, "domain":{"min":segment.domain.min, "max":segment.domain.max, "bounds":{"min":segment.domain.bounds.min, "max":segment.domain.bounds.max}}, "range":{"min":segment.range.min, "max":segment.range.max, "bounds":{"min":_flip_infinities(segment.range.bounds.min), "max":_flip_infinities(segment.range.bounds.max)}}})
517532

518533
xml.SubElement(controls, "script").text = _show_mouse_coordinates.substitute(root_id=root.get("id"), cartesian_axes=json.dumps(cartesian_axes, cls=_NumpyJSONEncoder, sort_keys=True))
519534

0 commit comments

Comments
 (0)