How to switch from log 10 to log 2 in jfree [duplicate]

Deadly 提交于 2019-12-25 05:36:24

问题


I am using JFree to generate my chart. Below is the image for your reference. As visible in image current scale is based on 10 so 10,100,1000 are there in x-axis scale. Would it possible to change it to log 2. So in case of log 2 point would be visible 2,4,8,16,32, 64 and so on. Class LogarithmicAxis.java is being used for rendering x-axis.

Please let me know if its possible

Below code generate log 2 scale but I am not able to set x-axis point vertically which is very important for me.

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:


Try using the LogAxis class rather than the LogarithmicAxis class (there are two, for historical reasons) and call axis.setBase(2.0) when you set up the axis.



来源:https://stackoverflow.com/questions/35631087/how-to-switch-from-log-10-to-log-2-in-jfree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!