Class Legend2D

Object
Legend2D

public class Legend2D extends Object
Helper responsible for legend sizing, drawing and tooltips for PopGraph2D.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The supported semantic legend modes.
    static final class 
    Semantic legend data prepared by views and consumed by legends for rendering, sizing and hit-testing.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Rectangle2D
    Current legend bar bounds.
    private static final double
    Width of the legend color bar in pixels.
    private final Rectangle2D
    The bounds of the visible legend content, including labels and other legend decorations, but excluding the outer reserve gap.
    private static final double
    Gap between graph and legend in pixels.
    private static final double
    Gap between legend labels and color bar in pixels.
    private double
    Width budget for legend labels.
    private static final double
    Minimum height of one legend color band in pixels.
    private final PopGraph2D
    The owning population graph.
    private double
    Height removed from the graph content area by a horizontal legend.
    private double
    Width removed from the graph content area by a vertical legend.
    Semantic legend data supplied by the view.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a legend helper tied to the given 2D population graph.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Draw the legend into the owner's canvas.
    private void
    Draw the segmented discrete-trait legend.
    private void
    Stroke the outline around the current legend bar.
    private void
    Draw the current gradient-style legend, including endpoint labels and marker annotations.
    private void
    drawGradientBands(double[] samples, ColorMap.Gradient1D<String> gradient)
    Fill the current legend bar with sampled gradient colors.
    private void
    drawMarkers(double[] markers, double min, double max)
    Draw marker labels alongside a vertical gradient legend.
    private String
    formatGradientLegend(double value)
    Format a numeric gradient value for legend labels and tooltips.
    private String
    formatGradientTooltip(double value)
    Format a numeric gradient value for HTML tooltips.
    private int
    getBandIndexAt(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 double
    getFractionAt(int x, int y)
    Convert graph coordinates to a normalized position along the current legend bar.
    private double
    getGradientAt(int x, int y, double min, double max)
    Map a position on the current gradient legend to the corresponding data value.
    private double
    Resolve the maximum value represented by the current gradient legend.
    private double
    Resolve the minimum value represented by the current gradient legend.
    private String
    getGradientTooltipAt(int x, int y)
    Build the tooltip text for gradient legends at the given graph coordinates.
    private double
    getHorizontalDiscreteTraitsBarWidth(String[] labels, double graphWidth)
    Compute the width of a horizontal discrete-trait legend bar.
    private double
    getHorizontalLabelY(double southLabelGap, double northLabelY)
    Compute the y-coordinate used for horizontal legend endpoint labels.
    private double
    Get the padding between the legend bar and its labels for the given side.
    private double
    Compute the x-coordinate used to draw a legend label next to the current bar.
    (package private) Legend2D.Mode
    Get the current legend mode.
    (package private) String
    getTooltipAt(int x, int y)
    Get the legend tooltip at graph-relative screen coordinates.
    private String
    getTraitTooltipAt(int x, int y)
    Build the tooltip text for trait legends at the given graph coordinates.
    (package private) boolean
    Check whether this legend should be shown.
    (package private) void
    reserve(Rectangle2D graphBounds)
    Recompute the legend reserves from the parent graph area and shrink the given bounds to the remaining plot area.
    (package private) void
    setBounds(int width, int height, Rectangle2D graphBounds)
    Position the legend within the available graph area.
    (package private) void
    setSpecs(Legend2D.Specs legendSpecs)
    Assign semantic legend data to this legend.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BAR_WIDTH

      private static final double BAR_WIDTH
      Width of the legend color bar in pixels.
      See Also:
    • GAP

      private static final double GAP
      Gap between graph and legend in pixels.
      See Also:
    • LABEL_PAD

      private static final double LABEL_PAD
      Gap between legend labels and color bar in pixels.
      See Also:
    • MIN_BAND_HEIGHT

      private static final double MIN_BAND_HEIGHT
      Minimum height of one legend color band in pixels.
      See Also:
    • owner

      private final PopGraph2D owner
      The owning population graph.
    • specs

      private Legend2D.Specs specs
      Semantic legend data supplied by the view.
    • bounds

      private final Rectangle2D bounds
      The bounds of the visible legend content, including labels and other legend decorations, but excluding the outer reserve gap.
    • reserveWidth

      private double reserveWidth
      Width removed from the graph content area by a vertical legend.
    • reserveHeight

      private double reserveHeight
      Height removed from the graph content area by a horizontal legend.
    • labelWidth

      private double labelWidth
      Width budget for legend labels.
    • bar

      private final Rectangle2D 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

      void setSpecs(Legend2D.Specs legendSpecs)
      Assign semantic legend data to this legend.
      Parameters:
      legendSpecs - legend data prepared by the view
    • getMode

      Legend2D.Mode getMode()
      Get the current legend mode.
      Returns:
      legend mode
    • hasLegend

      boolean hasLegend()
      Check whether this legend should be shown.
      Returns:
      true if legend content and a legend position are available
    • reserve

      void reserve(Rectangle2D graphBounds)
      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

      void setBounds(int width, int height, Rectangle2D graphBounds)
      Position the legend within the available graph area.
      Parameters:
      width - the total graph width
      height - the total graph height
      graphBounds - the drawable graph bounds, adjusted as needed
    • draw

      void draw()
      Draw the legend into the owner's canvas.
    • getTooltipAt

      String getTooltipAt(int x, int y)
      Get the legend tooltip at graph-relative screen coordinates.
      Parameters:
      x - the x-coordinate in graph widget coordinates
      y - 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

      private void drawGradientBands(double[] samples, ColorMap.Gradient1D<String> gradient)
      Fill the current legend bar with sampled gradient colors.
      Parameters:
      samples - sampled values along the legend axis
      gradient - 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

      private String getGradientTooltipAt(int x, int y)
      Build the tooltip text for gradient legends at the given graph coordinates.
      Parameters:
      x - the x-coordinate in graph widget coordinates
      y - the y-coordinate in graph widget coordinates
      Returns:
      tooltip HTML or null if the point is outside the legend
    • getTraitTooltipAt

      private String getTraitTooltipAt(int x, int y)
      Build the tooltip text for trait legends at the given graph coordinates.
      Parameters:
      x - the x-coordinate in graph widget coordinates
      y - the y-coordinate in graph widget coordinates
      Returns:
      tooltip HTML or null if 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 coordinates
      y - the y-coordinate in graph widget coordinates
      min - the minimum legend value
      max - the maximum legend value
      Returns:
      legend value, or Double.NaN if 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 coordinates
      y - the y-coordinate in graph widget coordinates
      Returns:
      normalized position in [0,1], or Double.NaN if 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 coordinates
      y - the y-coordinate in graph widget coordinates
      nBands - number of discrete legend bands
      Returns:
      band index, or -1 if 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 value
      max - the maximum legend value
      span - the pixel span of the legend axis
      markers - 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 annotate
      min - the minimum legend value
      max - the maximum legend value
    • getLabelX

      private double getLabelX(String label)
      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 legends
      northLabelY - label baseline for north legends without extra padding
      Returns:
      y-coordinate for the endpoint labels
    • getLabelPad

      private double getLabelPad(GraphStyle.Position pos)
      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

      private double getHorizontalDiscreteTraitsBarWidth(String[] labels, double graphWidth)
      Compute the width of a horizontal discrete-trait legend bar.
      Parameters:
      labels - discrete legend labels
      graphWidth - width of the actual graph-content bounds
      Returns:
      legend bar width in pixels
    • getDiscreteTraitsColors

      private String[] getDiscreteTraitsColors()
      Get the indexed colors used by the current discrete-trait legend.
      Returns:
      discrete legend colors, or an empty array if unavailable
    • formatGradientLegend

      private String formatGradientLegend(double value)
      Format a numeric gradient value for legend labels and tooltips.
      Parameters:
      value - the value to format
      Returns:
      formatted legend text
    • formatGradientTooltip

      private String formatGradientTooltip(double value)
      Format a numeric gradient value for HTML tooltips.
      Parameters:
      value - the value to format
      Returns:
      formatted tooltip text