aspose file tools*
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Customizing Y Axis in JFreechart" Watch "Customizing Y Axis in JFreechart" New topic
Author

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.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
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) {

super(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",
"X",
"Y",
data,
PlotOrientation.VERTICAL,
true,
true,
false
);

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));
axis.setRange(0,11);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);

}
public static void main(final String[] args)
{
final XYSeriesDemo demo = new XYSeriesDemo("XY Series Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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
Hi

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: http://aspose.com/file-tools
 
subject: Customizing Y Axis in JFreechart
 
Similar Threads
Problems with writing image on server side
calling an application frame with another application frame using mouse actions
Cewolf Legend coming twice
Need help in pointing 2 Y values for a single date(X-axis) in JFreeChart
Problem in displaying waterfall chart with -ve values