Class PlotBuilder<T extends PlotBuilder<?>>

java.lang.Object
org.moeaframework.analysis.plot.PlotBuilder<T>
Type Parameters:
T - the specific plot builder type
Direct Known Subclasses:
BarGraphBuilder, BoxAndWhiskerPlotBuilder, HeatMapBuilder, SensitivityPlotBuilder, XYPlotBuilder

public abstract class PlotBuilder<T extends PlotBuilder<?>> extends Object
Base class for constructing plots using a builder pattern.

These plot builders are intended for quickly generating plots from common data structures produced by this library. The ability to customize the plot style is therefore limited. If different plotting options or styles is required, you will need to utilize the underlying graphing library, JFreeChart, or a different library of your choice.

  • Field Details

    • paintHelper

      protected PaintHelper paintHelper
      Maps labels to their assigned color. This can be assigned an externally managed paint helper to keep colors synchronized.
  • Constructor Details

    • PlotBuilder

      public PlotBuilder()
      Constructs a new plot builder with default settings.
  • Method Details

    • setDisplayDriver

      public static void setDisplayDriver(PlotBuilder.DisplayDriver displayDriver)
      Sets the display driver, which will be used by subsequent calls to show(int, int) when displaying this plot.
      Parameters:
      displayDriver - the new display driver
    • getDisplayDriver

      public static PlotBuilder.DisplayDriver getDisplayDriver()
      Returns the current display driver.
      Returns:
      the current display driver
    • getInstance

      protected abstract T getInstance()
      Returns an instance of this builder cast to the concrete implementation type PlotBuilder. This is used by any intermediate classes to returns the concrete implementation.
      Returns:
      a reference to this builder instance
    • build

      public abstract org.jfree.chart.JFreeChart build()
      Builds and returns the configured plot.
      Returns:
      the resulting plot
    • buildPanel

      public org.jfree.chart.ChartPanel buildPanel()
      Builds and returns a panel that can be added to a Swing UI.
      Returns:
      the panel
    • title

      public T title(String title)
      Sets the chart title.
      Parameters:
      title - the title
      Returns:
      a reference to this builder
    • subtitle

      public T subtitle(String subtitle)
      Sets the chart subtitle.
      Parameters:
      subtitle - the subtitle
      Returns:
      a reference to this builder
    • defaultLegend

      public T defaultLegend()
      Configures the plot with the default legend, if any, for the chart type. This is the default behavior.
      Returns:
      a reference to this builder
    • noLegend

      public T noLegend()
      Configures the plot to not display a legend.
      Returns:
      a reference to this builder
    • legend

      public T legend(Function<org.jfree.chart.plot.Plot,org.jfree.chart.title.LegendTitle> legendBuilder)
      Provides a function to generate a custom legend for the plot.
      1. If the function is null, the default plot is generated for the plot type.
      2. If the function returns null, no legend is displayed in the plot.
      3. Otherwise, the returned legend is displayed in the plot.
      Parameters:
      legendBuilder - the function to create the legend
      Returns:
      a reference to this builder
    • theme

      public T theme(org.jfree.chart.ChartTheme theme)
      Sets the overall theme for the chart, specifying styles for titles, labels, axes, etc.
      Parameters:
      theme - the theme, or null to use the default theme
      Returns:
      a reference to this builder
    • paintHelper

      public T paintHelper(PaintHelper paintHelper)
      Sets the PaintHelper for assigning paints / colors to individual series. The paint helper instance is updated with any new mappings.
      Parameters:
      paintHelper - the paint helper
      Returns:
      a reference to this builder
    • save

      public T save(String filename) throws IOException
      Saves the plot to an image file. The type of image is determined from the filename extension, which must match one of the supported file types in ImageFileType.
      Parameters:
      filename - the filename
      Returns:
      a reference to this builder
      Throws:
      IOException - if an I/O error occurred
    • save

      public T save(File file) throws IOException
      Saves the plot to an image file. The type of image is determined from the filename extension, which must match one of the supported file types in ImageFileType.
      Parameters:
      file - the file
      Returns:
      a reference to this builder
      Throws:
      IOException - if an I/O error occurred
    • save

      public T save(File file, int width, int height) throws IOException
      Saves the plot to an image file.
      Parameters:
      file - the file
      width - the image width
      height - the image height
      Returns:
      a reference to this builder
      Throws:
      IOException - if an I/O error occurred
    • show

      public void show()
      Displays the chart.
    • show

      public void show(int width, int height)
      Displays the chart.
      Parameters:
      width - the width of the chart
      height - the height of the chart
    • build

      protected org.jfree.chart.JFreeChart build(org.jfree.chart.plot.Plot plot)
      Wraps the plot in a JFreeChart, which includes the title, legend, and the theme.
      Parameters:
      plot - the plot
      Returns:
      the chart
    • toArray

      protected double[] toArray(List<? extends Number> values)
      Converts a list of numbers into a double array. Any null values are translated into 0d/0d.
      Parameters:
      values - the list of numbers
      Returns:
      the double array
    • toArray

      protected <D, V extends Number> double[] toArray(List<D> data, Function<D,V> getter)
      Converts a list of objects into a double array.
      Type Parameters:
      D - the type of each object
      V - type numeric type returned by the getter
      Parameters:
      data - the list of objects
      getter - a function converting each object into a numeric value
      Returns:
      the double array
    • to2DArray

      protected double[][] to2DArray(List<? extends List<? extends Number>> values)
      Converts a 2D list of numbers into a 2D double array. Any null values are translated into 0d/0d.
      Parameters:
      values - the 2D list of numbers
      Returns:
      the 2D double array
    • applyStyle

      protected void applyStyle(org.jfree.chart.plot.Plot plot, int dataset, PlotAttribute... attributes)
      Applies the style attributes to all series in a dataset.
      Parameters:
      plot - the plot
      dataset - the index of the dataset
      attributes - the attributes configuring the plot
    • applyStyle

      protected void applyStyle(org.jfree.chart.plot.Plot plot, int dataset, int series, PlotAttribute... attributes)
      Applies the style attributes to the specified series in a dataset.
      Parameters:
      plot - the plot
      dataset - the index of the dataset
      series - the index of the series
      attributes - the attributes configuring the plot
    • getAttribute

      protected <A extends PlotAttribute> A getAttribute(Class<A> type, PlotAttribute... attributes)
      Returns the first attribute matching the specified type.
      Type Parameters:
      A - the type of the attribute
      Parameters:
      type - the class of the attribute
      attributes - the attributes configuring the plot
      Returns:
      the matching attribute, or null if no such attribute was found
    • getValueOrDefault

      protected <V, A extends ValueAttribute<V>> V getValueOrDefault(Class<A> type, V defaultValue, PlotAttribute... attributes)
      Returns the value stored in a ValueAttribute or the default value.
      Type Parameters:
      A - the type of the attribute
      V - the type of the value
      Parameters:
      type - the class of the value attribute
      defaultValue - the default value if the attribute is not set
      attributes - the attributes configuring the plot
      Returns:
      the value from the matching attribute, or the default value if no match was found
    • getValueOrDefault

      protected <V, A extends ValueAttribute<V>> V getValueOrDefault(Class<A> type, A defaultValue, PlotAttribute... attributes)
      Returns the value stored in a ValueAttribute or the default value.
      Type Parameters:
      A - the type of the attribute
      V - the type of the value
      Parameters:
      type - the class of the value attribute
      defaultValue - the default value if the attribute is not set
      attributes - the attributes configuring the plot
      Returns:
      the value from the matching attribute, or the default value if no match was found
    • hasAttribute

      protected boolean hasAttribute(Class<? extends PlotAttribute> type, PlotAttribute... attributes)
      Returns true if an attribute exists matching the specified type.
      Parameters:
      type - class of the attribute
      attributes - the attributes configuring the plot
      Returns:
      true if a matching attribute exists, false otherwise
    • xy

      public static XYPlotBuilder xy()
      Returns a new XY plot builder.
      Returns:
      the plot builder
    • barGraph

      public static BarGraphBuilder barGraph()
      Returns a new bar graph plot builder.
      Returns:
      the plot builder
    • boxAndWhisker

      public static BoxAndWhiskerPlotBuilder boxAndWhisker()
      Returns a new box-and-whisker plot builder.
      Returns:
      the plot builder
    • heatMap

      public static HeatMapBuilder heatMap()
      Returns a new heat map plot builder.
      Returns:
      the plot builder
    • sensitivity

      public static SensitivityPlotBuilder sensitivity()
      Returns a new sensitivity analysis result plot builder.
      Returns:
      the plot builder