Class AbstractGraph<B>

Type Parameters:
B - the type of buffer backing the graph: typically this is double[] but in some cases Color[], String[] or MeshLambertMaterial[] for Pop2D, PopGraph2D or PopGraph3D, respectively.
All Implemented Interfaces:
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>, ContextMenu.Listener, ContextMenu.Provider, Tooltip.Provider
Direct Known Subclasses:
GenericPopGraph, HistoGraph, LineGraph, ParaGraph, S3Graph

The base class for all graphical representations. The class provides the basic functionality for drawing graphs, handling mouse and touch events, and displaying tooltips and context menus. It also provides the default implementations for zooming and shifting the view of the graph.
Author:
Christoph Hauert
  • Field Details Link icon

    • doubleClickHandler Link icon

      HandlerRegistration doubleClickHandler
      The handler for DoubleClickEvents.
    • mouseOutHandler Link icon

      HandlerRegistration mouseOutHandler
      The handler for MouseOutEvents.
    • mouseDownHandler Link icon

      HandlerRegistration mouseDownHandler
      The handler for MouseDownEvents.
    • mouseUpHandler Link icon

      HandlerRegistration mouseUpHandler
      The handler for MouseUpEvents.
    • mouseMoveHandler Link icon

      HandlerRegistration mouseMoveHandler
      The handler for MouseMoveEvents.
    • mouseWheelHandler Link icon

      HandlerRegistration mouseWheelHandler
      The handler for MouseWheelEvents.
    • touchStartHandler Link icon

      HandlerRegistration touchStartHandler
      The handler for TouchStartEvents.
    • touchEndHandler Link icon

      HandlerRegistration touchEndHandler
      The handler for TouchEndEvents.
    • touchMoveHandler Link icon

      HandlerRegistration touchMoveHandler
      The handler for TouchMoveEvents.
    • zoomFactor Link icon

      protected double zoomFactor
      The current zoom level.
    • viewCorner Link icon

      protected Point2D viewCorner
      The coordinates of the lower left corner visible on the canvas.
    • controller Link icon

      protected AbstractGraph.Controller controller
      The controller of this graph.
    • shifter Link icon

      The controller for shifting this graph.
    • zoomer Link icon

      The controller for zooming this graph.
    • contextMenu Link icon

      protected ContextMenu contextMenu
      The reference to the (shared) context menu.
    • tooltip Link icon

      protected Tooltip tooltip
      The reference to the (shared) tooltip.
    • tooltipProvider Link icon

      BasicTooltipProvider tooltipProvider
      The provider for tooltips.
    • scale Link icon

      protected double scale
      The scale of this graph. Used to translate width and height into canvas coordinates. For example, on retina displays the scale is typically 2, i.e. two pixels per unit width or height.
      See Also:
    • wrapper Link icon

      LayoutPanel wrapper
      The wrapper element which displays the canvas. Subclasses may use this to add further elements to the graph such as labels.
    • logger Link icon

      protected Logger logger
      Logger for keeping track of and reporting events and issues.
    • element Link icon

      protected Element element
      Handle to the object's underlying DOM element.
    • canvas Link icon

      protected Canvas canvas
      Handle to the canvas for drawing the graphics.
    • g Link icon

      Handle to the graphical context for drawing on the canvas.
    • colors Link icon

      protected String[] colors
      The array of colors used for drawing the graph.
    • markers Link icon

      protected ArrayList<double[]> markers
      Markers for decorating the graph.
    • markerColors Link icon

      protected String[] markerColors
      The array of colors used for markers.
    • module Link icon

      protected Module module
      The module backing the graph.
    • isActive Link icon

      protected boolean isActive
      The flag to indicate whether the graph is live (visible, activated).
    • bounds Link icon

      protected Rectangle2D bounds
      The bounds of the displaying area. This excludes any frames and/or axis labels.
    • style Link icon

      protected AbstractGraph.GraphStyle style
      The style of the graph. Includes labels, fonts, etc.
    • buffer Link icon

      protected RingBuffer<B> buffer
      The buffer to store historical data, if applicable.
    • MIN_BUFFER_SIZE Link icon

      protected static final int MIN_BUFFER_SIZE
      The minimum buffer size.
      See Also:
    • DEFAULT_BUFFER_SIZE Link icon

      protected static final int DEFAULT_BUFFER_SIZE
      The default buffer size. Must be at least MIN_BUFFER_SIZE.
      See Also:
    • MIN_MSEC_BETWEEN_UPDATES Link icon

      public static final int MIN_MSEC_BETWEEN_UPDATES
      The minimum time between updates in milliseconds.
      See Also:
    • updatetime Link icon

      protected double updatetime
      The field to store the time of the last update.
    • bufferSizeMenu Link icon

      private ContextMenu bufferSizeMenu
      The context menu to set the buffer size for graphs with historical data.
    • bufferSizeTrigger Link icon

      private ContextMenuItem bufferSizeTrigger
      The context menu item which triggers the buffer size context menu.
    • hasZoom Link icon

      protected boolean hasZoom
      The flag to indicate whether the graph supports zoom. The default is no support.
    • zoomResetMenu Link icon

      ContextMenuItem zoomResetMenu
      The context menu item to reset the zoom level.
    • zoomInMenu Link icon

      ContextMenuItem zoomInMenu
      The context menu item to zoom in (enlarge) by a factor of 2.
    • zoomOutMenu Link icon

      ContextMenuItem zoomOutMenu
      The context menu item to zoom out (reduce) by a factor of 1/2.
    • TWOPI Link icon

      private static final double TWOPI
      Constant representing the numerical value of 2π
      See Also:
    • PIHALF Link icon

      protected static final double PIHALF
      Constant representing the numerical value of π/2
      See Also:
    • MAX_SEGEMENTS Link icon

      private static final int MAX_SEGEMENTS
      The maximum number of line segments to draw before returning control to the event loop.
      See Also:
    • hasMessage Link icon

      protected boolean hasMessage
      Flag to indicate whether the graph displays a message.
    • leftMouseButton Link icon

      protected boolean leftMouseButton
      The flag to indicate whether the left mouse button is pressed.
    • mouseX Link icon

      protected int mouseX
      The x-coordinate of the previous mouse or tap event.
    • mouseY Link icon

      protected int mouseY
      The y-coordinate of the previous mouse or tap event.
    • touchEndTime Link icon

      protected double touchEndTime
      The time when the previous touch ended.
    • pinchX Link icon

      protected int pinchX
      The x-coordinate of the center of the pinching gesture.
    • pinchY Link icon

      protected int pinchY
      The y-coordinate of the center of the pinching gesture.
    • pinchDist Link icon

      protected double pinchDist
      The distance between the pinching gesture.
    • zoomInertiaTimer Link icon

      protected Timer zoomInertiaTimer
      The timer to remove the CSS classes .evoludo-cursorZoomIn or .evoludo-cursorZoomIn, respectively, from the graph element after the inertia of zooming has worn off.
  • Constructor Details Link icon

    • AbstractGraph Link icon

      protected AbstractGraph(AbstractGraph.Controller controller, Module module)
      Create the base class for graphs. Allocates the canvas, retrieves the shared tooltip and context menu. Use the CSS class evoludo-Canvas2D for custom formatting of the canvas element.
      Parameters:
      controller - the controller of this graph
      module - the module backing the graph
  • Method Details Link icon

    • setTooltipProvider Link icon

      public void setTooltipProvider(BasicTooltipProvider tooltipProvider)
      Set the provider for tooltips.
      Parameters:
      tooltipProvider - the provider for tooltips
    • getBuffer Link icon

      public RingBuffer<B> getBuffer()
      Return the RingBuffer<double[]> containing historical data, if applicable.
      Returns:
      the buffer with historical data or null
    • doUpdate Link icon

      public boolean doUpdate()
      Determine whether it is time to update the display.
      Returns:
      true if time to update graph
    • activate Link icon

      public void activate()
      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.
      See Also:
    • deactivate Link icon

      public void deactivate()
      The graph is removed from the GUI. Opportunity for some clean up. Removes all mouse and touch handlers from graph.
      See Also:
    • onResize Link icon

      public void onResize()
      Specified by:
      onResize in interface RequiresResize
    • updateCanvas Link icon

      private void updateCanvas()
      Update the canvas size and coordinate space dimensions. IMPORTANT: Setting the canvas size or the coordinate space dimensions clears the canvas (even with no actual changes)!
    • reset Link icon

      public void reset()
      Reset the graph. Clear canvas and messages.
    • init Link icon

      public void init()
      Initialize the graph. Do not clear graph.
    • paint Link icon

      public void paint()
      Draw the graph.
    • getModule Link icon

      public Module getModule()
      Get the module that backs the graph.
      Returns:
      the module
    • paint Link icon

      public boolean paint(boolean force)
      Draw the graph. For re-drawing the graph, set force to true.
      Parameters:
      force - true to force re-drawing of graph
      Returns:
      true if painting skipped
    • setMarkers Link icon

      public void setMarkers(ArrayList<double[]> markers)
      Assign a list of markers to the graph.
      Parameters:
      markers - the list of markers
    • setMarkers Link icon

      public void setMarkers(ArrayList<double[]> markers, String[] colors)
      Assign a list of markers with custom colors to the graph.
      Parameters:
      markers - the list of markers
      colors - the list of custom colors
    • setColors Link icon

      public void setColors(String[] colors)
      Assign a list of colors to the graph.
      Parameters:
      colors - the list of colors
    • hasHistory Link icon

      public boolean hasHistory()
      Check whether the graph entertains a buffer with historical data.
      Returns:
      true if the graph stores the data history
    • clearHistory Link icon

      public void clearHistory()
      Clear the history of the graph (if there is one).
    • prependTime2Data Link icon

      protected double[] prependTime2Data(double t, double[] data)
      Utility method to prepend time to data point.
      Parameters:
      t - the time
      data - the data
      Returns:
      the new data array including time
    • populateContextMenuAt Link icon

      public void populateContextMenuAt(ContextMenu menu, int x, int y)
      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
      Parameters:
      menu - context menu entries are added here
      x - horizontal coordinate (relative to listening widget)
      y - horizontal coordinate (relative to listening widget)
      See Also:
    • setBufferCapacity Link icon

      protected void setBufferCapacity(int capacity)
      Sets the buffer capacity to capacity, if applicable.
      Parameters:
      capacity - the new buffer capacity
    • contains Link icon

      public boolean contains(int x, int y)
      Checks if the (browser) coordinates (x, y) are inside this graph. This is useful for controllers that may manage several graphs.
      Parameters:
      x - the x-coordinate to check
      y - the y-coordinate to check
      Returns:
      true if (x, y) is inside graph
    • convertToScaledCoordinates Link icon

      public Point2D convertToScaledCoordinates(int x, int y)
      Convert the (browser) coordinates (x, y) to scaled coordinates in [0,1]2.
      Parameters:
      x - the x-coordinate to convert
      y - the y-coordinate to convert
      Returns:
      the converted coordinates
    • convertToScaledCoordinates Link icon

      public Point2D convertToScaledCoordinates(int x, int y, Point2D dest)
      Convert the (browser) coordinates (x, y) to scaled coordinates in [0,1]2 and store in dest.
      Parameters:
      x - the x-coordinate to convert
      y - the y-coordinate to convert
      dest - the point to save the converted coordinates
      Returns:
      the converted coordinates
    • export Link icon

      public abstract void export(AbstractGraph.MyContext2d ctx)
      Export the graphical context ctx.
      Parameters:
      ctx - the graphical context to export
    • calcBounds Link icon

      protected boolean calcBounds()
      Calculate bounds of drawing area. If element has never been visible its size is not yet known.
      Returns:
      true if bounds are successfully calculated.
    • calcBounds Link icon

      public void calcBounds(int width, int height)
      Calculate bounds of drawing area.
      Parameters:
      width - the width of the drawing area
      height - the height of the drawing area
    • drawFrame Link icon

      protected void drawFrame(int xLevels, int yLevels)
      Draw the frame of the graph including axis labels, ticks and tick marks, as applicable.

      Implementation Note: Link icon

      The top, left corner of the canvas is assumed to be at (0, 0). Flips the direction of the y-axis.
      Parameters:
      xLevels - the number of vertical levels
      yLevels - the number of horizontal levels
      See Also:
    • drawFrame Link icon

      protected void drawFrame(int xLevels, int yLevels, double gscale)
      Draw the frame of the graph including axis labels, ticks and tick marks, as applicable, after scaling the canvas by gscale.

      Implementation Note: Link icon

      After scaling the origin (0, 0) is assumed to be in the bottom, left corner of the canvas.
      Parameters:
      xLevels - the number of vertical levels
      yLevels - the number of horizontal levels
      gscale - the scaling applied to the coordinate transformation
    • setFont Link icon

      protected void setFont(String cssfont)
      Set the font to cssfont in CSS format for drawing on the canvas.
      Parameters:
      cssfont - the font for drawing
    • setStrokeStyleAt Link icon

      protected void setStrokeStyleAt(int node)
      Set the stroke colour for node with index node.
      Parameters:
      node - the index of the node
    • stroke Link icon

      protected void stroke(Path2D path)
      Draw the path path.
      Parameters:
      path - the path to draw
    • fill Link icon

      protected void fill(Path2D path)
      Fill the path path.
      Parameters:
      path - the path to fill
    • sketch Link icon

      private void sketch(Path2D path, boolean fill)
      Draw or fill (if fill == true) the path path.

      Implementation notes: Link icon

      • Drawing too many segments at once becomes very slow (why?).
      • Periodically closing and stroking the path is orders of magnitudes faster!
      • This simple change renders freezing of the GUI essentially a non issue.
      • SVG seems to be a stickler about opening and closing paths or empty paths (see Canvas2SVG.js)
      Parameters:
      path - the path to draw (or fill)
      fill - true to fill path
    • closePath Link icon

      private void closePath(boolean fill)
      Helper method to close the path and draw (or fill) it.
      Parameters:
      fill - true to fill path
    • strokeRect Link icon

      protected void strokeRect(double x, double y, double w, double h)
      Draw the rectangle with origin at (x,y), width w and height h.
      Parameters:
      x - the x-coordinate of the origin
      y - the y-coordinate of the origin
      w - the width of the rectangle
      h - the height of the rectangle
    • fillRect Link icon

      protected void fillRect(double x, double y, double w, double h)
      Fill the rectangle with origin at (x,y), width w and height h.
      Parameters:
      x - the x-coordinate of the origin
      y - the y-coordinate of the origin
      w - the width of the rectangle
      h - the height of the rectangle
    • strokeCircle Link icon

      protected void strokeCircle(double x, double y, double radius)
      Draw the circle with the center at (x,y) and radius radius.
      Parameters:
      x - the x-coordinate of the center
      y - the y-coordinate of the center
      radius - the radius of the circle
    • fillCircle Link icon

      protected void fillCircle(double x, double y, double radius)
      Fill the circle with the center at (x,y) and radius radius.
      Parameters:
      x - the x-coordinate of the center
      y - the y-coordinate of the center
      radius - the radius of the circle
    • sketchCircle Link icon

      private void sketchCircle(double x, double y, double radius)
      Helper method to create a path for a circle with the center at (x,y) and radius radius.
      Parameters:
      x - the x-coordinate of the center
      y - the y-coordinate of the center
      radius - the radius of the circle
    • strokeLine Link icon

      protected void strokeLine(double sx, double sy, double ex, double ey)
      Draw a line from point (sx,sy) to (ex,ey).
      Parameters:
      sx - the x-coordinate of the start point
      sy - the y-coordinate of the start point
      ex - the x-coordinate of the end point
      ey - the y-coordinate of the end point
    • fillTextVertical Link icon

      protected void fillTextVertical(String msg, double x, double y)
      Draw filled text vertically starting at point (x,y)
      Parameters:
      msg - the text to write
      x - the x-coordinate of the start point
      y - the y-coordinate of the start point
    • clearGraph Link icon

      public void clearGraph()
      Clear the graph.
    • clearCanvas Link icon

      protected void clearCanvas()
      Clear the canvas.

      Implementation note: Link icon

      Assumes scaled canvas coordinates, i.e. lower, left corner at (0,0).
    • resetTransforms Link icon

      protected void resetTransforms()
      Reset the transformation of the graphics context to the identity transform.
    • displayMessage Link icon

      public boolean displayMessage(String msg)
      Display message msg on the graph (no HTML formatting).

      Implementation note: Link icon

      The message is centered and scaled such that it fills approximately two thirds of the width of the graph.
      Parameters:
      msg - the message to display
      Returns:
      true if message displayed
    • clearMessage Link icon

      public void clearMessage()
      Clear the message.
    • hasMessage Link icon

      public boolean hasMessage()
      Check if the graph displays a message.
      Returns:
      true if message displayed
    • zoom Link icon

      public void zoom()
      Reset zoom. Default implementation for graphs that implement Zooming.
      See Also:
    • zoom Link icon

      public void zoom(double zoom)
      Adjust zoom level by the factor zoom. Default implementation for graphs that implement Zooming.
      Parameters:
      zoom - the new zoom level
      See Also:
    • zoom Link icon

      public void zoom(double zoom, int x, int y)
      Adjust zoom level by the factor zoom with the center at coordinates (x,y) (in display coordinates as provided by event listeners). Default implementation for graphs that implement Zooming.
      Parameters:
      zoom - the new zoom level
      x - the x-coordinate of the zoom center
      y - the y-coordinate of the zoom center
      See Also:
    • zoom Link icon

      protected void zoom(double zoom, double fx, double fy)
      Helper method to adjust zoom level with the zoom center at the scaled coordinates (fx, fy), specified as a fraction of the view port in horizontal and vertical directions, respectively.
      Parameters:
      zoom - the new zoom level
      fx - the scaled x-coordinate of the zoom center
      fy - the scaled y-coordinate of the zoom center
      See Also:
    • shift Link icon

      public void shift(int dx, int dy)
      Shift the (zoomed) graph within the view port by (dx, dy). Default implementation for graphs that implement Shifting.
      Parameters:
      dx - the horizontal shift of the graph
      dy - the vertical shift of the graph
      See Also:
    • onMouseOut Link icon

      public void onMouseOut(MouseOutEvent event)

      If mouse leaves graph while shifting the view stop shifting. Mouse events are no longer received and hence it is impossible to track releasing or pressing mouse buttons.

      Specified by:
      onMouseOut in interface MouseOutHandler
    • onMouseDown Link icon

      public void onMouseDown(MouseDownEvent event)

      If a node has been hit by a left-click, remember the node's index and the current mouse coordinates. This information might be used by subsequent MouseMoveEvents.

      Specified by:
      onMouseDown in interface MouseDownHandler
      See Also:
    • onMouseUp Link icon

      public void onMouseUp(MouseUpEvent event)

      Cancel all interactions with the graph and reset node and mouse information.

      CSS Style Rules Link icon

      .evoludo-cursorMoveView
      removed from graph element.
      Specified by:
      onMouseUp in interface MouseUpHandler
      See Also:
    • onMouseMove Link icon

      public void onMouseMove(MouseMoveEvent event)

      If the left mouse-key is pressed the graph is shifted within its viewport. Note this requires that the zoom level of the graph exceeds 1.

      CSS Style Rules Link icon

      .evoludo-cursorMoveView
      added to graph element when shifting the view.
      Specified by:
      onMouseMove in interface MouseMoveHandler
      See Also:
    • onMouseWheel Link icon

      public void onMouseWheel(MouseWheelEvent event)

      Adjusts the zoom level of the graph, while preserving the location of the mouse on the graph. The zooming has inertia.

      CSS Style Rules Link icon

      .evoludo-cursorZoomIn
      added to graph element when zooming in and removed when inertia stops.
      .evoludo-cursorZoomOut
      added to graph element when zooming out and removed when inertia stops.
      Specified by:
      onMouseWheel in interface MouseWheelHandler
      See Also:
    • onTouchStart Link icon

      public void onTouchStart(TouchStartEvent event)

      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
      See Also:
    • onTouchEnd Link icon

      public void onTouchEnd(TouchEndEvent event)

      The number of touches on the graph changed.

      Specified by:
      onTouchEnd in interface TouchEndHandler
    • onTouchMove Link icon

      public void onTouchMove(TouchMoveEvent event)

      The graph reacts to different kinds of touch moves:

      Move one finger
      Shift view
      Pinch two fingers
      Zoom view
      Specified by:
      onTouchMove in interface TouchMoveHandler
    • addContextMenuHandler Link icon

      public HandlerRegistration addContextMenuHandler(ContextMenuHandler handler)
      Specified by:
      addContextMenuHandler in interface HasContextMenuHandlers
    • getStyle Link icon

      public AbstractGraph.GraphStyle getStyle()
      Get the graph style.
      Returns:
      the graph style
    • autoscale Link icon

      public void autoscale(boolean x, boolean y)
      Set auto-scaling for horizontal and vertical axes.
      Parameters:
      x - true to automatically scale the x-axis
      y - true to automatically scale the y-axis