// ChangeAxis:
// A graph's display properties (axis characteristics, orientation, and
// so on) are general properties that are defined in the graph's model.
// Thus, to change a ScatterPlot's axis characteristics,
// 1) Create an AxisModel and any other objects needed to define
// axis characteristics (line style, tick intervals, ...)
// 2) Use the ScatterPlot to set the axis definitions on the chart
// This example creates a StrokeLineStyle to define a
// 3pt solid red line for both the X and Y axes. It
// creates an AxisModel to define a line style and tick
// characteristics for the Y axis.
import com.sas.graphics.components.scatterplot.ScatterPlot;
import com.sas.graphics.components.AxisModel;
import com.sas.graphics.components.scatterplot.ScatterPlotTableDataModel;
import com.sas.graphics.components.PlotVariable;
import com.sas.graphics.components.scatterplot.ScatterPlotModel;
import com.sas.graphics.components.GraphConstants;
import com.sas.graphics.components.StrokeLineStyle;
import com.sas.measures.BaseLength;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
public class ChangeAxis extends JPanel {
private ChangeAxis theApp;
public ChangeAxis () {
setLayout(new BorderLayout());
// Create a ScatterPlot and a data source
ScatterPlot scatterPlot=new ScatterPlot();
SampleData dataTable=new SampleData();
// 1) Create the objects needed to define axis characteristics.
// A StrokeLineStyle is needed to define line characteristics
// for both axes
StrokeLineStyle lineStyle=new StrokeLineStyle(
StrokeLineStyle.SASGRAPH_LINE01,
Color.red,
new BaseLength(3, "pt"),
GraphConstants.TRUE);
// An AxisModel is needed to set an define a line style and tick
// characteristics for the Y axis
AxisModel rAxis = new AxisModel();
rAxis.setAxisLineStyle(lineStyle);
rAxis.setContinuousMajorTickPositionPolicy(
GraphConstants.TICK_POSITION_INTERVAL);
rAxis.setContinuousMajorTickInterval(40);
rAxis.getMinorTickStyle().getLineStyle()
.setVisibilityPolicy(GraphConstants.FALSE);
// 2) Use the ScatterPlot to set the axis definitions on the chart
// Set the line color on the X axis
scatterPlot.getGraphModel().getXAxisModel()
.setAxisLineStyle(lineStyle);
// Set the AxisModel on the Y axis
scatterPlot.getGraphModel().setYAxisModel(rAxis);
// Create a data model and attach the data source
ScatterPlotTableDataModel dataModel=
new ScatterPlotTableDataModel();
dataModel.setModel(dataTable);
// Assign the X and Y variable roles
dataModel.setXVariable(
new PlotVariable("Height"));
dataModel.setYVariable(
new PlotVariable("Weight"));
// Assign the data model to the ScatterPlot
scatterPlot.setDataModel(dataModel);
// Set a graph title
scatterPlot.getTitle1().setText(
"Relation of Weight to Height");
add(scatterPlot, BorderLayout.CENTER);
}
// Create the data source
static private class SampleData extends DefaultTableModel {
private static Class columnClass[]={
String.class, String.class,
Double.class, Double.class, Double.class};
private static String columnNames[]={
"Name","Gender","Age","Height","Weight"};
public SampleData() {
super();
Object data[][] = {
{"Alfred", "M", new Double(14), new Double(69), new Double(112.5) },
{"Alice", "F", new Double(13), new Double(56.5), new Double(84) },
{"Barbara", "F", new Double(13), new Double(65.3), new Double(98) },
{"Carol", "F", new Double(14), new Double(62.8), new Double(102.5) },
{"Henry", "M", new Double(14), new Double(63.5), new Double(102.5) },
{"James", "M", new Double(12), new Double(57.3), new Double(83) },
{"Jane", "F", new Double(12), new Double(59.8), new Double(84.5) },
{"Janet", "F", new Double(15), new Double(62.5), new Double(112.5) },
{"Jeffrey", "M", new Double(13), new Double(62.5), new Double(84) },
{"John", "M", new Double(12), new Double(59), new Double(99.5) },
{"Joyce", "F", new Double(11), new Double(51.3), new Double(50.5) },
{"Judy", "F", new Double(14), new Double(64.3), new Double(90) },
{"Louise", "F", new Double(12), new Double(56.3), new Double(77) },
{"Mary", "F", new Double(15), new Double(66.5), new Double(112) },
{"Philip", "M", new Double(16), new Double(72), new Double(150) },
{"Robert", "M", new Double(12), new Double(64.8), new Double(128) },
{"Ronald", "M", new Double(15), new Double(67), new Double(133) },
{"Thomas", "M", new Double(11), new Double(57.5), new Double(85) },
{"William", "M", new Double(15), new Double(66.5), new Double(112) }
};
setDataVector(data, columnNames);
}
public Class getColumnClass(int column) {
return columnClass[column];
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("BIP Graph Sample");
Container container = frame.getContentPane();
container.setLayout(new BorderLayout());
container.setBackground(Color.white);
JPanel bipPanel = new JPanel();
bipPanel.setLayout(new BorderLayout());
ChangeAxis bipGraphSample = new ChangeAxis ();
bipPanel.add(bipGraphSample, BorderLayout.CENTER);
container.add(bipPanel, BorderLayout.CENTER);
frame.setSize(600,450);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
frame.setVisible(true);
}
}