From 71fb2a84e47260c70b06c5f1418eec83802fb2e9 Mon Sep 17 00:00:00 2001 From: Ben Newhouse Date: Thu, 12 Mar 2020 16:51:52 -0400 Subject: [PATCH] Add axis labels for Rectangular and Clock Phylogeny trees. --- .../tree/phyloTree/defaultParams.js | 2 +- src/components/tree/phyloTree/grid.js | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/components/tree/phyloTree/defaultParams.js b/src/components/tree/phyloTree/defaultParams.js index 89d7969d0..e2d97ce68 100644 --- a/src/components/tree/phyloTree/defaultParams.js +++ b/src/components/tree/phyloTree/defaultParams.js @@ -11,7 +11,7 @@ export const createDefaultParams = () => ({ tickLabelFill: darkGrey, minorTicks: 4, orientation: [1, 1], - margins: {left: 25, right: 15, top: 10, bottom: 30}, + margins: {left: 30, right: 15, top: 10, bottom: 40}, showGrid: true, fillSelected: "#A73", radiusSelected: 5, diff --git a/src/components/tree/phyloTree/grid.js b/src/components/tree/phyloTree/grid.js index cb93c36dc..820be785d 100644 --- a/src/components/tree/phyloTree/grid.js +++ b/src/components/tree/phyloTree/grid.js @@ -14,6 +14,9 @@ export const hideGrid = function hideGrid() { if ("gridText" in this.groups) { this.groups.gridText.selectAll("*").style('visibility', 'hidden'); } + if ("axisText" in this.groups) { + this.groups.axisText.selectAll("*").style('visibility', 'hidden'); + } }; const addSVGGroupsIfNeeded = (groups, svg) => { @@ -29,6 +32,9 @@ const addSVGGroupsIfNeeded = (groups, svg) => { if (!("gridText" in groups)) { groups.gridText = svg.append("g").attr("id", "gridText"); } + if (!("axisText" in groups)) { + groups.axisText = svg.append("g").attr("id", "axisText"); + } }; /** @@ -330,6 +336,45 @@ export const addGrid = function addGrid() { .attr("x", xTextPos(this.xScale, layout)) .attr("y", yTextPos(this.yScale, layout)); + /* add axis labels */ + this.groups.axisText.selectAll("*").remove(); + this.svg.selectAll(".axisText").remove(); + if (layout == 'rect' || layout == "clock") { + let label = "Date"; + if (this.distance == "div" || layout == 'clock') { + // This is a heuristic to determine whether this data + // measures "substitutions per site" or "substitutions" + if (this.yScale.domain()[0] > 5) { + label = "Mutations"; + } else { + label = "Divergence"; + } + } + + this.groups.axisText + .append("text") + .text(layout == 'rect' ? label : "Date") // Clock always has Date on the X-axis + .attr("class", "gridText") + .style("font-size", this.params.tickLabelSize) + .style("font-family", this.params.fontFamily) + .style("fill", this.params.tickLabelFill) + .style("text-anchor", "middle") + .attr("x", this.xScale.range()[1] / 2) + .attr("y", this.yScale.range()[1] + this.params.margins.bottom - 6) + + if (layout == 'clock') { + this.groups.axisText + .append("text") + .text(label) // Clock always has Date on the X-axis + .attr("class", "gridText") + .style("font-size", this.params.tickLabelSize) + .style("font-family", this.params.fontFamily) + .style("fill", this.params.tickLabelFill) + .style("text-anchor", "middle") + .attr('transform', 'translate(' + 10 + ',' + (this.yScale.range()[1] / 2) + ') rotate(-90)') + } + } + this.grid=true; timerEnd("addGrid"); };