0

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

enter image description here

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

}

user1520277
  • 59
  • 2
  • 11

1 Answers1

2

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.

David Gilbert
  • 4,427
  • 14
  • 22
  • Reason for not using LogAxis is, I am not able to set x-axis point vertically even after setting it to true which is very important factor for me. I have added sample code in question – user1520277 Feb 26 '16 at 19:12