Creating plots and charts

Chart objects

All capabilities of a chart are accessible after creating the chart object. A chart is created by specifiying the appropriate type (i.e. ScatterPlot, LinePlot, etc). The information about the data is passed as an argument to the object constructor. See more information about chart types.

A chart object is created by the constructor of the chart of the selected type class. The constructor syntax is as follows:

PlotType(dataObject)

where PlotType should ne substituted for the name of the appropriate plot class. The dataObject parameter specifies the data object which should be used for creating the plot.

Chart object methods

For each chart object the following methods are available:

Table 11.1. Methods for Chart objects

method name and syntaxdescription
show()
creates the chart window for previously specified data
setTitle(name)
sets the chart window title
getTitle()
returns the chart window title
getJFreeChart()
returns the associated object of the org.jfree.chart.plot.Plot class, which can be used for extended edition of chart properties. See the Plot class documentation for details.

Example 11.7. Preparing data and displaying a scatter plot

d = Data('tableName', ['x', 'y'] )
ScatterPlot(d).show()
                

The data object d can also be used to create other charts.

There is also a simpler way of creating charts ad-hoc. It is particulary convenient for creating a single chart, without creating a Data object.

Example 11.8. Creating an ad-hoc scatter plot

ScatterPlot('tableName', ['x', 'y']).show()
                

However, the chart data from this example cannot be reused.

Chart types

The three main chart types are described below.

Type 1 - 2D charts

Classes

The following 2D plot classes can be used:

  • ScatterPlot
  • LinePlot

Data preparation

The syntax for the Data constructor for use with 2D charts is the following:

Data(tableName, [x_column, y_column])

or

Data(tableName, [series_column_name, x_column, y_column])

Table 11.2.  Description of Data constructor parameters for 2D charts

Parameter Description
series_name_column the name of the containing the names of series to which the the coordinate pairs belong
x_column the name of the column with the x coordinate values
y_column the name of the column with the y coordinate values

Example 11.9. Creating a 2D plot

table 'scatter_plot':
    s  x     y
    a  0     0
    b  0     0.5
    a  0.5   0.25
    b  0.5   1
    a  1     1
    a  1.5   2.25
    b  1     1.5
    b  1.5   2
    a  2     4
    b  2     2.5

d = Data('scatter_plot', ['s', 'x', 'y'])
ScatterPlot(d).show()         
    

Figure 11.9. 2 series scatter plot

2 series scatter plot

Type 2 - 3D charts

Classes

The following 3D plot classes can be used:

  • BubblePlot

Data preparation

The syntax for the Data constructor for use with 3D charts is the following:

Data(tableName, [x_column, y_column, z_column])

or

Data(tableName, [series_column_name, x_column, y_column, z_column])

Table 11.3.  Description of Data constructor parameters for 3D charts

Parameter Description
series_name_column the name of the containing the names of series to which the the coordinate pairs belong
x_column the name of the column with the x coordinate values
y_column the name of the column with the y coordinate values
z_column the name of the column with the z coordinate values

Example 11.10. Creating a bubble plot

table 'bubble_plot':
    x    y    z
    1    2    0.3
    1.5  1.5  0.5
    2    1    0.1
    2.5  2.7  0.2
    3    3    0.2
    3.5  1.8  0.4
    4    2    0.15

d = Data('bubble_plot', ['x', 'y', 'z'])
BubblePlot(d).show()       
    

Figure 11.10. Bubble plot

Bubble plot

Note

The z-axis information is encoded in the size og the bubbles.

Type 3 - nominal data charts

Classes

The following plot classes can be used with nominal data:

  • CategoryPlot (bar chart)
  • PiePlot
  • ScatterCategory3DPlot (requires Java3D to be installed)
  • LineCategory3DPlot (requires Java3D to be installed)
  • Bar3DPlot (requires Java3D to be installed)
  • Pie3DPlot (requires Java3D to be installed)

Data preparation

The syntax for the Data constructor for use with 3D nominal charts is the following:

Data(tableName, [group_name_column, numeric_value_column])

Table 11.4.  Description of Data constructor parameters for 3D charts

Parameter Description
group_name_column the name of the column containing the group names
numeric_values_column the name of the column with the values for each group

Example 11.11. Creating a category plot

table 'category_plot':
    c2        c3
    'high'    55
    'high'    32
    'high'    40
    'medium'  24
    'medium'  28
    'medium'  20
    'low'      4
    'low'      8
    'low'     11

d = Data('category_plot', ['c2', 'c3'])
CategoryPlot(d).show()     
    

Figure 11.11. Category plot

Category plot

Grouping charts

It is possible to plot more than one chart on the same display with common x and y axes. The range of common axes will be set to show all the values from the grouped charts.

It is possible to group horizontally (common x axis) as well as vertically (common y axis)

Syntax:

CombinedVerticalXYPlot(xyplot1, xyplot2, ..., xyplotN)

CombinedHorizontalXYPlot(xyplot1, xyplot2, ..., xyplotN)

Example 11.12. Grouping plots

table 'table_A':
    a1  a2
    1   2
    3   4
                    
table 'table2':
    x   y
    1   2
    3   4
    
p1 = ScatterPlot('table_A', ['a1', 'a2'])
p2 = LinePlot('table2', ['x', 'y'] )
CombinedVerticalXYPlot(p1, p2).show()
    

Figure 11.12. Grouped plots

Grouped plots

Additional topics

This subsection describes some useful capabilities of the JFreeChart interface.

Logarithmic axis

The JFreeChart class provides a mechanism for using logarithmic axes. The library provides the BetterLogarithmic implementation via the BetterLogarithmicAxis function.

Example 11.13. Creating a plot with a logarithmic axis

table 'table_1':
    a1  a2
    1   2
    3   4

chartObject = ScatterPlot('table_1', ['a1', 'a2'])
chartObject.getJFreeChart().getPlot().setDomainAxis(BetterLogarithmicAxis('axis_name'))
chartObject.show()
    

Warning

Logarithmic axes require positive data values.

Markers

Below is an example of how to add a vertical marker labeled 'marker_label'

                        table 'table':
                        a1  a2
                        1   2
                        3 4
chartObject = ScatterPlot('table', ['a1', 'a2'])
plot = chartObject.getJFreeChart().getPlot()
m = org.jfree.chart.plot.ValueMarker(2)
m.label = 'marker_label'
plot.addDomainMarker(m)
chartObject.show()
                    

See also: org.jfree.chart.demo.MarkerDemo1.java