Package org.evoludo.graphics
Class S3Graph
Object
UIObject
Widget
Panel
SimplePanel
FocusPanel
AbstractGraph<double[]>
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
public class S3Graph
extends AbstractGraph<double[]>
implements AbstractGraph.Zooming, AbstractGraph.Shifting, AbstractGraph.HasTrajectory, DoubleClickHandler
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
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractGraph
AbstractGraph.Controller, AbstractGraph.GraphStyle, AbstractGraph.HasTrajectory, AbstractGraph.MyContext2d, AbstractGraph.Shifter, AbstractGraph.Shifting, AbstractGraph.ZoomCommand, AbstractGraph.Zoomer, AbstractGraph.Zooming
Nested classes/interfaces inherited from class UIObject
UIObject.DebugIdImpl, UIObject.DebugIdImplEnabled
Nested classes/interfaces inherited from interface HasWidgets
HasWidgets.ForIsWidget
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate double
Threshold for storing new data point in buffer.private ContextMenuItem
The context menu item to clear the canvas.private int
The index of the corner closest to the mouse pointer.(package private) Point2D
The lower left corner of the simplex.(package private) Point2D
The lower right corner of the simplex.(package private) Point2D
The upper corner of the simplex.(package private) double[]
The starting point of the most recent trajectory.(package private) S3Map
The map for converting data to simplex coordinates (cartesian).private static double
The minimum distance between two subsequent points in pixels.(package private) Path2D
The path that outlines the simplex.(package private) int
The identifier of the role of the data.private ContextMenu
The context menu to select the trait in the closest corner.(package private) static final double
Constant for \(\sqrt{2}\).private ContextMenuItem
The context menu item to swap the order of the traits along the closest edge.Fields inherited from class AbstractGraph
bounds, buffer, canvas, colors, contextMenu, controller, DEFAULT_BUFFER_SIZE, doubleClickHandler, element, g, hasMessage, hasZoom, isActive, leftMouseButton, logger, markerColors, markers, MIN_BUFFER_SIZE, MIN_MSEC_BETWEEN_UPDATES, module, mouseDownHandler, mouseMoveHandler, mouseOutHandler, mouseUpHandler, mouseWheelHandler, mouseX, mouseY, PIHALF, pinchDist, pinchX, pinchY, scale, shifter, style, tooltip, tooltipProvider, touchEndHandler, touchEndTime, touchMoveHandler, touchStartHandler, updatetime, viewCorner, wrapper, zoomer, zoomFactor, zoomInertiaTimer, zoomInMenu, zoomOutMenu, zoomResetMenu
Fields inherited from class UIObject
DEBUG_ID_PREFIX
Fields inherited from interface AbstractGraph.Zooming
ZOOM_INCR, ZOOM_MAX
-
Constructor Summary
ConstructorsConstructorDescriptionS3Graph
(AbstractGraph.Controller controller, Module module, int role) Create a new simplex \(S_3\) graph formodule
running incontroller
with the specifiedrole
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
activate()
Perform necessary preparations to show the graph in the GUI.void
addData
(double t, double[] data, boolean force) Add data to the graph.void
calcBounds
(int width, int height) Calculate bounds of drawing area.protected int
closestCorner
(double x, double y) Find the corner closest to the point(x, y)
.protected int
closestEdge
(double x, double y) Find the edge closest to the point(x, y)
.private double
distSq
(double[] vec, double[] buf) Helper method to calculate the distance squared between two vectors.void
drawFrame
(int sLevels) Draws the frame of the simplex.void
Export the graphical contextctx
.void
exportTrajectory
(StringBuilder export) Export the trajectory of the graph toexport
.getMap()
Get the map for converting data to simplex coordinates.getTooltipAt
(int x, int y) Get the tooltip information for the location with coordinates(x, y)
.protected boolean
inside
(double sx, double sy) Check if point (in scaled user coordinates) lies inside of simplex.void
onDoubleClick
(DoubleClickEvent event) void
onTouchStart
(TouchStartEvent event) boolean
paint
(boolean force) Draw the graph.private void
paintS3
(boolean withMarkers) Paint the trajectory in the simplex.void
populateContextMenuAt
(ContextMenu menu, int x, int y) Populate context menumenu
in listening widget at (relative) position(x,y)
.private void
processInitXY
(int x, int y) Helper method to convert screen coordinates into an initial configuration and set the controller's initial state.void
reset()
Reset the graph.(package private) double
scaledX
(double x) Convert thex
-component of the screen coordinates(x, y)
into scaled (Cartesian) coordinates in[0, 1]
.(package private) double
scaledY
(double y) Convert they
-component of the screen coordinates(x, y)
into scaled (Cartesian) coordinates in[0, 1]
.void
Set the map for converting data to simplex coordinates.Methods inherited from class AbstractGraph
addContextMenuHandler, autoscale, calcBounds, clearCanvas, clearGraph, clearHistory, clearMessage, contains, convertToScaledCoordinates, convertToScaledCoordinates, deactivate, displayMessage, doUpdate, drawFrame, drawFrame, fill, fillCircle, fillRect, fillTextVertical, getBuffer, getModule, getStyle, hasHistory, hasMessage, init, onMouseDown, onMouseMove, onMouseOut, onMouseUp, onMouseWheel, onResize, onTouchEnd, onTouchMove, paint, prependTime2Data, resetTransforms, setBufferCapacity, setColors, setFont, setMarkers, setMarkers, setStrokeStyleAt, setTooltipProvider, shift, stroke, strokeCircle, strokeLine, strokeRect, zoom, zoom, zoom, zoom
Methods inherited from class FocusPanel
addBlurHandler, addClickHandler, addClickListener, addDoubleClickHandler, addDragEndHandler, addDragEnterHandler, addDragHandler, addDragLeaveHandler, addDragOverHandler, addDragStartHandler, addDropHandler, addFocusHandler, addFocusListener, addGestureChangeHandler, addGestureEndHandler, addGestureStartHandler, addKeyboardListener, addKeyDownHandler, addKeyPressHandler, addKeyUpHandler, addMouseDownHandler, addMouseListener, addMouseMoveHandler, addMouseOutHandler, addMouseOverHandler, addMouseUpHandler, addMouseWheelHandler, addMouseWheelListener, addTouchCancelHandler, addTouchEndHandler, addTouchMoveHandler, addTouchStartHandler, getTabIndex, removeClickListener, removeFocusListener, removeKeyboardListener, removeMouseListener, removeMouseWheelListener, setAccessKey, setFocus, setTabIndex
Methods inherited from class SimplePanel
add, getContainerElement, getWidget, iterator, remove, setWidget, setWidget
Methods inherited from class Panel
add, adopt, clear, doAttachChildren, doDetachChildren, orphan, remove
Methods inherited from class Widget
addAttachHandler, addBitlessDomHandler, addDomHandler, addHandler, asWidget, asWidgetOrNull, createHandlerManager, delegateEvent, fireEvent, getHandlerCount, getLayoutData, getParent, isAttached, isOrWasAttached, onAttach, onBrowserEvent, onDetach, onLoad, onUnload, removeFromParent, setLayoutData, sinkEvents, unsinkEvents
Methods inherited from class UIObject
addStyleDependentName, addStyleName, ensureDebugId, ensureDebugId, ensureDebugId, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, onEnsureDebugId, removeStyleDependentName, removeStyleName, resolvePotentialElement, setElement, setElement, setHeight, setPixelSize, setSize, setStyleDependentName, setStyleName, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkBitlessEvent, toString
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface AbstractGraph.Shifter
shift
Methods inherited from interface AbstractGraph.Zoomer
zoom
Methods inherited from interface AbstractGraph.Zooming
zoom, zoom
Methods inherited from interface ContextMenu.Listener
getAbsoluteLeft, getAbsoluteTop
Methods inherited from interface HasHandlers
fireEvent
Methods inherited from interface HasTouchCancelHandlers
addTouchCancelHandler
Methods inherited from interface HasTouchEndHandlers
addTouchEndHandler
Methods inherited from interface HasTouchMoveHandlers
addTouchMoveHandler
Methods inherited from interface HasTouchStartHandlers
addTouchStartHandler
Methods inherited from interface Iterable
forEach, spliterator
Methods inherited from interface MouseDownHandler
onMouseDown
Methods inherited from interface MouseMoveHandler
onMouseMove
Methods inherited from interface MouseOutHandler
onMouseOut
Methods inherited from interface MouseUpHandler
onMouseUp
Methods inherited from interface MouseWheelHandler
onMouseWheel
Methods inherited from interface TouchEndHandler
onTouchEnd
Methods inherited from interface TouchMoveHandler
onTouchMove
-
Field Details
-
init
double[] initThe starting point of the most recent trajectory. -
e0
Point2D e0The lower left corner of the simplex. -
e1
Point2D e1The lower right corner of the simplex. -
e2
Point2D e2The upper corner of the simplex. -
outline
Path2D outlineThe path that outlines the simplex. -
map
S3Map mapThe map for converting data to simplex coordinates (cartesian). -
role
int roleThe identifier of the role of the data. -
SQRT_2
static final double SQRT_2Constant for \(\sqrt{2}\). Aspect ratio of equilateral triangle.- See Also:
-
bufferThreshold
private double bufferThresholdThreshold 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_PIXELSThe minimum distance between two subsequent points in pixels. -
swapOrderMenu
The context menu item to swap the order of the traits along the closest edge. -
clearMenu
The context menu item to clear the canvas. -
setTraitMenu
The context menu to select the trait in the closest corner. -
cornerIdx
private int cornerIdxThe index of the corner closest to the mouse pointer.
-
-
Constructor Details
-
S3Graph
Create a new simplex \(S_3\) graph formodule
running incontroller
with the specifiedrole
.- Parameters:
controller
- the controller of this graphmodule
- the module backing the graphrole
- 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 implementAbstractGraph.Zooming
orAbstractGraph.Shifting
interfaces.- Overrides:
activate
in classAbstractGraph<double[]>
- See Also:
-
setMap
Set the map for converting data to simplex coordinates.- Parameters:
map
- the conversion map
-
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 classAbstractGraph<double[]>
-
addData
public void addData(double t, double[] data, boolean force) Add data to the graph. The timet
is prepended to the data as the first element.- Parameters:
t
- the time of the datadata
- the data to addforce
-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
, unlessforce == true
.
-
distSq
private double distSq(double[] vec, double[] buf) Helper method to calculate the distance squared between two vectors.- Parameters:
vec
- the first vectorbuf
- 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, setforce
totrue
.- Overrides:
paint
in classAbstractGraph<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. IfwithMarkers
istrue
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
Description copied from class:AbstractGraph
Export the graphical contextctx
.- Specified by:
export
in classAbstractGraph<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 classAbstractGraph<double[]>
- Parameters:
width
- the width of the drawing areaheight
- 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 intosLevels
sublevels.- Parameters:
sLevels
- the number of sublevels for the frame
-
onDoubleClick
- Specified by:
onDoubleClick
in interfaceDoubleClickHandler
-
onTouchStart
Description copied from class:AbstractGraph
The graph reacts to different kinds of touches: short touches or taps (
<250
msec) and long touches (>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 interfaceTouchStartHandler
- Overrides:
onTouchStart
in classAbstractGraph<double[]>
- See Also:
-
scaledX
double scaledX(double x) Convert thex
-component of the screen coordinates(x, y)
into scaled (Cartesian) coordinates in[0, 1]
.- Parameters:
x
- thex
-coordinate on screen- Returns:
- the scaled coordinate
-
scaledY
double scaledY(double y) Convert they
-component of the screen coordinates(x, y)
into scaled (Cartesian) coordinates in[0, 1]
.- Parameters:
y
- they
-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
- thex
-coordinate on screeny
- they
-coordinate on screen
-
getTooltipAt
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 interfaceTooltip.Provider
- Parameters:
x
- thex
-coordinate for the tooltipy
- they
-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 pointsy
-y
-coordinate of point- Returns:
true
if inside
-
populateContextMenuAt
Description copied from class:AbstractGraph
Populate context menumenu
in listening widget at (relative) position(x,y)
.Adds buffer size menu and queries the controller to add further functionality.
- Specified by:
populateContextMenuAt
in interfaceContextMenu.Provider
- Overrides:
populateContextMenuAt
in classAbstractGraph<double[]>
- Parameters:
menu
- context menu entries are added herex
- 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
- thex
-coordinate of the pointy
- they
-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
- thex
-coordinate of the pointy
- they
-coordinate of the point- Returns:
- the index of the closest edge
-
exportTrajectory
Description copied from interface:AbstractGraph.HasTrajectory
Export the trajectory of the graph toexport
.- Specified by:
exportTrajectory
in interfaceAbstractGraph.HasTrajectory
- Parameters:
export
- the string builder to export the trajectory
-