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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • 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.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: 70709
288
  • 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.
 
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic