Class S3Graph

All Implemented Interfaces:
DoubleClickHandler, HasAllDragAndDropHandlers, HasAllFocusHandlers, HasAllGestureHandlers, HasAllKeyHandlers, HasAllMouseHandlers, HasAllTouchHandlers, HasBlurHandlers, HasClickHandlers, HasContextMenuHandlers, HasDoubleClickHandlers, HasDragEndHandlers, HasDragEnterHandlers, HasDragHandlers, HasDragLeaveHandlers, HasDragOverHandlers, HasDragStartHandlers, HasDropHandlers, HasFocusHandlers, HasGestureChangeHandlers, HasGestureEndHandlers, HasGestureStartHandlers, HasKeyDownHandlers, HasKeyPressHandlers, HasKeyUpHandlers, HasMouseDownHandlers, HasMouseMoveHandlers, HasMouseOutHandlers, HasMouseOverHandlers, HasMouseUpHandlers, HasMouseWheelHandlers, HasTouchCancelHandlers, HasTouchEndHandlers, HasTouchMoveHandlers, HasTouchStartHandlers, MouseDownHandler, MouseMoveHandler, MouseOutHandler, MouseUpHandler, MouseWheelHandler, TouchEndHandler, TouchMoveHandler, TouchStartHandler, HasAttachHandlers, EventHandler, HasHandlers, EventListener, AcceptsOneWidget, Focusable, HasFocus, HasOneWidget, HasVisibility, HasWidgets, HasWidgets.ForIsWidget, IsWidget, RequiresResize, SourcesClickEvents, SourcesFocusEvents, SourcesKeyboardEvents, SourcesMouseEvents, SourcesMouseWheelEvents, Iterable<Widget>, AbstractGraph.HasTrajectory, AbstractGraph.Shifter, AbstractGraph.Shifting, AbstractGraph.Zoomer, AbstractGraph.Zooming, ContextMenu.Listener, ContextMenu.Provider, Tooltip.Provider

Graph for the simplex \(S_3\). The graph is used to visualize the evolution of three traits. The traits are projected onto the three corners of the simplex. The graph provides a context menu to set and/or swap the order of the traits.

The graph is backed by a RingBuffer to store the trajectory. The buffer is updated by calling addData(double, double[], boolean). It is interactive and allows the user to zoom and shift the view. The user can set the initial state by double-clicking on the graph. The graph can be exported in PNG or SVG graphics formats or the trajectory data as CSV.

Author:
Christoph Hauert
  • Field Details

    • init

      double[] init
      The starting point of the most recent trajectory.
    • e0

      The lower left corner of the simplex.
    • e1

      The lower right corner of the simplex.
    • e2

      The upper corner of the simplex.
    • outline

      Path2D outline
      The path that outlines the simplex.
    • map

      S3Map map
      The map for converting data to simplex coordinates (cartesian).
    • role

      int role
      The identifier of the role of the data.
    • SQRT_2

      static final double SQRT_2
      Constant for \(\sqrt{2}\). Aspect ratio of equilateral triangle.
      See Also:
    • bufferThreshold

      private double bufferThreshold
      Threshold for storing new data point in buffer. Roughly corresponds to the squared distance between two points that are at least a pixel apart.
    • MIN_PIXELS

      private static double MIN_PIXELS
      The minimum distance between two subsequent points in pixels.
    • swapOrderMenu

      private ContextMenuItem swapOrderMenu
      The context menu item to swap the order of the traits along the closest edge.
    • clearMenu

      private ContextMenuItem clearMenu
      The context menu item to clear the canvas.
    • setTraitMenu

      private ContextMenu setTraitMenu
      The context menu to select the trait in the closest corner.
    • cornerIdx

      private int cornerIdx
      The index of the corner closest to the mouse pointer.
  • Constructor Details

    • S3Graph

      public S3Graph(AbstractGraph.Controller controller, Module module, int role)
      Create a new simplex \(S_3\) graph for module running in controller with the specified role.
      Parameters:
      controller - the controller of this graph
      module - the module backing the graph
      role - the role of the data
  • Method Details

    • activate

      public void activate()
      Description copied from class: AbstractGraph
      Perform necessary preparations to show the graph in the GUI. Attaches mouse and touch handlers for graphs that implement AbstractGraph.Zooming or AbstractGraph.Shifting interfaces.
      Overrides:
      activate in class AbstractGraph<double[]>
      See Also:
    • setMap

      public void setMap(S3Map map)
      Set the map for converting data to simplex coordinates.
      Parameters:
      map - the conversion map
    • getMap

      public S3Map getMap()
      Get the map for converting data to simplex coordinates.
      Returns:
      the conversion map
    • reset

      public void reset()
      Description copied from class: AbstractGraph
      Reset the graph. Clear canvas and messages.
      Overrides:
      reset in class AbstractGraph<double[]>
    • addData

      public void addData(double t, double[] data, boolean force)
      Add data to the graph. The time t is prepended to the data as the first element.
      Parameters:
      t - the time of the data
      data - the data to add
      force - true to force adding the data
      Implementation Notes:
      • The data array is cloned and the time prepended before adding it to the buffer.
      • In order to conserve memory the data is added only if the distance between the new data point and the last point in the buffer is larger than threshold bufferThreshold, unless force == true.
    • distSq

      private double distSq(double[] vec, double[] buf)
      Helper method to calculate the distance squared between two vectors.
      Parameters:
      vec - the first vector
      buf - the second vector
      Returns:
      the squared distance
    • paint

      public boolean paint(boolean force)
      Description copied from class: AbstractGraph
      Draw the graph. For re-drawing the graph, set force to true.
      Overrides:
      paint in class AbstractGraph<double[]>
      Parameters:
      force - true to force re-drawing of graph
      Returns:
      true if painting skipped
    • paintS3

      private void paintS3(boolean withMarkers)
      Paint the trajectory in the simplex. If withMarkers is true the start and end points of the trajectory are marked with green and red circles, respectively.
      Parameters:
      withMarkers - true to mark start and end points
    • export

      public void export(AbstractGraph.MyContext2d ctx)
      Description copied from class: AbstractGraph
      Export the graphical context ctx.
      Specified by:
      export in class AbstractGraph<double[]>
      Parameters:
      ctx - the graphical context to export
    • calcBounds

      public void calcBounds(int width, int height)
      Description copied from class: AbstractGraph
      Calculate bounds of drawing area.
      Overrides:
      calcBounds in class AbstractGraph<double[]>
      Parameters:
      width - the width of the drawing area
      height - the height of the drawing area
    • drawFrame

      public void drawFrame(int sLevels)
      Draws the frame of the simplex. The corners are marked by the trait names in their respective colours. For visual guidance each side is subdivided into sLevels sublevels.
      Parameters:
      sLevels - the number of sublevels for the frame
    • onDoubleClick

      public void onDoubleClick(DoubleClickEvent event)
      Specified by:
      onDoubleClick in interface DoubleClickHandler
    • onTouchStart

      public void onTouchStart(TouchStartEvent event)
      Description copied from class: AbstractGraph

      The graph reacts to different kinds of touches: short touches or taps (&lt;250 msec) and long touches (&gt;250 msec). Long touches trigger different actions depending on the number of fingers:

      Single finger
      Initiate shifting the view
      Two fingers
      Initiate pinching zoom.
      Specified by:
      onTouchStart in interface TouchStartHandler
      Overrides:
      onTouchStart in class AbstractGraph<double[]>
      See Also:
    • scaledX

      double scaledX(double x)
      Convert the x-component of the screen coordinates (x, y) into scaled (Cartesian) coordinates in [0, 1].
      Parameters:
      x - the x-coordinate on screen
      Returns:
      the scaled coordinate
    • scaledY

      double scaledY(double y)
      Convert the y-component of the screen coordinates (x, y) into scaled (Cartesian) coordinates in [0, 1].
      Parameters:
      y - the y-coordinate on screen
      Returns:
      the scaled coordinate
    • processInitXY

      private void processInitXY(int x, int y)
      Helper method to convert screen coordinates into an initial configuration and set the controller's initial state.
      Parameters:
      x - the x-coordinate on screen
      y - the y-coordinate on screen
    • getTooltipAt

      public String getTooltipAt(int x, int y)
      Description copied from interface: Tooltip.Provider
      Get the tooltip information for the location with coordinates (x, y). The returned string may include HTML elements for formatting.
      Specified by:
      getTooltipAt in interface Tooltip.Provider
      Parameters:
      x - the x-coordinate for the tooltip
      y - the y-coordinate for the tooltip
      Returns:
      the (formatted) string with the tooltip info
    • inside

      protected boolean inside(double sx, double sy)
      Check if point (in scaled user coordinates) lies inside of simplex.
      Parameters:
      sx - x-coordinate of point
      sy - y-coordinate of point
      Returns:
      true if inside
    • populateContextMenuAt

      public void populateContextMenuAt(ContextMenu menu, int x, int y)
      Description copied from class: AbstractGraph
      Populate context menu menu in listening widget at (relative) position (x,y).

      Adds buffer size menu and queries the controller to add further functionality.

      Specified by:
      populateContextMenuAt in interface ContextMenu.Provider
      Overrides:
      populateContextMenuAt in class AbstractGraph<double[]>
      Parameters:
      menu - context menu entries are added here
      x - horizontal coordinate (relative to listening widget)
      y - horizontal coordinate (relative to listening widget)
      See Also:
    • closestCorner

      protected int closestCorner(double x, double y)
      Find the corner closest to the point (x, y).
      Parameters:
      x - the x-coordinate of the point
      y - the y-coordinate of the point
      Returns:
      the index of the closest corner
    • closestEdge

      protected int closestEdge(double x, double y)
      Find the edge closest to the point (x, y).
      Parameters:
      x - the x-coordinate of the point
      y - the y-coordinate of the point
      Returns:
      the index of the closest edge
    • exportTrajectory

      public void exportTrajectory(StringBuilder export)
      Description copied from interface: AbstractGraph.HasTrajectory
      Export the trajectory of the graph to export.
      Specified by:
      exportTrajectory in interface AbstractGraph.HasTrajectory
      Parameters:
      export - the string builder to export the trajectory