Class LineGraph


  • public class LineGraph
    extends AbstractLineGraph

    A subclass of AbstractLineGraph that plots one or more separate curves - a typical Line Graph.

    This class changes the default for optionXAxisAtZero and optionYAxisAtZero to true.

    Here is a simple example that graphs two curves - the first, a set of datapoints defined by a DataCurve, the second a Spline curve that's fitted to the DataCurve.

        import org.faceless.graph.output.ImageOutput;
        import org.faceless.graph.math.*;
        import java.awt.Color;
    
        // Create a simple datacurve with 4 points.
        //
        DataCurve data = new DataCurve();
        data.set(1,10);
        data.set(2,15);
        data.set(3,8);
        data.set(5,11);
        
        // Plot the data in red on a linegraph, with a diamond
        // at each point.
        //
        LineGraph g = new LineGraph();
        g.setCurve("My Data", data, Color.red);
        g.setCurveMarker("My Data", LineGraph.MARKER_DIAMOND);
    
        // Add a "smoothed" version of this DataCurve in green.
        //
        Curve smooth = new Spline(Spline.CATMULLROM, data);
        g.setCurve("Smoothed Data", smooth, Color.green);
    
        // Add a title to the graph
        //
        g.optionTitle("My First Line Graph");
    
        // Render to an image that's 400x400
        //
        ImageOutput out = new ImageOutput(400,400);
        out.render(g);
     
    • Field Detail

      • KEY_SHOW_MARKERS

        public static final int KEY_SHOW_MARKERS
        If markers are to be displayed on the key, add this value to the parameter passed to Graph.optionDisplayKey(int). For example, linegraph.optionDisplayKey(Graph.KEY_BOXED_BOTTOM + LineGraph.KEY_SHOW_MARKERS)
        Since:
        1.0.7
        See Also:
        Constant Field Values
      • MARKER_CIRCLE

        public static final int MARKER_CIRCLE
        A marker in the shape of a circle
        Since:
        1.0.10
        See Also:
        Constant Field Values
      • MARKER_SQUARE

        public static final int MARKER_SQUARE
        A marker in the shape of a square
        See Also:
        Constant Field Values
      • MARKER_OCTAGON

        public static final int MARKER_OCTAGON
        A marker in the shape of an octagon
        See Also:
        Constant Field Values
      • MARKER_DIAMOND

        public static final int MARKER_DIAMOND
        A marker in the shape of a diamond
        See Also:
        Constant Field Values
      • MARKER_LINE

        public static final int MARKER_LINE
        A marker that looks like a join in the line. This is the default.
        See Also:
        Constant Field Values
      • MARKER_UPTRIANGLE

        public static final int MARKER_UPTRIANGLE
        A marker in the shape of a triangle pointing up
        Since:
        1.0.7
        See Also:
        Constant Field Values
      • MARKER_DOWNTRIANGLE

        public static final int MARKER_DOWNTRIANGLE
        A marker in the shape of a triangle pointing down
        Since:
        1.0.7
        See Also:
        Constant Field Values
      • MARKER_BIG

        public static final int MARKER_BIG
        Add this value to the marker type to double the size of the marker. For example, setCurveMarker("mycurve", MARKER_SQUARE+MARKER_BIG)
        Since:
        1.0.7
        See Also:
        Constant Field Values
      • MARKER_SMALL

        public static final int MARKER_SMALL
        Add this value to the marker type to reduce the size of the marker. For example, setCurveMarker("mycurve", MARKER_SQUARE+MARKER_SMALL)
        Since:
        1.0.10
        See Also:
        Constant Field Values
      • MARKER_NOBORDER

        public static final int MARKER_NOBORDER
        Add this value to the marker type to remove the border from around the marker. For example, setCurveMarker("mycurve", MARKER_CIRCLE+MARKER_NOBORDER)
        Since:
        1.0.10
        See Also:
        Constant Field Values
      • MARKERS_ONLY

        public static final int MARKERS_ONLY
        A special type of marker indicator that should be OR'ed with the marker type to indicate that only markers should be drawn - i.e. no lines, just markers. This needs to be used with SQUARES, OCTAGONS, DIAMONDS, CIRCLES and TRIANGLES only - the line will simply disappear if it's used with any other type. For example, to draw a small circle at each point but no line inbetween, do: setCurveMarker("mycurve", MARKER_CIRCLE+MARKER_SMALL+MARKERS_ONLY)
        See Also:
        Constant Field Values
    • Constructor Detail

      • LineGraph

        public LineGraph()
    • Method Detail

      • optionLineThickness

        public void optionLineThickness​(double val)
        Set the "thickness" of the lines when drawing two-dimensional graphs. This has no effect if the graph is rotated in the X or Y direction, and may not be supported by all subclasses of Output. It's mainly intended for plotting graphs to high-resolution output devices like PDF or PostScript.
      • complete

        protected void complete()
        Description copied from class: Graph
        Complete is the first method that's called by a Graph when it's rendered. It should take the data from the dataset and add the various elements that it needs to the Graph. The first thing it should do, however, is call super.complete().
        Overrides:
        complete in class Graph
      • plotLine

        protected double plotLine​(int set,
                                  Curve c,
                                  double prevlastval,
                                  double prevval,
                                  double lastx,
                                  double x,
                                  double linewidth,
                                  boolean isLast,
                                  java.util.Map m)
        Description copied from class: AbstractLineGraph
        Actually plot the line. This method needs to be filled in by concrete subclasses of this class. This method must handle NaN and Infinite values. All current subclasses deal with these by skipping those points and leaving a gap in the line.
        Specified by:
        plotLine in class AbstractLineGraph
        Parameters:
        set - The number of the set that's being plotted
        c - The curve that's being plotted
        prevlastval - The value returned by the last X value from the previous set. Set to Double.NaN if there was no previous set, or no last X value.
        prevval - The value returned by this X value from the previous set. Set to Double.NaN if no previous set.
        lastx - The last X value passed in. Set to Double.NaN if no previous X value exists.
        x - The current X value.
        linewidth - The width (or rather depth) of the line to be plotted.
        isLast - Whether this X value is the last value in the set.
        m - A map which can be used by plotLine to pass data between other calls to plotLine. A common use is to set a "start" value if this is the first X value in the set.