• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Customizing Y Axis in JFreechart

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}

}
 
Marshal
Posts: 66504
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The answer to the above query was SymbolAxis class by which we can set the String in Y Axis.Hope it helps somebody.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!