Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

java - Logarithmic Axis Labels/Ticks Customization

I am using the JFreeChart API to generate some chart in my Java application. In one of my charts, I try to use the LogAxis object to make my y-axis a log-scale axis (A in the figure) by the following code:

LogAxis logAxis = new LogAxis("Price($)");
logAxis.setMinorTickMarksVisible(true);
logAxis.setAutoRange(true);
xyplot.setRangeAxis(logAxis);

enter image description here

Then I got a y-axis in log-scale with ticks like 10^n (like figure A). I want to make it like B, which is more intuitive to the user, and each interval represents different values, as shown in the figure, 2->4, 4->8, 8->16, the interval grows as 2^n. Something minor is that, the intervals are displaying equally wide even if they are representing different value. However, when O try to achieve this by the following code :

LogAxis logAxis = new LogAxis("Price($)");
logAxis.setBase(2);
logAxis.setTickUnit(new NumberTickUnit(2));
logAxis.setMinorTickMarksVisible(true);
logAxis.setAutoRange(true);
xyplot.setRangeAxis(logAxis);

What I get is something like figure C.

How can I achieve figure B?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you need is logAxis.setNumberFormatOverride(NumberFormat format);

EDIT: Since further help is needed... try this:

logAxis.setBase(10);
LogFormat format = new LogFormat(logAxis.getBase(), "", "", true);
logAxis.setNumberFormatOverride(format);

Here's a whole method that can be used to play with...:

  public static void main(String[] args) {
    XYSeries series = new XYSeries("");
    series.add(1, 10);
    series.add(2, 100);
    series.add(3, 1000);
    series.add(4, 10000);
    series.add(5, 100000);
    series.add(6, 1000000);

//    NumberAxis yAxis = new NumberAxis("");
    LogAxis yAxis = new LogAxis("");
    yAxis.setBase(10);
    LogFormat format = new LogFormat(yAxis.getBase(), "", "", true);
    yAxis.setNumberFormatOverride(format);
    XYPlot plot = new XYPlot(
      new XYSeriesCollection(series),
      new NumberAxis(""),
      yAxis,
      new XYLineAndShapeRenderer(true, false));
    JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

    JFrame frame = new JFrame("LogAxis Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ChartPanel(chart));
    frame.pack();
    frame.setVisible(true);
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...