如何在jfree中从log 10切换到log 2

时间:2016-02-25 15:09:03

标签: java graph jfreechart

我正在使用JFree生成我的图表。以下是供您参考的图片。如图所示,电流刻度基于10,因此10,100,1000存在x轴刻度。是否可以将其更改为log 2.因此,在log 2点的情况下,可以看到2,4,8,16,32,64等等。 LogarithmicAxis.java类用于渲染x轴。

请告诉我是否可能

enter image description here

下面的代码生成log 2比例,但我无法垂直设置x轴点,这对我来说非常重要。

public class TestHello {

/** @see http://stackoverflow.com/a/10353270/230513 */
private static void createFrame() {
    int N=22;
    XYSeries series = new XYSeries("Series");
    for (int i = 0; i <= N; i++) {
        System.out.println(Math.pow(2, i));
        Random r = new Random();
        double randomInt = r.nextInt(100) + 1;
        series.add(Math.pow(2, i),randomInt);
    }


    NumberAxis yAxis = new NumberAxis("Y");
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    LogAxis xAxis = new LogAxis("X");
    xAxis.setBase(2);
    xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    xAxis.setVerticalTickLabels(true);




      JFreeChart chart = ChartFactory.createXYLineChart(
                "Text", "x", "y", new XYSeriesCollection(series),
                PlotOrientation.VERTICAL, true, true, false);
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainAxis(xAxis);
        plot.setRangeAxis(yAxis);

        final Marker start = new ValueMarker(60.0);

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setLegendLine(new Line2D.Double(-20.0D, 0.0D, 20.0D, 0.0D));

        Shape square = new Rectangle2D.Double(-2.0, -2.0, 3.0, 3.0);
        renderer.setSeriesShape(0, square);
        plot.setRenderer(renderer);
        plot.addRangeMarker(start);

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

}

1 个答案:

答案 0 :(得分:2)

尝试使用LogAxis类而不是LogarithmicAxis类(由于历史原因,有两个类),并在设置轴时调用axis.setBase(2.0)