Class Legend2D
Object
Legend2D
Helper responsible for legend sizing, drawing and tooltips for
PopGraph2D.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe supported semantic legend modes.static final classSemantic legend data prepared by views and consumed by legends for rendering, sizing and hit-testing. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Rectangle2DCurrent legend bar bounds.private static final doubleWidth of the legend color bar in pixels.private final Rectangle2DThe bounds of the visible legend content, including labels and other legend decorations, but excluding the outer reserve gap.private static final doubleGap between graph and legend in pixels.private static final doubleGap between legend labels and color bar in pixels.private doubleWidth budget for legend labels.private static final doubleMinimum height of one legend color band in pixels.private final PopGraph2DThe owning population graph.private doubleHeight removed from the graph content area by a horizontal legend.private doubleWidth removed from the graph content area by a vertical legend.private Legend2D.SpecsSemantic legend data supplied by the view. -
Constructor Summary
ConstructorsConstructorDescriptionLegend2D(PopGraph2D owner) Create a legend helper tied to the given 2D population graph. -
Method Summary
Modifier and TypeMethodDescription(package private) voiddraw()Draw the legend into the owner's canvas.private voidDraw the segmented discrete-trait legend.private voidStroke the outline around the current legend bar.private voidDraw the current gradient-style legend, including endpoint labels and marker annotations.private voiddrawGradientBands(double[] samples, ColorMap.Gradient1D<String> gradient) Fill the current legend bar with sampled gradient colors.private voiddrawMarkers(double[] markers, double min, double max) Draw marker labels alongside a vertical gradient legend.private StringformatGradientLegend(double value) Format a numeric gradient value for legend labels and tooltips.private StringformatGradientTooltip(double value) Format a numeric gradient value for HTML tooltips.private intgetBandIndexAt(int x, int y, int nBands) Map graph coordinates to the corresponding discrete legend band.private double[]getBins(double min, double max, double span, double[] markers) Sample values along a gradient legend, preserving marker values when possible.private String[]Get the indexed colors used by the current discrete-trait legend.private doublegetFractionAt(int x, int y) Convert graph coordinates to a normalized position along the current legend bar.private doublegetGradientAt(int x, int y, double min, double max) Map a position on the current gradient legend to the corresponding data value.private doubleResolve the maximum value represented by the current gradient legend.private doubleResolve the minimum value represented by the current gradient legend.private StringgetGradientTooltipAt(int x, int y) Build the tooltip text for gradient legends at the given graph coordinates.private doublegetHorizontalDiscreteTraitsBarWidth(String[] labels, double graphWidth) Compute the width of a horizontal discrete-trait legend bar.private doublegetHorizontalLabelY(double southLabelGap, double northLabelY) Compute the y-coordinate used for horizontal legend endpoint labels.private doubleGet the padding between the legend bar and its labels for the given side.private doubleCompute the x-coordinate used to draw a legend label next to the current bar.(package private) Legend2D.ModegetMode()Get the current legend mode.(package private) StringgetTooltipAt(int x, int y) Get the legend tooltip at graph-relative screen coordinates.private StringgetTraitTooltipAt(int x, int y) Build the tooltip text for trait legends at the given graph coordinates.(package private) booleanCheck whether this legend should be shown.(package private) voidreserve(Rectangle2D graphBounds) Recompute the legend reserves from the parent graph area and shrink the given bounds to the remaining plot area.(package private) voidsetBounds(int width, int height, Rectangle2D graphBounds) Position the legend within the available graph area.(package private) voidsetSpecs(Legend2D.Specs legendSpecs) Assign semantic legend data to this legend.
-
Field Details
-
BAR_WIDTH
private static final double BAR_WIDTHWidth of the legend color bar in pixels.- See Also:
-
GAP
private static final double GAPGap between graph and legend in pixels.- See Also:
-
LABEL_PAD
private static final double LABEL_PADGap between legend labels and color bar in pixels.- See Also:
-
MIN_BAND_HEIGHT
private static final double MIN_BAND_HEIGHTMinimum height of one legend color band in pixels.- See Also:
-
owner
The owning population graph. -
specs
Semantic legend data supplied by the view. -
bounds
The bounds of the visible legend content, including labels and other legend decorations, but excluding the outer reserve gap. -
reserveWidth
private double reserveWidthWidth removed from the graph content area by a vertical legend. -
reserveHeight
private double reserveHeightHeight removed from the graph content area by a horizontal legend. -
labelWidth
private double labelWidthWidth budget for legend labels. -
bar
Current legend bar bounds.
-
-
Constructor Details
-
Legend2D
Legend2D(PopGraph2D owner) Create a legend helper tied to the given 2D population graph.- Parameters:
owner- the graph that provides style, bounds, canvas and color map
-
-
Method Details
-
setSpecs
Assign semantic legend data to this legend.- Parameters:
legendSpecs- legend data prepared by the view
-
getMode
-
hasLegend
boolean hasLegend()Check whether this legend should be shown.- Returns:
trueif legend content and a legend position are available
-
reserve
Recompute the legend reserves from the parent graph area and shrink the given bounds to the remaining plot area.- Parameters:
graphBounds- the graph area before graph-content layout is specialized; updated in place to the remaining plot area
-
setBounds
Position the legend within the available graph area.- Parameters:
width- the total graph widthheight- the total graph heightgraphBounds- the drawable graph bounds, adjusted as needed
-
draw
void draw()Draw the legend into the owner's canvas. -
getTooltipAt
Get the legend tooltip at graph-relative screen coordinates.- Parameters:
x- the x-coordinate in graph widget coordinatesy- the y-coordinate in graph widget coordinates- Returns:
- tooltip HTML or
null
-
drawGradient
private void drawGradient()Draw the current gradient-style legend, including endpoint labels and marker annotations. -
drawGradientBands
Fill the current legend bar with sampled gradient colors.- Parameters:
samples- sampled values along the legend axisgradient- gradient used to translate values into CSS colors
-
drawFrame
private void drawFrame()Stroke the outline around the current legend bar. -
drawDiscreteTraits
private void drawDiscreteTraits()Draw the segmented discrete-trait legend. -
getGradientTooltipAt
Build the tooltip text for gradient legends at the given graph coordinates.- Parameters:
x- the x-coordinate in graph widget coordinatesy- the y-coordinate in graph widget coordinates- Returns:
- tooltip HTML or
nullif the point is outside the legend
-
getTraitTooltipAt
Build the tooltip text for trait legends at the given graph coordinates.- Parameters:
x- the x-coordinate in graph widget coordinatesy- the y-coordinate in graph widget coordinates- Returns:
- tooltip HTML or
nullif the point is outside the legend
-
getGradientAt
private double getGradientAt(int x, int y, double min, double max) Map a position on the current gradient legend to the corresponding data value.- Parameters:
x- the x-coordinate in graph widget coordinatesy- the y-coordinate in graph widget coordinatesmin- the minimum legend valuemax- the maximum legend value- Returns:
- legend value, or
Double.NaNif the point is outside
-
getGradientMin
private double getGradientMin()Resolve the minimum value represented by the current gradient legend.- Returns:
- the minimum gradient value
-
getGradientMax
private double getGradientMax()Resolve the maximum value represented by the current gradient legend.- Returns:
- the maximum gradient value
-
getFractionAt
private double getFractionAt(int x, int y) Convert graph coordinates to a normalized position along the current legend bar.- Parameters:
x- the x-coordinate in graph widget coordinatesy- the y-coordinate in graph widget coordinates- Returns:
- normalized position in
[0,1], orDouble.NaNif outside the legend bar
-
getBandIndexAt
private int getBandIndexAt(int x, int y, int nBands) Map graph coordinates to the corresponding discrete legend band.- Parameters:
x- the x-coordinate in graph widget coordinatesy- the y-coordinate in graph widget coordinatesnBands- number of discrete legend bands- Returns:
- band index, or
-1if outside the legend bar
-
getBins
private double[] getBins(double min, double max, double span, double[] markers) Sample values along a gradient legend, preserving marker values when possible.- Parameters:
min- the minimum legend valuemax- the maximum legend valuespan- the pixel span of the legend axismarkers- marker values that should be represented exactly- Returns:
- sampled values from top-to-bottom or left-to-right
-
drawMarkers
private void drawMarkers(double[] markers, double min, double max) Draw marker labels alongside a vertical gradient legend.- Parameters:
markers- marker values to annotatemin- the minimum legend valuemax- the maximum legend value
-
getLabelX
Compute the x-coordinate used to draw a legend label next to the current bar.- Parameters:
label- the label text to position- Returns:
- x-coordinate for the label baseline
-
getHorizontalLabelY
private double getHorizontalLabelY(double southLabelGap, double northLabelY) Compute the y-coordinate used for horizontal legend endpoint labels.- Parameters:
southLabelGap- offset below the bar for south legendsnorthLabelY- label baseline for north legends without extra padding- Returns:
- y-coordinate for the endpoint labels
-
getLabelPad
Get the padding between the legend bar and its labels for the given side.- Parameters:
pos- the legend position- Returns:
- label padding in pixels
-
getHorizontalDiscreteTraitsBarWidth
Compute the width of a horizontal discrete-trait legend bar.- Parameters:
labels- discrete legend labelsgraphWidth- width of the actual graph-content bounds- Returns:
- legend bar width in pixels
-
getDiscreteTraitsColors
Get the indexed colors used by the current discrete-trait legend.- Returns:
- discrete legend colors, or an empty array if unavailable
-
formatGradientLegend
Format a numeric gradient value for legend labels and tooltips.- Parameters:
value- the value to format- Returns:
- formatted legend text
-
formatGradientTooltip
Format a numeric gradient value for HTML tooltips.- Parameters:
value- the value to format- Returns:
- formatted tooltip text
-