Skip to content

Commit

Permalink
[#187744843] Bug fix: Axis attribute label not showing
Browse files Browse the repository at this point in the history
* The GraphAttributeLabel component was unnecessarily complicated. Simplifying it by getting rid of the `setupTitle` useEffect solved the problems
  • Loading branch information
bfinzer committed Jun 7, 2024
1 parent 963a7f4 commit 69c6842
Showing 1 changed file with 46 additions and 56 deletions.
102 changes: 46 additions & 56 deletions v3/src/components/graph/components/graph-attribute-label.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React, {useCallback, useEffect, useRef} from "react"
import {select} from "d3"
import React, { useCallback, useRef } from "react"
import { select } from "d3"
import { t } from "../../../utilities/translation/translate"
import {useGraphDataConfigurationContext} from "../hooks/use-graph-data-configuration-context"
import {useGraphContentModelContext} from "../hooks/use-graph-content-model-context"
import {useGraphLayoutContext} from "../hooks/use-graph-layout-context"
import {AttributeType} from "../../../models/data/attribute"
import {IDataSet} from "../../../models/data/data-set"
import {GraphPlace, isVertical} from "../../axis-graph-shared"
import {AttributeLabel} from "../../data-display/components/attribute-label"
import {graphPlaceToAttrRole} from "../../data-display/data-display-types"
import {useTileModelContext} from "../../../hooks/use-tile-model-context"
import {getStringBounds} from "../../axis/axis-utils"
import { useGraphDataConfigurationContext } from "../hooks/use-graph-data-configuration-context"
import { useGraphContentModelContext } from "../hooks/use-graph-content-model-context"
import { useGraphLayoutContext } from "../hooks/use-graph-layout-context"
import { AttributeType } from "../../../models/data/attribute"
import { IDataSet } from "../../../models/data/data-set"
import { GraphPlace, isVertical } from "../../axis-graph-shared"
import { AttributeLabel } from "../../data-display/components/attribute-label"
import { graphPlaceToAttrRole } from "../../data-display/data-display-types"
import { useTileModelContext } from "../../../hooks/use-tile-model-context"
import { getStringBounds } from "../../axis/axis-utils"

import vars from "../../vars.scss"

Expand All @@ -22,8 +22,10 @@ interface IAttributeLabelProps {
}

export const GraphAttributeLabel =
function GraphAttributeLabel({place, onTreatAttributeAs, onRemoveAttribute,
onChangeAttribute}: IAttributeLabelProps) {
function GraphAttributeLabel({
place, onTreatAttributeAs, onRemoveAttribute,
onChangeAttribute
}: IAttributeLabelProps) {

Check warning on line 28 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L28

Added line #L28 was not covered by tests
const graphModel = useGraphContentModelContext(),
dataConfiguration = useGraphDataConfigurationContext(),
layout = useGraphLayoutContext(),
Expand All @@ -50,11 +52,11 @@ export const GraphAttributeLabel =
const unusedClassName = useClickHereCue ? 'attribute-label' : 'empty-label'
const visibility = hideClickHereCue ? 'hidden' : 'visible'
const labelFont = useClickHereCue ? vars.emptyLabelFont : vars.labelFont
return { useClickHereCue, className, unusedClassName, labelFont, visibility }
return {useClickHereCue, className, unusedClassName, labelFont, visibility}

Check warning on line 55 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L55

Added line #L55 was not covered by tests
}, [dataConfiguration, graphModel, isTileSelected, place])

const getLabel = useCallback(() => {
const { useClickHereCue } = getClickHereCue()
const {useClickHereCue} = getClickHereCue()

Check warning on line 59 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L59

Added line #L59 was not covered by tests
if (useClickHereCue) {
return t('DG.AxisView.emptyGraphCue')
}
Expand All @@ -66,10 +68,25 @@ export const GraphAttributeLabel =
return attrIDs.map(anID => dataset?.attrFromID(anID)?.name)
.filter(aName => aName !== '').join(', ')
}, [dataConfiguration?.secondaryRole, dataset, getAttributeIDs, getClickHereCue,
graphModel.pointsFusedIntoBars, place])
graphModel.pointsFusedIntoBars, place])

const refreshAxisTitle = useCallback(() => {
const {labelFont, className, visibility} = getClickHereCue(),

const updateSelection = (selection: any) => {
return selection

Check warning on line 76 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L75-L76

Added lines #L75 - L76 were not covered by tests
.attr('class', className)
.attr('text-anchor', 'middle')
.attr('data-testid', className)
.attr("transform", labelTransform + tRotation)
.attr('class', className)
.attr('data-testid', className)
.style('visibility', visibility)
.attr('x', tX)
.attr('y', tY)
.text(label)
}

const {labelFont, className, unusedClassName, visibility} = getClickHereCue(),

Check warning on line 89 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L89

Added line #L89 was not covered by tests
bounds = layout.getComputedBounds(place),
layoutIsVertical = isVertical(place),
halfRange = layoutIsVertical ? bounds.height / 2 : bounds.width / 2,
Expand All @@ -84,48 +101,21 @@ export const GraphAttributeLabel =
: place === 'legend' ? labelBounds.height / 2
: place === 'top' ? labelBounds.height : bounds.height - labelBounds.height / 2,
tRotation = isVertical(place) ? ` rotate(-90,${tX},${tY})` : ''
select(labelRef.current)
.selectAll(`text.${className}`)

select(labelRef.current).selectAll(`text.${unusedClassName}`).remove()

Check warning on line 105 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L105

Added line #L105 was not covered by tests

const labelTextSelection = select(labelRef.current).selectAll(`text.${className}`)
labelTextSelection

Check warning on line 108 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L107-L108

Added lines #L107 - L108 were not covered by tests
.data([1])
.join(
enter => enter,
(enter) =>
enter.append('text')

Check warning on line 112 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L112

Added line #L112 was not covered by tests
.attr('text-anchor', 'middle')
.call(updateSelection),
(update) =>
update
.attr("transform", labelTransform + tRotation)
.attr('class', className)
.attr('data-testid', className)
.style('visibility', visibility)
.attr('x', tX)
.attr('y', tY)
.text(label)
)
}, [getClickHereCue, getLabel, layout, place])

useEffect(function setupTitle() {

const { className, unusedClassName } = getClickHereCue()

const removeUnusedLabel = () => {
select(labelRef.current)
.selectAll(`text.${unusedClassName}`)
.remove()
}

if (labelRef.current) {
removeUnusedLabel()
select(labelRef.current)
.selectAll(`text.${className}`)
.data([1])
.join(
(enter) =>
enter.append('text')
.attr('class', className)
.attr('text-anchor', 'middle')
.attr('data-testid', className)
update.call(updateSelection),

Check warning on line 116 in v3/src/components/graph/components/graph-attribute-label.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/graph/components/graph-attribute-label.tsx#L116

Added line #L116 was not covered by tests
)
refreshAxisTitle()
}
}, [getClickHereCue, place, refreshAxisTitle])
}, [getClickHereCue, getLabel, layout, place])

return (
<AttributeLabel
Expand Down

0 comments on commit 69c6842

Please sign in to comment.