Package org.moeaframework.analysis.plot
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
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface for display drivers, which are responsible for displaying the resulting plot to the user, typically in some windowing system. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.protected void
applyStyle
(org.jfree.chart.plot.Plot plot, int dataset, PlotAttribute... attributes) Applies the style attributes to all series in a dataset.static BarGraphBuilder
barGraph()
Returns a new bar graph plot builder.static BoxAndWhiskerPlotBuilder
Returns a new box-and-whisker plot builder.abstract org.jfree.chart.JFreeChart
build()
Builds and returns the configured plot.protected org.jfree.chart.JFreeChart
build
(org.jfree.chart.plot.Plot plot) Wraps the plot in aJFreeChart
, which includes the title, legend, and the theme.org.jfree.chart.ChartPanel
Builds and returns a panel that can be added to a Swing UI.Configures the plot with the default legend, if any, for the chart type.protected <A extends PlotAttribute>
AgetAttribute
(Class<A> type, PlotAttribute... attributes) Returns the first attribute matching the specified type.static PlotBuilder.DisplayDriver
Returns the current display driver.protected abstract T
Returns an instance of this builder cast to the concrete implementation typePlotBuilder
.protected <V,
A extends ValueAttribute<V>>
VgetValueOrDefault
(Class<A> type, A defaultValue, PlotAttribute... attributes) Returns the value stored in aValueAttribute
or the default value.protected <V,
A extends ValueAttribute<V>>
VgetValueOrDefault
(Class<A> type, V defaultValue, PlotAttribute... attributes) Returns the value stored in aValueAttribute
or the default value.protected boolean
hasAttribute
(Class<? extends PlotAttribute> type, PlotAttribute... attributes) Returnstrue
if an attribute exists matching the specified type.static HeatMapBuilder
heatMap()
Returns a new heat map plot builder.Provides a function to generate a custom legend for the plot.noLegend()
Configures the plot to not display a legend.paintHelper
(PaintHelper paintHelper) Sets thePaintHelper
for assigning paints / colors to individual series.Saves the plot to an image file.Saves the plot to an image file.Saves the plot to an image file.static SensitivityPlotBuilder
Returns a new sensitivity analysis result plot builder.static void
setDisplayDriver
(PlotBuilder.DisplayDriver displayDriver) Sets the display driver, which will be used by subsequent calls toshow(int, int)
when displaying this plot.void
show()
Displays the chart.void
show
(int width, int height) Displays the chart.Sets the chart subtitle.theme
(org.jfree.chart.ChartTheme theme) Sets the overall theme for the chart, specifying styles for titles, labels, axes, etc.Sets the chart title.protected double[][]
Converts a 2D list of numbers into a 2D double array.protected double[]
Converts a list of numbers into a double array.protected <D,
V extends Number>
double[]Converts a list of objects into a double array.static XYPlotBuilder
xy()
Returns a new XY plot builder.
-
Field Details
-
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
Sets the display driver, which will be used by subsequent calls toshow(int, int)
when displaying this plot.- Parameters:
displayDriver
- the new display driver
-
getDisplayDriver
Returns the current display driver.- Returns:
- the current display driver
-
getInstance
Returns an instance of this builder cast to the concrete implementation typePlotBuilder
. 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
Sets the chart title.- Parameters:
title
- the title- Returns:
- a reference to this builder
-
subtitle
Sets the chart subtitle.- Parameters:
subtitle
- the subtitle- Returns:
- a reference to this builder
-
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
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.- If the function is
null
, the default plot is generated for the plot type. - If the function returns
null
, no legend is displayed in the plot. - Otherwise, the returned legend is displayed in the plot.
- Parameters:
legendBuilder
- the function to create the legend- Returns:
- a reference to this builder
- If the function is
-
theme
Sets the overall theme for the chart, specifying styles for titles, labels, axes, etc.- Parameters:
theme
- the theme, ornull
to use the default theme- Returns:
- a reference to this builder
-
paintHelper
Sets thePaintHelper
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
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 inImageFileType
.- Parameters:
filename
- the filename- Returns:
- a reference to this builder
- Throws:
IOException
- if an I/O error occurred
-
save
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 inImageFileType
.- Parameters:
file
- the file- Returns:
- a reference to this builder
- Throws:
IOException
- if an I/O error occurred
-
save
Saves the plot to an image file.- Parameters:
file
- the filewidth
- the image widthheight
- 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 chartheight
- the height of the chart
-
build
protected org.jfree.chart.JFreeChart build(org.jfree.chart.plot.Plot plot) Wraps the plot in aJFreeChart
, which includes the title, legend, and the theme.- Parameters:
plot
- the plot- Returns:
- the chart
-
toArray
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
Converts a list of objects into a double array.- Type Parameters:
D
- the type of each objectV
- type numeric type returned by the getter- Parameters:
data
- the list of objectsgetter
- a function converting each object into a numeric value- Returns:
- the double array
-
to2DArray
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
Applies the style attributes to all series in a dataset.- Parameters:
plot
- the plotdataset
- the index of the datasetattributes
- 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 plotdataset
- the index of the datasetseries
- the index of the seriesattributes
- the attributes configuring the plot
-
getAttribute
Returns the first attribute matching the specified type.- Type Parameters:
A
- the type of the attribute- Parameters:
type
- the class of the attributeattributes
- 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 aValueAttribute
or the default value.- Type Parameters:
A
- the type of the attributeV
- the type of the value- Parameters:
type
- the class of the value attributedefaultValue
- the default value if the attribute is not setattributes
- 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 aValueAttribute
or the default value.- Type Parameters:
A
- the type of the attributeV
- the type of the value- Parameters:
type
- the class of the value attributedefaultValue
- the default value if the attribute is not setattributes
- the attributes configuring the plot- Returns:
- the value from the matching attribute, or the default value if no match was found
-
hasAttribute
Returnstrue
if an attribute exists matching the specified type.- Parameters:
type
- class of the attributeattributes
- the attributes configuring the plot- Returns:
true
if a matching attribute exists,false
otherwise
-
xy
Returns a new XY plot builder.- Returns:
- the plot builder
-
barGraph
Returns a new bar graph plot builder.- Returns:
- the plot builder
-
boxAndWhisker
Returns a new box-and-whisker plot builder.- Returns:
- the plot builder
-
heatMap
Returns a new heat map plot builder.- Returns:
- the plot builder
-
sensitivity
Returns a new sensitivity analysis result plot builder.- Returns:
- the plot builder
-