Skip to content

Commit

Permalink
Merge pull request #1554 from nextstrain/feat/all-labels
Browse files Browse the repository at this point in the history
toggle to show all branch labels
  • Loading branch information
jameshadfield authored Oct 5, 2022
2 parents 6ef3a73 + 15a9ce6 commit ca0ec34
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ export const TOGGLE_MEASUREMENTS_OVERALL_MEAN = "TOGGLE_MEASUREMENTS_OVERALL_MEA
export const CHANGE_MEASUREMENTS_DISPLAY = "CHANGE_MEASUREMENTS_DISPLAY";
export const APPLY_MEASUREMENTS_FILTER = "APPLY_MEASUREMENTS_FILTER";
export const UPDATE_MEASUREMENTS_ERROR = "UPDATE_MEASUREMENTS_ERROR";
export const TOGGLE_SHOW_ALL_BRANCH_LABELS = "TOGGLE_SHOW_ALL_BRANCH_LABELS";
14 changes: 13 additions & 1 deletion src/components/controls/choose-branch-labelling.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import React from "react";
import { connect } from "react-redux";
import { withTranslation } from 'react-i18next';

import { CHANGE_BRANCH_LABEL } from "../../actions/types";
import { CHANGE_BRANCH_LABEL, TOGGLE_SHOW_ALL_BRANCH_LABELS } from "../../actions/types";
import { SidebarSubtitle } from "./styles";
import { controlsWidth } from "../../util/globals";
import CustomSelect from "./customSelect";
import Toggle from "./toggle";

@connect((state) => ({
selected: state.controls.selectedBranchLabel,
showAll: state.controls.showAllBranchLabels,
available: state.tree.availableBranchLabels,
canRenderBranchLabels: state.controls.canRenderBranchLabels
}))
Expand Down Expand Up @@ -36,6 +38,16 @@ class ChooseBranchLabelling extends React.Component {
onChange={this.change}
/>
</div>

{this.props.selected!=="none" && (
<Toggle
display
on={this.props.showAll}
callback={() => this.props.dispatch({type: TOGGLE_SHOW_ALL_BRANCH_LABELS})}
label={t("sidebar:Show all labels")}
style={{paddingBottom: "5px", paddingTop: "10px"}}
/>
)}
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/tree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Tree = connect((state) => ({
panelsToDisplay: state.controls.panelsToDisplay,
selectedBranchLabel: state.controls.selectedBranchLabel,
canRenderBranchLabels: state.controls.canRenderBranchLabels,
showAllBranchLabels: state.controls.showAllBranchLabels,
tipLabelKey: state.controls.tipLabelKey,
narrativeMode: state.narrative.display,
animationPlayPauseButton: state.controls.animationPlayPauseButton
Expand Down
6 changes: 6 additions & 0 deletions src/components/tree/phyloTree/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export const change = function change({
newLayout = undefined,
updateLayout = undefined, // todo - this seems identical to `newLayout`
newBranchLabellingKey = undefined,
showAllBranchLabels = undefined,
newTipLabelKey = undefined,
/* arrays of data (the same length as nodes) */
branchStroke = undefined,
Expand Down Expand Up @@ -365,6 +366,11 @@ export const change = function change({
/* show confidences - set this param which actually adds the svg paths for
confidence intervals when mapToScreen() gets called below */
if (showConfidences) this.params.confidence = true;
/* keep the state of phylotree in sync with redux (more complex than it should be) */
if (showAllBranchLabels!==undefined) {
this.params.showAllBranchLabels=showAllBranchLabels;
elemsToUpdate.add('.branchLabel');
}
/* mapToScreen */
if (
svgPropsToUpdate.has(["stroke-width"]) ||
Expand Down
7 changes: 5 additions & 2 deletions src/components/tree/phyloTree/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ const branchLabelFontWeight = (key) => {

/** createBranchLabelVisibility (the return value should be passed to d3 style call)
* @param {str} key e.g. "aa" or "clade"
* @param {bool} showAll
* @param {str} layout
* @param {int} totalTipsInView visible tips also in view
* @return {func} Returns a function with 1 argument: the current node (branch).
* This fn will return "visible" or "hidden".
* NOTE: the fn should only be provided nodes which have a label.
*/
const createBranchLabelVisibility = (key, layout, totalTipsInView) => (d) => {
const createBranchLabelVisibility = (key, showAll, layout, totalTipsInView) => (d) => {
if (showAll) return "visible";
if (d.visibility !== NODE_VISIBLE) return "hidden";
const magicTipFractionToShowBranchLabel = 0.03;
/* if the number of _visible_ tips descending from this node are over the
Expand All @@ -99,6 +101,7 @@ export const updateBranchLabels = function updateBranchLabels(dt) {
}
const visibility = createBranchLabelVisibility(
this.params.branchLabelKey,
this.params.showAllBranchLabels,
this.layout,
this.zoomNode.n.tipCount
);
Expand Down Expand Up @@ -128,7 +131,7 @@ export const drawBranchLabels = function drawBranchLabels(key) {
this.params.branchLabelKey = key;
const labelSize = branchLabelSize(key);
const fontWeight = branchLabelFontWeight(key);
const visibility = createBranchLabelVisibility(key, this.layout, this.zoomNode.n.tipCount);
const visibility = createBranchLabelVisibility(key, this.params.showAllBranchLabels, this.layout, this.zoomNode.n.tipCount);

if (!("branchLabels" in this.groups)) {
this.groups.branchLabels = this.svg.append("g").attr("id", "branchLabels").attr("clip-path", "url(#treeClip)");
Expand Down
4 changes: 4 additions & 0 deletions src/components/tree/reactD3Interface/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export const changePhyloTreeViaPropsComparison = (mainTree, phylotree, oldProps,
) {
args.newBranchLabellingKey = newProps.selectedBranchLabel;
}
if (oldProps.showAllBranchLabels!==newProps.showAllBranchLabels) {
args.showAllBranchLabels = newProps.showAllBranchLabels;
}

if (oldProps.tipLabelKey !== newProps.tipLabelKey) {
args.newTipLabelKey = newProps.tipLabelKey;
}
Expand Down
1 change: 1 addition & 0 deletions src/components/tree/reactD3Interface/initialRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const renderTree = (that, main, phylotree, props) => {
grid: true,
confidence: props.temporalConfidence.display,
branchLabelKey: renderBranchLabels && props.selectedBranchLabel,
showAllBranchLabels: props.showAllBranchLabels,
orientation: main ? [1, 1] : [-1, 1],
tipLabels: true,
showTipLabels: true
Expand Down
3 changes: 3 additions & 0 deletions src/reducers/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const getDefaultControlsState = () => {
colorScale: undefined,
explodeAttr: undefined,
selectedBranchLabel: "none",
showAllBranchLabels: false,
canRenderBranchLabels: true,
analysisSlider: false,
geoResolution: defaults.geoResolution,
Expand Down Expand Up @@ -125,6 +126,8 @@ const Controls = (state = getDefaultControlsState(), action) => {
});
case types.CHANGE_BRANCH_LABEL:
return Object.assign({}, state, { selectedBranchLabel: action.value });
case types.TOGGLE_SHOW_ALL_BRANCH_LABELS:
return Object.assign({}, state, { showAllBranchLabels: !state.showAllBranchLabels });
case types.CHANGE_LAYOUT:
return Object.assign({}, state, {
layout: action.layout,
Expand Down

0 comments on commit ca0ec34

Please sign in to comment.