File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Customizing Y Axis in JFreechart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Customizing Y Axis in JFreechart" Watch "Customizing Y Axis in JFreechart" New topic

Customizing Y Axis in JFreechart

Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
Hi all

i am constructing an xy plot with integer values in both the x axis and y axis.Y axis range has been set from 0 to 11 with interval of 1.I would like to append the String values as well to the Y axis for ex A 0,B 1,C 2.....K 11[or A,B,C,D ....K] etc .At present only the integer values from 0 to 11 is displayed.Is there any method in any class which takes String as first argument and double as second,since XYSeries.add() takes both as double for x and y axis values.Is this possible using JFreechart.My code is as follows

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class XYSeriesDemo extends ApplicationFrame
int [] val = { 0, 6, 0, 2, 4 , 3 , 3, 2 ,2 ,2};
final String[] type = {"A", "B", "C","D","E","F","G","H","I","J","K"};

public XYSeriesDemo(final String title) {

final XYSeries series = new XYSeries("Random Data");
for (int i = 0; i < val.length; i++)
series.add(i, val[i]);

final XYSeriesCollection data = new XYSeriesCollection(series);
final JFreeChart chart = ChartFactory.createXYLineChart(
"XY Series Demo",

XYPlot xyplot = chart.getXYPlot();
NumberAxis numberAxis = (NumberAxis) xyplot.getDomainAxis();
numberAxis.setTickUnit(new NumberTickUnit(1));

ValueAxis axis = xyplot.getDomainAxis();
axis = xyplot.getRangeAxis();
((NumberAxis) axis).setTickUnit(new NumberTickUnit(1));
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

public static void main(final String[] args)
final XYSeriesDemo demo = new XYSeriesDemo("XY Series Demo");

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45302
Please Use Code Tags when quoting code; it makes it easier to read.

If you want a method which takes (java.lang.String, double) as its arguments, you will probably find it easier to write your own. You can try going through the API index with ctrl-f for "java.lang.String, double" but it will take a long time.
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105

The answer to the above query was SymbolAxis class by which we can set the String in Y Axis.Hope it helps somebody.
I agree. Here's the link:
subject: Customizing Y Axis in JFreechart
It's not a secret anymore!