Restricting the number of category axis labels.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Martin Smith

Restricting the number of category axis labels.

Post by Martin Smith » Wed May 01, 2002 8:51 pm

Is this a bug or a feature? I am trying to create a line chart with a large number of horizontal values. I want to programatically manage the horizontalCategoryAxis labels to ensure fit. I tried to do this by setting intervening labels to "". This results in the chart plotting the value at the first category assigned "" as a labels for EVERY other "" label.

I have attached code that shows the problem. Basically what I would like to see is one of the following.

1) The ability to set a category label to ""
2) The ability to set a tick interval ie 3. this would indicate to only the first then every third label after that.


---------------------------- code follows --------------

package com.jrefinery.chart.demo;

import java.awt.Paint;
import java.awt.Color;
import java.awt.Stroke;
import java.awt.BasicStroke;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChartPanel;
import com.jrefinery.chart.CategoryPlot;
import com.jrefinery.chart.NumberAxis;
import com.jrefinery.chart.TickUnits;

/**
* A simple demonstration application showing how to create a line chart using data from a
* CategoryDataset.
*/
public class LineChartDemo1Broken extends ApplicationFrame {

/** The data. */
protected CategoryDataset data;

/**
* Default constructor.
*/
public LineChartDemo1Broken() {

// create a dataset...
double[][] data = new double[][] {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{ 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13, 14, 15, 16},
{ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
};

DefaultCategoryDataset dataset = new DefaultCategoryDataset(data);

// set the series names...
String[] seriesNames = new String[] { "First", "Second", "Third" };
dataset.setSeriesNames(seriesNames);

// set the category names...
String[] categories = new String[] { "Type 1", "","Type 3", "",
"Type 5", "","Type 7", "",
"Type 9","","Type 11", "",
"Type 13","","Type 15" };
dataset.setCategories(categories);

// create the chart...
JFreeChart chart = ChartFactory.createLineChart("Line Chart Demo 1", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
true // include legend
);

// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

// set the background color for the chart...
chart.setBackgroundPaint(Color.yellow);

// get a reference to the plot for further customisation...
CategoryPlot plot = chart.getCategoryPlot();

// set the color for each series...
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red });

// set the stroke for each series...
Stroke[] seriesStrokeArray = new Stroke[3];
seriesStrokeArray[0] = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
0.0f, new float[] { 10.0f, 0.0f }, 0.0f);
seriesStrokeArray[1] = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER,
1.0f, new float[] { 6.0f, 0.0f }, 0.0f);
seriesStrokeArray[2] = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
1.0f, new float[] { 2.0f, 0.0f }, 0.0f);


plot.setSeriesStroke(seriesStrokeArray);

// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setStandardTickUnits(TickUnits.createIntegerTickUnits());

// OPTIONAL CUSTOMISATION COMPLETED.

// add the chart to a panel...
JFreeChartPanel chartPanel = new JFreeChartPanel(chart);
this.setContentPane(chartPanel);

}

/**
* Starting point for the demonstration application.
*/
public static void main(String[] args) {

LineChartDemo1Broken demo = new LineChartDemo1Broken();
demo.pack();
demo.setVisible(true);

}

}

David Gilbert

Re: Restricting the number of category axis labels.

Post by David Gilbert » Fri May 03, 2002 10:39 am

The category (which can be any java.lang.Object) is a key for accessing the data, so the categories need to be unique. And the category labels are being generated using the toString() method, which is available for all Objects.

There's currently no way to suppress particular category labels. You could add an array of booleans to the category axis, and cycle through the array to decide whether or not a category should be displayed.

But in general it is my view that if you need to hide category labels then:

(a) your categories are probably value (date or number) based and you would be better off using XYPlot; or

(b) you have too many categories.

Regards,

DG.

Martin Smith

Re: Restricting the number of category axis labels.

Post by Martin Smith » Fri May 03, 2002 5:52 pm

Actually my "category" is a timeseries, the problem is that due to issues of ex-ante and ex-post data I cannot use the standard calendar and therefore your Quarter,Month etc classes. I guess I need to create my own class that extends TimePeriod and use that to build timeseries charts.

Thanks for your help

Martin Smith

Mark Perez

Re: Restricting the number of category axis labels.

Post by Mark Perez » Sun Dec 01, 2002 11:04 pm

Martin,

Did you ever extend TimePeriod and create a class that handled your "category". I think i have the same problem...i need a class that will hold and display the date class(day and hour). I think i am going to work on extending my own class to do this, but i want to check with you just in case you already had created something.

Thanks

Locked