Setting date labels

Charts generated on top of the Graph class can automatically calculate and display date labels by setting a start date, and optionally the date increment type. If a date in the format 'mmddaaaa' is passed to the method Graph.setStartDate(String startDate), JetChart calculates subsequent dates, as many times as the length of the largest series values array.

By default, the date increment takes place in a daily basis. Additionaly, the increment type can be set to a weekly, monthly or yearly increment. The method used to set the date increment type is Graph.setDateIncrement(int dateIncrementType), where 'dateIncrementType' can assume one of the following values:

Graph.DAY_INCREMENT
Graph.WEEK_INCREMENT
Graph.MONTH_INCREMENT
Graph.YEAR_INCREMENT

There is another variation of the date increment method that is passed the increment value as a second parameter. The increment value is 1 by default.

It is also possible to display only the name of the months at the beginning of each monthly period, setting the method Graph.setMonthLabelsEnabled(isMonthLabelsEnabled) to true. This method only works in conjunction with the Graph.setStartDate(String startDate) method. If labels are displayed as months, the date increment type and increment value are reset to the default values(daily type and unitary increment).

The following application displays a line chart whose labels were automatically calculated by JetChart, in a weekly basis, starting at May 01,2002.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jinsight.jetchart.*;

public class Main extends JFrame  {

   public Main() {

        Graph graph=new Graph();
        graph.setTitle(new String[]{"The JetChart Library","Setting date labels"});
        
        graph.setStartDate("05012002");

        graph.setDateIncrement(Graph.WEEK_INCREMENT); // Sets a weekly increment
        
        graph.setVerticalLabelsEnabled(true);

        // Disables automatic scale and sets maximum, minimum and increment values.
        GraphSet graphSet=graph.getGraphSet(0);
        Scale scale=graphSet.getScale();
        scale.setAutoScaleEnabled(false);
        scale.setMaxValue(140);
        scale.setMinValue(90);
        scale.setIncrement(10);

        // Enables grid.
        Grid grid=graphSet.getGrid();
        grid.setEnabled(true);
        grid.setStyle(Grid.DASHED);
        grid.setColor(Color.lightGray);

        // Creates a line series
        double[] values={131.90,132.80,130.50,131.00,136.75,135.00,131.50,130.50,
                         132.40,133.30,130.90,123.40,122.25,118.00,120.00,116.10,
                         115.00,103.45,107.50,103.80,99.00,96.75,94.00,95.90,97.50,
                         98.40,100.20,102.00,110.40,104.50,108.70,107.75,107.45};

       LineSerie ls=new LineSerie(values,"Line series");
       ls.setColor(Color.blue);
       ls.setMarksEnabled(false);
       ls.setThickness(2);
       ls.setLegendEnabled(false);

       graph.addSerie(ls);

       Container ct=getContentPane();
       ct.add(graph);

       setSize(500,400);
       setVisible(true);
   }

   public static void main(String[] args) {
       new Main();
   }

}