对数轴标签/刻度定制 [英] Logarithmic Axis Labels/Ticks Customization

查看:160
本文介绍了对数轴标签/刻度定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JFreeChart API在我的Java应用程序中生成一些图表。在我的一个图表中,我尝试使用 LogAxis 对象通过以下代码使我的y轴成为对数刻度轴(图中的A):

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

然后我得到了一个y轴,对数刻度为10 ^ n(如图A)。我想让它像B一样,对用户来说更直观,每个区间代表不同的值,如图所示,2-> 4,4-> 8,8-> 16,间隔增长为2 ^ ñ。轻微的是,即使它们代表不同的值,间隔也显示相同的宽度。但是,当O尝试通过以下代码实现此目的时:

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

我得到的是如图C所示。

What I get is something like figure C.

如何实现图B?

推荐答案

即使您使用的是 LogAxis ,你可以指定整数滴答单位,如下面@ amaidment的变量所示。

Even though you're using a LogAxis, you can specify integer tick units, as shown in the variation of @amaidment's example below.

/** @see http://stackoverflow.com/a/10353270/230513 */
private static void createFrame() {
    XYSeries series = new XYSeries("Series");
    for (int i = 0; i <= N; i++) {
        series.add(i, Math.pow(2, i));
    }
    NumberAxis xAxis = new NumberAxis("X");
    xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    LogAxis yAxis = new LogAxis("Y");
    yAxis.setBase(2);
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    XYPlot plot = new XYPlot(new XYSeriesCollection(series),
        xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
    JFreeChart chart = new JFreeChart(
        "Chart", 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);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            createFrame();
        }
    });
}

这篇关于对数轴标签/刻度定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆