Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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.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");

Posts: 70709
  • 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

The answer to the above query was SymbolAxis class by which we can set the String in Y Axis.Hope it helps somebody.
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic