-
Notifications
You must be signed in to change notification settings - Fork 49
CODAP-1530: explain a legend attribute the display cannot honor #2701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kswenson
merged 8 commits into
leads-point-shapes
from
CODAP-1530-inoperable-legend-attribute
Sep 15, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ecc0ad6
CODAP-1530: explain a legend attribute the display cannot honor
kswenson 78ff42f
CODAP-1530: address Copilot review
kswenson ec8c137
CODAP-1530: address Copilot review of the numeric branch
kswenson ffc4e54
CODAP-1530: scope the palette's gate to displays that gray their points
kswenson 848af76
CODAP-1530: show the legend for a map layer it cannot honor
kswenson dce56b3
CODAP-1530: make the map agree with the graph about an unusable legend
kswenson c1bcfb7
CODAP-1530: address Copilot review of the map changes
kswenson 1747755
CODAP-1530: address review comments
kswenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
v3/src/components/data-display/components/legend/inoperable-legend-message.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // jsdom does no text layout, so width is stubbed per character the way the sibling legend tests do. | ||
| jest.mock("../../../../hooks/use-measure-text", () => ({ | ||
| measureText: (text: string) => text.length * 10, | ||
| // also stubbed because labelHeight, pulled in transitively, measures at module load | ||
| measureTextExtent: (text: string) => ({ width: text.length * 10, height: 12 }) | ||
| })) | ||
|
|
||
| import { wrapTextToWidth } from "./inoperable-legend-message" | ||
|
|
||
| describe("wrapTextToWidth", () => { | ||
| it("leaves a string that fits on one line", () => { | ||
| expect(wrapTextToWidth("one two", 1000)).toEqual(["one two"]) | ||
| }) | ||
|
|
||
| it("breaks between words at the available width", () => { | ||
| // 10px per character, so 100px holds ten characters | ||
| expect(wrapTextToWidth("aaaa bbbb cccc dddd", 100)).toEqual(["aaaa bbbb", "cccc dddd"]) | ||
| }) | ||
|
|
||
| it("rewraps as the width changes", () => { | ||
| const text = "aaaa bbbb cccc dddd" | ||
| expect(wrapTextToWidth(text, 50)).toEqual(["aaaa", "bbbb", "cccc", "dddd"]) | ||
| expect(wrapTextToWidth(text, 150)).toEqual(["aaaa bbbb cccc", "dddd"]) | ||
| }) | ||
|
|
||
| it("keeps a word too long to fit rather than dropping or splitting it", () => { | ||
| // the legend can be narrower than a single word; overflowing one line beats losing the word | ||
| expect(wrapTextToWidth("aaaaaaaaaa bb", 30)).toEqual(["aaaaaaaaaa", "bb"]) | ||
| }) | ||
|
|
||
| it("collapses runs of whitespace", () => { | ||
| expect(wrapTextToWidth(" one two ", 1000)).toEqual(["one two"]) | ||
| }) | ||
|
|
||
| it("returns nothing for an empty string", () => { | ||
| expect(wrapTextToWidth("", 1000)).toEqual([]) | ||
| expect(wrapTextToWidth(" ", 1000)).toEqual([]) | ||
| }) | ||
| }) |
70 changes: 70 additions & 0 deletions
70
v3/src/components/data-display/components/legend/inoperable-legend-message.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { observer } from "mobx-react-lite" | ||
| import { useEffect } from "react" | ||
| import { measureText } from "../../../../hooks/use-measure-text" | ||
| import { t } from "../../../../utilities/translation/translate" | ||
| import { axisGap } from "../../../axis/axis-types" | ||
| import { kDataDisplayFont } from "../../data-display-types" | ||
| import { useDataConfigurationContext } from "../../hooks/use-data-configuration-context" | ||
| import { useDataDisplayLayout } from "../../hooks/use-data-display-layout" | ||
| import { labelHeight, padding } from "./categorical-legend-model" | ||
| import { IBaseLegendProps } from "./legend-common" | ||
|
|
||
| import "./legend.scss" | ||
|
|
||
| const kLineHeight = 16 | ||
|
|
||
| // Greedy word wrap. SVG text does not wrap, and this message is a sentence rather than a label, so | ||
| // it is broken into lines here and drawn as one tspan each. | ||
| export function wrapTextToWidth(text: string, maxWidth: number, font = kDataDisplayFont): string[] { | ||
| const words = text.split(/\s+/).filter(word => !!word) | ||
| if (!words.length) return [] | ||
|
|
||
| const lines: string[] = [] | ||
| let line = words[0] | ||
| for (const word of words.slice(1)) { | ||
| const candidate = `${line} ${word}` | ||
| if (measureText(candidate, font) <= maxWidth) { | ||
| line = candidate | ||
| } else { | ||
| lines.push(line) | ||
| line = word | ||
| } | ||
| } | ||
| lines.push(line) | ||
| return lines | ||
| } | ||
|
|
||
| /* | ||
| * Stands in for the keys when the assigned legend attribute is one this display cannot honor. | ||
| * | ||
| * The keys are the wrong thing to draw -- there is no per-category encoding to key -- but drawing | ||
| * nothing was worse: the legend collapsed entirely, taking the attribute's name and its remove | ||
| * action with it, so the assignment the user made became invisible and unreachable. | ||
| */ | ||
| export const InoperableLegendMessage = observer(function InoperableLegendMessage( | ||
| { layerIndex, setDesiredExtent }: IBaseLegendProps | ||
| ) { | ||
| const dataConfiguration = useDataConfigurationContext() | ||
| const dataDisplayLayout = useDataDisplayLayout() | ||
| const attrID = dataConfiguration?.assignedLegendAttributeID | ||
| const attrName = (attrID && dataConfiguration?.dataset?.attrFromID(attrID)?.name) || "" | ||
| // Both gaps, so a long word cannot push the text past the edge the keys stop at. | ||
| const maxWidth = Math.max(dataDisplayLayout.tileWidth - 2 * axisGap, 1) | ||
| const lines = wrapTextToWidth(t("V3.Legend.attributeNotOperable", { vars: [attrName] }), maxWidth) | ||
|
|
||
| useEffect(() => { | ||
| setDesiredExtent(layerIndex, labelHeight + lines.length * kLineHeight + padding + axisGap) | ||
| return () => setDesiredExtent(layerIndex, 0) | ||
| }, [layerIndex, lines.length, setDesiredExtent]) | ||
|
|
||
| return ( | ||
| <text className="legend-inoperable-message" data-testid="legend-inoperable-message" | ||
| x={axisGap} y={labelHeight + padding}> | ||
| {lines.map((line, i) => ( | ||
| // Keyed by position: a wrapped line has no identity apart from where it sits, and two of | ||
| // them can hold the same text once an attribute name repeats a word or the tile narrows. | ||
| <tspan key={i} x={axisGap} dy={i === 0 ? 0 : kLineHeight}>{line}</tspan> | ||
| ))} | ||
| </text> | ||
| ) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
v3/src/components/data-display/components/legend/multi-legend.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { IBaseLayerModel } from "../../models/base-data-display-content-model" | ||
| import { layerHasLegendToShow } from "./multi-legend" | ||
|
|
||
| const layer = ( | ||
| { legendID = "", inoperable = false, isVisible = true } = {} | ||
| ) => ({ | ||
| id: "layer-1", | ||
| layerIndex: 0, | ||
| isVisible, | ||
| dataConfiguration: { | ||
| attributeID: () => legendID, | ||
| legendAttributeIsInoperable: inoperable | ||
| } | ||
| } as unknown as IBaseLayerModel) | ||
|
|
||
| describe("layerHasLegendToShow", () => { | ||
| it("shows a layer with a legend attribute", () => { | ||
| expect(layerHasLegendToShow(layer({ legendID: "legId" }))).toBe(true) | ||
| }) | ||
|
|
||
| it("skips a layer with no legend attribute", () => { | ||
| expect(layerHasLegendToShow(layer())).toBe(false) | ||
| }) | ||
|
|
||
| it("skips a hidden layer", () => { | ||
| expect(layerHasLegendToShow(layer({ legendID: "legId", isVisible: false }))).toBe(false) | ||
| }) | ||
|
|
||
| it("shows a layer whose legend attribute cannot be honored", () => { | ||
| /* | ||
| * The base configuration reports "" for an assignment it cannot honor, so this layer would | ||
| * otherwise be dropped here -- before Legend could render the label, the remove action, and the | ||
| * message explaining why the points are not colored by it. | ||
| */ | ||
| expect(layerHasLegendToShow(layer({ inoperable: true }))).toBe(true) | ||
| }) | ||
|
|
||
| it("still skips that layer when it is hidden", () => { | ||
| expect(layerHasLegendToShow(layer({ inoperable: true, isVisible: false }))).toBe(false) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.