Skip to content

Commit

Permalink
Revise formatAxis to use not just the first gap, but the median of al…
Browse files Browse the repository at this point in the history
…l gaps
  • Loading branch information
kbroman committed Jan 7, 2022
1 parent 0da7de5 commit 69ab1b8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 33 deletions.
88 changes: 66 additions & 22 deletions d3panels.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,77 @@
!function() { // encapsulate d3panels functions
var d3panels = {
version: "1.8.0"
version: "1.8.1"
};
"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// A variety of utility functions used by the different panel functions
// determine rounding of axis labels
var modulo = function modulo(a, b) {
return (+a % (b = +b) + b) % b;
};

d3panels.formatAxis = function (d) {
var extra_digits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var gap, ndig; // gap between values
var gap, i, ndig, x; // determine typical gap between values
//# remove nulls and sort

d = function () {
var k, len, results;
results = [];

for (k = 0, len = d.length; k < len; k++) {
x = d[k];

if (d[0] != null) {
gap = d[1] - d[0];
} else if (d.length > 2) {
// allow first value to be NULL
gap = d[2] - d[1];
if (x != null) {
results.push(x);
}
}

return results;
}();

d.sort();

if (d.length === 0) {
gap = 0;
} else if (d.length === 1) {
gap = d[0];
} else {
gap = d[1];
} // turn gap into number of digits
// take differences
d = function () {
var k, ref, results;
results = [];

for (i = k = 1, ref = d.length - 1; 1 <= ref ? k <= ref : k >= ref; i = 1 <= ref ? ++k : --k) {
results.push(d[i] - d[i - 1]);
}

ndig = Math.floor(d3panels.log10(Math.abs(gap)));
return results;
}(); // get median


d.sort();
console.log(d);

if (modulo(d.length, 2)) {
// odd number of values
gap = d[(d.length - 1) / 2];
} else {
gap = (d[d.length / 2 - 1] + d[d.length / 2]) / 2;
}
}

console.log(gap); // turn gap into number of digits

ndig = Math.round(d3panels.log10(Math.abs(gap)));

if (ndig > 0) {
ndig = 0;
}

ndig = Math.abs(ndig) + extra_digits; // function to return
ndig = Math.abs(ndig) + extra_digits;
console.log("ndig: " + ndig); // function to return

return function (val) {
if (val != null && val !== "NA") {
Expand Down Expand Up @@ -1073,7 +1117,7 @@ d3panels.object_position = function (object) {
};
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// lod2dheatmap: heat map panel, with the two dimensions broken into chromosomes
d3panels.lod2dheatmap = function (chartOpts) {
var cellSelect, cells, celltip, chart, chrGap, colors, equalCells, height, hilitcolor, margin, nullcolor, oneAtTop, ref, ref1, ref10, ref11, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, svg, tipclass, width, xscale, yscale, zlim, zscale, zthresh;
Expand Down Expand Up @@ -2880,7 +2924,7 @@ d3panels.cichart = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// crosstab: Display of a cross-tabulation (a two-way table)
d3panels.crosstab = function (chartOpts) {
var bordercolor, cellPad, chart, colrect, fontsize, height, hilitcolor, margin, rectcolor, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, rowrect, svg, title, titlepos, width;
Expand Down Expand Up @@ -3764,7 +3808,7 @@ d3panels.dotchart = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// heatmap: heat map panel
d3panels.heatmap = function (chartOpts) {
var cellSelect, cells, celltip, chart, colors, hilitcolor, margin, nullcolor, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, svg, tipclass, xlim, xscale, ylim, yscale, zlim, zscale, zthresh;
Expand Down Expand Up @@ -4281,7 +4325,7 @@ d3panels.lodchart = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// add_lodcurve: add lod curve to a lodchart() chart
d3panels.add_lodcurve = function (chartOpts) {
var chart, g, horizontal, linecolor, linedash, linewidth, markerSelect, markertip, pointcolor, pointsize, pointstroke, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, tipclass;
Expand Down Expand Up @@ -4484,7 +4528,7 @@ d3panels.add_lodcurve = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// add_curves: add curves to a plain panelframe() chart
d3panels.add_curves = function (chartOpts) {
var chart, curveGroup, curves, indtip, linecolor, linecolorhilit, linewidth, linewidthhilit, ref, ref1, ref2, ref3, ref4, tipclass;
Expand Down Expand Up @@ -4977,7 +5021,7 @@ d3panels.add_points = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// lodheatmap: heat map for multiple LOD curves (with one dimension broken by chromosomes)
d3panels.lodheatmap = function (chartOpts) {
var cellSelect, cells, celltip, chart, chrGap, colors, equalCells, height, hilitcolor, horizontal, margin, nullcolor, ref, ref1, ref10, ref11, ref12, ref13, ref14, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, svg, tipclass, width, xlab, xscale, ylab, ylim, yscale, zlim, zscale, zthresh;
Expand Down Expand Up @@ -5407,7 +5451,7 @@ d3panels.lodheatmap = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// mapchart: plot of a genetic marker map
d3panels.mapchart = function (chartOpts) {
var chart, horizontal, linecolor, linecolorhilit, linewidth, markerSelect, martip, ref, ref1, ref10, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, shiftStart, svg, tickwidth, tipclass, v_over_h, xlab, xlineOpts, xscale, ylab, yscale;
Expand Down Expand Up @@ -6618,7 +6662,7 @@ d3panels.histchart = function (chartOpts) {
return chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// d3-based slider
d3panels.slider = function (chartOpts) {
var buttoncolor, buttondotcolor, buttondotsize, buttonround, buttonsize, buttonstroke, _chart, height, margin, nticks, rectcolor, rectheight, ref, ref1, ref10, ref11, ref12, ref13, ref14, ref15, ref16, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, slider_svg, stopindex, textsize, tickgap, tickheight, ticks, ticks_at_stops, value, width;
Expand Down Expand Up @@ -6848,7 +6892,7 @@ d3panels.slider = function (chartOpts) {
return _chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// a d3-based double-slider (or range slider)
d3panels.double_slider = function (chartOpts) {
var buttoncolor, buttondotcolor, buttondotsize, buttonround, buttonsize, buttonstroke, _chart, height, margin, nticks, rectcolor, rectheight, ref, ref1, ref10, ref11, ref12, ref13, ref14, ref15, ref16, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, slider_svg, stopindex, textsize, tickgap, tickheight, ticks, ticks_at_stops, value, width;
Expand Down Expand Up @@ -7122,7 +7166,7 @@ d3panels.double_slider = function (chartOpts) {
return _chart;
};"use strict";

// Generated by CoffeeScript 2.5.1
// Generated by CoffeeScript 2.6.1
// tool tips
d3panels.tooltip_create = function (selection, objects, options, tooltip_func) {
var direction, fill, fontcolor, fontsize, in_duration, out_duration, pad, ref, ref1, ref2, ref3, ref4, tipclass, tipdiv, tipgroup, triChar, tridiv;
Expand Down
2 changes: 1 addition & 1 deletion d3panels.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3panels",
"version": "1.8.0",
"version": "1.8.1",
"description": "D3-based graphics panels",
"author": "Karl Broman <[email protected]> (https://kbroman.org)",
"repository": "github:kbroman/d3panels",
Expand Down
2 changes: 1 addition & 1 deletion src/d3panels_top.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!function() { // encapsulate d3panels functions
var d3panels = {
version: "1.8.0"
version: "1.8.1"
};
30 changes: 22 additions & 8 deletions src/panelutil.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@
# determine rounding of axis labels
d3panels.formatAxis = (d, extra_digits=0) ->

# gap between values
if d[0]?
gap = d[1]-d[0]
else if d.length > 2 # allow first value to be NULL
gap = d[2]-d[1]
else # if we just have one non-null value
gap = d[1]
# determine typical gap between values
## remove nulls and sort
d = (x for x in d when x?)
d.sort()
if d.length == 0
gap = 0
else if d.length == 1
gap = d[0]
else
# take differences
d = (d[i]-d[i-1] for i in [1..(d.length-1)])
# get median
d.sort()
console.log(d)
if d.length %% 2 # odd number of values
gap = d[(d.length-1)/2]
else
gap = (d[d.length/2-1] + d[d.length/2])/2
console.log(gap)

# turn gap into number of digits
ndig = Math.floor( d3panels.log10(Math.abs(gap)) )
ndig = Math.round( d3panels.log10(Math.abs(gap)) )
ndig = 0 if ndig > 0
ndig = Math.abs(ndig) + extra_digits

console.log("ndig: #{ndig}")

# function to return
(val) ->
return d3.format(".#{ndig}f")(val) if val? and val != "NA"
Expand Down

0 comments on commit 69ab1b8

Please sign in to comment.