JMSL Chart Programmer's Guide
Drawing Elements >> AxisXY >> Axis Label  Previous Page  Contents  Next Page

Axis Label

The AxisLabel node controls the labeling of an axis. The drawing of this node is controlled by the Text Attributes (see ).

Scientific Notation

If the values along an axis are large, scientific notation is more readable than a decimal format with many zeros. In this example the y-axis is labeled with scientific notation where each number has exactly two fractional digits displayed. This format pattern is "0.00E0". See java.text.DecimalFormat (http:// java.sun.com/j2se/1.3/docs/api/java/text/DecimalFormat.html)  for details on number formatting patterns.

(Download Code)
import com.imsl.chart.*;

public class SampleAxisLabel1 extends JFrameChart {
    
    public SampleAxisLabel1() {
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);
        axis.getAxisY().getAxisLabel().setTextFormat("0.00E0");
        double y[] = {4e6, 2e6, 3e6, 9e6};
        new Data(axis, y);
    }
    
    public static void main(String argv[]) {
        new SampleAxisLabel1().setVisible(true);
    }
}

Date Labels

If the TextFormat attribute for an axis is an instance of DateFormat, then the axis is scaled and labeled as a date/time axis, instead of as a real axis.

Date information passed to the Date constructor must be a double number representing the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT. This is used by the constructor for Date.

In this example the x-axis is labeled with dates. The setTextFormat(String) method translates the string "Date(MEDIUM)" into a date format string correct for the current locale (here U.S. English). If more control is desired, the method setTextFormat(Format) could be used with any Format object.

To make room for the text, the labels are rotated 90 degrees. Also, the viewport is changed to make more room for the x-axis labels.

Since the x-axis label TextFormat is a DateFormat, date/time autoscaling is used on the x-axis. The default real number autoscaling is still used on the y-axis.

(Download Code)
import com.imsl.chart.*;
import java.util.Date;
import java.util.GregorianCalendar;

public class SampleAxisLabelDate extends JFrameChart {
    
    public SampleAxisLabelDate() {        
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);
        axis.getAxisX().getAxisLabel().setTextFormat("Date(MEDIUM)");
        axis.getAxisX().getAxisLabel().setTextAngle(90);
        
        GregorianCalendar t[] = {
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 10),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 11),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 12),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 13)
        };
        double x[] = new double[4];
        for (int k = 0;  k < x.length;  k++)  x[k] = t[k].getTime().getTime();
        double y[] = {4, 2, 3, 9};
        new Data(axis, x, y);
        
        axis.setViewport(0.2, 0.9, 0.1, 0.7);
    }
    
    public static void main(String argv[]) {
        new SampleAxisLabelDate().setVisible(true);
    }
}

Skipping Weekends

An additional feature of Date axes is the ability to skip weekends. This feature is often needed for charting stock price data.

To skip weekends it is necessary to adjust the autoscaling for weekdays-only. This is done by setting the attribute SkipWeekends to true. It is also necessary to set a custom transformation, TransformDate, on the axis. This is shown in the following example, which is a modification of the example in the previous section.

Skipping weekends is intended only for data sets where no weekend data exists. It will not give the expected results if the data set contains weekend data.

This example also uses a custom SimpleDateFormat object to include the day of the week in the label.


(Download Code)
import com.imsl.chart.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class SampleWeekend extends JFrameChart {
    
    public SampleWeekend() {        
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);
        Axis1D axisX = axis.getAxisX();
        axisX.getAxisLabel().setTextFormat(new SimpleDateFormat("EEE MM/d/yy"));
        axisX.getAxisLabel().setTextAngle(90);
        
        // Skip weekends
        axisX.setSkipWeekends(true);
        axisX.setTransform(axisX.TRANSFORM_CUSTOM);
        axisX.setCustomTransform(new TransformDate());
        
        GregorianCalendar t[] = {
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 10),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 11),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 12),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 15),
            new GregorianCalendar(2001, GregorianCalendar.OCTOBER, 16)
        };
        double x[] = new double[5];
        for (int k = 0;  k < x.length;  k++)  x[k] = t[k].getTime().getTime();
        double y[] = {4, 2, 3, 9, 2};
        new Data(axis, x, y);
        
        axis.setViewport(0.2, 0.9, 0.1, 0.7);
    }
    
    public static void main(String argv[]) {
        new SampleWeekend().setVisible(true);
    }
}

String Labels

In this example the program specifies the strings to be used as the x-axis labels. The strings are specified using the setLabels(String[]) method.

The strings are used as labels at the major tick marks. The setLabels(String[]) method sets the Number attribute to the number of strings. In the example the AutoscaleOutput attribute is set to AUTOSCALE_WINDOW. Its default value is AUTOSCALE_WINDOW | AUTOSCALE_NUMBER, so the effect is to keep autoscaling to set the window range, but not to change the number of major tick marks.

(Download Code)
import com.imsl.chart.*;

public class SampleAxisLabelStrings extends JFrameChart {
    
    public SampleAxisLabelStrings() {
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);
        double y[] = {4, 2, 3, 9};
        new Data(axis, y);
        
        String labels[] = {"A", "B", "C", "D"};
        axis.getAxisX().getAxisLabel().setLabels(labels);
        axis.getAxisX().setAutoscaleOutput(ChartNode.AUTOSCALE_WINDOW);
    }
    
    public static void main(String argv[]) {
        new SampleAxisLabelStrings().setVisible(true);
    }
}


©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page