JClass Chart
Frequently Asked Questions


This document contains a list of the questions asked most often about JClass Chart. We at KL Group keep track of support questions in order to update this list.


  1. How to Make a Plot Chart from a File
  2. How to Change the Chart Type at Run Time
  3. How to Specify a Header and Footer
  4. How to Change the Position of the Chart Legend
  5. How to Make a Bar Chart from a File
  6. How to Change Bar Colors
  7. How to Change Bar Appearance
  8. How to Make a Pie Chart from a File
  9. How to Change the Appearance of the Other Slice
  10. How to Attach an External Data Object to JClass Chart
  11. How to Modify External Data from JClass Chart
  12. How to Make External Data Update JClass Chart
  13. How to Make a Time-Labelled Axis
  14. How to Use Value Labels on an Axis
  15. How to Add a Second Y-Axis (for a Second Data Set)
  16. How to Invert the Axes for a Data View
  17. How to Put an Image in the Chart Header
  18. How to Put a URL in the Chart Footer
  19. How to Retrieve a Data Series by Name
  20. How to Enable Chart Actions


1. How to Make a Plot Chart from a File

JCChart c = new JCChart(JCChart.PLOT);
FileDataSource f = new FileDataSource("source.dat");
c.getDataView(0).setDataSource(f);


2. How to Change the Chart Type at Run Time

c.getDataView(0).setChartType(JCChart.PIE);


3. How to Specify a Header and Footer

JCTitle header = c.getHeader();
header.setIsShowing(true);
header.setText("Top of the Chart", false);

JCTitle footer = c.getFooter();
footer.setIsShowing(true);
footer.setText("Footsies", false);


4. How to Change the Position of the Chart Legend

c.getLegend().setAnchor(JCLegend.SOUTHEAST);


5. How to Make a Bar Chart from a File

JCChart c = new JCChart(JCChart.BAR);
FileDataSource f = new FileDataSource("source.dat");
c.getDataView(0).setDataSource(f);


6. How to Change Bar Colors

// Suppose there are 3 data series
ChartDataView data = c.getDataView(0);
data.getSeries(0).getStyle().getFillStyle().setColor(Color.black);
data.getSeries(1).getStyle().getFillStyle().setColor(Color.green);
data.getSeries(2).getStyle().getFillStyle().setColor(Color.blue);


7. How to Change Bar Appearance

// Get ChartStyle instance for data series
JCChartStyle style = c.getDataView(0).getSeries(0).getStyle();
style.getFillStyle().setPattern(FillStyle.DIAG_HATCHED);


8. How to Make a Pie Chart from a File

JCChart c = new JCChart(JCChart.PIE);
FileDataSource f = new FileDataSource("source.dat");
c.getDataView(0).setDataSource(f);


9. How to Change the Appearance of the Other Slice

JCPieChartFormat pie = c.getDataView(0).getPieChartFormat();

// Change other slice fill color
JCFillStyle pieFillStyle = pie.getOtherStyle().getFillStyle();
pieFillStyle.setColor(Color.cyan);

// All slices less than 10 percent of total will be placed in other
pie.setThresholdMethod(JCPieChartFormat.SLICE_CUTOFF);
pie.setThresholdValue(10.0);


10. How to Attach an External Data Object to JClass Chart

First, suppose there already exists an external data source called MyDataSource:
// MyDataSource reads data from a file and places it in a
// vector of vectors called dataStuff
class MyDataSource {
Vector dataStuff;
}

Use the Chartable interface to make MyDataSource into a JClass Chart data source.

// To make MyDataSource work with JClass Chart, add Chartable
class MyDataSource extends Chartable {
Vector dataStuff;

public int getDataInterpretation() {
  // Return format of the data
  return Chartable.ARRAY;
}

public java.lang.Object getDataItem(int row, int column) {
  if (dataStuff == null) return null;
  Object rval = null;
  try {
    rval = (Vector)(dataStuff.elementAt(row)).elementAt(column);
  } catch (Exception e) {
    rval = null;
  }
  return rval;
}

public java.util.Vector getRow(int row) {
  if (dataStuff == null) return null;
  java.util.Vector rval = null;
  try {
    rval = (Vector)(dataStuff.elementAt(row));
  } catch (Exception e) {
    rval = null;
  }
  return rval;
}

public int getNumRows() {
  int rval = 0;
  try {
    rval = dataStuff.size();
  } catch (Exception e) {
  return rval;
}

public String[] getPointLabels(int row) {
  // We don't care about point labels
  return null;
}

public String getSeriesName(int row) {
  // We don't care about series name
  return null;
}

public String getSeriesLabel(int row) {
  // Series label and name are the same
  return getSeriesName(row);
}

public String getName() {
  // Name of the data view
  return "MyData";
}
}

Finally, add the data source to JClass Chart. JClass Chart will take care of the rest.

MyDataSource me = new MyDataSource();
c.getDataView(0).setDataSource(me);


11. How to Modify External Data from JClass Chart

To allow JClass Chart to modify external data, implement EditableChartable instead of Chartable:

class MyModifiableDataSource implements EditableChartable {
Vector dataStuff;

// Same as before
public int getDataInterpretation() {
  // Return format of the data
  return Chartable.ARRAY;
}

// Same as before
public java.lang.Object getDataItem(int row, int column) {
  if (dataStuff == null) return null;
  Object rval = null;
  try {
    rval = (Vector)(dataStuff.elementAt(row)).elementAt(column);
  } catch (Exception e) {
    rval = null;
  }
  return rval;
}

// Same as before
public java.util.Vector getRow(int row) {
  if (dataStuff == null) return null;
  java.util.Vector rval = null;
  try {
    rval = (Vector)(dataStuff.elementAt(row));
  } catch (Exception e) {
    rval = null;
  }
  return rval;
}

// Same as before
public int getNumRows() {
  int rval = 0;
  try {
    rval = dataStuff.size();
  } catch (Exception e) {
  return rval;
}

// Same as before
public String[] getPointLabels(int row) {
  // We don't care about point labels
  return null;
}

// Same as before
public String getSeriesName(int row) {
  // We don't care about series name
  return null;
}

// Same as before
public String getName() {
  // Name of the data view
  return "MyData";
}

// Same as before
public String getSeriesLabel(int row) {
  // Series label and name are the same
  return getSeriesName(row);
}

// Set a single item in the data
public void setDataItem(int row, int column, java.lang.Object item) {
  try {
    if (lockDataItem(row, column)) {
      dataStuff.elementAt(row).setElementAt(item, column);
      unlockDataItem(row, column);
    }
  } catch (Exception, e) {
  }
}

}

As before, add the data source to JClass Chart. JClass Chart will take care of the rest.

MySmartDataSource me = new MySmartDataSource();
c.getDataView(0).setDataSource(me);


12. How to Make External Data Update JClass Chart

To make a data source that can update JClass Chart, the data source must extend the abstract class ChartDataModel. The methods that must be overridden are the same as the events that must be provided when implementing the EditableChartable interface.
class MySmartDataSource extends ChartDataModel {
Vector dataStuff;

// Same as MyModifiableDataSource
 public int getDataInterpretation() { }
public Object getDataItem(int row, int column) {}
public Vector getRow(int row) { }
public int getNumRows() { }
public String[] getPointLabels(int row) { }
public String getSeriesLabel(int row) { }
public String getSeriesName(int row) { }
public String getName() { }
public void setDataItem(int row, int column, java.lang.Object item) { }
}

Since MySmartDataSource inherits from ChartDataModel, it is an Observable object, and can make calls to notifyObservers() to inform all views of a change in data. Below are some example calls:

// New row #3
notifyObservers(new ChartDataModelUpdate(ChartDataModelUpdate.NEW_ROW,
                                         3, -1);
// Row #2 has changed
notifyObservers(new ChartDataModelUpdate(ChartDataModelUpdate.CHANGE_ROW, 
                                         2, -1);
// Value at (5,3) has changed
notifyObservers(new ChartDataModelUpdate(ChartDataModelUpdate.CHANGE_VALUE, 
                                         5, 3);
Note that the data is not sent with the update message. Instead, it is up to JClass Chart to retrieve the data. Note also that JClass Chart is prohibited from modifying the data during a notifyObservers() call.


13. How to Make a Time-Labelled Axis

JCAxis x = c.getDataView(0).getXAxis();
x.setAnnotationMethod(JCAxis.TIME_LABELS_AXIS);


14. How to Use Value Labels on an Axis

JCAxis y = c.getDataView(0).getYAxis();
y.setAnnotationMethod(JCAxis.VALUE_LABELS);
double runlen = y.getMax() - y.getMin();
double inc = runlen / 10;

for (int i = 1; i < 10; i++) {
  y.addValueLabel(new JCValueLabel(y.getMin() + inc*i,
                                                                   new String("Item " + i)));
} 
y.addValueLabel(new JCValueLabel(y.getMin(), new String("Start")));
y.addValueLabel(new JCValueLabel(y.getMax(), new String("End")));


15. How to Add a Second Y-Axis (for a Second Data Set)

JCAxis ax = new JCAxis(c.getChartArea(), true, JCAxis.VALUE);
c.getDataView(1).setYAxis(ax);


16. How to Invert the Axes for a Data View

c.getDataView(0).setInverted(true);


17. How to Put an Image in the Chart Header

c.getHeader().setText("[IMAGE=images/duke.gif]", true);


18. How to Put a URL in the Chart Footer

c.getFooter().setText("[HREF=http://www.klg.com]KL GROUP[/HREF]",
                          true);


19. How to Retrieve a Data Series by Name

ChartDataView data = c.getDataView(0);
ChartDataViewSeries series = data.getSeries(data.getSeriesIndex("Profit"));


20. How to Enable Chart Actions

// Shift-drag means zoom
c.setTrigger(0, new EventTrigger(Event.SHIFT_MASK, EventTrigger.ZOOM));
// Control-drag means translate
c.setTrigger(1, new EventTrigger(Event.CTRL_MASK, EventTrigger.TRANSLATE));
// Drag means edit
c.setTrigger(2, new EventTrigger(0, EventTrigger.EDIT));