The minimum specification for a LinePlot requires a valid data model. Generally the data model should assign the X and Y variable roles to PlotVariables in the data. If the X role is not assigned, the LinePlot automatically assigns it to the first numeric column found in the data model, or to the first string column if the model has no numeric column. If the Y role is not assigned, the LinePlot selects the first numeric column that is different from X and uses it as the Y variable. If no valid numeric column is found, then it selects the first string column that is different from X and uses it in the Y role.
If multiple variables are assigned the X role or the Y role, a separate plot line is generated for each variable. If X and Y both have multiple variables, then a line is produced for each pair of X and Y values. For example, if X has values x1, x2, x3 and Y has values y1, y2, y3, then there will be three lines: one line for (x1, y1), another for (x2, y2), and a third for (x3, y3). With multiple variables assigned X and/or Y roles, all plot lines use the same set of axes. If a group variable is specified, different colored lines are used for each plot generated by the group values.
If ColumnVariable and/or RowVariable roles are specified, a separate plot is produced for each classification value, and each plot has its own axes. The plots are arranged in a column for the ColumnVariable role, in a row for the RowVariable role, or in a graph matrix if both columns and rows are used. In the graph matrix, a separate graph is produced for each (column, row) value pair. You can control the arrangement and text of the labels and values that identify the column and row axes.
Data points in a LinePlot can be joined by straight lines, smooth lines, or step functions. Line appearance can be customized for style, color, and width.
If High and/or Low arguments are specified on a PlotVariable's constructor, up and down errors are generated if the variable is assigned the Y role; left and right errors are generated if the variable is assigned the X role. In either case, the error values must be numeric. In the plot, errors are joined to their respective markers. You can define styles for the error marker and error line.
A line plot's data properties are defined in a data model that descends from LinePlotDataModel, an abstract class whose subclasses provide a handle to the data and determine the number and arrangement of data elements (plot points). LinePlotDataModel has the following subclasses:
A LinePlot's individual display properties can be set by calling the LinePlot's getGraphModel() method, which returns a LinePlotModel that can be used to set the desired properties. The LinePlotModel class contains
Although modifying the LinePlotModel provides considerable control, it can be tedious to modify properties individually. For example, graphs distinguish between different types of text, such as label text, value text, data-label text, and title text. The text might also apply to different locations, such as the response axis, response2 axis, category axis, and legend. Whereas the LinePlotModel can independently control these different text types and locations, doing so requires you to apply the same properties to all applicable locations.
An easier way to set a LinePlot's display properties is to apply a pre-defined GraphStyle to the plot. A graph style sets many of the graph's visual characteristics, such as its use of colors, fonts, background, transparency, drop shadows, and more. A graph style can be used as is or modified through the GraphStyle class.
A graph style can be specified as an argument on a LinePlot's constructor. Or, it can be applied to an existing plot by calling the LinePlot's applyGraphStyle() method as follows:
LinePlot linePlot = new LinePlot();
linePlot.applyGraphStyle(new GraphStyle(GraphStyle.STYLE_SCIENCE);
Applying a GraphStyle to a graph propagates the GraphStyle's properties onto all applicable graph properties, including those in the graph model.
Rather than use the style as is, you can modify it before applying it. Modifying the style definition is typically easier and more convenient than modifying the graph's individual display properties. For example, the GraphStyle class contains only a single set of value text propertes. After applying the modified style, you can still adjust individual properties through the graph model.
The following code fragment modifies a graph style before applying it to a graph, and then uses the graph model to adjust an individual property.
// Create and modify a graph style
GraphStyle graphStyle = new GraphStyle(GraphStyle.STYLE_SCIENCE);
graphStyle.getValueTextStyle().setColor(Color.darkGray);
graphStyle.getValueTextStyle().setFont(new java.awt.Font("Arial",Font.BOLD,14));
// Apply the modified graph style to a plot
LinePlot linePlot = new LinePlot();
linePlot.applyGraphStyle(graphStyle);
// Set the category axis value text to black
LinePlotModel graphModel=linePlot.getGraphModel();
graphModel.getCategoryAxisModel().getValueTextStyle().setColor(Color.black);
Whether you set a graph style or individual display properties, the new display settings override any previous settings that were applied to the graph. For example, if a gray background and yellow markers have been set on a graph, and then a style is applied to that same graph, the style's background and marker settings replace the previously set gray background and yellow markers.
The LinePlot component supports four titles and two footnotes, each with its own display attributes. To display multiple lines of text with the same attributes, use the line-end character (\n) in the specified text string.
To assign a title or footnote,
The easiest way to set the text for a title or footnote is to get the plot's title or footnote property and set its text as follows:
linePlot.getTitle1().setText("Line One\nLine Two");
To change the text attributes for a title or footnote, set the attributes on a NoteModel and assign the NoteModel to the appropriate title or footnote. The following example sets the title "Product Revenue" and displays it in blue, using a 24-point TimesRoman font with a drop shadow.
NoteModel myTitle = new NoteModel( "Product Revenue" );
myTitle.getTextStyle().setColor(Color.blue);
myTitle.getTextStyle().setFont(new java.awt.Font("TimesRoman",Font.PLAIN,24));
myTitle.getTextStyle().getShadowStyle().setVisible(true);
// Create the graph and assign the title
LinePlot linePlot = new LinePlot();
linePlot.setTitle1( myTitle );
The following example compares various stock markets with the Dow Jones Industrial Average. Data used are from January 1988 to December 1990. In the example, the TableModel that is graphed contains numeric data columns of DowJones, NYSE and Wilshire 500. the example assigns the Gears GraphStyle to the plot.
javax.swing.table.TableModel mySalesData = <...>;
// Create the LinePlot's data model.
LinePlotTableDataModel linePlotDataModel = new LinePlotTableDataModel( myStockData );
linePlotDataModel.setXVariable( new PlotVariable("DowJones") );
linePlotDataModel.setYVariable( new PlotVariable("NYSE") );
linePlotDataModel.setY2Variable( new PlotVariable("Wilshire 500") );
// Create the LinePlot applying the data model and the "Gears" GraphStyle
LinePlot linePlot = new LinePlot( linePlotDataModel, new GraphStyle(GraphStyle.STYLE_GEARS) );
linePlot.getTitle1().setText("Dow Jones vs Other Markets") );
linePlot.getTitle2().setText("Values are % gain from Jan-88") );
linePlot.getFootnote1().setText("source: sashelp:Citibase daily indicaters") );
Now consider that the TableModel also contains columns for U.K.-All Shares and Nikkei-DOW. The example organizes the graph by Dow Jones Composite and other market indices.
javax.swing.table.TableModel myStockData = <...>;
// Create the LinePlot's data model.
LinePlotTableDataModel linePlotDataModel = new LinePlotTableDataModel( myStockData );
linePlotDataModel.setXVariable( new PlotVariable("DowJones") );
linePlotDataModel.setYVariable( new PlotVariableList(new PlotVarible[] {
PlotVariable("NYSE")
,PlotVariable("Wilshire 500")
});
linePlotDataModel.setY2Variable( new PlotVariableList(new PlotVarible[] {
PlotVariable("U.K.-All Shares")
,PlotVariable("Nikkei-DOW")
});
linePlot.getTitle1().setText("Dow Jones vs Other Market Indices");
linePlot.getTitle2().setText("Values are % gain from Jan-88");
linePlot.getFootnote1().setText("source: sashelp:Citibase daily indicaters");
//Set the axis labels
LinePlotModel linePlotModel = linePlot.getGraphModel();
linePlotModel.getYAxisModel().setLabel("NYSE & Wilshire");
linePlotModel.getY2AxisModel().setLabel("UK & Nikkei");
In the next example, the TableModel also contains a string data column for Year. To organize the graph by year, you could add following line to the previous example:
javax.swing.table.TableModel myStockData = <...>;
// Add a row role to the data model.
linePlotDataModel.setRowVariable( new ClassificationVariable("Year") );
The following example uses a dataset with errorColumns:
// Create the LinePlot's data model.
LinePlotTableDataModel linePlotDataModel = new LinePlotTableDataModel( myData );
linePlotDataModel.setGroupVariable(new ClassificationVariable("S1", null , null, "S1Lab", GraphConstants.SORT_ASCENDING));
// Create x and y variables with low and high errors
PlotVariable pvx = new PlotVariable( "X" // ColumnName
, "$000.00" // format
, null // informat
, "Xlabel" // label
, "X1" // highColumnName
, "LeftError" // highLabel
, "X2" // lowColumnName
, "RightError" // lowLabel
);
PlotVariable pvy = new PlotVariable( "Y" // ColumnName
, "%0.00" // format
, null // informat
, "Ylabel" // label
, "Y1" // highColumnName
, "UpError" // highLabel
, "Y2" // lowColumnName
, "DownError" // lowLabel
);
// Attach variables to the dataModel
linePlotDataModel.setXVariable(pvx);
linePlotDataModel.setYVariable(pvy);
// Create the LinePlot applying the data model and the "Statistical" GraphStyle
LinePlot linePlot = new LinePlot( linePlotDataModel, new GraphStyle(GraphStyle.STYLE_STATISTICAL) );
linePlot.getTitle1().setText("Plot showing high and low confidence values") );
The LinePlot asynchronously updates when a PropertyChangeEvent is received from any of its models (DataModel, GraphModel or NoteModels).