LineChart: Minimum Specification

The minimum specification for a LineChart requires a valid data model. Generally the data model should assign the Category variable role to a ClassificationVariable and the Response variable role to an AnalysisVariable. If the Response role is not assigned, the LineChart 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 Category role is not assigned, the LineChart selects the first string column and uses it as the Category variable. If no valid string column is found, then it selects the first numeric column different from the Response and uses it in the Category role.

Example
Basic Requirements for Creating a LineChart: Swing-based code, Servlet-based code

LineChart: Functional Overview

If multiple variables are assigned the Response role, a separate chart line is generated for each variable. With multiple Response variables, all chart lines use the same set of axes.

If ColumnVariable and/or RowVariable roles are specified, a separate chart is produced for each classification value, and each chart has its own axes. The charts 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 LineChart can be joined by straight lines, smooth lines, or step functions. Line appearance can be customized for style, color, and width.

LineChart: Data Access

A line chart's data properties are defined in a data model that descends from LineChartDataModel, an abstract class whose subclasses provide a handle to the data and determine the number and arrangement of data elements (plot points). LineChartDataModel has the following subclasses:

LineChart: Display Properties

A LineChart's individual display properties can be set by calling the LineChart's getGraphModel() method, which returns a LineChartModel that can be used to set the desired properties. The LineChartModel class contains

Although modifying the LineChartModel 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 LineChartModel 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 LineChart's display properties is to apply a pre-defined GraphStyle to the chart. 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 LineChart's constructor. Or, it can be applied to an existing chart by calling the LineChart's applyGraphStyle() method as follows:


LineChart lineChart = new LineChart();
lineChart.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 chart
LineChart lineChart = new LineChart();
   lineChart.applyGraphStyle(graphStyle);

// Set the category axis value text to black
LineChartModel graphModel=lineChart.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.

LineChart: Titles and Footnotes

The LineChart 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 chart's title or footnote property and set its text as follows:


lineChart.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
 LineChart lineChart = new LineChart();
 lineChart.setTitle1( myTitle );

LineChart: Usage Examples

The following example fragment graphs product revenue. It uses relational data from a Swing TableModel that contains a string column of Product IDs and a numeric column of product Revenues. To enhance the graph's appearance, the example assigns the "Magnify" GraphStyle to the chart.


javax.swing.table.TableModel mySalesData = <...>;

// Create the LineChart's data model.
LineChartTableDataModel lineChartDataModel = new LineChartTableDataModel( mySalesData );
lineChartDataModel.setCategoryVariable( new ClassificationVariable("Product") );
lineChartDataModel.setResponseVariable( new AnalysisVariable("Revenue") );

// Create the LineChart applying the data model and the "Magnify" GraphStyle
LineChart lineChart = new LineChart( lineChartDataModel, new GraphStyle(GraphStyle.STYLE_MAGNIFY) );
lineChart.getTitle1().setText("Product Revenue");
lineChart.getFootnote1().setText("Company Confidential");

Assuming the TableModel also contains a column named Year (numeric or string) that identifies the year, and a column named ProductType (numeric or string) that identifies the product type, the following example organizes the graph by year and product type:


javax.swing.table.TableModel mySalesData = <...>;

// Create the LineChart's data model.
LineChartTableDataModel lineChartDataModel = new LineChartTableDataModel( mySalesData );
lineChartDataModel.setColumnVariable( new ClassificationVariable("Year") );
lineChartDataModel.setRowVariable( new ClassificationVariable("ProductType") );
lineChartDataModel.setCategoryVariable( new ClassificationVariable("Product") );
lineChartDataModel.setResponseVariable( new AnalysisVariable("Revenue") );

// Create the LineChart applying the data model and the "Magnify" GraphStyle
LineChart lineChart = new LineChart( lineChartDataModel,
  new GraphStyle(GraphStyle.STYLE_MAGNIFY) );
lineChart.getTitle1().setText("Product Revenue");
lineChart.getTitle2().setText("By Year and Product Type");
lineChart.getFootnote1().setText("Company Confidential");

Assuming the TableModel also contains a numeric data column named PredictedRevenue for predicted revenue, the following example compares predicted revenue to actual revenue by product organized by year and product type:


javax.swing.table.TableModel mySalesData = <...>;

// Create the LineChart's data model.
LineChartTableDataModel lineChartDataModel = new LineChartTableDataModel( mySalesData );
lineChartDataModel.setColumnVariable( new ClassificationVariable("Year") );
lineChartDataModel.setRowVariable( new ClassificationVariable("ProductType") );
lineChartDataModel.setCategoryVariable( new ClassificationVariable("Product") );
lineChartDataModel.setResponseVariable( new AnalysisVariableList(
  new String[] {"PredictedRevenue", "Revenue"} ) );

// Create the LineChart applying the data model and the "Magnify" GraphStyle
LineChart lineChart = new LineChart( lineChartDataModel,
  new GraphStyle(GraphStyle.STYLE_MAGNIFY) );
lineChart.getTitle1().setText("Predicted Revenue vs Actual Revenue");
lineChart.getTitle2().setText("By Year and Product Type");
lineChart.getFootnote1().setText("Company Confidential");

LineChart: Behavior

The LineChart asynchronously updates when a PropertyChangeEvent is received from any of its models (DataModel, GraphModel or NoteModels).